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
12class CCollision;
13class CCharacter;
14class CEntity;
15class CMapBugs;
16
17class CGameWorld
18{
19public:
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 class CEntityIterator
36 {
37 friend CGameWorld;
38
39 CGameWorld *m_pWorld;
40 CEntity *m_pNext = nullptr;
41 bool m_AllowEntityRemoval = false;
42
43 public:
44 CEntityIterator(CGameWorld *pWorld, CEntity *pFirstEntity);
45 CEntityIterator(CGameWorld *pWorld, int EntityType);
46 ~CEntityIterator();
47 void AllowEntityRemoval();
48 CEntity *Next();
49 };
50
51 CWorldCore m_Core;
52 CTeamsCore m_Teams;
53
54 CGameWorld();
55 ~CGameWorld();
56 void Init(CCollision *pCollision, CTuningParams *pTuningList, const CMapBugs *pMapBugs);
57
58 int FindEntities(vec2 Pos, float Radius, CEntity **ppEnts, int Max, int Type);
59 CCharacter *IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, const CCharacter *pNotThis = nullptr, int CollideWith = -1, const CCharacter *pThisOnly = nullptr);
60 CEntity *IntersectEntity(vec2 Pos0, vec2 Pos1, float Radius, int Type, vec2 &NewPos, const CEntity *pNotThis = nullptr, int CollideWith = -1, const CEntity *pThisOnly = nullptr);
61 void InsertEntity(CEntity *pEntity, bool Last = false);
62 void RemoveEntity(CEntity *pEntity);
63 void RemoveCharacter(CCharacter *pChar);
64 void Tick();
65
66 // DDRace
67 void ReleaseHooked(int ClientId);
68 std::vector<CCharacter *> IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis = nullptr);
69
70 int m_GameTick;
71
72 // getter for server variables
73 int GameTick() const { return m_GameTick; }
74 int GameTickSpeed() const { return SERVER_TICK_SPEED; }
75 const CCollision *Collision() const { return m_pCollision; }
76 CCollision *Collision() { return m_pCollision; }
77 CTeamsCore *Teams() { return &m_Teams; }
78 std::vector<SSwitchers> &Switchers() { return m_Core.m_vSwitchers; }
79 CEntity *GetEntity(int Id, int EntityType);
80 CCharacter *GetCharacterById(int Id) { return (Id >= 0 && Id < MAX_CLIENTS) ? m_apCharacters[Id] : nullptr; }
81
82 // from gamecontext
83 void CreateExplosion(vec2 Pos, int Owner, int Weapon, bool NoDamage, int ActivatedTeam, CClientMask Mask, int Id = -1);
84
85 // for client side prediction
86 struct
87 {
88 bool m_IsDDRace;
89 bool m_IsVanilla;
90 bool m_IsFNG;
91 bool m_InfiniteAmmo;
92 bool m_PredictTiles;
93 int m_PredictFreeze;
94 bool m_PredictWeapons;
95 bool m_PredictDDRace;
96 bool m_IsSolo;
97 bool m_UseTuneZones;
98 bool m_BugDDRaceInput;
99 bool m_NoWeakHookAndBounce;
100 bool m_PredictEvents;
101 bool m_OldLaser;
102 } m_WorldConfig;
103
104 bool m_IsValidCopy;
105 CGameWorld *m_pParent;
106 CGameWorld *m_pChild;
107
108 int m_LocalClientId;
109
110 bool IsLocalTeam(int OwnerId) const;
111 void OnModified() const;
112 void NetObjBegin(CTeamsCore Teams, int LocalClientId);
113 void NetCharAdd(int ObjId, CNetObj_Character *pChar, CNetObj_DDNetCharacter *pExtended, int GameTeam, bool IsLocal);
114 void NetObjAdd(int ObjId, int ObjType, const void *pObjData, const CNetObj_EntityEx *pDataEx);
115 void ResetDoorCollision();
116 void NetObjEnd();
117 void CopyWorld(CGameWorld *pFrom);
118 CEntity *FindMatch(int ObjId, int ObjType, const void *pObjData);
119 void Clear();
120
121 const CTuningParams *TuningList() const { return m_pTuningList; }
122 CTuningParams *TuningList() { return m_pTuningList; }
123 const CTuningParams *GlobalTuning() const { return &TuningList()[0]; }
124 CTuningParams *GlobalTuning() { return &TuningList()[0]; }
125 const CTuningParams *GetTuning(int i) const { return &TuningList()[i]; }
126 CTuningParams *GetTuning(int i) { return &TuningList()[i]; }
127
128 bool EmulateBug(int Bug) const;
129
130 class CPredictedEvent
131 {
132 public:
133 int m_EventId;
134 vec2 m_Pos; // NetEvent's Pos are integers
135 int m_Id; // identifier to prevent adding the same event multiple times
136 int m_Tick;
137
138 int m_ExtraInfo;
139 bool m_Handled = false;
140
141 CPredictedEvent(int EventId, vec2 Pos, int Id, int Tick, int ExtraInfo = -1) :
142 m_EventId(EventId), m_Pos(vec2((int)Pos.x, (int)Pos.y)), m_Id(Id), m_Tick(Tick), m_ExtraInfo(ExtraInfo)
143 {
144 }
145 };
146
147 std::vector<CPredictedEvent> m_PredictedEvents;
148
149 void CreatePredictedEvent(const CPredictedEvent &NewEvent);
150 bool CheckPredictedEventHandled(const CPredictedEvent &CheckEvent);
151 void PlayPredictedEvents(int Tick);
152
153 void CreatePredictedSound(vec2 Pos, int SoundId, int Id = -1);
154 void CreatePredictedExplosionEvent(vec2 Pos, int Id = -1);
155 void CreatePredictedHammerHitEvent(vec2 Pos, int Id = -1);
156 void CreatePredictedDamageIndEvent(vec2 Pos, float Angle, int Amount, int Id = -1);
157
158private:
159 void RemoveEntities();
160
161 std::vector<CEntityIterator *> m_vpEntityIterators;
162 CEntity *m_apFirstEntityTypes[NUM_ENTTYPES];
163
164 CCharacter *m_apCharacters[MAX_CLIENTS];
165
166 CCollision *m_pCollision;
167 CTuningParams *m_pTuningList;
168 const CMapBugs *m_pMapBugs;
169};
170
171class CCharOrder
172{
173public:
174 std::list<int> m_Ids; // reverse of the order in the gameworld, since entities will be inserted in reverse
175 CCharOrder()
176 {
177 Reset();
178 }
179 void Reset()
180 {
181 m_Ids.clear();
182 for(int i = 0; i < MAX_CLIENTS; i++)
183 m_Ids.push_back(x: i);
184 }
185 void GiveStrong(int c)
186 {
187 if(0 <= c && c < MAX_CLIENTS)
188 {
189 m_Ids.remove(value: c);
190 m_Ids.push_front(x: c);
191 }
192 }
193 void GiveWeak(int c)
194 {
195 if(0 <= c && c < MAX_CLIENTS)
196 {
197 m_Ids.remove(value: c);
198 m_Ids.push_back(x: c);
199 }
200 }
201 bool HasStrongAgainst(int From, int To)
202 {
203 for(int i : m_Ids)
204 {
205 if(i == To)
206 return false;
207 else if(i == From)
208 return true;
209 }
210 return false;
211 }
212};
213
214#endif
215