Debian Codegraph @

engine/unit/unit_manager.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name unit_manager.cpp - Unit manager. */
11 //
12 // (c) Copyright 2007-2014 by Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include "stratagus.h"
36
37 #include "unit_manager.h"
38 #include "unit.h"
39 #include "iolib.h"
40
41
42 /*----------------------------------------------------------------------------
43 -- Variables
44 ----------------------------------------------------------------------------*/
45
46 CUnit *UnitSlots[MAX_UNIT_SLOTS]; /// All possible units
47 unsigned int UnitSlotFree; /// First free unit slot
48
49 CUnitManager UnitManager; /// Unit manager
50
51
52 /*----------------------------------------------------------------------------
53 -- Functions
54 ----------------------------------------------------------------------------*/
55
56 /**
57 ** Initial memory allocation for units.
58 */
59 void CUnitManager::Init()
60 {
61 // Release memory of units in release list.
62 while (!ReleasedUnits.empty()) {
63 CUnit *unit = ReleasedUnits.front();
64 ReleasedUnits.pop_front();
65 delete unit;
66 }
67
68 // Initialize the free unit slots
69 memset(UnitSlots, 0, MAX_UNIT_SLOTS * sizeof(*UnitSlots));
70 UnitSlotFree = 0;
71 }
72
73 /**
74 ** Allocate a new unit
75 **
76 ** @return New unit
77 */
78 CUnit *CUnitManager::AllocUnit()
79 {
80 CUnit *unit = NoUnitP;
81
82 //
83 // Can use released unit?
84 //
85 if (!ReleasedUnits.empty() && ReleasedUnits.front()->Refs < GameCycle) {
86 unit = ReleasedUnits.front();
87 ReleasedUnits.pop_front();
88 int slot = unit->Slot;
89 unit->Init();
90 unit->Slot = slot;
91 } else {
92 unit = new CUnit;
93 if (!unit) {
94 fprintf(stderr, "Out of memory\n");
95 return NoUnitP;
96 }
97 UnitSlots[UnitSlotFree] = unit;
98 unit->Slot = UnitSlotFree;
99 UnitSlotFree++;
100 }
101
102 return unit;
103 }
104
105 /**
106 ** Release a unit
107 **
108 ** @param unit Unit to release
109 */
110 void CUnitManager::ReleaseUnit(CUnit *unit)
111 {
112 ReleasedUnits.push_back(unit);
113 unit->Refs = GameCycle + 500; // can be reused after this time
114 }
115
116 /**
117 ** Save state of unit manager to file.
118 **
119 ** @param file Output file.
120 */
121 void CUnitManager::Save(CFile *file)
122 {
123 file->printf("SlotUsage(%d, {", UnitSlotFree);
124
125 std::list<CUnit *>::iterator it = ReleasedUnits.begin();
126 for (; it != ReleasedUnits.end(); ++it) {
127 if (it != ReleasedUnits.begin()) {
128 file->printf(", ");
129 }
130 file->printf("{Slot = %d, FreeCycle = %lu}", (*it)->Slot, (*it)->Refs);
131 }
132 file->printf("})\n");
133 }
134
135
136 //@}