# map_sl.cpp

Go to the documentation of this file.

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