# labelmaps_sl.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../station_map.h"
    00014 #include "../tunnelbridge_map.h"
    00015
    00016 #include "saveload.h"
    00017
    00018 static SmallVector<RailTypeLabel, RAILTYPE_END> _railtype_list;
    00019
    00025 static bool NeedRailTypeConversion()
    00026 {
    00027   for (uint i = 0; i < _railtype_list.Length(); i++) {
    00028     if ((RailType)i < RAILTYPE_END) {
    00029       const RailtypeInfo *rti = GetRailTypeInfo((RailType)i);
    00030       if (rti->label != _railtype_list[i]) return true;
    00031     } else {
    00032       if (_railtype_list[i] != 0) return true;
    00033     }
    00034   }
    00035
    00036   /* No rail type conversion is necessary */
    00037   return false;
    00038 }
    00039
    00040 void AfterLoadLabelMaps()
    00041 {
    00042   if (NeedRailTypeConversion()) {
    00043     SmallVector<RailType, RAILTYPE_END> railtype_conversion_map;
    00044
    00045     for (uint i = 0; i < _railtype_list.Length(); i++) {
    00046       RailType r = GetRailTypeByLabel(_railtype_list[i]);
    00047       if (r == INVALID_RAILTYPE) r = RAILTYPE_BEGIN;
    00048
    00049       *railtype_conversion_map.Append() = r;
    00050     }
    00051
    00052     for (TileIndex t = 0; t < MapSize(); t++) {
    00053       switch (GetTileType(t)) {
    00054         case MP_RAILWAY:
    00055           SetRailType(t, railtype_conversion_map[GetRailType(t)]);
    00056           break;
    00057
    00058         case MP_ROAD:
    00059           if (IsLevelCrossing(t)) {
    00060             SetRailType(t, railtype_conversion_map[GetRailType(t)]);
    00061           }
    00062           break;
    00063
    00064         case MP_STATION:
    00065           if (HasStationRail(t)) {
    00066             SetRailType(t, railtype_conversion_map[GetRailType(t)]);
    00067           }
    00068           break;
    00069
    00070         case MP_TUNNELBRIDGE:
    00071           if (GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL) {
    00072             SetRailType(t, railtype_conversion_map[GetRailType(t)]);
    00073           }
    00074           break;
    00075
    00076         default:
    00077           break;
    00078       }
    00079     }
    00080   }
    00081
    00082   _railtype_list.Clear();
    00083 }
    00084
    00086 struct LabelObject {
    00087   uint32 label;
    00088 };
    00089
    00090 static const SaveLoad _label_object_desc[] = {
    00091   SLE_VAR(LabelObject, label, SLE_UINT32),
    00092   SLE_END(),
    00093 };
    00094
    00095 static void Save_RAIL()
    00096 {
    00097   LabelObject lo;
    00098
    00099   for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
    00100     lo.label = GetRailTypeInfo(r)->label;
    00101
    00102     SlSetArrayIndex(r);
    00103     SlObject(&lo, _label_object_desc);
    00104   }
    00105 }
    00106
    00107 static void Load_RAIL()
    00108 {
    00109   _railtype_list.Clear();
    00110
    00111   LabelObject lo;
    00112
    00113   while (SlIterateArray() != -1) {
    00114     SlObject(&lo, _label_object_desc);
    00115     *_railtype_list.Append() = (RailTypeLabel)lo.label;
    00116   }
    00117 }
    00118
    00119 extern const ChunkHandler _labelmaps_chunk_handlers[] = {
    00120   { 'RAIL', Save_RAIL, Load_RAIL, NULL, NULL, CH_ARRAY | CH_LAST},
    00121 };
    00122