src/saveload.cpp
branchnoai
changeset 9732 f8eb3e208514
parent 9724 b39bc69bb2f2
child 9800 ab08ca2a2018
equal deleted inserted replaced
9731:9b1552d0fd9b 9732:f8eb3e208514
    32 #include "autoreplace_base.h"
    32 #include "autoreplace_base.h"
    33 #include <list>
    33 #include <list>
    34 
    34 
    35 #include "table/strings.h"
    35 #include "table/strings.h"
    36 
    36 
    37 extern const uint16 SAVEGAME_VERSION = 86;
    37 extern const uint16 SAVEGAME_VERSION = 89;
    38 uint16 _sl_version;       ///< the major savegame version identifier
    38 uint16 _sl_version;       ///< the major savegame version identifier
    39 byte   _sl_minor_version; ///< the minor savegame version, DO NOT USE!
    39 byte   _sl_minor_version; ///< the minor savegame version, DO NOT USE!
    40 
    40 
    41 typedef void WriterProc(uint len);
    41 typedef void WriterProc(uint len);
    42 typedef uint ReaderProc();
    42 typedef uint ReaderProc();
  1327  */
  1327  */
  1328 static void *IntToReference(uint index, SLRefType rt)
  1328 static void *IntToReference(uint index, SLRefType rt)
  1329 {
  1329 {
  1330 	/* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
  1330 	/* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
  1331 	 * and should be loaded like that */
  1331 	 * and should be loaded like that */
  1332 	if (rt == REF_VEHICLE_OLD && !CheckSavegameVersionOldStyle(4, 4))
  1332 	if (rt == REF_VEHICLE_OLD && !CheckSavegameVersionOldStyle(4, 4)) {
  1333 		rt = REF_VEHICLE;
  1333 		rt = REF_VEHICLE;
       
  1334 	}
  1334 
  1335 
  1335 	/* No need to look up NULL pointers, just return immediately */
  1336 	/* No need to look up NULL pointers, just return immediately */
  1336 	if (rt != REF_VEHICLE_OLD && index == 0)
  1337 	if (rt != REF_VEHICLE_OLD && index == 0) {
  1337 		return NULL;
  1338 		return NULL;
       
  1339 	}
  1338 
  1340 
  1339 	index--; // correct for the NULL index
  1341 	index--; // correct for the NULL index
  1340 
  1342 
  1341 	switch (rt) {
  1343 	switch (rt) {
  1342 		case REF_ORDER: {
  1344 		case REF_ORDER:
  1343 			if (!_Order_pool.AddBlockIfNeeded(index))
  1345 			if (_Order_pool.AddBlockIfNeeded(index)) return GetOrder(index);
  1344 				error("Orders: failed loading savegame: too many orders");
  1346 			error("Orders: failed loading savegame: too many orders");
  1345 			return GetOrder(index);
  1347 
  1346 		}
  1348 		case REF_VEHICLE:
  1347 		case REF_VEHICLE: {
  1349 			if (_Vehicle_pool.AddBlockIfNeeded(index)) return GetVehicle(index);
  1348 			if (!_Vehicle_pool.AddBlockIfNeeded(index))
  1350 			error("Vehicles: failed loading savegame: too many vehicles");
  1349 				error("Vehicles: failed loading savegame: too many vehicles");
  1351 
  1350 			return GetVehicle(index);
  1352 		case REF_STATION:
  1351 		}
  1353 			if (_Station_pool.AddBlockIfNeeded(index)) return GetStation(index);
  1352 		case REF_STATION: {
  1354 			error("Stations: failed loading savegame: too many stations");
  1353 			if (!_Station_pool.AddBlockIfNeeded(index))
  1355 
  1354 				error("Stations: failed loading savegame: too many stations");
  1356 		case REF_TOWN:
  1355 			return GetStation(index);
  1357 			if (_Town_pool.AddBlockIfNeeded(index)) return GetTown(index);
  1356 		}
  1358 			error("Towns: failed loading savegame: too many towns");
  1357 		case REF_TOWN: {
  1359 
  1358 			if (!_Town_pool.AddBlockIfNeeded(index))
  1360 		case REF_ROADSTOPS:
  1359 				error("Towns: failed loading savegame: too many towns");
  1361 			if (_RoadStop_pool.AddBlockIfNeeded(index)) return GetRoadStop(index);
  1360 			return GetTown(index);
  1362 			error("RoadStops: failed loading savegame: too many RoadStops");
  1361 		}
  1363 
  1362 		case REF_ROADSTOPS: {
  1364 		case REF_ENGINE_RENEWS:
  1363 			if (!_RoadStop_pool.AddBlockIfNeeded(index))
  1365 			if (_EngineRenew_pool.AddBlockIfNeeded(index)) return GetEngineRenew(index);
  1364 				error("RoadStops: failed loading savegame: too many RoadStops");
  1366 			error("EngineRenews: failed loading savegame: too many EngineRenews");
  1365 			return GetRoadStop(index);
  1367 
  1366 		}
  1368 		case REF_CARGO_PACKET:
  1367 		case REF_ENGINE_RENEWS: {
  1369 			if (_CargoPacket_pool.AddBlockIfNeeded(index)) return GetCargoPacket(index);
  1368 			if (!_EngineRenew_pool.AddBlockIfNeeded(index))
  1370 			error("CargoPackets: failed loading savegame: too many Cargo packets");
  1369 				error("EngineRenews: failed loading savegame: too many EngineRenews");
  1371 
  1370 			return GetEngineRenew(index);
  1372 		case REF_VEHICLE_OLD:
  1371 		}
       
  1372 		case REF_CARGO_PACKET: {
       
  1373 			if (!_CargoPacket_pool.AddBlockIfNeeded(index))
       
  1374 				error("CargoPackets: failed loading savegame: too many Cargo packets");
       
  1375 			return GetCargoPacket(index);
       
  1376 		}
       
  1377 
       
  1378 		case REF_VEHICLE_OLD: {
       
  1379 			/* Old vehicles were saved differently:
  1373 			/* Old vehicles were saved differently:
  1380 			 * invalid vehicle was 0xFFFF,
  1374 			 * invalid vehicle was 0xFFFF,
  1381 			 * and the index was not - 1.. correct for this */
  1375 			 * and the index was not - 1.. correct for this */
  1382 			index++;
  1376 			index++;
  1383 			if (index == INVALID_VEHICLE)
  1377 			if (index == INVALID_VEHICLE) return NULL;
  1384 				return NULL;
  1378 
  1385 
  1379 			if (_Vehicle_pool.AddBlockIfNeeded(index)) return GetVehicle(index);
  1386 			if (!_Vehicle_pool.AddBlockIfNeeded(index))
  1380 			error("Vehicles: failed loading savegame: too many vehicles");
  1387 				error("Vehicles: failed loading savegame: too many vehicles");
  1381 
  1388 			return GetVehicle(index);
       
  1389 		}
       
  1390 		default: NOT_REACHED();
  1382 		default: NOT_REACHED();
  1391 	}
  1383 	}
  1392 
  1384 
  1393 	return NULL;
  1385 	return NULL;
  1394 }
  1386 }