$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 1998-2012 by Lutz Sammer, Jimmy Salmon and Joris Dauphin 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 00030 #ifndef __UNIT_FIND_H__ 00031 #define __UNIT_FIND_H__ 00032 00034 00035 #include "map.h" 00036 #include "pathfinder.h" 00037 #include "unit.h" 00038 #include "unittype.h" 00039 00040 /*---------------------------------------------------------------------------- 00041 -- Declarations 00042 ----------------------------------------------------------------------------*/ 00043 00044 // 00045 // Some predicates 00046 // 00047 00048 class HasSameTypeAs 00049 { 00050 public: 00051 explicit HasSameTypeAs(const CUnitType &_type) : type(&_type) {} 00052 bool operator()(const CUnit *unit) const { return unit->Type == type; } 00053 private: 00054 const CUnitType *type; 00055 }; 00056 00057 class HasSamePlayerAs 00058 { 00059 public: 00060 explicit HasSamePlayerAs(const CPlayer &_player) : player(&_player) {} 00061 bool operator()(const CUnit *unit) const { return unit->Player == player; } 00062 private: 00063 const CPlayer *player; 00064 }; 00065 00066 class HasNotSamePlayerAs 00067 { 00068 public: 00069 explicit HasNotSamePlayerAs(const CPlayer &_player) : player(&_player) {} 00070 bool operator()(const CUnit *unit) const { return unit->Player != player; } 00071 private: 00072 const CPlayer *player; 00073 }; 00074 00075 class IsAlliedWith 00076 { 00077 public: 00078 explicit IsAlliedWith(const CPlayer &_player) : player(&_player) {} 00079 bool operator()(const CUnit *unit) const { return unit->IsAllied(*player); } 00080 private: 00081 const CPlayer *player; 00082 }; 00083 00084 class IsEnemyWith 00085 { 00086 public: 00087 explicit IsEnemyWith(const CPlayer &_player) : player(&_player) {} 00088 bool operator()(const CUnit *unit) const { return unit->IsEnemy(*player); } 00089 private: 00090 const CPlayer *player; 00091 }; 00092 00093 class HasSamePlayerAndTypeAs 00094 { 00095 public: 00096 explicit HasSamePlayerAndTypeAs(const CUnit &unit) : 00097 player(unit.Player), type(unit.Type) 00098 {} 00099 HasSamePlayerAndTypeAs(const CPlayer &_player, const CUnitType &_type) : 00100 player(&_player), type(&_type) 00101 {} 00102 00103 bool operator()(const CUnit *unit) const { 00104 return (unit->Player == player && unit->Type == type); 00105 } 00106 00107 private: 00108 const CPlayer *player; 00109 const CUnitType *type; 00110 }; 00111 00112 class IsNotTheSameUnitAs 00113 { 00114 public: 00115 explicit IsNotTheSameUnitAs(const CUnit &unit) : forbidden(&unit) {} 00116 bool operator()(const CUnit *unit) const { return unit != forbidden; } 00117 private: 00118 const CUnit *forbidden; 00119 }; 00120 00121 class IsBuildingType 00122 { 00123 public: 00124 bool operator()(const CUnit *unit) const { return unit->Type->Building; } 00125 }; 00126 00127 00128 template <typename Pred> 00129 class NotPredicate 00130 { 00131 public: 00132 explicit NotPredicate(Pred _pred) : pred(_pred) {} 00133 bool operator()(const CUnit *unit) const { return pred(unit) == false; } 00134 private: 00135 Pred pred; 00136 }; 00137 00138 template <typename Pred> 00139 NotPredicate<Pred> MakeNotPredicate(Pred pred) { return NotPredicate<Pred>(pred); } 00140 00141 template <typename Pred1, typename Pred2> 00142 class AndPredicate 00143 { 00144 public: 00145 AndPredicate(Pred1 _pred1, Pred2 _pred2) : pred1(_pred1), pred2(_pred2) {} 00146 bool operator()(const CUnit *unit) const { return pred1(unit) && pred2(unit); } 00147 private: 00148 Pred1 pred1; 00149 Pred2 pred2; 00150 }; 00151 00152 template <typename Pred1, typename Pred2> 00153 AndPredicate<Pred1, Pred2> MakeAndPredicate(Pred1 pred1, Pred2 pred2) { return AndPredicate<Pred1, Pred2>(pred1, pred2); } 00154 00155 00156 //unit_find 00157 class CUnitTypeFinder 00158 { 00159 public: 00160 explicit CUnitTypeFinder(const UnitTypeType t) : unitTypeType(t) {} 00161 bool operator()(const CUnit *const unit) const { 00162 const CUnitType &type = *unit->Type; 00163 if (type.Vanishes || (unitTypeType != static_cast<UnitTypeType>(-1) && type.UnitType != unitTypeType)) { 00164 return false; 00165 } 00166 return true; 00167 } 00168 private: 00169 const UnitTypeType unitTypeType; 00170 }; 00171 00172 00173 class UnitFinder 00174 { 00175 public: 00176 UnitFinder(const CPlayer &player, const std::vector<CUnit *> &units, int maxDist, int movemask, CUnit **unitP) : 00177 player(player), units(units), maxDist(maxDist), movemask(movemask), unitP(unitP) {} 00178 VisitResult Visit(TerrainTraversal &terrainTraversal, const Vec2i &pos, const Vec2i &from); 00179 private: 00180 CUnit *FindUnitAtPos(const Vec2i &pos) const; 00181 private: 00182 const CPlayer &player; 00183 const std::vector<CUnit *> &units; 00184 int maxDist; 00185 int movemask; 00186 CUnit **unitP; 00187 }; 00188 00189 void Select(const Vec2i <Pos, const Vec2i &rbPos, std::vector<CUnit *> &units); 00190 void SelectFixed(const Vec2i <Pos, const Vec2i &rbPos, std::vector<CUnit *> &units); 00191 void SelectAroundUnit(const CUnit &unit, int range, std::vector<CUnit *> &around); 00192 00193 template <typename Pred> 00194 void SelectFixed(const Vec2i <Pos, const Vec2i &rbPos, std::vector<CUnit *> &units, Pred pred) 00195 { 00196 Assert(Map.Info.IsPointOnMap(ltPos)); 00197 Assert(Map.Info.IsPointOnMap(rbPos)); 00198 Assert(units.empty()); 00199 00200 for (Vec2i posIt = ltPos; posIt.y != rbPos.y + 1; ++posIt.y) { 00201 for (posIt.x = ltPos.x; posIt.x != rbPos.x + 1; ++posIt.x) { 00202 const CMapField &mf = *Map.Field(posIt); 00203 const CUnitCache &cache = mf.UnitCache; 00204 00205 for (size_t i = 0; i != cache.size(); ++i) { 00206 CUnit &unit = *cache[i]; 00207 00208 if (unit.CacheLock == 0 && pred(&unit)) { 00209 unit.CacheLock = 1; 00210 units.push_back(&unit); 00211 } 00212 } 00213 } 00214 } 00215 for (size_t i = 0; i != units.size(); ++i) { 00216 units[i]->CacheLock = 0; 00217 } 00218 } 00219 00220 template <typename Pred> 00221 void SelectAroundUnit(const CUnit &unit, int range, std::vector<CUnit *> &around, Pred pred) 00222 { 00223 const Vec2i offset(range, range); 00224 const Vec2i typeSize(unit.Type->TileWidth - 1, unit.Type->TileHeight - 1); 00225 00226 Select(unit.tilePos - offset, 00227 unit.tilePos + typeSize + offset, around, 00228 MakeAndPredicate(IsNotTheSameUnitAs(unit), pred)); 00229 } 00230 00231 template <typename Pred> 00232 void Select(const Vec2i <Pos, const Vec2i &rbPos, std::vector<CUnit *> &units, Pred pred) 00233 { 00234 Vec2i minPos = ltPos; 00235 Vec2i maxPos = rbPos; 00236 00237 Map.FixSelectionArea(minPos, maxPos); 00238 SelectFixed(minPos, maxPos, units, pred); 00239 } 00240 00241 template <typename Pred> 00242 CUnit *FindUnit_IfFixed(const Vec2i <Pos, const Vec2i &rbPos, Pred pred) 00243 { 00244 Assert(Map.Info.IsPointOnMap(ltPos)); 00245 Assert(Map.Info.IsPointOnMap(rbPos)); 00246 00247 for (Vec2i posIt = ltPos; posIt.y != rbPos.y + 1; ++posIt.y) { 00248 for (posIt.x = ltPos.x; posIt.x != rbPos.x + 1; ++posIt.x) { 00249 const CMapField &mf = *Map.Field(posIt); 00250 const CUnitCache &cache = mf.UnitCache; 00251 00252 CUnitCache::const_iterator it = std::find_if(cache.begin(), cache.end(), pred); 00253 if (it != cache.end()) { 00254 return *it; 00255 } 00256 } 00257 } 00258 return NULL; 00259 } 00260 00261 template <typename Pred> 00262 CUnit *FindUnit_If(const Vec2i <Pos, const Vec2i &rbPos, Pred pred) 00263 { 00264 Vec2i minPos = ltPos; 00265 Vec2i maxPos = rbPos; 00266 00267 Map.FixSelectionArea(minPos, maxPos); 00268 return FindUnit_IfFixed(minPos, maxPos, pred); 00269 } 00270 00272 extern CUnit *UnitFindResource(const CUnit &unit, const CUnit &startUnit, int range, 00273 int resource, bool check_usage = false, const CUnit *deposit = NULL); 00274 00276 extern CUnit *FindDeposit(const CUnit &unit, int range, int resource); 00278 extern CUnit *FindIdleWorker(const CPlayer &player, const CUnit *last); 00279 00281 extern bool FindTerrainType(int movemask, int resmask, int range, 00282 const CPlayer &player, const Vec2i &startPos, Vec2i *pos); 00283 00284 extern void FindUnitsByType(const CUnitType &type, std::vector<CUnit *> &units); 00285 00287 extern void FindPlayerUnitsByType(const CPlayer &player, const CUnitType &type, std::vector<CUnit *> &units); 00289 extern CUnit *UnitOnMapTile(const Vec2i &pos, unsigned int type);// = -1); 00291 extern CUnit *TargetOnMap(const CUnit &unit, const Vec2i &pos1, const Vec2i &pos2); 00292 00294 extern CUnit *ResourceOnMap(const Vec2i &pos, int resource, bool mine_on_top = true); 00296 extern CUnit *ResourceDepositOnMap(const Vec2i &pos, int resource); 00297 00299 extern CUnit *AttackUnitsInDistance(const CUnit &unit, int range, bool onlyBuildings = false); 00301 extern CUnit *AttackUnitsInRange(const CUnit &unit); 00303 extern CUnit *AttackUnitsInReactRange(const CUnit &unit); 00304 00306 00307 #endif // !__UNIT_FIND_H__