$treeview $search $mathjax
Stratagus
2.2.6
$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" 00042 #include "unit.h" 00043 00044 /*---------------------------------------------------------------------------- 00045 -- Declarations 00046 ----------------------------------------------------------------------------*/ 00047 00048 class CUnit; 00049 class CUnitType; 00050 class CUpgrade; 00051 class CPlayer; 00052 00056 class CAiType { 00057 public: 00058 CAiType() {} 00059 00060 std::string Name; 00061 std::string Race; 00062 std::string Class; 00063 std::string Script; 00064 }; 00065 00069 class AiRequestType { 00070 public: 00071 AiRequestType() : Count(0), Type(NULL) {} 00072 00073 unsigned int Count; 00074 CUnitType *Type; 00075 }; 00076 00080 class AiUnitType { 00081 public: 00082 AiUnitType() : Want(0), Type(NULL) {} 00083 00084 unsigned int Want; 00085 CUnitType *Type; 00086 }; 00087 00091 enum AiForceRole { 00092 AiForceRoleAttack = 0, 00093 AiForceRoleDefend 00094 }; 00095 00096 enum AiForceAttackingState 00097 { 00098 AiForceAttackingState_Free = -1, 00099 AiForceAttackingState_Waiting = 0, 00100 AiForceAttackingState_Boarding, 00101 AiForceAttackingState_AttackingWithTransporter, 00102 AiForceAttackingState_Attacking, 00103 }; 00104 00110 class AiForce { 00111 friend class AiForceManager; 00112 public: 00113 AiForce() : 00114 Completed(false), Defending(false), Attacking(false), 00115 Role(0), State(AiForceAttackingState_Free) 00116 { 00117 GoalPos.x = GoalPos.y = 0; 00118 } 00119 00120 void Remove(CUnit &unit) 00121 { 00122 if (Units.Remove(&unit)) { 00123 InternalRemoveUnit(&unit); 00124 } 00125 } 00126 00130 void Reset(bool types = false) { 00131 Completed = false; 00132 Defending = false; 00133 Attacking = false; 00134 if (types) { 00135 UnitTypes.clear(); 00136 State = AiForceAttackingState_Free; 00137 } else { 00138 State = AiForceAttackingState_Waiting; 00139 } 00140 Units.for_each(InternalRemoveUnit); 00141 Units.clear(); 00142 GoalPos.x = GoalPos.y = 0; 00143 } 00144 inline size_t Size() const 00145 { 00146 return Units.size(); 00147 } 00148 00149 inline bool IsAttacking() const 00150 { 00151 return (!Defending && Attacking); 00152 } 00153 00154 void Attack(const Vec2i &pos); 00155 void Clean(); 00156 int PlanAttack(); 00157 00158 private: 00159 void CountTypes(unsigned int *counter, const size_t len); 00160 bool IsBelongsTo(const CUnitType *type); 00161 void Insert(CUnit &unit) 00162 { 00163 Units.Insert(&unit); 00164 unit.RefsIncrease(); 00165 } 00166 00167 void Update(); 00168 00169 static void InternalRemoveUnit(CUnit *unit) { 00170 unit->GroupId = 0; 00171 unit->RefsDecrease(); 00172 } 00173 00174 public: 00175 bool Completed; 00176 bool Defending; 00177 bool Attacking; 00178 char Role; 00179 00180 std::vector<AiUnitType> UnitTypes; 00181 CUnitCache Units; 00182 00183 // 00184 // If attacking 00185 // 00186 AiForceAttackingState State; 00187 Vec2i GoalPos; 00188 }; 00189 00190 // forces 00191 #define AI_MAX_FORCES 10 00192 00193 00198 class AiForceManager { 00199 public: 00200 AiForceManager(); 00201 00202 inline size_t Size() const 00203 { 00204 return forces.size(); 00205 } 00206 00207 AiForce &operator[](unsigned int index) { 00208 return forces[index]; 00209 } 00210 00211 int getIndex(AiForce *force) { 00212 for (unsigned int i = 0; i < forces.size(); ++i) { 00213 if (force == &forces[i]) return i; 00214 } 00215 return -1; 00216 } 00217 00218 inline unsigned int getScriptForce(unsigned int index) { 00219 if (script[index] == -1) { 00220 script[index] = FindFreeForce(); 00221 } 00222 return script[index]; 00223 } 00224 00225 void Clean(); 00226 bool Assign(CUnit &unit); 00227 void Update(); 00228 unsigned int FindFreeForce(int role = AiForceRoleAttack); 00229 void CheckUnits(int *counter); 00230 private: 00231 std::vector<AiForce> forces; 00232 char script[AI_MAX_FORCES]; 00233 }; 00234 00240 class AiBuildQueue { 00241 public: 00242 AiBuildQueue() : Want(0), Made(0), Type(NULL), Wait(0), X(-1), Y(-1) {} 00243 00244 public: 00245 unsigned int Want; 00246 unsigned int Made; 00247 CUnitType *Type; 00248 unsigned long Wait; 00249 short int X; 00250 short int Y; 00251 }; 00252 00256 class AiExplorationRequest { 00257 public: 00258 AiExplorationRequest(const Vec2i& pos, int mask) : pos(pos), Mask(mask) { } 00259 00260 public: 00261 Vec2i pos; 00262 int Mask; 00263 }; 00264 00268 class AiTransportRequest { 00269 public: 00270 AiTransportRequest() : Unit(NULL) {} 00271 00272 public: 00273 CUnit *Unit; 00274 CUnit::COrder Order; 00275 }; 00276 00280 class PlayerAi { 00281 public: 00282 PlayerAi() : Player(NULL), AiType(NULL), 00283 SleepCycles(0), NeededMask(0), NeedSupply(false), 00284 ScriptDebug(false), LastExplorationGameCycle(0), 00285 LastCanNotMoveGameCycle(0), LastRepairBuilding(0) 00286 { 00287 memset(Reserve, 0, sizeof(Reserve)); 00288 memset(Used, 0, sizeof(Used)); 00289 memset(Needed, 0, sizeof(Needed)); 00290 memset(Collect, 0, sizeof(Collect)); 00291 memset(TriedRepairWorkers, 0, sizeof(TriedRepairWorkers)); 00292 } 00293 00294 public: 00295 CPlayer *Player; 00296 CAiType *AiType; 00297 // controller 00298 std::string Script; 00299 unsigned long SleepCycles; 00300 00301 AiForceManager Force; 00302 00303 // resource manager 00304 int Reserve[MaxCosts]; 00305 int Used[MaxCosts]; 00306 int Needed[MaxCosts]; 00307 int Collect[MaxCosts]; 00308 int NeededMask; 00309 bool NeedSupply; 00310 bool ScriptDebug; 00311 00312 std::vector<AiExplorationRequest> FirstExplorationRequest; 00313 unsigned long LastExplorationGameCycle; 00314 std::vector<AiTransportRequest> TransportRequests; 00315 unsigned long LastCanNotMoveGameCycle; 00316 std::vector<AiRequestType> UnitTypeRequests; 00317 std::vector<CUnitType *> UpgradeToRequests; 00318 std::vector<CUpgrade *> ResearchRequests; 00319 std::vector<AiBuildQueue> UnitTypeBuilt; 00320 int LastRepairBuilding; 00321 unsigned int TriedRepairWorkers[UnitMax]; 00322 }; 00323 00331 class AiHelper { 00332 public: 00337 std::vector<std::vector<CUnitType *> > Train; 00342 std::vector<std::vector<CUnitType *> > Build; 00347 std::vector<std::vector<CUnitType *> > Upgrade; 00352 std::vector<std::vector<CUnitType *> > Research; 00357 std::vector<std::vector<CUnitType *> > Repair; 00362 std::vector<std::vector<CUnitType *> > UnitLimit; 00367 std::vector<std::vector<CUnitType *> > Equiv; 00368 00373 std::vector<std::vector<CUnitType *> > Refinery; 00374 00379 std::vector<std::vector<CUnitType *> > Depots; 00380 }; 00381 00382 /*---------------------------------------------------------------------------- 00383 -- Variables 00384 ----------------------------------------------------------------------------*/ 00385 00386 extern std::vector<CAiType *> AiTypes; 00387 extern AiHelper AiHelpers; 00388 00389 extern int UnitTypeEquivs[UnitTypeMax + 1]; 00390 extern PlayerAi *AiPlayer; 00391 00392 /*---------------------------------------------------------------------------- 00393 -- Functions 00394 ----------------------------------------------------------------------------*/ 00395 00396 // 00397 // Resource manager 00398 // 00400 extern void AiAddUnitTypeRequest(CUnitType &type, int count); 00402 extern void AiAddUpgradeToRequest(CUnitType &type); 00404 extern void AiAddResearchRequest(CUpgrade *upgrade); 00406 extern void AiResourceManager(); 00408 extern void AiExplore(const Vec2i &pos, int exploreMask); 00410 extern void AiNewUnitTypeEquiv(CUnitType *a, CUnitType *b); 00412 extern void AiResetUnitTypeEquiv(); 00414 extern int AiFindUnitTypeEquiv(const CUnitType &type, int *result); 00416 extern int AiFindAvailableUnitTypeEquiv(const CUnitType &type, int *result); 00417 extern int AiGetBuildRequestsCount(PlayerAi*,int counter[UnitTypeMax]); 00418 00419 extern void AiNewDepotRequest(CUnit &worker); 00420 00421 // 00422 // Buildings 00423 // 00425 extern int AiFindBuildingPlace(const CUnit &worker, 00426 const CUnitType &type, int nx, int ny, Vec2i *dpos); 00427 00428 // 00429 // Forces 00430 // 00432 extern void AiCleanForces(); 00434 extern bool AiAssignToForce(CUnit &unit); 00436 extern void AiAssignFreeUnitsToForce(); 00438 extern void AiAttackWithForceAt(unsigned int force, int x, int y); 00440 extern void AiAttackWithForce(unsigned int force); 00442 extern void AiAttackWithForces(int *forces); 00443 00445 extern void AiForceManager(); 00446 00447 // 00448 // Plans 00449 // 00451 extern int AiFindWall(AiForce *force); 00454 extern void AiSendExplorers(); 00456 extern int AiEnemyUnitsInDistance(const CPlayer *player, const CUnitType *type, 00457 const Vec2i& pos, unsigned range); 00458 00459 // 00460 // Magic 00461 // 00463 extern void AiCheckMagic(); 00464 00466 00467 #endif // !__AI_LOCAL_H__