src/oldloader.cpp
branchgamebalance
changeset 9895 7bd07f43b0e3
parent 6434 d12a3ab5acbc
child 9903 dc85aaa556ae
equal deleted inserted replaced
9894:70d78ac95d6c 9895:7bd07f43b0e3
    25 	BUFFER_SIZE = 4096,
    25 	BUFFER_SIZE = 4096,
    26 
    26 
    27 	OLD_MAP_SIZE = 256 * 256
    27 	OLD_MAP_SIZE = 256 * 256
    28 };
    28 };
    29 
    29 
    30 typedef struct LoadgameState {
    30 struct LoadgameState {
    31 	FILE *file;
    31 	FILE *file;
    32 
    32 
    33 	uint chunk_size;
    33 	uint chunk_size;
    34 
    34 
    35 	bool decoding;
    35 	bool decoding;
    39 	uint buffer_cur;
    39 	uint buffer_cur;
    40 	byte buffer[BUFFER_SIZE];
    40 	byte buffer[BUFFER_SIZE];
    41 
    41 
    42 	uint total_read;
    42 	uint total_read;
    43 	bool failed;
    43 	bool failed;
    44 } LoadgameState;
    44 };
    45 
    45 
    46 /* OldChunk-Type */
    46 /* OldChunk-Type */
    47 typedef enum OldChunkTypes {
    47 enum OldChunkType {
    48 	OC_SIMPLE    = 0,
    48 	OC_SIMPLE    = 0,
    49 	OC_NULL      = 1,
    49 	OC_NULL      = 1,
    50 	OC_CHUNK     = 2,
    50 	OC_CHUNK     = 2,
    51 	OC_ASSERT    = 3,
    51 	OC_ASSERT    = 3,
    52 	/* 8 bytes allocated (256 max) */
    52 	/* 8 bytes allocated (256 max) */
    76 	OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
    76 	OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
    77 
    77 
    78 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    78 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    79 
    79 
    80 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    80 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    81 } OldChunkType;
    81 };
    82 
    82 
    83 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
    83 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
    84 
    84 
    85 typedef bool OldChunkProc(LoadgameState *ls, int num);
    85 typedef bool OldChunkProc(LoadgameState *ls, int num);
    86 
    86 
    87 typedef struct OldChunks {
    87 struct OldChunks {
    88 	OldChunkType type;   ///< Type of field
    88 	OldChunkType type;   ///< Type of field
    89 	uint32 amount;       ///< Amount of fields
    89 	uint32 amount;       ///< Amount of fields
    90 
    90 
    91 	void *ptr;           ///< Pointer where to save the data (may only be set if offset is 0)
    91 	void *ptr;           ///< Pointer where to save the data (may only be set if offset is 0)
    92 	uint offset;         ///< Offset from basepointer (may only be set if ptr is NULL)
    92 	uint offset;         ///< Offset from basepointer (may only be set if ptr is NULL)
    93 	OldChunkProc *proc;  ///< Pointer to function that is called with OC_CHUNK
    93 	OldChunkProc *proc;  ///< Pointer to function that is called with OC_CHUNK
    94 } OldChunks;
    94 };
    95 
    95 
    96 /* If it fails, check lines above.. */
    96 /* If it fails, check lines above.. */
    97 assert_compile(sizeof(TileIndex) == 4);
    97 assert_compile(sizeof(TileIndex) == 4);
    98 
    98 
    99 static uint32 _bump_assert_value;
    99 static uint32 _bump_assert_value;
   288  * Begin -- Stuff to fix the savegames to be OpenTTD compatible
   288  * Begin -- Stuff to fix the savegames to be OpenTTD compatible
   289  */
   289  */
   290 
   290 
   291 extern uint32 GetOldTownName(uint32 townnameparts, byte old_town_name_type);
   291 extern uint32 GetOldTownName(uint32 townnameparts, byte old_town_name_type);
   292 
   292 
   293 static void FixOldTowns(void)
   293 static void FixOldTowns()
   294 {
   294 {
   295 	Town *town;
   295 	Town *town;
   296 
   296 
   297 	/* Convert town-names if needed */
   297 	/* Convert town-names if needed */
   298 	FOR_ALL_TOWNS(town) {
   298 	FOR_ALL_TOWNS(town) {
   301 			town->townnameparts = GetOldTownName(town->townnameparts, _opt.town_name);
   301 			town->townnameparts = GetOldTownName(town->townnameparts, _opt.town_name);
   302 		}
   302 		}
   303 	}
   303 	}
   304 }
   304 }
   305 
   305 
   306 static void FixOldStations(void)
   306 static void FixOldStations()
   307 {
   307 {
   308 	Station *st;
   308 	Station *st;
   309 
   309 
   310 	FOR_ALL_STATIONS(st) {
   310 	FOR_ALL_STATIONS(st) {
   311 		/* Check if we need to swap width and height for the station */
   311 		/* Check if we need to swap width and height for the station */
   313 			Swap(st->trainst_w, st->trainst_h);
   313 			Swap(st->trainst_w, st->trainst_h);
   314 		}
   314 		}
   315 	}
   315 	}
   316 }
   316 }
   317 
   317 
   318 static void FixOldVehicles(void)
   318 static void FixOldVehicles()
   319 {
   319 {
   320 	/* Check for shared orders, and link them correctly */
   320 	/* Check for shared orders, and link them correctly */
   321 	Vehicle* v;
   321 	Vehicle* v;
   322 
   322 
   323 	FOR_ALL_VEHICLES(v) {
   323 	FOR_ALL_VEHICLES(v) {
   372 static uint32 _old_town_index;
   372 static uint32 _old_town_index;
   373 static uint16 _old_string_id;
   373 static uint16 _old_string_id;
   374 static uint16 _old_string_id_2;
   374 static uint16 _old_string_id_2;
   375 static uint16 _old_extra_chunk_nums;
   375 static uint16 _old_extra_chunk_nums;
   376 
   376 
   377 static void ReadTTDPatchFlags(void)
   377 static void ReadTTDPatchFlags()
   378 {
   378 {
   379 	int i;
   379 	int i;
   380 
   380 
   381 	if (_read_ttdpatch_flags) return;
   381 	if (_read_ttdpatch_flags) return;
   382 
   382 
   484 	if (!LoadChunk(ls, NULL, order_chunk)) return false;
   484 	if (!LoadChunk(ls, NULL, order_chunk)) return false;
   485 
   485 
   486 	AssignOrder(GetOrder(num), UnpackOldOrder(_old_order));
   486 	AssignOrder(GetOrder(num), UnpackOldOrder(_old_order));
   487 
   487 
   488 	/* Relink the orders to eachother (in TTD(Patch) the orders for one
   488 	/* Relink the orders to eachother (in TTD(Patch) the orders for one
   489 	vehicle are behind eachother, with OT_NOTHING as indication that
   489 	vehicle are behind eachother, with an invalid order (OT_NOTHING) as indication that
   490 	it is the last order */
   490 	it is the last order */
   491 	if (num > 0 && GetOrder(num)->type != OT_NOTHING)
   491 	if (num > 0 && GetOrder(num)->IsValid())
   492 		GetOrder(num - 1)->next = GetOrder(num);
   492 		GetOrder(num - 1)->next = GetOrder(num);
   493 
   493 
   494 	return true;
   494 	return true;
   495 }
   495 }
   496 
   496 
  1066 
  1066 
  1067 	/* We changed the offset of the vehicle types, so fix it
  1067 	/* We changed the offset of the vehicle types, so fix it
  1068 	 * Basically v->type -= 0x10; would suffice, but play safely */
  1068 	 * Basically v->type -= 0x10; would suffice, but play safely */
  1069 	switch (v->type) {
  1069 	switch (v->type) {
  1070 		default: NOT_REACHED();
  1070 		default: NOT_REACHED();
  1071 		case 0x00 /*VEH_Invalid */: v->type = VEH_Invalid;  res = LoadChunk(ls, NULL,           vehicle_empty_chunk);    break;
  1071 		case 0x00 /*VEH_INVALID */: v->type = VEH_INVALID;  res = LoadChunk(ls, NULL,           vehicle_empty_chunk);    break;
  1072 		case 0x10 /*VEH_Train   */: v->type = VEH_Train;    res = LoadChunk(ls, &v->u.rail,     vehicle_train_chunk);    break;
  1072 		case 0x10 /*VEH_TRAIN   */: v->type = VEH_TRAIN;    res = LoadChunk(ls, &v->u.rail,     vehicle_train_chunk);    break;
  1073 		case 0x11 /*VEH_Road    */: v->type = VEH_Road;     res = LoadChunk(ls, &v->u.road,     vehicle_road_chunk);     break;
  1073 		case 0x11 /*VEH_ROAD    */: v->type = VEH_ROAD;     res = LoadChunk(ls, &v->u.road,     vehicle_road_chunk);     break;
  1074 		case 0x12 /*VEH_Ship    */: v->type = VEH_Ship;     res = LoadChunk(ls, &v->u.ship,     vehicle_ship_chunk);     break;
  1074 		case 0x12 /*VEH_SHIP    */: v->type = VEH_SHIP;     res = LoadChunk(ls, &v->u.ship,     vehicle_ship_chunk);     break;
  1075 		case 0x13 /*VEH_Aircraft*/: v->type = VEH_Aircraft; res = LoadChunk(ls, &v->u.air,      vehicle_air_chunk);      break;
  1075 		case 0x13 /*VEH_AIRCRAFT*/: v->type = VEH_AIRCRAFT; res = LoadChunk(ls, &v->u.air,      vehicle_air_chunk);      break;
  1076 		case 0x14 /*VEH_Special */: v->type = VEH_Special;  res = LoadChunk(ls, &v->u.special,  vehicle_special_chunk);  break;
  1076 		case 0x14 /*VEH_SPECIAL */: v->type = VEH_SPECIAL;  res = LoadChunk(ls, &v->u.special,  vehicle_special_chunk);  break;
  1077 		case 0x15 /*VEH_Disaster*/: v->type = VEH_Disaster; res = LoadChunk(ls, &v->u.disaster, vehicle_disaster_chunk); break;
  1077 		case 0x15 /*VEH_DISASTER*/: v->type = VEH_DISASTER; res = LoadChunk(ls, &v->u.disaster, vehicle_disaster_chunk); break;
  1078 	}
  1078 	}
  1079 
  1079 
  1080 	/* This chunk size should always be 10 bytes */
  1080 	/* This chunk size should always be 10 bytes */
  1081 	if (ls->total_read - temp != 10) {
  1081 	if (ls->total_read - temp != 10) {
  1082 		DEBUG(oldloader, 0, "Assert failed in VehicleUnion: invalid chunk size");
  1082 		DEBUG(oldloader, 0, "Assert failed in VehicleUnion: invalid chunk size");
  1207 		if (_old_next_ptr != 0xFFFF) v->next = GetVehicle(_old_next_ptr);
  1207 		if (_old_next_ptr != 0xFFFF) v->next = GetVehicle(_old_next_ptr);
  1208 
  1208 
  1209 		v->string_id = RemapOldStringID(_old_string_id);
  1209 		v->string_id = RemapOldStringID(_old_string_id);
  1210 
  1210 
  1211 		/* Vehicle-subtype is different in TTD(Patch) */
  1211 		/* Vehicle-subtype is different in TTD(Patch) */
  1212 		if (v->type == VEH_Special) v->subtype = v->subtype >> 1;
  1212 		if (v->type == VEH_SPECIAL) v->subtype = v->subtype >> 1;
  1213 	}
  1213 	}
  1214 
  1214 
  1215 	return true;
  1215 	return true;
  1216 }
  1216 }
  1217 
  1217 
  1609 	/* Load the main chunk */
  1609 	/* Load the main chunk */
  1610 	if (!LoadOldMain(&ls)) return false;
  1610 	if (!LoadOldMain(&ls)) return false;
  1611 
  1611 
  1612 	fclose(ls.file);
  1612 	fclose(ls.file);
  1613 
  1613 
  1614 	_pause = 2;
  1614 	_pause_game = 2;
  1615 
  1615 
  1616 	return true;
  1616 	return true;
  1617 }
  1617 }
  1618 
  1618 
  1619 void GetOldSaveGameName(char *title, const char *path, const char *file)
  1619 void GetOldSaveGameName(char *title, const char *path, const char *file)