$treeview $search $mathjax
Stratagus
2.2.7
$projectbrief
|
$projectbrief
|
$searchbox |
_________ __ __ / _____// |_____________ _/ |______ ____ __ __ ______ \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > \/ \/ \//_____/ \/ ______________________ ______________________ T H E W A R B E G I N S Stratagus - A free fantasy real time strategy game engine
00001 // _________ __ __ 00002 // / _____// |_____________ _/ |______ ____ __ __ ______ 00003 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 00004 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 00005 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 00006 // \/ \/ \//_____/ \/ 00007 // ______________________ ______________________ 00008 // T H E W A R B E G I N S 00009 // Stratagus - A free fantasy real time strategy game engine 00010 // 00012 // 00013 // (c) Copyright 2008 by Rafal Bursig 00014 // 00015 // This program is free software; you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation; only version 2 of the License. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 // GNU General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 00029 #ifndef __UNIT_CACHE_H__ 00030 #define __UNIT_CACHE_H__ 00031 00033 00034 /*---------------------------------------------------------------------------- 00035 -- Includes 00036 ----------------------------------------------------------------------------*/ 00037 00038 #include <vector> 00039 #include <algorithm> 00040 00041 /*---------------------------------------------------------------------------- 00042 -- Declarations 00043 ----------------------------------------------------------------------------*/ 00044 00045 class CUnit; 00046 class CMap; 00050 class CUnitCache 00051 { 00052 public: 00053 typedef std::vector<CUnit *>::iterator iterator; 00054 typedef std::vector<CUnit *>::const_iterator const_iterator; 00055 00056 public: 00057 CUnitCache() : Units() {} 00058 00059 size_t size() const { return Units.size(); } 00060 00061 void clear() { Units.clear(); } 00062 00063 const_iterator begin() const { return Units.begin(); } 00064 iterator begin() { return Units.begin(); } 00065 const_iterator end() const { return Units.end(); } 00066 iterator end() { return Units.end(); } 00067 00068 CUnit *operator[](const unsigned int index) const { 00069 //Assert(index < Units.size()); 00070 return Units[index]; 00071 } 00072 CUnit *operator[](const unsigned int index) { 00073 //Assert(index < Units.size()); 00074 return Units[index]; 00075 } 00076 00083 template<typename _T> 00084 CUnit *find(const _T &pred) const { 00085 std::vector<CUnit *>::const_iterator ret = std::find_if(Units.begin(), Units.end(), pred); 00086 00087 return ret != Units.end() ? (*ret) : NULL; 00088 } 00089 00098 template<typename _T> 00099 void for_each(const _T functor) { 00100 const size_t size = Units.size(); 00101 00102 for (size_t i = 0; i != size; ++i) { 00103 functor(Units[i]); 00104 } 00105 } 00106 00116 template<typename _T> 00117 int for_each_if(const _T &functor) { 00118 const size_t size = Units.size(); 00119 00120 for (size_t count = 0; count != size; ++count) { 00121 if (functor(Units[count]) == false) { 00122 return count; 00123 } 00124 } 00125 return size; 00126 } 00127 00128 00135 CUnit *Remove(const unsigned int index) { 00136 const size_t size = Units.size(); 00137 Assert(index < size); 00138 CUnit *tmp = Units[index]; 00139 if (size > 1) { 00140 Units[index] = Units[size - 1]; 00141 } 00142 Units.pop_back(); 00143 return tmp; 00144 } 00145 00151 bool Remove(CUnit *const unit) { 00152 #ifndef SECURE_UNIT_REMOVING 00153 const size_t size = Units.size(); 00154 if (size == 1 && unit == Units[0]) { 00155 Units.pop_back(); 00156 return true; 00157 } else { 00158 for (unsigned int i = 0; i < size; ++i) { 00159 // Do we care on unit sequence in tile cache ? 00160 if (Units[i] == unit) { 00161 Units[i] = Units[size - 1]; 00162 Units.pop_back(); 00163 return true; 00164 } 00165 } 00166 } 00167 #else 00168 for (std::vector<CUnit *>::iterator i(Units.begin()), end(Units.end()); i != end; ++i) { 00169 if ((*i) == unit) { 00170 Units.erase(i); 00171 return true; 00172 } 00173 } 00174 #endif 00175 return false; 00176 } 00177 00183 void RemoveS(CUnit *const unit) { 00184 for (std::vector<CUnit *>::iterator i(Units.begin()), end(Units.end()); i != end; ++i) { 00185 if ((*i) == unit) { 00186 Units.erase(i); 00187 return; 00188 } 00189 } 00190 } 00191 00199 bool InsertS(CUnit *unit) { 00200 if (!binary_search(Units.begin(), Units.end(), unit)) { 00201 Units.insert(std::lower_bound(Units.begin(), Units.end(), unit), unit); 00202 return true; 00203 } 00204 return false; 00205 } 00206 00212 void Insert(CUnit *unit) { 00213 Units.push_back(unit); 00214 } 00215 00216 public: 00217 std::vector<CUnit *> Units; 00218 }; 00219 00220 00222 00223 #endif // !__UNIT_CACHE_H__ 00224