src/oldloader.cpp
branchNewGRF_ports
changeset 10274 b3c58f3df92b
parent 10242 52b4a9006029
child 10724 68a692eacf22
equal deleted inserted replaced
10243:e9066a148720 10274:b3c58f3df92b
    22 #include "zoom_func.h"
    22 #include "zoom_func.h"
    23 #include "functions.h"
    23 #include "functions.h"
    24 #include "date_func.h"
    24 #include "date_func.h"
    25 #include "vehicle_func.h"
    25 #include "vehicle_func.h"
    26 #include "variables.h"
    26 #include "variables.h"
       
    27 #include "strings_func.h"
       
    28 #include "effectvehicle_base.h"
    27 
    29 
    28 #include "table/strings.h"
    30 #include "table/strings.h"
    29 
    31 
    30 enum {
    32 enum {
    31 	HEADER_SIZE = 49,
    33 	HEADER_SIZE = 49,
    81 	OC_UINT16    = OC_VAR_U16  | OC_FILE_U16,
    83 	OC_UINT16    = OC_VAR_U16  | OC_FILE_U16,
    82 	OC_INT32     = OC_VAR_I32  | OC_FILE_I32,
    84 	OC_INT32     = OC_VAR_I32  | OC_FILE_I32,
    83 	OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
    85 	OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
    84 
    86 
    85 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    87 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
       
    88 
       
    89 	/**
       
    90 	 * Dereference the pointer once before writing to it,
       
    91 	 * so we do not have to use big static arrays.
       
    92 	 */
       
    93 	OC_DEREFERENCE_POINTER = 1 << 31,
    86 
    94 
    87 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    95 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    88 };
    96 };
    89 
    97 
    90 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
    98 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
   199 {
   207 {
   200 	const OldChunks *chunk = chunks;
   208 	const OldChunks *chunk = chunks;
   201 	byte *base_ptr = (byte*)base;
   209 	byte *base_ptr = (byte*)base;
   202 
   210 
   203 	while (chunk->type != OC_END) {
   211 	while (chunk->type != OC_END) {
   204 		byte* ptr = (byte*)chunk->ptr;
   212 		byte *ptr = (byte*)chunk->ptr;
   205 		uint i;
   213 		if ((chunk->type & OC_DEREFERENCE_POINTER) != 0) ptr = *(byte**)ptr;
   206 
   214 
   207 		for (i = 0; i < chunk->amount; i++) {
   215 		for (uint i = 0; i < chunk->amount; i++) {
   208 			if (ls->failed) return false;
   216 			if (ls->failed) return false;
   209 
   217 
   210 			/* Handle simple types */
   218 			/* Handle simple types */
   211 			if (GetOldChunkType(chunk->type) != 0) {
   219 			if (GetOldChunkType(chunk->type) != 0) {
   212 				switch (GetOldChunkType(chunk->type)) {
   220 				switch (GetOldChunkType(chunk->type)) {
   389 #define REMAP_TOWN_IDX(x) ((x) - (0x0459154 - 0x0458EF0)) / 94
   397 #define REMAP_TOWN_IDX(x) ((x) - (0x0459154 - 0x0458EF0)) / 94
   390 #define REMAP_ORDER_IDX(x) ((x) - (0x045AB08 - 0x0458EF0)) / 2
   398 #define REMAP_ORDER_IDX(x) ((x) - (0x045AB08 - 0x0458EF0)) / 2
   391 
   399 
   392 extern TileIndex *_animated_tile_list;
   400 extern TileIndex *_animated_tile_list;
   393 extern uint _animated_tile_count;
   401 extern uint _animated_tile_count;
   394 extern char _name_array[512][32];
   402 extern char *_old_name_array;
   395 
   403 
   396 static byte   _old_vehicle_multiplier;
   404 static byte   _old_vehicle_multiplier;
   397 static uint8  _old_map3[OLD_MAP_SIZE * 2];
   405 static uint8  *_old_map3;
   398 static bool   _new_ttdpatch_format;
   406 static bool   _new_ttdpatch_format;
   399 static uint32 _old_town_index;
   407 static uint32 _old_town_index;
   400 static uint16 _old_string_id;
   408 static uint16 _old_string_id;
   401 static uint16 _old_string_id_2;
   409 static uint16 _old_string_id_2;
   402 static uint16 _old_extra_chunk_nums;
   410 static uint16 _old_extra_chunk_nums;
  1077 	OCL_NULL( 5 ), ///< Junk
  1085 	OCL_NULL( 5 ), ///< Junk
  1078 
  1086 
  1079 	OCL_END()
  1087 	OCL_END()
  1080 };
  1088 };
  1081 
  1089 
  1082 static const OldChunks vehicle_special_chunk[] = {
  1090 static const OldChunks vehicle_effect_chunk[] = {
  1083 	OCL_SVAR( OC_UINT16, VehicleSpecial, animation_state ),
  1091 	OCL_SVAR( OC_UINT16, VehicleEffect, animation_state ),
  1084 	OCL_SVAR(  OC_UINT8, VehicleSpecial, animation_substate ),
  1092 	OCL_SVAR(  OC_UINT8, VehicleEffect, animation_substate ),
  1085 
  1093 
  1086 	OCL_NULL( 7 ), // Junk
  1094 	OCL_NULL( 7 ), // Junk
  1087 
  1095 
  1088 	OCL_END()
  1096 	OCL_END()
  1089 };
  1097 };
  1114 		case VEH_INVALID : res = LoadChunk(ls, NULL,           vehicle_empty_chunk);    break;
  1122 		case VEH_INVALID : res = LoadChunk(ls, NULL,           vehicle_empty_chunk);    break;
  1115 		case VEH_TRAIN   : res = LoadChunk(ls, &v->u.rail,     vehicle_train_chunk);    break;
  1123 		case VEH_TRAIN   : res = LoadChunk(ls, &v->u.rail,     vehicle_train_chunk);    break;
  1116 		case VEH_ROAD    : res = LoadChunk(ls, &v->u.road,     vehicle_road_chunk);     break;
  1124 		case VEH_ROAD    : res = LoadChunk(ls, &v->u.road,     vehicle_road_chunk);     break;
  1117 		case VEH_SHIP    : res = LoadChunk(ls, &v->u.ship,     vehicle_ship_chunk);     break;
  1125 		case VEH_SHIP    : res = LoadChunk(ls, &v->u.ship,     vehicle_ship_chunk);     break;
  1118 		case VEH_AIRCRAFT: res = LoadChunk(ls, &v->u.air,      vehicle_air_chunk);      break;
  1126 		case VEH_AIRCRAFT: res = LoadChunk(ls, &v->u.air,      vehicle_air_chunk);      break;
  1119 		case VEH_SPECIAL : res = LoadChunk(ls, &v->u.special,  vehicle_special_chunk);  break;
  1127 		case VEH_EFFECT  : res = LoadChunk(ls, &v->u.effect,   vehicle_effect_chunk);   break;
  1120 		case VEH_DISASTER: res = LoadChunk(ls, &v->u.disaster, vehicle_disaster_chunk); break;
  1128 		case VEH_DISASTER: res = LoadChunk(ls, &v->u.disaster, vehicle_disaster_chunk); break;
  1121 	}
  1129 	}
  1122 
  1130 
  1123 	/* This chunk size should always be 10 bytes */
  1131 	/* This chunk size should always be 10 bytes */
  1124 	if (ls->total_read - temp != 10) {
  1132 	if (ls->total_read - temp != 10) {
  1227 			case 0x00 /*VEH_INVALID */: v = new (_current_vehicle_id) InvalidVehicle();  break;
  1235 			case 0x00 /*VEH_INVALID */: v = new (_current_vehicle_id) InvalidVehicle();  break;
  1228 			case 0x10 /*VEH_TRAIN   */: v = new (_current_vehicle_id) Train();           break;
  1236 			case 0x10 /*VEH_TRAIN   */: v = new (_current_vehicle_id) Train();           break;
  1229 			case 0x11 /*VEH_ROAD    */: v = new (_current_vehicle_id) RoadVehicle();     break;
  1237 			case 0x11 /*VEH_ROAD    */: v = new (_current_vehicle_id) RoadVehicle();     break;
  1230 			case 0x12 /*VEH_SHIP    */: v = new (_current_vehicle_id) Ship();            break;
  1238 			case 0x12 /*VEH_SHIP    */: v = new (_current_vehicle_id) Ship();            break;
  1231 			case 0x13 /*VEH_AIRCRAFT*/: v = new (_current_vehicle_id) Aircraft();        break;
  1239 			case 0x13 /*VEH_AIRCRAFT*/: v = new (_current_vehicle_id) Aircraft();        break;
  1232 			case 0x14 /*VEH_SPECIAL */: v = new (_current_vehicle_id) SpecialVehicle();  break;
  1240 			case 0x14 /*VEH_EFFECT  */: v = new (_current_vehicle_id) EffectVehicle();   break;
  1233 			case 0x15 /*VEH_DISASTER*/: v = new (_current_vehicle_id) DisasterVehicle(); break;
  1241 			case 0x15 /*VEH_DISASTER*/: v = new (_current_vehicle_id) DisasterVehicle(); break;
  1234 		}
  1242 		}
  1235 		if (!LoadChunk(ls, v, vehicle_chunk)) return false;
  1243 		if (!LoadChunk(ls, v, vehicle_chunk)) return false;
  1236 
  1244 
  1237 		/* This should be consistent, else we have a big problem... */
  1245 		/* This should be consistent, else we have a big problem... */
  1261 
  1269 
  1262 		_old_string_id = RemapOldStringID(_old_string_id);
  1270 		_old_string_id = RemapOldStringID(_old_string_id);
  1263 		v->name = CopyFromOldName(_old_string_id);
  1271 		v->name = CopyFromOldName(_old_string_id);
  1264 
  1272 
  1265 		/* Vehicle-subtype is different in TTD(Patch) */
  1273 		/* Vehicle-subtype is different in TTD(Patch) */
  1266 		if (v->type == VEH_SPECIAL) v->subtype = v->subtype >> 1;
  1274 		if (v->type == VEH_EFFECT) v->subtype = v->subtype >> 1;
  1267 
  1275 
  1268 		if (_cargo_count != 0) {
  1276 		if (_cargo_count != 0) {
  1269 			CargoPacket *cp = new CargoPacket((_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source, _cargo_count);
  1277 			CargoPacket *cp = new CargoPacket((_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source, _cargo_count);
  1270 			cp->days_in_transit = _cargo_days;
  1278 			cp->days_in_transit = _cargo_days;
  1271 			v->cargo.Append(cp);
  1279 			v->cargo.Append(cp);
  1519 
  1527 
  1520 	OCL_CHUNK( 850, LoadOldVehicle ),
  1528 	OCL_CHUNK( 850, LoadOldVehicle ),
  1521 
  1529 
  1522 	OCL_ASSERT( 0x6F0F2 ),
  1530 	OCL_ASSERT( 0x6F0F2 ),
  1523 
  1531 
  1524 	OCL_VAR (  OC_UINT8, 32 * 500, &_name_array[0] ),
  1532 	OCL_VAR (  OC_UINT8 | OC_DEREFERENCE_POINTER, 32 * 500, &_old_name_array ),
  1525 
  1533 
  1526 	OCL_NULL( 0x2000 ),            ///< Old hash-table, no longer in use
  1534 	OCL_NULL( 0x2000 ),            ///< Old hash-table, no longer in use
  1527 
  1535 
  1528 	OCL_CHUNK( 40, LoadOldSign ),
  1536 	OCL_CHUNK( 40, LoadOldSign ),
  1529 	OCL_CHUNK(256, LoadOldEngine ),
  1537 	OCL_CHUNK(256, LoadOldEngine ),
  1599 	/* The first 49 is the name of the game + checksum, skip it */
  1607 	/* The first 49 is the name of the game + checksum, skip it */
  1600 	fseek(ls->file, HEADER_SIZE, SEEK_SET);
  1608 	fseek(ls->file, HEADER_SIZE, SEEK_SET);
  1601 
  1609 
  1602 	DEBUG(oldloader, 3, "Reading main chunk...");
  1610 	DEBUG(oldloader, 3, "Reading main chunk...");
  1603 	/* Load the biggest chunk */
  1611 	/* Load the biggest chunk */
       
  1612 	_old_map3 = MallocT<byte>(OLD_MAP_SIZE * 2);
  1604 	if (!LoadChunk(ls, NULL, main_chunk)) {
  1613 	if (!LoadChunk(ls, NULL, main_chunk)) {
  1605 		DEBUG(oldloader, 0, "Loading failed");
  1614 		DEBUG(oldloader, 0, "Loading failed");
       
  1615 		free(_old_map3);
  1606 		return false;
  1616 		return false;
  1607 	}
  1617 	}
  1608 	DEBUG(oldloader, 3, "Done, converting game data...");
  1618 	DEBUG(oldloader, 3, "Done, converting game data...");
  1609 
  1619 
  1610 	/* Fix some general stuff */
  1620 	/* Fix some general stuff */
  1667 	_opt.diff.town_council_tolerance = Clamp(_opt.diff_level, 0, 2);
  1677 	_opt.diff.town_council_tolerance = Clamp(_opt.diff_level, 0, 2);
  1668 
  1678 
  1669 	DEBUG(oldloader, 3, "Finished converting game data");
  1679 	DEBUG(oldloader, 3, "Finished converting game data");
  1670 	DEBUG(oldloader, 1, "TTD(Patch) savegame successfully converted");
  1680 	DEBUG(oldloader, 1, "TTD(Patch) savegame successfully converted");
  1671 
  1681 
       
  1682 	free(_old_map3);
       
  1683 
  1672 	return true;
  1684 	return true;
  1673 }
  1685 }
  1674 
  1686 
  1675 bool LoadOldSaveGame(const char *file)
  1687 bool LoadOldSaveGame(const char *file)
  1676 {
  1688 {