$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-2007 by Lutz Sammer and Jimmy Salmon 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_H__ 00031 #define __UNIT_H__ 00032 00034 00035 /*---------------------------------------------------------------------------- 00036 -- Includes 00037 ----------------------------------------------------------------------------*/ 00038 00039 #include <vector> 00040 00041 #ifndef __UNITTYPE_H__ 00042 #include "unittype.h" 00043 #endif 00044 00045 #ifndef __PLAYER_H__ 00046 #include "player.h" 00047 #endif 00048 00049 #include "vec2i.h" 00050 00051 /*---------------------------------------------------------------------------- 00052 -- Declarations 00053 ----------------------------------------------------------------------------*/ 00054 00055 class CAnimation; 00056 class CBuildRestrictionOnTop; 00057 class CConstructionFrame; 00058 class CFile; 00059 class Missile; 00060 class COrder; 00061 class CPlayer; 00062 class CUnit; 00063 class CUnitColors; 00064 class CUnitPtr; 00065 class CUnitStats; 00066 class CUnitType; 00067 class CUpgrade; 00068 class CVariable; 00069 class CViewport; 00070 class PathFinderData; 00071 class SpellType; 00072 struct lua_State; 00073 00074 typedef COrder *COrderPtr; 00075 00076 /* 00077 ** Configuration of the small (unit) AI. 00078 */ 00079 #define PRIORITY_FACTOR 0x00001000 00080 #define HEALTH_FACTOR 0x00000001 00081 #define DISTANCE_FACTOR 0x00010000 00082 #define INRANGE_FACTOR 0x00001000 00083 #define INRANGE_BONUS 0x00100000 00084 #define CANATTACK_BONUS 0x00010000 00085 #define AIPRIORITY_BONUS 0x01000000 00086 00087 00089 extern void SelectedUnitChanged(); 00090 00092 extern int MapDistanceBetweenTypes(const CUnitType &src, const Vec2i &pos1, 00093 const CUnitType &dst, const Vec2i &pos2); 00094 00103 enum _directions_ { 00104 LookingN = 0 * 32, 00105 LookingNE = 1 * 32, 00106 LookingE = 2 * 32, 00107 LookingSE = 3 * 32, 00108 LookingS = 4 * 32, 00109 LookingSW = 5 * 32, 00110 LookingW = 6 * 32, 00111 LookingNW = 7 * 32 00112 }; 00113 00114 #define NextDirection 32 00115 #define UnitNotSeen 0x7fffffff 00116 00118 class CUnit 00119 { 00120 public: 00121 CUnit() : tilePos(-1, -1), pathFinderData(NULL), SavedOrder(NULL), NewOrder(NULL), CriticalOrder(NULL) { Init(); } 00122 00123 void Init(); 00124 00125 COrder *CurrentOrder() const { return Orders[0]; } 00126 00127 unsigned int CurrentAction() const; 00128 00129 bool IsIdle() const; 00130 00131 void ClearAction(); 00132 00134 void RefsIncrease(); 00136 void RefsDecrease(); 00137 00139 void Init(const CUnitType &type); 00141 void AssignToPlayer(CPlayer &player); 00142 00144 void Draw(const CViewport &vp) const; 00146 void Place(const Vec2i &pos); 00147 00149 void MoveToXY(const Vec2i &pos); 00151 void AddInContainer(CUnit &host); 00153 void ChangeOwner(CPlayer &newplayer); 00154 00156 void Remove(CUnit *host); 00157 00158 void AssignWorkerToMine(CUnit &mine); 00159 void DeAssignWorkerFromMine(CUnit &mine); 00160 00162 void Release(bool final = false); 00163 00164 bool RestoreOrder(); 00165 bool CanStoreOrder(COrder *order); 00166 00167 // Cowards and invisible units don't attack unless ordered. 00168 bool IsAgressive() const { 00169 return (Type->CanAttack && !Type->Coward 00170 && Variable[INVISIBLE_INDEX].Value == 0); 00171 } 00172 00174 bool IsVisible(const CPlayer &player) const; 00175 00176 inline bool IsInvisibile(const CPlayer &player) const { 00177 return (&player != Player && !!Variable[INVISIBLE_INDEX].Value 00178 && !player.IsBothSharedVision(*Player)); 00179 } 00180 00187 bool IsAlive() const; 00188 00195 inline bool IsAliveOnMap() const { 00196 return !Removed && IsAlive(); 00197 } 00198 00206 inline bool IsVisibleAsGoal(const CPlayer &player) const { 00207 // Invisibility 00208 if (IsInvisibile(player)) { 00209 return false; 00210 } 00211 // Don't attack revealers 00212 if (this->Type->Revealer) { 00213 return false; 00214 } 00215 if ((player.Type == PlayerComputer && !this->Type->PermanentCloak) 00216 || IsVisible(player) || IsVisibleOnRadar(player)) { 00217 return IsAliveOnMap(); 00218 } else { 00219 return Type->VisibleUnderFog 00220 && (Seen.ByPlayer & (1 << player.Index)) 00221 && !(Seen.Destroyed & (1 << player.Index)); 00222 } 00223 } 00224 00233 inline bool IsVisibleOnMap(const CPlayer &player) const { 00234 return IsAliveOnMap() && !IsInvisibile(player) && IsVisible(player); 00235 } 00236 00238 bool IsVisibleOnMinimap() const; 00239 00240 // Returns true if unit is visible under radar (By player, or by shared vision) 00241 bool IsVisibleOnRadar(const CPlayer &pradar) const; 00242 00244 bool IsVisibleInViewport(const CViewport &vp) const; 00245 00246 bool IsEnemy(const CPlayer &player) const; 00247 bool IsEnemy(const CUnit &unit) const; 00248 bool IsAllied(const CPlayer &player) const; 00249 bool IsAllied(const CUnit &unit) const; 00250 bool IsSharedVision(const CPlayer &player) const; 00251 bool IsSharedVision(const CUnit &unit) const; 00252 bool IsBothSharedVision(const CPlayer &player) const; 00253 bool IsBothSharedVision(const CUnit &unit) const; 00254 bool IsTeamed(const CPlayer &player) const; 00255 bool IsTeamed(const CUnit &unit) const; 00256 00257 bool IsUnusable(bool ignore_built_state = false) const; 00258 00266 int MapDistanceTo(const CUnit &dst) const { 00267 return MapDistanceBetweenTypes(*Type, tilePos, *dst.Type, dst.tilePos); 00268 } 00269 00270 int MapDistanceTo(const Vec2i &pos) const; 00271 00278 bool CanMove() const { return Type->CanMove(); } 00279 00280 int GetDrawLevel() const; 00281 00282 PixelPos GetMapPixelPosTopLeft() const; 00283 PixelPos GetMapPixelPosCenter() const; 00284 00285 public: 00286 class CUnitManagerData 00287 { 00288 friend class CUnitManager; 00289 public: 00290 CUnitManagerData() : slot(-1), unitSlot(-1) {} 00291 00292 int GetUnitId() const { return slot; } 00293 private: 00294 int slot; 00295 int unitSlot; 00296 }; 00297 public: 00298 // @note int is faster than shorts 00299 unsigned int Refs; 00300 unsigned int ReleaseCycle; 00301 CUnitManagerData UnitManagerData; 00302 size_t PlayerSlot; 00303 00304 int InsideCount; 00305 int BoardCount; 00306 CUnit *UnitInside; 00307 CUnit *Container; 00308 CUnit *NextContained; 00309 CUnit *PrevContained; 00310 00311 CUnit *NextWorker; //pointer to next assigned worker to "Goal" resource. 00312 struct { 00313 CUnit *Workers; 00314 int Assigned; 00315 int Active; 00316 } Resource; 00317 00318 Vec2i tilePos; 00319 00320 unsigned int Offset; 00321 00322 const CUnitType *Type; 00323 CPlayer *Player; 00324 const CUnitStats *Stats; 00325 int CurrentSightRange; 00326 00327 // Pathfinding stuff: 00328 PathFinderData *pathFinderData; 00329 00330 // DISPLAY: 00331 int Frame; 00332 CUnitColors *Colors; 00333 00334 signed char IX; 00335 signed char IY; 00336 unsigned char Direction; //: 8; /// angle (0-255) unit looking 00337 unsigned char CurrentResource; 00338 int ResourcesHeld; 00339 00340 unsigned char DamagedType; 00341 unsigned long Attacked; 00342 unsigned Blink : 3; 00343 unsigned Moving : 1; 00344 unsigned ReCast : 1; 00345 unsigned AutoRepair : 1; 00346 00347 unsigned Burning : 1; 00348 unsigned Destroyed : 1; 00349 unsigned Removed : 1; 00350 unsigned Selected : 1; 00351 00352 unsigned Constructed : 1; 00353 unsigned Active : 1; 00354 unsigned Boarded : 1; 00355 unsigned CacheLock : 1; 00356 00360 unsigned Rs : 8; 00361 00362 unsigned TeamSelected; 00363 CPlayer *RescuedFrom; 00364 00365 /* Seen stuff. */ 00366 int VisCount[PlayerMax]; 00367 struct _seen_stuff_ { 00368 _seen_stuff_() : CFrame(NULL), Type(NULL), tilePos(-1, -1) {} 00369 const CConstructionFrame *CFrame; 00370 int Frame; 00371 const CUnitType *Type; 00372 Vec2i tilePos; 00373 signed char IX; 00374 signed char IY; 00375 unsigned Constructed : 1; 00376 unsigned State : 3; 00377 unsigned Destroyed : PlayerMax; 00378 unsigned ByPlayer : PlayerMax; 00379 } Seen; 00380 00381 CVariable *Variable; 00382 00383 unsigned long TTL; 00384 00385 int GroupId; 00386 int LastGroup; 00387 00388 unsigned int Wait; 00389 int Threshold; 00390 00391 struct _unit_anim_ { 00392 const CAnimation *Anim; 00393 const CAnimation *CurrAnim; 00394 int Wait; 00395 int Unbreakable; 00396 } Anim; 00397 00398 00399 std::vector<COrder *> Orders; 00400 COrder *SavedOrder; 00401 COrder *NewOrder; 00402 COrder *CriticalOrder; 00403 00404 char *AutoCastSpell; 00405 00406 CUnit *Goal; 00407 }; 00408 00409 #define NoUnitP (CUnit *)0 00410 00411 00414 #define UnitNumber(unit) ((unit).UnitManagerData.GetUnitId()) 00415 00419 class CPreference 00420 { 00421 public: 00422 CPreference() : ShowSightRange(false), ShowReactionRange(false), 00423 ShowAttackRange(false), ShowMessages(true), 00424 BigScreen(false), ShowOrders(0), ShowNameDelay(0), ShowNameTime(0) {}; 00425 00426 bool ShowSightRange; 00427 bool ShowReactionRange; 00428 bool ShowAttackRange; 00429 bool ShowMessages; 00430 bool BigScreen; 00431 00432 int ShowOrders; 00433 int ShowNameDelay; 00434 int ShowNameTime; 00435 }; 00436 00437 extern CPreference Preference; 00438 00439 /*---------------------------------------------------------------------------- 00440 -- Variables 00441 ----------------------------------------------------------------------------*/ 00442 00443 // in unit_draw.c 00445 extern unsigned long ShowOrdersCount; 00446 extern unsigned long ShowNameDelay; 00447 extern unsigned long ShowNameTime; 00448 extern bool EnableTrainingQueue; 00449 extern bool EnableBuildingCapture; 00450 extern bool RevealAttacker; 00451 extern int ResourcesMultiBuildersMultiplier; 00452 extern const CViewport *CurrentViewport; 00453 extern void DrawUnitSelection(const CViewport &vp, const CUnit &unit); 00454 extern void (*DrawSelection)(Uint32, int, int, int, int); 00455 extern int MaxSelectable; 00456 00457 extern CUnit **Selected; 00458 extern CUnit **TeamSelected[PlayerMax]; 00459 extern int NumSelected; 00460 extern int TeamNumSelected[PlayerMax]; 00461 00462 /*---------------------------------------------------------------------------- 00463 -- Functions 00464 ----------------------------------------------------------------------------*/ 00465 00467 void MarkUnitFieldFlags(const CUnit &unit); 00469 void UnmarkUnitFieldFlags(const CUnit &unit); 00471 void UpdateUnitSightRange(CUnit &unit); 00473 extern CUnit *MakeUnit(const CUnitType &type, CPlayer *player); 00475 extern CUnit *MakeUnitAndPlace(const Vec2i &pos, const CUnitType &type, CPlayer *player); 00477 extern void UnitLost(CUnit &unit); 00479 extern void UnitClearOrders(CUnit &unit); 00481 extern void UpdateForNewUnit(const CUnit &unit, int upgrade); 00483 extern void NearestOfUnit(const CUnit &unit, const Vec2i &pos, Vec2i *dpos); 00484 00485 extern CUnit *GetFirstContainer(const CUnit &unit); 00486 00488 extern void UnitGoesUnderFog(CUnit &unit, const CPlayer &player); 00490 extern void UnitGoesOutOfFog(CUnit &unit, const CPlayer &player); 00492 extern void UnitsOnTileMarkSeen(const CPlayer &player, int x, int y, int p); 00493 extern void UnitsOnTileMarkSeen(const CPlayer &player, unsigned int index, int p); 00494 00496 extern void UnitsOnTileUnmarkSeen(const CPlayer &player, int x, int y, int p); 00497 extern void UnitsOnTileUnmarkSeen(const CPlayer &player, unsigned int index, int p); 00498 00500 extern void UnitCountSeen(CUnit &unit); 00501 00503 extern void RescueUnits(); 00504 00506 extern int DirectionToHeading(const Vec2i &dir); 00508 extern int DirectionToHeading(const PixelDiff &dir); 00509 00511 extern void CorrectWallDirections(CUnit &unit); 00513 extern void CorrectWallNeighBours(CUnit &unit); 00514 00516 extern void UnitUpdateHeading(CUnit &unit); 00518 extern void UnitHeadingFromDeltaXY(CUnit &unit, const Vec2i &delta); 00519 00520 00522 extern void DropOutOnSide(CUnit &unit, int heading, const CUnit *container); 00524 extern void DropOutNearest(CUnit &unit, const Vec2i &goalPos, const CUnit *container); 00525 00527 extern void DropOutAll(const CUnit &unit); 00528 00530 extern CBuildRestrictionOnTop *OnTopDetails(const CUnit &unit, const CUnitType *parent); 00532 extern CUnit *CanBuildHere(const CUnit *unit, const CUnitType &type, const Vec2i &pos); 00534 extern bool CanBuildOn(const Vec2i &pos, int mask); 00536 extern CUnit *CanBuildUnitType(const CUnit *unit, const CUnitType &type, const Vec2i &pos, int real); 00538 extern int ExtraDeathIndex(const char *death); 00539 00541 extern CUnit *UnitOnScreen(CUnit *unit, int x, int y); 00542 00544 extern void LetUnitDie(CUnit &unit); 00546 extern void DestroyAllInside(CUnit &source); 00548 extern int ThreatCalculate(const CUnit &unit, const CUnit &dest); 00550 extern void HitUnit(CUnit *attacker, CUnit &target, int damage, const Missile *missile = NULL); 00551 00553 extern int ViewPointDistance(const Vec2i &pos); 00555 extern int ViewPointDistanceToUnit(const CUnit &dest); 00556 00558 extern int CanTarget(const CUnitType *type, const CUnitType *dest); 00560 extern int CanTransport(const CUnit &transporter, const CUnit &unit); 00561 00563 extern std::string UnitReference(const CUnit &unit); 00565 extern std::string UnitReference(const CUnitPtr &unit); 00566 00568 extern void SaveUnit(const CUnit &unit, CFile &file); 00569 00571 extern void InitUnits(); 00573 extern void CleanUnits(); 00574 00575 // in unit_draw.c 00576 //-------------------- 00578 extern void DrawSelectionNone(Uint32, int, int, int, int); 00580 extern void DrawSelectionCircle(Uint32, int, int, int, int); 00582 extern void DrawSelectionCircleWithTrans(Uint32, int, int, int, int); 00584 extern void DrawSelectionRectangle(Uint32, int, int, int, int); 00586 extern void DrawSelectionRectangleWithTrans(Uint32, int, int, int, int); 00588 extern void DrawSelectionCorners(Uint32, int, int, int, int); 00589 00591 extern void DecorationCclRegister(); 00593 extern void LoadDecorations(); 00595 extern void CleanDecorations(); 00596 00598 extern void DrawShadow(const CUnitType &type, int frame, const PixelPos &screenPos); 00600 extern int FindAndSortUnits(const CViewport &vp, std::vector<CUnit *> &table); 00601 00603 extern void ShowOrder(const CUnit &unit); 00604 00605 // in groups.c 00606 00608 extern void InitGroups(); 00610 extern void SaveGroups(CFile &file); 00612 extern void CleanGroups(); 00613 00614 // 2 functions to conseal the groups internal data structures... 00616 extern int GetNumberUnitsOfGroup(int num, GroupSelectionMode mode = SELECTABLE_BY_RECTANGLE_ONLY); 00618 extern CUnit **GetUnitsOfGroup(int num); 00619 00621 extern void ClearGroup(int num); 00623 extern void AddToGroup(CUnit **units, int nunits, int num); 00625 extern void SetGroup(CUnit **units, int nunits, int num); 00627 extern void RemoveUnitFromGroups(CUnit &unit); 00629 extern void GroupCclRegister(); 00630 extern bool IsGroupTainted(int num); 00631 00632 // in selection.c 00633 00635 #define IsOnlySelected(unit) (NumSelected == 1 && Selected[0] == &(unit)) 00636 00638 extern void SaveSelection(); 00640 extern void RestoreSelection(); 00642 extern void UnSelectAll(); 00644 extern void ChangeTeamSelectedUnits(CPlayer &player, const std::vector<CUnit *> &units, int adjust); 00646 extern int SelectUnit(CUnit &unit); 00648 extern void SelectSingleUnit(CUnit &unit); 00650 extern void UnSelectUnit(CUnit &unit); 00652 extern int ToggleSelectUnit(CUnit &unit); 00654 extern int SelectUnitsByType(CUnit &base); 00656 extern int ToggleUnitsByType(CUnit &base); 00658 extern int SelectGroup(int group_number, GroupSelectionMode mode = SELECTABLE_BY_RECTANGLE_ONLY); 00660 extern int AddGroupFromUnitToSelection(CUnit &unit); 00662 extern int SelectGroupFromUnit(CUnit &unit); 00664 extern int SelectUnitsInRectangle(const PixelPos &corner_topleft, const PixelPos &corner_bottomright); 00666 extern int SelectGroundUnitsInRectangle(const PixelPos &corner_topleft, const PixelPos &corner_bottomright); 00668 extern int SelectAirUnitsInRectangle(const PixelPos &corner_topleft, const PixelPos &corner_bottomright); 00670 extern int AddSelectedUnitsInRectangle(const PixelPos &corner_topleft, const PixelPos &corner_bottomright); 00672 extern int AddSelectedGroundUnitsInRectangle(const PixelPos &corner_topleft, const PixelPos &corner_bottomright); 00674 extern int AddSelectedAirUnitsInRectangle(const PixelPos &corner_topleft, const PixelPos &corner_bottomright); 00675 00677 extern void InitSelections(); 00679 extern void SaveSelections(CFile &file); 00681 extern void CleanSelections(); 00683 extern void SelectionCclRegister(); 00684 00685 // in ccl_unit.c 00686 00688 extern void UnitCclRegister(); 00689 00691 00692 #endif // !__UNIT_H__