src/newgrf_commons.cpp
branchNewGRF_ports
changeset 6871 5a9dc001e1ad
parent 6870 ca3fd1fbe311
child 6872 1c4a4a609f85
--- a/src/newgrf_commons.cpp	Sat Oct 06 21:16:00 2007 +0000
+++ b/src/newgrf_commons.cpp	Mon Dec 03 23:39:38 2007 +0000
@@ -27,6 +27,7 @@
 	mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
 	entity_overrides = MallocT<uint16>(max_offset);
 	memset(entity_overrides, invalid, sizeof(entity_overrides));
+	grfid_overrides = CallocT<uint32>(max_offset);
 }
 
 /** Destructor of the generic class.
@@ -36,18 +37,23 @@
 {
 	free(mapping_ID);
 	free(entity_overrides);
+	free(grfid_overrides);
 }
 
 /** Since the entity IDs defined by the GRF file does not necessarily correlate
  * to those used by the game, the IDs used for overriding old entities must be
  * translated when the entity spec is set.
- * @param local_id id in grf file
+ * @param local_id ID in grf file
+ * @param grfid  ID of the grf file
  * @param entity_type original entity type
  */
-void OverrideManagerBase::Add(uint8 local_id, uint entity_type)
+void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
 {
 	assert(entity_type < max_offset);
+	/* An override can be set only once */
+	if (entity_overrides[entity_type] != invalid_ID) return;
 	entity_overrides[entity_type] = local_id;
+	grfid_overrides[entity_type] = grfid;
 }
 
 /** Resets the mapping, which is used while initializing game */
@@ -61,6 +67,7 @@
 {
 	for (uint16 i = 0; i < max_offset; i++) {
 		entity_overrides[i] = invalid_ID;
+		grfid_overrides[i] = 0;
 	}
 }
 
@@ -79,6 +86,12 @@
 			return id;
 		}
 	}
+
+	/* No mapping found, try the overrides */
+	for (uint16 id = 0; id < max_offset; id++) {
+		if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
+	}
+
 	return invalid_ID;
 }
 
@@ -144,10 +157,11 @@
 	for (int i = 0; i != max_offset; i++) {
 		HouseSpec *overridden_hs = GetHouseSpecs(i);
 
-		if (entity_overrides[i] != hs->local_id) continue;
+		if (entity_overrides[i] != hs->local_id || grfid_overrides[i] != hs->grffile->grfid) continue;
 
 		overridden_hs->override = house_id;
 		entity_overrides[i] = invalid_ID;
+		grfid_overrides[i] = 0;
 	}
 }
 
@@ -161,6 +175,9 @@
 {
 	/* This entity hasn't been defined before, so give it an ID now. */
 	for (uint16 id = 0; id < max_new_entities; id++) {
+		/* Skip overriden industries */
+		if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
+
 		/* Get the real live industry */
 		const IndustrySpec *inds = GetIndustrySpec(id);
 
@@ -193,21 +210,13 @@
 	/* First step : We need to find if this industry is already specified in the savegame data */
 	IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
 
-	if (ind_id == invalid_ID) { // not found?  So this is the introduction of a new industry
-		/* Second step is dealing with the override. */
-		if (inds->grf_prop.override != invalid_ID && _industry_specs[inds->grf_prop.override].grf_prop.override == invalid_ID) {
-			/* this is an override, which means it will take the place of the industry it is
-			 * designed to replace. Before we conclude that the override is allowed,
-			* we first need to verify that the slot is not holding another override
-			* If it's the case,it will be considered as a normal substitute */
-			ind_id = inds->grf_prop.override;
-		} else {
-			/* It has already been overriden, so you've lost your place old boy.
-			 * Or it is a simple substitute.
-			 * In both case, we need to find a free available slot */
-			ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
-			inds->grf_prop.override = invalid_ID;  // make sure it will not be detected as overriden
-		}
+	if (ind_id == invalid_ID) {
+		/* Not found.
+		 * Or it has already been overriden, so you've lost your place old boy.
+		 * Or it is a simple substitute.
+		 * We need to find a free available slot */
+		ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
+		inds->grf_prop.override = invalid_ID;  // make sure it will not be detected as overriden
 	}
 
 	if (ind_id == invalid_ID) {
@@ -236,11 +245,12 @@
 	for (int i = 0; i < max_offset; i++) {
 		IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
 
-		if (entity_overrides[i] != its->grf_prop.local_id) continue;
+		if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
 
 		overridden_its->grf_prop.override = indt_id;
 		overridden_its->enabled = false;
 		entity_overrides[i] = invalid_ID;
+		grfid_overrides[i] = 0;
 	}
 }