____ _ __
/ __ )____ _____ | | / /___ ___________
/ __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
/ /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
/_____/\____/____/ |__/|__/\__,_/_/ /____/
A futuristic real-time strategy game.
This file is part of Bos Wars.
(C) Copyright 2001-2007 by the Bos Wars and Stratagus Project.
Distributed under the "GNU General Public License"00001 // ____ _ __ 00002 // / __ )____ _____ | | / /___ ___________ 00003 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ 00004 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) 00005 // /_____/\____/____/ |__/|__/\__,_/_/ /____/ 00006 // 00007 // A futuristic real-time strategy game. 00008 // This file is part of Bos Wars. 00009 // 00011 // 00012 // (c) Copyright 2007 by Jimmy Salmon 00013 // 00014 // This program is free software; you can redistribute it and/or modify 00015 // it under the terms of the GNU General Public License as published by 00016 // the Free Software Foundation; only version 2 of the License. 00017 // 00018 // This program is distributed in the hope that it will be useful, 00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 // GNU General Public License for more details. 00022 // 00023 // You should have received a copy of the GNU General Public License 00024 // along with this program; if not, write to the Free Software 00025 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00026 // 02111-1307, USA. 00027 // 00028 00030 00031 /*---------------------------------------------------------------------------- 00032 -- Includes 00033 ----------------------------------------------------------------------------*/ 00034 00035 #include "stratagus.h" 00036 00037 #include "unit_manager.h" 00038 #include "unit.h" 00039 #include "iolib.h" 00040 00041 00042 /*---------------------------------------------------------------------------- 00043 -- Variables 00044 ----------------------------------------------------------------------------*/ 00045 00046 CUnit *UnitSlots[MAX_UNIT_SLOTS]; 00047 unsigned int UnitSlotFree; 00048 00049 CUnitManager UnitManager; 00050 00051 00052 /*---------------------------------------------------------------------------- 00053 -- Functions 00054 ----------------------------------------------------------------------------*/ 00055 00059 void CUnitManager::Init() 00060 { 00061 // Release memory of units in release list. 00062 while (!ReleasedUnits.empty()) { 00063 CUnit *unit = ReleasedUnits.front(); 00064 ReleasedUnits.pop_front(); 00065 delete unit; 00066 } 00067 00068 // Initialize the free unit slots 00069 memset(UnitSlots, 0, MAX_UNIT_SLOTS * sizeof(*UnitSlots)); 00070 UnitSlotFree = 0; 00071 } 00072 00078 CUnit *CUnitManager::AllocUnit() 00079 { 00080 CUnit *unit = NoUnitP; 00081 00082 // 00083 // Can use released unit? 00084 // 00085 if (!ReleasedUnits.empty() && ReleasedUnits.front()->Refs < GameCycle) { 00086 unit = ReleasedUnits.front(); 00087 ReleasedUnits.pop_front(); 00088 int slot = unit->Slot; 00089 unit->Init(); 00090 unit->Slot = slot; 00091 } else { 00092 unit = new CUnit; 00093 if (!unit) { 00094 fprintf(stderr, "Out of memory\n"); 00095 return NoUnitP; 00096 } 00097 UnitSlots[UnitSlotFree] = unit; 00098 unit->Slot = UnitSlotFree; 00099 UnitSlotFree++; 00100 } 00101 00102 return unit; 00103 } 00104 00110 void CUnitManager::ReleaseUnit(CUnit *unit) 00111 { 00112 ReleasedUnits.push_back(unit); 00113 unit->Refs = GameCycle + 500; // can be reused after this time 00114 } 00115 00121 void CUnitManager::Save(CFile *file) 00122 { 00123 file->printf("SlotUsage(%d", UnitSlotFree); 00124 00125 std::list<CUnit *>::iterator it = ReleasedUnits.begin(); 00126 for (; it != ReleasedUnits.end(); ++it) { 00127 file->printf(", {Slot = %d, FreeCycle = %lu}", (*it)->Slot, (*it)->Refs); 00128 } 00129 file->printf(")\n"); 00130 } 00131 00132
1.5.6