Skip to content

Commit

Permalink
relocate new trajs' lasertrail update
Browse files Browse the repository at this point in the history
as @CrimRecya suggested, and make PhobosTrajectory::CreateInstance virtual
  • Loading branch information
chaserli committed Jun 30, 2024
1 parent d02081c commit bd90324
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 32 deletions.
7 changes: 4 additions & 3 deletions src/Ext/Bullet/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ DEFINE_HOOK(0x4666F7, BulletClass_AI, 0x6)
}
}

// LaserTrails update routine is in BulletClass::AI hook because BulletClass::Draw
// doesn't run when the object is off-screen which leads to visual bugs - Kerbiter
if (pBulletExt && pBulletExt->LaserTrails.size())
//Because the laser trails will be drawn before the calculation of changing the velocity direction in each frame.
//This will cause the laser trails to be drawn in the wrong position too early, resulting in a visual appearance resembling a "bouncing".
//Let trajectories draw their own laser trails after the Trajectory's OnAI() to avoid predicting incorrect positions or pass through targets.
if (!pBulletExt->Trajectory && pBulletExt->LaserTrails.size())
{
CoordStruct location = pThis->GetCoords();
const BulletVelocity& velocity = pThis->Velocity;
Expand Down
5 changes: 5 additions & 0 deletions src/Ext/Bullet/Trajectories/BombardTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include <Ext/BulletType/Body.h>

PhobosTrajectory* BombardTrajectoryType::CreateInstance() const
{
return new BombardTrajectory(this);
}

bool BombardTrajectoryType::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
this->PhobosTrajectoryType::Load(Stm, false);
Expand Down
3 changes: 2 additions & 1 deletion src/Ext/Bullet/Trajectories/BombardTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BombardTrajectoryType final : public PhobosTrajectoryType

virtual bool Load(PhobosStreamReader& Stm, bool RegisterForChange) override;
virtual bool Save(PhobosStreamWriter& Stm) const override;
virtual PhobosTrajectory* CreateInstance() const override;

virtual void Read(CCINIClass* const pINI, const char* pSection) override;

Expand All @@ -25,7 +26,7 @@ class BombardTrajectory final : public PhobosTrajectory
, Height { 0.0 }
{}

BombardTrajectory(PhobosTrajectoryType* pType) : PhobosTrajectory(TrajectoryFlag::Bombard)
BombardTrajectory(PhobosTrajectoryType const* pType) : PhobosTrajectory(TrajectoryFlag::Bombard)
, IsFalling { false }
, Height { 0.0 }
{}
Expand Down
44 changes: 22 additions & 22 deletions src/Ext/Bullet/Trajectories/PhobosTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,6 @@ double PhobosTrajectory::GetTrajectorySpeed(BulletClass* pBullet) const
return 100.0;
}

PhobosTrajectory* PhobosTrajectory::CreateInstance(PhobosTrajectoryType* pType, BulletClass* pBullet, CoordStruct* pCoord, BulletVelocity* pVelocity)
{
PhobosTrajectory* pRet = nullptr;

switch (pType->Flag)
{
case TrajectoryFlag::Straight:
pRet = DLLCreate<StraightTrajectory>(pType);
break;

case TrajectoryFlag::Bombard:
pRet = DLLCreate<BombardTrajectory>(pType);
break;
}

if (pRet)
pRet->OnUnlimbo(pBullet, pCoord, pVelocity);

return pRet;
}

PhobosTrajectory* PhobosTrajectory::LoadFromStream(PhobosStreamReader& Stm)
{
PhobosTrajectory* pTraj = nullptr;
Expand Down Expand Up @@ -207,6 +186,24 @@ DEFINE_HOOK(0x4666F7, BulletClass_AI_Trajectories, 0x6)
if (detonate && !pThis->SpawnNextAnim)
return Detonate;

if (pExt->Trajectory && pExt->LaserTrails.size())
{
CoordStruct futureCoords
{
pThis->Location.X + static_cast<int>(pThis->Velocity.X),
pThis->Location.Y + static_cast<int>(pThis->Velocity.Y),
pThis->Location.Z + static_cast<int>(pThis->Velocity.Z)
};

for (auto& trail : pExt->LaserTrails)
{
if (!trail.LastLocation.isset())
trail.LastLocation = pThis->Location;

trail.Update(futureCoords);
}
}

return 0;
}

Expand Down Expand Up @@ -302,7 +299,10 @@ DEFINE_HOOK(0x468B72, BulletClass_Unlimbo_Trajectories, 0x5)
auto const pTypeExt = pExt->TypeExtData;

if (pTypeExt && pTypeExt->TrajectoryType)
pExt->Trajectory = PhobosTrajectory::CreateInstance(pTypeExt->TrajectoryType, pThis, pCoord, pVelocity);
{
pExt->Trajectory = pTypeExt->TrajectoryType->CreateInstance();
pExt->Trajectory->OnUnlimbo(pThis, pCoord, pVelocity);
}

return 0;
}
6 changes: 2 additions & 4 deletions src/Ext/Bullet/Trajectories/PhobosTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum class TrajectoryCheckReturnType : int
SatisfyGameCheck = 2,
Detonate = 3
};

class PhobosTrajectory;
class PhobosTrajectoryType
{
public:
Expand All @@ -35,7 +35,7 @@ class PhobosTrajectoryType
virtual bool Save(PhobosStreamWriter& Stm) const;

virtual void Read(CCINIClass* const pINI, const char* pSection) = 0;

virtual PhobosTrajectory* CreateInstance() const = 0;
static void CreateType(PhobosTrajectoryType*& pType, CCINIClass* const pINI, const char* pSection, const char* pKey);

static PhobosTrajectoryType* LoadFromStream(PhobosStreamReader& Stm);
Expand Down Expand Up @@ -71,8 +71,6 @@ class PhobosTrajectory
}
double GetTrajectorySpeed(BulletClass* pBullet) const;

static PhobosTrajectory* CreateInstance(PhobosTrajectoryType* pType, BulletClass* pBullet, CoordStruct* pCoord, BulletVelocity* pVelocity);

static PhobosTrajectory* LoadFromStream(PhobosStreamReader& Stm);
static void WriteToStream(PhobosStreamWriter& Stm, PhobosTrajectory* pTraj);
static PhobosTrajectory* ProcessFromStream(PhobosStreamReader& Stm, PhobosTrajectory* pTraj);
Expand Down
5 changes: 5 additions & 0 deletions src/Ext/Bullet/Trajectories/StraightTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ bool StraightTrajectoryType::Load(PhobosStreamReader& Stm, bool RegisterForChang
return true;
}

PhobosTrajectory* StraightTrajectoryType::CreateInstance() const
{
return new StraightTrajectory(this);
}

bool StraightTrajectoryType::Save(PhobosStreamWriter& Stm) const
{
this->PhobosTrajectoryType::Save(Stm);
Expand Down
4 changes: 2 additions & 2 deletions src/Ext/Bullet/Trajectories/StraightTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class StraightTrajectoryType final : public PhobosTrajectoryType

virtual bool Load(PhobosStreamReader& Stm, bool RegisterForChange) override;
virtual bool Save(PhobosStreamWriter& Stm) const override;

virtual PhobosTrajectory* CreateInstance() const override;
virtual void Read(CCINIClass* const pINI, const char* pSection) override;

Valueable<Leptons> DetonationDistance;
Expand All @@ -32,7 +32,7 @@ class StraightTrajectory final : public PhobosTrajectory
, TargetZPosition { 0 }
{}

StraightTrajectory(PhobosTrajectoryType* pType) : PhobosTrajectory(TrajectoryFlag::Straight)
StraightTrajectory(PhobosTrajectoryType const* pType) : PhobosTrajectory(TrajectoryFlag::Straight)
, DetonationDistance { Leptons(102) }
, TargetSnapDistance { Leptons(128) }
, PassThrough { false }
Expand Down

0 comments on commit bd90324

Please sign in to comment.