src/newgrf_house.cpp
branchgamebalance
changeset 9911 0b8b245a2391
parent 9908 0fa543611bbe
child 9912 1ac8aac92385
--- a/src/newgrf_house.cpp	Wed Jun 13 11:17:30 2007 +0000
+++ b/src/newgrf_house.cpp	Wed Jun 13 11:45:14 2007 +0000
@@ -24,92 +24,12 @@
 #include "newgrf_callbacks.h"
 #include "newgrf_town.h"
 #include "newgrf_sound.h"
+#include "newgrf_commons.h"
 
 static BuildingCounts    _building_counts;
 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
-HouseIDMapping _house_id_mapping[HOUSE_MAX];
-
-/* Since the house IDs defined by the GRF file don't necessarily correlate
- * to those used by the game, the IDs used for overriding old houses must be
- * translated when the house spec is set. */
-static uint16 _house_overrides[NEW_HOUSE_OFFSET];
-
-void AddHouseOverride(uint8 local_id, uint house_type)
-{
-	assert(house_type < NEW_HOUSE_OFFSET);
-	_house_overrides[house_type] = local_id;
-}
-
-void ResetHouseOverrides()
-{
-	for (int i = 0; i != lengthof(_house_overrides); i++) {
-		_house_overrides[i] = INVALID_HOUSE_ID;
-	}
-}
-
-static HouseID GetHouseID(byte grf_local_id, uint32 grfid)
-{
-	const HouseIDMapping *map;
-
-	for (HouseID house_id = NEW_HOUSE_OFFSET; house_id != lengthof(_house_id_mapping); house_id++) {
-		map = &_house_id_mapping[house_id];
-		if (map->house_id == grf_local_id && map->grfid == grfid) return house_id;
-	}
-	return INVALID_HOUSE_ID;
-}
-
-static HouseID AddHouseID(byte grf_local_id, uint32 grfid, byte substitute_id)
-{
-	HouseID house_id;
-	HouseIDMapping *map;
 
-	/* Look to see if this house has already been added. This is done
-	 * separately from the loop below in case a GRF has been deleted, and there
-	 * are any gaps in the array. */
-	house_id = GetHouseID(grf_local_id, grfid);
-	if (house_id != INVALID_HOUSE_ID) return house_id;
-
-	/* This house hasn't been defined before, so give it an ID now. */
-	for (house_id = NEW_HOUSE_OFFSET; house_id != lengthof(_house_id_mapping); house_id++) {
-		map = &_house_id_mapping[house_id];
-
-		if (map->house_id == 0 && map->grfid == 0) {
-			map->house_id      = grf_local_id;
-			map->grfid         = grfid;
-			map->substitute_id = substitute_id;
-			return house_id;
-		}
-	}
-
-	return INVALID_HOUSE_ID;
-}
-
-void SetHouseSpec(const HouseSpec *hs)
-{
-	HouseID house_id = AddHouseID(hs->local_id, hs->grffile->grfid, hs->substitute_id);
-
-	if (house_id == INVALID_HOUSE_ID) {
-		grfmsg(1, "SetHouseSpec: Too many houses allocated. Ignoring.");
-		return;
-	}
-
-	memcpy(&_house_specs[house_id], hs, sizeof(*hs));
-
-	/* Now add the overrides. */
-	for (int i = 0; i != lengthof(_house_overrides); i++) {
-		HouseSpec *overridden_hs = GetHouseSpecs(i);
-
-		if (_house_overrides[i] != hs->local_id) continue;
-
-		overridden_hs->override = house_id;
-		_house_overrides[i] = INVALID_HOUSE_ID;
-	}
-}
-
-void ResetHouseIDMapping()
-{
-	memset(&_house_id_mapping, 0, sizeof(_house_id_mapping));
-}
+HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
 
 void CheckHouseIDs()
 {
@@ -122,7 +42,7 @@
 		if (!GetHouseSpecs(house_id)->enabled && house_id >= NEW_HOUSE_OFFSET) {
 			/* The specs for this type of house are not available any more, so
 			 * replace it with the substitute original house type. */
-			SetHouseType(t, _house_id_mapping[house_id].substitute_id);
+			SetHouseType(t, _house_mngr.GetSubstituteID(house_id));
 		}
 	}
 
@@ -313,7 +233,7 @@
 			const HouseSpec *hs = GetHouseSpecs(house_id);
 			if (hs->grffile == NULL) return 0;
 
-			HouseID new_house = GetHouseID(parameter, hs->grffile->grfid);
+			HouseID new_house = _house_mngr.GetID(parameter, hs->grffile->grfid);
 			return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
 		}
 
@@ -374,7 +294,7 @@
 	res->reseed          = 0;
 }
 
-uint16 GetHouseCallback(uint16 callback, uint32 param1, HouseID house_id, Town *town, TileIndex tile)
+uint16 GetHouseCallback(uint16 callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile)
 {
 	ResolverObject object;
 	const SpriteGroup *group;
@@ -382,7 +302,7 @@
 	NewHouseResolver(&object, house_id, tile, town);
 	object.callback = callback;
 	object.callback_param1 = param1;
-	object.callback_param2 = 0;
+	object.callback_param2 = param2;
 
 	group = Resolve(GetHouseSpecs(house_id)->spritegroup, &object);
 	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
@@ -413,7 +333,7 @@
 			if (pal == 0) {
 				const HouseSpec *hs = GetHouseSpecs(house_id);
 				if (HASBIT(hs->callback_mask, CBM_BUILDING_COLOUR)) {
-					uint16 callback = GetHouseCallback(CBID_BUILDING_COLOUR, 0, house_id, GetTownByTile(ti->tile), ti->tile);
+					uint16 callback = GetHouseCallback(CBID_BUILDING_COLOUR, 0, 0, house_id, GetTownByTile(ti->tile), ti->tile);
 					if (callback != CALLBACK_FAILED) {
 						/* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
 						pal = HASBIT(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
@@ -468,7 +388,7 @@
 	bool frame_set_by_callback = false;
 
 	if (HASBIT(hs->callback_mask, CBM_ANIMATION_SPEED)) {
-		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, GetHouseType(tile), GetTownByTile(tile), tile);
+		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
 		if (callback_res != CALLBACK_FAILED) animation_speed = clamp(callback_res & 0xFF, 2, 16);
 	}
 
@@ -483,7 +403,7 @@
 
 	if (HASBIT(hs->callback_mask, CBM_ANIMATION_NEXT_FRAME)) {
 		uint32 param = (hs->extra_flags & CALLBACK_1A_RANDOM_BITS) ? Random() : 0;
-		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, GetHouseType(tile), GetTownByTile(tile), tile);
+		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
 
 		if (callback_res != CALLBACK_FAILED) {
 			frame_set_by_callback = true;
@@ -549,7 +469,7 @@
 			|| _current_player == OWNER_WATER || _current_player == OWNER_NONE) return true;
 
 	if (HASBIT(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
-		uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, GetHouseType(tile), GetTownByTile(tile), tile);
+		uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
 		return (callback_res == CALLBACK_FAILED || callback_res == 0);
 	} else {
 		return !(hs->extra_flags & BUILDING_IS_PROTECTED);
@@ -562,7 +482,7 @@
 
 	if (HASBIT(hs->callback_mask, CBM_ANIMATION_START_STOP)) {
 		uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
-		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, GetHouseType(tile), GetTownByTile(tile), tile);
+		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
 
 		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(tile, callback_res);
 	}
@@ -577,7 +497,11 @@
 		return true;
 	}
 
-	/* @todo: Magic with triggers goes here.  Got to implement that, one day. .. */
+	/* @todo: Magic with triggers goes here.  Got to implement that, one day. ..
+	 * Process randomizing of tiles following specs.
+	 * Once done, redraw the house
+	 * MarkTileDirtyByTile(tile);
+	 */
 
 	if (HASBIT(hs->callback_mask, CBM_ANIMATION_START_STOP)) {
 		/* If this house is marked as having a synchronised callback, all the
@@ -598,7 +522,7 @@
 
 	/* Check callback 21, which determines if a house should be destroyed. */
 	if (HASBIT(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
-		uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, GetHouseType(tile), GetTownByTile(tile), tile);
+		uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
 		if (callback_res != CALLBACK_FAILED && callback_res > 0) {
 			ClearTownHouse(GetTownByTile(tile), tile);
 			return false;