src/oldloader.cpp
branchnoai
changeset 10294 7798ae816af8
parent 10249 58810805030e
child 10455 22c441f5adf9
equal deleted inserted replaced
10292:7856e972f8aa 10294:7798ae816af8
    21 #include "zoom_func.h"
    21 #include "zoom_func.h"
    22 #include "functions.h"
    22 #include "functions.h"
    23 #include "date_func.h"
    23 #include "date_func.h"
    24 #include "vehicle_func.h"
    24 #include "vehicle_func.h"
    25 #include "variables.h"
    25 #include "variables.h"
    26 #include "settings_type.h"
    26 #include "strings_func.h"
       
    27 #include "effectvehicle_base.h"
    27 
    28 
    28 #include "table/strings.h"
    29 #include "table/strings.h"
    29 
    30 
    30 enum {
    31 enum {
    31 	HEADER_SIZE = 49,
    32 	HEADER_SIZE = 49,
    82 	OC_INT32     = OC_VAR_I32  | OC_FILE_I32,
    83 	OC_INT32     = OC_VAR_I32  | OC_FILE_I32,
    83 	OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
    84 	OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
    84 
    85 
    85 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    86 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    86 
    87 
       
    88 	/**
       
    89 	 * Dereference the pointer once before writing to it,
       
    90 	 * so we do not have to use big static arrays.
       
    91 	 */
       
    92 	OC_DEREFERENCE_POINTER = 1 << 31,
       
    93 
    87 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    94 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    88 };
    95 };
    89 
    96 
    90 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
    97 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
    91 
    98 
   199 {
   206 {
   200 	const OldChunks *chunk = chunks;
   207 	const OldChunks *chunk = chunks;
   201 	byte *base_ptr = (byte*)base;
   208 	byte *base_ptr = (byte*)base;
   202 
   209 
   203 	while (chunk->type != OC_END) {
   210 	while (chunk->type != OC_END) {
   204 		byte* ptr = (byte*)chunk->ptr;
   211 		byte *ptr = (byte*)chunk->ptr;
   205 		uint i;
   212 		if ((chunk->type & OC_DEREFERENCE_POINTER) != 0) ptr = *(byte**)ptr;
   206 
   213 
   207 		for (i = 0; i < chunk->amount; i++) {
   214 		for (uint i = 0; i < chunk->amount; i++) {
   208 			if (ls->failed) return false;
   215 			if (ls->failed) return false;
   209 
   216 
   210 			/* Handle simple types */
   217 			/* Handle simple types */
   211 			if (GetOldChunkType(chunk->type) != 0) {
   218 			if (GetOldChunkType(chunk->type) != 0) {
   212 				switch (GetOldChunkType(chunk->type)) {
   219 				switch (GetOldChunkType(chunk->type)) {
   389 #define REMAP_TOWN_IDX(x) ((x) - (0x0459154 - 0x0458EF0)) / 94
   396 #define REMAP_TOWN_IDX(x) ((x) - (0x0459154 - 0x0458EF0)) / 94
   390 #define REMAP_ORDER_IDX(x) ((x) - (0x045AB08 - 0x0458EF0)) / 2
   397 #define REMAP_ORDER_IDX(x) ((x) - (0x045AB08 - 0x0458EF0)) / 2
   391 
   398 
   392 extern TileIndex *_animated_tile_list;
   399 extern TileIndex *_animated_tile_list;
   393 extern uint _animated_tile_count;
   400 extern uint _animated_tile_count;
   394 extern char _name_array[512][32];
   401 extern char *_old_name_array;
   395 
   402 
   396 static byte   _old_vehicle_multiplier;
   403 static byte   _old_vehicle_multiplier;
   397 static uint8  _old_map3[OLD_MAP_SIZE * 2];
   404 static uint8  *_old_map3;
   398 static bool   _new_ttdpatch_format;
   405 static bool   _new_ttdpatch_format;
   399 static uint32 _old_town_index;
   406 static uint32 _old_town_index;
   400 static uint16 _old_string_id;
   407 static uint16 _old_string_id;
   401 static uint16 _old_string_id_2;
   408 static uint16 _old_string_id_2;
   402 static uint16 _old_extra_chunk_nums;
   409 static uint16 _old_extra_chunk_nums;
   817 	OCL_SVAR(  OC_UINT8, Player, quarters_of_bankrupcy ),
   824 	OCL_SVAR(  OC_UINT8, Player, quarters_of_bankrupcy ),
   818 	OCL_SVAR(  OC_UINT8, Player, bankrupt_asked ),
   825 	OCL_SVAR(  OC_UINT8, Player, bankrupt_asked ),
   819 	OCL_SVAR( OC_FILE_U32 | OC_VAR_I64, Player, bankrupt_value ),
   826 	OCL_SVAR( OC_FILE_U32 | OC_VAR_I64, Player, bankrupt_value ),
   820 	OCL_SVAR( OC_UINT16, Player, bankrupt_timeout ),
   827 	OCL_SVAR( OC_UINT16, Player, bankrupt_timeout ),
   821 
   828 
   822 	OCL_SVAR( OC_FILE_U32 | OC_VAR_U16, Player, cargo_types ),
   829 	OCL_SVAR( OC_UINT32, Player, cargo_types ),
   823 
   830 
   824 	OCL_CHUNK( 3, OldPlayerYearly ),
   831 	OCL_CHUNK( 3, OldPlayerYearly ),
   825 	OCL_CHUNK( 1, OldPlayerEconomy ),
   832 	OCL_CHUNK( 1, OldPlayerEconomy ),
   826 
   833 
   827 	OCL_SVAR( OC_FILE_U16 | OC_VAR_I32, Player, inaugurated_year),
   834 	OCL_SVAR( OC_FILE_U16 | OC_VAR_I32, Player, inaugurated_year),
   930 	OCL_NULL( 5 ), ///< Junk
   937 	OCL_NULL( 5 ), ///< Junk
   931 
   938 
   932 	OCL_END()
   939 	OCL_END()
   933 };
   940 };
   934 
   941 
   935 static const OldChunks vehicle_special_chunk[] = {
   942 static const OldChunks vehicle_effect_chunk[] = {
   936 	OCL_SVAR( OC_UINT16, VehicleSpecial, animation_state ),
   943 	OCL_SVAR( OC_UINT16, VehicleEffect, animation_state ),
   937 	OCL_SVAR(  OC_UINT8, VehicleSpecial, animation_substate ),
   944 	OCL_SVAR(  OC_UINT8, VehicleEffect, animation_substate ),
   938 
   945 
   939 	OCL_NULL( 7 ), // Junk
   946 	OCL_NULL( 7 ), // Junk
   940 
   947 
   941 	OCL_END()
   948 	OCL_END()
   942 };
   949 };
   967 		case VEH_INVALID : res = LoadChunk(ls, NULL,           vehicle_empty_chunk);    break;
   974 		case VEH_INVALID : res = LoadChunk(ls, NULL,           vehicle_empty_chunk);    break;
   968 		case VEH_TRAIN   : res = LoadChunk(ls, &v->u.rail,     vehicle_train_chunk);    break;
   975 		case VEH_TRAIN   : res = LoadChunk(ls, &v->u.rail,     vehicle_train_chunk);    break;
   969 		case VEH_ROAD    : res = LoadChunk(ls, &v->u.road,     vehicle_road_chunk);     break;
   976 		case VEH_ROAD    : res = LoadChunk(ls, &v->u.road,     vehicle_road_chunk);     break;
   970 		case VEH_SHIP    : res = LoadChunk(ls, &v->u.ship,     vehicle_ship_chunk);     break;
   977 		case VEH_SHIP    : res = LoadChunk(ls, &v->u.ship,     vehicle_ship_chunk);     break;
   971 		case VEH_AIRCRAFT: res = LoadChunk(ls, &v->u.air,      vehicle_air_chunk);      break;
   978 		case VEH_AIRCRAFT: res = LoadChunk(ls, &v->u.air,      vehicle_air_chunk);      break;
   972 		case VEH_SPECIAL : res = LoadChunk(ls, &v->u.special,  vehicle_special_chunk);  break;
   979 		case VEH_EFFECT  : res = LoadChunk(ls, &v->u.effect,   vehicle_effect_chunk);   break;
   973 		case VEH_DISASTER: res = LoadChunk(ls, &v->u.disaster, vehicle_disaster_chunk); break;
   980 		case VEH_DISASTER: res = LoadChunk(ls, &v->u.disaster, vehicle_disaster_chunk); break;
   974 	}
   981 	}
   975 
   982 
   976 	/* This chunk size should always be 10 bytes */
   983 	/* This chunk size should always be 10 bytes */
   977 	if (ls->total_read - temp != 10) {
   984 	if (ls->total_read - temp != 10) {
  1080 			case 0x00 /*VEH_INVALID */: v = new (_current_vehicle_id) InvalidVehicle();  break;
  1087 			case 0x00 /*VEH_INVALID */: v = new (_current_vehicle_id) InvalidVehicle();  break;
  1081 			case 0x10 /*VEH_TRAIN   */: v = new (_current_vehicle_id) Train();           break;
  1088 			case 0x10 /*VEH_TRAIN   */: v = new (_current_vehicle_id) Train();           break;
  1082 			case 0x11 /*VEH_ROAD    */: v = new (_current_vehicle_id) RoadVehicle();     break;
  1089 			case 0x11 /*VEH_ROAD    */: v = new (_current_vehicle_id) RoadVehicle();     break;
  1083 			case 0x12 /*VEH_SHIP    */: v = new (_current_vehicle_id) Ship();            break;
  1090 			case 0x12 /*VEH_SHIP    */: v = new (_current_vehicle_id) Ship();            break;
  1084 			case 0x13 /*VEH_AIRCRAFT*/: v = new (_current_vehicle_id) Aircraft();        break;
  1091 			case 0x13 /*VEH_AIRCRAFT*/: v = new (_current_vehicle_id) Aircraft();        break;
  1085 			case 0x14 /*VEH_SPECIAL */: v = new (_current_vehicle_id) SpecialVehicle();  break;
  1092 			case 0x14 /*VEH_EFFECT  */: v = new (_current_vehicle_id) EffectVehicle();   break;
  1086 			case 0x15 /*VEH_DISASTER*/: v = new (_current_vehicle_id) DisasterVehicle(); break;
  1093 			case 0x15 /*VEH_DISASTER*/: v = new (_current_vehicle_id) DisasterVehicle(); break;
  1087 		}
  1094 		}
  1088 		if (!LoadChunk(ls, v, vehicle_chunk)) return false;
  1095 		if (!LoadChunk(ls, v, vehicle_chunk)) return false;
  1089 
  1096 
  1090 		/* This should be consistent, else we have a big problem... */
  1097 		/* This should be consistent, else we have a big problem... */
  1114 
  1121 
  1115 		_old_string_id = RemapOldStringID(_old_string_id);
  1122 		_old_string_id = RemapOldStringID(_old_string_id);
  1116 		v->name = CopyFromOldName(_old_string_id);
  1123 		v->name = CopyFromOldName(_old_string_id);
  1117 
  1124 
  1118 		/* Vehicle-subtype is different in TTD(Patch) */
  1125 		/* Vehicle-subtype is different in TTD(Patch) */
  1119 		if (v->type == VEH_SPECIAL) v->subtype = v->subtype >> 1;
  1126 		if (v->type == VEH_EFFECT) v->subtype = v->subtype >> 1;
  1120 
  1127 
  1121 		if (_cargo_count != 0) {
  1128 		if (_cargo_count != 0) {
  1122 			CargoPacket *cp = new CargoPacket((_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source, _cargo_count);
  1129 			CargoPacket *cp = new CargoPacket((_cargo_source == 0xFF) ? INVALID_STATION : _cargo_source, _cargo_count);
  1123 			cp->days_in_transit = _cargo_days;
  1130 			cp->days_in_transit = _cargo_days;
  1124 			v->cargo.Append(cp);
  1131 			v->cargo.Append(cp);
  1372 
  1379 
  1373 	OCL_CHUNK( 850, LoadOldVehicle ),
  1380 	OCL_CHUNK( 850, LoadOldVehicle ),
  1374 
  1381 
  1375 	OCL_ASSERT( 0x6F0F2 ),
  1382 	OCL_ASSERT( 0x6F0F2 ),
  1376 
  1383 
  1377 	OCL_VAR (  OC_UINT8, 32 * 500, &_name_array[0] ),
  1384 	OCL_VAR (  OC_UINT8 | OC_DEREFERENCE_POINTER, 32 * 500, &_old_name_array ),
  1378 
  1385 
  1379 	OCL_NULL( 0x2000 ),            ///< Old hash-table, no longer in use
  1386 	OCL_NULL( 0x2000 ),            ///< Old hash-table, no longer in use
  1380 
  1387 
  1381 	OCL_CHUNK( 40, LoadOldSign ),
  1388 	OCL_CHUNK( 40, LoadOldSign ),
  1382 	OCL_CHUNK(256, LoadOldEngine ),
  1389 	OCL_CHUNK(256, LoadOldEngine ),
  1452 	/* The first 49 is the name of the game + checksum, skip it */
  1459 	/* The first 49 is the name of the game + checksum, skip it */
  1453 	fseek(ls->file, HEADER_SIZE, SEEK_SET);
  1460 	fseek(ls->file, HEADER_SIZE, SEEK_SET);
  1454 
  1461 
  1455 	DEBUG(oldloader, 3, "Reading main chunk...");
  1462 	DEBUG(oldloader, 3, "Reading main chunk...");
  1456 	/* Load the biggest chunk */
  1463 	/* Load the biggest chunk */
       
  1464 	_old_map3 = MallocT<byte>(OLD_MAP_SIZE * 2);
  1457 	if (!LoadChunk(ls, NULL, main_chunk)) {
  1465 	if (!LoadChunk(ls, NULL, main_chunk)) {
  1458 		DEBUG(oldloader, 0, "Loading failed");
  1466 		DEBUG(oldloader, 0, "Loading failed");
       
  1467 		free(_old_map3);
  1459 		return false;
  1468 		return false;
  1460 	}
  1469 	}
  1461 	DEBUG(oldloader, 3, "Done, converting game data...");
  1470 	DEBUG(oldloader, 3, "Done, converting game data...");
  1462 
  1471 
  1463 	/* Fix some general stuff */
  1472 	/* Fix some general stuff */
  1520 	_opt.diff.town_council_tolerance = Clamp(_opt.diff_level, 0, 2);
  1529 	_opt.diff.town_council_tolerance = Clamp(_opt.diff_level, 0, 2);
  1521 
  1530 
  1522 	DEBUG(oldloader, 3, "Finished converting game data");
  1531 	DEBUG(oldloader, 3, "Finished converting game data");
  1523 	DEBUG(oldloader, 1, "TTD(Patch) savegame successfully converted");
  1532 	DEBUG(oldloader, 1, "TTD(Patch) savegame successfully converted");
  1524 
  1533 
       
  1534 	free(_old_map3);
       
  1535 
  1525 	return true;
  1536 	return true;
  1526 }
  1537 }
  1527 
  1538 
  1528 bool LoadOldSaveGame(const char *file)
  1539 bool LoadOldSaveGame(const char *file)
  1529 {
  1540 {