1/* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
2#include "plasma.h"
3
4#include "character.h"
5
6#include <engine/server.h>
7
8#include <generated/protocol.h>
9
10#include <game/mapitems.h>
11#include <game/server/gamecontext.h>
12#include <game/teamscore.h>
13
14const float PLASMA_ACCEL = 1.1f;
15
16CPlasma::CPlasma(CGameWorld *pGameWorld, vec2 Pos, vec2 Dir, bool Freeze,
17 bool Explosive, int ForClientId) :
18 CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER, true)
19{
20 m_Pos = Pos;
21 m_Number = 0;
22 m_Layer = LAYER_GAME;
23 m_Core = Dir;
24 m_Freeze = Freeze;
25 m_Explosive = Explosive;
26 m_ForClientId = ForClientId;
27 m_EvalTick = Server()->Tick();
28 m_LifeTime = Server()->TickSpeed() * 1.5f;
29
30 GameWorld()->InsertEntity(pEntity: this);
31}
32
33void CPlasma::Tick()
34{
35 // A plasma bullet has only a limited lifetime
36 if(m_LifeTime == 0)
37 {
38 Reset();
39 return;
40 }
41 CCharacter *pTarget = GameServer()->GetPlayerChar(ClientId: m_ForClientId);
42 // Without a target, a plasma bullet has no reason to live
43 if(!pTarget)
44 {
45 Reset();
46 return;
47 }
48 m_LifeTime--;
49 Move();
50 HitCharacter(pTarget);
51 // Plasma bullets may explode twice if they would hit both a player and an obstacle in the next move step
52 HitObstacle(pTarget);
53}
54
55void CPlasma::Move()
56{
57 m_Pos += m_Core;
58 m_Core *= PLASMA_ACCEL;
59}
60
61bool CPlasma::HitCharacter(CCharacter *pTarget)
62{
63 vec2 IntersectPos;
64 CCharacter *pHitPlayer = GameServer()->m_World.IntersectCharacter(
65 Pos0: m_Pos, Pos1: m_Pos + m_Core, Radius: 0.0f, NewPos&: IntersectPos, pNotThis: nullptr, CollideWith: m_ForClientId);
66 if(!pHitPlayer)
67 {
68 return false;
69 }
70
71 // Super player should not be able to stop the plasma bullets
72 if(pHitPlayer->Team() == TEAM_SUPER)
73 {
74 return false;
75 }
76
77 m_Freeze ? pHitPlayer->Freeze() : pHitPlayer->Unfreeze();
78 if(m_Explosive)
79 {
80 // Plasma Turrets are very precise weapons only one tee gets speed from it,
81 // other tees near the explosion remain unaffected
82 GameServer()->CreateExplosion(
83 Pos: m_Pos, Owner: m_ForClientId, Weapon: WEAPON_GRENADE, NoDamage: true, ActivatedTeam: pTarget->Team(), Mask: pTarget->TeamMask());
84 }
85 Reset();
86 return true;
87}
88
89bool CPlasma::HitObstacle(CCharacter *pTarget)
90{
91 // Check if the plasma bullet is stopped by a solid block or a laser stopper
92 int HasIntersection = GameServer()->Collision()->IntersectNoLaser(Pos0: m_Pos, Pos1: m_Pos + m_Core, pOutCollision: nullptr, pOutBeforeCollision: nullptr);
93 if(HasIntersection)
94 {
95 if(m_Explosive)
96 {
97 // Even in the case of an explosion due to a collision with obstacles, only one player is affected
98 GameServer()->CreateExplosion(
99 Pos: m_Pos, Owner: m_ForClientId, Weapon: WEAPON_GRENADE, NoDamage: true, ActivatedTeam: pTarget->Team(), Mask: pTarget->TeamMask());
100 }
101 Reset();
102 return true;
103 }
104 return false;
105}
106
107void CPlasma::Reset()
108{
109 m_MarkedForDestroy = true;
110}
111
112void CPlasma::Snap(int SnappingClient)
113{
114 // Only players who can see the targeted player can see the plasma bullet
115 CCharacter *pTarget = GameServer()->GetPlayerChar(ClientId: m_ForClientId);
116 if(!pTarget || !pTarget->CanSnapCharacter(SnappingClient))
117 {
118 return;
119 }
120
121 // Only players with the plasma bullet in their field of view or who want to see everything will receive the snap
122 if(NetworkClipped(SnappingClient) || !GetId().has_value())
123 return;
124
125 int SnappingClientVersion = GameServer()->GetClientVersion(ClientId: SnappingClient);
126
127 int Subtype = (m_Explosive ? 1 : 0) | (m_Freeze ? 2 : 0);
128 GameServer()->SnapLaserObject(Context: CSnapContext(SnappingClientVersion, Server()->IsSixup(ClientId: SnappingClient), SnappingClient), SnapId: GetId().value(),
129 To: m_Pos, From: m_Pos, StartTick: m_EvalTick, Owner: m_ForClientId, LaserType: LASERTYPE_PLASMA, Subtype, SwitchNumber: m_Number);
130}
131
132void CPlasma::SwapClients(int Client1, int Client2)
133{
134 m_ForClientId = m_ForClientId == Client1 ? Client2 : (m_ForClientId == Client2 ? Client1 : m_ForClientId);
135}
136