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