diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 34a48170f8..190ac269b0 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -649,8 +649,7 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte if (engine != EngineID::Invalid()) { Engine *e = Engine::Get(engine); if (!e->grf_prop.HasGrfFile()) { - e->grf_prop.grfid = file->grfid; - e->grf_prop.grffile = file; + e->grf_prop.SetGRFFile(file); } return e; } @@ -662,8 +661,7 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte Engine *e = Engine::Get(engine); if (!e->grf_prop.HasGrfFile()) { - e->grf_prop.grfid = file->grfid; - e->grf_prop.grffile = file; + e->grf_prop.SetGRFFile(file); GrfMsg(5, "Replaced engine at index {} for GRFID {:x}, type {}, index {}", e->index, std::byteswap(file->grfid), type, internal_id); } @@ -681,8 +679,7 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte /* ... it's not, so create a new one based off an existing engine */ Engine *e = new Engine(type, internal_id); - e->grf_prop.grfid = file->grfid; - e->grf_prop.grffile = file; + e->grf_prop.SetGRFFile(file); /* Reserve the engine slot */ _engine_mngr.SetID(type, internal_id, scope_grfid, std::min(internal_id, _engine_counts[type]), e->index); @@ -2559,8 +2556,7 @@ static ChangeInfoResult TownHouseChangeInfo(uint first, uint last, int prop, Byt housespec->enabled = true; housespec->grf_prop.local_id = id; housespec->grf_prop.subst_id = subs_id; - housespec->grf_prop.grfid = _cur.grffile->grfid; - housespec->grf_prop.grffile = _cur.grffile; + housespec->grf_prop.SetGRFFile(_cur.grffile); /* Set default colours for randomization, used if not overridden. */ housespec->random_colour[0] = COLOUR_RED; housespec->random_colour[1] = COLOUR_BLUE; @@ -3472,8 +3468,7 @@ static ChangeInfoResult IndustrytilesChangeInfo(uint first, uint last, int prop, tsp->grf_prop.local_id = id; tsp->grf_prop.subst_id = subs_id; - tsp->grf_prop.grfid = _cur.grffile->grfid; - tsp->grf_prop.grffile = _cur.grffile; + tsp->grf_prop.SetGRFFile(_cur.grffile); _industile_mngr.AddEntityID(id, _cur.grffile->grfid, subs_id); // pre-reserve the tile slot } break; @@ -3738,8 +3733,7 @@ static ChangeInfoResult IndustriesChangeInfo(uint first, uint last, int prop, By indsp->enabled = true; indsp->grf_prop.local_id = id; indsp->grf_prop.subst_id = subs_id; - indsp->grf_prop.grfid = _cur.grffile->grfid; - indsp->grf_prop.grffile = _cur.grffile; + indsp->grf_prop.SetGRFFile(_cur.grffile); /* If the grf industry needs to check its surrounding upon creation, it should * rely on callbacks, not on the original placement functions */ indsp->check_proc = CHECK_NOTHING; @@ -4116,8 +4110,7 @@ static ChangeInfoResult AirportChangeInfo(uint first, uint last, int prop, ByteR as->enabled = true; as->grf_prop.local_id = id; as->grf_prop.subst_id = subs_id; - as->grf_prop.grfid = _cur.grffile->grfid; - as->grf_prop.grffile = _cur.grffile; + as->grf_prop.SetGRFFile(_cur.grffile); /* override the default airport */ _airport_mngr.Add(id, _cur.grffile->grfid, subs_id); } @@ -4904,8 +4897,7 @@ static ChangeInfoResult AirportTilesChangeInfo(uint first, uint last, int prop, tsp->grf_prop.local_id = id; tsp->grf_prop.subst_id = subs_id; - tsp->grf_prop.grfid = _cur.grffile->grfid; - tsp->grf_prop.grffile = _cur.grffile; + tsp->grf_prop.SetGRFFile(_cur.grffile); _airporttile_mngr.AddEntityID(id, _cur.grffile->grfid, subs_id); // pre-reserve the tile slot } break; @@ -6018,8 +6010,7 @@ static void StationMapSpriteGroup(ByteReader &buf, uint8_t idcount) } statspec->grf_prop.SetSpriteGroup(SpriteGroupCargo::SG_DEFAULT, _cur.spritegroups[groupid]); - statspec->grf_prop.grfid = _cur.grffile->grfid; - statspec->grf_prop.grffile = _cur.grffile; + statspec->grf_prop.SetGRFFile(_cur.grffile); statspec->grf_prop.local_id = station; StationClass::Assign(statspec); } @@ -6203,8 +6194,7 @@ static void ObjectMapSpriteGroup(ByteReader &buf, uint8_t idcount) } spec->grf_prop.SetSpriteGroup(OBJECT_SPRITE_GROUP_DEFAULT, _cur.spritegroups[groupid]); - spec->grf_prop.grfid = _cur.grffile->grfid; - spec->grf_prop.grffile = _cur.grffile; + spec->grf_prop.SetGRFFile(_cur.grffile); spec->grf_prop.local_id = object; } } @@ -6390,8 +6380,7 @@ static void RoadStopMapSpriteGroup(ByteReader &buf, uint8_t idcount) } roadstopspec->grf_prop.SetSpriteGroup(SpriteGroupCargo::SG_DEFAULT, _cur.spritegroups[groupid]); - roadstopspec->grf_prop.grfid = _cur.grffile->grfid; - roadstopspec->grf_prop.grffile = _cur.grffile; + roadstopspec->grf_prop.SetGRFFile(_cur.grffile); roadstopspec->grf_prop.local_id = roadstop; RoadStopClass::Assign(roadstopspec); } @@ -6442,7 +6431,7 @@ static void BadgeMapSpriteGroup(ByteReader &buf, uint8_t idcount) auto &badge = *GetBadge(found->second); badge.grf_prop.SetSpriteGroup(GSF_END, _cur.spritegroups[groupid]); - badge.grf_prop.grffile = _cur.grffile; + badge.grf_prop.SetGRFFile(_cur.grffile); badge.grf_prop.local_id = local_id; } } diff --git a/src/newgrf_commons.cpp b/src/newgrf_commons.cpp index ccd6603c06..74cda21591 100644 --- a/src/newgrf_commons.cpp +++ b/src/newgrf_commons.cpp @@ -732,6 +732,16 @@ void NewGRFSpriteLayout::ProcessRegisters(uint8_t resolved_var10, uint32_t resol } } +/** + * Set the NewGRF file, and its grfid, associated with grf props. + * @param grffile GRFFile to set. + */ +void GRFFilePropsBase::SetGRFFile(const struct GRFFile *grffile) +{ + this->grffile = grffile; + this->grfid = grffile == nullptr ? 0 : grffile->grfid; +} + /** * Get the SpriteGroup at the specified index. * @param index Index to get. diff --git a/src/newgrf_commons.h b/src/newgrf_commons.h index 13e0f9a053..49b3c0108b 100644 --- a/src/newgrf_commons.h +++ b/src/newgrf_commons.h @@ -294,6 +294,8 @@ struct GRFFilePropsBase { uint32_t grfid = 0; ///< grfid that introduced this entity. const struct GRFFile *grffile = nullptr; ///< grf file that introduced this entity + void SetGRFFile(const struct GRFFile *grffile); + /** * Test if this entity was introduced by NewGRF. * @returns true iff the grfid property is set. diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index fa02ecf4f2..196b3a93cb 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -71,8 +71,7 @@ void SetCustomEngineSprites(EngineID engine, CargoType cargo, const SpriteGroup void SetEngineGRF(EngineID engine, const GRFFile *file) { Engine *e = Engine::Get(engine); - e->grf_prop.grfid = file->grfid; - e->grf_prop.grffile = file; + e->grf_prop.SetGRFFile(file); }