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 | |
12 | class CCollision; |
13 | class CGameContext; |
14 | |
15 | /* |
16 | Class: Entity |
17 | Basic entity class. |
18 | */ |
19 | class CEntity |
20 | { |
21 | MACRO_ALLOC_HEAP() |
22 | |
23 | private: |
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 | |
41 | protected: |
42 | /* State */ |
43 | bool m_MarkedForDestroy; |
44 | |
45 | public: // 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: PostSnap |
127 | Called after all clients received their snapshot. |
128 | */ |
129 | virtual void PostSnap() {} |
130 | |
131 | /* |
132 | Function: SwapClients |
133 | Called when two players have swapped their client ids. |
134 | |
135 | Arguments: |
136 | Client1 - First client ID |
137 | Client2 - Second client ID |
138 | */ |
139 | virtual void SwapClients(int Client1, int Client2) {} |
140 | |
141 | /* |
142 | Function GetOwnerId |
143 | Returns: |
144 | ClientId of the initiator from this entity. -1 created by map. |
145 | This is used by save/load to remove related entities to the tee. |
146 | CCharacter should not return the PlayerId, because they get |
147 | handled separately in save/load code. |
148 | */ |
149 | virtual int GetOwnerId() const { return -1; } |
150 | |
151 | /* |
152 | Function: NetworkClipped |
153 | Performs a series of test to see if a client can see the |
154 | entity. |
155 | |
156 | Arguments: |
157 | SnappingClient - ID of the client which snapshot is |
158 | being generated. Could be -1 to create a complete |
159 | snapshot of everything in the game for demo |
160 | recording. |
161 | |
162 | Returns: |
163 | True if the entity doesn't have to be in the snapshot. |
164 | */ |
165 | bool NetworkClipped(int SnappingClient) const; |
166 | bool NetworkClipped(int SnappingClient, vec2 CheckPos) const; |
167 | bool NetworkClippedLine(int SnappingClient, vec2 StartPos, vec2 EndPos) const; |
168 | |
169 | bool GameLayerClipped(vec2 CheckPos); |
170 | |
171 | // DDRace |
172 | |
173 | bool GetNearestAirPos(vec2 Pos, vec2 PrevPos, vec2 *pOutPos); |
174 | bool GetNearestAirPosPlayer(vec2 PlayerPos, vec2 *pOutPos); |
175 | |
176 | int m_Number; |
177 | int m_Layer; |
178 | }; |
179 | |
180 | bool NetworkClipped(const CGameContext *pGameServer, int SnappingClient, vec2 CheckPos); |
181 | bool NetworkClippedLine(const CGameContext *pGameServer, int SnappingClient, vec2 StartPos, vec2 EndPos); |
182 | |
183 | #endif |
184 | |