src/newgrf.cpp
branchnoai
changeset 9732 f8eb3e208514
parent 9724 b39bc69bb2f2
child 9800 ab08ca2a2018
equal deleted inserted replaced
9731:9b1552d0fd9b 9732:f8eb3e208514
    91 
    91 
    92 static byte _grf_data_blocks;
    92 static byte _grf_data_blocks;
    93 static GrfDataType _grf_data_type;
    93 static GrfDataType _grf_data_type;
    94 
    94 
    95 
    95 
    96 enum grfspec_feature {
       
    97 	GSF_TRAIN,
       
    98 	GSF_ROAD,
       
    99 	GSF_SHIP,
       
   100 	GSF_AIRCRAFT,
       
   101 	GSF_STATION,
       
   102 	GSF_CANAL,
       
   103 	GSF_BRIDGE,
       
   104 	GSF_TOWNHOUSE,
       
   105 	GSF_GLOBALVAR,
       
   106 	GSF_INDUSTRYTILES,
       
   107 	GSF_INDUSTRIES,
       
   108 	GSF_CARGOS,
       
   109 	GSF_SOUNDFX,
       
   110 };
       
   111 
       
   112 
       
   113 typedef void (*SpecialSpriteHandler)(byte *buf, int len);
    96 typedef void (*SpecialSpriteHandler)(byte *buf, int len);
   114 
    97 
   115 static const uint _vehcounts[4] = {
    98 static const uint _vehcounts[4] = {
   116 	/* GSF_TRAIN */    NUM_TRAIN_ENGINES,
    99 	/* GSF_TRAIN */    NUM_TRAIN_ENGINES,
   117 	/* GSF_ROAD */     NUM_ROAD_ENGINES,
   100 	/* GSF_ROAD */     NUM_ROAD_ENGINES,
   304 	if (colour >= 245 && colour < 254) return colour - 28;
   287 	if (colour >= 245 && colour < 254) return colour - 28;
   305 
   288 
   306 	return colour;
   289 	return colour;
   307 }
   290 }
   308 
   291 
       
   292 /** Map the colour modifiers of TTDPatch to those that Open is using.
       
   293  * @param grf_sprite pointer to the structure been modified
       
   294  */
       
   295 static void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
       
   296 {
       
   297 	if (HasBit(grf_sprite->pal, 14)) {
       
   298 		ClrBit(grf_sprite->pal, 14);
       
   299 		SetBit(grf_sprite->sprite, SPRITE_MODIFIER_OPAQUE);
       
   300 	}
       
   301 
       
   302 	if (HasBit(grf_sprite->sprite, 14)) {
       
   303 		ClrBit(grf_sprite->sprite, 14);
       
   304 		SetBit(grf_sprite->sprite, PALETTE_MODIFIER_TRANSPARENT);
       
   305 	}
       
   306 
       
   307 	if (HasBit(grf_sprite->sprite, 15)) {
       
   308 		ClrBit(grf_sprite->sprite, 15);
       
   309 		SetBit(grf_sprite->sprite, PALETTE_MODIFIER_COLOR);
       
   310 	}
       
   311 }
   309 
   312 
   310 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   313 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
   311 
   314 
   312 static void dewagonize(int condition, int engine)
   315 static void dewagonize(int condition, int engine)
   313 {
   316 {
   372 			case 0x0D: { // Running cost factor
   375 			case 0x0D: { // Running cost factor
   373 				uint8 runcostfact = grf_load_byte(&buf);
   376 				uint8 runcostfact = grf_load_byte(&buf);
   374 
   377 
   375 				if (rvi->railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   378 				if (rvi->railveh_type == RAILVEH_MULTIHEAD) runcostfact /= 2;
   376 
   379 
   377 				rvi->running_cost_base = runcostfact;
   380 				rvi->running_cost = runcostfact;
   378 			} break;
   381 			} break;
   379 
   382 
   380 			case 0x0E: { // Running cost base
   383 			case 0x0E: { // Running cost base
   381 				uint32 base = grf_load_dword(&buf);
   384 				uint32 base = grf_load_dword(&buf);
   382 
   385 
   383 				switch (base) {
   386 				/* These magic numbers are used in GRFs to specify the base cost:
   384 					case 0x4C30: rvi->running_cost_class = 0; break;
   387 				 * http://wiki.ttdpatch.net/tiki-index.php?page=BaseCosts
   385 					case 0x4C36: rvi->running_cost_class = 1; break;
   388 				 */
   386 					case 0x4C3C: rvi->running_cost_class = 2; break;
   389 				if (base == 0) {
   387 					case 0: break; // Used by wagons
   390 					rvi->running_cost_class = 0xFF;
   388 					default:
   391 				} else if (base < 0x4B34 || base > 0x4C54 || (base - 0x4B34) % 6 != 0) {
   389 						grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   392 					grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
   390 						break;
   393 				} else {
       
   394 					/* Convert the magic number to an index into the price data */
       
   395 					rvi->running_cost_class = (base - 0x4B34) / 6;
   391 				}
   396 				}
   392 			} break;
   397 			} break;
   393 
   398 
   394 			case 0x12: { // Sprite ID
   399 			case 0x12: { // Sprite ID
   395 				uint8 spriteid = grf_load_byte(&buf);
   400 				uint8 spriteid = grf_load_byte(&buf);
   406 
   411 
   407 				if (dual != 0) {
   412 				if (dual != 0) {
   408 					if (rvi->railveh_type != RAILVEH_MULTIHEAD) {
   413 					if (rvi->railveh_type != RAILVEH_MULTIHEAD) {
   409 						// adjust power and running cost if needed
   414 						// adjust power and running cost if needed
   410 						rvi->power /= 2;
   415 						rvi->power /= 2;
   411 						rvi->running_cost_base /= 2;
   416 						rvi->running_cost /= 2;
   412 					}
   417 					}
   413 					rvi->railveh_type = RAILVEH_MULTIHEAD;
   418 					rvi->railveh_type = RAILVEH_MULTIHEAD;
   414 				} else {
   419 				} else {
   415 					if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
   420 					if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
   416 						// adjust power and running cost if needed
   421 						// adjust power and running cost if needed
   417 						rvi->power *= 2;
   422 						rvi->power *= 2;
   418 						rvi->running_cost_base *= 2;
   423 						rvi->running_cost *= 2;
   419 					}
   424 					}
   420 					rvi->railveh_type = rvi->power == 0 ?
   425 					rvi->railveh_type = rvi->power == 0 ?
   421 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   426 						RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
   422 				}
   427 				}
   423 			} break;
   428 			} break;
   589 
   594 
   590 			case 0x09: // Running cost factor
   595 			case 0x09: // Running cost factor
   591 				rvi->running_cost = grf_load_byte(&buf);
   596 				rvi->running_cost = grf_load_byte(&buf);
   592 				break;
   597 				break;
   593 
   598 
   594 			case 0x0A: // Running cost base
   599 			case 0x0A: { // Running cost base
   595 				/** @todo : I have no idea. --pasky
   600 				uint32 base= grf_load_dword(&buf);
   596 				 * I THINK it is used for overriding the base cost of all road vehicle (_price.roadveh_base) --belugas */
   601 
   597 				grf_load_dword(&buf);
   602 				/* These magic numbers are used in GRFs to specify the base cost:
   598 				ret = true;
   603 				 * http://wiki.ttdpatch.net/tiki-index.php?page=BaseCosts
   599 				break;
   604 				 */
       
   605 				if (base == 0) {
       
   606 					rvi->running_cost_class = 0xFF;
       
   607 				} else if (base < 0x4B34 || base > 0x4C54 || (base - 0x4B34) % 6 != 0) {
       
   608 					grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
       
   609 				} else {
       
   610 					/* Convert the magic number to an index into the price data */
       
   611 					rvi->running_cost_class = (base - 0x4B34) / 6;
       
   612 				}
       
   613 
       
   614 				break;
       
   615 			}
   600 
   616 
   601 			case 0x0E: { // Sprite ID
   617 			case 0x0E: { // Sprite ID
   602 				uint8 spriteid = grf_load_byte(&buf);
   618 				uint8 spriteid = grf_load_byte(&buf);
   603 
   619 
   604 				/* cars have different custom id in the GRF file */
   620 				/* cars have different custom id in the GRF file */
   944 				for (uint t = 0; t < statspec->tiles; t++) {
   960 				for (uint t = 0; t < statspec->tiles; t++) {
   945 					DrawTileSprites *dts = &statspec->renderdata[t];
   961 					DrawTileSprites *dts = &statspec->renderdata[t];
   946 					uint seq_count = 0;
   962 					uint seq_count = 0;
   947 
   963 
   948 					dts->seq = NULL;
   964 					dts->seq = NULL;
   949 					dts->ground_sprite = grf_load_word(&buf);
   965 					dts->ground.sprite = grf_load_word(&buf);
   950 					dts->ground_pal = grf_load_word(&buf);
   966 					dts->ground.pal = grf_load_word(&buf);
   951 					if (dts->ground_sprite == 0) continue;
   967 					if (dts->ground.sprite == 0) continue;
   952 					if (HasBit(dts->ground_pal, 15)) {
   968 					if (HasBit(dts->ground.pal, 15)) {
   953 						ClrBit(dts->ground_pal, 15);
   969 						ClrBit(dts->ground.pal, 15);
   954 						SetBit(dts->ground_sprite, SPRITE_MODIFIER_USE_OFFSET);
   970 						SetBit(dts->ground.sprite, SPRITE_MODIFIER_USE_OFFSET);
   955 					}
   971 					}
   956 					if (HasBit(dts->ground_pal, 14)) {
   972 
   957 						ClrBit(dts->ground_pal, 14);
   973 					MapSpriteMappingRecolour(&dts->ground);
   958 						SetBit(dts->ground_sprite, SPRITE_MODIFIER_OPAQUE);
       
   959 					}
       
   960 					if (HasBit(dts->ground_sprite, 15)) {
       
   961 						ClrBit(dts->ground_sprite, 15);
       
   962 						SetBit(dts->ground_sprite, PALETTE_MODIFIER_COLOR);
       
   963 					}
       
   964 					if (HasBit(dts->ground_sprite, 14)) {
       
   965 						ClrBit(dts->ground_sprite, 14);
       
   966 						SetBit(dts->ground_sprite, PALETTE_MODIFIER_TRANSPARENT);
       
   967 					}
       
   968 
   974 
   969 					while (buf < *bufp + len) {
   975 					while (buf < *bufp + len) {
   970 						DrawTileSeqStruct *dtss;
   976 						DrawTileSeqStruct *dtss;
   971 
   977 
   972 						/* no relative bounding box support */
   978 						/* no relative bounding box support */
   978 						dtss->delta_y = grf_load_byte(&buf);
   984 						dtss->delta_y = grf_load_byte(&buf);
   979 						dtss->delta_z = grf_load_byte(&buf);
   985 						dtss->delta_z = grf_load_byte(&buf);
   980 						dtss->size_x = grf_load_byte(&buf);
   986 						dtss->size_x = grf_load_byte(&buf);
   981 						dtss->size_y = grf_load_byte(&buf);
   987 						dtss->size_y = grf_load_byte(&buf);
   982 						dtss->size_z = grf_load_byte(&buf);
   988 						dtss->size_z = grf_load_byte(&buf);
   983 						dtss->image = grf_load_word(&buf);
   989 						dtss->image.sprite = grf_load_word(&buf);
   984 						dtss->pal = grf_load_word(&buf);
   990 						dtss->image.pal = grf_load_word(&buf);
   985 
   991 
   986 						/* Remap flags as ours collide */
   992 						/* Remap flags as ours collide */
   987 						if (HasBit(dtss->pal, 15)) {
   993 						if (HasBit(dtss->image.pal, 15)) {
   988 							ClrBit(dtss->pal, 15);
   994 							ClrBit(dtss->image.pal, 15);
   989 							SetBit(dtss->image, SPRITE_MODIFIER_USE_OFFSET);
   995 							SetBit(dtss->image.sprite, SPRITE_MODIFIER_USE_OFFSET);
   990 						}
   996 						}
   991 						if (HasBit(dtss->pal, 14)) {
   997 
   992 							ClrBit(dtss->pal, 14);
   998 						MapSpriteMappingRecolour(&dtss->image);
   993 							SetBit(dtss->image, SPRITE_MODIFIER_OPAQUE);
       
   994 						}
       
   995 
       
   996 						if (HasBit(dtss->image, 15)) {
       
   997 							ClrBit(dtss->image, 15);
       
   998 							SetBit(dtss->image, PALETTE_MODIFIER_COLOR);
       
   999 						}
       
  1000 						if (HasBit(dtss->image, 14)) {
       
  1001 							ClrBit(dtss->image, 14);
       
  1002 							SetBit(dtss->image, PALETTE_MODIFIER_TRANSPARENT);
       
  1003 						}
       
  1004 					}
   999 					}
  1005 				}
  1000 				}
  1006 				break;
  1001 				break;
  1007 
  1002 
  1008 			case 0x0A: { // Copy sprite layout
  1003 			case 0x0A: { // Copy sprite layout
  1175 		grfmsg(1, "BridgeChangeInfo: Bridge %u is invalid, max %u, ignoring", brid + numinfo, MAX_BRIDGES);
  1170 		grfmsg(1, "BridgeChangeInfo: Bridge %u is invalid, max %u, ignoring", brid + numinfo, MAX_BRIDGES);
  1176 		return false;
  1171 		return false;
  1177 	}
  1172 	}
  1178 
  1173 
  1179 	for (int i = 0; i < numinfo; i++) {
  1174 	for (int i = 0; i < numinfo; i++) {
  1180 		Bridge *bridge = &_bridge[brid + i];
  1175 		BridgeSpec *bridge = &_bridge[brid + i];
  1181 
  1176 
  1182 		switch (prop) {
  1177 		switch (prop) {
  1183 			case 0x08: // Year of availability
  1178 			case 0x08: // Year of availability
  1184 				bridge->avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
  1179 				bridge->avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
  1185 				break;
  1180 				break;
  1222 
  1217 
  1223 					for (byte sprite = 0; sprite < 32; sprite++) {
  1218 					for (byte sprite = 0; sprite < 32; sprite++) {
  1224 						SpriteID image = grf_load_word(&buf);
  1219 						SpriteID image = grf_load_word(&buf);
  1225 						SpriteID pal   = grf_load_word(&buf);
  1220 						SpriteID pal   = grf_load_word(&buf);
  1226 
  1221 
  1227 						if (HasBit(pal, 15)) {
       
  1228 							SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
       
  1229 						}
       
  1230 
       
  1231 						/* Clear old color modifer bit */
       
  1232 						ClrBit(image, 15);
       
  1233 
       
  1234 						bridge->sprite_table[tableid][sprite].sprite = image;
  1222 						bridge->sprite_table[tableid][sprite].sprite = image;
  1235 						bridge->sprite_table[tableid][sprite].pal    = pal;
  1223 						bridge->sprite_table[tableid][sprite].pal    = pal;
       
  1224 
       
  1225 						MapSpriteMappingRecolour(&bridge->sprite_table[tableid][sprite]);
  1236 					}
  1226 					}
  1237 				}
  1227 				}
  1238 			} break;
  1228 			} break;
  1239 
  1229 
  1240 			case 0x0E: // Flags; bit 0 - disable far pillars
  1230 			case 0x0E: // Flags; bit 0 - disable far pillars
  1242 				break;
  1232 				break;
  1243 
  1233 
  1244 			case 0x0F: // Long format year of availability (year since year 0)
  1234 			case 0x0F: // Long format year of availability (year since year 0)
  1245 				bridge->avail_year = Clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
  1235 				bridge->avail_year = Clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
  1246 				break;
  1236 				break;
       
  1237 
       
  1238 			case 0x10: { // purchase string
       
  1239 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  1240 				if (newone != STR_UNDEFINED) bridge->material = newone;
       
  1241 				} break;
       
  1242 
       
  1243 			case 0x11: // description of bridge with rails or roads
       
  1244 			case 0x12: {
       
  1245 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
       
  1246 				if (newone != STR_UNDEFINED) bridge->transport_name[prop - 0x11] = newone;
       
  1247 				} break;
  1247 
  1248 
  1248 			default:
  1249 			default:
  1249 				ret = true;
  1250 				ret = true;
  1250 				break;
  1251 				break;
  1251 		}
  1252 		}
  1486 				} else {
  1487 				} else {
  1487 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1488 					grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
  1488 				}
  1489 				}
  1489 			} break;
  1490 			} break;
  1490 
  1491 
  1491 			case 0x09: { // Cargo translation table
  1492 			case 0x09: // Cargo translation table
  1492 				if (gvid != 0) {
  1493 				/* This is loaded during the reservation stage, so just skip it here. */
  1493 					if (i == 0) grfmsg(1, "InitChangeInfo: Cargo translation table must start at zero");
  1494 				/* Each entry is 4 bytes. */
  1494 					/* Skip data */
  1495 				buf += 4;
  1495 					buf += 4;
  1496 				break;
  1496 					break;
       
  1497 				}
       
  1498 				if (i == 0) {
       
  1499 					free(_cur_grffile->cargo_list);
       
  1500 					_cur_grffile->cargo_max = numinfo;
       
  1501 					_cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
       
  1502 				}
       
  1503 				CargoLabel cl = grf_load_dword(&buf);
       
  1504 				_cur_grffile->cargo_list[i] = BSWAP32(cl);
       
  1505 			} break;
       
  1506 
  1497 
  1507 			case 0x0A: { // Currency display names
  1498 			case 0x0A: { // Currency display names
  1508 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1499 				uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
  1509 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1500 				StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
  1510 
  1501 
  2125 			case 0x23: // removal cost multiplier
  2116 			case 0x23: // removal cost multiplier
  2126 				indsp->removal_cost_multiplier = grf_load_dword(&buf);
  2117 				indsp->removal_cost_multiplier = grf_load_dword(&buf);
  2127 				break;
  2118 				break;
  2128 
  2119 
  2129 			case 0x24: // name for nearby station
  2120 			case 0x24: // name for nearby station
  2130 				indsp->station_name = GRFMappedStringID(grf_load_dword(&buf), _cur_grffile->grfid);
  2121 				indsp->station_name = GRFMappedStringID(grf_load_word(&buf), _cur_grffile->grfid);
  2131 				break;
  2122 				break;
  2132 
  2123 
  2133 			default:
  2124 			default:
  2134 				ret = true;
  2125 				ret = true;
  2135 				break;
  2126 				break;
  2165 		/* GSF_AIRCRAFT */     AircraftVehicleChangeInfo,
  2156 		/* GSF_AIRCRAFT */     AircraftVehicleChangeInfo,
  2166 		/* GSF_STATION */      StationChangeInfo,
  2157 		/* GSF_STATION */      StationChangeInfo,
  2167 		/* GSF_CANAL */        CanalChangeInfo,
  2158 		/* GSF_CANAL */        CanalChangeInfo,
  2168 		/* GSF_BRIDGE */       BridgeChangeInfo,
  2159 		/* GSF_BRIDGE */       BridgeChangeInfo,
  2169 		/* GSF_TOWNHOUSE */    TownHouseChangeInfo,
  2160 		/* GSF_TOWNHOUSE */    TownHouseChangeInfo,
  2170 		/* GSF_GLOBALVAR */    NULL, /* Global variables are handled during reservation */
  2161 		/* GSF_GLOBALVAR */    GlobalVarChangeInfo,
  2171 		/* GSF_INDUSTRYTILES */IndustrytilesChangeInfo,
  2162 		/* GSF_INDUSTRYTILES */IndustrytilesChangeInfo,
  2172 		/* GSF_INDUSTRIES */   IndustriesChangeInfo,
  2163 		/* GSF_INDUSTRIES */   IndustriesChangeInfo,
  2173 		/* GSF_CARGOS */       NULL, /* Cargo is handled during reservation */
  2164 		/* GSF_CARGOS */       NULL, /* Cargo is handled during reservation */
  2174 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  2165 		/* GSF_SOUNDFX */      SoundEffectChangeInfo,
  2175 	};
  2166 	};
  2285 /* Action 0x00 (GLS_RESERVE) */
  2276 /* Action 0x00 (GLS_RESERVE) */
  2286 static void ReserveChangeInfo(byte *buf, int len)
  2277 static void ReserveChangeInfo(byte *buf, int len)
  2287 {
  2278 {
  2288 	byte *bufend = buf + len;
  2279 	byte *bufend = buf + len;
  2289 
  2280 
  2290 	if (!check_length(len, 6, "InitChangeInfo")) return;
  2281 	if (!check_length(len, 6, "ReserveChangeInfo")) return;
  2291 	buf++;
  2282 	buf++;
  2292 	uint8 feature  = grf_load_byte(&buf);
  2283 	uint8 feature  = grf_load_byte(&buf);
  2293 
  2284 
  2294 	if (feature != GSF_CARGOS && feature != GSF_GLOBALVAR) return;
  2285 	if (feature != GSF_CARGOS && feature != GSF_GLOBALVAR) return;
  2295 
  2286 
  2305 			default: NOT_REACHED();
  2296 			default: NOT_REACHED();
  2306 			case GSF_CARGOS:
  2297 			case GSF_CARGOS:
  2307 				ignoring = CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf);
  2298 				ignoring = CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf);
  2308 				break;
  2299 				break;
  2309 			case GSF_GLOBALVAR:
  2300 			case GSF_GLOBALVAR:
  2310 				ignoring = GlobalVarChangeInfo(index, numinfo, prop, &buf, bufend - buf);
  2301 				switch (prop) {
       
  2302 					case 0x09: // Cargo Translation Table
       
  2303 						if (index != 0) {
       
  2304 							grfmsg(1, "ReserveChangeInfo: Cargo translation table must start at zero");
       
  2305 							return;
       
  2306 						}
       
  2307 
       
  2308 						free(_cur_grffile->cargo_list);
       
  2309 						_cur_grffile->cargo_max = numinfo;
       
  2310 						_cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
       
  2311 
       
  2312 						for (uint i = 0; i < numinfo; i++) {
       
  2313 							CargoLabel cl = grf_load_dword(&buf);
       
  2314 							_cur_grffile->cargo_list[i] = BSWAP32(cl);
       
  2315 						}
       
  2316 						break;
       
  2317 				}
  2311 				break;
  2318 				break;
  2312 		}
  2319 		}
  2313 
  2320 
  2314 		if (ignoring) grfmsg(2, "ReserveChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  2321 		if (ignoring) grfmsg(2, "ReserveChangeInfo: Ignoring property 0x%02X (not implemented)", prop);
  2315 	}
  2322 	}
  2650 					group->type = SGT_TILELAYOUT;
  2657 					group->type = SGT_TILELAYOUT;
  2651 					group->g.layout.num_sprites = sprites;
  2658 					group->g.layout.num_sprites = sprites;
  2652 					group->g.layout.dts = CallocT<DrawTileSprites>(1);
  2659 					group->g.layout.dts = CallocT<DrawTileSprites>(1);
  2653 
  2660 
  2654 					/* Groundsprite */
  2661 					/* Groundsprite */
  2655 					group->g.layout.dts->ground_sprite = grf_load_word(&buf);
  2662 					group->g.layout.dts->ground.sprite = grf_load_word(&buf);
  2656 					group->g.layout.dts->ground_pal    = grf_load_word(&buf);
  2663 					group->g.layout.dts->ground.pal    = grf_load_word(&buf);
       
  2664 
  2657 					/* Remap transparent/colour modifier bits */
  2665 					/* Remap transparent/colour modifier bits */
  2658 					if (HasBit(group->g.layout.dts->ground_sprite, 14)) {
  2666 					MapSpriteMappingRecolour(&group->g.layout.dts->ground);
  2659 						ClrBit(group->g.layout.dts->ground_sprite, 14);
  2667 
  2660 						SetBit(group->g.layout.dts->ground_sprite, PALETTE_MODIFIER_TRANSPARENT);
  2668 					if (HasBit(group->g.layout.dts->ground.pal, 15)) {
  2661 					}
       
  2662 					if (HasBit(group->g.layout.dts->ground_sprite, 15)) {
       
  2663 						ClrBit(group->g.layout.dts->ground_sprite, 15);
       
  2664 						SetBit(group->g.layout.dts->ground_sprite, PALETTE_MODIFIER_COLOR);
       
  2665 					}
       
  2666 					if (HasBit(group->g.layout.dts->ground_pal, 14)) {
       
  2667 						ClrBit(group->g.layout.dts->ground_pal, 14);
       
  2668 						SetBit(group->g.layout.dts->ground_sprite, SPRITE_MODIFIER_OPAQUE);
       
  2669 					}
       
  2670 					if (HasBit(group->g.layout.dts->ground_pal, 15)) {
       
  2671 						/* Bit 31 set means this is a custom sprite, so rewrite it to the
  2669 						/* Bit 31 set means this is a custom sprite, so rewrite it to the
  2672 						 * last spriteset defined. */
  2670 						 * last spriteset defined. */
  2673 						SpriteID sprite = _cur_grffile->spriteset_start + GB(group->g.layout.dts->ground_sprite, 0, 14) * sprites;
  2671 						SpriteID sprite = _cur_grffile->spriteset_start + GB(group->g.layout.dts->ground.sprite, 0, 14) * sprites;
  2674 						SB(group->g.layout.dts->ground_sprite, 0, SPRITE_WIDTH, sprite);
  2672 						SB(group->g.layout.dts->ground.sprite, 0, SPRITE_WIDTH, sprite);
  2675 						ClrBit(group->g.layout.dts->ground_pal, 15);
  2673 						ClrBit(group->g.layout.dts->ground.pal, 15);
  2676 					}
  2674 					}
  2677 
  2675 
  2678 					group->g.layout.dts->seq = CallocT<DrawTileSeqStruct>(num_sprites + 1);
  2676 					group->g.layout.dts->seq = CallocT<DrawTileSeqStruct>(num_sprites + 1);
  2679 
  2677 
  2680 					for (i = 0; i < num_sprites; i++) {
  2678 					for (i = 0; i < num_sprites; i++) {
  2681 						DrawTileSeqStruct *seq = (DrawTileSeqStruct*)&group->g.layout.dts->seq[i];
  2679 						DrawTileSeqStruct *seq = (DrawTileSeqStruct*)&group->g.layout.dts->seq[i];
  2682 
  2680 
  2683 						seq->image = grf_load_word(&buf);
  2681 						seq->image.sprite = grf_load_word(&buf);
  2684 						seq->pal   = grf_load_word(&buf);
  2682 						seq->image.pal   = grf_load_word(&buf);
  2685 						seq->delta_x = grf_load_byte(&buf);
  2683 						seq->delta_x = grf_load_byte(&buf);
  2686 						seq->delta_y = grf_load_byte(&buf);
  2684 						seq->delta_y = grf_load_byte(&buf);
  2687 
  2685 
  2688 						if (HasBit(seq->image, 14)) {
  2686 						MapSpriteMappingRecolour(&seq->image);
  2689 							ClrBit(seq->image, 14);
  2687 
  2690 							SetBit(seq->image, PALETTE_MODIFIER_TRANSPARENT);
  2688 						if (HasBit(seq->image.pal, 15)) {
  2691 						}
       
  2692 						if (HasBit(seq->image, 15)) {
       
  2693 							ClrBit(seq->image, 15);
       
  2694 							SetBit(seq->image, PALETTE_MODIFIER_COLOR);
       
  2695 						}
       
  2696 						if (HasBit(seq->pal, 14)) {
       
  2697 							ClrBit(seq->pal, 14);
       
  2698 							SetBit(seq->image, SPRITE_MODIFIER_OPAQUE);
       
  2699 						}
       
  2700 						if (HasBit(seq->pal, 15)) {
       
  2701 							/* Bit 31 set means this is a custom sprite, so rewrite it to the
  2689 							/* Bit 31 set means this is a custom sprite, so rewrite it to the
  2702 							 * last spriteset defined. */
  2690 							 * last spriteset defined. */
  2703 							SpriteID sprite = _cur_grffile->spriteset_start + GB(seq->image, 0, 14) * sprites;
  2691 							SpriteID sprite = _cur_grffile->spriteset_start + GB(seq->image.sprite, 0, 14) * sprites;
  2704 							SB(seq->image, 0, SPRITE_WIDTH, sprite);
  2692 							SB(seq->image.sprite, 0, SPRITE_WIDTH, sprite);
  2705 							ClrBit(seq->pal, 15);
  2693 							ClrBit(seq->image.pal, 15);
  2706 						}
  2694 						}
  2707 
  2695 
  2708 						if (type > 0) {
  2696 						if (type > 0) {
  2709 							seq->delta_z = grf_load_byte(&buf);
  2697 							seq->delta_z = grf_load_byte(&buf);
  2710 							if ((byte)seq->delta_z == 0x80) continue;
  2698 							if ((byte)seq->delta_z == 0x80) continue;
  3087 	 *                 can be zero, in that case the def-cid is used always
  3075 	 *                 can be zero, in that case the def-cid is used always
  3088 	 * B cargo-type    type of this cargo type (e.g. mail=2, wood=7, see below)
  3076 	 * B cargo-type    type of this cargo type (e.g. mail=2, wood=7, see below)
  3089 	 * W cid           cargo ID (sprite group ID) for this type of cargo
  3077 	 * W cid           cargo ID (sprite group ID) for this type of cargo
  3090 	 * W def-cid       default cargo ID (sprite group ID) */
  3078 	 * W def-cid       default cargo ID (sprite group ID) */
  3091 
  3079 
       
  3080 	if (_cur_grffile->spritegroups == 0) {
       
  3081 		grfmsg(1, "FeatureMapSpriteGroup: No sprite groups to work on! Skipping");
       
  3082 		return;
       
  3083 	}
       
  3084 
  3092 	if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
  3085 	if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
  3093 
  3086 
  3094 	uint8 feature = buf[1];
  3087 	uint8 feature = buf[1];
  3095 	uint8 idcount = buf[2] & 0x7F;
  3088 	uint8 idcount = buf[2] & 0x7F;
  3096 	bool wagover = (buf[2] & 0x80) == 0x80;
  3089 	bool wagover = (buf[2] & 0x80) == 0x80;
  3097 
  3090 
  3098 	if (!check_length(len, 3 + idcount, "FeatureMapSpriteGroup")) return;
  3091 	if (!check_length(len, 3 + idcount, "FeatureMapSpriteGroup")) return;
  3099 
  3092 
  3100 	/* If idcount is zero, this is a feature callback */
  3093 	/* If idcount is zero, this is a feature callback */
  3101 	if (idcount == 0) {
  3094 	if (idcount == 0) {
  3102 		grfmsg(2, "FeatureMapSpriteGroup: Feature callbacks not implemented yet");
  3095 		byte *bp = &buf[4];
       
  3096 		uint16 groupid = grf_load_word(&bp);
       
  3097 
       
  3098 		grfmsg(6, "FeatureMapSpriteGroup: Adding generic feature callback for feature %d", feature);
       
  3099 
       
  3100 		AddGenericCallback(feature, _cur_grffile, _cur_grffile->spritegroups[groupid]);
  3103 		return;
  3101 		return;
  3104 	}
  3102 	}
  3105 
  3103 
  3106 	uint8 cidcount = buf[3 + idcount];
  3104 	uint8 cidcount = buf[3 + idcount];
  3107 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  3105 	if (!check_length(len, 4 + idcount + cidcount * 3, "FeatureMapSpriteGroup")) return;
  3108 
  3106 
  3109 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  3107 	grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d",
  3110 			feature, idcount, cidcount, wagover);
  3108 			feature, idcount, cidcount, wagover);
  3111 
       
  3112 	if (_cur_grffile->spritegroups == 0) {
       
  3113 		grfmsg(1, "FeatureMapSpriteGroup: No sprite groups to work on! Skipping");
       
  3114 		return;
       
  3115 	}
       
  3116 
  3109 
  3117 	switch (feature) {
  3110 	switch (feature) {
  3118 		case GSF_TRAIN:
  3111 		case GSF_TRAIN:
  3119 		case GSF_ROAD:
  3112 		case GSF_ROAD:
  3120 		case GSF_SHIP:
  3113 		case GSF_SHIP:
  3433 	_skip_sprites = grf_load_extended(&buf);
  3426 	_skip_sprites = grf_load_extended(&buf);
  3434 
  3427 
  3435 	grfmsg(3, "SkipAct5: Skipping %d sprites", _skip_sprites);
  3428 	grfmsg(3, "SkipAct5: Skipping %d sprites", _skip_sprites);
  3436 }
  3429 }
  3437 
  3430 
       
  3431 /**
       
  3432  * Reads a variable common to VarAction2 and Action7/9/D.
       
  3433  *
       
  3434  * Returns VarAction2 variable 'param' resp. Action7/9/D variable '0x80 + param'.
       
  3435  * If a variable is not accessible from all four actions, it is handled in the action specific functions.
       
  3436  *
       
  3437  * @param param variable number (as for VarAction2, for Action7/9/D you have to subtract 0x80 first).
       
  3438  * @param value returns the value of the variable.
       
  3439  * @return true iff the variable is known and the value is returned in 'value'.
       
  3440  */
       
  3441 bool GetGlobalVariable(byte param, uint32 *value)
       
  3442 {
       
  3443 	switch (param) {
       
  3444 		case 0x00: // current date
       
  3445 			*value = max(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
       
  3446 			return true;
       
  3447 
       
  3448 		case 0x01: // current year
       
  3449 			*value = Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
       
  3450 			return true;
       
  3451 
       
  3452 		case 0x02: // current month
       
  3453 			*value = _cur_month;
       
  3454 			return true;
       
  3455 
       
  3456 		case 0x03: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
       
  3457 			*value = _opt.landscape;
       
  3458 			return true;
       
  3459 
       
  3460 		case 0x06: // road traffic side, bit 4 clear=left, set=right
       
  3461 			*value = _opt.road_side << 4;
       
  3462 			return true;
       
  3463 
       
  3464 		case 0x09: // date fraction
       
  3465 			*value = _date_fract;
       
  3466 			return true;
       
  3467 
       
  3468 		case 0x0A: // animation counter
       
  3469 			*value = _tick_counter;
       
  3470 			return true;
       
  3471 
       
  3472 		case 0x0B: { // TTDPatch version
       
  3473 			uint major    = 2;
       
  3474 			uint minor    = 6;
       
  3475 			uint revision = 1; // special case: 2.0.1 is 2.0.10
       
  3476 			uint build    = 1382;
       
  3477 			*value = (major << 24) | (minor << 20) | (revision << 16) | build;
       
  3478 			return true;
       
  3479 		}
       
  3480 
       
  3481 		case 0x0D: // TTD Version, 00=DOS, 01=Windows
       
  3482 			*value = !_use_dos_palette;
       
  3483 			return true;
       
  3484 
       
  3485 		case 0x0E: // Y-offset for train sprites
       
  3486 			*value = _traininfo_vehicle_pitch;
       
  3487 			return true;
       
  3488 
       
  3489 		case 0x0F: // Rail track type cost factors
       
  3490 			*value = 0;
       
  3491 			SB(*value, 0, 8, _railtype_cost_multiplier[0]); // normal rail
       
  3492 			if (_patches.disable_elrails) {
       
  3493 				/* skip elrail multiplier - disabled */
       
  3494 				SB(*value, 8, 8, _railtype_cost_multiplier[2]); // monorail
       
  3495 			} else {
       
  3496 				SB(*value, 8, 8, _railtype_cost_multiplier[1]); // electified railway
       
  3497 				/* Skip monorail multiplier - no space in result */
       
  3498 			}
       
  3499 			SB(*value, 16, 8, _railtype_cost_multiplier[3]); // maglev
       
  3500 			return true;
       
  3501 
       
  3502 		case 0x11: // current rail tool type
       
  3503 			*value = 0;
       
  3504 			return true;
       
  3505 
       
  3506 		case 0x12: // Game mode
       
  3507 			*value = _game_mode;
       
  3508 			return true;
       
  3509 
       
  3510 		/* case 0x13: // Tile refresh offset to left    not implemented */
       
  3511 		/* case 0x14: // Tile refresh offset to right   not implemented */
       
  3512 		/* case 0x15: // Tile refresh offset upwards    not implemented */
       
  3513 		/* case 0x16: // Tile refresh offset downwards  not implemented */
       
  3514 		/* case 0x17: // temperate snow line            not implemented */
       
  3515 
       
  3516 		case 0x1A: // Always -1
       
  3517 			*value = UINT_MAX;
       
  3518 			return true;
       
  3519 
       
  3520 		case 0x1B: // Display options
       
  3521 			*value = GB(_display_opt, 0, 6);
       
  3522 			return true;
       
  3523 
       
  3524 		case 0x1D: // TTD Platform, 00=TTDPatch, 01=OpenTTD
       
  3525 			*value = 1;
       
  3526 			return true;
       
  3527 
       
  3528 		case 0x1E: // Miscellaneous GRF features
       
  3529 			*value = _misc_grf_features;
       
  3530 			return true;
       
  3531 
       
  3532 		/* case 0x1F: // locale dependent settings not implemented */
       
  3533 
       
  3534 		case 0x20: // snow line height
       
  3535 			*value = _opt.landscape == LT_ARCTIC ? GetSnowLine() : 0xFF;
       
  3536 			return true;
       
  3537 
       
  3538 		case 0x21: { // OpenTTD version
       
  3539 			extern uint32 _openttd_newgrf_version;
       
  3540 			*value = _openttd_newgrf_version;
       
  3541 			return true;
       
  3542 		}
       
  3543 
       
  3544 		default: return false;
       
  3545 	}
       
  3546 }
       
  3547 
  3438 static uint32 GetParamVal(byte param, uint32 *cond_val)
  3548 static uint32 GetParamVal(byte param, uint32 *cond_val)
  3439 {
  3549 {
       
  3550 	/* First handle variable common with VarAction2 */
       
  3551 	uint32 value;
       
  3552 	if (GetGlobalVariable(param - 0x80, &value)) return value;
       
  3553 
       
  3554 	/* Non-common variable */
  3440 	switch (param) {
  3555 	switch (param) {
  3441 		case 0x81: // current year
       
  3442 			return Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
       
  3443 
       
  3444 		case 0x83: // current climate, 0=temp, 1=arctic, 2=trop, 3=toyland
       
  3445 			return _opt.landscape;
       
  3446 
       
  3447 		case 0x84: { // GRF loading stage
  3556 		case 0x84: { // GRF loading stage
  3448 			uint32 res = 0;
  3557 			uint32 res = 0;
  3449 
  3558 
  3450 			if (_cur_stage > GLS_INIT) SetBit(res, 0);
  3559 			if (_cur_stage > GLS_INIT) SetBit(res, 0);
  3451 			if (_cur_stage == GLS_RESERVE) SetBit(res, 8);
  3560 			if (_cur_stage == GLS_RESERVE) SetBit(res, 8);
  3461 				uint32 param_val = _ttdpatch_flags[*cond_val / 0x20];
  3570 				uint32 param_val = _ttdpatch_flags[*cond_val / 0x20];
  3462 				*cond_val %= 0x20;
  3571 				*cond_val %= 0x20;
  3463 				return param_val;
  3572 				return param_val;
  3464 			}
  3573 			}
  3465 
  3574 
  3466 		case 0x86: // road traffic side, bit 4 clear=left, set=right
       
  3467 			return _opt.road_side << 4;
       
  3468 
       
  3469 		case 0x88: // GRF ID check
  3575 		case 0x88: // GRF ID check
  3470 			return 0;
  3576 			return 0;
  3471 
  3577 
  3472 		case 0x8B: { // TTDPatch version
  3578 		/* case 0x99: Global ID offest not implemented */
  3473 			uint major    = 2;
       
  3474 			uint minor    = 6;
       
  3475 			uint revision = 1; // special case: 2.0.1 is 2.0.10
       
  3476 			uint build    = 1382;
       
  3477 			return (major << 24) | (minor << 20) | (revision << 16) | build;
       
  3478 		}
       
  3479 
       
  3480 		case 0x8D: // TTD Version, 00=DOS, 01=Windows
       
  3481 			return !_use_dos_palette;
       
  3482 
       
  3483 		case 0x8E: // Y-offset for train sprites
       
  3484 			return _traininfo_vehicle_pitch;
       
  3485 
       
  3486 		case 0x92: // Game mode
       
  3487 			return _game_mode;
       
  3488 
       
  3489 		case 0x9A: // Always -1
       
  3490 			return UINT_MAX;
       
  3491 
       
  3492 		case 0x9D: // TTD Platform, 00=TTDPatch, 01=OpenTTD
       
  3493 			return 1;
       
  3494 
       
  3495 		case 0x9E: // Miscellaneous GRF features
       
  3496 			return _misc_grf_features;
       
  3497 
       
  3498 		case 0xA1: { // OpenTTD version
       
  3499 			extern uint32 _openttd_newgrf_version;
       
  3500 			return _openttd_newgrf_version;
       
  3501 		}
       
  3502 
  3579 
  3503 		default:
  3580 		default:
  3504 			/* GRF Parameter */
  3581 			/* GRF Parameter */
  3505 			if (param < 0x80) return _cur_grffile->param[param];
  3582 			if (param < 0x80) return _cur_grffile->param[param];
  3506 
  3583 
  4348 
  4425 
  4349 		case 0x9E: // Miscellaneous GRF features
  4426 		case 0x9E: // Miscellaneous GRF features
  4350 			_misc_grf_features = res;
  4427 			_misc_grf_features = res;
  4351 			/* Set train list engine width */
  4428 			/* Set train list engine width */
  4352 			_traininfo_vehicle_width = HasGrfMiscBit(GMB_TRAIN_WIDTH_32_PIXELS) ? 32 : 29;
  4429 			_traininfo_vehicle_width = HasGrfMiscBit(GMB_TRAIN_WIDTH_32_PIXELS) ? 32 : 29;
       
  4430 			break;
       
  4431 
       
  4432 		case 0x9F: // locale-dependent settings
       
  4433 			grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
  4353 			break;
  4434 			break;
  4354 
  4435 
  4355 		default:
  4436 		default:
  4356 			if (target < 0x80) {
  4437 			if (target < 0x80) {
  4357 				_cur_grffile->param[target] = res;
  4438 				_cur_grffile->param[target] = res;
  5083 	CleanUpGRFTownNames();
  5164 	CleanUpGRFTownNames();
  5084 
  5165 
  5085 	/* Copy/reset original engine info data */
  5166 	/* Copy/reset original engine info data */
  5086 	SetupEngines();
  5167 	SetupEngines();
  5087 
  5168 
  5088 	/* Copy/reset original bridge info data
  5169 	/* Copy/reset original bridge info data */
  5089 	 * First, free sprite table data */
  5170 	ResetBridges();
  5090 	for (uint i = 0; i < MAX_BRIDGES; i++) {
       
  5091 		if (_bridge[i].sprite_table != NULL) {
       
  5092 			for (uint j = 0; j < 7; j++) free(_bridge[i].sprite_table[j]);
       
  5093 			free(_bridge[i].sprite_table);
       
  5094 		}
       
  5095 	}
       
  5096 	memcpy(&_bridge, &orig_bridge, sizeof(_bridge));
       
  5097 
  5171 
  5098 	/* Reset refit/cargo class data */
  5172 	/* Reset refit/cargo class data */
  5099 	memset(&cargo_allowed, 0, sizeof(cargo_allowed));
  5173 	memset(&cargo_allowed, 0, sizeof(cargo_allowed));
  5100 	memset(&cargo_disallowed, 0, sizeof(cargo_disallowed));
  5174 	memset(&cargo_disallowed, 0, sizeof(cargo_disallowed));
  5101 
  5175 
  5105 
  5179 
  5106 	/* Unload sprite group data */
  5180 	/* Unload sprite group data */
  5107 	UnloadWagonOverrides();
  5181 	UnloadWagonOverrides();
  5108 	UnloadCustomEngineSprites();
  5182 	UnloadCustomEngineSprites();
  5109 	ResetEngineListOrder();
  5183 	ResetEngineListOrder();
       
  5184 
       
  5185 	/* Reset generic feature callback lists */
       
  5186 	ResetGenericCallbacks();
  5110 
  5187 
  5111 	/* Reset price base data */
  5188 	/* Reset price base data */
  5112 	ResetPriceBaseMultipliers();
  5189 	ResetPriceBaseMultipliers();
  5113 
  5190 
  5114 	/* Reset the curencies array */
  5191 	/* Reset the curencies array */