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

138 lines
3.8 KiB
C++
Raw Normal View History

/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
#include <engine/server.h>
#include <engine/shared/config.h>
#include <game/generated/protocol.h>
#include <game/server/gamecontext.h>
#include <game/server/teams.h>
#include "gun.h"
#include "plasma.h"
//////////////////////////////////////////////////
// CGun
//////////////////////////////////////////////////
CGun::CGun(CGameWorld *pGameWorld, vec2 Pos, bool Freeze, bool Explosive, int Layer, int Number)
: CEntity(pGameWorld, CGameWorld::ENTTYPE_LASER)
{
m_Layer = Layer;
m_Number = Number;
m_LastFire = Server()->Tick();
m_Pos = Pos;
m_EvalTick = Server()->Tick();
m_Freeze = Freeze;
m_Explosive = Explosive;
2015-07-09 00:08:14 +00:00
GameWorld()->InsertEntity(this);
}
void CGun::Fire()
{
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];
for (int i = 0; i < MAX_CLIENTS; i++)
{
IdInTeam[i] = -1;
LenInTeam[i] = 0;
}
2015-07-09 00:08:14 +00:00
int Num = -1;
2014-01-02 00:56:09 +00:00
Num = GameServer()->m_World.FindEntities(m_Pos, g_Config.m_SvPlasmaRange, (CEntity**)Ents, MAX_CLIENTS, CGameWorld::ENTTYPE_CHARACTER);
for (int i = 0; i < Num; i++)
{
CCharacter *Target = Ents[i];
//now gun doesn't affect on super
if(Target->Team() == TEAM_SUPER)
continue;
if(m_Layer == LAYER_SWITCH && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[Target->Team()])
continue;
2015-11-08 09:20:44 +00:00
int res = GameServer()->Collision()->IntersectLine(m_Pos, Target->m_Pos,0,0);
if (!res)
{
int Len = length(Target->m_Pos - m_Pos);
if (LenInTeam[Target->Team()] == 0 || LenInTeam[Target->Team()] > Len)
{
LenInTeam[Target->Team()] = Len;
IdInTeam[Target->Team()] = i;
}
}
}
2014-01-01 21:34:10 +00:00
for (int i = 0; i < MAX_CLIENTS; i++)
{
if(IdInTeam[i] != -1)
{
CCharacter *Target = Ents[IdInTeam[i]];
new CPlasma(&GameServer()->m_World, m_Pos, normalize(Target->m_Pos - m_Pos), m_Freeze, m_Explosive, i);
m_LastFire = Server()->Tick();
}
}
for (int i = 0; i < Num; i++)
{
CCharacter *Target = Ents[i];
2014-09-22 16:56:59 +00:00
if (Target->IsAlive() && Target->Teams()->m_Core.GetSolo(Target->GetPlayer()->GetCID()))
{
if (IdInTeam[Target->Team()] != i)
{
2015-11-08 09:20:44 +00:00
int res = GameServer()->Collision()->IntersectLine(m_Pos, Target->m_Pos,0,0);
if (!res)
{
new CPlasma(&GameServer()->m_World, m_Pos, normalize(Target->m_Pos - m_Pos), m_Freeze, m_Explosive, Target->Team());
m_LastFire = Server()->Tick();
}
}
}
}
2015-07-09 00:08:14 +00:00
}
2015-07-09 00:08:14 +00:00
void CGun::Reset()
{
GameServer()->m_World.DestroyEntity(this);
}
void CGun::Tick()
{
if (Server()->Tick()%int(Server()->TickSpeed()*0.15f)==0)
{
2010-11-01 01:51:17 +00:00
int Flags;
m_EvalTick=Server()->Tick();
int index = GameServer()->Collision()->IsMover(m_Pos.x,m_Pos.y, &Flags);
2010-11-01 01:51:17 +00:00
if (index)
{
2010-11-01 01:51:17 +00:00
m_Core=GameServer()->Collision()->CpSpeed(index, Flags);
}
m_Pos+=m_Core;
}
if (m_LastFire + Server()->TickSpeed() / g_Config.m_SvPlasmaPerSec <= Server()->Tick())
Fire();
}
void CGun::Snap(int SnappingClient)
2015-07-09 00:08:14 +00:00
{
if(NetworkClipped(SnappingClient))
return;
2014-01-13 16:00:49 +00:00
CCharacter *Char = GameServer()->GetPlayerChar(SnappingClient);
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);
int Tick = (Server()->Tick()%Server()->TickSpeed())%11;
2014-01-13 16:00:49 +00:00
if (Char && Char->IsAlive() && (m_Layer == LAYER_SWITCH && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[Char->Team()]) && (!Tick)) return;
CNetObj_Laser *pObj = static_cast<CNetObj_Laser *>(Server()->SnapNewItem(NETOBJTYPE_LASER, m_ID, sizeof(CNetObj_Laser)));
2014-01-13 16:00:49 +00:00
if (!pObj)
return;
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;
pObj->m_StartTick = m_EvalTick;
}