Codechange: Replace separate EffectVehicle arrays. (#12565)

Combine 3 separate arrays into a single struct. This keeps related data together, and avoids needing to check that each array is same length.

Use of constexpr construct ensures data in the array is not default-initialised.

Removes lengthof.
This commit is contained in:
Peter Nelson 2024-04-24 21:29:12 +01:00 committed by GitHub
parent e20f48799e
commit 6a3f50aa72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 54 deletions

View File

@ -527,61 +527,33 @@ static bool BubbleTick(EffectVehicle *v)
return true;
}
struct EffectProcs {
using InitProc = void(EffectVehicle *);
using TickProc = bool(EffectVehicle *);
typedef void EffectInitProc(EffectVehicle *v);
typedef bool EffectTickProc(EffectVehicle *v);
InitProc *init_proc; ///< Function to initialise an effect vehicle after construction.
TickProc *tick_proc; ///< Functions for controlling effect vehicles at each tick.
TransparencyOption transparency; ///< Transparency option affecting the effect.
/** Functions to initialise an effect vehicle after construction. */
static EffectInitProc * const _effect_init_procs[] = {
ChimneySmokeInit, // EV_CHIMNEY_SMOKE
SteamSmokeInit, // EV_STEAM_SMOKE
DieselSmokeInit, // EV_DIESEL_SMOKE
ElectricSparkInit, // EV_ELECTRIC_SPARK
SmokeInit, // EV_CRASH_SMOKE
ExplosionLargeInit, // EV_EXPLOSION_LARGE
BreakdownSmokeInit, // EV_BREAKDOWN_SMOKE
ExplosionSmallInit, // EV_EXPLOSION_SMALL
BulldozerInit, // EV_BULLDOZER
BubbleInit, // EV_BUBBLE
SmokeInit, // EV_BREAKDOWN_SMOKE_AIRCRAFT
SmokeInit, // EV_COPPER_MINE_SMOKE
constexpr EffectProcs(InitProc *init_proc, TickProc *tick_proc, TransparencyOption transparency)
: init_proc(init_proc), tick_proc(tick_proc), transparency(transparency) {}
};
static_assert(lengthof(_effect_init_procs) == EV_END);
/** Functions for controlling effect vehicles at each tick. */
static EffectTickProc * const _effect_tick_procs[] = {
ChimneySmokeTick, // EV_CHIMNEY_SMOKE
SteamSmokeTick, // EV_STEAM_SMOKE
DieselSmokeTick, // EV_DIESEL_SMOKE
ElectricSparkTick, // EV_ELECTRIC_SPARK
SmokeTick, // EV_CRASH_SMOKE
ExplosionLargeTick, // EV_EXPLOSION_LARGE
BreakdownSmokeTick, // EV_BREAKDOWN_SMOKE
ExplosionSmallTick, // EV_EXPLOSION_SMALL
BulldozerTick, // EV_BULLDOZER
BubbleTick, // EV_BUBBLE
SmokeTick, // EV_BREAKDOWN_SMOKE_AIRCRAFT
SmokeTick, // EV_COPPER_MINE_SMOKE
};
static_assert(lengthof(_effect_tick_procs) == EV_END);
/** Transparency options affecting the effects. */
static const TransparencyOption _effect_transparency_options[] = {
TO_INDUSTRIES, // EV_CHIMNEY_SMOKE
TO_INVALID, // EV_STEAM_SMOKE
TO_INVALID, // EV_DIESEL_SMOKE
TO_INVALID, // EV_ELECTRIC_SPARK
TO_INVALID, // EV_CRASH_SMOKE
TO_INVALID, // EV_EXPLOSION_LARGE
TO_INVALID, // EV_BREAKDOWN_SMOKE
TO_INVALID, // EV_EXPLOSION_SMALL
TO_INVALID, // EV_BULLDOZER
TO_INDUSTRIES, // EV_BUBBLE
TO_INVALID, // EV_BREAKDOWN_SMOKE_AIRCRAFT
TO_INDUSTRIES, // EV_COPPER_MINE_SMOKE
};
static_assert(lengthof(_effect_transparency_options) == EV_END);
/** Per-EffectVehicleType handling. */
static std::array<EffectProcs, EV_END> _effect_procs = {{
{ ChimneySmokeInit, ChimneySmokeTick, TO_INDUSTRIES }, // EV_CHIMNEY_SMOKE
{ SteamSmokeInit, SteamSmokeTick, TO_INVALID }, // EV_STEAM_SMOKE
{ DieselSmokeInit, DieselSmokeTick, TO_INVALID }, // EV_DIESEL_SMOKE
{ ElectricSparkInit, ElectricSparkTick, TO_INVALID }, // EV_ELECTRIC_SPARK
{ SmokeInit, SmokeTick, TO_INVALID }, // EV_CRASH_SMOKE
{ ExplosionLargeInit, ExplosionLargeTick, TO_INVALID }, // EV_EXPLOSION_LARGE
{ BreakdownSmokeInit, BreakdownSmokeTick, TO_INVALID }, // EV_BREAKDOWN_SMOKE
{ ExplosionSmallInit, ExplosionSmallTick, TO_INVALID }, // EV_EXPLOSION_SMALL
{ BulldozerInit, BulldozerTick, TO_INVALID }, // EV_BULLDOZER
{ BubbleInit, BubbleTick, TO_INDUSTRIES }, // EV_BUBBLE
{ SmokeInit, SmokeTick, TO_INVALID }, // EV_BREAKDOWN_SMOKE_AIRCRAFT
{ SmokeInit, SmokeTick, TO_INDUSTRIES }, // EV_COPPER_MINE_SMOKE
}};
/**
* Create an effect vehicle at a particular location.
@ -604,7 +576,7 @@ EffectVehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicleType type)
v->UpdateDeltaXY();
v->vehstatus = VS_UNCLICKABLE;
_effect_init_procs[type](v);
_effect_procs[type].init_proc(v);
v->UpdatePositionAndViewport();
@ -642,7 +614,7 @@ EffectVehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, Eff
bool EffectVehicle::Tick()
{
return _effect_tick_procs[this->subtype](this);
return _effect_procs[this->subtype].tick_proc(this);
}
void EffectVehicle::UpdateDeltaXY()
@ -660,5 +632,5 @@ void EffectVehicle::UpdateDeltaXY()
*/
TransparencyOption EffectVehicle::GetTransparencyOption() const
{
return _effect_transparency_options[this->subtype];
return _effect_procs[this->subtype].transparency;
}