Line data Source code
1 : /* Copyright (C) 2022 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : /*
19 : * Container that owns all units
20 : */
21 :
22 : #include "precompiled.h"
23 :
24 : #include <float.h>
25 :
26 : #include "Model.h"
27 : #include "UnitManager.h"
28 : #include "Unit.h"
29 : #include "ObjectManager.h"
30 : #include "ObjectEntry.h"
31 : #include "ps/Game.h"
32 : #include "ps/World.h"
33 :
34 : #include <algorithm>
35 :
36 : ///////////////////////////////////////////////////////////////////////////////
37 : // CUnitManager constructor
38 0 : CUnitManager::CUnitManager() :
39 0 : m_ObjectManager(NULL)
40 : {
41 0 : }
42 :
43 : ///////////////////////////////////////////////////////////////////////////////
44 : // CUnitManager destructor
45 0 : CUnitManager::~CUnitManager()
46 : {
47 0 : DeleteAll();
48 0 : }
49 :
50 :
51 : ///////////////////////////////////////////////////////////////////////////////
52 : // AddUnit: add given unit to world
53 0 : void CUnitManager::AddUnit(CUnit* unit)
54 : {
55 0 : m_Units.push_back(unit);
56 0 : }
57 :
58 : ///////////////////////////////////////////////////////////////////////////////
59 : // RemoveUnit: remove given unit from world, but don't delete it
60 0 : void CUnitManager::RemoveUnit(CUnit* unit)
61 : {
62 : // find entry in list
63 : typedef std::vector<CUnit*>::iterator Iter;
64 0 : Iter i=std::find(m_Units.begin(),m_Units.end(),unit);
65 0 : if (i!=m_Units.end()) {
66 0 : m_Units.erase(i);
67 : }
68 0 : }
69 :
70 : ///////////////////////////////////////////////////////////////////////////////
71 : // DeleteUnit: remove given unit from world and delete it
72 0 : void CUnitManager::DeleteUnit(CUnit* unit)
73 : {
74 0 : RemoveUnit(unit);
75 0 : delete unit;
76 0 : }
77 :
78 : ///////////////////////////////////////////////////////////////////////////////
79 : // DeleteAll: remove and delete all units
80 0 : void CUnitManager::DeleteAll()
81 : {
82 0 : for (size_t i=0;i<m_Units.size();i++) {
83 0 : delete m_Units[i];
84 : }
85 0 : m_Units.clear();
86 0 : }
87 :
88 : ///////////////////////////////////////////////////////////////////////////////
89 : // CreateUnit: create a new unit and add it to the world
90 0 : CUnit* CUnitManager::CreateUnit(const CStrW& actorName, uint32_t seed)
91 : {
92 0 : if (! m_ObjectManager)
93 0 : return NULL;
94 :
95 0 : CUnit* unit = CUnit::Create(actorName, seed, *m_ObjectManager);
96 0 : if (unit)
97 0 : AddUnit(unit);
98 0 : return unit;
99 : }
100 :
101 0 : void CUnitManager::MakeTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags)
102 : {
103 0 : if (!(dirtyFlags & RENDERDATA_UPDATE_VERTICES))
104 0 : return;
105 0 : for (CUnit* unit : m_Units)
106 0 : unit->GetModel().SetTerrainDirty(i0, j0, i1, j1);
107 3 : }
|