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_GAMECORE_H
4#define GAME_GAMECORE_H
5
6#include "prng.h"
7
8#include <base/vmath.h>
9
10#include <engine/shared/protocol.h>
11
12#include <generated/protocol.h>
13
14#include <game/teamscore.h>
15
16#include <limits>
17#include <set>
18#include <vector>
19
20class CCollision;
21class CTeamsCore;
22
23class CTuneParam
24{
25 int m_Value;
26
27public:
28 int Get() const { return m_Value; }
29 CTuneParam &operator=(float v)
30 {
31 const float Fixed = v * 100.0f;
32 if(Fixed >= static_cast<float>(std::numeric_limits<int>::min()) && Fixed < static_cast<float>(std::numeric_limits<int>::max()))
33 m_Value = static_cast<int>(Fixed);
34 else
35 m_Value = std::numeric_limits<int>::min();
36 return *this;
37 }
38 operator float() const { return m_Value / 100.0f; }
39};
40
41class CTuningParams
42{
43 static const char *ms_apNames[];
44
45public:
46 CTuningParams()
47 {
48#define MACRO_TUNING_PARAM(Name, ScriptName, Value, Description) m_##Name = (Value);
49#include "tuning.h"
50#undef MACRO_TUNING_PARAM
51 }
52
53#define MACRO_TUNING_PARAM(Name, ScriptName, Value, Description) CTuneParam m_##Name;
54#include "tuning.h"
55#undef MACRO_TUNING_PARAM
56
57 static int Num()
58 {
59 return sizeof(CTuningParams) / sizeof(int);
60 }
61 int *NetworkArray() { return (int *)this; }
62 const int *NetworkArray() const { return (const int *)this; }
63 bool Set(int Index, float Value);
64 bool Set(const char *pName, float Value);
65 bool Get(int Index, float *pValue) const;
66 bool Get(const char *pName, float *pValue) const;
67 static const char *Name(int Index) { return ms_apNames[Index]; }
68 float GetWeaponFireDelay(int Weapon) const;
69
70 static const CTuningParams DEFAULT;
71};
72
73// Do not use these function unless for legacy code!
74void StrToInts(int *pInts, size_t NumInts, const char *pStr);
75bool IntsToStr(const int *pInts, size_t NumInts, char *pStr, size_t StrSize);
76
77inline vec2 CalcPos(vec2 Pos, vec2 Velocity, float Curvature, float Speed, float Time)
78{
79 vec2 n;
80 Time *= Speed;
81 n.x = Pos.x + Velocity.x * Time;
82 n.y = Pos.y + Velocity.y * Time + Curvature / 10000 * (Time * Time);
83 return n;
84}
85
86template<typename T>
87inline T SaturatedAdd(T Min, T Max, T Current, T Modifier)
88{
89 if(Modifier < 0)
90 {
91 if(Current < Min)
92 return Current;
93 Current += Modifier;
94 if(Current < Min)
95 Current = Min;
96 return Current;
97 }
98 else
99 {
100 if(Current > Max)
101 return Current;
102 Current += Modifier;
103 if(Current > Max)
104 Current = Max;
105 return Current;
106 }
107}
108
109float VelocityRamp(float Value, float Start, float Range, float Curvature);
110
111// hooking stuff
112enum
113{
114 HOOK_RETRACTED = -1,
115 HOOK_IDLE = 0,
116 HOOK_RETRACT_START = 1,
117 HOOK_RETRACT_END = 3,
118 HOOK_FLYING = 4,
119 HOOK_GRABBED = 5,
120
121 COREEVENT_GROUND_JUMP = 0x01,
122 COREEVENT_AIR_JUMP = 0x02,
123 COREEVENT_HOOK_LAUNCH = 0x04,
124 COREEVENT_HOOK_ATTACH_PLAYER = 0x08,
125 COREEVENT_HOOK_ATTACH_GROUND = 0x10,
126 COREEVENT_HOOK_HIT_NOHOOK = 0x20,
127 COREEVENT_HOOK_RETRACT = 0x40,
128};
129
130// show others values - do not change them
131enum
132{
133 SHOW_OTHERS_NOT_SET = -1, // show others value before it is set
134 SHOW_OTHERS_OFF = 0, // show no other players in solo or other teams
135 SHOW_OTHERS_ON = 1, // show all other players in solo and other teams
136 SHOW_OTHERS_ONLY_TEAM = 2 // show players that are in solo and are in the same team
137};
138
139struct SSwitchers
140{
141 bool m_aStatus[NUM_DDRACE_TEAMS];
142 bool m_Initial;
143 int m_aEndTick[NUM_DDRACE_TEAMS];
144 int m_aType[NUM_DDRACE_TEAMS];
145 int m_aLastUpdateTick[NUM_DDRACE_TEAMS];
146};
147
148class CWorldCore
149{
150public:
151 CWorldCore()
152 {
153 for(auto &pCharacter : m_apCharacters)
154 {
155 pCharacter = nullptr;
156 }
157 m_pPrng = nullptr;
158 }
159
160 int RandomOr0(int BelowThis) // NOLINT(readability-make-member-function-const)
161 {
162 if(BelowThis <= 1 || !m_pPrng)
163 {
164 return 0;
165 }
166 // This makes the random number slightly biased if `BelowThis`
167 // is not a power of two, but we have decided that this is not
168 // significant for DDNet and favored the simple implementation.
169 return m_pPrng->RandomBits() % BelowThis;
170 }
171
172 class CCharacterCore *m_apCharacters[MAX_CLIENTS];
173 CPrng *m_pPrng;
174
175 void InitSwitchers(int HighestSwitchNumber);
176 std::vector<SSwitchers> m_vSwitchers;
177};
178
179typedef std::function<void(int ClientId, bool DisallowReset)> FAntiPingInterfereCallback;
180
181class CCharacterCore
182{
183 CWorldCore *m_pWorld = nullptr;
184 CCollision *m_pCollision;
185
186public:
187 static constexpr float PhysicalSize() { return 28.0f; }
188 static constexpr vec2 PhysicalSizeVec2() { return vec2(28.0f, 28.0f); }
189 vec2 m_Pos;
190 vec2 m_Vel;
191
192 vec2 m_HookPos;
193 vec2 m_HookDir;
194 vec2 m_HookTeleBase;
195 int m_HookTick;
196 int m_HookState;
197 std::set<int> m_AttachedPlayers;
198 int HookedPlayer() const { return m_HookedPlayer; }
199 void SetHookedPlayer(int HookedPlayer);
200
201 int m_ActiveWeapon;
202 class CWeaponStat
203 {
204 public:
205 int m_AmmoRegenStart;
206 int m_Ammo;
207 int m_Ammocost;
208 bool m_Got;
209 } m_aWeapons[NUM_WEAPONS];
210
211 // ninja
212 struct
213 {
214 vec2 m_ActivationDir;
215 int m_ActivationTick;
216 int m_CurrentMoveTime;
217 int m_OldVelAmount;
218 } m_Ninja;
219
220 bool m_NewHook;
221
222 int m_Jumped;
223 // m_JumpedTotal counts the jumps performed in the air
224 int m_JumpedTotal;
225 int m_Jumps;
226
227 int m_Direction;
228 int m_Angle;
229 CNetObj_PlayerInput m_Input;
230
231 int m_TriggeredEvents;
232
233 void Init(CWorldCore *pWorld, CCollision *pCollision, CTeamsCore *pTeams = nullptr);
234 void SetCoreWorld(CWorldCore *pWorld, CCollision *pCollision, CTeamsCore *pTeams);
235 void Reset();
236 void TickDeferred();
237 void Tick(bool UseInput, bool DoDeferredTick = true);
238 void Move();
239
240 void Read(const CNetObj_CharacterCore *pObjCore);
241 void Write(CNetObj_CharacterCore *pObjCore) const;
242 void Quantize();
243
244 // DDRace
245 int m_Id;
246 bool m_Reset;
247 CCollision *Collision() { return m_pCollision; }
248
249 int m_Colliding;
250 bool m_LeftWall;
251
252 // DDNet Character
253 void SetTeamsCore(CTeamsCore *pTeams);
254 void ReadDDNet(const CNetObj_DDNetCharacter *pObjDDNet);
255 bool m_Solo;
256 bool m_Jetpack;
257 bool m_CollisionDisabled;
258 bool m_EndlessHook;
259 bool m_EndlessJump;
260 bool m_HammerHitDisabled;
261 bool m_GrenadeHitDisabled;
262 bool m_LaserHitDisabled;
263 bool m_ShotgunHitDisabled;
264 bool m_HookHitDisabled;
265 bool m_Super;
266 bool m_Invincible;
267 bool m_HasTelegunGun;
268 bool m_HasTelegunGrenade;
269 bool m_HasTelegunLaser;
270 int m_FreezeStart;
271 int m_FreezeEnd;
272 bool m_IsInFreeze;
273 bool m_DeepFrozen;
274 bool m_LiveFrozen;
275 CTuningParams m_Tuning;
276
277 // clientside only: antiping
278 void SetAntiPingInterfereCallback(FAntiPingInterfereCallback Callback);
279
280private:
281 CTeamsCore *m_pTeams;
282 int m_MoveRestrictions;
283 int m_HookedPlayer;
284 static bool IsSwitchActiveCb(unsigned char Number, void *pUser);
285
286 FAntiPingInterfereCallback m_AntiPingInterfereCallback = [](int ClientId, bool DisallowReset) {};
287};
288
289// input count
290struct CInputCount
291{
292 int m_Presses;
293 int m_Releases;
294};
295
296inline CInputCount CountInput(int Prev, int Cur)
297{
298 CInputCount c = {.m_Presses: 0, .m_Releases: 0};
299 Prev &= INPUT_STATE_MASK;
300 Cur &= INPUT_STATE_MASK;
301 int i = Prev;
302
303 while(i != Cur)
304 {
305 i = (i + 1) & INPUT_STATE_MASK;
306 if(i & 1)
307 c.m_Presses++;
308 else
309 c.m_Releases++;
310 }
311
312 return c;
313}
314
315#endif
316