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"
|
2021-01-17 16:18:08 +00:00
|
|
|
#include <game/client/projectile_data.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;
|
|
|
|
|
2021-04-23 03:01:38 +00:00
|
|
|
m_TuneZone = GameWorld()->m_WorldConfig.m_UseTuneZones ? 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--;
|
|
|
|
|
2021-06-23 05:05:49 +00:00
|
|
|
int64_t 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
|
|
|
}
|
2021-09-19 12:14:00 +00:00
|
|
|
else if(m_Freeze)
|
|
|
|
{
|
|
|
|
CCharacter *apEnts[MAX_CLIENTS];
|
|
|
|
int Num = GameWorld()->FindEntities(CurPos, 1.0f, (CEntity **)apEnts, MAX_CLIENTS, CGameWorld::ENTTYPE_CHARACTER);
|
|
|
|
for(int i = 0; i < Num; ++i)
|
|
|
|
if(apEnts[i] && (m_Layer != LAYER_SWITCH || (m_Layer == LAYER_SWITCH && m_Number > 0 && GameWorld()->Collision()->m_pSwitchers[m_Number].m_Status[apEnts[i]->Team()])))
|
|
|
|
apEnts[i]->Freeze();
|
|
|
|
}
|
2019-04-11 22:46:54 +00:00
|
|
|
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
|
|
|
{
|
2021-05-22 18:02:00 +00:00
|
|
|
m_MarkedForDestroy = true;
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
else if(!m_Freeze)
|
2021-05-22 18:02:00 +00:00
|
|
|
m_MarkedForDestroy = true;
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
if(m_LifeSpan == -1)
|
|
|
|
{
|
|
|
|
if(m_Explosive)
|
|
|
|
{
|
|
|
|
if(m_Owner >= 0)
|
|
|
|
pOwnerChar = GameWorld()->GetCharacterByID(m_Owner);
|
|
|
|
|
2021-06-23 05:05:49 +00:00
|
|
|
int64_t 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
|
|
|
}
|
2021-05-22 18:02:00 +00:00
|
|
|
m_MarkedForDestroy = true;
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DDRace
|
|
|
|
|
|
|
|
void CProjectile::SetBouncing(int Value)
|
|
|
|
{
|
|
|
|
m_Bouncing = Value;
|
|
|
|
}
|
|
|
|
|
2021-09-19 12:14:00 +00:00
|
|
|
CProjectile::CProjectile(CGameWorld *pGameWorld, int ID, CProjectileData *pProj, const CNetObj_EntityEx *pEntEx) :
|
2020-09-26 19:41:58 +00:00
|
|
|
CEntity(pGameWorld, CGameWorld::ENTTYPE_PROJECTILE)
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
2021-01-17 16:18:08 +00:00
|
|
|
m_Pos = pProj->m_StartPos;
|
|
|
|
m_Direction = pProj->m_StartVel;
|
|
|
|
if(pProj->m_ExtraInfo)
|
|
|
|
{
|
|
|
|
m_Owner = pProj->m_Owner;
|
|
|
|
m_Explosive = pProj->m_Explosive;
|
|
|
|
m_Bouncing = pProj->m_Bouncing;
|
|
|
|
m_Freeze = pProj->m_Freeze;
|
|
|
|
}
|
2019-04-11 22:46:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Owner = -1;
|
2021-01-17 16:18:08 +00:00
|
|
|
m_Bouncing = 0;
|
|
|
|
m_Freeze = 0;
|
2019-04-11 22:46:54 +00:00
|
|
|
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;
|
2021-04-23 03:01:38 +00:00
|
|
|
m_TuneZone = pProj->m_TuneZone;
|
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;
|
2021-06-27 22:29:42 +00:00
|
|
|
m_Layer = LAYER_GAME;
|
|
|
|
m_Number = 0;
|
2021-09-19 12:14:00 +00:00
|
|
|
|
|
|
|
if(pEntEx)
|
|
|
|
{
|
|
|
|
m_Layer = LAYER_SWITCH;
|
|
|
|
m_Number = pEntEx->m_SwitchNumber;
|
|
|
|
}
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 16:18:08 +00:00
|
|
|
CProjectileData CProjectile::GetData() const
|
2019-04-11 22:46:54 +00:00
|
|
|
{
|
2021-01-17 16:18:08 +00:00
|
|
|
CProjectileData Result;
|
|
|
|
Result.m_StartPos = m_Pos;
|
|
|
|
Result.m_StartVel = m_Direction;
|
|
|
|
Result.m_Type = m_Type;
|
|
|
|
Result.m_StartTick = m_StartTick;
|
|
|
|
Result.m_ExtraInfo = true;
|
|
|
|
Result.m_Owner = m_Owner;
|
|
|
|
Result.m_Explosive = m_Explosive;
|
|
|
|
Result.m_Bouncing = m_Bouncing;
|
|
|
|
Result.m_Freeze = m_Freeze;
|
2021-04-23 03:01:38 +00:00
|
|
|
Result.m_TuneZone = m_TuneZone;
|
2021-01-17 16:18:08 +00:00
|
|
|
return Result;
|
2019-04-11 22:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|