$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 2000-2005 by Lutz Sammer and Antonis Chaniotis. 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 __AI_LOCAL_H__ 00031 #define __AI_LOCAL_H__ 00032 00034 00035 /*---------------------------------------------------------------------------- 00036 -- Includes 00037 ----------------------------------------------------------------------------*/ 00038 00039 #include <vector> 00040 00041 #include "upgrade_structs.h" // MaxCost 00042 #include "unit_cache.h" 00043 #include "vec2i.h" 00044 00045 /*---------------------------------------------------------------------------- 00046 -- Declarations 00047 ----------------------------------------------------------------------------*/ 00048 00049 class CUnit; 00050 class CUnitType; 00051 class CUpgrade; 00052 class CPlayer; 00053 00057 class CAiType 00058 { 00059 public: 00060 CAiType() {} 00061 00062 std::string Name; 00063 std::string Race; 00064 std::string Class; 00065 std::string Script; 00066 }; 00067 00071 class AiRequestType 00072 { 00073 public: 00074 AiRequestType() : Count(0), Type(NULL) {} 00075 00076 unsigned int Count; 00077 CUnitType *Type; 00078 }; 00079 00083 class AiUnitType 00084 { 00085 public: 00086 AiUnitType() : Want(0), Type(NULL) {} 00087 00088 unsigned int Want; 00089 CUnitType *Type; 00090 }; 00091 00095 enum AiForceRole { 00096 AiForceRoleDefault = 0, 00097 AiForceRoleAttack = 0, 00098 AiForceRoleDefend 00099 }; 00100 00101 enum AiForceAttackingState { 00102 AiForceAttackingState_Free = -1, 00103 AiForceAttackingState_Waiting = 0, 00104 AiForceAttackingState_Boarding, 00105 AiForceAttackingState_AttackingWithTransporter, 00106 AiForceAttackingState_Attacking, 00107 }; 00108 00114 class AiForce 00115 { 00116 friend class AiForceManager; 00117 public: 00118 AiForce() : 00119 Completed(false), Defending(false), Attacking(false), 00120 Role(AiForceRoleDefault), State(AiForceAttackingState_Free) { 00121 HomePos.x = HomePos.y = GoalPos.x = GoalPos.y = -1; 00122 } 00123 00124 void Remove(CUnit &unit) { 00125 if (Units.Remove(&unit)) { 00126 InternalRemoveUnit(&unit); 00127 } 00128 } 00129 00133 void Reset(bool types = false) { 00134 Completed = false; 00135 Defending = false; 00136 Attacking = false; 00137 if (types) { 00138 UnitTypes.clear(); 00139 State = AiForceAttackingState_Free; 00140 } else { 00141 State = AiForceAttackingState_Waiting; 00142 } 00143 Units.for_each(InternalRemoveUnit); 00144 Units.clear(); 00145 HomePos.x = HomePos.y = GoalPos.x = GoalPos.y = -1; 00146 } 00147 inline size_t Size() const { return Units.size(); } 00148 00149 inline bool IsAttacking() const { return (!Defending && Attacking); } 00150 00151 void Attack(const Vec2i &pos); 00152 void RemoveDeadUnit(); 00153 int PlanAttack(); 00154 00155 void ReturnToHome(); 00156 00157 private: 00158 void CountTypes(unsigned int *counter, const size_t len); 00159 bool IsBelongsTo(const CUnitType *type); 00160 void Insert(CUnit &unit); 00161 00162 void Update(); 00163 00164 static void InternalRemoveUnit(CUnit *unit); 00165 00166 public: 00167 bool Completed; 00168 bool Defending; 00169 bool Attacking; 00170 AiForceRole Role; 00171 00172 std::vector<AiUnitType> UnitTypes; 00173 CUnitCache Units; 00174 00175 // If attacking 00176 AiForceAttackingState State; 00177 Vec2i GoalPos; 00178 Vec2i HomePos; 00179 }; 00180 00181 // forces 00182 #define AI_MAX_FORCES 50 00183 00184 00189 class AiForceManager 00190 { 00191 public: 00192 AiForceManager(); 00193 00194 inline size_t Size() const { return forces.size(); } 00195 00196 const AiForce &operator[](unsigned int index) const { return forces[index]; } 00197 AiForce &operator[](unsigned int index) { return forces[index]; } 00198 00199 int getIndex(AiForce *force) const { 00200 for (unsigned int i = 0; i < forces.size(); ++i) { 00201 if (force == &forces[i]) { 00202 return i; 00203 } 00204 } 00205 return -1; 00206 } 00207 00208 unsigned int getScriptForce(unsigned int index) { 00209 if (script[index] == -1) { 00210 script[index] = FindFreeForce(); 00211 } 00212 return script[index]; 00213 } 00214 00215 void RemoveDeadUnit(); 00216 bool Assign(CUnit &unit); 00217 void Update(); 00218 unsigned int FindFreeForce(AiForceRole role = AiForceRoleDefault); 00219 void CheckUnits(int *counter); 00220 private: 00221 std::vector<AiForce> forces; 00222 char script[AI_MAX_FORCES]; 00223 }; 00224 00230 class AiBuildQueue 00231 { 00232 public: 00233 AiBuildQueue() : Want(0), Made(0), Type(NULL), Wait(0) { 00234 Pos.x = Pos.y = -1; 00235 } 00236 00237 public: 00238 unsigned int Want; 00239 unsigned int Made; 00240 CUnitType *Type; 00241 unsigned long Wait; 00242 Vec2i Pos; 00243 }; 00244 00248 class AiExplorationRequest 00249 { 00250 public: 00251 AiExplorationRequest(const Vec2i &pos, int mask) : pos(pos), Mask(mask) {} 00252 00253 public: 00254 Vec2i pos; 00255 int Mask; 00256 }; 00257 00261 class PlayerAi 00262 { 00263 public: 00264 PlayerAi() : Player(NULL), AiType(NULL), 00265 SleepCycles(0), NeededMask(0), NeedSupply(false), 00266 ScriptDebug(false), LastExplorationGameCycle(0), 00267 LastCanNotMoveGameCycle(0), LastRepairBuilding(0) { 00268 memset(Reserve, 0, sizeof(Reserve)); 00269 memset(Used, 0, sizeof(Used)); 00270 memset(Needed, 0, sizeof(Needed)); 00271 memset(Collect, 0, sizeof(Collect)); 00272 } 00273 00274 public: 00275 CPlayer *Player; 00276 CAiType *AiType; 00277 // controller 00278 std::string Script; 00279 unsigned long SleepCycles; 00280 00281 AiForceManager Force; 00282 00283 // resource manager 00284 int Reserve[MaxCosts]; 00285 int Used[MaxCosts]; 00286 int Needed[MaxCosts]; 00287 int Collect[MaxCosts]; 00288 int NeededMask; 00289 bool NeedSupply; 00290 bool ScriptDebug; 00291 00292 std::vector<AiExplorationRequest> FirstExplorationRequest; 00293 unsigned long LastExplorationGameCycle; 00294 unsigned long LastCanNotMoveGameCycle; 00295 std::vector<AiRequestType> UnitTypeRequests; 00296 std::vector<CUnitType *> UpgradeToRequests; 00297 std::vector<CUpgrade *> ResearchRequests; 00298 std::vector<AiBuildQueue> UnitTypeBuilt; 00299 int LastRepairBuilding; 00300 }; 00301 00309 class AiHelper 00310 { 00311 public: 00316 std::vector<std::vector<CUnitType *> > Train; 00321 std::vector<std::vector<CUnitType *> > Build; 00326 std::vector<std::vector<CUnitType *> > Upgrade; 00331 std::vector<std::vector<CUnitType *> > Research; 00336 std::vector<std::vector<CUnitType *> > Repair; 00341 std::vector<std::vector<CUnitType *> > UnitLimit; 00346 std::vector<std::vector<CUnitType *> > Equiv; 00347 00352 std::vector<std::vector<CUnitType *> > Refinery; 00353 00358 std::vector<std::vector<CUnitType *> > Depots; 00359 }; 00360 00361 /*---------------------------------------------------------------------------- 00362 -- Variables 00363 ----------------------------------------------------------------------------*/ 00364 00365 extern std::vector<CAiType *> AiTypes; 00366 extern AiHelper AiHelpers; 00367 00368 extern int UnitTypeEquivs[UnitTypeMax + 1]; 00369 extern PlayerAi *AiPlayer; 00370 00371 /*---------------------------------------------------------------------------- 00372 -- Functions 00373 ----------------------------------------------------------------------------*/ 00374 00375 // 00376 // Resource manager 00377 // 00379 extern void AiAddUnitTypeRequest(CUnitType &type, int count); 00381 extern void AiAddUpgradeToRequest(CUnitType &type); 00383 extern void AiAddResearchRequest(CUpgrade *upgrade); 00385 extern void AiResourceManager(); 00387 extern void AiExplore(const Vec2i &pos, int exploreMask); 00389 extern void AiNewUnitTypeEquiv(CUnitType *a, CUnitType *b); 00391 extern void AiResetUnitTypeEquiv(); 00393 extern int AiFindUnitTypeEquiv(const CUnitType &type, int *result); 00395 extern int AiFindAvailableUnitTypeEquiv(const CUnitType &type, int *result); 00396 extern int AiGetBuildRequestsCount(const PlayerAi &pai, int (&counter)[UnitTypeMax]); 00397 00398 extern void AiNewDepotRequest(CUnit &worker); 00399 00400 // 00401 // Buildings 00402 // 00404 extern bool AiFindBuildingPlace(const CUnit &worker, const CUnitType &type, const Vec2i &nearPos, Vec2i *resultPos); 00405 00406 // 00407 // Forces 00408 // 00410 extern void AiRemoveDeadUnitInForces(); 00412 extern bool AiAssignToForce(CUnit &unit); 00414 extern void AiAssignFreeUnitsToForce(); 00416 extern void AiAttackWithForceAt(unsigned int force, int x, int y); 00418 extern void AiAttackWithForce(unsigned int force); 00420 extern void AiAttackWithForces(int *forces); 00421 00423 extern void AiForceManager(); 00424 00425 // 00426 // Plans 00427 // 00429 extern int AiFindWall(AiForce *force); 00432 extern void AiSendExplorers(); 00434 extern int AiEnemyUnitsInDistance(const CPlayer &player, const CUnitType *type, 00435 const Vec2i &pos, unsigned range); 00436 00437 // 00438 // Magic 00439 // 00441 extern void AiCheckMagic(); 00442 00444 00445 #endif // !__AI_LOCAL_H__