# map_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 "../map_func.h"
    00014 #include "../core/bitmath_func.hpp"
    00015 #include "../fios.h"
    00016
    00017 #include "saveload.h"
    00018
    00019 static uint32 _map_dim_x;
    00020 static uint32 _map_dim_y;
    00021
    00022 static const SaveLoadGlobVarList _map_dimensions[] = {
    00023   SLEG_CONDVAR(_map_dim_x, SLE_UINT32, 6, SL_MAX_VERSION),
    00024   SLEG_CONDVAR(_map_dim_y, SLE_UINT32, 6, SL_MAX_VERSION),
    00025       SLEG_END()
    00026 };
    00027
    00028 static void Save_MAPS()
    00029 {
    00030   _map_dim_x = MapSizeX();
    00031   _map_dim_y = MapSizeY();
    00032   SlGlobList(_map_dimensions);
    00033 }
    00034
    00035 static void Load_MAPS()
    00036 {
    00037   SlGlobList(_map_dimensions);
    00038   AllocateMap(_map_dim_x, _map_dim_y);
    00039 }
    00040
    00041 static void Check_MAPS()
    00042 {
    00043   SlGlobList(_map_dimensions);
    00044   _load_check_data.map_size_x = _map_dim_x;
    00045   _load_check_data.map_size_y = _map_dim_y;
    00046 }
    00047
    00048 static const uint MAP_SL_BUF_SIZE = 4096;
    00049
    00050 static void Load_MAPT()
    00051 {
    00052   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00053   TileIndex size = MapSize();
    00054
    00055   for (TileIndex i = 0; i != size;) {
    00056     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00057     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].type_height = buf[j];
    00058   }
    00059 }
    00060
    00061 static void Save_MAPT()
    00062 {
    00063   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00064   TileIndex size = MapSize();
    00065
    00066   SlSetLength(size);
    00067   for (TileIndex i = 0; i != size;) {
    00068     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].type_height;
    00069     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00070   }
    00071 }
    00072
    00073 static void Load_MAP1()
    00074 {
    00075   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00076   TileIndex size = MapSize();
    00077
    00078   for (TileIndex i = 0; i != size;) {
    00079     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00080     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m1 = buf[j];
    00081   }
    00082 }
    00083
    00084 static void Save_MAP1()
    00085 {
    00086   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00087   TileIndex size = MapSize();
    00088
    00089   SlSetLength(size);
    00090   for (TileIndex i = 0; i != size;) {
    00091     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m1;
    00092     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00093   }
    00094 }
    00095
    00096 static void Load_MAP2()
    00097 {
    00098   SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
    00099   TileIndex size = MapSize();
    00100
    00101   for (TileIndex i = 0; i != size;) {
    00102     SlArray(buf, MAP_SL_BUF_SIZE,
    00103       /* In those versions the m2 was 8 bits */
    00104       IsSavegameVersionBefore(5) ? SLE_FILE_U8 | SLE_VAR_U16 : SLE_UINT16
    00105     );
    00106     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m2 = buf[j];
    00107   }
    00108 }
    00109
    00110 static void Save_MAP2()
    00111 {
    00112   SmallStackSafeStackAlloc<uint16, MAP_SL_BUF_SIZE> buf;
    00113   TileIndex size = MapSize();
    00114
    00115   SlSetLength(size * sizeof(uint16));
    00116   for (TileIndex i = 0; i != size;) {
    00117     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m2;
    00118     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT16);
    00119   }
    00120 }
    00121
    00122 static void Load_MAP3()
    00123 {
    00124   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00125   TileIndex size = MapSize();
    00126
    00127   for (TileIndex i = 0; i != size;) {
    00128     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00129     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m3 = buf[j];
    00130   }
    00131 }
    00132
    00133 static void Save_MAP3()
    00134 {
    00135   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00136   TileIndex size = MapSize();
    00137
    00138   SlSetLength(size);
    00139   for (TileIndex i = 0; i != size;) {
    00140     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m3;
    00141     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00142   }
    00143 }
    00144
    00145 static void Load_MAP4()
    00146 {
    00147   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00148   TileIndex size = MapSize();
    00149
    00150   for (TileIndex i = 0; i != size;) {
    00151     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00152     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m4 = buf[j];
    00153   }
    00154 }
    00155
    00156 static void Save_MAP4()
    00157 {
    00158   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00159   TileIndex size = MapSize();
    00160
    00161   SlSetLength(size);
    00162   for (TileIndex i = 0; i != size;) {
    00163     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m4;
    00164     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00165   }
    00166 }
    00167
    00168 static void Load_MAP5()
    00169 {
    00170   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00171   TileIndex size = MapSize();
    00172
    00173   for (TileIndex i = 0; i != size;) {
    00174     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00175     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m5 = buf[j];
    00176   }
    00177 }
    00178
    00179 static void Save_MAP5()
    00180 {
    00181   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00182   TileIndex size = MapSize();
    00183
    00184   SlSetLength(size);
    00185   for (TileIndex i = 0; i != size;) {
    00186     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m5;
    00187     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00188   }
    00189 }
    00190
    00191 static void Load_MAP6()
    00192 {
    00193   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00194   TileIndex size = MapSize();
    00195
    00196   if (IsSavegameVersionBefore(42)) {
    00197     for (TileIndex i = 0; i != size;) {
    00198       /* 1024, otherwise we overflow on 64x64 maps! */
    00199       SlArray(buf, 1024, SLE_UINT8);
    00200       for (uint j = 0; j != 1024; j++) {
    00201         _m[i++].m6 = GB(buf[j], 0, 2);
    00202         _m[i++].m6 = GB(buf[j], 2, 2);
    00203         _m[i++].m6 = GB(buf[j], 4, 2);
    00204         _m[i++].m6 = GB(buf[j], 6, 2);
    00205       }
    00206     }
    00207   } else {
    00208     for (TileIndex i = 0; i != size;) {
    00209       SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00210       for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _m[i++].m6 = buf[j];
    00211     }
    00212   }
    00213 }
    00214
    00215 static void Save_MAP6()
    00216 {
    00217   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00218   TileIndex size = MapSize();
    00219
    00220   SlSetLength(size);
    00221   for (TileIndex i = 0; i != size;) {
    00222     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _m[i++].m6;
    00223     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00224   }
    00225 }
    00226
    00227 static void Load_MAP7()
    00228 {
    00229   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00230   TileIndex size = MapSize();
    00231
    00232   for (TileIndex i = 0; i != size;) {
    00233     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00234     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) _me[i++].m7 = buf[j];
    00235   }
    00236 }
    00237
    00238 static void Save_MAP7()
    00239 {
    00240   SmallStackSafeStackAlloc<byte, MAP_SL_BUF_SIZE> buf;
    00241   TileIndex size = MapSize();
    00242
    00243   SlSetLength(size);
    00244   for (TileIndex i = 0; i != size;) {
    00245     for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = _me[i++].m7;
    00246     SlArray(buf, MAP_SL_BUF_SIZE, SLE_UINT8);
    00247   }
    00248 }
    00249
    00250 extern const ChunkHandler _map_chunk_handlers[] = {
    00251   { 'MAPS', Save_MAPS, Load_MAPS, NULL, Check_MAPS, CH_RIFF },
    00252   { 'MAPT', Save_MAPT, Load_MAPT, NULL, NULL,       CH_RIFF },
    00253   { 'MAPO', Save_MAP1, Load_MAP1, NULL, NULL,       CH_RIFF },
    00254   { 'MAP2', Save_MAP2, Load_MAP2, NULL, NULL,       CH_RIFF },
    00255   { 'M3LO', Save_MAP3, Load_MAP3, NULL, NULL,       CH_RIFF },
    00256   { 'M3HI', Save_MAP4, Load_MAP4, NULL, NULL,       CH_RIFF },
    00257   { 'MAP5', Save_MAP5, Load_MAP5, NULL, NULL,       CH_RIFF },
    00258   { 'MAPE', Save_MAP6, Load_MAP6, NULL, NULL,       CH_RIFF },
    00259   { 'MAP7', Save_MAP7, Load_MAP7, NULL, NULL,       CH_RIFF | CH_LAST },
    00260 };