summaryrefslogtreecommitdiff
path: root/src/newgrf.cpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2009-03-08 16:51:08 +0000
committerfrosch <frosch@openttd.org>2009-03-08 16:51:08 +0000
commit987e72f4f2d89da1575b0c55eccdaff0d1ad4f99 (patch)
treed0b5e144e2002e62ecfeb6cffe5e5bf0090a2aea /src/newgrf.cpp
parent4a5c4f6089b344c4e385a1f1e4d26545f46f4984 (diff)
downloadopenttd-987e72f4f2d89da1575b0c55eccdaff0d1ad4f99.tar.xz
(svn r15645) -Fix (r12924)[FS#2612]: Add an EngineOverrideManager to give the term 'compatible newgrf' again some sense and to not crash because of trivial changes.
Diffstat (limited to 'src/newgrf.cpp')
-rw-r--r--src/newgrf.cpp54
1 files changed, 31 insertions, 23 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index dc507ecda..d85fd610d 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -328,39 +328,57 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16 intern
{
/* Hack for add-on GRFs that need to modify another GRF's engines. This lets
* them use the same engine slots. */
- const GRFFile *grf_match = NULL;
+ uint32 scope_grfid = INVALID_GRFID; // If not using dynamic_engines, all newgrfs share their ID range
if (_settings_game.vehicle.dynamic_engines) {
+ /* If dynamic_engies is enabled, there can be multiple independent ID ranges. */
+ scope_grfid = file->grfid;
uint32 override = _grf_id_overrides[file->grfid];
if (override != 0) {
- grf_match = GetFileByGRFID(override);
+ scope_grfid = override;
+ const GRFFile *grf_match = GetFileByGRFID(override);
if (grf_match == NULL) {
grfmsg(5, "Tried mapping from GRFID %x to %x but target is not loaded", BSWAP32(file->grfid), BSWAP32(override));
} else {
grfmsg(5, "Mapping from GRFID %x to %x", BSWAP32(file->grfid), BSWAP32(override));
}
}
+
+ /* Check if the engine is registered in the override manager */
+ EngineID engine = _engine_mngr.GetID(type, internal_id, scope_grfid);
+ if (engine != INVALID_ENGINE) return GetEngine(engine);
}
- /* Check if this vehicle is already defined... */
- Engine *e = NULL;
- FOR_ALL_ENGINES(e) {
- if (_settings_game.vehicle.dynamic_engines && e->grffile != NULL && e->grffile != file && e->grffile != grf_match) continue;
- if (e->type != type) continue;
- if (e->internal_id != internal_id) continue;
+ /* Check if there is an unreserved slot */
+ EngineID engine = _engine_mngr.GetID(type, internal_id, INVALID_GRFID);
+ if (engine != INVALID_ENGINE) {
+ Engine *e = GetEngine(engine);
if (e->grffile == NULL) {
e->grffile = file;
grfmsg(5, "Replaced engine at index %d for GRFID %x, type %d, index %d", e->index, BSWAP32(file->grfid), type, internal_id);
}
+
+ /* Reserve the engine slot */
+ EngineIDMapping *eid = _engine_mngr.Get(engine);
+ eid->grfid = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
+
return e;
}
uint engine_pool_size = GetEnginePoolSize();
/* ... it's not, so create a new one based off an existing engine */
- e = new Engine(type, internal_id);
+ Engine *e = new Engine(type, internal_id);
e->grffile = file;
+ /* Reserve the engine slot */
+ assert(_engine_mngr.Length() == e->index);
+ EngineIDMapping *eid = _engine_mngr.Append();
+ eid->type = type;
+ eid->grfid = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
+ eid->internal_id = internal_id;
+ eid->substitute_id = min(internal_id, _engine_counts[type]); // substitute_id == _engine_counts[subtype] means "no substitute"
+
if (engine_pool_size != GetEnginePoolSize()) {
/* Resize temporary engine data ... */
_gted = ReallocT(_gted, GetEnginePoolSize());
@@ -377,24 +395,14 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16 intern
EngineID GetNewEngineID(const GRFFile *file, VehicleType type, uint16 internal_id)
{
- extern uint32 GetNewGRFOverride(uint32 grfid);
-
- const GRFFile *grf_match = NULL;
+ uint32 scope_grfid = INVALID_GRFID; // If not using dynamic_engines, all newgrfs share their ID range
if (_settings_game.vehicle.dynamic_engines) {
+ scope_grfid = file->grfid;
uint32 override = _grf_id_overrides[file->grfid];
- if (override != 0) grf_match = GetFileByGRFID(override);
- }
-
- const Engine *e = NULL;
- FOR_ALL_ENGINES(e) {
- if (_settings_game.vehicle.dynamic_engines && e->grffile != file && (grf_match == NULL || e->grffile != grf_match)) continue;
- if (e->type != type) continue;
- if (e->internal_id != internal_id) continue;
-
- return e->index;
+ if (override != 0) scope_grfid = override;
}
- return INVALID_ENGINE;
+ return _engine_mngr.GetID(type, internal_id, scope_grfid);
}
/** Map the colour modifiers of TTDPatch to those that Open is using.