1/* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
2#include "gamecontext.h"
3
4#include <base/io.h>
5#include <base/log.h>
6#include <base/time.h>
7
8#include <engine/antibot.h>
9#include <engine/server/authmanager.h>
10#include <engine/shared/config.h>
11
12#include <generated/protocol.h>
13
14#include <game/mapitems.h>
15#include <game/server/entities/character.h>
16#include <game/server/gamemodes/ddnet.h>
17#include <game/server/player.h>
18#include <game/server/save.h>
19#include <game/server/teams.h>
20
21#include <algorithm>
22
23static vec2 ClampPlacement(const CCollision *pCollision, vec2 Pos)
24{
25 // Further out a tee is killed by CEntity::GameLayerClipped anyway
26 constexpr float Margin = 200 * 32.0f;
27 return vec2(std::clamp(val: Pos.x, lo: -Margin, hi: pCollision->GetWidth() * 32.0f + Margin),
28 std::clamp(val: Pos.y, lo: -Margin, hi: pCollision->GetHeight() * 32.0f + Margin));
29}
30
31void CGameContext::ConGoLeft(IConsole::IResult *pResult, void *pUserData)
32{
33 CGameContext *pSelf = (CGameContext *)pUserData;
34 int Tiles = pResult->NumArguments() == 1 ? pResult->GetInteger(Index: 0) : 1;
35
36 if(!CheckClientId(ClientId: pResult->m_ClientId))
37 return;
38 pSelf->MoveCharacter(ClientId: pResult->m_ClientId, X: -1 * Tiles, Y: 0);
39}
40
41void CGameContext::ConGoRight(IConsole::IResult *pResult, void *pUserData)
42{
43 CGameContext *pSelf = (CGameContext *)pUserData;
44 int Tiles = pResult->NumArguments() == 1 ? pResult->GetInteger(Index: 0) : 1;
45
46 if(!CheckClientId(ClientId: pResult->m_ClientId))
47 return;
48 pSelf->MoveCharacter(ClientId: pResult->m_ClientId, X: Tiles, Y: 0);
49}
50
51void CGameContext::ConGoDown(IConsole::IResult *pResult, void *pUserData)
52{
53 CGameContext *pSelf = (CGameContext *)pUserData;
54 int Tiles = pResult->NumArguments() == 1 ? pResult->GetInteger(Index: 0) : 1;
55
56 if(!CheckClientId(ClientId: pResult->m_ClientId))
57 return;
58 pSelf->MoveCharacter(ClientId: pResult->m_ClientId, X: 0, Y: Tiles);
59}
60
61void CGameContext::ConGoUp(IConsole::IResult *pResult, void *pUserData)
62{
63 CGameContext *pSelf = (CGameContext *)pUserData;
64 int Tiles = pResult->NumArguments() == 1 ? pResult->GetInteger(Index: 0) : 1;
65
66 if(!CheckClientId(ClientId: pResult->m_ClientId))
67 return;
68 pSelf->MoveCharacter(ClientId: pResult->m_ClientId, X: 0, Y: -1 * Tiles);
69}
70
71void CGameContext::ConMove(IConsole::IResult *pResult, void *pUserData)
72{
73 CGameContext *pSelf = (CGameContext *)pUserData;
74 if(!CheckClientId(ClientId: pResult->m_ClientId))
75 return;
76 pSelf->MoveCharacter(ClientId: pResult->m_ClientId, X: pResult->GetInteger(Index: 0),
77 Y: pResult->GetInteger(Index: 1));
78}
79
80void CGameContext::ConMoveRaw(IConsole::IResult *pResult, void *pUserData)
81{
82 CGameContext *pSelf = (CGameContext *)pUserData;
83 if(!CheckClientId(ClientId: pResult->m_ClientId))
84 return;
85 pSelf->MoveCharacter(ClientId: pResult->m_ClientId, X: pResult->GetInteger(Index: 0),
86 Y: pResult->GetInteger(Index: 1), Raw: true);
87}
88
89void CGameContext::MoveCharacter(int ClientId, int X, int Y, bool Raw)
90{
91 CCharacter *pChr = GetPlayerChar(ClientId);
92
93 if(!pChr)
94 return;
95
96 const float Scale = Raw ? 1.0f : 32.0f; // Scaled in float, 32 * X would overflow an int
97 pChr->Move(RelPos: vec2(Scale * X, Scale * Y));
98 pChr->SetPosition(ClampPlacement(pCollision: Collision(), Pos: pChr->Core()->m_Pos));
99 pChr->ResetVelocity();
100 pChr->m_DDRaceState = ERaceState::CHEATED;
101}
102
103void CGameContext::ConKillPlayer(IConsole::IResult *pResult, void *pUserData)
104{
105 CGameContext *pSelf = (CGameContext *)pUserData;
106 if(!CheckClientId(ClientId: pResult->m_ClientId))
107 return;
108 int Victim = pResult->GetVictim(Slot: 0);
109
110 if(pSelf->m_apPlayers[Victim])
111 {
112 pSelf->m_apPlayers[Victim]->KillCharacter(Weapon: WEAPON_GAME);
113 char aBuf[512];
114 if(pResult->NumArguments() == 2)
115 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s was killed by authorized player (%s)",
116 pSelf->Server()->ClientName(ClientId: Victim),
117 pResult->GetString(Index: 1));
118 else
119 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s was killed by authorized player",
120 pSelf->Server()->ClientName(ClientId: Victim));
121 pSelf->SendChat(ClientId: -1, Team: TEAM_ALL, pText: aBuf);
122 }
123}
124
125void CGameContext::ConNinja(IConsole::IResult *pResult, void *pUserData)
126{
127 CGameContext *pSelf = (CGameContext *)pUserData;
128 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_NINJA, Remove: false);
129}
130
131void CGameContext::ConUnNinja(IConsole::IResult *pResult, void *pUserData)
132{
133 CGameContext *pSelf = (CGameContext *)pUserData;
134 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_NINJA, Remove: true);
135}
136
137void CGameContext::ConEndlessHook(IConsole::IResult *pResult, void *pUserData)
138{
139 CGameContext *pSelf = (CGameContext *)pUserData;
140 if(!CheckClientId(ClientId: pResult->m_ClientId))
141 return;
142 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
143 if(pChr)
144 {
145 pChr->SetEndlessHook(true);
146 }
147}
148
149void CGameContext::ConUnEndlessHook(IConsole::IResult *pResult, void *pUserData)
150{
151 CGameContext *pSelf = (CGameContext *)pUserData;
152 if(!CheckClientId(ClientId: pResult->m_ClientId))
153 return;
154 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
155 if(pChr)
156 {
157 pChr->SetEndlessHook(false);
158 }
159}
160
161void CGameContext::ConSuper(IConsole::IResult *pResult, void *pUserData)
162{
163 CGameContext *pSelf = (CGameContext *)pUserData;
164 if(!CheckClientId(ClientId: pResult->m_ClientId))
165 return;
166 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
167 if(pChr && !pChr->IsSuper())
168 {
169 pChr->SetSuper(true);
170 pChr->Unfreeze();
171 }
172}
173
174void CGameContext::ConUnSuper(IConsole::IResult *pResult, void *pUserData)
175{
176 CGameContext *pSelf = (CGameContext *)pUserData;
177 if(!CheckClientId(ClientId: pResult->m_ClientId))
178 return;
179 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
180 if(pChr && pChr->IsSuper())
181 {
182 pChr->SetSuper(false);
183 }
184}
185
186void CGameContext::ConToggleInvincible(IConsole::IResult *pResult, void *pUserData)
187{
188 CGameContext *pSelf = (CGameContext *)pUserData;
189 if(!CheckClientId(ClientId: pResult->m_ClientId))
190 return;
191 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
192 if(pChr)
193 pChr->SetInvincible(pResult->NumArguments() == 0 ? !pChr->Core()->m_Invincible : pResult->GetInteger(Index: 0));
194}
195
196void CGameContext::ConSolo(IConsole::IResult *pResult, void *pUserData)
197{
198 CGameContext *pSelf = (CGameContext *)pUserData;
199 if(!CheckClientId(ClientId: pResult->m_ClientId))
200 return;
201 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
202 if(pChr)
203 pChr->SetSolo(true);
204}
205
206void CGameContext::ConUnSolo(IConsole::IResult *pResult, void *pUserData)
207{
208 CGameContext *pSelf = (CGameContext *)pUserData;
209 if(!CheckClientId(ClientId: pResult->m_ClientId))
210 return;
211 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
212 if(pChr)
213 pChr->SetSolo(false);
214}
215
216void CGameContext::ConFreeze(IConsole::IResult *pResult, void *pUserData)
217{
218 CGameContext *pSelf = (CGameContext *)pUserData;
219 if(!CheckClientId(ClientId: pResult->m_ClientId))
220 return;
221 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
222 if(pChr)
223 pChr->Freeze();
224}
225
226void CGameContext::ConUnfreeze(IConsole::IResult *pResult, void *pUserData)
227{
228 CGameContext *pSelf = (CGameContext *)pUserData;
229 if(!CheckClientId(ClientId: pResult->m_ClientId))
230 return;
231 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
232 if(pChr)
233 pChr->Unfreeze();
234}
235
236void CGameContext::ConDeep(IConsole::IResult *pResult, void *pUserData)
237{
238 CGameContext *pSelf = (CGameContext *)pUserData;
239 if(!CheckClientId(ClientId: pResult->m_ClientId))
240 return;
241 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
242 if(pChr)
243 pChr->SetDeepFrozen(true);
244}
245
246void CGameContext::ConUnDeep(IConsole::IResult *pResult, void *pUserData)
247{
248 CGameContext *pSelf = (CGameContext *)pUserData;
249 if(!CheckClientId(ClientId: pResult->m_ClientId))
250 return;
251 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
252 if(pChr)
253 {
254 pChr->SetDeepFrozen(false);
255 pChr->Unfreeze();
256 }
257}
258
259void CGameContext::ConLiveFreeze(IConsole::IResult *pResult, void *pUserData)
260{
261 CGameContext *pSelf = (CGameContext *)pUserData;
262 if(!CheckClientId(ClientId: pResult->m_ClientId))
263 return;
264 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
265 if(pChr)
266 pChr->SetLiveFrozen(true);
267}
268
269void CGameContext::ConUnLiveFreeze(IConsole::IResult *pResult, void *pUserData)
270{
271 CGameContext *pSelf = (CGameContext *)pUserData;
272 if(!CheckClientId(ClientId: pResult->m_ClientId))
273 return;
274 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
275 if(pChr)
276 pChr->SetLiveFrozen(false);
277}
278
279void CGameContext::ConShotgun(IConsole::IResult *pResult, void *pUserData)
280{
281 CGameContext *pSelf = (CGameContext *)pUserData;
282 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_SHOTGUN, Remove: false);
283}
284
285void CGameContext::ConGrenade(IConsole::IResult *pResult, void *pUserData)
286{
287 CGameContext *pSelf = (CGameContext *)pUserData;
288 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_GRENADE, Remove: false);
289}
290
291void CGameContext::ConLaser(IConsole::IResult *pResult, void *pUserData)
292{
293 CGameContext *pSelf = (CGameContext *)pUserData;
294 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_LASER, Remove: false);
295}
296
297void CGameContext::ConJetpack(IConsole::IResult *pResult, void *pUserData)
298{
299 CGameContext *pSelf = (CGameContext *)pUserData;
300 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
301 if(pChr)
302 pChr->SetJetpack(true);
303}
304
305void CGameContext::ConEndlessJump(IConsole::IResult *pResult, void *pUserData)
306{
307 CGameContext *pSelf = (CGameContext *)pUserData;
308 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
309 if(pChr)
310 pChr->SetEndlessJump(true);
311}
312
313void CGameContext::ConSetJumps(IConsole::IResult *pResult, void *pUserData)
314{
315 CGameContext *pSelf = (CGameContext *)pUserData;
316 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
317 if(pChr)
318 pChr->SetJumps(pResult->GetInteger(Index: 0));
319}
320
321void CGameContext::ConWeapons(IConsole::IResult *pResult, void *pUserData)
322{
323 CGameContext *pSelf = (CGameContext *)pUserData;
324 pSelf->ModifyWeapons(pResult, pUserData, Weapon: -1, Remove: false);
325}
326
327void CGameContext::ConUnShotgun(IConsole::IResult *pResult, void *pUserData)
328{
329 CGameContext *pSelf = (CGameContext *)pUserData;
330 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_SHOTGUN, Remove: true);
331}
332
333void CGameContext::ConUnGrenade(IConsole::IResult *pResult, void *pUserData)
334{
335 CGameContext *pSelf = (CGameContext *)pUserData;
336 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_GRENADE, Remove: true);
337}
338
339void CGameContext::ConUnLaser(IConsole::IResult *pResult, void *pUserData)
340{
341 CGameContext *pSelf = (CGameContext *)pUserData;
342 pSelf->ModifyWeapons(pResult, pUserData, Weapon: WEAPON_LASER, Remove: true);
343}
344
345void CGameContext::ConUnJetpack(IConsole::IResult *pResult, void *pUserData)
346{
347 CGameContext *pSelf = (CGameContext *)pUserData;
348 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
349 if(pChr)
350 pChr->SetJetpack(false);
351}
352
353void CGameContext::ConUnEndlessJump(IConsole::IResult *pResult, void *pUserData)
354{
355 CGameContext *pSelf = (CGameContext *)pUserData;
356 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
357 if(pChr)
358 pChr->SetEndlessJump(false);
359}
360
361void CGameContext::ConSetSwitch(IConsole::IResult *pResult, void *pUserData)
362{
363 CGameContext *pSelf = (CGameContext *)pUserData;
364 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
365 if(!pChr)
366 {
367 log_info("chatresp", "You can't set switch while you are dead/a spectator.");
368 return;
369 }
370 const int Team = pChr->Team();
371 const int Switch = pResult->GetInteger(Index: 0);
372 if(!in_range(a: Switch, upper: (int)pSelf->Switchers().size() - 1))
373 {
374 log_info("chatresp", "Invalid switch ID");
375 return;
376 }
377 const bool State = pResult->NumArguments() == 1 ? !pSelf->Switchers()[Switch].m_aStatus[Team] : pResult->GetInteger(Index: 1) != 0;
378 const int EndTick = pResult->NumArguments() == 3 ? pSelf->Server()->Tick() + 1 + pResult->GetInteger(Index: 2) * pSelf->Server()->TickSpeed() : 0;
379 pSelf->Switchers()[Switch].m_aStatus[Team] = State;
380 pSelf->Switchers()[Switch].m_aEndTick[Team] = EndTick;
381 if(State)
382 pSelf->Switchers()[Switch].m_aType[Team] = EndTick ? TILE_SWITCHTIMEDOPEN : TILE_SWITCHOPEN;
383 else
384 pSelf->Switchers()[Switch].m_aType[Team] = EndTick ? TILE_SWITCHTIMEDCLOSE : TILE_SWITCHCLOSE;
385}
386
387void CGameContext::ConUnWeapons(IConsole::IResult *pResult, void *pUserData)
388{
389 CGameContext *pSelf = (CGameContext *)pUserData;
390 pSelf->ModifyWeapons(pResult, pUserData, Weapon: -1, Remove: true);
391}
392
393void CGameContext::ConAddWeapon(IConsole::IResult *pResult, void *pUserData)
394{
395 CGameContext *pSelf = (CGameContext *)pUserData;
396 pSelf->ModifyWeapons(pResult, pUserData, Weapon: pResult->GetInteger(Index: 0), Remove: false);
397}
398
399void CGameContext::ConRemoveWeapon(IConsole::IResult *pResult, void *pUserData)
400{
401 CGameContext *pSelf = (CGameContext *)pUserData;
402 pSelf->ModifyWeapons(pResult, pUserData, Weapon: pResult->GetInteger(Index: 0), Remove: true);
403}
404
405void CGameContext::ModifyWeapons(IConsole::IResult *pResult, void *pUserData,
406 int Weapon, bool Remove)
407{
408 CGameContext *pSelf = (CGameContext *)pUserData;
409 CCharacter *pChr = GetPlayerChar(ClientId: pResult->m_ClientId);
410 if(!pChr)
411 return;
412
413 if(std::clamp(val: Weapon, lo: -1, hi: NUM_WEAPONS - 1) != Weapon)
414 {
415 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "info",
416 pStr: "invalid weapon id");
417 return;
418 }
419
420 if(Weapon == -1)
421 {
422 pChr->GiveWeapon(Weapon: WEAPON_SHOTGUN, Remove);
423 pChr->GiveWeapon(Weapon: WEAPON_GRENADE, Remove);
424 pChr->GiveWeapon(Weapon: WEAPON_LASER, Remove);
425 }
426 else
427 {
428 pChr->GiveWeapon(Weapon, Remove);
429 }
430
431 pChr->m_DDRaceState = ERaceState::CHEATED;
432}
433
434void CGameContext::Teleport(CCharacter *pChr, vec2 Pos)
435{
436 Pos = ClampPlacement(pCollision: Collision(), Pos);
437 pChr->SetPosition(Pos);
438 pChr->m_Pos = Pos;
439 pChr->m_PrevPos = Pos;
440 pChr->m_DDRaceState = ERaceState::CHEATED;
441}
442
443void CGameContext::PracticeTeleport(CCharacter *pChr, vec2 Pos)
444{
445 CMapItemLayerTilemap *pGameLayer = m_Layers.GameLayer();
446 constexpr float OuterKillTileBoundaryDistance = 201 * 32.f;
447 const float MapWidth = (pGameLayer->m_Width * 32) + (OuterKillTileBoundaryDistance * 2.f);
448 const float MapHeight = (pGameLayer->m_Height * 32) + (OuterKillTileBoundaryDistance * 2.f);
449 Pos.x = std::clamp(val: Pos.x, lo: (-OuterKillTileBoundaryDistance) + 1.f, hi: (-OuterKillTileBoundaryDistance) + MapWidth - 1.f);
450 Pos.y = std::clamp(val: Pos.y, lo: (-OuterKillTileBoundaryDistance) + 1.f, hi: (-OuterKillTileBoundaryDistance) + MapHeight - 1.f);
451
452 Teleport(pChr, Pos);
453 pChr->ResetJumps();
454 pChr->Unfreeze();
455 pChr->ResetVelocity();
456 pChr->GetPlayer()->m_LastTeleTee.Save(pChr);
457}
458
459void CGameContext::ConToTeleporter(IConsole::IResult *pResult, void *pUserData)
460{
461 CGameContext *pSelf = (CGameContext *)pUserData;
462 unsigned int TeleTo = pResult->GetInteger(Index: 0);
463
464 if(!pSelf->Collision()->TeleOuts(Number: TeleTo - 1).empty())
465 {
466 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
467 if(pChr)
468 {
469 int TeleOut = pSelf->m_World.m_Core.RandomOr0(BelowThis: pSelf->Collision()->TeleOuts(Number: TeleTo - 1).size());
470 pSelf->Teleport(pChr, Pos: pSelf->Collision()->TeleOuts(Number: TeleTo - 1)[TeleOut]);
471 }
472 }
473}
474
475void CGameContext::ConToCheckTeleporter(IConsole::IResult *pResult, void *pUserData)
476{
477 CGameContext *pSelf = (CGameContext *)pUserData;
478 unsigned int TeleTo = pResult->GetInteger(Index: 0);
479
480 if(!pSelf->Collision()->TeleCheckOuts(Number: TeleTo - 1).empty())
481 {
482 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: pResult->m_ClientId);
483 if(pChr)
484 {
485 int TeleOut = pSelf->m_World.m_Core.RandomOr0(BelowThis: pSelf->Collision()->TeleCheckOuts(Number: TeleTo - 1).size());
486 pSelf->Teleport(pChr, Pos: pSelf->Collision()->TeleCheckOuts(Number: TeleTo - 1)[TeleOut]);
487 pChr->m_TeleCheckpoint = TeleTo;
488 }
489 }
490}
491
492void CGameContext::ConTeleport(IConsole::IResult *pResult, void *pUserData)
493{
494 CGameContext *pSelf = (CGameContext *)pUserData;
495 if(!CheckClientId(ClientId: pResult->m_ClientId))
496 return;
497 const bool HasSource = pResult->NumArguments() == 2;
498 int Tele = HasSource ? pResult->GetVictim(Slot: 0) : pResult->m_ClientId;
499 int TeleTo = pResult->NumArguments() ? pResult->GetVictim(Slot: HasSource ? 1 : 0) : pResult->m_ClientId;
500 int AuthLevel = pSelf->Server()->GetAuthedState(ClientId: pResult->m_ClientId);
501
502 auto MinTeleLevel = CAuthManager::RoleNameToAuthLevel(pRoleName: g_Config.m_SvTeleOthersAuthLevel);
503 if(!MinTeleLevel.has_value())
504 {
505 // if it is not a valid role name like "helper"
506 // we fallback to backcompat numeric values
507 if(str_comp(a: g_Config.m_SvTeleOthersAuthLevel, b: "1") == 0)
508 {
509 MinTeleLevel = AUTHED_HELPER;
510 }
511 else if(str_comp(a: g_Config.m_SvTeleOthersAuthLevel, b: "2") == 0)
512 {
513 MinTeleLevel = AUTHED_MOD;
514 }
515 else if(str_comp(a: g_Config.m_SvTeleOthersAuthLevel, b: "3") == 0)
516 {
517 MinTeleLevel = AUTHED_ADMIN;
518 }
519 }
520 dbg_assert(MinTeleLevel.has_value(), "sv_tele_others_auth_level got unexpected value '%s'", g_Config.m_SvTeleOthersAuthLevel);
521
522 if(Tele != pResult->m_ClientId && AuthLevel < MinTeleLevel.value())
523 {
524 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "tele", pStr: "you aren't allowed to tele others");
525 return;
526 }
527
528 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: Tele);
529 CPlayer *pPlayer = pSelf->m_apPlayers[pResult->m_ClientId];
530
531 if(pChr && pPlayer && pSelf->GetPlayerChar(ClientId: TeleTo))
532 {
533 // default to view pos when character is not available
534 vec2 Pos = pSelf->m_apPlayers[TeleTo]->m_ViewPos;
535 if(pResult->NumArguments() == 0 && !pPlayer->IsPaused() && pChr->IsAlive())
536 {
537 vec2 Target = vec2(pChr->Core()->m_Input.m_TargetX, pChr->Core()->m_Input.m_TargetY);
538 Pos = pPlayer->m_CameraInfo.ConvertTargetToWorld(Position: pChr->GetPos(), Target);
539 }
540 pSelf->Teleport(pChr, Pos);
541 pChr->ResetJumps();
542 pChr->Unfreeze();
543 pChr->SetVelocity(vec2(0, 0));
544 }
545}
546
547void CGameContext::ConKill(IConsole::IResult *pResult, void *pUserData)
548{
549 CGameContext *pSelf = (CGameContext *)pUserData;
550 if(!CheckClientId(ClientId: pResult->m_ClientId))
551 return;
552 CPlayer *pPlayer = pSelf->m_apPlayers[pResult->m_ClientId];
553
554 if(!pPlayer || (pPlayer->m_LastKill && pPlayer->m_LastKill + pSelf->Server()->TickSpeed() * g_Config.m_SvKillDelay > pSelf->Server()->Tick()))
555 return;
556
557 pPlayer->m_LastKill = pSelf->Server()->Tick();
558 pPlayer->KillCharacter(Weapon: WEAPON_SELF);
559}
560
561void CGameContext::ConForcePause(IConsole::IResult *pResult, void *pUserData)
562{
563 CGameContext *pSelf = (CGameContext *)pUserData;
564 int Victim = pResult->GetVictim(Slot: 0);
565 int Seconds = 0;
566 if(pResult->NumArguments() > 1)
567 Seconds = std::clamp(val: pResult->GetInteger(Index: 1), lo: 0, hi: 360);
568
569 CPlayer *pPlayer = pSelf->m_apPlayers[Victim];
570 if(!pPlayer)
571 return;
572
573 pPlayer->ForcePause(Time: Seconds);
574}
575
576void CGameContext::ConModerate(IConsole::IResult *pResult, void *pUserData)
577{
578 CGameContext *pSelf = (CGameContext *)pUserData;
579 if(!CheckClientId(ClientId: pResult->m_ClientId))
580 return;
581
582 bool HadModerator = pSelf->PlayerModerating();
583
584 CPlayer *pPlayer = pSelf->m_apPlayers[pResult->m_ClientId];
585 pPlayer->m_Moderating = !pPlayer->m_Moderating;
586
587 if(!HadModerator && pPlayer->m_Moderating)
588 pSelf->SendChat(ClientId: -1, Team: TEAM_ALL, pText: "Server kick/spec votes will now be actively moderated.", SpamProtectionClientId: 0);
589
590 if(!pSelf->PlayerModerating())
591 pSelf->SendChat(ClientId: -1, Team: TEAM_ALL, pText: "Server kick/spec votes are no longer actively moderated.", SpamProtectionClientId: 0);
592
593 if(pPlayer->m_Moderating)
594 pSelf->SendChatTarget(To: pResult->m_ClientId, pText: "Active moderator mode enabled for you.");
595 else
596 pSelf->SendChatTarget(To: pResult->m_ClientId, pText: "Active moderator mode disabled for you.");
597}
598
599void CGameContext::ConSetDDRTeam(IConsole::IResult *pResult, void *pUserData)
600{
601 CGameContext *pSelf = (CGameContext *)pUserData;
602 auto *pController = pSelf->m_pController;
603
604 if(g_Config.m_SvTeam == SV_TEAM_FORBIDDEN || g_Config.m_SvTeam == SV_TEAM_FORCED_SOLO)
605 {
606 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "join",
607 pStr: "Teams are disabled");
608 return;
609 }
610
611 const int Target = pResult->GetVictim(Slot: 0);
612 CPlayer *pPlayer = pSelf->m_apPlayers[Target];
613 if(!pPlayer)
614 return;
615
616 const int Team = pResult->GetInteger(Index: 1);
617 if(!pController->Teams().IsValidTeamNumber(Team))
618 return;
619
620 CCharacter *pChr = pSelf->GetPlayerChar(ClientId: Target);
621
622 if((pSelf->GetDDRaceTeam(ClientId: Target) && pController->Teams().GetDDRaceState(Player: pPlayer) == ERaceState::STARTED) || (pChr && pController->Teams().IsPractice(Team: pChr->Team())))
623 pPlayer->KillCharacter(Weapon: WEAPON_GAME);
624
625 pController->Teams().SetForceCharacterTeam(ClientId: Target, Team);
626 pController->Teams().SetTeamLock(Team, Lock: true);
627}
628
629void CGameContext::ConUninvite(IConsole::IResult *pResult, void *pUserData)
630{
631 CGameContext *pSelf = (CGameContext *)pUserData;
632 auto *pController = pSelf->m_pController;
633
634 const int Target = pResult->GetVictim(Slot: 0);
635 if(!pSelf->m_apPlayers[Target])
636 return;
637
638 pController->Teams().SetClientInvited(Team: pResult->GetInteger(Index: 1), ClientId: Target, Invited: false);
639}
640
641void CGameContext::ConVoteNo(IConsole::IResult *pResult, void *pUserData)
642{
643 CGameContext *pSelf = (CGameContext *)pUserData;
644
645 pSelf->ForceVote(Success: false);
646}
647
648void CGameContext::ConDrySave(IConsole::IResult *pResult, void *pUserData)
649{
650 CGameContext *pSelf = (CGameContext *)pUserData;
651 if(!CheckClientId(ClientId: pResult->m_ClientId))
652 return;
653 CPlayer *pPlayer = pSelf->m_apPlayers[pResult->m_ClientId];
654 if(!pPlayer || !pSelf->Server()->IsRconAuthedAdmin(ClientId: pResult->m_ClientId))
655 return;
656
657 CSaveTeam SavedTeam;
658 int Team = pSelf->GetDDRaceTeam(ClientId: pResult->m_ClientId);
659 ESaveResult Result = SavedTeam.Save(pGameServer: pSelf, Team, Dry: true);
660 if(CSaveTeam::HandleSaveError(Result, ClientId: pResult->m_ClientId, pGameContext: pSelf))
661 return;
662
663 char aTimestamp[32];
664 str_timestamp(buffer: aTimestamp, buffer_size: sizeof(aTimestamp));
665 const char *pSaveState = SavedTeam.GetString();
666 if(!pSaveState)
667 {
668 pSelf->SendChatTarget(To: pResult->m_ClientId, pText: "Your team is too large to save");
669 return;
670 }
671
672 char aBuf[64];
673 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s_%s_%s.save", pSelf->Map()->BaseName(), aTimestamp, pSelf->Server()->GetAuthName(ClientId: pResult->m_ClientId));
674 IOHANDLE File = pSelf->Storage()->OpenFile(pFilename: aBuf, Flags: IOFLAG_WRITE, Type: IStorage::TYPE_SAVE);
675 if(!File)
676 return;
677
678 io_write(io: File, buffer: pSaveState, size: str_length(str: pSaveState));
679 io_close(io: File);
680}
681
682void CGameContext::ConReloadCensorlist(IConsole::IResult *pResult, void *pUserData)
683{
684 CGameContext *pSelf = (CGameContext *)pUserData;
685 pSelf->ReadCensorList();
686}
687
688void CGameContext::ConDumpAntibot(IConsole::IResult *pResult, void *pUserData)
689{
690 CGameContext *pSelf = (CGameContext *)pUserData;
691 pSelf->Antibot()->ConsoleCommand(ClientId: pResult->m_ClientId, pCommand: "dump");
692}
693
694void CGameContext::ConAntibot(IConsole::IResult *pResult, void *pUserData)
695{
696 CGameContext *pSelf = (CGameContext *)pUserData;
697 pSelf->Antibot()->ConsoleCommand(ClientId: pResult->m_ClientId, pCommand: pResult->GetString(Index: 0));
698}
699
700void CGameContext::ConDumpLog(IConsole::IResult *pResult, void *pUserData)
701{
702 CGameContext *pSelf = (CGameContext *)pUserData;
703 int LimitSecs = MAX_LOG_SECONDS;
704 if(pResult->NumArguments() > 0)
705 LimitSecs = pResult->GetInteger(Index: 0);
706
707 if(LimitSecs < 0)
708 return;
709
710 int Iterator = pSelf->m_LatestLog;
711 for(int i = 0; i < MAX_LOGS; i++)
712 {
713 CLog *pEntry = &pSelf->m_aLogs[Iterator];
714 Iterator = (Iterator + 1) % MAX_LOGS;
715
716 if(!pEntry->m_Timestamp)
717 continue;
718
719 int Seconds = (time_get() - pEntry->m_Timestamp) / time_freq();
720 if(Seconds > LimitSecs)
721 continue;
722
723 char aBuf[sizeof(pEntry->m_aDescription) + 128];
724 if(pEntry->m_FromServer)
725 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s, %d seconds ago", pEntry->m_aDescription, Seconds);
726 else
727 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s, %d seconds ago < addr=<{%s}> name='%s' client=%d",
728 pEntry->m_aDescription, Seconds, pEntry->m_aClientAddrStr, pEntry->m_aClientName, pEntry->m_ClientVersion);
729 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "log", pStr: aBuf);
730 }
731}
732
733void CGameContext::LogEvent(const char *Description, int ClientId)
734{
735 CLog *pNewEntry = &m_aLogs[m_LatestLog];
736 m_LatestLog = (m_LatestLog + 1) % MAX_LOGS;
737
738 pNewEntry->m_Timestamp = time_get();
739 str_copy(dst&: pNewEntry->m_aDescription, src: Description);
740 pNewEntry->m_FromServer = ClientId < 0;
741 if(!pNewEntry->m_FromServer)
742 {
743 pNewEntry->m_ClientVersion = Server()->GetClientVersion(ClientId);
744 str_copy(dst&: pNewEntry->m_aClientAddrStr, src: Server()->ClientAddrString(ClientId, IncludePort: false));
745 str_copy(dst&: pNewEntry->m_aClientName, src: Server()->ClientName(ClientId));
746 }
747}
748