2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-03-21 23:58:20 +00:00
|
|
|
/** @file labelmaps_sl.cpp Code handling saving and loading of rail type label mappings */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../station_map.h"
|
|
|
|
#include "../tunnelbridge_map.h"
|
|
|
|
|
|
|
|
#include "saveload.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2019-03-03 17:30:09 +00:00
|
|
|
static std::vector<RailTypeLabel> _railtype_list;
|
2009-03-21 23:58:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if any saved rail type labels are different to the currently loaded
|
|
|
|
* rail types, which therefore requires conversion.
|
|
|
|
* @return true if (and only if) conversion due to rail type changes is needed.
|
|
|
|
*/
|
|
|
|
static bool NeedRailTypeConversion()
|
|
|
|
{
|
2018-09-23 11:23:54 +00:00
|
|
|
for (uint i = 0; i < _railtype_list.size(); i++) {
|
2009-03-21 23:58:20 +00:00
|
|
|
if ((RailType)i < RAILTYPE_END) {
|
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo((RailType)i);
|
|
|
|
if (rti->label != _railtype_list[i]) return true;
|
|
|
|
} else {
|
|
|
|
if (_railtype_list[i] != 0) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No rail type conversion is necessary */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AfterLoadLabelMaps()
|
|
|
|
{
|
|
|
|
if (NeedRailTypeConversion()) {
|
2019-03-03 17:30:09 +00:00
|
|
|
std::vector<RailType> railtype_conversion_map;
|
2009-03-21 23:58:20 +00:00
|
|
|
|
2018-09-23 11:23:54 +00:00
|
|
|
for (uint i = 0; i < _railtype_list.size(); i++) {
|
2009-03-21 23:58:20 +00:00
|
|
|
RailType r = GetRailTypeByLabel(_railtype_list[i]);
|
|
|
|
if (r == INVALID_RAILTYPE) r = RAILTYPE_BEGIN;
|
|
|
|
|
2019-02-18 22:39:06 +00:00
|
|
|
railtype_conversion_map.push_back(r);
|
2009-03-21 23:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (TileIndex t = 0; t < MapSize(); t++) {
|
|
|
|
switch (GetTileType(t)) {
|
|
|
|
case MP_RAILWAY:
|
|
|
|
SetRailType(t, railtype_conversion_map[GetRailType(t)]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_ROAD:
|
|
|
|
if (IsLevelCrossing(t)) {
|
|
|
|
SetRailType(t, railtype_conversion_map[GetRailType(t)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_STATION:
|
2009-07-24 11:47:12 +00:00
|
|
|
if (HasStationRail(t)) {
|
2009-03-21 23:58:20 +00:00
|
|
|
SetRailType(t, railtype_conversion_map[GetRailType(t)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_TUNNELBRIDGE:
|
|
|
|
if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
|
|
|
|
SetRailType(t, railtype_conversion_map[GetRailType(t)]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:44:14 +00:00
|
|
|
_railtype_list.clear();
|
2009-03-21 23:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Container for a label for SaveLoad system */
|
|
|
|
struct LabelObject {
|
|
|
|
uint32 label;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SaveLoad _label_object_desc[] = {
|
|
|
|
SLE_VAR(LabelObject, label, SLE_UINT32),
|
|
|
|
SLE_END(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void Save_RAIL()
|
|
|
|
{
|
|
|
|
LabelObject lo;
|
|
|
|
|
|
|
|
for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
|
|
|
|
lo.label = GetRailTypeInfo(r)->label;
|
|
|
|
|
|
|
|
SlSetArrayIndex(r);
|
|
|
|
SlObject(&lo, _label_object_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Load_RAIL()
|
|
|
|
{
|
2018-09-20 22:44:14 +00:00
|
|
|
_railtype_list.clear();
|
2009-03-21 23:58:20 +00:00
|
|
|
|
|
|
|
LabelObject lo;
|
|
|
|
|
2010-10-01 16:42:28 +00:00
|
|
|
while (SlIterateArray() != -1) {
|
2009-03-21 23:58:20 +00:00
|
|
|
SlObject(&lo, _label_object_desc);
|
2019-02-18 22:39:06 +00:00
|
|
|
_railtype_list.push_back((RailTypeLabel)lo.label);
|
2009-03-21 23:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern const ChunkHandler _labelmaps_chunk_handlers[] = {
|
2019-04-10 21:07:06 +00:00
|
|
|
{ 'RAIL', Save_RAIL, Load_RAIL, nullptr, nullptr, CH_ARRAY | CH_LAST},
|
2009-03-21 23:58:20 +00:00
|
|
|
};
|
|
|
|
|