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

190 lines
5 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include <game/generated/protocol.h>
#include <game/server/gamecontext.h>
#include <engine/shared/config.h>
#include <game/server/teams.h>
2010-05-29 07:25:38 +00:00
#include "projectile.h"
CProjectile::CProjectile
(
CGameWorld *pGameWorld,
int Type,
int Owner,
vec2 Pos,
vec2 Dir,
int Span,
bool Freeze,
bool Explosive,
float Force,
int SoundImpact,
int Weapon,
int Layer,
int Number
)
: CEntity(pGameWorld, CGameWorld::ENTTYPE_PROJECTILE)
{
m_Layer = Layer;
m_Number = Number;
2010-05-29 07:25:38 +00:00
m_Type = Type;
m_Pos = Pos;
m_Direction = Dir;
m_LifeSpan = Span;
m_Owner = Owner;
m_Force = Force;
//m_Damage = Damage;
m_Freeze = Freeze;
2010-05-29 07:25:38 +00:00
m_SoundImpact = SoundImpact;
m_Weapon = Weapon;
m_StartTick = Server()->Tick();
m_Explosive = Explosive;
GameWorld()->InsertEntity(this);
}
2010-05-29 07:25:38 +00:00
void CProjectile::Reset()
{
if(m_LifeSpan > -2)
GameServer()->m_World.DestroyEntity(this);
}
2010-05-29 07:25:38 +00:00
vec2 CProjectile::GetPos(float Time)
{
2010-05-29 07:25:38 +00:00
float Curvature = 0;
float Speed = 0;
switch(m_Type)
{
2010-05-29 07:25:38 +00:00
case WEAPON_GRENADE:
Curvature = GameServer()->Tuning()->m_GrenadeCurvature;
Speed = GameServer()->Tuning()->m_GrenadeSpeed;
break;
case WEAPON_SHOTGUN:
Curvature = GameServer()->Tuning()->m_ShotgunCurvature;
Speed = GameServer()->Tuning()->m_ShotgunSpeed;
break;
case WEAPON_GUN:
Curvature = GameServer()->Tuning()->m_GunCurvature;
Speed = GameServer()->Tuning()->m_GunSpeed;
break;
}
2010-05-29 07:25:38 +00:00
return CalcPos(m_Pos, m_Direction, Curvature, Speed, Time);
}
void CProjectile::SetBouncing(int Value)
{
2010-08-14 10:46:54 +00:00
m_Bouncing = Value;
}
2010-05-29 07:25:38 +00:00
void CProjectile::Tick()
{
2010-05-29 07:25:38 +00:00
float Pt = (Server()->Tick()-m_StartTick-1)/(float)Server()->TickSpeed();
float Ct = (Server()->Tick()-m_StartTick)/(float)Server()->TickSpeed();
vec2 PrevPos = GetPos(Pt);
vec2 CurPos = GetPos(Ct);
vec2 ColPos;
vec2 NewPos;
vec2 Speed = CurPos - PrevPos;
int Collide = GameServer()->Collision()->IntersectLine(PrevPos, CurPos, &ColPos, &NewPos, false);
CCharacter *OwnerChar = 0;
if(m_Owner >= 0)
OwnerChar = GameServer()->GetPlayerChar(m_Owner);
2010-08-14 10:46:54 +00:00
CCharacter *TargetChr = GameServer()->m_World.IntersectCharacter(PrevPos, ColPos, (m_Freeze) ? 1.0f : 6.0f, ColPos, OwnerChar);
if(m_LifeSpan > -1)
m_LifeSpan--;
2010-09-22 19:01:09 +00:00
int TeamMask = -1;
bool isWeaponCollide = false;
if
(
OwnerChar &&
TargetChr &&
OwnerChar->m_Alive &&
TargetChr->m_Alive &&
!TargetChr->CanCollide(m_Owner)
)
{
2010-09-22 19:01:09 +00:00
isWeaponCollide = true;
2010-10-23 12:15:29 +00:00
//TeamMask = OwnerChar->Teams()->TeamMask( OwnerChar->Team());
2010-09-22 19:01:09 +00:00
}
2010-10-23 12:15:29 +00:00
if (OwnerChar &&
OwnerChar->m_Alive)
{
TeamMask = OwnerChar->Teams()->TeamMask( OwnerChar->Team());
}
if( ((TargetChr && (g_Config.m_SvHit || m_Owner == -1 || TargetChr == OwnerChar)) || Collide || GameLayerClipped(CurPos)) && !isWeaponCollide)//TODO:TEAM
{
if(m_Explosive/*??*/ && (!TargetChr || (TargetChr && !m_Freeze)))
{
2010-11-14 01:32:23 +00:00
GameServer()->CreateExplosion(ColPos, m_Owner, m_Weapon, m_Owner == -1, (!TargetChr ? -1 : TargetChr->Team()),
2010-09-22 19:01:09 +00:00
(m_Owner != -1)? TeamMask : -1);
GameServer()->CreateSound(ColPos, m_SoundImpact,
2010-09-22 19:01:09 +00:00
(m_Owner != -1)? TeamMask : -1);
}
else if(TargetChr && m_Freeze && ((m_Layer == LAYER_SWITCH && GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[TargetChr->Team()]) || m_Layer != LAYER_SWITCH))
TargetChr->Freeze();
if(Collide && m_Bouncing != 0)
{
m_StartTick = Server()->Tick();
2010-10-11 18:19:13 +00:00
m_Pos = NewPos+(-(m_Direction*4));
if (m_Bouncing == 1)
m_Direction.x = -m_Direction.x;
else if(m_Bouncing == 2)
m_Direction.y =- m_Direction.y;
m_Pos += m_Direction;
}
else if (m_Weapon == WEAPON_GUN)
{
GameServer()->CreateDamageInd(CurPos, -atan2(m_Direction.x, m_Direction.y), 10, (m_Owner != -1)? TeamMask : -1);
GameServer()->m_World.DestroyEntity(this);
}
else
if (!m_Freeze)
GameServer()->m_World.DestroyEntity(this);
}
if (m_LifeSpan == -1)
{
2010-05-29 07:25:38 +00:00
GameServer()->m_World.DestroyEntity(this);
}
}
2010-05-29 07:25:38 +00:00
void CProjectile::FillInfo(CNetObj_Projectile *pProj)
{
2010-05-29 07:25:38 +00:00
pProj->m_X = (int)m_Pos.x;
pProj->m_Y = (int)m_Pos.y;
pProj->m_VelX = (int)(m_Direction.x*100.0f);
pProj->m_VelY = (int)(m_Direction.y*100.0f);
pProj->m_StartTick = m_StartTick;
pProj->m_Type = m_Type;
}
2010-05-29 07:25:38 +00:00
void CProjectile::Snap(int SnappingClient)
{
2010-05-29 07:25:38 +00:00
float Ct = (Server()->Tick()-m_StartTick)/(float)Server()->TickSpeed();
2010-05-29 07:25:38 +00:00
if(NetworkClipped(SnappingClient, GetPos(Ct)))
return;
CCharacter * SnapChar = GameServer()->GetPlayerChar(SnappingClient);
2010-11-14 10:13:11 +00:00
int Tick = (Server()->Tick()%Server()->TickSpeed())%((m_Explosive)?6:20);
2010-11-14 15:51:04 +00:00
if (SnapChar && SnapChar->m_Alive && (m_Layer == LAYER_SWITCH && !GameServer()->Collision()->m_pSwitchers[m_Number].m_Status[SnapChar->Team()] && (!Tick))) return;
if
(
SnapChar &&
m_Owner != -1 &&
!SnapChar->CanCollide(m_Owner)
)
return;
2010-05-29 07:25:38 +00:00
CNetObj_Projectile *pProj = static_cast<CNetObj_Projectile *>(Server()->SnapNewItem(NETOBJTYPE_PROJECTILE, m_Id, sizeof(CNetObj_Projectile)));
if(pProj)
FillInfo(pProj);
}