Skip to content

Commit

Permalink
minor refactor new trajectory boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
chaserli committed Sep 23, 2024
1 parent 76a8f44 commit 29efd60
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/Ext/Bullet/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ DEFINE_HOOK(0x4665E9, BulletClass_DTOR, 0xA)
GET(BulletClass*, pItem, ESI);

if (auto pTraj = BulletExt::ExtMap.Find(pItem)->Trajectory)
DLLDelete(pTraj);
delete pTraj;

BulletExt::ExtMap.Remove(pItem);
return 0;
Expand Down
27 changes: 18 additions & 9 deletions src/Ext/Bullet/Trajectories/BombardTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ PhobosTrajectory* BombardTrajectoryType::CreateInstance() const
return new BombardTrajectory(this);
}

template<typename T>
void BombardTrajectoryType::Serialize(T& Stm)
{
Stm.Process(this->Height);
}

bool BombardTrajectoryType::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
this->PhobosTrajectoryType::Load(Stm, false);
Stm.Process(this->Height, false);
this->Serialize(Stm);
return true;
}

bool BombardTrajectoryType::Save(PhobosStreamWriter& Stm) const
{
this->PhobosTrajectoryType::Save(Stm);
Stm.Process(this->Height);
const_cast<BombardTrajectoryType*>(this)->Serialize(Stm);
return true;
}

Expand All @@ -26,14 +32,20 @@ void BombardTrajectoryType::Read(CCINIClass* const pINI, const char* pSection)
this->Height = pINI->ReadDouble(pSection, "Trajectory.Bombard.Height", 0.0);
}

bool BombardTrajectory::Load(PhobosStreamReader& Stm, bool RegisterForChange)
template<typename T>
void BombardTrajectory::Serialize(T& Stm)
{
this->PhobosTrajectory::Load(Stm, false);

Stm
.Process(this->IsFalling)
.Process(this->Height)
;
}

bool BombardTrajectory::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
this->PhobosTrajectory::Load(Stm, false);

this->Serialize(Stm);

return true;
}
Expand All @@ -42,10 +54,7 @@ bool BombardTrajectory::Save(PhobosStreamWriter& Stm) const
{
this->PhobosTrajectory::Save(Stm);

Stm
.Process(this->IsFalling)
.Process(this->Height)
;
const_cast<BombardTrajectory*>(this)->Serialize(Stm);

return true;
}
Expand Down
13 changes: 9 additions & 4 deletions src/Ext/Bullet/Trajectories/BombardTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ class BombardTrajectoryType final : public PhobosTrajectoryType
virtual void Read(CCINIClass* const pINI, const char* pSection) override;

double Height;

private:
template <typename T>
void Serialize(T& Stm);
};

class BombardTrajectory final : public PhobosTrajectory
{
public:
BombardTrajectory() : PhobosTrajectory(TrajectoryFlag::Bombard)
, IsFalling { false }
, Height { 0.0 }
{}
BombardTrajectory(noinit_t) :PhobosTrajectory { noinit_t{} } { }

BombardTrajectory(PhobosTrajectoryType const* pType) : PhobosTrajectory(TrajectoryFlag::Bombard)
, IsFalling { false }
Expand All @@ -43,4 +44,8 @@ class BombardTrajectory final : public PhobosTrajectory

bool IsFalling;
double Height;

private:
template <typename T>
void Serialize(T& Stm);
};
4 changes: 2 additions & 2 deletions src/Ext/Bullet/Trajectories/PhobosTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ PhobosTrajectory* PhobosTrajectory::LoadFromStream(PhobosStreamReader& Stm)
switch (flag)
{
case TrajectoryFlag::Straight:
pTraj = DLLCreate<StraightTrajectory>();
pTraj = new StraightTrajectory(noinit_t {});
break;
case TrajectoryFlag::Bombard:
pTraj = DLLCreate<BombardTrajectory>();
pTraj = new BombardTrajectory(noinit_t {});
break;
default:
return nullptr;
Expand Down
49 changes: 22 additions & 27 deletions src/Ext/Bullet/Trajectories/StraightTrajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,35 @@
#include <Ext/Bullet/Body.h>
#include <Ext/WeaponType/Body.h>

bool StraightTrajectoryType::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
this->PhobosTrajectoryType::Load(Stm, false);

Stm
.Process(this->DetonationDistance, false)
.Process(this->ApplyRangeModifiers, false)
.Process(this->TargetSnapDistance, false)
.Process(this->PassThrough, false)
;

return true;
}

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

bool StraightTrajectoryType::Save(PhobosStreamWriter& Stm) const
template<typename T>
void StraightTrajectoryType::Serialize(T& Stm)
{
this->PhobosTrajectoryType::Save(Stm);

Stm
.Process(this->DetonationDistance)
.Process(this->ApplyRangeModifiers)
.Process(this->TargetSnapDistance)
.Process(this->PassThrough)
;
}

bool StraightTrajectoryType::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
this->PhobosTrajectoryType::Load(Stm, false);
this->Serialize(Stm);
return true;
}

bool StraightTrajectoryType::Save(PhobosStreamWriter& Stm) const
{
this->PhobosTrajectoryType::Save(Stm);
const_cast<StraightTrajectoryType*>(this)->Serialize(Stm);
return true;
}

void StraightTrajectoryType::Read(CCINIClass* const pINI, const char* pSection)
{
Expand All @@ -46,17 +42,22 @@ void StraightTrajectoryType::Read(CCINIClass* const pINI, const char* pSection)
this->PassThrough.Read(exINI, pSection, "Trajectory.Straight.PassThrough");
}

bool StraightTrajectory::Load(PhobosStreamReader& Stm, bool RegisterForChange)
template<typename T>
void StraightTrajectory::Serialize(T& Stm)
{
this->PhobosTrajectory::Load(Stm, false);

Stm
.Process(this->DetonationDistance)
.Process(this->TargetSnapDistance)
.Process(this->PassThrough)
.Process(this->FirerZPosition)
.Process(this->TargetZPosition)
;
}

bool StraightTrajectory::Load(PhobosStreamReader& Stm, bool RegisterForChange)
{
this->PhobosTrajectory::Load(Stm, false);
this->Serialize(Stm);

return true;
}
Expand All @@ -65,13 +66,7 @@ bool StraightTrajectory::Save(PhobosStreamWriter& Stm) const
{
this->PhobosTrajectory::Save(Stm);

Stm
.Process(this->DetonationDistance)
.Process(this->TargetSnapDistance)
.Process(this->PassThrough)
.Process(this->FirerZPosition)
.Process(this->TargetZPosition)
;
const_cast<StraightTrajectory*>(this)->Serialize(Stm);

return true;
}
Expand Down
15 changes: 8 additions & 7 deletions src/Ext/Bullet/Trajectories/StraightTrajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ class StraightTrajectoryType final : public PhobosTrajectoryType
Valueable<bool> ApplyRangeModifiers;
Valueable<Leptons> TargetSnapDistance;
Valueable<bool> PassThrough;

private:
template <typename T>
void Serialize(T& Stm);
};

class StraightTrajectory final : public PhobosTrajectory
{
public:
StraightTrajectory() : PhobosTrajectory(TrajectoryFlag::Straight)
, DetonationDistance { Leptons(102) }
, TargetSnapDistance { Leptons(128) }
, PassThrough { false }
, FirerZPosition { 0 }
, TargetZPosition { 0 }
{}
StraightTrajectory(noinit_t) :PhobosTrajectory { noinit_t{} } { }

StraightTrajectory(PhobosTrajectoryType const* pType) : PhobosTrajectory(TrajectoryFlag::Straight)
, DetonationDistance { Leptons(102) }
Expand Down Expand Up @@ -63,4 +61,7 @@ class StraightTrajectory final : public PhobosTrajectory
int GetFirerZPosition(BulletClass* pBullet);
int GetTargetZPosition(BulletClass* pBullet);
bool ElevationDetonationCheck(BulletClass* pBullet);

template <typename T>
void Serialize(T& Stm);
};

0 comments on commit 29efd60

Please sign in to comment.