Changeset - r28472:c0dc7fe1d8ca
[Not reviewed]
master
0 1 0
Rubidium - 11 months ago 2024-01-15 18:22:51
rubidium@openttd.org
Change: be more resilient against missing towns in old loader
1 file changed with 11 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/saveload/oldloader_sl.cpp
Show inline comments
 
@@ -492,12 +492,20 @@ static void ClearOldMap3(TileIndex t)
 
{
 
	Tile tile(t);
 
	tile.m3() = 0;
 
	tile.m4() = 0;
 
}
 

	
 
static Town *RemapTown(TileIndex fallback)
 
{
 
	/* In some cases depots, industries and stations could refer to a missing town. */
 
	Town *t = Town::GetIfValid(RemapTownIndex(_old_town_index));
 
	if (t == nullptr) t = CalcClosestTownFromTile(fallback);
 
	return t;
 
}
 

	
 
static void ReadTTDPatchFlags()
 
{
 
	if (_read_ttdpatch_flags) return;
 

	
 
	_read_ttdpatch_flags = true;
 

	
 
@@ -671,16 +679,13 @@ static const OldChunks depot_chunk[] = {
 
static bool LoadOldDepot(LoadgameState *ls, int num)
 
{
 
	Depot *d = new (num) Depot();
 
	if (!LoadChunk(ls, d, depot_chunk)) return false;
 

	
 
	if (d->xy != 0) {
 
		/* In some cases, there could be depots referencing invalid town. */
 
		Town *t = Town::GetIfValid(RemapTownIndex(_old_town_index));
 
		if (t == nullptr) t = Town::GetRandom();
 
		d->town = t;
 
		d->town = RemapTown(d->xy);
 
	} else {
 
		delete d;
 
	}
 

	
 
	return true;
 
}
 
@@ -764,13 +769,13 @@ static bool LoadOldStation(LoadgameState
 
	Station *st = new (num) Station();
 
	_current_station_id = num;
 

	
 
	if (!LoadChunk(ls, st, station_chunk)) return false;
 

	
 
	if (st->xy != 0) {
 
		st->town = Town::Get(RemapTownIndex(_old_town_index));
 
		st->town = RemapTown(st->xy);
 

	
 
		if (_savegame_type == SGT_TTO) {
 
			if (IsInsideBS(_old_string_id, 0x180F, 32)) {
 
				st->string_id = STR_SV_STNAME + (_old_string_id - 0x180F); // automatic name
 
			} else {
 
				st->string_id = _old_string_id + 0x2800; // custom name
 
@@ -840,13 +845,13 @@ static const OldChunks industry_chunk[] 
 
static bool LoadOldIndustry(LoadgameState *ls, int num)
 
{
 
	Industry *i = new (num) Industry();
 
	if (!LoadChunk(ls, i, industry_chunk)) return false;
 

	
 
	if (i->location.tile != 0) {
 
		i->town = Town::Get(RemapTownIndex(_old_town_index));
 
		i->town = RemapTown(i->location.tile);
 

	
 
		if (_savegame_type == SGT_TTO) {
 
			if (i->type > 0x06) i->type++; // Printing Works were added
 
			if (i->type == 0x0A) i->type = 0x12; // Iron Ore Mine has different ID
 

	
 
			TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
0 comments (0 inline, 0 general)