| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #ifndef GAME_CLIENT_PREDICTION_GAMEWORLD_H |
| 4 | #define GAME_CLIENT_PREDICTION_GAMEWORLD_H |
| 5 | |
| 6 | #include <game/gamecore.h> |
| 7 | #include <game/teamscore.h> |
| 8 | |
| 9 | #include <list> |
| 10 | #include <vector> |
| 11 | |
| 12 | class CCollision; |
| 13 | class CCharacter; |
| 14 | class CEntity; |
| 15 | class CMapBugs; |
| 16 | |
| 17 | class CGameWorld |
| 18 | { |
| 19 | public: |
| 20 | enum |
| 21 | { |
| 22 | ENTTYPE_PROJECTILE = 0, |
| 23 | ENTTYPE_LASER, |
| 24 | ENTTYPE_DOOR, |
| 25 | ENTTYPE_DRAGGER, |
| 26 | ENTTYPE_LIGHT, |
| 27 | ENTTYPE_GUN, |
| 28 | ENTTYPE_PLASMA, |
| 29 | ENTTYPE_PICKUP, |
| 30 | ENTTYPE_FLAG, |
| 31 | ENTTYPE_CHARACTER, |
| 32 | NUM_ENTTYPES |
| 33 | }; |
| 34 | |
| 35 | CWorldCore m_Core; |
| 36 | CTeamsCore m_Teams; |
| 37 | |
| 38 | CGameWorld(); |
| 39 | ~CGameWorld(); |
| 40 | void Init(CCollision *pCollision, CTuningParams *pTuningList, const CMapBugs *pMapBugs); |
| 41 | |
| 42 | CEntity *FindFirst(int Type); |
| 43 | CEntity *FindLast(int Type); |
| 44 | int FindEntities(vec2 Pos, float Radius, CEntity **ppEnts, int Max, int Type); |
| 45 | CCharacter *IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, const CCharacter *pNotThis = nullptr, int CollideWith = -1, const CCharacter *pThisOnly = nullptr); |
| 46 | CEntity *IntersectEntity(vec2 Pos0, vec2 Pos1, float Radius, int Type, vec2 &NewPos, const CEntity *pNotThis = nullptr, int CollideWith = -1, const CEntity *pThisOnly = nullptr); |
| 47 | void InsertEntity(CEntity *pEntity, bool Last = false); |
| 48 | void RemoveEntity(CEntity *pEntity); |
| 49 | void RemoveCharacter(CCharacter *pChar); |
| 50 | void Tick(); |
| 51 | |
| 52 | // DDRace |
| 53 | void ReleaseHooked(int ClientId); |
| 54 | std::vector<CCharacter *> IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis = nullptr); |
| 55 | |
| 56 | int m_GameTick; |
| 57 | |
| 58 | // getter for server variables |
| 59 | int GameTick() const { return m_GameTick; } |
| 60 | int GameTickSpeed() const { return SERVER_TICK_SPEED; } |
| 61 | const CCollision *Collision() const { return m_pCollision; } |
| 62 | CCollision *Collision() { return m_pCollision; } |
| 63 | CTeamsCore *Teams() { return &m_Teams; } |
| 64 | std::vector<SSwitchers> &Switchers() { return m_Core.m_vSwitchers; } |
| 65 | CEntity *GetEntity(int Id, int EntityType); |
| 66 | CCharacter *GetCharacterById(int Id) { return (Id >= 0 && Id < MAX_CLIENTS) ? m_apCharacters[Id] : nullptr; } |
| 67 | |
| 68 | // from gamecontext |
| 69 | void CreateExplosion(vec2 Pos, int Owner, int Weapon, bool NoDamage, int ActivatedTeam, CClientMask Mask); |
| 70 | |
| 71 | // for client side prediction |
| 72 | struct |
| 73 | { |
| 74 | bool m_IsDDRace; |
| 75 | bool m_IsVanilla; |
| 76 | bool m_IsFNG; |
| 77 | bool m_InfiniteAmmo; |
| 78 | bool m_PredictTiles; |
| 79 | int m_PredictFreeze; |
| 80 | bool m_PredictWeapons; |
| 81 | bool m_PredictDDRace; |
| 82 | bool m_IsSolo; |
| 83 | bool m_UseTuneZones; |
| 84 | bool m_BugDDRaceInput; |
| 85 | bool m_NoWeakHookAndBounce; |
| 86 | } m_WorldConfig; |
| 87 | |
| 88 | bool m_IsValidCopy; |
| 89 | CGameWorld *m_pParent; |
| 90 | CGameWorld *m_pChild; |
| 91 | |
| 92 | int m_LocalClientId; |
| 93 | |
| 94 | bool IsLocalTeam(int OwnerId) const; |
| 95 | void OnModified() const; |
| 96 | void NetObjBegin(CTeamsCore Teams, int LocalClientId); |
| 97 | void NetCharAdd(int ObjId, CNetObj_Character *pChar, CNetObj_DDNetCharacter *pExtended, int GameTeam, bool IsLocal); |
| 98 | void NetObjAdd(int ObjId, int ObjType, const void *pObjData, const CNetObj_EntityEx *pDataEx); |
| 99 | void NetObjEnd(); |
| 100 | void CopyWorld(CGameWorld *pFrom); |
| 101 | CEntity *FindMatch(int ObjId, int ObjType, const void *pObjData); |
| 102 | void Clear(); |
| 103 | |
| 104 | const CTuningParams *TuningList() const { return m_pTuningList; } |
| 105 | CTuningParams *TuningList() { return m_pTuningList; } |
| 106 | const CTuningParams *GlobalTuning() const { return &TuningList()[0]; } |
| 107 | CTuningParams *GlobalTuning() { return &TuningList()[0]; } |
| 108 | const CTuningParams *GetTuning(int i) const { return &TuningList()[i]; } |
| 109 | CTuningParams *GetTuning(int i) { return &TuningList()[i]; } |
| 110 | |
| 111 | bool EmulateBug(int Bug) const; |
| 112 | |
| 113 | private: |
| 114 | void RemoveEntities(); |
| 115 | |
| 116 | CEntity *m_pNextTraverseEntity = nullptr; |
| 117 | CEntity *m_apFirstEntityTypes[NUM_ENTTYPES]; |
| 118 | |
| 119 | CCharacter *m_apCharacters[MAX_CLIENTS]; |
| 120 | |
| 121 | CCollision *m_pCollision; |
| 122 | CTuningParams *m_pTuningList; |
| 123 | const CMapBugs *m_pMapBugs; |
| 124 | }; |
| 125 | |
| 126 | class CCharOrder |
| 127 | { |
| 128 | public: |
| 129 | std::list<int> m_Ids; // reverse of the order in the gameworld, since entities will be inserted in reverse |
| 130 | CCharOrder() |
| 131 | { |
| 132 | Reset(); |
| 133 | } |
| 134 | void Reset() |
| 135 | { |
| 136 | m_Ids.clear(); |
| 137 | for(int i = 0; i < MAX_CLIENTS; i++) |
| 138 | m_Ids.push_back(x: i); |
| 139 | } |
| 140 | void GiveStrong(int c) |
| 141 | { |
| 142 | if(0 <= c && c < MAX_CLIENTS) |
| 143 | { |
| 144 | m_Ids.remove(value: c); |
| 145 | m_Ids.push_front(x: c); |
| 146 | } |
| 147 | } |
| 148 | void GiveWeak(int c) |
| 149 | { |
| 150 | if(0 <= c && c < MAX_CLIENTS) |
| 151 | { |
| 152 | m_Ids.remove(value: c); |
| 153 | m_Ids.push_back(x: c); |
| 154 | } |
| 155 | } |
| 156 | bool HasStrongAgainst(int From, int To) |
| 157 | { |
| 158 | for(int i : m_Ids) |
| 159 | { |
| 160 | if(i == To) |
| 161 | return false; |
| 162 | else if(i == From) |
| 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | #endif |
| 170 | |