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_SERVER_GAMEWORLD_H
4#define GAME_SERVER_GAMEWORLD_H
5
6#include "save.h"
7
8#include <game/gamecore.h>
9
10#include <vector>
11
12class CCollision;
13class CEntity;
14class CCharacter;
15
16/*
17 Class: Game World
18 Tracks all entities in the game. Propagates tick and
19 snap calls to all entities.
20*/
21class CGameWorld
22{
23public:
24 enum
25 {
26 ENTTYPE_PROJECTILE = 0,
27 ENTTYPE_LASER,
28 ENTTYPE_PICKUP,
29 ENTTYPE_FLAG,
30 ENTTYPE_CHARACTER,
31 NUM_ENTTYPES
32 };
33
34 class CEntityIterator
35 {
36 friend CGameWorld;
37
38 CGameWorld *m_pWorld;
39 CEntity *m_pNext = nullptr;
40 bool m_AllowEntityRemoval = false;
41
42 public:
43 CEntityIterator(CGameWorld *pWorld, CEntity *pFirstEntity);
44 CEntityIterator(CGameWorld *pWorld, int EntityType);
45 ~CEntityIterator();
46 void AllowEntityRemoval();
47 CEntity *Next();
48 };
49
50private:
51 void Reset();
52 void RemoveEntities();
53
54 std::vector<CEntityIterator *> m_vpEntityIterators;
55 CEntity *m_apFirstEntityTypes[NUM_ENTTYPES];
56
57 class CGameContext *m_pGameServer;
58 class CConfig *m_pConfig;
59 class IServer *m_pServer;
60 CTuningParams *m_pTuningList;
61
62public:
63 class CGameContext *GameServer() { return m_pGameServer; }
64 class CConfig *Config() { return m_pConfig; }
65 class IServer *Server() { return m_pServer; }
66
67 bool m_ResetRequested;
68 bool m_Paused;
69 CWorldCore m_Core;
70
71 CGameWorld();
72 ~CGameWorld();
73
74 void SetGameServer(CGameContext *pGameServer);
75 void Init(CCollision *pCollision, CTuningParams *pTuningList);
76
77 /*
78 Function: FindEntities
79 Finds entities close to a position and returns them in a list.
80
81 Arguments:
82 Pos - Position.
83 Radius - How close the entities have to be.
84 ppEnts - Pointer to a list that should be filled with the pointers
85 to the entities.
86 Max - Number of entities that fits into the ents array.
87 Type - Type of the entities to find.
88
89 Returns:
90 Number of entities found and added to the ents array.
91 */
92 int FindEntities(vec2 Pos, float Radius, CEntity **ppEnts, int Max, int Type);
93
94 /**
95 * Finds the CCharacter that intersects the line.
96 *
97 * @see IntersectEntity
98 *
99 * @param Pos0 Start position
100 * @param Pos1 End position
101 * @param Radius How far from the line the @link CCharacter @endlink is allowed to be
102 * @param NewPos Intersection position
103 * @param pNotThis Character to ignore intersecting with
104 * @param CollideWith Only find entities that can collide with that Client Id (pass -1 to ignore this check)
105 * @param pThisOnly Only search this specific character and ignore all others
106 *
107 * @return Pointer to the closest hit or `nullptr` if there is no intersection.
108 */
109 CCharacter *IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, const CCharacter *pNotThis = nullptr, int CollideWith = -1, const CCharacter *pThisOnly = nullptr);
110
111 /**
112 * Finds the CEntity that intersects the line.
113 *
114 * @see IntersectCharacter
115 *
116 * @param Pos0 Start position
117 * @param Pos1 End position
118 * @param Radius How far from the line the @link CEntity @endlink is allowed to be
119 * @param Type Type of the entity to intersect
120 * @param NewPos Intersection position
121 * @param pNotThis Entity to ignore intersecting with
122 * @param CollideWith Only find entities that can collide with that Client Id (pass -1 to ignore this check)
123 * @param pThisOnly Only search this specific entity and ignore all others
124 *
125 * @return Pointer to the closest hit or `nullptr` if there is no intersection.
126 */
127 CEntity *IntersectEntity(vec2 Pos0, vec2 Pos1, float Radius, int Type, vec2 &NewPos, const CEntity *pNotThis = nullptr, int CollideWith = -1, const CEntity *pThisOnly = nullptr);
128
129 /*
130 Function: ClosestCharacter
131 Finds the closest CCharacter to a specific point.
132
133 Arguments:
134 Pos - The center position.
135 Radius - How far off the CCharacter is allowed to be
136 pNotThis - Entity to ignore
137
138 Returns:
139 Returns a pointer to the closest CCharacter or nullptr if no CCharacter is close enough.
140 */
141 CCharacter *ClosestCharacter(vec2 Pos, float Radius, const CEntity *pNotThis);
142
143 /*
144 Function: InsertEntity
145 Adds an entity to the world.
146
147 Arguments:
148 pEntity - Entity to add
149 */
150 void InsertEntity(CEntity *pEntity);
151
152 /*
153 Function: RemoveEntity
154 Removes an entity from the world.
155
156 Arguments:
157 pEntity - Entity to remove
158 */
159 void RemoveEntity(CEntity *pEntity);
160
161 void RemoveEntitiesFromPlayer(int PlayerId);
162 void RemoveEntitiesFromPlayers(int PlayerIds[], int NumPlayers);
163
164 /*
165 Function: Snap
166 Calls Snap on all the entities in the world to create
167 the snapshot.
168
169 Arguments:
170 SnappingClient - ID of the client which snapshot
171 is being created.
172 */
173 void Snap(int SnappingClient);
174
175 /*
176 Function: Tick
177 Calls Tick on all the entities in the world to progress
178 the world to the next tick.
179 */
180 void Tick();
181
182 /*
183 Function: SwapClients
184 Calls SwapClients on all the entities in the world to ensure that /swap
185 command is handled safely.
186 */
187 void SwapClients(int Client1, int Client2);
188
189 /*
190 Function: BlocksSave
191 Checks if any entity would block /save
192 */
193 ESaveResult BlocksSave(int ClientId);
194
195 // DDRace
196 void ReleaseHooked(int ClientId);
197
198 /*
199 Function: IntersectedCharacters
200 Finds all CCharacters that intersect the line.
201
202 Arguments:
203 Pos0 - Start position
204 Pos1 - End position
205 Radius - How for from the line the CCharacter is allowed to be.
206 pNotThis - Entity to ignore intersecting with
207
208 Returns:
209 Returns list with all Characters on line.
210 */
211 std::vector<CCharacter *> IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, const CEntity *pNotThis = nullptr);
212
213 const CTuningParams *TuningList() const { return m_pTuningList; }
214 CTuningParams *TuningList() { return m_pTuningList; }
215 const CTuningParams *GetTuning(int i) const { return &TuningList()[i]; }
216 CTuningParams *GetTuning(int i) { return &TuningList()[i]; }
217};
218
219#endif
220