2010-07-29 05:21:18 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
|
|
|
#include <engine/server.h>
|
2011-04-20 13:47:55 +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>
|
2014-05-07 22:53:21 +00:00
|
|
|
#include <game/server/teams.h>
|
2021-10-14 20:59:39 +00:00
|
|
|
#include <game/version.h>
|
2014-05-07 22:53:21 +00:00
|
|
|
|
2021-01-09 14:18:52 +00:00
|
|
|
#include "character.h"
|
2010-07-29 05:21:18 +00:00
|
|
|
#include "gun.h"
|
|
|
|
#include "plasma.h"
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// CGun
|
|
|
|
//////////////////////////////////////////////////
|
2020-09-26 19:41:58 +00:00
|
|
|
CGun::CGun(CGameWorld *pGameWorld, vec2 Pos, bool Freeze, bool Explosive, 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-04-20 13:47:55 +00:00
|
|
|
m_LastFire = Server()->Tick();
|
2010-08-28 20:32:16 +00:00
|
|
|
m_Pos = Pos;
|
|
|
|
m_EvalTick = Server()->Tick();
|
|
|
|
m_Freeze = Freeze;
|
|
|
|
m_Explosive = Explosive;
|
2015-07-09 00:08:14 +00:00
|
|
|
|
2010-07-29 05:21:18 +00:00
|
|
|
GameWorld()->InsertEntity(this);
|
|
|
|
}
|
|
|
|
|
2010-08-28 20:32:16 +00:00
|
|
|
void CGun::Fire()
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2014-01-01 21:34:10 +00:00
|
|
|
CCharacter *Ents[MAX_CLIENTS];
|
2015-07-09 00:08:14 +00:00
|
|
|
int IdInTeam[MAX_CLIENTS];
|
2014-01-01 21:34:10 +00:00
|
|
|
int LenInTeam[MAX_CLIENTS];
|
2020-09-26 19:41:58 +00:00
|
|
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
2011-04-20 13:47:55 +00:00
|
|
|
{
|
2010-08-31 17:14:33 +00:00
|
|
|
IdInTeam[i] = -1;
|
|
|
|
LenInTeam[i] = 0;
|
|
|
|
}
|
2015-07-09 00:08:14 +00:00
|
|
|
|
2010-08-28 20:32:16 +00:00
|
|
|
int Num = -1;
|
2020-09-26 19:41:58 +00:00
|
|
|
Num = GameServer()->m_World.FindEntities(m_Pos, g_Config.m_SvPlasmaRange, (CEntity **)Ents, MAX_CLIENTS, CGameWorld::ENTTYPE_CHARACTER);
|
2010-08-31 17:14:33 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
for(int i = 0; i < Num; i++)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-08-28 20:32:16 +00:00
|
|
|
CCharacter *Target = Ents[i];
|
2010-10-23 18:11:42 +00:00
|
|
|
//now gun doesn't affect on super
|
2011-04-20 13:47:55 +00:00
|
|
|
if(Target->Team() == TEAM_SUPER)
|
|
|
|
continue;
|
2021-01-20 19:00:16 +00:00
|
|
|
if(m_Layer == LAYER_SWITCH && m_Number > 0 && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[Target->Team()])
|
2011-04-20 13:47:55 +00:00
|
|
|
continue;
|
2020-09-26 19:41:58 +00:00
|
|
|
int res = GameServer()->Collision()->IntersectLine(m_Pos, Target->m_Pos, 0, 0);
|
|
|
|
if(!res)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-09-08 16:22:11 +00:00
|
|
|
int Len = length(Target->m_Pos - m_Pos);
|
2020-09-26 19:41:58 +00:00
|
|
|
if(LenInTeam[Target->Team()] == 0 || LenInTeam[Target->Team()] > Len)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2010-08-31 17:14:33 +00:00
|
|
|
LenInTeam[Target->Team()] = Len;
|
|
|
|
IdInTeam[Target->Team()] = i;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
for(int i = 0; i < MAX_CLIENTS; i++)
|
2011-04-20 13:47:55 +00:00
|
|
|
{
|
|
|
|
if(IdInTeam[i] != -1)
|
|
|
|
{
|
2010-08-31 17:14:33 +00:00
|
|
|
CCharacter *Target = Ents[IdInTeam[i]];
|
2010-09-08 16:22:11 +00:00
|
|
|
new CPlasma(&GameServer()->m_World, m_Pos, normalize(Target->m_Pos - m_Pos), m_Freeze, m_Explosive, i);
|
2011-04-20 13:47:55 +00:00
|
|
|
m_LastFire = Server()->Tick();
|
2010-08-31 17:14:33 +00:00
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
for(int i = 0; i < Num; i++)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
|
|
|
CCharacter *Target = Ents[i];
|
2020-09-26 19:41:58 +00:00
|
|
|
if(Target->IsAlive() && Target->Teams()->m_Core.GetSolo(Target->GetPlayer()->GetCID()))
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(IdInTeam[Target->Team()] != i)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
int res = GameServer()->Collision()->IntersectLine(m_Pos, Target->m_Pos, 0, 0);
|
|
|
|
if(!res)
|
2014-05-07 22:53:21 +00:00
|
|
|
{
|
|
|
|
new CPlasma(&GameServer()->m_World, m_Pos, normalize(Target->m_Pos - m_Pos), m_Freeze, m_Explosive, Target->Team());
|
|
|
|
m_LastFire = Server()->Tick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2015-07-09 00:08:14 +00:00
|
|
|
|
2010-07-29 05:21:18 +00:00
|
|
|
void CGun::Reset()
|
|
|
|
{
|
2021-05-22 18:02:00 +00:00
|
|
|
m_MarkedForDestroy = true;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGun::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;
|
2020-09-26 19:41:58 +00:00
|
|
|
m_EvalTick = Server()->Tick();
|
|
|
|
int index = GameServer()->Collision()->IsMover(m_Pos.x, m_Pos.y, &Flags);
|
|
|
|
if(index)
|
2010-07-29 05:21:18 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
m_Core = GameServer()->Collision()->CpSpeed(index, Flags);
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
m_Pos += m_Core;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
2021-11-03 16:21:51 +00:00
|
|
|
if(g_Config.m_SvPlasmaPerSec > 0 && m_LastFire + Server()->TickSpeed() / g_Config.m_SvPlasmaPerSec <= Server()->Tick())
|
2011-04-20 13:47:55 +00:00
|
|
|
Fire();
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 10:01:42 +00:00
|
|
|
void CGun::Snap(int SnappingClient)
|
2015-07-09 00:08:14 +00:00
|
|
|
{
|
2021-10-20 23:02:44 +00:00
|
|
|
if(NetworkClipped(SnappingClient))
|
|
|
|
return;
|
|
|
|
|
2021-10-14 20:59:39 +00:00
|
|
|
CNetObj_EntityEx *pEntData = static_cast<CNetObj_EntityEx *>(Server()->SnapNewItem(NETOBJTYPE_ENTITYEX, GetID(), sizeof(CNetObj_EntityEx)));
|
|
|
|
if(!pEntData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pEntData->m_SwitchNumber = m_Number;
|
|
|
|
pEntData->m_Layer = m_Layer;
|
|
|
|
|
|
|
|
if(m_Explosive && !m_Freeze)
|
|
|
|
pEntData->m_EntityClass = ENTITYCLASS_GUN_NORMAL;
|
|
|
|
else if(m_Explosive && m_Freeze)
|
|
|
|
pEntData->m_EntityClass = ENTITYCLASS_GUN_EXPLOSIVE;
|
|
|
|
else if(!m_Explosive && m_Freeze)
|
|
|
|
pEntData->m_EntityClass = ENTITYCLASS_GUN_FREEZE;
|
|
|
|
else
|
|
|
|
pEntData->m_EntityClass = ENTITYCLASS_GUN_UNFREEZE;
|
|
|
|
|
2014-01-13 16:00:49 +00:00
|
|
|
CCharacter *Char = GameServer()->GetPlayerChar(SnappingClient);
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(SnappingClient > -1 && (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-10-14 20:59:39 +00:00
|
|
|
int SnappingClientVersion = SnappingClient >= 0 ? GameServer()->GetClientVersion(SnappingClient) : CLIENT_VERSIONNR;
|
|
|
|
if(SnappingClientVersion < VERSION_DDNET_SWITCH)
|
|
|
|
{
|
|
|
|
int Tick = (Server()->Tick() % Server()->TickSpeed()) % 11;
|
|
|
|
if(Char && Char->IsAlive() && (m_Layer == LAYER_SWITCH && m_Number > 0 && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[Char->Team()]) && (!Tick))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:40:17 +00:00
|
|
|
CNetObj_Laser *pObj = static_cast<CNetObj_Laser *>(Server()->SnapNewItem(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;
|
|
|
|
|
2010-07-29 05:21:18 +00:00
|
|
|
pObj->m_X = (int)m_Pos.x;
|
|
|
|
pObj->m_Y = (int)m_Pos.y;
|
|
|
|
pObj->m_FromX = (int)m_Pos.x;
|
|
|
|
pObj->m_FromY = (int)m_Pos.y;
|
2010-08-28 20:32:16 +00:00
|
|
|
pObj->m_StartTick = m_EvalTick;
|
2010-07-29 05:21:18 +00:00
|
|
|
}
|