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

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ai_local.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 2000-2005 by Lutz Sammer and Antonis Chaniotis.
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 __AI_LOCAL_H__
31 #define __AI_LOCAL_H__
32 
34 
35 /*----------------------------------------------------------------------------
36 -- Includes
37 ----------------------------------------------------------------------------*/
38 
39 #include <vector>
40 
41 #include "upgrade_structs.h" // MaxCost
42 #include "unit_cache.h"
43 #include "vec2i.h"
44 
45 /*----------------------------------------------------------------------------
46 -- Declarations
47 ----------------------------------------------------------------------------*/
48 
49 class CUnit;
50 class CUnitType;
51 class CUpgrade;
52 class CPlayer;
53 
57 class CAiType
58 {
59 public:
60  CAiType() {}
61 
62  std::string Name;
63  std::string Race;
64  std::string Class;
65  std::string Script;
66 };
67 
72 {
73 public:
74  AiRequestType() : Count(0), Type(NULL) {}
75 
76  unsigned int Count;
78 };
79 
84 {
85 public:
86  AiUnitType() : Want(0), Type(NULL) {}
87 
88  unsigned int Want;
90 };
91 
99 };
100 
108 };
109 
110 #define AI_WAIT_ON_RALLY_POINT 60
111 
112 
117 class AiForce
118 {
119  friend class AiForceManager;
120 public:
122  Completed(false), Defending(false), Attacking(false),
125  {
126  HomePos.x = HomePos.y = GoalPos.x = GoalPos.y = -1;
127  }
128 
129  void Remove(CUnit &unit)
130  {
131  if (Units.Remove(&unit)) {
132  InternalRemoveUnit(&unit);
133  }
134  }
135 
139  void Reset(bool types = false)
140  {
141  FormerForce = -1;
142  Completed = false;
143  Defending = false;
144  Attacking = false;
146  if (types) {
147  UnitTypes.clear();
149  } else {
151  }
152  Units.for_each(InternalRemoveUnit);
153  Units.clear();
154  HomePos.x = HomePos.y = GoalPos.x = GoalPos.y = -1;
155  }
156  inline size_t Size() const { return Units.size(); }
157 
158  inline bool IsAttacking() const { return (!Defending && Attacking); }
159 
160  void Attack(const Vec2i &pos);
161  void RemoveDeadUnit();
162  int PlanAttack();
163 
164  void ReturnToHome();
165  bool NewRallyPoint(const Vec2i &startPos, Vec2i *resultPos);
166  void Insert(CUnit &unit);
167 
168 private:
169  void CountTypes(unsigned int *counter, const size_t len);
170  bool IsBelongsTo(const CUnitType &type);
171 
172  void Update();
173 
174  static void InternalRemoveUnit(CUnit *unit);
175 
176 public:
177  bool Completed;
178  bool Defending;
179  bool Attacking;
181 
182  std::vector<AiUnitType> UnitTypes;
184 
185  // If attacking
191 };
192 
193 // forces
194 #define AI_MAX_FORCES 50
195 #define AI_MAX_FORCE_INTERNAL (AI_MAX_FORCES / 2)
196 
197 
203 {
204 public:
205  AiForceManager();
206 
207  inline size_t Size() const { return forces.size(); }
208 
209  const AiForce &operator[](unsigned int index) const { return forces[index]; }
210  AiForce &operator[](unsigned int index) { return forces[index]; }
211 
212  int getIndex(AiForce *force) const
213  {
214  for (unsigned int i = 0; i < forces.size(); ++i) {
215  if (force == &forces[i]) {
216  return i;
217  }
218  }
219  return -1;
220  }
221 
222  unsigned int getScriptForce(unsigned int index)
223  {
224  if (script[index] == -1) {
225  script[index] = FindFreeForce();
226  }
227  return script[index];
228  }
229 
230  int GetForce(const CUnit &unit);
231  void RemoveDeadUnit();
232  bool Assign(CUnit &unit, int force = -1);
233  void Update();
234  unsigned int FindFreeForce(AiForceRole role = AiForceRoleDefault, int begin = 0);
235  void CheckUnits(int *counter);
236 private:
237  std::vector<AiForce> forces;
238  char script[AI_MAX_FORCES];
239 };
240 
247 {
248 public:
249  AiBuildQueue() : Want(0), Made(0), Type(NULL), Wait(0)
250  {
251  Pos.x = Pos.y = -1;
252  }
253 
254 public:
255  unsigned int Want;
256  unsigned int Made;
258  unsigned long Wait;
260 };
261 
266 {
267 public:
268  AiExplorationRequest(const Vec2i &pos, int mask) : pos(pos), Mask(mask) {}
269 
270 public:
272  int Mask;
273 };
274 
278 class PlayerAi
279 {
280 public:
281  PlayerAi() : Player(NULL), AiType(NULL),
282  SleepCycles(0), NeededMask(0), NeedSupply(false),
285  {
286  memset(Reserve, 0, sizeof(Reserve));
287  memset(Used, 0, sizeof(Used));
288  memset(Needed, 0, sizeof(Needed));
289  memset(Collect, 0, sizeof(Collect));
290  }
291 
292 public:
295  // controller
296  std::string Script;
297  unsigned long SleepCycles;
298 
300 
301  // resource manager
303  int Used[MaxCosts];
307  bool NeedSupply;
308  bool ScriptDebug;
309  bool BuildDepots;
310 
311  std::vector<AiExplorationRequest> FirstExplorationRequest;
312  unsigned long LastExplorationGameCycle;
313  unsigned long LastCanNotMoveGameCycle;
314  std::vector<AiRequestType> UnitTypeRequests;
315  std::vector<CUnitType *> UpgradeToRequests;
316  std::vector<CUpgrade *> ResearchRequests;
317  std::vector<AiBuildQueue> UnitTypeBuilt;
319 };
320 
328 class AiHelper
329 {
330 public:
335  std::vector<std::vector<CUnitType *> > Train;
340  std::vector<std::vector<CUnitType *> > Build;
345  std::vector<std::vector<CUnitType *> > Upgrade;
350  std::vector<std::vector<CUnitType *> > Research;
355  std::vector<std::vector<CUnitType *> > Repair;
360  std::vector<std::vector<CUnitType *> > UnitLimit;
365  std::vector<std::vector<CUnitType *> > Equiv;
366 
371  std::vector<std::vector<CUnitType *> > Refinery;
372 
377  std::vector<std::vector<CUnitType *> > Depots;
378 };
379 
380 /*----------------------------------------------------------------------------
381 -- Variables
382 ----------------------------------------------------------------------------*/
383 
384 extern std::vector<CAiType *> AiTypes;
385 extern AiHelper AiHelpers;
386 
387 extern int UnitTypeEquivs[UnitTypeMax + 1];
388 extern PlayerAi *AiPlayer;
389 
390 /*----------------------------------------------------------------------------
391 -- Functions
392 ----------------------------------------------------------------------------*/
393 
394 //
395 // Resource manager
396 //
398 extern void AiAddUnitTypeRequest(CUnitType &type, int count);
400 extern void AiAddUpgradeToRequest(CUnitType &type);
402 extern void AiAddResearchRequest(CUpgrade *upgrade);
404 extern void AiResourceManager();
406 extern void AiExplore(const Vec2i &pos, int exploreMask);
408 extern void AiNewUnitTypeEquiv(const CUnitType &a, const CUnitType &b);
410 extern void AiResetUnitTypeEquiv();
412 extern int AiFindUnitTypeEquiv(const CUnitType &type, int *result);
414 extern int AiFindAvailableUnitTypeEquiv(const CUnitType &type, int *result);
415 extern int AiGetBuildRequestsCount(const PlayerAi &pai, int (&counter)[UnitTypeMax]);
416 
417 extern void AiNewDepotRequest(CUnit &worker);
418 extern CUnit *AiGetSuitableDepot(const CUnit &worker, const CUnit &oldDepot, CUnit **resUnit);
419 
420 //
421 // Buildings
422 //
424 extern bool AiFindBuildingPlace(const CUnit &worker, const CUnitType &type, const Vec2i &nearPos, Vec2i *resultPos);
425 
426 //
427 // Forces
428 //
430 extern void AiRemoveDeadUnitInForces();
432 extern bool AiAssignToForce(CUnit &unit);
434 extern void AiAssignFreeUnitsToForce(int force = -1);
436 extern void AiAttackWithForceAt(unsigned int force, int x, int y);
438 extern void AiAttackWithForce(unsigned int force);
440 extern void AiAttackWithForces(int *forces);
441 
443 extern void AiForceManager();
444 
445 //
446 // Plans
447 //
449 extern int AiFindWall(AiForce *force);
452 extern void AiSendExplorers();
454 extern int AiEnemyUnitsInDistance(const CPlayer &player, const CUnitType *type,
455  const Vec2i &pos, unsigned range);
456 
457 //
458 // Magic
459 //
461 extern void AiCheckMagic();
462 
464 
465 #endif // !__AI_LOCAL_H__
Vec2i GoalPos
Attack state.
Definition: ai_local.h:188
unsigned int Made
requested number
Definition: ai_local.h:256
bool NeedSupply
Mask for needed resources.
Definition: ai_local.h:307
bool Attacking
Flag saying force is defending.
Definition: ai_local.h:179
#define AI_WAIT_ON_RALLY_POINT
Definition: ai_local.h:110
void AiRemoveDeadUnitInForces()
Cleanup units in force.
Definition: ai_force.cpp:626
So default is attacking.
Definition: ai_local.h:97
AiForceAttackingState
Definition: ai_local.h:101
Definition: ai_local.h:103
void Update()
Definition: ai_force.cpp:1056
std::string Script
AI type of this player AI.
Definition: ai_local.h:296
int Mask
pos on map
Definition: ai_local.h:272
Definition: ai_local.h:246
resource 6
Definition: upgrade_structs.h:70
unsigned int getScriptForce(unsigned int index)
Definition: ai_local.h:222
AiForceAttackingState State
Original force number.
Definition: ai_local.h:187
Force should attack.
Definition: ai_local.h:98
std::string Script
class of this ai
Definition: ai_local.h:65
void Insert(CUnit &unit)
Definition: ai_force.cpp:284
AiForce & operator[](unsigned int index)
Definition: ai_local.h:210
T y
Definition: vec2i.h:43
CAiType()
Definition: ai_local.h:60
int WaitOnRallyPoint
Return after attack tile map position.
Definition: ai_local.h:190
void ReturnToHome()
Definition: ai_force.cpp:468
void AiAttackWithForce(unsigned int force)
Attack with force.
Definition: ai_force.cpp:687
int AiGetBuildRequestsCount(const PlayerAi &pai, int(&counter)[UnitTypeMax])
Definition: ai_resource.cpp:339
bool Defending
Flag saying force is complete build.
Definition: ai_local.h:178
bool AiFindBuildingPlace(const CUnit &worker, const CUnitType &type, const Vec2i &nearPos, Vec2i *resultPos)
Find nice building place.
Definition: ai_building.cpp:433
std::string Class
for this race
Definition: ai_local.h:64
int GetForce(const CUnit &unit)
Definition: ai_force.cpp:514
The big unit structure.
Definition: unit.h:119
std::vector< std::vector< CUnitType * > > UnitLimit
Definition: ai_local.h:360
Definition: ai_local.h:104
size_t size() const
Definition: unit_cache.h:59
int Collect[MaxCosts]
Needed resources.
Definition: ai_local.h:305
std::vector< std::vector< CUnitType * > > Research
Definition: ai_local.h:350
std::vector< std::vector< CUnitType * > > Refinery
Definition: ai_local.h:371
Definition: ai_local.h:117
Definition: ai_local.h:107
Vec2i HomePos
Attack point tile map position.
Definition: ai_local.h:189
void AiCheckMagic()
Check for magic.
Definition: ai_magic.cpp:51
void AiForceManager()
Periodic called force manager handler.
Definition: ai_force.cpp:1129
void AiAddUpgradeToRequest(CUnitType &type)
Add upgrade-to request to resource manager.
Definition: ai_resource.cpp:806
void Remove(CUnit &unit)
Definition: ai_local.h:129
int UnitTypeEquivs[UnitTypeMax+1]
AI helper variables.
Definition: ai_force.cpp:125
void AiAssignFreeUnitsToForce(int force=-1)
Assign a free units to a force.
Definition: ai_force.cpp:644
std::vector< AiBuildQueue > UnitTypeBuilt
Upgrades requested and priority list.
Definition: ai_local.h:317
PlayerAi()
Definition: ai_local.h:281
void AiAttackWithForces(int *forces)
Attack with forces in array.
Definition: ai_force.cpp:732
const AiForce & operator[](unsigned int index) const
Definition: ai_local.h:209
bool AiAssignToForce(CUnit &unit)
Assign a new unit to a force.
Definition: ai_force.cpp:636
void RemoveDeadUnit()
Definition: ai_force.cpp:300
CUnit * AiGetSuitableDepot(const CUnit &worker, const CUnit &oldDepot, CUnit **resUnit)
Definition: ai_resource.cpp:463
AiForceManager()
Definition: ai_force.cpp:485
CUnitCache Units
Count and types of unit-type.
Definition: ai_local.h:183
bool ScriptDebug
Flag need food.
Definition: ai_local.h:308
int Used[MaxCosts]
Resources to keep in reserve.
Definition: ai_local.h:303
Definition: unittype.h:467
Definition: ai_local.h:328
void CheckUnits(int *counter)
Definition: ai_force.cpp:575
std::vector< CUnitType * > UpgradeToRequests
unit-types to build/train request,priority list
Definition: ai_local.h:315
int getIndex(AiForce *force) const
Definition: ai_local.h:212
AiUnitType()
Definition: ai_local.h:86
CPlayer * Player
Definition: ai_local.h:293
std::vector< CUpgrade * > ResearchRequests
Upgrade to unit-type requested and priority list.
Definition: ai_local.h:316
void Attack(const Vec2i &pos)
Definition: ai_force.cpp:368
std::vector< std::vector< CUnitType * > > Repair
Definition: ai_local.h:355
void AiResourceManager()
Periodic called resource manager handler.
Definition: ai_resource.cpp:1456
AiForce()
Definition: ai_local.h:121
void RemoveDeadUnit()
Definition: ai_force.cpp:533
unsigned long SleepCycles
Script executed.
Definition: ai_local.h:297
int AiFindUnitTypeEquiv(const CUnitType &type, int *result)
Finds all equivalents units to a given one.
Definition: ai_force.cpp:174
unsigned int Want
Definition: ai_local.h:255
AiForceRole
Definition: ai_local.h:95
AiBuildQueue()
Definition: ai_local.h:249
CAiType * AiType
Engine player structure.
Definition: ai_local.h:294
int NeededMask
Collect % of resources.
Definition: ai_local.h:306
Definition: ai_local.h:83
std::vector< std::vector< CUnitType * > > Upgrade
Definition: ai_local.h:345
Vec2i pos
Definition: ai_local.h:271
CUnitType * Type
number of this unit-type wanted
Definition: ai_local.h:89
std::vector< AiUnitType > UnitTypes
Role of the force.
Definition: ai_local.h:182
Definition: ai_local.h:71
Definition: ai_local.h:102
Vec2i Pos
wait until this cycle
Definition: ai_local.h:259
unsigned int FindFreeForce(AiForceRole role=AiForceRoleDefault, int begin=0)
Definition: ai_force.cpp:491
void for_each(const _T functor)
Apply a function to every element of a cache.
Definition: unit_cache.h:102
std::string Race
Name of this ai.
Definition: ai_local.h:63
bool Completed
Definition: ai_local.h:177
unsigned long Wait
unit-type
Definition: ai_local.h:258
unsigned long LastCanNotMoveGameCycle
When did the last explore occur?
Definition: ai_local.h:313
AiForceManager Force
Cycles to sleep.
Definition: ai_local.h:299
unsigned int Count
Definition: ai_local.h:76
Definition: ai_local.h:278
std::vector< CAiType * > AiTypes
Ai sleeps # cycles.
Definition: ai.cpp:164
Definition: unit_cache.h:50
AiRequestType()
Definition: ai_local.h:74
AiForceRole Role
Flag saying force is attacking.
Definition: ai_local.h:180
size_t Size() const
Definition: ai_local.h:156
void AiNewUnitTypeEquiv(const CUnitType &a, const CUnitType &b)
Make two unittypes be considered equals.
Definition: ai_force.cpp:147
T x
Definition: vec2i.h:42
Diplomacy states for CommandDiplomacy.
Definition: player.h:79
void AiExplore(const Vec2i &pos, int exploreMask)
Ask the ai to explore around pos.
Definition: ai_resource.cpp:1441
int FormerForce
Units in the force.
Definition: ai_local.h:186
unsigned long LastExplorationGameCycle
Requests for exploration.
Definition: ai_local.h:312
void AiSendExplorers()
Definition: ai_plan.cpp:503
std::vector< AiExplorationRequest > FirstExplorationRequest
Build new depots if nessesary.
Definition: ai_local.h:311
CUnitType * Type
built number
Definition: ai_local.h:257
void clear()
Definition: unit_cache.h:61
int LastRepairBuilding
What the resource manager should build.
Definition: ai_local.h:318
bool BuildDepots
Flag script debuging on/off.
Definition: ai_local.h:309
PlayerAi * AiPlayer
equivalence between unittypes
Definition: ai.cpp:167
Definition: ai_local.h:105
int AiEnemyUnitsInDistance(const CPlayer &player, const CUnitType *type, const Vec2i &pos, unsigned range)
Enemy units in distance.
Definition: ai_resource.cpp:209
bool Assign(CUnit &unit, int force=-1)
Definition: ai_force.cpp:545
std::string Name
Definition: ai_local.h:62
int Needed[MaxCosts]
Used resources.
Definition: ai_local.h:304
int Reserve[MaxCosts]
Forces controlled by AI.
Definition: ai_local.h:302
Definition: upgrade_structs.h:154
std::vector< std::vector< CUnitType * > > Equiv
Definition: ai_local.h:365
Definition: ai_local.h:265
Definition: vec2i.h:36
std::vector< AiRequestType > UnitTypeRequests
Last can not move cycle.
Definition: ai_local.h:314
size_t Size() const
Definition: ai_local.h:207
std::vector< std::vector< CUnitType * > > Train
Definition: ai_local.h:335
CUnitType * Type
elements in table
Definition: ai_local.h:77
int AiFindAvailableUnitTypeEquiv(const CUnitType &type, int *result)
Finds all available equivalents units to a given one, in the preferred order.
Definition: ai_force.cpp:207
#define AI_MAX_FORCES
Definition: ai_local.h:194
void AiAttackWithForceAt(unsigned int force, int x, int y)
Attack with force at position.
Definition: ai_force.cpp:665
Definition: ai_local.h:202
Definition: ai_local.h:96
bool NewRallyPoint(const Vec2i &startPos, Vec2i *resultPos)
Definition: ai_force.cpp:347
AiExplorationRequest(const Vec2i &pos, int mask)
Definition: ai_local.h:268
void AiResetUnitTypeEquiv()
Remove any equivalence between unittypes.
Definition: ai_force.cpp:134
int AiFindWall(AiForce *force)
Find a wall to attack.
Definition: ai_plan.cpp:184
void AiNewDepotRequest(CUnit &worker)
Definition: ai_resource.cpp:354
bool IsAttacking() const
Definition: ai_local.h:158
int PlanAttack()
Definition: ai_plan.cpp:343
std::vector< std::vector< CUnitType * > > Depots
Definition: ai_local.h:377
AiHelper AiHelpers
List of all AI types.
Definition: ai.cpp:165
#define UnitTypeMax
Definition: stratagus.h:158
std::vector< std::vector< CUnitType * > > Build
Definition: ai_local.h:340
Definition: ai_local.h:57
unsigned int Want
Definition: ai_local.h:88
void AiAddResearchRequest(CUpgrade *upgrade)
Add research request to resource manager.
Definition: ai_resource.cpp:737
void Reset(bool types=false)
Definition: ai_local.h:139
void AiAddUnitTypeRequest(CUnitType &type, int count)
Current AI player.
Definition: ai_resource.cpp:1425
CUnit * Remove(const unsigned int index)
Definition: unit_cache.h:140
(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.