ddnet/src/game/server/entities/light.cpp
Robert Müller db4ec4a12d Replace usages of C math functions
Replacing the C standard headers with the C++ standard headers causes various `error: call to 'floor' promotes float to double [performance-type-promotion-in-math-fn,-warnings-as-errors]`, which are fixed by using the C++ std math functions or our own math functions instead of the C math functions.

- Use `absolute` instead of `abs` and `fabs`.
- Use `std::floor` instead of `floor` and `floorf`.
- Use `std::ceil` instead of `ceil`, `ceilf` and `round_ceil`.
- Use `std::round` instead of `round` and `roundf`.
- Use `std::sin` instead of `sin` and `sinf`.
- Use `std::asin` instead of `asin` and `asinf`.
- Use `std::cos` instead of `cos` and `cosf`.
- Use `std::acos` instead of `acos` and `acosf`.
- Use `std::tan` instead of `tan` and `tanf`.
- Use `std::atan` instead of `atan` and `atanf`.
- Use `std::pow` instead of `pow` and `powf`.
- Use `std::log` instead of `log` and `logf`.
- Use `std::log2` instead of `log2` and `log2f`.
- Use `std::log10` instead of `log10` and `log10f`.
- Use `std::pow` instead of `pow` and `powf`.
- Use `std::sqrt` instead of `sqrt` and `sqrtf`.
- Use `std::fmod` instead of `fmod` and `fmodf`.
- Use `direction(Angle)` instead of `vec2(std::cos(Angle), std::sin(Angle))`.
- Use `length(vec2(x, y))` instead of `std::sqrt(x * x + y * y)`.
- Remove unused `NormalizeAngular` and `AngularDistance` functions.
2023-03-01 19:26:53 +01:00

160 lines
4 KiB
C++

/* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
#include "light.h"
#include "character.h"
#include <engine/server.h>
#include <game/generated/protocol.h>
#include <game/mapitems.h>
#include <game/teamscore.h>
#include <game/server/gamecontext.h>
#include <game/server/player.h>
CLight::CLight(CGameWorld *pGameWorld, vec2 Pos, float Rotation, int Length,
int Layer, int Number) :
CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER)
{
m_Layer = Layer;
m_Number = Number;
m_Tick = (Server()->TickSpeed() * 0.15f);
m_Pos = Pos;
m_Rotation = Rotation;
m_Length = Length;
m_EvalTick = Server()->Tick();
GameWorld()->InsertEntity(this);
Step();
}
bool CLight::HitCharacter()
{
std::list<CCharacter *> HitCharacters =
GameServer()->m_World.IntersectedCharacters(m_Pos, m_To, 0.0f, 0);
if(HitCharacters.empty())
return false;
for(auto *pChar : HitCharacters)
{
if(m_Layer == LAYER_SWITCH && m_Number > 0 && !Switchers()[m_Number].m_aStatus[pChar->Team()])
continue;
pChar->Freeze();
}
return true;
}
void CLight::Move()
{
if(m_Speed != 0)
{
if((m_CurveLength >= m_Length && m_Speed > 0) || (m_CurveLength <= 0 && m_Speed < 0))
m_Speed = -m_Speed;
m_CurveLength += m_Speed * m_Tick + m_LengthL;
m_LengthL = 0;
if(m_CurveLength > m_Length)
{
m_LengthL = m_CurveLength - m_Length;
m_CurveLength = m_Length;
}
else if(m_CurveLength < 0)
{
m_LengthL = 0 + m_CurveLength;
m_CurveLength = 0;
}
}
m_Rotation += m_AngularSpeed * m_Tick;
if(m_Rotation > pi * 2)
m_Rotation -= pi * 2;
else if(m_Rotation < 0)
m_Rotation += pi * 2;
}
void CLight::Step()
{
Move();
vec2 dir(std::sin(m_Rotation), std::cos(m_Rotation));
vec2 to2 = m_Pos + normalize(dir) * m_CurveLength;
GameServer()->Collision()->IntersectNoLaser(m_Pos, to2, &m_To, 0);
}
void CLight::Reset()
{
m_MarkedForDestroy = true;
}
void CLight::Tick()
{
if(Server()->Tick() % (int)(Server()->TickSpeed() * 0.15f) == 0)
{
int Flags;
m_EvalTick = Server()->Tick();
int index = GameServer()->Collision()->IsMover(m_Pos.x, m_Pos.y,
&Flags);
if(index)
{
m_Core = GameServer()->Collision()->CpSpeed(index, Flags);
}
m_Pos += m_Core;
Step();
}
HitCharacter();
}
void CLight::Snap(int SnappingClient)
{
if(NetworkClipped(SnappingClient, m_Pos) && NetworkClipped(SnappingClient, m_To))
return;
int SnappingClientVersion = GameServer()->GetClientVersion(SnappingClient);
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)));
CCharacter *pChr = GameServer()->GetPlayerChar(SnappingClient);
if(SnappingClient != SERVER_DEMO_CLIENT && (GameServer()->m_apPlayers[SnappingClient]->GetTeam() == TEAM_SPECTATORS || GameServer()->m_apPlayers[SnappingClient]->IsPaused()) && GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID != SPEC_FREEVIEW)
pChr = GameServer()->GetPlayerChar(GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID);
if(pEntData)
{
pEntData->m_SwitchNumber = m_Number;
pEntData->m_Layer = m_Layer;
pEntData->m_EntityClass = ENTITYCLASS_LIGHT;
}
else
{
int Tick = (Server()->Tick() % Server()->TickSpeed()) % 6;
if(pChr && pChr->IsAlive() && m_Layer == LAYER_SWITCH && m_Number > 0 && !Switchers()[m_Number].m_aStatus[pChr->Team()] && Tick)
return;
}
vec2 From = m_Pos;
int StartTick = 0;
if(pChr && pChr->Team() == TEAM_SUPER)
{
From = m_Pos;
}
else if(pChr && m_Layer == LAYER_SWITCH && Switchers()[m_Number].m_aStatus[pChr->Team()])
{
From = m_To;
}
else if(m_Layer != LAYER_SWITCH)
{
From = m_To;
}
if(!pEntData)
{
StartTick = m_EvalTick;
if(StartTick < Server()->Tick() - 4)
StartTick = Server()->Tick() - 4;
else if(StartTick > Server()->Tick())
StartTick = Server()->Tick();
}
GameServer()->SnapLaserObject(CSnapContext(SnappingClientVersion), GetID(),
m_Pos, From, StartTick, -1, LASERTYPE_FREEZE);
}