# map_sl.cpp

Go to the documentation of this file.

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