_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

unit_find.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 1998-2012 by Lutz Sammer, Jimmy Salmon and Joris Dauphin
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef __UNIT_FIND_H__
31 #define __UNIT_FIND_H__
32 
34 
35 #include "map.h"
36 #include "pathfinder.h"
37 #include "unit.h"
38 #include "unittype.h"
39 
40 /*----------------------------------------------------------------------------
41 -- Declarations
42 ----------------------------------------------------------------------------*/
43 
44 //
45 // Some predicates
46 //
47 
49 {
50 public:
51  bool operator()(const CUnit *unit) const { return true; };
52 };
53 
54 class NoFilter : public CUnitFilter
55 {
56 public:
57  bool operator()(const CUnit *) const { return true; }
58 };
59 
60 class HasSameTypeAs : public CUnitFilter
61 {
62 public:
63  explicit HasSameTypeAs(const CUnitType &_type) : type(&_type) {}
64  bool operator()(const CUnit *unit) const { return unit->Type == type; }
65 private:
66  const CUnitType *type;
67 };
68 
70 {
71 public:
72  explicit HasSamePlayerAs(const CPlayer &_player) : player(&_player) {}
73  bool operator()(const CUnit *unit) const { return unit->Player == player; }
74 private:
75  const CPlayer *player;
76 };
77 
79 {
80 public:
81  explicit HasNotSamePlayerAs(const CPlayer &_player) : player(&_player) {}
82  bool operator()(const CUnit *unit) const { return unit->Player != player; }
83 private:
84  const CPlayer *player;
85 };
86 
87 class IsAlliedWith : public CUnitFilter
88 {
89 public:
90  explicit IsAlliedWith(const CPlayer &_player) : player(&_player) {}
91  bool operator()(const CUnit *unit) const { return unit->IsAllied(*player); }
92 private:
93  const CPlayer *player;
94 };
95 
96 class IsEnemyWith : public CUnitFilter
97 {
98 public:
99  explicit IsEnemyWith(const CPlayer &_player) : player(&_player) {}
100  bool operator()(const CUnit *unit) const { return unit->IsEnemy(*player); }
101 private:
102  const CPlayer *player;
103 };
104 
106 {
107 public:
108  explicit HasSamePlayerAndTypeAs(const CUnit &unit) :
109  player(unit.Player), type(unit.Type)
110  {}
111  HasSamePlayerAndTypeAs(const CPlayer &_player, const CUnitType &_type) :
112  player(&_player), type(&_type)
113  {}
114 
115  bool operator()(const CUnit *unit) const
116  {
117  return (unit->Player == player && unit->Type == type);
118  }
119 
120 private:
121  const CPlayer *player;
122  const CUnitType *type;
123 };
124 
126 {
127 public:
128  explicit IsNotTheSameUnitAs(const CUnit &unit) : forbidden(&unit) {}
129  bool operator()(const CUnit *unit) const { return unit != forbidden; }
130 private:
131  const CUnit *forbidden;
132 };
133 
135 {
136 public:
137  bool operator()(const CUnit *unit) const { return unit->Type->Building; }
138 };
139 
141 {
142 public:
143  bool operator()(const CUnit *unit) const { return unit->IsAgressive(); }
144 };
145 
147 {
148 public:
149  explicit OutOfMinRange(const int range, const Vec2i pos) : range(range), pos(pos) {}
150  bool operator()(const CUnit *unit) const { return unit->MapDistanceTo(pos) >= range; }
151 private:
152  int range;
153  Vec2i pos;
154 };
155 
156 
157 template <typename Pred>
158 class NotPredicate : public CUnitFilter
159 {
160 public:
161  explicit NotPredicate(Pred _pred) : pred(_pred) {}
162  bool operator()(const CUnit *unit) const { return pred(unit) == false; }
163 private:
164  Pred pred;
165 };
166 
167 template <typename Pred>
169 
170 template <typename Pred1, typename Pred2>
171 class AndPredicate : public CUnitFilter
172 {
173 public:
174  AndPredicate(Pred1 _pred1, Pred2 _pred2) : pred1(_pred1), pred2(_pred2) {}
175  bool operator()(const CUnit *unit) const { return pred1(unit) && pred2(unit); }
176 private:
177  Pred1 pred1;
178  Pred2 pred2;
179 };
180 
181 template <typename Pred1, typename Pred2>
182 AndPredicate<Pred1, Pred2> MakeAndPredicate(Pred1 pred1, Pred2 pred2) { return AndPredicate<Pred1, Pred2>(pred1, pred2); }
183 
184 
185 //unit_find
187 {
188 public:
189  explicit CUnitTypeFinder(const UnitTypeType t) : unitTypeType(t) {}
190  bool operator()(const CUnit *const unit) const
191  {
192  const CUnitType &type = *unit->Type;
193  if (type.Vanishes || (unitTypeType != static_cast<UnitTypeType>(-1) && type.UnitType != unitTypeType)) {
194  return false;
195  }
196  return true;
197  }
198 private:
199  const UnitTypeType unitTypeType;
200 };
201 
202 
204 {
205 public:
206  UnitFinder(const CPlayer &player, const std::vector<CUnit *> &units, int maxDist, int movemask, CUnit **unitP) :
207  player(player), units(units), maxDist(maxDist), movemask(movemask), unitP(unitP) {}
208  VisitResult Visit(TerrainTraversal &terrainTraversal, const Vec2i &pos, const Vec2i &from);
209 private:
210  CUnit *FindUnitAtPos(const Vec2i &pos) const;
211 private:
212  const CPlayer &player;
213  const std::vector<CUnit *> &units;
214  int maxDist;
215  int movemask;
216  CUnit **unitP;
217 };
218 
219 void Select(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units);
220 void SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units);
221 void SelectAroundUnit(const CUnit &unit, int range, std::vector<CUnit *> &around);
222 
223 template <typename Pred>
224 void SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units, Pred pred)
225 {
226  Assert(Map.Info.IsPointOnMap(ltPos));
227  Assert(Map.Info.IsPointOnMap(rbPos));
228  Assert(units.empty());
229 
230  for (Vec2i posIt = ltPos; posIt.y != rbPos.y + 1; ++posIt.y) {
231  for (posIt.x = ltPos.x; posIt.x != rbPos.x + 1; ++posIt.x) {
232  const CMapField &mf = *Map.Field(posIt);
233  const CUnitCache &cache = mf.UnitCache;
234 
235  for (size_t i = 0; i != cache.size(); ++i) {
236  CUnit &unit = *cache[i];
237 
238  if (unit.CacheLock == 0 && pred(&unit)) {
239  unit.CacheLock = 1;
240  units.push_back(&unit);
241  }
242  }
243  }
244  }
245  for (size_t i = 0; i != units.size(); ++i) {
246  units[i]->CacheLock = 0;
247  }
248 }
249 
250 template <typename Pred>
251 void SelectAroundUnit(const CUnit &unit, int range, std::vector<CUnit *> &around, Pred pred)
252 {
253  const Vec2i offset(range, range);
254  const Vec2i typeSize(unit.Type->TileWidth - 1, unit.Type->TileHeight - 1);
255 
256  Select(unit.tilePos - offset,
257  unit.tilePos + typeSize + offset, around,
258  MakeAndPredicate(IsNotTheSameUnitAs(unit), pred));
259 }
260 
261 template <typename Pred>
262 void Select(const Vec2i &ltPos, const Vec2i &rbPos, std::vector<CUnit *> &units, Pred pred)
263 {
264  Vec2i minPos = ltPos;
265  Vec2i maxPos = rbPos;
266 
267  Map.FixSelectionArea(minPos, maxPos);
268  SelectFixed(minPos, maxPos, units, pred);
269 }
270 
271 template <typename Pred>
272 CUnit *FindUnit_IfFixed(const Vec2i &ltPos, const Vec2i &rbPos, Pred pred)
273 {
274  Assert(Map.Info.IsPointOnMap(ltPos));
275  Assert(Map.Info.IsPointOnMap(rbPos));
276 
277  for (Vec2i posIt = ltPos; posIt.y != rbPos.y + 1; ++posIt.y) {
278  for (posIt.x = ltPos.x; posIt.x != rbPos.x + 1; ++posIt.x) {
279  const CMapField &mf = *Map.Field(posIt);
280  const CUnitCache &cache = mf.UnitCache;
281 
282  CUnitCache::const_iterator it = std::find_if(cache.begin(), cache.end(), pred);
283  if (it != cache.end()) {
284  return *it;
285  }
286  }
287  }
288  return NULL;
289 }
290 
291 template <typename Pred>
292 CUnit *FindUnit_If(const Vec2i &ltPos, const Vec2i &rbPos, Pred pred)
293 {
294  Vec2i minPos = ltPos;
295  Vec2i maxPos = rbPos;
296 
297  Map.FixSelectionArea(minPos, maxPos);
298  return FindUnit_IfFixed(minPos, maxPos, pred);
299 }
300 
302 extern CUnit *UnitFindResource(const CUnit &unit, const CUnit &startUnit, int range,
303  int resource, bool check_usage = false, const CUnit *deposit = NULL);
304 
306 extern CUnit *FindDeposit(const CUnit &unit, int range, int resource);
308 extern CUnit *FindIdleWorker(const CPlayer &player, const CUnit *last);
309 
311 extern bool FindTerrainType(int movemask, int resmask, int range,
312  const CPlayer &player, const Vec2i &startPos, Vec2i *pos);
313 
314 extern void FindUnitsByType(const CUnitType &type, std::vector<CUnit *> &units, bool everybody = false);
315 
317 extern void FindPlayerUnitsByType(const CPlayer &player, const CUnitType &type, std::vector<CUnit *> &units);
319 extern CUnit *UnitOnMapTile(const Vec2i &pos, unsigned int type);// = -1);
321 extern CUnit *TargetOnMap(const CUnit &unit, const Vec2i &pos1, const Vec2i &pos2);
322 
324 extern CUnit *ResourceOnMap(const Vec2i &pos, int resource, bool mine_on_top = true);
326 extern CUnit *ResourceDepositOnMap(const Vec2i &pos, int resource);
327 
329 extern bool CheckObstaclesBetweenTiles(const Vec2i &unitPos, const Vec2i &goalPos, unsigned short flags, int *distance = NULL);
331 extern CUnit *AttackUnitsInDistance(const CUnit &unit, int range, CUnitFilter pred);
332 extern CUnit *AttackUnitsInDistance(const CUnit &unit, int range);
334 extern CUnit *AttackUnitsInRange(const CUnit &unit, CUnitFilter pred);
335 extern CUnit *AttackUnitsInRange(const CUnit &unit);
337 extern CUnit *AttackUnitsInReactRange(const CUnit &unit, CUnitFilter pred);
338 extern CUnit *AttackUnitsInReactRange(const CUnit &unit);
339 
340 
341 
343 
344 #endif // !__UNIT_FIND_H__
CUnit * ResourceOnMap(const Vec2i &pos, int resource, bool mine_on_top=true)
Return resource, if on map tile.
Definition: unit_find.cpp:640
NotPredicate< Pred > MakeNotPredicate(Pred pred)
Definition: unit_find.h:168
Definition: unit_find.h:105
VisitResult Visit(TerrainTraversal &terrainTraversal, const Vec2i &pos, const Vec2i &from)
Definition: unit_find.cpp:86
CUnitCache UnitCache
HP for walls/ Wood Regeneration.
Definition: tile.h:236
void Select(const Vec2i &ltPos, const Vec2i &rbPos, std::vector< CUnit * > &units)
Definition: unit_find.cpp:57
bool operator()(const CUnit *unit) const
Definition: unit_find.h:129
Definition: unit_find.h:140
bool operator()(const CUnit *unit) const
Definition: unit_find.h:143
Definition: unit_find.h:146
const_iterator begin() const
Definition: unit_cache.h:63
CMapInfo Info
graphic for fog of war
Definition: map.h:267
CUnit * AttackUnitsInReactRange(const CUnit &unit, CUnitFilter pred)
Find best enemy in reaction range to attack.
Definition: unit_find.cpp:1194
void SelectFixed(const Vec2i &ltPos, const Vec2i &rbPos, std::vector< CUnit * > &units)
Definition: unit_find.cpp:62
unsigned CacheLock
Unit is on board a transporter.
Definition: unit.h:362
int TileWidth
How much it costs to repair.
Definition: unittype.h:500
bool CheckObstaclesBetweenTiles(const Vec2i &unitPos, const Vec2i &goalPos, unsigned short flags, int *distance=NULL)
Check map for obstacles in a line between 2 tiles.
Definition: unit_find.cpp:1080
T y
Definition: vec2i.h:43
HasSamePlayerAndTypeAs(const CPlayer &_player, const CUnitType &_type)
Definition: unit_find.h:111
Definition: unit_find.h:48
bool IsPointOnMap(int x, int y) const
Definition: map.h:122
IsAlliedWith(const CPlayer &_player)
Definition: unit_find.h:90
AndPredicate< Pred1, Pred2 > MakeAndPredicate(Pred1 pred1, Pred2 pred2)
Definition: unit_find.h:182
The big unit structure.
Definition: unit.h:119
void FindPlayerUnitsByType(const CPlayer &player, const CUnitType &type, std::vector< CUnit * > &units)
Find all units of this type of the player.
Definition: unit_find.cpp:543
bool operator()(const CUnit *unit) const
Definition: unit_find.h:91
size_t size() const
Definition: unit_cache.h:59
HasNotSamePlayerAs(const CPlayer &_player)
Definition: unit_find.h:81
Definition: unit_find.h:69
int TileHeight
Tile size on map width.
Definition: unittype.h:501
bool operator()(const CUnit *const unit) const
Definition: unit_find.h:190
OutOfMinRange(const int range, const Vec2i pos)
Definition: unit_find.h:149
const_iterator end() const
Definition: unit_cache.h:65
UnitTypeType UnitType
originally only visual effect, we do more with this!
Definition: unittype.h:524
CUnit * AttackUnitsInDistance(const CUnit &unit, int range, CUnitFilter pred)
Find best enemy in numeric range to attack.
Definition: unit_find.cpp:1124
bool FindTerrainType(int movemask, int resmask, int range, const CPlayer &player, const Vec2i &startPos, Vec2i *pos)
Find the neareast piece of terrain with specific flags.
Definition: unit_find.cpp:162
bool operator()(const CUnit *unit) const
Definition: unit_find.h:64
IsEnemyWith(const CPlayer &_player)
Definition: unit_find.h:99
unsigned Vanishes
Can attack from transporter.
Definition: unittype.h:553
Definition: unittype.h:430
CMapField * Field(unsigned int index) const
Definition: map.h:164
unsigned Building
Death explosion animated.
Definition: unittype.h:547
#define Assert(cond)
Definition: stratagus.h:120
bool operator()(const CUnit *unit) const
Definition: unit_find.h:162
CUnit * UnitFindResource(const CUnit &unit, const CUnit &startUnit, int range, int resource, bool check_usage=false, const CUnit *deposit=NULL)
Find resource.
Definition: unit_find.cpp:432
Definition: unit_find.h:203
HasSameTypeAs(const CUnitType &_type)
Definition: unit_find.h:63
CUnit * AttackUnitsInRange(const CUnit &unit, CUnitFilter pred)
Find best enemy in attack range to attack.
Definition: unit_find.cpp:1176
CUnitTypeFinder(const UnitTypeType t)
Definition: unit_find.h:189
bool operator()(const CUnit *unit) const
Definition: unit_find.h:175
Vec2i tilePos
Resource still.
Definition: unit.h:325
bool IsAllied(const CPlayer &player) const
Definition: unit.cpp:2960
bool operator()(const CUnit *unit) const
Definition: unit_find.h:137
CUnit * FindUnit_IfFixed(const Vec2i &ltPos, const Vec2i &rbPos, Pred pred)
Definition: unit_find.h:272
CUnit * FindUnit_If(const Vec2i &ltPos, const Vec2i &rbPos, Pred pred)
Definition: unit_find.h:292
bool operator()(const CUnit *unit) const
Definition: unit_find.h:51
Definition: unit_find.h:125
void FindUnitsByType(const CUnitType &type, std::vector< CUnit * > &units, bool everybody=false)
Definition: unit_find.cpp:525
Definition: pathfinder.h:134
CUnit * TargetOnMap(const CUnit &unit, const Vec2i &pos1, const Vec2i &pos2)
Return possible attack target on that map area.
Definition: unit_find.cpp:603
CUnit * FindDeposit(const CUnit &unit, int range, int resource)
Find nearest deposit.
Definition: unit_find.cpp:465
void FixSelectionArea(Vec2i &minpos, Vec2i &maxpos)
Definition: map.h:237
UnitFinder(const CPlayer &player, const std::vector< CUnit * > &units, int maxDist, int movemask, CUnit **unitP)
Definition: unit_find.h:206
Definition: unit_cache.h:50
bool operator()(const CUnit *unit) const
Definition: unit_find.h:100
UnitTypeType
Definition: unittype.h:319
CUnit * ResourceDepositOnMap(const Vec2i &pos, int resource)
Return resource deposit, if on map tile.
Definition: unit_find.cpp:665
int MapDistanceTo(const CUnit &dst) const
Definition: unit.h:272
CUnit * FindIdleWorker(const CPlayer &player, const CUnit *last)
Find the next idle worker.
Definition: unit_find.cpp:490
CUnit * UnitOnMapTile(const Vec2i &pos, unsigned int type)
Return any unit on that map tile.
Definition: unit_find.cpp:589
T x
Definition: vec2i.h:42
Diplomacy states for CommandDiplomacy.
Definition: player.h:79
bool operator()(const CUnit *) const
Definition: unit_find.h:57
Definition: unit_find.h:186
CMap Map
Definition: map.cpp:54
Definition: unit_find.h:78
NotPredicate(Pred _pred)
Definition: unit_find.h:161
bool operator()(const CUnit *unit) const
Definition: unit_find.h:115
bool operator()(const CUnit *unit) const
Definition: unit_find.h:73
CPlayer * Player
Pointer to unit-type (peon,...)
Definition: unit.h:330
bool operator()(const CUnit *unit) const
Definition: unit_find.h:150
HasSamePlayerAndTypeAs(const CUnit &unit)
Definition: unit_find.h:108
AndPredicate(Pred1 _pred1, Pred2 _pred2)
Definition: unit_find.h:174
Definition: unit_find.h:87
IsNotTheSameUnitAs(const CUnit &unit)
Definition: unit_find.h:128
void SelectAroundUnit(const CUnit &unit, int range, std::vector< CUnit * > &around)
Definition: unit_find.cpp:67
HasSamePlayerAs(const CPlayer &_player)
Definition: unit_find.h:72
Definition: unit_find.h:60
std::vector< CUnit * >::const_iterator const_iterator
Definition: unit_cache.h:54
Describes a field of the map.
Definition: tile.h:186
const CUnitType * Type
Map position as flat index offset (x + y * w)
Definition: unit.h:329
bool operator()(const CUnit *unit) const
Definition: unit_find.h:82
bool IsEnemy(const CPlayer &player) const
Definition: unit.cpp:2940
Definition: unit_find.h:134
Definition: unit_find.h:158
Definition: unit_find.h:96
VisitResult
Definition: pathfinder.h:127
Definition: unit_find.h:171
bool IsAgressive() const
Definition: unit.h:169
Definition: unit_find.h:54
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.