src/newgrf.cpp
branchgamebalance
changeset 9896 2473804114de
parent 9895 7bd07f43b0e3
child 9903 dc85aaa556ae
equal deleted inserted replaced
9895:7bd07f43b0e3 9896:2473804114de
    16 #include "newgrf.h"
    16 #include "newgrf.h"
    17 #include "variables.h"
    17 #include "variables.h"
    18 #include "string.h"
    18 #include "string.h"
    19 #include "table/strings.h"
    19 #include "table/strings.h"
    20 #include "bridge.h"
    20 #include "bridge.h"
       
    21 #include "town.h"
    21 #include "economy.h"
    22 #include "economy.h"
    22 #include "newgrf_engine.h"
    23 #include "newgrf_engine.h"
    23 #include "vehicle.h"
    24 #include "vehicle.h"
    24 #include "newgrf_text.h"
    25 #include "newgrf_text.h"
    25 #include "table/sprites.h"
    26 #include "table/sprites.h"
    26 #include "fontcache.h"
    27 #include "fontcache.h"
    27 #include "date.h"
    28 #include "date.h"
    28 #include "currency.h"
    29 #include "currency.h"
    29 #include "sound.h"
    30 #include "sound.h"
    30 #include "newgrf_config.h"
    31 #include "newgrf_config.h"
       
    32 #include "newgrf_house.h"
    31 #include "newgrf_sound.h"
    33 #include "newgrf_sound.h"
    32 #include "newgrf_spritegroup.h"
    34 #include "newgrf_spritegroup.h"
    33 #include "helpers.hpp"
    35 #include "helpers.hpp"
       
    36 #include "table/town_land.h"
    34 #include "cargotype.h"
    37 #include "cargotype.h"
    35 
    38 
    36 /* TTDPatch extended GRF format codec
    39 /* TTDPatch extended GRF format codec
    37  * (c) Petr Baudis 2004 (GPL'd)
    40  * (c) Petr Baudis 2004 (GPL'd)
    38  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    41  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
    65 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    68 /* Used by Action 0x06 to preload a pseudo sprite and modify its content */
    66 static byte *_preload_sprite = NULL;
    69 static byte *_preload_sprite = NULL;
    67 
    70 
    68 /* Set if any vehicle is loaded which uses 2cc (two company colours) */
    71 /* Set if any vehicle is loaded which uses 2cc (two company colours) */
    69 bool _have_2cc = false;
    72 bool _have_2cc = false;
       
    73 
       
    74 /* Set if there are any newhouses loaded. */
       
    75 bool _have_newhouses = false;
    70 
    76 
    71 /* Default cargo translation table. By default there are 27 possible cargo types */
    77 /* Default cargo translation table. By default there are 27 possible cargo types */
    72 static const uint _default_cargo_max = 27;
    78 static const uint _default_cargo_max = 27;
    73 static CargoLabel _default_cargo_list[_default_cargo_max];
    79 static CargoLabel _default_cargo_list[_default_cargo_max];
    74 
    80 
   225 	}
   231 	}
   226 	return file;
   232 	return file;
   227 }
   233 }
   228 
   234 
   229 
   235 
       
   236 /** Used when setting an object's property to map to the GRF's strings
       
   237  * while taking in consideration the "drift" between TTDPatch string system and OpenTTD's one
       
   238  * @param str StringID that we want to have the equivalent in OoenTTD
       
   239  * @return the properly adjusted StringID
       
   240  */
       
   241 static StringID MapGRFStringID(StringID str)
       
   242 {
       
   243 	/* 0xD0 and 0xDC stand for all the TextIDs in the range
       
   244 	*  of 0xD000 (misc graphics texts) and 0xDC00 (misc persistent texts).
       
   245 	 * These strings are unique to each grf file, and thus require to be used with the
       
   246 	 * grfid in which they are declared */
       
   247 	if (GB(str, 8, 8) == 0xD0 || GB(str, 8, 8) == 0xDC) {
       
   248 		return GetGRFStringID(_cur_grffile->grfid, str);
       
   249 	}
       
   250 
       
   251 	/* Map building names according to our lang file changes
       
   252 	 * 0x200F = Tall Office Block, first house name in the original data, the one that TTDPatch stil uses
       
   253 	 * 0x201F = Old houses is the last house name.
       
   254 	 * OpenTTD does not have exactly the same order aymore, so, the code below allows
       
   255 	 * to compensate for the difference */
       
   256 	if (str >= 0x200F && str <= 0x201F) return str + (STR_200F_TALL_OFFICE_BLOCK - 0x200F);
       
   257 
       
   258 	return str;
       
   259 }
       
   260 
       
   261 
   230 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   262 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   231 
   263 
   232 #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++)
   264 #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++)
   233 
   265 
   234 static void dewagonize(int condition, int engine)
   266 static void dewagonize(int condition, int engine)
  1166 
  1198 
  1167 	*bufp = buf;
  1199 	*bufp = buf;
  1168 	return ret;
  1200 	return ret;
  1169 }
  1201 }
  1170 
  1202 
       
  1203 static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
       
  1204 {
       
  1205 	HouseSpec **housespec;
       
  1206 	byte *buf = *bufp;
       
  1207 	int i;
       
  1208 	bool ret = false;
       
  1209 
       
  1210 	if (hid + numinfo >= HOUSE_MAX) {
       
  1211 		grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX-1);
       
  1212 		return false;
       
  1213 	}
       
  1214 
       
  1215 	/* Allocate house specs if they haven't been allocated already. */
       
  1216 	if (_cur_grffile->housespec == NULL) {
       
  1217 		_cur_grffile->housespec = CallocT<HouseSpec*>(HOUSE_MAX);
       
  1218 
       
  1219 		/* Reset any overrides that have been set. */
       
  1220 		ResetHouseOverrides();
       
  1221 	}
       
  1222 
       
  1223 	housespec = &_cur_grffile->housespec[hid];
       
  1224 
       
  1225 	if (prop != 0x08) {
       
  1226 		/* Check that all the houses being modified have been defined. */
       
  1227 		FOR_EACH_OBJECT {
       
  1228 			if (housespec[i] == NULL) {
       
  1229 				grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
       
  1230 				return false;
       
  1231 			}
       
  1232 		}
       
  1233 	}
       
  1234 
       
  1235 	switch (prop) {
       
  1236 		case 0x08: // Substitute building type, and definition of a new house
       
  1237 			FOR_EACH_OBJECT {
       
  1238 				byte subs_id = grf_load_byte(&buf);
       
  1239 
       
  1240 				if (subs_id == 0xFF) {
       
  1241 					/* Instead of defining a new house, a substitute house id
       
  1242 					 * of 0xFF disables the old house with the current id. */
       
  1243 					_house_specs[hid + i].enabled = false;
       
  1244 					continue;
       
  1245 				} else if (subs_id >= NEW_HOUSE_OFFSET) {
       
  1246 					/* The substitute id must be one of the original houses. */
       
  1247 					grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
       
  1248 					return false;
       
  1249 				}
       
  1250 
       
  1251 				/* Allocate space for this house. */
       
  1252 				if (housespec[i] == NULL) housespec[i] = CallocT<HouseSpec>(1);
       
  1253 
       
  1254 				memcpy(housespec[i], &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
       
  1255 
       
  1256 				housespec[i]->enabled = true;
       
  1257 				housespec[i]->local_id = hid + i;
       
  1258 				housespec[i]->substitute_id = subs_id;
       
  1259 				housespec[i]->random_colour[0] = 0x04;  // those 4 random colours are the base colour
       
  1260 				housespec[i]->random_colour[1] = 0x08;  // for all new houses
       
  1261 				housespec[i]->random_colour[2] = 0x0C;  // they stand for red, blue, orange and green
       
  1262 				housespec[i]->random_colour[3] = 0x06;
       
  1263 
       
  1264 				/* New houses do not (currently) expect to have a default start
       
  1265 				 * date before 1930, as this breaks the build date stuff. See
       
  1266 				 * FinaliseHouseArray() for more details. */
       
  1267 				if (housespec[i]->min_date < 1930) housespec[i]->min_date = 1930;
       
  1268 			}
       
  1269 			_have_newhouses = true;
       
  1270 			break;
       
  1271 
       
  1272 		case 0x09: // Building flags
       
  1273 			FOR_EACH_OBJECT {
       
  1274 				byte state = grf_load_byte(&buf);
       
  1275 				housespec[i]->building_flags = (BuildingFlags)state;
       
  1276 			}
       
  1277 			break;
       
  1278 
       
  1279 		case 0x0A: // Availability years
       
  1280 			FOR_EACH_OBJECT {
       
  1281 				uint16 years = grf_load_word(&buf);
       
  1282 				housespec[i]->min_date = GB(years, 0, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 0, 8);
       
  1283 				housespec[i]->max_date = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
       
  1284 			}
       
  1285 			break;
       
  1286 
       
  1287 		case 0x0B: // Population
       
  1288 			FOR_EACH_OBJECT housespec[i]->population = grf_load_byte(&buf);
       
  1289 			break;
       
  1290 
       
  1291 		case 0x0C: // Mail generation multiplier
       
  1292 			FOR_EACH_OBJECT housespec[i]->mail_generation = grf_load_byte(&buf);
       
  1293 			break;
       
  1294 
       
  1295 		case 0x0D: // Passenger acceptance
       
  1296 			FOR_EACH_OBJECT housespec[i]->passenger_acceptance = grf_load_byte(&buf);
       
  1297 			break;
       
  1298 
       
  1299 		case 0x0E: // Mail acceptance
       
  1300 			FOR_EACH_OBJECT housespec[i]->mail_acceptance = grf_load_byte(&buf);
       
  1301 			break;
       
  1302 
       
  1303 		case 0x0F: // Goods, food or fizzy drinks acceptance
       
  1304 			FOR_EACH_OBJECT {
       
  1305 				int8 goods = grf_load_byte(&buf);
       
  1306 				if (goods > 0) {
       
  1307 					housespec[i]->goods_acceptance = goods;
       
  1308 				} else {
       
  1309 					housespec[i]->food_acceptance = -goods;
       
  1310 				}
       
  1311 			}
       
  1312 			break;
       
  1313 
       
  1314 		case 0x10: // Local authority rating decrease on removal
       
  1315 			FOR_EACH_OBJECT housespec[i]->remove_rating_decrease = grf_load_word(&buf);
       
  1316 			break;
       
  1317 
       
  1318 		case 0x11: // Removal cost multiplier
       
  1319 			FOR_EACH_OBJECT housespec[i]->removal_cost = grf_load_byte(&buf);
       
  1320 			break;
       
  1321 
       
  1322 		case 0x12: // Building name ID
       
  1323 			FOR_EACH_OBJECT housespec[i]->building_name = MapGRFStringID(grf_load_word(&buf));
       
  1324 			break;
       
  1325 
       
  1326 		case 0x13: // Building availability mask
       
  1327 			FOR_EACH_OBJECT {
       
  1328 				uint16 avail = grf_load_word(&buf);
       
  1329 				housespec[i]->building_availability = (HouseZones)avail;
       
  1330 			}
       
  1331 			break;
       
  1332 
       
  1333 		case 0x14: // House callback flags
       
  1334 			FOR_EACH_OBJECT housespec[i]->callback_mask = grf_load_byte(&buf);
       
  1335 			break;
       
  1336 
       
  1337 		case 0x15: // House override byte
       
  1338 			FOR_EACH_OBJECT {
       
  1339 				byte override = grf_load_byte(&buf);
       
  1340 
       
  1341 				/* The house being overridden must be an original house. */
       
  1342 				if (override >= NEW_HOUSE_OFFSET) {
       
  1343 					grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid);
       
  1344 					return false;
       
  1345 				}
       
  1346 
       
  1347 				AddHouseOverride(hid, override);
       
  1348 			}
       
  1349 			break;
       
  1350 
       
  1351 		case 0x16: // Periodic refresh multiplier
       
  1352 			FOR_EACH_OBJECT housespec[i]->processing_time = grf_load_byte(&buf);
       
  1353 			break;
       
  1354 
       
  1355 		case 0x17: // Four random colours to use
       
  1356 			FOR_EACH_OBJECT {
       
  1357 				uint j;
       
  1358 				for (j = 0; j < 4; j++)	housespec[i]->random_colour[j] = grf_load_byte(&buf);
       
  1359 			}
       
  1360 			break;
       
  1361 
       
  1362 		case 0x18: // Relative probability of appearing
       
  1363 			FOR_EACH_OBJECT housespec[i]->probability = grf_load_byte(&buf);
       
  1364 			break;
       
  1365 
       
  1366 		case 0x19: // Extra flags
       
  1367 			FOR_EACH_OBJECT {
       
  1368 				byte flags = grf_load_byte(&buf);
       
  1369 				housespec[i]->extra_flags = (HouseExtraFlags)flags;
       
  1370 			}
       
  1371 			break;
       
  1372 
       
  1373 		case 0x1A: // Animation frames
       
  1374 			FOR_EACH_OBJECT housespec[i]->animation_frames = grf_load_byte(&buf);
       
  1375 			break;
       
  1376 
       
  1377 		case 0x1B: // Animation speed
       
  1378 			FOR_EACH_OBJECT housespec[i]->animation_speed = clamp(grf_load_byte(&buf), 2, 16);
       
  1379 			break;
       
  1380 
       
  1381 		case 0x1C: // Class of the building type
       
  1382 			FOR_EACH_OBJECT housespec[i]->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
       
  1383 			break;
       
  1384 
       
  1385 		case 0x1D: // Callback flags 2
       
  1386 			FOR_EACH_OBJECT housespec[i]->callback_mask |= (grf_load_byte(&buf) << 8);
       
  1387 			break;
       
  1388 
       
  1389 		case 0x1E: // Accepted cargo types
       
  1390 			FOR_EACH_OBJECT grf_load_dword(&buf);
       
  1391 			ret = true;
       
  1392 			break;
       
  1393 
       
  1394 		default:
       
  1395 			ret = true;
       
  1396 			break;
       
  1397 	}
       
  1398 
       
  1399 	*bufp = buf;
       
  1400 	return ret;
       
  1401 }
       
  1402 
  1171 static bool GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
  1403 static bool GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
  1172 {
  1404 {
  1173 	byte *buf = *bufp;
  1405 	byte *buf = *bufp;
  1174 	int i;
  1406 	int i;
  1175 	bool ret = false;
  1407 	bool ret = false;
  1373 		/* GSF_SHIP */         ShipVehicleChangeInfo,
  1605 		/* GSF_SHIP */         ShipVehicleChangeInfo,
  1374 		/* GSF_AIRCRAFT */     AircraftVehicleChangeInfo,
  1606 		/* GSF_AIRCRAFT */     AircraftVehicleChangeInfo,
  1375 		/* GSF_STATION */      StationChangeInfo,
  1607 		/* GSF_STATION */      StationChangeInfo,
  1376 		/* GSF_CANAL */        NULL,
  1608 		/* GSF_CANAL */        NULL,
  1377 		/* GSF_BRIDGE */       BridgeChangeInfo,
  1609 		/* GSF_BRIDGE */       BridgeChangeInfo,
  1378 		/* GSF_TOWNHOUSE */    NULL,
  1610 		/* GSF_TOWNHOUSE */    TownHouseChangeInfo,
  1379 		/* GSF_GLOBALVAR */    GlobalVarChangeInfo,
  1611 		/* GSF_GLOBALVAR */    GlobalVarChangeInfo,
  1380 		/* GSF_INDUSTRYTILES */NULL,
  1612 		/* GSF_INDUSTRYTILES */NULL,
  1381 		/* GSF_INDUSTRIES */   NULL,
  1613 		/* GSF_INDUSTRIES */   NULL,
  1382 		/* GSF_CARGOS */       NULL,
  1614 		/* GSF_CARGOS */       NULL,
  1383 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1615 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  1847 					for (uint i = 0; i < num_loading; i++) {
  2079 					for (uint i = 0; i < num_loading; i++) {
  1848 						uint16 spriteid = grf_load_word(&buf);
  2080 						uint16 spriteid = grf_load_word(&buf);
  1849 						group->g.real.loading[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
  2081 						group->g.real.loading[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
  1850 						grfmsg(8, "NewSpriteGroup: + rg->loading[%i] = subset %u", i, spriteid);
  2082 						grfmsg(8, "NewSpriteGroup: + rg->loading[%i] = subset %u", i, spriteid);
  1851 					}
  2083 					}
       
  2084 
       
  2085 					break;
       
  2086 				}
       
  2087 
       
  2088 				case GSF_TOWNHOUSE: {
       
  2089 					byte sprites     = _cur_grffile->spriteset_numents;
       
  2090 					byte num_sprites = max((uint8)1, type);
       
  2091 					uint i;
       
  2092 
       
  2093 					group = AllocateSpriteGroup();
       
  2094 					group->type = SGT_TILELAYOUT;
       
  2095 					group->g.layout.num_sprites = sprites;
       
  2096 					group->g.layout.dts = CallocT<DrawTileSprites>(1);
       
  2097 
       
  2098 					/* Groundsprite */
       
  2099 					group->g.layout.dts->ground_sprite = grf_load_word(&buf);
       
  2100 					group->g.layout.dts->ground_pal    = grf_load_word(&buf);
       
  2101 					/* Remap transparent/colour modifier bits */
       
  2102 					if (HASBIT(group->g.layout.dts->ground_sprite, 14)) {
       
  2103 						CLRBIT(group->g.layout.dts->ground_sprite, 14);
       
  2104 						SETBIT(group->g.layout.dts->ground_sprite, PALETTE_MODIFIER_TRANSPARENT);
       
  2105 					}
       
  2106 					if (HASBIT(group->g.layout.dts->ground_sprite, 15)) {
       
  2107 						CLRBIT(group->g.layout.dts->ground_sprite, 15);
       
  2108 						SETBIT(group->g.layout.dts->ground_sprite, PALETTE_MODIFIER_COLOR);
       
  2109 					}
       
  2110 					if (HASBIT(group->g.layout.dts->ground_pal, 14)) {
       
  2111 						CLRBIT(group->g.layout.dts->ground_pal, 14);
       
  2112 						SETBIT(group->g.layout.dts->ground_sprite, SPRITE_MODIFIER_OPAQUE);
       
  2113 					}
       
  2114 					if (HASBIT(group->g.layout.dts->ground_pal, 15)) {
       
  2115 						/* Bit 31 set means this is a custom sprite, so rewrite it to the
       
  2116 						 * last spriteset defined. */
       
  2117 						SpriteID sprite = _cur_grffile->spriteset_start + GB(group->g.layout.dts->ground_sprite, 0, 14) * sprites;
       
  2118 						SB(group->g.layout.dts->ground_sprite, 0, SPRITE_WIDTH, sprite);
       
  2119 						CLRBIT(group->g.layout.dts->ground_pal, 15);
       
  2120 					}
       
  2121 
       
  2122 					group->g.layout.dts->seq = CallocT<DrawTileSeqStruct>(num_sprites + 1);
       
  2123 
       
  2124 					for (i = 0; i < num_sprites; i++) {
       
  2125 						DrawTileSeqStruct *seq = (DrawTileSeqStruct*)&group->g.layout.dts->seq[i];
       
  2126 
       
  2127 						seq->image = grf_load_word(&buf);
       
  2128 						seq->pal   = grf_load_word(&buf);
       
  2129 						seq->delta_x = grf_load_byte(&buf);
       
  2130 						seq->delta_y = grf_load_byte(&buf);
       
  2131 
       
  2132 						if (HASBIT(seq->image, 14)) {
       
  2133 							CLRBIT(seq->image, 14);
       
  2134 							SETBIT(seq->image, PALETTE_MODIFIER_TRANSPARENT);
       
  2135 						}
       
  2136 						if (HASBIT(seq->image, 15)) {
       
  2137 							CLRBIT(seq->image, 15);
       
  2138 							SETBIT(seq->image, PALETTE_MODIFIER_COLOR);
       
  2139 						}
       
  2140 						if (HASBIT(seq->pal, 14)) {
       
  2141 							CLRBIT(seq->pal, 14);
       
  2142 							SETBIT(seq->image, SPRITE_MODIFIER_OPAQUE);
       
  2143 						}
       
  2144 						if (HASBIT(seq->pal, 15)) {
       
  2145 							/* Bit 31 set means this is a custom sprite, so rewrite it to the
       
  2146 							 * last spriteset defined. */
       
  2147 							SpriteID sprite = _cur_grffile->spriteset_start + GB(seq->image, 0, 14) * sprites;
       
  2148 							SB(seq->image, 0, SPRITE_WIDTH, sprite);
       
  2149 							CLRBIT(seq->pal, 15);
       
  2150 						}
       
  2151 
       
  2152 						if (type > 0) {
       
  2153 							seq->delta_z = grf_load_byte(&buf);
       
  2154 							if ((byte)seq->delta_z == 0x80) continue;
       
  2155 						}
       
  2156 
       
  2157 						seq->size_x = grf_load_byte(&buf);
       
  2158 						seq->size_y = grf_load_byte(&buf);
       
  2159 						seq->size_z = grf_load_byte(&buf);
       
  2160 					}
       
  2161 
       
  2162 					/* Set the terminator value. */
       
  2163 					((DrawTileSeqStruct*)group->g.layout.dts->seq)[i].delta_x = (byte)0x80;
  1852 
  2164 
  1853 					break;
  2165 					break;
  1854 				}
  2166 				}
  1855 
  2167 
  1856 				/* Loading of Tile Layout and Production Callback groups would happen here */
  2168 				/* Loading of Tile Layout and Production Callback groups would happen here */
  1932 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  2244 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  1933 
  2245 
  1934 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  2246 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  1935 			feature, idcount, cidcount, wagover);
  2247 			feature, idcount, cidcount, wagover);
  1936 
  2248 
  1937 	if (feature > GSF_STATION) {
  2249 	if (feature > GSF_STATION && feature != GSF_TOWNHOUSE) {
  1938 		grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
  2250 		grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
  1939 		return;
  2251 		return;
  1940 	}
  2252 	}
  1941 
  2253 
  1942 
  2254 
  1982 				statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
  2294 				statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
  1983 				statspec->grfid = _cur_grffile->grfid;
  2295 				statspec->grfid = _cur_grffile->grfid;
  1984 				statspec->localidx = stid;
  2296 				statspec->localidx = stid;
  1985 				SetCustomStationSpec(statspec);
  2297 				SetCustomStationSpec(statspec);
  1986 			}
  2298 			}
       
  2299 		}
       
  2300 		return;
       
  2301 	} else if (feature == GSF_TOWNHOUSE) {
       
  2302 		byte *bp = &buf[4 + idcount + cidcount * 3];
       
  2303 		uint16 groupid = grf_load_word(&bp);
       
  2304 
       
  2305 		if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
       
  2306 			grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.",
       
  2307 			       groupid, _cur_grffile->spritegroups_count);
       
  2308 			return;
       
  2309 		}
       
  2310 
       
  2311 		for (uint i = 0; i < idcount; i++) {
       
  2312 			uint8 hid = buf[3 + i];
       
  2313 			HouseSpec *hs = _cur_grffile->housespec[hid];
       
  2314 
       
  2315 			if (hs == NULL) {
       
  2316 				grfmsg(1, "FeatureMapSpriteGroup: Too many houses defined, skipping");
       
  2317 				return;
       
  2318 			}
       
  2319 
       
  2320 			hs->spritegroup = _cur_grffile->spritegroups[groupid];
       
  2321 			hs->grffile = _cur_grffile;
  1987 		}
  2322 		}
  1988 		return;
  2323 		return;
  1989 	}
  2324 	}
  1990 
  2325 
  1991 	// FIXME: Tropicset contains things like:
  2326 	// FIXME: Tropicset contains things like:
  2146 						AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
  2481 						AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
  2147 					}
  2482 					}
  2148 					break;
  2483 					break;
  2149 				}
  2484 				}
  2150 
  2485 
       
  2486 				case GSF_TOWNHOUSE:
  2151 				default:
  2487 				default:
  2152 					switch (GB(id, 8, 8)) {
  2488 					switch (GB(id, 8, 8)) {
  2153 						case 0xC4: /* Station class name */
  2489 						case 0xC4: /* Station class name */
  2154 							if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  2490 							if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
  2155 								grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
  2491 								grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
  2165 							} else {
  2501 							} else {
  2166 								_cur_grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2502 								_cur_grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2167 							}
  2503 							}
  2168 							break;
  2504 							break;
  2169 
  2505 
  2170 						case 0xC9:
  2506 						case 0xC9: { /* House name */
       
  2507 							if (_cur_grffile->housespec == NULL || _cur_grffile->housespec[GB(id, 0, 8)] == NULL) {
       
  2508 								grfmsg(1, "FeatureNewName: Attempt to name undefined house 0x%X, ignoring.", GB(id, 0, 8));
       
  2509 							} else {
       
  2510 								_cur_grffile->housespec[GB(id, 0, 8)]->building_name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
       
  2511 							}
       
  2512 							break;
       
  2513 						}
       
  2514 
  2171 						case 0xD0:
  2515 						case 0xD0:
  2172 						case 0xDC:
  2516 						case 0xDC:
  2173 							AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2517 							AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
  2174 							break;
  2518 							break;
  2175 
  2519 
  2180 					break;
  2524 					break;
  2181 
  2525 
  2182 #if 0
  2526 #if 0
  2183 				case GSF_CANAL :
  2527 				case GSF_CANAL :
  2184 				case GSF_BRIDGE :
  2528 				case GSF_BRIDGE :
  2185 				case GSF_TOWNHOUSE :
       
  2186 					AddGRFString(_cur_spriteid, id, lang, name);
  2529 					AddGRFString(_cur_spriteid, id, lang, name);
  2187 					switch (GB(id, 8,8)) {
  2530 					switch (GB(id, 8,8)) {
  2188 						case 0xC9: /* House name */
  2531 						case 0xC9: /* House name */
  2189 						default:
  2532 						default:
  2190 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  2533 							grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
  3476 	                   |                                        (0 << 0x14)  // manualconvert
  3819 	                   |                                        (0 << 0x14)  // manualconvert
  3477 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x15)  // buildoncoasts
  3820 	                   |       ((_patches.build_on_slopes ? 1 : 0) << 0x15)  // buildoncoasts
  3478 	                   |                                        (1 << 0x16)  // canals
  3821 	                   |                                        (1 << 0x16)  // canals
  3479 	                   |                                        (1 << 0x17)  // newstartyear
  3822 	                   |                                        (1 << 0x17)  // newstartyear
  3480 	                   |                                        (0 << 0x18)  // freighttrains
  3823 	                   |                                        (0 << 0x18)  // freighttrains
  3481 	                   |                                        (0 << 0x19)  // newhouses
  3824 	                   |                                        (1 << 0x19)  // newhouses
  3482 	                   |                                        (1 << 0x1A)  // newbridges
  3825 	                   |                                        (1 << 0x1A)  // newbridges
  3483 	                   |                                        (0 << 0x1B)  // newtownnames
  3826 	                   |                                        (0 << 0x1B)  // newtownnames
  3484 	                   |                                        (0 << 0x1C)  // moreanimations
  3827 	                   |                                        (0 << 0x1C)  // moreanimations
  3485 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  3828 	                   |    ((_patches.wagon_speed_limits ? 1 : 0) << 0x1D)  // wagonspeedlimits
  3486 	                   |                                        (1 << 0x1E)  // newshistory
  3829 	                   |                                        (1 << 0x1E)  // newshistory
  3548 		free(file->stations);
  3891 		free(file->stations);
  3549 		file->stations = NULL;
  3892 		file->stations = NULL;
  3550 	}
  3893 	}
  3551 }
  3894 }
  3552 
  3895 
       
  3896 static void ResetCustomHouses()
       
  3897 {
       
  3898 	GRFFile *file;
       
  3899 	uint i;
       
  3900 
       
  3901 	for (file = _first_grffile; file != NULL; file = file->next) {
       
  3902 		if (file->housespec == NULL) continue;
       
  3903 		for (i = 0; i < HOUSE_MAX; i++) free(file->housespec[i]);
       
  3904 
       
  3905 		free(file->housespec);
       
  3906 		file->housespec = NULL;
       
  3907 	}
       
  3908 }
       
  3909 
  3553 static void ResetNewGRF()
  3910 static void ResetNewGRF()
  3554 {
  3911 {
  3555 	GRFFile *next;
  3912 	GRFFile *next;
  3556 
  3913 
  3557 	for (GRFFile *f = _first_grffile; f != NULL; f = next) {
  3914 	for (GRFFile *f = _first_grffile; f != NULL; f = next) {
  3608 	ResetPriceBaseMultipliers();
  3965 	ResetPriceBaseMultipliers();
  3609 
  3966 
  3610 	/* Reset the curencies array */
  3967 	/* Reset the curencies array */
  3611 	ResetCurrencies();
  3968 	ResetCurrencies();
  3612 
  3969 
       
  3970 	/* Reset the house array */
       
  3971 	ResetCustomHouses();
       
  3972 	ResetHouses();
       
  3973 
  3613 	// Reset station classes
  3974 	// Reset station classes
  3614 	ResetStationClasses();
  3975 	ResetStationClasses();
  3615 	ResetCustomStations();
  3976 	ResetCustomStations();
  3616 
  3977 
  3617 	/* Reset NewGRF files */
  3978 	/* Reset NewGRF files */
  3633 	/* Reset misc GRF features and train list display variables */
  3994 	/* Reset misc GRF features and train list display variables */
  3634 	_misc_grf_features = 0;
  3995 	_misc_grf_features = 0;
  3635 	_traininfo_vehicle_pitch = 0;
  3996 	_traininfo_vehicle_pitch = 0;
  3636 	_traininfo_vehicle_width = 29;
  3997 	_traininfo_vehicle_width = 29;
  3637 	_have_2cc = false;
  3998 	_have_2cc = false;
       
  3999 	_have_newhouses = false;
  3638 	_signal_base = 0;
  4000 	_signal_base = 0;
  3639 	_coast_base = 0;
  4001 	_coast_base = 0;
  3640 
  4002 
  3641 	InitializeSoundPool();
  4003 	InitializeSoundPool();
  3642 	InitializeSpriteGroupPool();
  4004 	InitializeSpriteGroupPool();
  3832 				ShipVehicleInfo *svi = &_ship_vehicle_info[engine - SHIP_ENGINES_INDEX];
  4194 				ShipVehicleInfo *svi = &_ship_vehicle_info[engine - SHIP_ENGINES_INDEX];
  3833 				if (svi->cargo_type == CT_INVALID) svi->cargo_type = FindFirstRefittableCargo(engine);
  4195 				if (svi->cargo_type == CT_INVALID) svi->cargo_type = FindFirstRefittableCargo(engine);
  3834 				if (svi->cargo_type == CT_INVALID) _engine_info[engine].climates = 0;
  4196 				if (svi->cargo_type == CT_INVALID) _engine_info[engine].climates = 0;
  3835 				break;
  4197 				break;
  3836 			}
  4198 			}
       
  4199 		}
       
  4200 	}
       
  4201 }
       
  4202 
       
  4203 /** Add all new houses to the house array. House properties can be set at any
       
  4204  * time in the GRF file, so we can only add a house spec to the house array
       
  4205  * after the file has finished loading. We also need to check the dates, due to
       
  4206  * the TTDPatch behaviour described below that we need to emulate. */
       
  4207 static void FinaliseHouseArray()
       
  4208 {
       
  4209 	/* If there are no houses with start dates before 1930, then all houses
       
  4210 	 * with start dates of 1930 have them reset to 0. This is in order to be
       
  4211 	 * compatible with TTDPatch, where if no houses have start dates before
       
  4212 	 * 1930 and the date is before 1930, the game pretends that this is 1930.
       
  4213 	 * If there have been any houses defined with start dates before 1930 then
       
  4214 	 * the dates are left alone. */
       
  4215 	bool reset_dates = true;
       
  4216 
       
  4217 	for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
       
  4218 		if (file->housespec == NULL) continue;
       
  4219 
       
  4220 		for (int i = 0; i < HOUSE_MAX; i++) {
       
  4221 			HouseSpec *hs = file->housespec[i];
       
  4222 			if (hs != NULL) {
       
  4223 				SetHouseSpec(hs);
       
  4224 				if (hs->min_date < 1930) reset_dates = false;
       
  4225 			}
       
  4226 		}
       
  4227 	}
       
  4228 
       
  4229 	if (reset_dates) {
       
  4230 		for (int i = NEW_HOUSE_OFFSET; i < HOUSE_MAX; i++) {
       
  4231 			HouseSpec *hs = GetHouseSpecs(i);
       
  4232 
       
  4233 			if (hs->enabled && hs->min_date == 1930) hs->min_date = 0;
  3837 		}
  4234 		}
  3838 	}
  4235 	}
  3839 }
  4236 }
  3840 
  4237 
  3841 /* Here we perform initial decoding of some special sprites (as are they
  4238 /* Here we perform initial decoding of some special sprites (as are they
  4001 	}
  4398 	}
  4002 }
  4399 }
  4003 
  4400 
  4004 void InitDepotWindowBlockSizes();
  4401 void InitDepotWindowBlockSizes();
  4005 
  4402 
       
  4403 static void AfterLoadGRFs()
       
  4404 {
       
  4405 	/* Pre-calculate all refit masks after loading GRF files. */
       
  4406 	CalculateRefitMasks();
       
  4407 
       
  4408 	/* Set the block size in the depot windows based on vehicle sprite sizes */
       
  4409 	InitDepotWindowBlockSizes();
       
  4410 
       
  4411 	/* Add all new houses to the house array. */
       
  4412 	FinaliseHouseArray();
       
  4413 }
       
  4414 
  4006 void LoadNewGRF(uint load_index, uint file_index)
  4415 void LoadNewGRF(uint load_index, uint file_index)
  4007 {
  4416 {
  4008 	InitializeGRFSpecial();
  4417 	InitializeGRFSpecial();
  4009 
  4418 
  4010 	ResetNewGRFData();
  4419 	ResetNewGRFData();
  4031 				DEBUG(sprite, 2, "Currently %i sprites are loaded", _cur_spriteid);
  4440 				DEBUG(sprite, 2, "Currently %i sprites are loaded", _cur_spriteid);
  4032 			}
  4441 			}
  4033 		}
  4442 		}
  4034 	}
  4443 	}
  4035 
  4444 
  4036 	// Pre-calculate all refit masks after loading GRF files
  4445 	/* Call any functions that should be run after GRFs have been loaded. */
  4037 	CalculateRefitMasks();
  4446 	AfterLoadGRFs();
  4038 
  4447 }
  4039 	/* Set the block size in the depot windows based on vehicle sprite sizes */
       
  4040 	InitDepotWindowBlockSizes();
       
  4041 }