ddnet/src/game/server/entities/dragger.cpp

243 lines
7.1 KiB
C++
Raw Normal View History

/* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */
#include <engine/server.h>
#include <engine/shared/config.h>
#include <game/generated/protocol.h>
#include <game/server/gamecontext.h>
#include <game/server/player.h>
#include <game/server/teams.h>
#include <game/version.h>
#include "character.h"
2022-05-02 18:31:17 +00:00
#include "dragger.h"
#include "dragger_beam.h"
2022-05-02 18:31:17 +00:00
CDragger::CDragger(CGameWorld *pGameWorld, vec2 Pos, float Strength, bool IgnoreWalls, int Layer, int Number) :
CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER)
{
m_Pos = Pos;
m_Strength = Strength;
2022-05-02 18:31:17 +00:00
m_IgnoreWalls = IgnoreWalls;
m_Layer = Layer;
m_Number = Number;
m_EvalTick = Server()->Tick();
for(auto &TargetId : m_TargetIdInTeam)
{
TargetId = -1;
}
2022-05-02 18:31:17 +00:00
mem_zero(m_apDraggerBeam, sizeof(m_apDraggerBeam));
GameWorld()->InsertEntity(this);
}
2022-05-02 18:31:17 +00:00
void CDragger::Tick()
{
2022-05-02 18:31:17 +00:00
if(Server()->Tick() % int(Server()->TickSpeed() * 0.15f) == 0)
{
2022-05-02 18:31:17 +00:00
int Flags;
m_EvalTick = Server()->Tick();
int index = GameServer()->Collision()->IsMover(m_Pos.x, m_Pos.y, &Flags);
if(index)
{
2022-05-02 18:31:17 +00:00
m_Core = GameServer()->Collision()->CpSpeed(index, Flags);
}
2022-05-02 18:31:17 +00:00
m_Pos += m_Core;
2014-05-08 13:42:20 +00:00
2022-05-02 18:31:17 +00:00
// Adopt the new position for all outgoing laser beams
for(auto &DraggerBeam : m_apDraggerBeam)
{
if(DraggerBeam != nullptr)
{
DraggerBeam->SetPos(m_Pos);
}
}
2021-11-06 11:52:14 +00:00
2022-05-02 18:31:17 +00:00
LookForPlayersToDrag();
2021-11-06 11:52:14 +00:00
}
2022-05-02 18:31:17 +00:00
}
2014-05-08 13:42:20 +00:00
2022-05-02 18:31:17 +00:00
void CDragger::LookForPlayersToDrag()
{
// Create a list of players who are in the range of the dragger
2022-05-02 18:31:17 +00:00
CCharacter *pPlayersInRange[MAX_CLIENTS];
mem_zero(pPlayersInRange, sizeof(pPlayersInRange));
2022-05-16 14:24:18 +00:00
int NumPlayersInRange = GameServer()->m_World.FindEntities(m_Pos,
g_Config.m_SvDraggerRange - CCharacterCore::PhysicalSize(),
2022-05-02 18:31:17 +00:00
(CEntity **)pPlayersInRange, MAX_CLIENTS, CGameWorld::ENTTYPE_CHARACTER);
// The closest player (within range) in a team is selected as the target
int ClosestTargetIdInTeam[MAX_CLIENTS];
bool CanStillBeTeamTarget[MAX_CLIENTS];
2022-05-02 18:31:17 +00:00
bool IsTarget[MAX_CLIENTS];
int MinDistInTeam[MAX_CLIENTS];
mem_zero(CanStillBeTeamTarget, sizeof(CanStillBeTeamTarget));
mem_zero(MinDistInTeam, sizeof(MinDistInTeam));
2022-05-02 18:31:17 +00:00
mem_zero(IsTarget, sizeof(IsTarget));
for(int &TargetId : ClosestTargetIdInTeam)
2022-05-02 18:31:17 +00:00
{
TargetId = -1;
}
2014-05-08 13:42:20 +00:00
2022-05-02 18:31:17 +00:00
for(int i = 0; i < NumPlayersInRange; i++)
{
2022-05-02 18:31:17 +00:00
CCharacter *pTarget = pPlayersInRange[i];
const int &TargetTeam = pTarget->Team();
2022-05-02 18:31:17 +00:00
// Do not create a dragger beam for super player
if(TargetTeam == TEAM_SUPER)
{
continue;
}
// If the dragger is disabled for the target's team, no dragger beam will be generated
if(m_Layer == LAYER_SWITCH && m_Number > 0 &&
2022-02-13 19:57:27 +00:00
!Switchers()[m_Number].m_Status[TargetTeam])
{
continue;
}
// Dragger beams can be created only for reachable, alive players
2022-05-02 18:31:17 +00:00
int IsReachable =
m_IgnoreWalls ?
!GameServer()->Collision()->IntersectNoLaserNW(m_Pos, pTarget->m_Pos, 0, 0) :
!GameServer()->Collision()->IntersectNoLaser(m_Pos, pTarget->m_Pos, 0, 0);
if(IsReachable && pTarget->IsAlive())
{
const int &TargetClientId = pTarget->GetPlayer()->GetCID();
2022-05-02 18:31:17 +00:00
// Solo players are dragged independently from the rest of the team
if(pTarget->Teams()->m_Core.GetSolo(TargetClientId))
{
IsTarget[TargetClientId] = true;
2022-05-02 18:31:17 +00:00
}
else
{
int Distance = distance(pTarget->m_Pos, m_Pos);
if(MinDistInTeam[TargetTeam] == 0 || MinDistInTeam[TargetTeam] > Distance)
2022-05-02 18:31:17 +00:00
{
MinDistInTeam[TargetTeam] = Distance;
ClosestTargetIdInTeam[TargetTeam] = TargetClientId;
2022-05-02 18:31:17 +00:00
}
CanStillBeTeamTarget[TargetClientId] = true;
}
}
}
// Set the closest player for each team as a target if the team does not have a target player yet
for(int i = 0; i < MAX_CLIENTS; i++)
{
if((m_TargetIdInTeam[i] != -1 && !CanStillBeTeamTarget[m_TargetIdInTeam[i]]) || m_TargetIdInTeam[i] == -1)
{
m_TargetIdInTeam[i] = ClosestTargetIdInTeam[i];
}
if(m_TargetIdInTeam[i] != -1)
{
IsTarget[m_TargetIdInTeam[i]] = true;
}
}
2022-05-02 18:31:17 +00:00
for(int i = 0; i < MAX_CLIENTS; i++)
2020-11-04 22:09:22 +00:00
{
2022-05-02 18:31:17 +00:00
// Create Dragger Beams which have not been created yet
if(IsTarget[i] && m_apDraggerBeam[i] == nullptr)
2020-11-04 22:09:22 +00:00
{
m_apDraggerBeam[i] = new CDraggerBeam(&GameServer()->m_World, this, m_Pos, m_Strength, m_IgnoreWalls, i, m_Layer, m_Number);
// The generated dragger beam is placed in the first position in the tick sequence and would therefore
// no longer be executed automatically in this tick. To execute the dragger beam nevertheless already
// this tick we call it manually (we do this to keep the old game logic)
m_apDraggerBeam[i]->Tick();
2020-11-04 22:09:22 +00:00
}
2022-05-02 18:31:17 +00:00
// Remove dragger beams that have not yet been deleted
else if(!IsTarget[i] && m_apDraggerBeam[i] != nullptr)
2020-11-04 22:09:22 +00:00
{
2022-05-02 18:31:17 +00:00
m_apDraggerBeam[i]->Reset();
}
}
}
2022-05-02 18:31:17 +00:00
void CDragger::RemoveDraggerBeam(int ClientID)
{
m_apDraggerBeam[ClientID] = nullptr;
}
void CDragger::Reset()
{
2021-05-22 18:02:00 +00:00
m_MarkedForDestroy = true;
}
2022-05-02 18:31:17 +00:00
void CDragger::Snap(int SnappingClient)
{
2022-05-02 18:31:17 +00:00
// Only players with the dragger in their field of view or who want to see everything will receive the snap
if(NetworkClipped(SnappingClient))
2011-12-25 13:00:47 +00:00
return;
2022-05-02 18:31:17 +00:00
// Send the dragger in its resting position if the player would not otherwise see a dragger beam
for(int i = 0; i < MAX_CLIENTS; i++)
{
2022-05-02 18:31:17 +00:00
if(m_apDraggerBeam[i] != nullptr)
{
2022-05-02 18:31:17 +00:00
CCharacter *pChar = GameServer()->GetPlayerChar(i);
if(pChar && pChar->CanSnapCharacter(SnappingClient))
{
return;
}
}
}
2021-11-18 23:42:00 +00:00
2022-05-02 18:31:17 +00:00
int SnappingClientVersion = SnappingClient != SERVER_DEMO_CLIENT ?
GameServer()->GetClientVersion(SnappingClient) :
CLIENT_VERSIONNR;
CNetObj_EntityEx *pEntData = 0;
if(SnappingClientVersion >= VERSION_DDNET_SWITCH)
{
2022-05-02 18:31:17 +00:00
pEntData = static_cast<CNetObj_EntityEx *>(Server()->SnapNewItem(NETOBJTYPE_ENTITYEX, GetID(),
sizeof(CNetObj_EntityEx)));
if(pEntData)
{
pEntData->m_SwitchNumber = m_Number;
pEntData->m_Layer = m_Layer;
2022-05-02 18:31:17 +00:00
pEntData->m_EntityClass = clamp(ENTITYCLASS_DRAGGER_WEAK + round_to_int(m_Strength) - 1,
(int)ENTITYCLASS_DRAGGER_WEAK, (int)ENTITYCLASS_DRAGGER_STRONG);
}
}
else
{
2022-05-02 18:31:17 +00:00
// Emulate turned off blinking dragger for old clients
CCharacter *pChar = GameServer()->GetPlayerChar(SnappingClient);
if(SnappingClient != SERVER_DEMO_CLIENT &&
(GameServer()->m_apPlayers[SnappingClient]->GetTeam() == TEAM_SPECTATORS ||
GameServer()->m_apPlayers[SnappingClient]->IsPaused()) &&
2022-05-02 18:31:17 +00:00
GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID != SPEC_FREEVIEW)
pChar = GameServer()->GetPlayerChar(GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID);
int Tick = (Server()->Tick() % Server()->TickSpeed()) % 11;
if(pChar && m_Layer == LAYER_SWITCH && m_Number > 0 &&
2022-02-13 19:57:27 +00:00
!Switchers()[m_Number].m_Status[pChar->Team()] && !Tick)
2022-05-02 18:31:17 +00:00
return;
}
2014-01-13 16:00:49 +00:00
2022-05-02 18:31:17 +00:00
CNetObj_Laser *obj = static_cast<CNetObj_Laser *>(Server()->SnapNewItem(
NETOBJTYPE_LASER, GetID(), sizeof(CNetObj_Laser)));
2022-05-02 18:31:17 +00:00
if(!obj)
return;
2022-05-02 18:31:17 +00:00
obj->m_X = (int)m_Pos.x;
obj->m_Y = (int)m_Pos.y;
obj->m_FromX = (int)m_Pos.x;
obj->m_FromY = (int)m_Pos.y;
2022-05-02 18:31:17 +00:00
if(pEntData)
{
obj->m_StartTick = 0;
}
2022-05-02 18:31:17 +00:00
else
{
2022-05-02 18:31:17 +00:00
int StartTick = m_EvalTick;
if(StartTick < Server()->Tick() - 4)
StartTick = Server()->Tick() - 4;
else if(StartTick > Server()->Tick())
StartTick = Server()->Tick();
obj->m_StartTick = StartTick;
2010-09-22 10:43:59 +00:00
}
}