2011-12-25 13:33:05 +00:00
|
|
|
/* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
|
2020-09-26 19:41:58 +00:00
|
|
|
#include "light.h"
|
2010-07-29 05:21:18 +00:00
|
|
|
#include <engine/config.h>
|
|
|
|
#include <engine/server.h>
|
|
|
|
#include <game/generated/protocol.h>
|
|
|
|
#include <game/mapitems.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <game/server/gamecontext.h>
|
2021-01-09 14:37:02 +00:00
|
|
|
#include <game/server/player.h>
|
2021-10-14 20:59:39 +00:00
|
|
|
#include <game/version.h>
|
2010-07-29 05:21:18 +00:00
|
|
|
|
2021-01-09 14:18:52 +00:00
|
|
|
#include "character.h"
|
|
|
|
|
2011-12-25 13:51:04 +00:00
|
|
|
CLight::CLight(CGameWorld *pGameWorld, vec2 Pos, float Rotation, int Length,
|
2020-09-26 19:41:58 +00:00
|
|
|
int Layer, int Number) :
|
|
|
|
CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-11-13 13:22:19 +00:00
|
|
|
m_Layer = Layer;
|
|
|
|
m_Number = Number;
|
2011-12-25 13:51:04 +00:00
|
|
|
m_Tick = (Server()->TickSpeed() * 0.15f);
|
2010-08-30 23:45:42 +00:00
|
|
|
m_Pos = Pos;
|
|
|
|
m_Rotation = Rotation;
|
|
|
|
m_Length = Length;
|
|
|
|
m_EvalTick = Server()->Tick();
|
2010-07-29 05:21:18 +00:00
|
|
|
GameWorld()->InsertEntity(this);
|
2010-08-30 23:45:42 +00:00
|
|
|
Step();
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 23:45:42 +00:00
|
|
|
bool CLight::HitCharacter()
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2011-12-25 13:51:04 +00:00
|
|
|
std::list<CCharacter *> HitCharacters =
|
2020-09-26 19:41:58 +00:00
|
|
|
GameServer()->m_World.IntersectedCharacters(m_Pos, m_To, 0.0f, 0);
|
|
|
|
if(HitCharacters.empty())
|
2011-12-25 13:51:04 +00:00
|
|
|
return false;
|
2020-10-26 13:11:11 +00:00
|
|
|
for(auto *Char : HitCharacters)
|
2011-12-25 13:51:04 +00:00
|
|
|
{
|
2021-01-20 19:00:16 +00:00
|
|
|
if(m_Layer == LAYER_SWITCH && m_Number > 0 && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[Char->Team()])
|
2011-12-25 13:51:04 +00:00
|
|
|
continue;
|
2011-01-25 09:34:14 +00:00
|
|
|
Char->Freeze();
|
2010-08-31 17:32:25 +00:00
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-30 23:45:42 +00:00
|
|
|
void CLight::Move()
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(m_Speed != 0)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if((m_CurveLength >= m_Length && m_Speed > 0) || (m_CurveLength <= 0 && m_Speed < 0))
|
2011-12-25 13:51:04 +00:00
|
|
|
m_Speed = -m_Speed;
|
|
|
|
m_CurveLength += m_Speed * m_Tick + m_LengthL;
|
|
|
|
m_LengthL = 0;
|
2020-09-26 19:41:58 +00:00
|
|
|
if(m_CurveLength > m_Length)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2011-12-25 13:51:04 +00:00
|
|
|
m_LengthL = m_CurveLength - m_Length;
|
|
|
|
m_CurveLength = m_Length;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(m_CurveLength < 0)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2011-12-25 13:51:04 +00:00
|
|
|
m_LengthL = 0 + m_CurveLength;
|
|
|
|
m_CurveLength = 0;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-07 13:28:29 +00:00
|
|
|
|
2011-12-25 13:51:04 +00:00
|
|
|
m_Rotation += m_AngularSpeed * m_Tick;
|
2020-09-26 19:41:58 +00:00
|
|
|
if(m_Rotation > pi * 2)
|
2011-12-25 13:51:04 +00:00
|
|
|
m_Rotation -= pi * 2;
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(m_Rotation < 0)
|
2011-12-25 13:51:04 +00:00
|
|
|
m_Rotation += pi * 2;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 23:45:42 +00:00
|
|
|
void CLight::Step()
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-08-30 23:45:42 +00:00
|
|
|
Move();
|
|
|
|
vec2 dir(sin(m_Rotation), cos(m_Rotation));
|
2011-12-25 13:51:04 +00:00
|
|
|
vec2 to2 = m_Pos + normalize(dir) * m_CurveLength;
|
|
|
|
GameServer()->Collision()->IntersectNoLaser(m_Pos, to2, &m_To, 0);
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2011-12-25 13:51:04 +00:00
|
|
|
|
2010-07-29 05:21:18 +00:00
|
|
|
void CLight::Reset()
|
|
|
|
{
|
2021-05-22 18:02:00 +00:00
|
|
|
m_MarkedForDestroy = true;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLight::Tick()
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(Server()->Tick() % int(Server()->TickSpeed() * 0.15f) == 0)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-11-01 01:51:17 +00:00
|
|
|
int Flags;
|
2011-12-25 13:51:04 +00:00
|
|
|
m_EvalTick = Server()->Tick();
|
|
|
|
int index = GameServer()->Collision()->IsMover(m_Pos.x, m_Pos.y,
|
2020-09-26 19:41:58 +00:00
|
|
|
&Flags);
|
|
|
|
if(index)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2011-12-25 13:51:04 +00:00
|
|
|
m_Core = GameServer()->Collision()->CpSpeed(index, Flags);
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2011-12-25 13:51:04 +00:00
|
|
|
m_Pos += m_Core;
|
2010-08-30 23:45:42 +00:00
|
|
|
Step();
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2010-10-07 13:28:29 +00:00
|
|
|
|
2010-08-30 23:45:42 +00:00
|
|
|
HitCharacter();
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 13:22:19 +00:00
|
|
|
void CLight::Snap(int SnappingClient)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2021-10-20 23:02:44 +00:00
|
|
|
if(NetworkClipped(SnappingClient, m_Pos) && NetworkClipped(SnappingClient, m_To))
|
|
|
|
return;
|
|
|
|
|
2022-03-04 20:23:32 +00:00
|
|
|
int SnappingClientVersion = SnappingClient != SERVER_DEMO_CLIENT ? GameServer()->GetClientVersion(SnappingClient) : CLIENT_VERSIONNR;
|
2021-10-14 20:59:39 +00:00
|
|
|
|
2021-11-06 11:13:42 +00:00
|
|
|
CNetObj_EntityEx *pEntData = 0;
|
|
|
|
if(SnappingClientVersion >= VERSION_DDNET_SWITCH && (m_Layer == LAYER_SWITCH || length(m_Core) > 0))
|
|
|
|
pEntData = static_cast<CNetObj_EntityEx *>(Server()->SnapNewItem(NETOBJTYPE_ENTITYEX, GetID(), sizeof(CNetObj_EntityEx)));
|
2021-10-14 20:59:39 +00:00
|
|
|
|
2014-01-13 16:00:49 +00:00
|
|
|
CCharacter *Char = GameServer()->GetPlayerChar(SnappingClient);
|
|
|
|
|
2022-03-04 20:23:32 +00:00
|
|
|
if(SnappingClient != SERVER_DEMO_CLIENT && (GameServer()->m_apPlayers[SnappingClient]->GetTeam() == -1 || GameServer()->m_apPlayers[SnappingClient]->IsPaused()) && GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID != SPEC_FREEVIEW)
|
2014-01-13 16:00:49 +00:00
|
|
|
Char = GameServer()->GetPlayerChar(GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID);
|
|
|
|
|
2021-11-06 11:13:42 +00:00
|
|
|
if(pEntData)
|
|
|
|
{
|
|
|
|
pEntData->m_SwitchNumber = m_Number;
|
|
|
|
pEntData->m_Layer = m_Layer;
|
|
|
|
pEntData->m_EntityClass = ENTITYCLASS_LIGHT;
|
|
|
|
}
|
|
|
|
else
|
2021-10-14 20:59:39 +00:00
|
|
|
{
|
|
|
|
int Tick = (Server()->Tick() % Server()->TickSpeed()) % 6;
|
|
|
|
if(Char && Char->IsAlive() && m_Layer == LAYER_SWITCH && m_Number > 0 && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[Char->Team()] && Tick)
|
|
|
|
return;
|
|
|
|
}
|
2010-11-14 10:16:27 +00:00
|
|
|
|
2011-12-25 13:51:04 +00:00
|
|
|
CNetObj_Laser *pObj = static_cast<CNetObj_Laser *>(Server()->SnapNewItem(
|
2021-01-09 20:40:17 +00:00
|
|
|
NETOBJTYPE_LASER, GetID(), sizeof(CNetObj_Laser)));
|
2014-01-13 16:00:49 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(!pObj)
|
2014-01-13 16:00:49 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
pObj->m_X = (int)m_Pos.x;
|
|
|
|
pObj->m_Y = (int)m_Pos.y;
|
2010-11-14 10:16:27 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(Char && Char->Team() == TEAM_SUPER)
|
2010-11-13 13:22:19 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
pObj->m_FromX = (int)m_Pos.x;
|
|
|
|
pObj->m_FromY = (int)m_Pos.y;
|
2010-11-13 13:22:19 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(Char && m_Layer == LAYER_SWITCH && GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[Char->Team()])
|
2010-11-13 13:22:19 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
pObj->m_FromX = (int)m_To.x;
|
|
|
|
pObj->m_FromY = (int)m_To.y;
|
2010-11-13 13:22:19 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(m_Layer != LAYER_SWITCH)
|
2010-11-13 13:22:19 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
pObj->m_FromX = (int)m_To.x;
|
|
|
|
pObj->m_FromY = (int)m_To.y;
|
2010-11-13 13:22:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
pObj->m_FromX = (int)m_Pos.x;
|
|
|
|
pObj->m_FromY = (int)m_Pos.y;
|
2010-11-13 13:22:19 +00:00
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
|
2021-11-06 11:13:42 +00:00
|
|
|
if(pEntData)
|
|
|
|
{
|
|
|
|
pObj->m_StartTick = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int StartTick = m_EvalTick;
|
|
|
|
if(StartTick < Server()->Tick() - 4)
|
|
|
|
StartTick = Server()->Tick() - 4;
|
|
|
|
else if(StartTick > Server()->Tick())
|
|
|
|
StartTick = Server()->Tick();
|
|
|
|
pObj->m_StartTick = StartTick;
|
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|