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