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/config.h>
|
|
|
|
#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>
|
2011-12-25 13:00:47 +00:00
|
|
|
#include <game/server/gamemodes/DDRace.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-11-13 13:22:19 +00:00
|
|
|
m_Layer = Layer;
|
|
|
|
m_Number = Number;
|
2010-08-30 23:45:42 +00:00
|
|
|
m_Pos = Pos;
|
|
|
|
m_Strength = Strength;
|
|
|
|
m_EvalTick = Server()->Tick();
|
2022-05-02 18:31:17 +00:00
|
|
|
m_IgnoreWalls = IgnoreWalls;
|
2014-05-07 22:53:21 +00:00
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
// Create a list of players who are in the range of the dragger.
|
|
|
|
CCharacter *pPlayersInRange[MAX_CLIENTS];
|
|
|
|
mem_zero(pPlayersInRange, sizeof(pPlayersInRange));
|
|
|
|
|
|
|
|
int NumPlayersInRange = GameServer()->m_World.FindEntities(m_Pos, g_Config.m_SvDraggerRange,
|
|
|
|
(CEntity **)pPlayersInRange, MAX_CLIENTS, CGameWorld::ENTTYPE_CHARACTER);
|
|
|
|
|
|
|
|
// The closest player (within range) in a team is selected as the target
|
|
|
|
int TargetIdInTeam[MAX_CLIENTS];
|
|
|
|
bool IsTarget[MAX_CLIENTS];
|
|
|
|
int MinLenInTeam[MAX_CLIENTS];
|
|
|
|
mem_zero(MinLenInTeam, sizeof(MinLenInTeam));
|
|
|
|
mem_zero(IsTarget, sizeof(IsTarget));
|
|
|
|
for(int &TargetId : TargetIdInTeam)
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
// Do not create a dragger beam for super player
|
|
|
|
if(pTarget->Team() == 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-02 18:31:17 +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 && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[pTarget->Team()])
|
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-02 18:31:17 +00:00
|
|
|
// Dragger beams can be created only for reachable alive players
|
|
|
|
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-02 18:31:17 +00:00
|
|
|
// Solo players are dragged independently from the rest of the team
|
|
|
|
if(pTarget->Teams()->m_Core.GetSolo(pTarget->GetPlayer()->GetCID()))
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
IsTarget[pTarget->GetPlayer()->GetCID()] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int Len = length(pTarget->m_Pos - m_Pos);
|
|
|
|
if(MinLenInTeam[pTarget->Team()] == 0 || MinLenInTeam[pTarget->Team()] > Len)
|
|
|
|
{
|
|
|
|
MinLenInTeam[pTarget->Team()] = Len;
|
|
|
|
TargetIdInTeam[pTarget->Team()] = pTarget->GetPlayer()->GetCID();
|
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2014-05-07 22:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 18:31:17 +00:00
|
|
|
// Set the closest player for each team as a target
|
|
|
|
for(int &TargetId : TargetIdInTeam)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
if(TargetId != -1)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2022-05-02 18:31:17 +00:00
|
|
|
IsTarget[TargetId] = 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-02 18:31:17 +00:00
|
|
|
m_apDraggerBeam[i] = new CDraggerBeam(&GameServer()->m_World, this, m_Pos, m_Strength, m_IgnoreWalls, i);
|
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-02 18:31:17 +00:00
|
|
|
if(SnappingClientVersion < VERSION_DDNET_SWITCH)
|
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);
|
|
|
|
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)
|
|
|
|
pChar = GameServer()->GetPlayerChar(GameServer()->m_apPlayers[SnappingClient]->m_SpectatorID);
|
|
|
|
|
|
|
|
int Tick = (Server()->Tick() % Server()->TickSpeed()) % 11;
|
|
|
|
if(pChar && m_Layer == LAYER_SWITCH && m_Number && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[pChar->Team()] && (!Tick))
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|