2019-04-11 22:46:54 +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. */
|
|
|
|
#include "projectile.h"
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <game/generated/protocol.h>
|
2019-04-11 22:46:54 +00:00
|
|
|
|
|
|
|
#include <engine/shared/config.h>
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
CProjectile::CProjectile(
|
|
|
|
CGameWorld *pGameWorld,
|
|
|
|
int Type,
|
|
|
|
int Owner,
|
|
|
|
vec2 Pos,
|
|
|
|
vec2 Dir,
|
|
|
|
int Span,
|
|
|
|
bool Freeze,
|
|
|
|
bool Explosive,
|
|
|
|
float Force,
|
|
|
|
int SoundImpact,
|
|
|
|
int Layer,
|
|
|
|
int Number) :
|
|
|
|
CEntity(pGameWorld, CGameWorld::ENTTYPE_PROJECTILE)
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
|
|
|
m_Type = Type;
|
|
|
|
m_Pos = Pos;
|
|
|
|
m_Direction = Dir;
|
|
|
|
m_LifeSpan = Span;
|
|
|
|
m_Owner = Owner;
|
|
|
|
m_Force = Force;
|
|
|
|
m_SoundImpact = SoundImpact;
|
|
|
|
m_StartTick = GameWorld()->GameTick();
|
|
|
|
m_Explosive = Explosive;
|
|
|
|
|
|
|
|
m_Layer = Layer;
|
|
|
|
m_Number = Number;
|
|
|
|
m_Freeze = Freeze;
|
|
|
|
|
2019-09-15 22:07:42 +00:00
|
|
|
m_TuneZone = GameWorld()->m_WorldConfig.m_PredictTiles ? Collision()->IsTune(Collision()->GetMapIndex(m_Pos)) : 0;
|
2019-09-08 22:53:07 +00:00
|
|
|
|
2019-04-11 22:46:54 +00:00
|
|
|
GameWorld()->InsertEntity(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec2 CProjectile::GetPos(float Time)
|
|
|
|
{
|
|
|
|
float Curvature = 0;
|
|
|
|
float Speed = 0;
|
2019-09-15 22:07:42 +00:00
|
|
|
CTuningParams *pTuning = GetTuning(m_TuneZone);
|
2019-04-11 22:46:54 +00:00
|
|
|
|
|
|
|
switch(m_Type)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
case WEAPON_GRENADE:
|
|
|
|
Curvature = pTuning->m_GrenadeCurvature;
|
|
|
|
Speed = pTuning->m_GrenadeSpeed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WEAPON_SHOTGUN:
|
|
|
|
Curvature = pTuning->m_ShotgunCurvature;
|
|
|
|
Speed = pTuning->m_ShotgunSpeed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WEAPON_GUN:
|
|
|
|
Curvature = pTuning->m_GunCurvature;
|
|
|
|
Speed = pTuning->m_GunSpeed;
|
|
|
|
break;
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CalcPos(m_Pos, m_Direction, Curvature, Speed, Time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CProjectile::Tick()
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
float Pt = (GameWorld()->GameTick() - m_StartTick - 1) / (float)GameWorld()->GameTickSpeed();
|
|
|
|
float Ct = (GameWorld()->GameTick() - m_StartTick) / (float)GameWorld()->GameTickSpeed();
|
2019-04-11 22:46:54 +00:00
|
|
|
vec2 PrevPos = GetPos(Pt);
|
|
|
|
vec2 CurPos = GetPos(Ct);
|
|
|
|
vec2 ColPos;
|
|
|
|
vec2 NewPos;
|
|
|
|
int Collide = Collision()->IntersectLine(PrevPos, CurPos, &ColPos, &NewPos);
|
|
|
|
CCharacter *pOwnerChar = GameWorld()->GetCharacterByID(m_Owner);
|
|
|
|
|
|
|
|
CCharacter *pTargetChr = GameWorld()->IntersectCharacter(PrevPos, ColPos, m_Freeze ? 1.0f : 6.0f, ColPos, pOwnerChar, m_Owner);
|
|
|
|
|
2020-04-25 23:12:34 +00:00
|
|
|
if(GameWorld()->m_WorldConfig.m_IsSolo && !(m_Type == WEAPON_SHOTGUN && GameWorld()->m_WorldConfig.m_IsDDRace))
|
2019-05-11 19:13:09 +00:00
|
|
|
pTargetChr = 0;
|
|
|
|
|
2019-04-11 22:46:54 +00:00
|
|
|
if(m_LifeSpan > -1)
|
|
|
|
m_LifeSpan--;
|
|
|
|
|
2020-07-08 14:59:32 +00:00
|
|
|
int64 TeamMask = -1LL;
|
2019-04-11 22:46:54 +00:00
|
|
|
bool isWeaponCollide = false;
|
2020-09-26 19:41:58 +00:00
|
|
|
if(
|
|
|
|
pOwnerChar &&
|
|
|
|
pTargetChr &&
|
|
|
|
pOwnerChar->IsAlive() &&
|
|
|
|
pTargetChr->IsAlive() &&
|
|
|
|
!pTargetChr->CanCollide(m_Owner))
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
isWeaponCollide = true;
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(((pTargetChr && (pOwnerChar ? !(pOwnerChar->m_Hit & CCharacter::DISABLE_HIT_GRENADE) : g_Config.m_SvHit || m_Owner == -1 || pTargetChr == pOwnerChar)) || Collide || GameLayerClipped(CurPos)) && !isWeaponCollide)
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
2020-04-25 23:12:34 +00:00
|
|
|
if(m_Explosive && (!pTargetChr || (pTargetChr && (!m_Freeze || (m_Type == WEAPON_SHOTGUN && Collide)))))
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
2020-04-25 23:12:34 +00:00
|
|
|
GameWorld()->CreateExplosion(ColPos, m_Owner, m_Type, m_Owner == -1, (!pTargetChr ? -1 : pTargetChr->Team()),
|
2020-09-26 19:41:58 +00:00
|
|
|
(m_Owner != -1) ? TeamMask : -1LL);
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
else if(pTargetChr && m_Freeze && ((m_Layer == LAYER_SWITCH && Collision()->m_pSwitchers[m_Number].m_Status[pTargetChr->Team()]) || m_Layer != LAYER_SWITCH))
|
|
|
|
pTargetChr->Freeze();
|
|
|
|
if(Collide && m_Bouncing != 0)
|
|
|
|
{
|
|
|
|
m_StartTick = GameWorld()->GameTick();
|
2020-09-26 19:41:58 +00:00
|
|
|
m_Pos = NewPos + (-(m_Direction * 4));
|
|
|
|
if(m_Bouncing == 1)
|
2019-04-11 22:46:54 +00:00
|
|
|
m_Direction.x = -m_Direction.x;
|
|
|
|
else if(m_Bouncing == 2)
|
|
|
|
m_Direction.y = -m_Direction.y;
|
2020-09-26 19:41:58 +00:00
|
|
|
if(fabs(m_Direction.x) < 1e-6)
|
2019-04-11 22:46:54 +00:00
|
|
|
m_Direction.x = 0;
|
2020-09-26 19:41:58 +00:00
|
|
|
if(fabs(m_Direction.y) < 1e-6)
|
2019-04-11 22:46:54 +00:00
|
|
|
m_Direction.y = 0;
|
|
|
|
m_Pos += m_Direction;
|
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(m_Type == WEAPON_GUN)
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
|
|
|
GameWorld()->DestroyEntity(this);
|
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(!m_Freeze)
|
|
|
|
GameWorld()->DestroyEntity(this);
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
if(m_LifeSpan == -1)
|
|
|
|
{
|
|
|
|
if(m_Explosive)
|
|
|
|
{
|
|
|
|
if(m_Owner >= 0)
|
|
|
|
pOwnerChar = GameWorld()->GetCharacterByID(m_Owner);
|
|
|
|
|
2020-07-08 14:59:32 +00:00
|
|
|
int64 TeamMask = -1LL;
|
2019-04-11 22:46:54 +00:00
|
|
|
|
2020-04-25 23:12:34 +00:00
|
|
|
GameWorld()->CreateExplosion(ColPos, m_Owner, m_Type, m_Owner == -1, (!pOwnerChar ? -1 : pOwnerChar->Team()),
|
2020-09-26 19:41:58 +00:00
|
|
|
(m_Owner != -1) ? TeamMask : -1LL);
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
GameWorld()->DestroyEntity(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DDRace
|
|
|
|
|
|
|
|
void CProjectile::SetBouncing(int Value)
|
|
|
|
{
|
|
|
|
m_Bouncing = Value;
|
|
|
|
}
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
CProjectile::CProjectile(CGameWorld *pGameWorld, int ID, CNetObj_Projectile *pProj) :
|
|
|
|
CEntity(pGameWorld, CGameWorld::ENTTYPE_PROJECTILE)
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
2019-05-21 08:51:43 +00:00
|
|
|
ExtractInfo(pProj, &m_Pos, &m_Direction);
|
2019-04-11 22:46:54 +00:00
|
|
|
if(UseExtraInfo(pProj))
|
|
|
|
ExtractExtraInfo(pProj, &m_Owner, &m_Explosive, &m_Bouncing, &m_Freeze);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Owner = -1;
|
|
|
|
m_Bouncing = m_Freeze = 0;
|
|
|
|
m_Explosive = (pProj->m_Type == WEAPON_GRENADE) && (fabs(1.0f - length(m_Direction)) < 0.015f);
|
|
|
|
}
|
2020-05-02 22:00:27 +00:00
|
|
|
m_Type = pProj->m_Type;
|
2019-04-11 22:46:54 +00:00
|
|
|
m_StartTick = pProj->m_StartTick;
|
2019-09-15 22:07:42 +00:00
|
|
|
m_TuneZone = GameWorld()->m_WorldConfig.m_PredictTiles ? Collision()->IsTune(Collision()->GetMapIndex(m_Pos)) : 0;
|
2019-04-11 22:46:54 +00:00
|
|
|
|
|
|
|
int Lifetime = 20 * GameWorld()->GameTickSpeed();
|
|
|
|
m_SoundImpact = -1;
|
2020-04-25 23:12:34 +00:00
|
|
|
if(m_Type == WEAPON_GRENADE)
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
2019-09-15 22:07:42 +00:00
|
|
|
Lifetime = GetTuning(m_TuneZone)->m_GrenadeLifetime * GameWorld()->GameTickSpeed();
|
2019-04-11 22:46:54 +00:00
|
|
|
m_SoundImpact = SOUND_GRENADE_EXPLODE;
|
|
|
|
}
|
2020-04-25 23:12:34 +00:00
|
|
|
else if(m_Type == WEAPON_GUN)
|
2019-09-15 22:07:42 +00:00
|
|
|
Lifetime = GetTuning(m_TuneZone)->m_GunLifetime * GameWorld()->GameTickSpeed();
|
2020-04-25 23:12:34 +00:00
|
|
|
else if(m_Type == WEAPON_SHOTGUN && !GameWorld()->m_WorldConfig.m_IsDDRace)
|
2019-09-15 22:07:42 +00:00
|
|
|
Lifetime = GetTuning(m_TuneZone)->m_ShotgunLifetime * GameWorld()->GameTickSpeed();
|
2019-04-11 22:46:54 +00:00
|
|
|
m_LifeSpan = Lifetime - (pGameWorld->GameTick() - m_StartTick);
|
|
|
|
m_ID = ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CProjectile::FillInfo(CNetObj_Projectile *pProj)
|
|
|
|
{
|
|
|
|
pProj->m_X = (int)m_Pos.x;
|
|
|
|
pProj->m_Y = (int)m_Pos.y;
|
2020-09-26 19:41:58 +00:00
|
|
|
pProj->m_VelX = (int)(m_Direction.x * 100.0f);
|
|
|
|
pProj->m_VelY = (int)(m_Direction.y * 100.0f);
|
2019-04-11 22:46:54 +00:00
|
|
|
pProj->m_StartTick = m_StartTick;
|
|
|
|
pProj->m_Type = m_Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CProjectile::FillExtraInfo(CNetObj_Projectile *pProj)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
const int MaxPos = 0x7fffffff / 100;
|
|
|
|
if(abs((int)m_Pos.y) + 1 >= MaxPos || abs((int)m_Pos.x) + 1 >= MaxPos)
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
|
|
|
//If the modified data would be too large to fit in an integer, send normal data instead
|
|
|
|
FillInfo(pProj);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Send additional/modified info, by modifiying the fields of the netobj
|
|
|
|
float Angle = -atan2f(m_Direction.x, m_Direction.y);
|
|
|
|
|
|
|
|
int Data = 0;
|
2020-09-26 19:41:58 +00:00
|
|
|
Data |= (abs(m_Owner) & 255) << 0;
|
2019-04-11 22:46:54 +00:00
|
|
|
if(m_Owner < 0)
|
2020-09-26 19:41:58 +00:00
|
|
|
Data |= 1 << 8;
|
|
|
|
Data |= 1 << 9; //This bit tells the client to use the extra info
|
|
|
|
Data |= (m_Bouncing & 3) << 10;
|
2019-04-11 22:46:54 +00:00
|
|
|
if(m_Explosive)
|
2020-09-26 19:41:58 +00:00
|
|
|
Data |= 1 << 12;
|
2019-04-11 22:46:54 +00:00
|
|
|
if(m_Freeze)
|
2020-09-26 19:41:58 +00:00
|
|
|
Data |= 1 << 13;
|
2019-04-11 22:46:54 +00:00
|
|
|
|
|
|
|
pProj->m_X = (int)(m_Pos.x * 100.0f);
|
|
|
|
pProj->m_Y = (int)(m_Pos.y * 100.0f);
|
|
|
|
pProj->m_VelX = (int)(Angle * 1000000.0f);
|
|
|
|
pProj->m_VelY = Data;
|
|
|
|
pProj->m_StartTick = m_StartTick;
|
|
|
|
pProj->m_Type = m_Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CProjectile::Match(CProjectile *pProj)
|
|
|
|
{
|
2020-04-25 23:12:34 +00:00
|
|
|
if(pProj->m_Type != m_Type)
|
2019-04-11 22:46:54 +00:00
|
|
|
return false;
|
|
|
|
if(pProj->m_StartTick != m_StartTick)
|
|
|
|
return false;
|
|
|
|
if(distance(pProj->m_Pos, m_Pos) > 2.f)
|
|
|
|
return false;
|
|
|
|
if(distance(pProj->m_Direction, m_Direction) > 2.f)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|