Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix #6409
  • Loading branch information
rtri committed Jul 20, 2020
1 parent 5588635 commit fa07f14
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
29 changes: 13 additions & 16 deletions rts/Sim/Projectiles/ExpGenSpawner.cpp
Expand Up @@ -17,38 +17,34 @@ CR_REG_METADATA(CExpGenSpawner,
))


CExpGenSpawner::CExpGenSpawner() :
CProjectile(),
delay(1),
damage(0.0f),
explosionGenerator(nullptr)
CExpGenSpawner::CExpGenSpawner() : CProjectile()
{
checkCol = false;
deleteMe = false;
}

void CExpGenSpawner::Serialize(creg::ISerializer* s) {
int generatorID;

if (s->IsWriting())
generatorID = explosionGenerator->GetGeneratorID();

s->SerializeInt(&generatorID, sizeof(generatorID));

if (!s->IsWriting())
explosionGenerator = explGenHandler.GetGenerator(generatorID);
}
if (s->IsWriting())
return;

void CExpGenSpawner::Update()
{
if ((deleteMe |= ((delay--) <= 0))) {
explosionGenerator->Explosion(pos, dir, damage, 0.0f, 0.0f, owner(), nullptr);
}
// NOTE:
// projectiles are serialized, but ExplosionGeneratorHandler itself is not
// as such generator-id can be invalid when this spawner gets deserialized
// (if additional generators were loaded at runtime before game was saved)
explosionGenerator = explGenHandler.GetGenerator(generatorID);
}


int CExpGenSpawner::GetProjectilesCount() const
void CExpGenSpawner::Update()
{
return 0;
if ((deleteMe |= ((delay--) <= 0)))
explosionGenerator->Explosion(pos, dir, damage, 0.0f, 0.0f, owner(), nullptr);
}


Expand All @@ -59,6 +55,7 @@ bool CExpGenSpawner::GetMemberInfo(SExpGenSpawnableMemberInfo& memberInfo)

CHECK_MEMBER_INFO_INT (CExpGenSpawner, delay )
CHECK_MEMBER_INFO_FLOAT(CExpGenSpawner, damage)
// TODO: much nicer to load cegID directly via LoadGeneratorID callback
CHECK_MEMBER_INFO_PTR (CExpGenSpawner, explosionGenerator, explGenHandler.LoadGenerator)

return false;
Expand Down
8 changes: 4 additions & 4 deletions rts/Sim/Projectiles/ExpGenSpawner.h
Expand Up @@ -18,15 +18,15 @@ class CExpGenSpawner : public CProjectile

virtual void Update() override;

virtual int GetProjectilesCount() const override;
virtual int GetProjectilesCount() const override { return 0; }

static bool GetMemberInfo(SExpGenSpawnableMemberInfo& memberInfo);

private:
int delay;
float damage;
int delay = 1;
float damage = 0.0f;

IExplosionGenerator* explosionGenerator;
IExplosionGenerator* explosionGenerator = nullptr;
};

#endif // EXP_GEN_SPAWNER_H
15 changes: 10 additions & 5 deletions rts/Sim/Projectiles/ExplosionGenerator.cpp
Expand Up @@ -154,11 +154,15 @@ std::string ClassAliasList::FindAlias(const std::string& className) const
void CExplosionGeneratorHandler::Init()
{
egMemPool.clear();
egMemPool.reserve(32);
egMemPool.reserve(512);

explosionGenerators.reserve(512);
explosionGenerators.push_back(egMemPool.alloc<CStdExplosionGenerator>()); // id=0 (shared standard EG)
explosionGenerators.push_back(egMemPool.alloc< IExplosionGenerator>()); // id=1 (dummy fallback EG)

explosionGenerators[EXPGEN_ID_STANDARD]->SetGeneratorID(EXPGEN_ID_STANDARD);
explosionGenerators[EXPGEN_ID_FALLBACK]->SetGeneratorID(EXPGEN_ID_FALLBACK);

explosionGenerators.reserve(32);
explosionGenerators.push_back(egMemPool.alloc<CStdExplosionGenerator>()); // id=0
explosionGenerators[0]->SetGeneratorID(EXPGEN_ID_STANDARD);

exploParser = nullptr;
aliasParser = nullptr;
Expand Down Expand Up @@ -331,8 +335,9 @@ IExplosionGenerator* CExplosionGeneratorHandler::GetGenerator(unsigned int expGe
{
if (expGenID == EXPGEN_ID_INVALID)
return nullptr;
// can happen after save/load for spawners
if (expGenID >= explosionGenerators.size())
return nullptr;
return explosionGenerators[EXPGEN_ID_FALLBACK];

return explosionGenerators[expGenID];
}
Expand Down
5 changes: 3 additions & 2 deletions rts/Sim/Projectiles/ExplosionGenerator.h
Expand Up @@ -42,6 +42,7 @@ class CExplosionGeneratorHandler
enum {
EXPGEN_ID_INVALID = -1u,
EXPGEN_ID_STANDARD = 0u,
EXPGEN_ID_FALLBACK = 1u,
};

void Init();
Expand Down Expand Up @@ -93,7 +94,7 @@ class IExplosionGenerator
IExplosionGenerator(): generatorID(CExplosionGeneratorHandler::EXPGEN_ID_INVALID) {}
virtual ~IExplosionGenerator() {}

virtual bool Load(CExplosionGeneratorHandler* handler, const char* tag) = 0;
virtual bool Load(CExplosionGeneratorHandler* handler, const char* tag) { return false; }
virtual bool Reload(CExplosionGeneratorHandler* handler, const char* tag) { return true; }
virtual bool Explosion(
const float3& pos,
Expand All @@ -103,7 +104,7 @@ class IExplosionGenerator
float gfxMod,
CUnit* owner,
CUnit* hit
) = 0;
) { return false; }

unsigned int GetGeneratorID() const { return generatorID; }
void SetGeneratorID(unsigned int id) { generatorID = id; }
Expand Down

0 comments on commit fa07f14

Please sign in to comment.