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. */
|
2010-07-29 05:21:18 +00:00
|
|
|
#include <engine/server.h>
|
2015-09-01 21:15:48 +00:00
|
|
|
#include <engine/shared/config.h>
|
2010-07-29 05:21:18 +00:00
|
|
|
#include <game/generated/protocol.h>
|
|
|
|
#include <game/server/gamecontext.h>
|
2021-01-09 14:37:02 +00:00
|
|
|
#include <game/server/player.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <game/server/teams.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"
|
2022-05-02 18:31:17 +00:00
|
|
|
#include "dragger.h"
|
|
|
|
#include "dragger_beam.h"
|
2021-01-09 14:18:52 +00:00
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
CDragger::CDragger(CGameWorld *pGameWorld, vec2 Pos, float Strength, bool IgnoreWalls, int Layer, int Number) :
|
2020-09-26 19:41:58 +00:00
|
|
|
CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-08-30 23:45:42 +00:00
|
|
|
m_Pos = Pos;
|
|
|
|
m_Strength = Strength;
|
2022-05-02 18:31:17 +00:00
|
|
|
m_IgnoreWalls = IgnoreWalls;
|
2022-05-04 17:33:48 +00:00
|
|
|
m_Layer = Layer;
|
|
|
|
m_Number = Number;
|
|
|
|
m_EvalTick = Server()->Tick();
|
2014-05-07 22:53:21 +00:00
|
|
|
|
2022-05-09 11:56:06 +00:00
|
|
|
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);
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
void CDragger::Tick()
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
if(Server()->Tick() % int(Server()->TickSpeed() * 0.15f) == 0)
|
2021-11-02 16:09:25 +00:00
|
|
|
{
|
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)
|
2021-11-02 16:09:25 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
m_Core = GameServer()->Collision()->CpSpeed(index, Flags);
|
2021-11-02 16:09:25 +00:00
|
|
|
}
|
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()
|
|
|
|
{
|
2022-05-04 17:33:48 +00:00
|
|
|
// 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,
|
2022-05-28 17:28:44 +00:00
|
|
|
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
|
2022-05-09 11:56:06 +00:00
|
|
|
int ClosestTargetIdInTeam[MAX_CLIENTS];
|
|
|
|
bool CanStillBeTeamTarget[MAX_CLIENTS];
|
2022-05-02 18:31:17 +00:00
|
|
|
bool IsTarget[MAX_CLIENTS];
|
2022-05-04 17:33:48 +00:00
|
|
|
int MinDistInTeam[MAX_CLIENTS];
|
2022-05-09 11:56:06 +00:00
|
|
|
mem_zero(CanStillBeTeamTarget, sizeof(CanStillBeTeamTarget));
|
2022-05-04 17:33:48 +00:00
|
|
|
mem_zero(MinDistInTeam, sizeof(MinDistInTeam));
|
2022-05-02 18:31:17 +00:00
|
|
|
mem_zero(IsTarget, sizeof(IsTarget));
|
2022-05-09 11:56:06 +00:00
|
|
|
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++)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
CCharacter *pTarget = pPlayersInRange[i];
|
2022-05-09 11:56:06 +00:00
|
|
|
const int &TargetTeam = pTarget->Team();
|
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
// Do not create a dragger beam for super player
|
2022-05-09 11:56:06 +00:00
|
|
|
if(TargetTeam == TEAM_SUPER)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2011-12-25 13:51:04 +00:00
|
|
|
continue;
|
2014-05-07 22:53:21 +00:00
|
|
|
}
|
2022-05-04 17:33:48 +00:00
|
|
|
// 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])
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2011-12-25 13:51:04 +00:00
|
|
|
continue;
|
2014-05-07 22:53:21 +00:00
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
|
2022-05-04 17:33:48 +00:00
|
|
|
// 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())
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-09 11:56:06 +00:00
|
|
|
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
|
2022-05-09 11:56:06 +00:00
|
|
|
if(pTarget->Teams()->m_Core.GetSolo(TargetClientId))
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-09 11:56:06 +00:00
|
|
|
IsTarget[TargetClientId] = true;
|
2022-05-02 18:31:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-04 17:33:48 +00:00
|
|
|
int Distance = distance(pTarget->m_Pos, m_Pos);
|
2022-05-09 11:56:06 +00:00
|
|
|
if(MinDistInTeam[TargetTeam] == 0 || MinDistInTeam[TargetTeam] > Distance)
|
2022-05-02 18:31:17 +00:00
|
|
|
{
|
2022-05-09 11:56:06 +00:00
|
|
|
MinDistInTeam[TargetTeam] = Distance;
|
|
|
|
ClosestTargetIdInTeam[TargetTeam] = TargetClientId;
|
2022-05-02 18:31:17 +00:00
|
|
|
}
|
2022-05-09 11:56:06 +00:00
|
|
|
CanStillBeTeamTarget[TargetClientId] = true;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2014-05-07 22:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 11:56:06 +00:00
|
|
|
// 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++)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2022-05-09 11:56:06 +00:00
|
|
|
if((m_TargetIdInTeam[i] != -1 && !CanStillBeTeamTarget[m_TargetIdInTeam[i]]) || m_TargetIdInTeam[i] == -1)
|
|
|
|
{
|
|
|
|
m_TargetIdInTeam[i] = ClosestTargetIdInTeam[i];
|
|
|
|
}
|
|
|
|
if(m_TargetIdInTeam[i] != -1)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2022-05-09 11:56:06 +00:00
|
|
|
IsTarget[m_TargetIdInTeam[i]] = true;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-05-26 17:33:01 +00:00
|
|
|
m_apDraggerBeam[i] = new CDraggerBeam(&GameServer()->m_World, this, m_Pos, m_Strength, m_IgnoreWalls, i, m_Layer, m_Number);
|
2022-05-28 15:27:10 +00:00
|
|
|
// 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();
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
void CDragger::RemoveDraggerBeam(int ClientID)
|
|
|
|
{
|
|
|
|
m_apDraggerBeam[ClientID] = nullptr;
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:32:42 +00:00
|
|
|
void CDragger::Reset()
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2021-05-22 18:02:00 +00:00
|
|
|
m_MarkedForDestroy = true;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
void CDragger::Snap(int SnappingClient)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
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++)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
if(m_apDraggerBeam[i] != nullptr)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
CCharacter *pChar = GameServer()->GetPlayerChar(i);
|
|
|
|
if(pChar && pChar->CanSnapCharacter(SnappingClient))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
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;
|
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)
|
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
pEntData = static_cast<CNetObj_EntityEx *>(Server()->SnapNewItem(NETOBJTYPE_ENTITYEX, GetID(),
|
|
|
|
sizeof(CNetObj_EntityEx)));
|
2021-11-06 11:13:42 +00:00
|
|
|
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);
|
2021-11-06 11:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-04 17:33:48 +00:00
|
|
|
else
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
// Emulate turned off blinking dragger for old clients
|
|
|
|
CCharacter *pChar = GameServer()->GetPlayerChar(SnappingClient);
|
2022-05-04 17:33:48 +00:00
|
|
|
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;
|
2022-05-04 17:33:48 +00:00
|
|
|
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-05-07 22:53:21 +00:00
|
|
|
}
|
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)));
|
2014-05-07 22:53:21 +00:00
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
if(!obj)
|
|
|
|
return;
|
2010-07-29 05:21:18 +00:00
|
|
|
|
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;
|
2014-05-07 22:53:21 +00:00
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
if(pEntData)
|
|
|
|
{
|
|
|
|
obj->m_StartTick = 0;
|
2014-05-07 22:53:21 +00:00
|
|
|
}
|
2022-05-02 18:31:17 +00:00
|
|
|
else
|
2011-12-25 13:51:04 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|