Skip to content

Commit

Permalink
Character: Incapsulate m_MoveRestrictions
Browse files Browse the repository at this point in the history
Instead of leaking the m_MoveRestrictions to all classes which needs to
adjust the Character velocity, add a setter which takes into account those
restrictions.

The shotgun bug replication requires an access to the Velocity without the
restrictions applied (so we have to have this dirty setter).
  • Loading branch information
Kaffeine committed Jan 21, 2024
1 parent 432b431 commit 6520eb0
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 35 deletions.
27 changes: 27 additions & 0 deletions src/game/client/prediction/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,33 @@ void CCharacter::GiveAllWeapons()
}
}

void CCharacter::ResetVelocity()
{
Core()->m_Vel = vec2(0, 0);
}

// The method is needed only to reproduce 'shotgun bug' ddnet#5258
// Use SetVelocity() instead.
void CCharacter::SetVelocity(const vec2 NewVelocity)
{
Core()->m_Vel = ClampVel(m_MoveRestrictions, NewVelocity);
}

void CCharacter::SetRawVelocity(const vec2 NewVelocity)
{
Core()->m_Vel = NewVelocity;
}

void CCharacter::AddVelocity(const vec2 Addition)
{
SetVelocity(Core()->m_Vel + Addition);
}

void CCharacter::ApplyMoveRestrictions()
{
m_Core.m_Vel = ClampVel(m_MoveRestrictions, m_Core.m_Vel);
}

CTeamsCore *CCharacter::TeamsCore()
{
return GameWorld()->Teams();
Expand Down
9 changes: 8 additions & 1 deletion src/game/client/prediction/entities/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class CCharacter : public CEntity
void GiveNinja();
void RemoveNinja();

void ResetVelocity();
void SetVelocity(vec2 NewVelocity);
void SetRawVelocity(vec2 NewVelocity);
void AddVelocity(vec2 Addition);
void ApplyMoveRestrictions();

bool m_IsLocal;

CTeamsCore *TeamsCore();
Expand All @@ -81,7 +87,6 @@ class CCharacter : public CEntity
int m_TileIndex;
int m_TileFIndex;

int m_MoveRestrictions;
bool m_LastRefillJumps;

// Setters/Getters because i don't want to modify vanilla vars access modifiers
Expand Down Expand Up @@ -145,6 +150,8 @@ class CCharacter : public CEntity
int m_ReloadTimer;
int m_AttackTick;

int m_MoveRestrictions;

// these are non-heldback inputs
CNetObj_PlayerInput m_LatestPrevInput;
CNetObj_PlayerInput m_LatestInput;
Expand Down
3 changes: 1 addition & 2 deletions src/game/client/prediction/entities/dragger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ void CDragger::DraggerBeamTick()
// In the center of the dragger a tee does not experience speed-up
else if(distance(pTarget->m_Pos, m_Pos) > 28)
{
vec2 Temp = pTarget->Core()->m_Vel + (normalize(m_Pos - pTarget->m_Pos) * m_Strength);
pTarget->Core()->m_Vel = ClampVel(pTarget->m_MoveRestrictions, Temp);
pTarget->AddVelocity(normalize(m_Pos - pTarget->m_Pos) * m_Strength);
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/game/client/prediction/entities/laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,39 @@ bool CLaser::HitCharacter(vec2 From, vec2 To)
m_Energy = -1;
if(m_Type == WEAPON_SHOTGUN)
{
vec2 Temp;
float Strength = GetTuning(m_TuneZone)->m_ShotgunStrength;
float Strength;
if(!m_TuneZone)
Strength = Tuning()->m_ShotgunStrength;
else
Strength = TuningList()[m_TuneZone].m_ShotgunStrength;

const vec2 &HitPos = pHit->Core()->m_Pos;
if(!g_Config.m_SvOldLaser)
{
if(m_PrevPos != HitPos)
{
Temp = pHit->Core()->m_Vel + normalize(m_PrevPos - HitPos) * Strength;
pHit->Core()->m_Vel = ClampVel(pHit->m_MoveRestrictions, Temp);
pHit->AddVelocity(normalize(m_PrevPos - HitPos) * Strength);
}
else
{
pHit->Core()->m_Vel = StackedLaserShotgunBugSpeed;
pHit->SetRawVelocity(StackedLaserShotgunBugSpeed);
}
}
else if(g_Config.m_SvOldLaser && pOwnerChar)
{
if(pOwnerChar->Core()->m_Pos != HitPos)
{
Temp = pHit->Core()->m_Vel + normalize(pOwnerChar->Core()->m_Pos - HitPos) * Strength;
pHit->Core()->m_Vel = ClampVel(pHit->m_MoveRestrictions, Temp);
pHit->AddVelocity(normalize(pOwnerChar->Core()->m_Pos - HitPos) * Strength);
}
else
{
pHit->Core()->m_Vel = StackedLaserShotgunBugSpeed;
pHit->SetRawVelocity(StackedLaserShotgunBugSpeed);
}
}
else
{
pHit->Core()->m_Vel = ClampVel(pHit->m_MoveRestrictions, pHit->Core()->m_Vel);
// Re-apply move restrictions as a part of 'shotgun bug' reproduction
pHit->ApplyMoveRestrictions();
}
}
else if(m_Type == WEAPON_LASER)
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/prediction/gameworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ void CGameWorld::NetObjEnd()
if(pHookedChar->m_MarkedForDestroy)
{
pHookedChar->m_Pos = pHookedChar->m_Core.m_Pos = pChar->m_Core.m_HookPos;
pHookedChar->m_Core.m_Vel = vec2(0, 0);
pHookedChar->ResetVelocity();
mem_zero(&pHookedChar->m_SavedInput, sizeof(pHookedChar->m_SavedInput));
pHookedChar->m_SavedInput.m_TargetY = -1;
pHookedChar->m_KeepHooked = true;
Expand Down
6 changes: 3 additions & 3 deletions src/game/server/ddracechat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ void CGameContext::ConTeleTo(IConsole::IResult *pResult, void *pUserData)
// Teleport tee
pSelf->Teleport(pCallingCharacter, Pos);
pCallingCharacter->UnFreeze();
pCallingCharacter->Core()->m_Vel = vec2(0, 0);
pCallingCharacter->ResetVelocity();
pCallingPlayer->m_LastTeleTee.Save(pCallingCharacter);
}

Expand Down Expand Up @@ -1670,7 +1670,7 @@ void CGameContext::ConTeleXY(IConsole::IResult *pResult, void *pUserData)
// Teleport tee
pSelf->Teleport(pCallingCharacter, Pos);
pCallingCharacter->UnFreeze();
pCallingCharacter->Core()->m_Vel = vec2(0, 0);
pCallingCharacter->ResetVelocity();
pCallingPlayer->m_LastTeleTee.Save(pCallingCharacter);
}

Expand Down Expand Up @@ -1722,7 +1722,7 @@ void CGameContext::ConTeleCursor(IConsole::IResult *pResult, void *pUserData)
}
pSelf->Teleport(pChr, Pos);
pChr->UnFreeze();
pChr->Core()->m_Vel = vec2(0, 0);
pChr->ResetVelocity();
pPlayer->m_LastTeleTee.Save(pChr);
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/server/ddracecommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void CGameContext::ConTeleport(IConsole::IResult *pResult, void *pUserData)
}
pSelf->Teleport(pChr, Pos);
pChr->UnFreeze();
pChr->Core()->m_Vel = vec2(0, 0);
pChr->SetVelocity(vec2(0, 0));
}
}

Expand Down
31 changes: 29 additions & 2 deletions src/game/server/entities/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void CCharacter::HandleNinja()
Collision()->MoveBox(&m_Core.m_Pos, &m_Core.m_Vel, vec2(GetProximityRadius(), GetProximityRadius()), GroundElasticity);

// reset velocity so the client doesn't predict stuff
m_Core.m_Vel = vec2(0.f, 0.f);
ResetVelocity();

// check if we Hit anything along the way
{
Expand Down Expand Up @@ -1640,7 +1640,7 @@ void CCharacter::HandleTiles(int Index)
m_Core.m_Jumped = 0;
m_Core.m_JumpedTotal = 0;
}
m_Core.m_Vel = ClampVel(m_MoveRestrictions, m_Core.m_Vel);
ApplyMoveRestrictions();

// handle switch tiles
if(Collision()->GetSwitchType(MapIndex) == TILE_SWITCHOPEN && Team() != TEAM_SUPER && Collision()->GetSwitchNumber(MapIndex) > 0)
Expand Down Expand Up @@ -2357,6 +2357,33 @@ CClientMask CCharacter::TeamMask()
return Teams()->TeamMask(Team(), -1, GetPlayer()->GetCID());
}

void CCharacter::ResetVelocity()
{
Core()->m_Vel = vec2(0, 0);
}

void CCharacter::SetVelocity(vec2 NewVelocity)
{
Core()->m_Vel = ClampVel(m_MoveRestrictions, NewVelocity);
}

// The method is needed only to reproduce 'shotgun bug' ddnet#5258
// Use SetVelocity() instead.
void CCharacter::SetRawVelocity(vec2 NewVelocity)
{
Core()->m_Vel = NewVelocity;
}

void CCharacter::AddVelocity(vec2 Addition)
{
SetVelocity(Core()->m_Vel + Addition);
}

void CCharacter::ApplyMoveRestrictions()
{
m_Core.m_Vel = ClampVel(m_MoveRestrictions, m_Core.m_Vel);
}

void CCharacter::SwapClients(int Client1, int Client2)
{
const int HookedPlayer = m_Core.HookedPlayer();
Expand Down
10 changes: 8 additions & 2 deletions src/game/server/entities/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class CCharacter : public CEntity
class CPlayer *GetPlayer() { return m_pPlayer; }
CClientMask TeamMask();

void ResetVelocity();
void SetVelocity(vec2 NewVelocity);
void SetRawVelocity(vec2 NewVelocity);
void AddVelocity(vec2 Addition);
void ApplyMoveRestrictions();

private:
// player controlling this character
class CPlayer *m_pPlayer;
Expand All @@ -106,6 +112,8 @@ class CCharacter : public CEntity
int m_ReloadTimer;
int m_AttackTick;

int m_MoveRestrictions;

int m_DamageTaken;

int m_EmoteType;
Expand Down Expand Up @@ -201,8 +209,6 @@ class CCharacter : public CEntity
int m_TileIndex;
int m_TileFIndex;

int m_MoveRestrictions;

int64_t m_LastStartWarning;
int64_t m_LastRescue;
bool m_LastRefillJumps;
Expand Down
3 changes: 1 addition & 2 deletions src/game/server/entities/dragger_beam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ void CDraggerBeam::Tick()
// In the center of the dragger a tee does not experience speed-up
else if(distance(pTarget->m_Pos, m_Pos) > 28)
{
vec2 Temp = pTarget->Core()->m_Vel + (normalize(m_Pos - pTarget->m_Pos) * m_Strength);
pTarget->Core()->m_Vel = ClampVel(pTarget->m_MoveRestrictions, Temp);
pTarget->AddVelocity(normalize(m_Pos - pTarget->m_Pos) * m_Strength);
}
}

Expand Down
21 changes: 9 additions & 12 deletions src/game/server/entities/laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ bool CLaser::HitCharacter(vec2 From, vec2 To)
bool pDontHitSelf = g_Config.m_SvOldLaser || (m_Bounces == 0 && !m_WasTele);

if(pOwnerChar ? (!pOwnerChar->LaserHitDisabled() && m_Type == WEAPON_LASER) || (!pOwnerChar->ShotgunHitDisabled() && m_Type == WEAPON_SHOTGUN) : g_Config.m_SvHit)
pHit = GameServer()->m_World.IntersectCharacter(m_Pos, To, 0.f, At, pDontHitSelf ? pOwnerChar : 0, m_Owner);
pHit = GameWorld()->IntersectCharacter(m_Pos, To, 0.f, At, pDontHitSelf ? pOwnerChar : 0, m_Owner);
else
pHit = GameServer()->m_World.IntersectCharacter(m_Pos, To, 0.f, At, pDontHitSelf ? pOwnerChar : 0, m_Owner, pOwnerChar);
pHit = GameWorld()->IntersectCharacter(m_Pos, To, 0.f, At, pDontHitSelf ? pOwnerChar : 0, m_Owner, pOwnerChar);

if(!pHit || (pHit == pOwnerChar && g_Config.m_SvOldLaser) || (pHit != pOwnerChar && pOwnerChar ? (pOwnerChar->LaserHitDisabled() && m_Type == WEAPON_LASER) || (pOwnerChar->ShotgunHitDisabled() && m_Type == WEAPON_SHOTGUN) : !g_Config.m_SvHit))
return false;
Expand All @@ -55,42 +55,39 @@ bool CLaser::HitCharacter(vec2 From, vec2 To)
m_Energy = -1;
if(m_Type == WEAPON_SHOTGUN)
{
vec2 Temp;

float Strength;
if(!m_TuneZone)
Strength = Tuning()->m_ShotgunStrength;
else
Strength = TuningList()[m_TuneZone].m_ShotgunStrength;

vec2 &HitPos = pHit->Core()->m_Pos;
const vec2 &HitPos = pHit->Core()->m_Pos;
if(!g_Config.m_SvOldLaser)
{
if(m_PrevPos != HitPos)
{
Temp = pHit->Core()->m_Vel + normalize(m_PrevPos - HitPos) * Strength;
pHit->Core()->m_Vel = ClampVel(pHit->m_MoveRestrictions, Temp);
pHit->AddVelocity(normalize(m_PrevPos - HitPos) * Strength);
}
else
{
pHit->Core()->m_Vel = StackedLaserShotgunBugSpeed;
pHit->SetRawVelocity(StackedLaserShotgunBugSpeed);
}
}
else if(g_Config.m_SvOldLaser && pOwnerChar)
{
if(pOwnerChar->Core()->m_Pos != HitPos)
{
Temp = pHit->Core()->m_Vel + normalize(pOwnerChar->Core()->m_Pos - HitPos) * Strength;
pHit->Core()->m_Vel = ClampVel(pHit->m_MoveRestrictions, Temp);
pHit->AddVelocity(normalize(pOwnerChar->Core()->m_Pos - HitPos) * Strength);
}
else
{
pHit->Core()->m_Vel = StackedLaserShotgunBugSpeed;
pHit->SetRawVelocity(StackedLaserShotgunBugSpeed);
}
}
else
{
pHit->Core()->m_Vel = ClampVel(pHit->m_MoveRestrictions, pHit->Core()->m_Vel);
// Re-apply move restrictions as a part of 'shotgun bug' reproduction
pHit->ApplyMoveRestrictions();
}
}
else if(m_Type == WEAPON_LASER)
Expand Down

0 comments on commit 6520eb0

Please sign in to comment.