$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 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 __UNITTYPE_H__ 00031 #define __UNITTYPE_H__ 00032 00034 00035 /*---------------------------------------------------------------------------- 00036 -- Documentation 00037 ----------------------------------------------------------------------------*/ 00038 00513 /*---------------------------------------------------------------------------- 00514 -- Includes 00515 ----------------------------------------------------------------------------*/ 00516 00517 #include <SDL.h> 00518 #include <vector> 00519 #include <algorithm> 00520 00521 #ifndef __UPGRADE_STRUCTS_H__ 00522 #include "upgrade_structs.h" 00523 #endif 00524 00525 #ifndef __UTIL_H__ 00526 #include "util.h" 00527 #endif 00528 00529 #ifndef __UNITSOUND_H__ 00530 #include "unitsound.h" 00531 #endif 00532 00533 #ifndef __ICONS_H__ 00534 #include "icons.h" 00535 #endif 00536 00537 #ifndef __ANIMATIONS_H__ 00538 #include "animation.h" 00539 #endif 00540 00541 #include "vec2i.h" 00542 00543 /*---------------------------------------------------------------------------- 00544 -- Declarations 00545 ----------------------------------------------------------------------------*/ 00546 00547 class CPlayerColorGraphic; 00548 class CConstruction; 00549 class MissileType; 00550 class CFile; 00551 struct lua_State; 00552 #ifdef USE_MNG 00553 class Mng; 00554 #endif 00555 class LuaCallback; 00556 00557 CUnitType *UnitTypeByIdent(const std::string &ident); 00558 00559 enum GroupSelectionMode { 00560 SELECTABLE_BY_RECTANGLE_ONLY = 0, 00561 NON_SELECTABLE_BY_RECTANGLE_ONLY, 00562 SELECT_ALL 00563 }; 00564 00570 class MissileConfig { 00571 public: 00572 MissileConfig() : Missile(NULL) {} 00573 00574 std::string Name; 00575 MissileType *Missile; 00576 }; 00577 00578 class ResourceInfo { 00579 public: 00580 ResourceInfo() : WaitAtResource(0), ResourceStep(0), 00581 ResourceCapacity(0), WaitAtDepot(0), ResourceId(0), FinalResource(0), 00582 TerrainHarvester(0), LoseResources(0), HarvestFromOutside(0), 00583 SpriteWhenLoaded(NULL), SpriteWhenEmpty(NULL) 00584 {} 00585 00586 std::string FileWhenLoaded; 00587 std::string FileWhenEmpty; 00588 unsigned WaitAtResource; 00589 unsigned ResourceStep; 00590 int ResourceCapacity; 00591 unsigned WaitAtDepot; 00592 unsigned ResourceId; 00593 unsigned FinalResource; 00594 unsigned char TerrainHarvester; 00595 unsigned char LoseResources; 00596 unsigned char HarvestFromOutside; 00597 unsigned char RefineryHarvester; 00598 // Runtime info: 00599 CPlayerColorGraphic *SpriteWhenLoaded; 00600 CPlayerColorGraphic *SpriteWhenEmpty; 00601 }; 00602 00609 class CVariable { 00610 public: 00611 CVariable() : Max(0), Value(0), Increase(0), Enable(0) {} 00612 00613 int Max; 00614 int Value; 00615 char Increase; 00616 char Enable; 00617 }; 00618 00619 // Index for boolflag aready defined 00620 enum { 00621 COWARD_INDEX = 0, 00622 BUILDING_INDEX, 00623 FLIP_INDEX, 00624 REVEALER_INDEX, 00625 LANDUNIT_INDEX, 00626 AIRUNIT_INDEX, 00627 SEAUNIT_INDEX, 00628 EXPLODEWHENKILLED_INDEX, 00629 VISIBLEUNDERFOG_INDEX, 00630 PERMANENTCLOAK_INDEX, 00631 DETECTCLOAK_INDEX, 00632 ATTACKFROMTRANSPORTER_INDEX, 00633 VANISHES_INDEX, 00634 GROUNDATTACK_INDEX, 00635 SHOREBUILDING_INDEX, 00636 CANATTACK_INDEX, 00637 BUILDEROUTSIDE_INDEX, 00638 BUILDERLOST_INDEX, 00639 CANHARVEST_INDEX, 00640 HARVESTER_INDEX, 00641 SELECTABLEBYRECTANGLE_INDEX, 00642 ISNOTSELECTABLE_INDEX, 00643 DECORATION_INDEX, 00644 INDESTRUCTIBLE_INDEX, 00645 TELEPORTER_INDEX, 00646 SHIELDPIERCE_INDEX, 00647 SAVECARGO_INDEX, 00648 NBARALREADYDEFINED 00649 }; 00650 00651 // Index for variable already defined. 00652 enum { 00653 HP_INDEX = 0, 00654 BUILD_INDEX, 00655 MANA_INDEX, 00656 TRANSPORT_INDEX, 00657 RESEARCH_INDEX, 00658 TRAINING_INDEX, 00659 UPGRADINGTO_INDEX, 00660 GIVERESOURCE_INDEX, 00661 CARRYRESOURCE_INDEX, 00662 XP_INDEX, 00663 KILL_INDEX, 00664 SUPPLY_INDEX, 00665 DEMAND_INDEX, 00666 ARMOR_INDEX, 00667 SIGHTRANGE_INDEX, 00668 ATTACKRANGE_INDEX, 00669 PIERCINGDAMAGE_INDEX, 00670 BASICDAMAGE_INDEX, 00671 POSX_INDEX, 00672 POSY_INDEX, 00673 RADAR_INDEX, 00674 RADARJAMMER_INDEX, 00675 AUTOREPAIRRANGE_INDEX, 00676 BLOODLUST_INDEX, 00677 HASTE_INDEX, 00678 SLOW_INDEX, 00679 INVISIBLE_INDEX, 00680 UNHOLYARMOR_INDEX, 00681 SLOT_INDEX, 00682 SHIELD_INDEX, 00683 POINTS_INDEX, 00684 MAXHARVESTERS_INDEX, 00685 NVARALREADYDEFINED 00686 }; 00687 00688 class CUnit; 00689 class CUnitType; 00690 class CFont; 00691 00698 class CDecoVar { 00699 public: 00700 00701 CDecoVar() {}; 00702 virtual ~CDecoVar() { 00703 }; 00704 00706 virtual void Draw(int x, int y, const CUnitType *Type, const CVariable &var) const = 0; 00707 00708 unsigned int Index; 00709 00710 int OffsetX; 00711 int OffsetY; 00712 00713 int OffsetXPercent; 00714 int OffsetYPercent; 00715 00716 bool IsCenteredInX; 00717 bool IsCenteredInY; 00718 00719 bool ShowIfNotEnable; 00720 bool ShowWhenNull; 00721 bool HideHalf; 00722 bool ShowWhenMax; 00723 bool ShowOnlySelected; 00724 00725 bool HideNeutral; 00726 bool HideAllied; 00727 bool ShowOpponent; 00728 }; 00729 00730 class CDecoVarBar : public CDecoVar 00731 { 00732 public: 00734 virtual void Draw(int x, int y, const CUnitType *type, const CVariable &var) const; 00735 00736 bool IsVertical; 00737 bool SEToNW; 00738 int Height; 00739 int Width; 00740 bool ShowFullBackground; 00741 char BorderSize; 00742 // FIXME color depend of percent (red, Orange, Yellow, Green...) 00743 Uint32 Color; 00744 Uint32 BColor; 00745 }; 00746 00747 class CDecoVarText : public CDecoVar 00748 { 00749 public: 00750 CDecoVarText() : Font(NULL) {}; 00752 virtual void Draw(int x, int y, const CUnitType *type, const CVariable &var) const; 00753 00754 CFont *Font; 00755 // FIXME : Add Color, format 00756 }; 00757 00759 class CDecoVarSpriteBar : public CDecoVar 00760 { 00761 public: 00762 CDecoVarSpriteBar() : NSprite(-1) {}; 00764 virtual void Draw(int x, int y, 00765 const CUnitType *Type, const CVariable &Variable) const; 00766 00767 char NSprite; 00768 // FIXME Sprite info. better way ? 00769 }; 00770 00772 class CDecoVarStaticSprite : public CDecoVar 00773 { 00774 public: 00775 CDecoVarStaticSprite() : NSprite(-1), n(0) {} 00777 virtual void Draw(int x, int y, const CUnitType *type, const CVariable &var) const; 00778 00779 // FIXME Sprite info. and Replace n with more appropriate var. 00780 char NSprite; 00781 int n; 00782 }; 00783 00784 enum UnitTypeType { 00785 UnitTypeLand, 00786 UnitTypeFly, 00787 UnitTypeNaval 00788 }; 00789 00790 enum DistanceTypeType { 00791 Equal, 00792 NotEqual, 00793 LessThan, 00794 LessThanEqual, 00795 GreaterThan, 00796 GreaterThanEqual 00797 }; 00798 00799 00800 class CBuildRestriction { 00801 public: 00802 00803 virtual ~CBuildRestriction() {} ; 00804 virtual void Init() {}; 00805 virtual bool Check(const CUnitType &type, int x, int y, CUnit *&ontoptarget) const = 0; 00806 }; 00807 00808 00809 class CBuildRestrictionAnd : public CBuildRestriction { 00810 public: 00811 00812 virtual ~CBuildRestrictionAnd() { 00813 for (std::vector<CBuildRestriction*>::const_iterator i = _or_list.begin(); 00814 i != _or_list.end(); ++i) { 00815 delete *i; 00816 } 00817 _or_list.clear(); 00818 } ; 00819 virtual void Init() { 00820 for (std::vector<CBuildRestriction*>::const_iterator i = _or_list.begin(); 00821 i != _or_list.end(); ++i) { 00822 (*i)->Init(); 00823 } 00824 }; 00825 virtual bool Check(const CUnitType &type, int x, int y, CUnit *&ontoptarget) const; 00826 00827 void push_back(CBuildRestriction *restriction) { 00828 _or_list.push_back(restriction); 00829 } 00830 00831 std::vector<CBuildRestriction*> _or_list; 00832 }; 00833 00834 00835 00836 class CBuildRestrictionAddOn : public CBuildRestriction { 00837 struct functor { 00838 const CUnitType *const Parent; 00839 const int x,y; //functor work position 00840 functor(const CUnitType *type, int _x, int _y): Parent(type), x(_x), y(_y) {} 00841 inline bool operator() (const CUnit *const unit) const; 00842 }; 00843 public: 00844 CBuildRestrictionAddOn() : OffsetX(0), OffsetY(0), Parent(NULL) {}; 00845 virtual ~CBuildRestrictionAddOn() {}; 00846 virtual void Init() {this->Parent = UnitTypeByIdent(this->ParentName);}; 00847 virtual bool Check(const CUnitType &type, int x, int y, CUnit *&ontoptarget) const; 00848 00849 int OffsetX; 00850 int OffsetY; 00851 std::string ParentName; 00852 CUnitType *Parent; 00853 }; 00854 00855 class CBuildRestrictionOnTop : public CBuildRestriction { 00856 struct functor { 00857 CUnit *ontop; 00858 const CUnitType *const Parent; 00859 const int x,y; //functor work position 00860 functor(const CUnitType *type, int _x, int _y): ontop(0), Parent(type), x(_x), y(_y) {} 00861 inline bool operator() (CUnit *const unit); 00862 }; 00863 public: 00864 CBuildRestrictionOnTop() : Parent(NULL), ReplaceOnDie(0), ReplaceOnBuild(0) {}; 00865 virtual ~CBuildRestrictionOnTop() {}; 00866 virtual void Init() {this->Parent = UnitTypeByIdent(this->ParentName);}; 00867 virtual bool Check(const CUnitType &type, int x, int y, CUnit *&ontoptarget) const; 00868 00869 std::string ParentName; 00870 CUnitType *Parent; 00871 int ReplaceOnDie:1; 00872 int ReplaceOnBuild:1; 00873 }; 00874 00875 class CBuildRestrictionDistance : public CBuildRestriction { 00876 public: 00877 CBuildRestrictionDistance() : Distance(0), RestrictType(NULL) {}; 00878 virtual ~CBuildRestrictionDistance() {}; 00879 virtual void Init() {this->RestrictType = UnitTypeByIdent(this->RestrictTypeName);}; 00880 virtual bool Check(const CUnitType &type, int x, int y, CUnit *&ontoptarget) const; 00881 00882 int Distance; 00883 DistanceTypeType DistanceType; 00884 std::string RestrictTypeName; 00885 CUnitType *RestrictType; 00886 }; 00887 00890 class CUnitType { 00891 public: 00892 CUnitType(); 00893 ~CUnitType(); 00894 00895 Vec2i GetHalfTileSize() const { 00896 Vec2i res = {TileWidth / 2, TileHeight / 2}; 00897 00898 return res; 00899 } 00900 00901 std::string Ident; 00902 std::string Name; 00903 int Slot; 00904 std::string File; 00905 std::string ShadowFile; 00906 00907 int Width; 00908 int Height; 00909 int OffsetX; 00910 int OffsetY; 00911 int DrawLevel; 00912 int ShadowWidth; 00913 int ShadowHeight; 00914 int ShadowOffsetX; 00915 int ShadowOffsetY; 00916 00917 CAnimations *Animations; 00918 int StillFrame; 00919 00920 IconConfig Icon; 00921 #ifdef USE_MNG 00922 struct _portrait_ { 00923 std::string *Files; 00924 int Num; 00925 Mng **Mngs; 00926 int CurrMng; 00927 int NumIterations; 00928 } Portrait; 00929 #endif 00930 MissileConfig Missile; 00931 MissileConfig Explosion; 00932 00933 LuaCallback *DeathExplosion; 00934 00935 std::string DamageType; 00936 00937 std::string CorpseName; 00938 CUnitType *CorpseType; 00939 00940 CConstruction *Construction; 00941 00942 int _Costs[MaxCosts]; 00943 int RepairHP; 00944 int RepairCosts[MaxCosts]; 00945 00946 int TileWidth; 00947 int TileHeight; 00948 int BoxWidth; 00949 int BoxHeight; 00950 int NumDirections; 00951 int MinAttackRange; 00952 int ReactRangeComputer; 00953 int ReactRangePerson; 00954 int Priority; 00955 int BurnPercent; 00956 int BurnDamageRate; 00957 int RepairRange; 00958 char *CanCastSpell; 00959 char *AutoCastActive; 00960 int AutoBuildRate; 00961 int RandomMovementProbability; 00962 int ClicksToExplode; 00963 int MaxOnBoard; 00964 int StartingResources; 00965 00966 UnitTypeType UnitType; 00967 int DecayRate; 00968 // TODO: not used 00969 int AnnoyComputerFactor; 00970 int MouseAction; 00971 #define MouseActionNone 0 00972 #define MouseActionAttack 1 00973 #define MouseActionMove 2 00974 #define MouseActionHarvest 3 00975 #define MouseActionSpellCast 5 00976 #define MouseActionSail 6 00977 int CanTarget; 00978 #define CanTargetLand 1 00979 #define CanTargetSea 2 00980 #define CanTargetAir 4 00981 00982 unsigned Flip : 1; 00983 unsigned Revealer : 1; 00984 unsigned LandUnit : 1; 00985 unsigned AirUnit : 1; 00986 unsigned SeaUnit : 1; 00987 unsigned ExplodeWhenKilled : 1; 00988 unsigned Building : 1; 00989 unsigned VisibleUnderFog : 1; 00990 unsigned PermanentCloak : 1; 00991 unsigned DetectCloak : 1; 00992 unsigned Coward : 1; 00993 unsigned AttackFromTransporter : 1; 00994 unsigned Vanishes : 1; 00995 unsigned GroundAttack : 1; 00996 unsigned ShoreBuilding : 1; 00997 unsigned CanAttack : 1; 00998 unsigned BuilderOutside : 1; 00999 unsigned BuilderLost : 1; 01000 unsigned CanHarvest : 1; 01001 unsigned Harvester : 1; 01002 unsigned Neutral : 1; 01003 01004 unsigned SelectableByRectangle : 1; 01005 unsigned IsNotSelectable : 1; 01006 unsigned Decoration : 1; 01007 unsigned Indestructible : 1; 01008 unsigned Teleporter : 1; 01009 unsigned ShieldPiercing : 1; 01010 unsigned SaveCargo : 1; 01011 01012 CVariable *Variable; 01013 struct BoolFlags { 01014 bool value; 01015 char CanTransport; 01016 char CanTargetFlag; 01017 }; 01018 std::vector<BoolFlags> BoolFlag; 01019 01020 int CanStore[MaxCosts]; 01021 int GivesResource; 01022 ResourceInfo *ResInfo[MaxCosts]; 01023 std::vector<CBuildRestriction *> BuildingRules; 01024 SDL_Color NeutralMinimapColorRGB; 01025 01026 CUnitSound Sound; 01027 01028 int Supply; 01029 int Demand; 01030 01031 // --- FILLED UP --- 01032 01033 int ImproveIncomes[MaxCosts]; 01034 01035 unsigned FieldFlags; 01036 unsigned MovementMask; 01037 01039 CUnitStats Stats[PlayerMax]; 01040 01041 CPlayerColorGraphic *Sprite; 01042 CGraphic *ShadowSprite; 01043 01044 /* API */ 01045 01046 bool CheckUserBoolFlags(char *BoolFlags); 01047 bool CanTransport() const 01048 { 01049 return MaxOnBoard > 0 && !GivesResource; 01050 } 01051 bool CanMove() const 01052 { 01053 return Animations && Animations->Move[99]; 01054 } 01055 01056 bool CanSelect(GroupSelectionMode mode = SELECTABLE_BY_RECTANGLE_ONLY) const 01057 { 01058 if(!IsNotSelectable) { 01059 switch(mode) { 01060 case SELECTABLE_BY_RECTANGLE_ONLY: 01061 return SelectableByRectangle; 01062 case NON_SELECTABLE_BY_RECTANGLE_ONLY: 01063 return !SelectableByRectangle; 01064 default: 01065 return true; 01066 } 01067 } 01068 return false; 01069 } 01070 01071 }; 01072 01073 /*---------------------------------------------------------------------------- 01074 -- Variables 01075 ----------------------------------------------------------------------------*/ 01076 01077 extern std::vector<CUnitType *> UnitTypes; 01078 01080 extern CUnitType *UnitTypeHumanWall; 01081 extern CUnitType *UnitTypeOrcWall; 01082 01086 class CUnitTypeVar { 01087 public: 01088 01089 template <const unsigned int SIZE> 01090 struct CKeys { 01091 01092 struct DataKey 01093 { 01094 static bool key_pred(const DataKey& lhs, 01095 const DataKey& rhs) 01096 { 01097 return ((lhs.keylen == rhs.keylen) ? 01098 (strcmp(lhs.key, rhs.key) < 0) : (lhs.keylen < rhs.keylen)); 01099 } 01100 int offset; 01101 unsigned int keylen; 01102 const char* key; 01103 }; 01104 01105 CKeys(): TotalKeys(SIZE) {} 01106 01107 DataKey buildin[SIZE]; 01108 std::map<std::string, int> user; 01109 unsigned int TotalKeys; 01110 01111 void Init() { 01112 std::sort(buildin, buildin+SIZE, DataKey::key_pred); 01113 } 01114 01115 const char *operator[](int index) { 01116 for (unsigned int i = 0; i < SIZE; ++i) { 01117 if (buildin[i].offset == index) { 01118 return buildin[i].key; 01119 } 01120 } 01121 for (std::map<std::string, int>::iterator 01122 it(user.begin()), end(user.end()); 01123 it != end; ++it) { 01124 if ((*it).second == index) { 01125 return ((*it).first).c_str(); 01126 } 01127 } 01128 return NULL; 01129 } 01130 01138 int operator[](const char*const key) { 01139 DataKey k; 01140 k.key = key; 01141 k.keylen = strlen(key); 01142 const DataKey* p = std::lower_bound(buildin, buildin + SIZE, 01143 k, DataKey::key_pred); 01144 if ((p != buildin + SIZE) && p->keylen == k.keylen && 01145 0 == strcmp(p->key, key)) { 01146 return p->offset; 01147 } else { 01148 std::map<std::string, int>::iterator 01149 ret(user.find(key)); 01150 if (ret != user.end()) { 01151 return (*ret).second; 01152 } 01153 } 01154 return -1; 01155 } 01156 01157 int AddKey(const char*const key) 01158 { 01159 int index = this->operator[](key); 01160 if (index != -1) { 01161 DebugPrint("Warning, Key '%s' already defined\n" _C_ key); 01162 return index; 01163 } 01164 user[key] = TotalKeys++; 01165 return TotalKeys - 1; 01166 } 01167 01168 }; 01169 01170 struct CBoolKeys : public CKeys<NBARALREADYDEFINED> { 01171 CBoolKeys(); 01172 }; 01173 01174 struct CVariableKeys : public CKeys<NVARALREADYDEFINED> { 01175 CVariableKeys(); 01176 }; 01177 01178 CUnitTypeVar() {} 01179 01180 void Init(); 01181 void Clear(); 01182 01183 CBoolKeys BoolFlagNameLookup; 01184 CVariableKeys VariableNameLookup; 01185 01186 // EventType *Event; /// Array of functions sets to call when en event occurs. 01187 std::vector<CVariable> Variable; 01188 std::vector<CDecoVar *> DecoVar; 01189 01190 unsigned int GetNumberBoolFlag() const { 01191 return BoolFlagNameLookup.TotalKeys; 01192 } 01193 01194 unsigned int GetNumberVariable() const { 01195 return VariableNameLookup.TotalKeys; 01196 } 01197 }; 01198 01199 extern CUnitTypeVar UnitTypeVar; 01200 01201 /*---------------------------------------------------------------------------- 01202 -- Functions 01203 ----------------------------------------------------------------------------*/ 01204 extern CUnitType *CclGetUnitType(lua_State *l); 01205 extern void UnitTypeCclRegister(); 01206 01207 extern void UpdateStats(int reset_to_default); 01208 extern CUnitType *UnitTypeByIdent(const std::string &ident); 01209 01210 extern void SaveUnitTypes(CFile *file); 01211 extern CUnitType *NewUnitTypeSlot(const std::string &ident); 01212 01213 extern void DrawUnitType(const CUnitType &type, CPlayerColorGraphic *sprite, 01214 int player, int frame, int x, int y); 01215 01216 extern void InitUnitTypes(int reset_player_stats); 01217 extern void LoadUnitTypeSprite(CUnitType &unittype); 01218 extern void LoadUnitTypes(); 01219 extern void CleanUnitTypes(); 01220 01221 // in script_unittype.c 01222 01224 extern void DefineVariableField(lua_State *l, CVariable *var, int lua_index); 01225 01227 extern void UpdateUnitVariables(const CUnit &unit); 01228 01230 01231 #endif // !__UNITTYPE_H__