1/* See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
2#include "dragger_beam.h"
3
4#include "character.h"
5#include "dragger.h"
6
7#include <engine/server.h>
8#include <engine/shared/config.h>
9
10#include <generated/protocol.h>
11
12#include <game/mapitems.h>
13#include <game/server/gamecontext.h>
14#include <game/server/save.h>
15
16CDraggerBeam::CDraggerBeam(CGameWorld *pGameWorld, CDragger *pDragger, vec2 Pos, float Strength, bool IgnoreWalls,
17 int ForClientId, int Layer, int Number) :
18 CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER, true)
19{
20 m_pDragger = pDragger;
21 m_Pos = Pos;
22 m_Strength = Strength;
23 m_IgnoreWalls = IgnoreWalls;
24 m_ForClientId = ForClientId;
25 m_Active = true;
26 m_Layer = Layer;
27 m_Number = Number;
28 m_EvalTick = Server()->Tick();
29
30 GameWorld()->InsertEntity(pEntity: this);
31}
32
33void CDraggerBeam::Tick()
34{
35 if(!m_Active)
36 {
37 return;
38 }
39
40 // Drag only if the player is reachable and alive
41 CCharacter *pTarget = GameServer()->GetPlayerChar(ClientId: m_ForClientId);
42 if(!pTarget)
43 {
44 Reset();
45 return;
46 }
47
48 // The following checks are necessary, because the checks in CDragger::LookForPlayersToDrag only take place
49 // after CDraggerBeam::Tick and only every 150ms
50 // When the dragger is disabled for the target player's team, the dragger beam dissolves. The check if a dragger
51 // is disabled is only executed every 150ms, so the beam can stay activated up to 6 extra ticks
52 if(Server()->Tick() % (int)(Server()->TickSpeed() * 0.15f) == 0)
53 {
54 if(m_Layer == LAYER_SWITCH && m_Number > 0 &&
55 !Switchers()[m_Number].m_aStatus[pTarget->Team()])
56 {
57 Reset();
58 return;
59 }
60 }
61
62 // When the dragger can no longer reach the target player, the dragger beam dissolves
63 if(distance(a: pTarget->m_Pos, b: m_Pos) >= g_Config.m_SvDraggerRange || !pTarget->IsAlive() ||
64 (m_IgnoreWalls ?
65 GameServer()->Collision()->IntersectNoLaserNoWalls(Pos0: m_Pos, Pos1: pTarget->m_Pos, pOutCollision: nullptr, pOutBeforeCollision: nullptr) :
66 GameServer()->Collision()->IntersectNoLaser(Pos0: m_Pos, Pos1: pTarget->m_Pos, pOutCollision: nullptr, pOutBeforeCollision: nullptr)))
67 {
68 Reset();
69 return;
70 }
71 // In the center of the dragger a tee does not experience speed-up
72 else if(distance(a: pTarget->m_Pos, b: m_Pos) > 28)
73 {
74 pTarget->AddVelocity(Addition: normalize(v: m_Pos - pTarget->m_Pos) * m_Strength);
75 }
76}
77
78void CDraggerBeam::SetPos(vec2 Pos)
79{
80 m_Pos = Pos;
81}
82
83void CDraggerBeam::Reset()
84{
85 m_MarkedForDestroy = true;
86 m_Active = false;
87
88 m_pDragger->RemoveDraggerBeam(ClientId: m_ForClientId);
89}
90
91void CDraggerBeam::Snap(int SnappingClient)
92{
93 if(!m_Active)
94 {
95 return;
96 }
97
98 // Only players who can see the player attached to the dragger can see the dragger beam
99 CCharacter *pTarget = GameServer()->GetPlayerChar(ClientId: m_ForClientId);
100 if(!pTarget || !pTarget->CanSnapCharacter(SnappingClient))
101 {
102 return;
103 }
104 // Only players with the dragger beam in their field of view or who want to see everything will receive the snap
105 vec2 TargetPos = vec2(pTarget->m_Pos.x, pTarget->m_Pos.y);
106 if(distance(a: pTarget->m_Pos, b: m_Pos) >= g_Config.m_SvDraggerRange || NetworkClippedLine(SnappingClient, StartPos: m_Pos, EndPos: TargetPos))
107 {
108 return;
109 }
110
111 int Subtype = (m_IgnoreWalls ? 1 : 0) | (std::clamp(val: round_to_int(f: m_Strength - 1.f), lo: 0, hi: 2) << 1);
112
113 int StartTick = m_EvalTick;
114 if(StartTick < Server()->Tick() - 4)
115 {
116 StartTick = Server()->Tick() - 4;
117 }
118 else if(StartTick > Server()->Tick())
119 {
120 StartTick = Server()->Tick();
121 }
122
123 int SnappingClientVersion = GameServer()->GetClientVersion(ClientId: SnappingClient);
124 if(SnappingClientVersion >= VERSION_DDNET_ENTITY_NETOBJS)
125 {
126 StartTick = -1;
127 }
128
129 std::optional<int> SnapObjId = GetId();
130 if(m_pDragger->WillDraggerBeamUseDraggerId(TargetClientId: m_ForClientId, SnappingClientId: SnappingClient) && m_pDragger->GetId().has_value())
131 {
132 SnapObjId = m_pDragger->GetId();
133 }
134
135 if(!SnapObjId.has_value())
136 return;
137
138 GameServer()->SnapLaserObject(Context: CSnapContext(SnappingClientVersion, Server()->IsSixup(ClientId: SnappingClient), SnappingClient), SnapId: SnapObjId.value(),
139 To: TargetPos, From: m_Pos, StartTick, Owner: m_ForClientId, LaserType: LASERTYPE_DRAGGER, Subtype, SwitchNumber: m_Number);
140}
141
142void CDraggerBeam::SwapClients(int Client1, int Client2)
143{
144 m_ForClientId = m_ForClientId == Client1 ? Client2 : (m_ForClientId == Client2 ? Client1 : m_ForClientId);
145}
146
147ESaveResult CDraggerBeam::BlocksSave(int ClientId)
148{
149 return m_ForClientId == ClientId ? ESaveResult::DRAGGER_ACTIVE : ESaveResult::SUCCESS;
150}
151