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 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 int GetId() const { return m_Id; }
55
56 /* Constructor */
57 CEntity(CGameWorld *pGameWorld, int Objtype, 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 CEntity *TypeNext() { return m_pNextTypeEntity; }
75 CEntity *TypePrev() { return m_pPrevTypeEntity; }
76 const vec2 &GetPos() const { return m_Pos; }
77 float GetProximityRadius() const { return m_ProximityRadius; }
78
79 /* Other functions */
80
81 /*
82 Function: Destroy
83 Destroys the entity.
84 */
85 virtual void Destroy() { delete this; }
86
87 /*
88 Function: Reset
89 Called when the game resets the map. Puts the entity
90 back to its starting state or perhaps destroys it.
91 */
92 virtual void Reset() {}
93
94 /*
95 Function: Tick
96 Called to progress the entity to the next tick. Updates
97 and moves the entity to its new state and position.
98 */
99 virtual void Tick() {}
100
101 /*
102 Function: TickDeferred
103 Called after all entities Tick() function has been called.
104 */
105 virtual void TickDeferred() {}
106
107 /*
108 Function: TickPaused
109 Called when the game is paused, to freeze the state and position of the entity.
110 */
111 virtual void TickPaused() {}
112
113 /*
114 Function: Snap
115 Called when a new snapshot is being generated for a specific
116 client.
117
118 Arguments:
119 SnappingClient - ID of the client which snapshot is
120 being generated. Could be -1 to create a complete
121 snapshot of everything in the game for demo
122 recording.
123 */
124 virtual void Snap(int SnappingClient) {}
125
126 /*
127 Function: SwapClients
128 Called when two players have swapped their client ids.
129
130 Arguments:
131 Client1 - First client ID
132 Client2 - Second client ID
133 */
134 virtual void SwapClients(int Client1, int Client2) {}
135
136 /*
137 Function: BlocksSave
138 Called to check if a team can be saved
139
140 Arguments:
141 ClientId - Client ID
142 */
143 virtual ESaveResult BlocksSave(int ClientId) { return ESaveResult::SUCCESS; }
144
145 /*
146 Function GetOwnerId
147 Returns:
148 ClientId of the initiator from this entity. -1 created by map.
149 This is used by save/load to remove related entities to the tee.
150 CCharacter should not return the PlayerId, because they get
151 handled separately in save/load code.
152 */
153 virtual int GetOwnerId() const { return -1; }
154
155 /*
156 Function: NetworkClipped
157 Performs a series of test to see if a client can see the
158 entity.
159
160 Arguments:
161 SnappingClient - ID of the client which snapshot is
162 being generated. Could be -1 to create a complete
163 snapshot of everything in the game for demo
164 recording.
165
166 Returns:
167 True if the entity doesn't have to be in the snapshot.
168 */
169 bool NetworkClipped(int SnappingClient) const;
170 bool NetworkClipped(int SnappingClient, vec2 CheckPos) const;
171 bool NetworkClippedLine(int SnappingClient, vec2 StartPos, vec2 EndPos) const;
172
173 bool GameLayerClipped(vec2 CheckPos);
174 virtual bool CanCollide(int ClientId) { return true; }
175
176 // DDRace
177
178 bool GetNearestAirPos(vec2 Pos, vec2 PrevPos, vec2 *pOutPos);
179 bool GetNearestAirPosPlayer(vec2 PlayerPos, vec2 *pOutPos);
180
181 int m_Number;
182 int m_Layer;
183};
184
185bool NetworkClipped(const CGameContext *pGameServer, int SnappingClient, vec2 CheckPos);
186bool NetworkClippedLine(const CGameContext *pGameServer, int SnappingClient, vec2 StartPos, vec2 EndPos);
187
188#endif
189