2025-07-16 11:25:46 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2004-06-03 11:38:14 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2025-07-16 11:25:46 -07:00
|
|
|
#include "graphics/ModelAbstract.h"
|
|
|
|
|
#include "graphics/RenderableObject.h"
|
2023-08-28 11:01:44 -07:00
|
|
|
#include "graphics/Unit.h"
|
|
|
|
|
#include "graphics/UnitManager.h"
|
2005-01-23 13:56:00 -08:00
|
|
|
|
2004-05-29 17:46:58 -07:00
|
|
|
#include <algorithm>
|
2025-07-16 11:25:46 -07:00
|
|
|
#include <cstddef>
|
|
|
|
|
#include <utility>
|
2004-05-29 17:46:58 -07:00
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// CUnitManager constructor
|
2010-06-30 14:41:04 -07:00
|
|
|
CUnitManager::CUnitManager() :
|
|
|
|
|
m_ObjectManager(NULL)
|
2004-05-29 17:46:58 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// CUnitManager destructor
|
2023-10-28 05:57:01 -07:00
|
|
|
CUnitManager::~CUnitManager() = default;
|
2004-05-29 17:46:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// AddUnit: add given unit to world
|
2023-10-28 05:57:01 -07:00
|
|
|
CUnit* CUnitManager::AddUnit(std::unique_ptr<CUnit> unit)
|
2004-05-29 17:46:58 -07:00
|
|
|
{
|
2023-10-28 05:57:01 -07:00
|
|
|
return m_Units.emplace_back(std::move(unit)).get();
|
2004-05-29 17:46:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-10-06 11:45:59 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// DeleteUnit: remove given unit from world and delete it
|
|
|
|
|
void CUnitManager::DeleteUnit(CUnit* unit)
|
|
|
|
|
{
|
2023-10-28 05:57:01 -07:00
|
|
|
const auto it = std::find_if(m_Units.begin(), m_Units.end(), [&](const std::unique_ptr<CUnit>& elem)
|
|
|
|
|
{
|
|
|
|
|
return elem.get() == unit;
|
|
|
|
|
});
|
2004-10-06 11:45:59 -07:00
|
|
|
|
2023-10-28 05:57:01 -07:00
|
|
|
if (it != m_Units.end())
|
|
|
|
|
m_Units.erase(it);
|
2004-05-29 17:46:58 -07:00
|
|
|
}
|
|
|
|
|
|
2005-03-19 03:55:27 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// CreateUnit: create a new unit and add it to the world
|
2023-08-28 11:01:44 -07:00
|
|
|
CUnit* CUnitManager::CreateUnit(const CStrW& actorName, const entity_id_t id, const uint32_t seed)
|
2005-03-19 03:55:27 -08:00
|
|
|
{
|
2023-10-28 05:57:01 -07:00
|
|
|
if (!m_ObjectManager)
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CUnit> unit{CUnit::Create(actorName, id, seed, *m_ObjectManager)};
|
|
|
|
|
if (!unit)
|
|
|
|
|
return nullptr;
|
2007-01-07 17:56:46 -08:00
|
|
|
|
2023-10-28 05:57:01 -07:00
|
|
|
return AddUnit(std::move(unit));
|
2005-10-07 08:24:29 -07:00
|
|
|
}
|
2022-01-29 00:28:04 -08:00
|
|
|
|
|
|
|
|
void CUnitManager::MakeTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags)
|
|
|
|
|
{
|
|
|
|
|
if (!(dirtyFlags & RENDERDATA_UPDATE_VERTICES))
|
|
|
|
|
return;
|
2023-10-28 05:57:01 -07:00
|
|
|
for (const std::unique_ptr<CUnit>& unit : m_Units)
|
2022-01-29 00:28:04 -08:00
|
|
|
unit->GetModel().SetTerrainDirty(i0, j0, i1, j1);
|
|
|
|
|
}
|