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_ENTITY_H
4#define GAME_SERVER_ENTITY_H
5
6#include "gameworld.h"
7#include "save.h"
8
9#include <base/vmath.h>
10
11#include <game/alloc.h>
12
13class CCollision;
14class CGameContext;
15
16/*
17 Class: Entity
18 Basic entity class.
19*/
20class CEntity
21{
22 MACRO_ALLOC_HEAP()
23
24private:
25 friend CGameWorld; // entity list handling
26 CEntity *m_pPrevTypeEntity;
27 CEntity *m_pNextTypeEntity;
28
29 /* Identity */
30 CGameWorld *m_pGameWorld;
31 CCollision *m_pCCollision;
32
33 std::optional<int> m_Id;
34 int m_ObjType;
35
36 /*
37 Variable: m_ProximityRadius
38 Contains the physical size of the entity.
39 */
40 float m_ProximityRadius;
41
42protected:
43 /* State */
44 bool m_MarkedForDestroy;
45
46public: // TODO: Maybe make protected
47 /*
48 Variable: m_Pos
49 Contains the current posititon of the entity.
50 */
51 vec2 m_Pos;
52
53 /* Getters */
54 std::optional<int> GetId() const { return m_Id; }
55
56 /* Constructor */
57 CEntity(CGameWorld *pGameWorld, int Objtype, bool RequestSnapId, vec2 Pos = vec2(0, 0), int ProximityRadius = 0);
58
59 /* Destructor */
60 virtual ~CEntity();
61
62 /* Objects */
63 std::vector<SSwitchers> &Switchers() { return m_pGameWorld->m_Core.m_vSwitchers; }
64 CGameWorld *GameWorld() { return m_pGameWorld; }
65 CTuningParams *GlobalTuning() { return &GameWorld()->TuningList()[0]; }
66 CTuningParams *TuningList() { return GameWorld()->TuningList(); }
67 CTuningParams *GetTuning(int i) { return GameWorld()->GetTuning(i); }
68 class CConfig *Config() { return m_pGameWorld->Config(); }
69 class CGameContext *GameServer() { return m_pGameWorld->GameServer(); }
70 class IServer *Server() { return m_pGameWorld->Server(); }
71 CCollision *Collision() { return m_pCCollision; }
72
73 /* Getters */
74 const vec2 &GetPos() const { return m_Pos; }
75 float GetProximityRadius() const { return m_ProximityRadius; }
76
77 /* Other functions */
78
79 /*
80 Function: Destroy
81 Destroys the entity.
82 */
83 virtual void Destroy() { delete this; }
84
85 /*
86 Function: Reset
87 Called when the game resets the map. Puts the entity
88 back to its starting state or perhaps destroys it.
89 */
90 virtual void Reset() {}
91
92 /*
93 Function: Tick
94 Called to progress the entity to the next tick. Updates
95 and moves the entity to its new state and position.
96 */
97 virtual void Tick() {}
98
99 /*
100 Function: TickDeferred
101 Called after all entities Tick() function has been called.
102 */
103 virtual void TickDeferred() {}
104
105 /*
106 Function: TickPaused
107 Called when the game is paused, to freeze the state and position of the entity.
108 */
109 virtual void TickPaused() {}
110
111 /*
112 Function: Snap
113 Called when a new snapshot is being generated for a specific
114 client.
115
116 Arguments:
117 SnappingClient - ID of the client which snapshot is
118 being generated. Could be -1 to create a complete
119 snapshot of everything in the game for demo
120 recording.
121 */
122 virtual void Snap(int SnappingClient) {}
123
124 /*
125 Function: SwapClients
126 Called when two players have swapped their client ids.
127
128 Arguments:
129 Client1 - First client ID
130 Client2 - Second client ID
131 */
132 virtual void SwapClients(int Client1, int Client2) {}
133
134 /*
135 Function: BlocksSave
136 Called to check if a team can be saved
137
138 Arguments:
139 ClientId - Client ID
140 */
141 virtual ESaveResult BlocksSave(int ClientId) { return ESaveResult::SUCCESS; }
142
143 /*
144 Function GetOwnerId
145 Returns:
146 ClientId of the initiator from this entity. -1 created by map.
147 This is used by save/load to remove related entities to the tee.
148 CCharacter should not return the PlayerId, because they get
149 handled separately in save/load code.
150 */
151 virtual int GetOwnerId() const { return -1; }
152
153 /*
154 Function: NetworkClipped
155 Performs a series of test to see if a client can see the
156 entity.
157
158 Arguments:
159 SnappingClient - ID of the client which snapshot is
160 being generated. Could be -1 to create a complete
161 snapshot of everything in the game for demo
162 recording.
163
164 Returns:
165 True if the entity doesn't have to be in the snapshot.
166 */
167 bool NetworkClipped(int SnappingClient) const;
168 bool NetworkClipped(int SnappingClient, vec2 CheckPos) const;
169 bool NetworkClippedLine(int SnappingClient, vec2 StartPos, vec2 EndPos) const;
170
171 bool GameLayerClipped(vec2 CheckPos);
172 virtual bool CanCollide(int ClientId) { return true; }
173
174 // DDRace
175
176 bool GetNearestAirPos(vec2 Pos, vec2 PrevPos, vec2 *pOutPos);
177 bool GetNearestAirPosPlayer(vec2 PlayerPos, vec2 *pOutPos);
178
179 int m_Number;
180 int m_Layer;
181};
182
183bool NetworkClipped(const CGameContext *pGameServer, int SnappingClient, vec2 CheckPos);
184bool NetworkClippedLine(const CGameContext *pGameServer, int SnappingClient, vec2 StartPos, vec2 EndPos);
185
186#endif
187