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#include "laser.h"
4
5#include "character.h"
6
7#include <engine/shared/config.h>
8#include <engine/shared/protocol.h>
9
10#include <generated/protocol.h>
11
12#include <game/mapitems.h>
13#include <game/server/gamecontext.h>
14#include <game/server/gamemodes/ddnet.h>
15#include <game/server/player.h>
16
17CLaser::CLaser(CGameWorld *pGameWorld, vec2 Pos, vec2 Direction, float StartEnergy, int Owner, int Type) :
18 CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER, true)
19{
20 m_Pos = Pos;
21 m_Number = 0;
22 m_Layer = LAYER_GAME;
23 m_Owner = Owner;
24 m_Energy = StartEnergy;
25 m_Dir = Direction;
26 m_Bounces = 0;
27 m_EvalTick = 0;
28 m_TelePos = vec2(0, 0);
29 m_WasTele = false;
30 m_Type = Type;
31 m_TeleportCancelled = false;
32 m_IsBlueTeleport = false;
33 m_ZeroEnergyBounceInLastTick = false;
34 m_TuneZone = GameServer()->Collision()->IsTune(Index: GameServer()->Collision()->GetMapIndex(Pos: m_Pos));
35 CCharacter *pOwnerChar = GameServer()->GetPlayerChar(ClientId: m_Owner);
36 m_BelongsToPracticeTeam = pOwnerChar && pOwnerChar->Teams()->IsPractice(Team: pOwnerChar->Team());
37
38 CPlayer *pOwnerPlayer = (Owner >= 0 && Owner < MAX_CLIENTS) ? GameServer()->m_apPlayers[m_Owner] : nullptr;
39 m_InteractState.Init(OwnerId: Owner, UniqueOwnerId: pOwnerPlayer ? pOwnerPlayer->GetUniqueCid() : 0);
40 SyncInteractState();
41 GameWorld()->InsertEntity(pEntity: this);
42 DoBounce();
43}
44
45bool CLaser::HitCharacter(vec2 From, vec2 To)
46{
47 static const vec2 StackedLaserShotgunBugSpeed = vec2(-2147483648.0f, -2147483648.0f);
48 SyncInteractState();
49 vec2 At;
50 CCharacter *pOwnerChar = GameServer()->GetPlayerChar(ClientId: m_Owner);
51 CCharacter *pHit;
52 bool pDontHitSelf = g_Config.m_SvOldLaser || (m_Bounces == 0 && !m_WasTele);
53
54 if(pOwnerChar ? (!pOwnerChar->LaserHitDisabled() && m_Type == WEAPON_LASER) || (!pOwnerChar->ShotgunHitDisabled() && m_Type == WEAPON_SHOTGUN) : g_Config.m_SvHit)
55 pHit = GameWorld()->IntersectCharacter(Pos0: m_Pos, Pos1: To, Radius: 0.f, NewPos&: At, pNotThis: pDontHitSelf ? pOwnerChar : nullptr, CollideWith: m_Owner);
56 else
57 pHit = GameWorld()->IntersectCharacter(Pos0: m_Pos, Pos1: To, Radius: 0.f, NewPos&: At, pNotThis: pDontHitSelf ? pOwnerChar : nullptr, CollideWith: m_Owner, pThisOnly: pOwnerChar);
58
59 if(!pHit || !m_InteractState.CanHit(pGameServer: GameServer(), ClientId: pHit->GetPlayer()->GetCid()))
60 return false;
61 m_From = From;
62 m_Pos = At;
63 m_Energy = -1;
64 if(m_Type == WEAPON_SHOTGUN)
65 {
66 float Strength = TuningList()[m_TuneZone].m_ShotgunStrength;
67 const vec2 &HitPos = pHit->Core()->m_Pos;
68 if(!g_Config.m_SvOldLaser)
69 {
70 if(m_PrevPos != HitPos)
71 {
72 pHit->AddVelocity(Addition: normalize(v: m_PrevPos - HitPos) * Strength);
73 }
74 else
75 {
76 pHit->SetRawVelocity(StackedLaserShotgunBugSpeed);
77 }
78 }
79 else if(g_Config.m_SvOldLaser && pOwnerChar)
80 {
81 if(pOwnerChar->Core()->m_Pos != HitPos)
82 {
83 pHit->AddVelocity(Addition: normalize(v: pOwnerChar->Core()->m_Pos - HitPos) * Strength);
84 }
85 else
86 {
87 pHit->SetRawVelocity(StackedLaserShotgunBugSpeed);
88 }
89 }
90 else
91 {
92 // Re-apply move restrictions as a part of 'shotgun bug' reproduction
93 pHit->ApplyMoveRestrictions();
94 }
95 }
96 else if(m_Type == WEAPON_LASER)
97 {
98 pHit->Unfreeze();
99 }
100 pHit->TakeDamage(Force: vec2(0, 0), Dmg: 0, From: m_Owner, Weapon: m_Type);
101 return true;
102}
103
104void CLaser::DoBounce()
105{
106 m_EvalTick = Server()->Tick();
107
108 if(m_Energy < 0)
109 {
110 m_MarkedForDestroy = true;
111 return;
112 }
113 m_PrevPos = m_Pos;
114 vec2 Coltile;
115
116 int Res;
117 int z;
118
119 if(m_WasTele)
120 {
121 m_PrevPos = m_TelePos;
122 m_Pos = m_TelePos;
123 m_TelePos = vec2(0, 0);
124 }
125
126 vec2 To = m_Pos + m_Dir * m_Energy;
127
128 Res = GameServer()->Collision()->IntersectLineTeleWeapon(Pos0: m_Pos, Pos1: To, pOutCollision: &Coltile, pOutBeforeCollision: &To, pTeleNr: &z);
129
130 if(Res)
131 {
132 if(!HitCharacter(From: m_Pos, To))
133 {
134 // intersected
135 m_From = m_Pos;
136 m_Pos = To;
137
138 vec2 TempPos = m_Pos;
139 vec2 TempDir = m_Dir * 4.0f;
140
141 int f = 0;
142 if(Res == -1)
143 {
144 f = GameServer()->Collision()->GetTile(x: round_to_int(f: Coltile.x), y: round_to_int(f: Coltile.y));
145 GameServer()->Collision()->SetCollisionAt(x: round_to_int(f: Coltile.x), y: round_to_int(f: Coltile.y), Index: TILE_SOLID);
146 }
147 GameServer()->Collision()->MovePoint(pInoutPos: &TempPos, pInoutVel: &TempDir, Elasticity: 1.0f, pBounces: nullptr);
148 if(Res == -1)
149 {
150 GameServer()->Collision()->SetCollisionAt(x: round_to_int(f: Coltile.x), y: round_to_int(f: Coltile.y), Index: f);
151 }
152 m_Pos = TempPos;
153 m_Dir = normalize(v: TempDir);
154
155 const float Distance = distance(a: m_From, b: m_Pos);
156 // Prevent infinite bounces
157 if(Distance == 0.0f && m_ZeroEnergyBounceInLastTick)
158 {
159 m_Energy = -1;
160 }
161 else
162 {
163 m_Energy -= Distance + GameServer()->TuningList()[m_TuneZone].m_LaserBounceCost;
164 }
165 m_ZeroEnergyBounceInLastTick = Distance == 0.0f;
166
167 if(Res == TILE_TELEINWEAPON && !GameServer()->Collision()->TeleOuts(Number: z - 1).empty())
168 {
169 int TeleOut = GameServer()->m_World.m_Core.RandomOr0(BelowThis: GameServer()->Collision()->TeleOuts(Number: z - 1).size());
170 m_TelePos = GameServer()->Collision()->TeleOuts(Number: z - 1)[TeleOut];
171 m_WasTele = true;
172 }
173 else
174 {
175 m_Bounces++;
176 m_WasTele = false;
177 }
178
179 int BounceNum = TuningList()[m_TuneZone].m_LaserBounceNum;
180
181 if(m_Bounces > BounceNum)
182 m_Energy = -1;
183
184 GameServer()->CreateSound(Pos: m_Pos, Sound: SOUND_LASER_BOUNCE, Mask: m_InteractState.CanSeeMask(pGameServer: GameServer()));
185 }
186 }
187 else
188 {
189 if(!HitCharacter(From: m_Pos, To))
190 {
191 m_From = m_Pos;
192 m_Pos = To;
193 m_Energy = -1;
194 }
195 }
196
197 CCharacter *pOwnerChar = GameServer()->GetPlayerChar(ClientId: m_Owner);
198 if(m_Owner >= 0 && m_Energy <= 0 && !m_TeleportCancelled && pOwnerChar &&
199 pOwnerChar->IsAlive() && pOwnerChar->HasTelegunLaser() && m_Type == WEAPON_LASER)
200 {
201 vec2 PossiblePos;
202 bool Found = false;
203
204 // Check if the laser hits a player.
205 bool DontHitSelf = g_Config.m_SvOldLaser || (m_Bounces == 0 && !m_WasTele);
206 vec2 At;
207 CCharacter *pHit;
208 if(pOwnerChar ? (!pOwnerChar->LaserHitDisabled() && m_Type == WEAPON_LASER) : g_Config.m_SvHit)
209 pHit = GameServer()->m_World.IntersectCharacter(Pos0: m_Pos, Pos1: To, Radius: 0.f, NewPos&: At, pNotThis: DontHitSelf ? pOwnerChar : nullptr, CollideWith: m_Owner);
210 else
211 pHit = GameServer()->m_World.IntersectCharacter(Pos0: m_Pos, Pos1: To, Radius: 0.f, NewPos&: At, pNotThis: DontHitSelf ? pOwnerChar : nullptr, CollideWith: m_Owner, pThisOnly: pOwnerChar);
212
213 if(pHit)
214 Found = GetNearestAirPosPlayer(PlayerPos: pHit->m_Pos, pOutPos: &PossiblePos);
215 else
216 Found = GetNearestAirPos(Pos: m_Pos, PrevPos: m_From, pOutPos: &PossiblePos);
217
218 if(Found)
219 {
220 pOwnerChar->m_TeleGunPos = PossiblePos;
221 pOwnerChar->m_TeleGunTeleport = true;
222 pOwnerChar->m_IsBlueTeleGunTeleport = m_IsBlueTeleport;
223 }
224 }
225 else if(m_Owner >= 0)
226 {
227 int MapIndex = GameServer()->Collision()->GetPureMapIndex(Pos: Coltile);
228 int TileFIndex = GameServer()->Collision()->GetFrontTileIndex(Index: MapIndex);
229 bool IsSwitchTeleGun = GameServer()->Collision()->GetSwitchType(Index: MapIndex) == TILE_ALLOW_TELE_GUN;
230 bool IsBlueSwitchTeleGun = GameServer()->Collision()->GetSwitchType(Index: MapIndex) == TILE_ALLOW_BLUE_TELE_GUN;
231 int IsTeleInWeapon = GameServer()->Collision()->IsTeleportWeapon(Index: MapIndex);
232
233 if(!IsTeleInWeapon)
234 {
235 if(IsSwitchTeleGun || IsBlueSwitchTeleGun)
236 {
237 // Delay specifies which weapon the tile should work for.
238 // Delay = 0 means all.
239 const int Delay = GameServer()->Collision()->GetSwitchDelay(Index: MapIndex);
240
241 if((Delay != 3 && Delay != 0) && m_Type == WEAPON_LASER)
242 {
243 IsSwitchTeleGun = IsBlueSwitchTeleGun = false;
244 }
245 }
246
247 m_IsBlueTeleport = TileFIndex == TILE_ALLOW_BLUE_TELE_GUN || IsBlueSwitchTeleGun;
248
249 // Teleport is canceled if the last bounce tile is not a TILE_ALLOW_TELE_GUN.
250 // Teleport also works if laser didn't bounce.
251 m_TeleportCancelled =
252 m_Type == WEAPON_LASER && (TileFIndex != TILE_ALLOW_TELE_GUN && TileFIndex != TILE_ALLOW_BLUE_TELE_GUN && !IsSwitchTeleGun && !IsBlueSwitchTeleGun);
253 }
254 }
255}
256
257void CLaser::Reset()
258{
259 m_MarkedForDestroy = true;
260}
261
262void CLaser::Tick()
263{
264 SyncInteractState();
265 if((g_Config.m_SvDestroyLasersOnDeath || m_BelongsToPracticeTeam) && m_Owner >= 0)
266 {
267 CCharacter *pOwnerChar = GameServer()->GetPlayerChar(ClientId: m_Owner);
268 if(!(pOwnerChar && pOwnerChar->IsAlive()))
269 {
270 Reset();
271 }
272 }
273
274 float Delay = TuningList()[m_TuneZone].m_LaserBounceDelay;
275 if((Server()->Tick() - m_EvalTick) > (Server()->TickSpeed() * Delay / 1000.0f))
276 DoBounce();
277}
278
279void CLaser::TickPaused()
280{
281 ++m_EvalTick;
282}
283
284void CLaser::Snap(int SnappingClient)
285{
286 if((NetworkClipped(SnappingClient) && NetworkClipped(SnappingClient, CheckPos: m_From)) || !GetId().has_value())
287 return;
288
289 if(SnappingClient != SERVER_DEMO_CLIENT && !m_InteractState.CanSee(pGameServer: GameServer(), ClientId: SnappingClient))
290 return;
291
292 int SnappingClientVersion = GameServer()->GetClientVersion(ClientId: SnappingClient);
293 int LaserType = m_Type == WEAPON_LASER ? LASERTYPE_RIFLE : (m_Type == WEAPON_SHOTGUN ? LASERTYPE_SHOTGUN : -1);
294
295 GameServer()->SnapLaserObject(Context: CSnapContext(SnappingClientVersion, Server()->IsSixup(ClientId: SnappingClient), SnappingClient), SnapId: GetId().value(),
296 To: m_Pos, From: m_From, StartTick: m_EvalTick, Owner: m_Owner, LaserType, Subtype: 0, SwitchNumber: m_Number);
297}
298
299void CLaser::SwapClients(int Client1, int Client2)
300{
301 m_Owner = m_Owner == Client1 ? Client2 : (m_Owner == Client2 ? Client1 : m_Owner);
302}
303
304void CLaser::SyncInteractState()
305{
306 CPlayer *pOwnerPlayer = (m_Owner >= 0 && m_Owner < MAX_CLIENTS) ? GameServer()->m_apPlayers[m_Owner] : nullptr;
307 CCharacter *pOwnerChar = GameServer()->GetPlayerChar(ClientId: m_Owner);
308
309 // as long as the owner is connected
310 // refill the state on tick
311 // as soon as the owner disconnects keep that state
312 if(pOwnerPlayer)
313 {
314 bool NoHitOthers = g_Config.m_SvHit == 0;
315 if(pOwnerChar)
316 NoHitOthers = (m_Type == WEAPON_LASER && pOwnerChar->LaserHitDisabled()) || (m_Type == WEAPON_SHOTGUN && pOwnerChar->ShotgunHitDisabled());
317 bool NoHitSelf = g_Config.m_SvOldLaser || (m_Bounces == 0 && !m_WasTele);
318 m_InteractState.FillOwnerConnected(
319 OwnerAlive: pOwnerChar && pOwnerChar->IsAlive(),
320 DDRaceTeam: pOwnerPlayer ? GameServer()->GetDDRaceTeam(ClientId: m_Owner) : 0,
321 Solo: pOwnerChar && pOwnerChar->Core()->m_Solo,
322 NoHitOthers,
323 NoHitSelf);
324 }
325 else
326 {
327 m_InteractState.FillOwnerDisconnected();
328 }
329}
330