# town_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 "../newgrf_house.h"
    00014 #include "../town.h"
    00015 #include "../landscape.h"
    00016
    00017 #include "saveload.h"
    00018 #include "newgrf_sl.h"
    00019
    00028 void UpdateHousesAndTowns()
    00029 {
    00030   Town *town;
    00031   InitializeBuildingCounts();
    00032
    00033   /* Reset town population and num_houses */
    00034   FOR_ALL_TOWNS(town) {
    00035     town->population = 0;
    00036     town->num_houses = 0;
    00037   }
    00038
    00039   for (TileIndex t = 0; t < MapSize(); t++) {
    00040     if (!IsTileType(t, MP_HOUSE)) continue;
    00041
    00042     HouseID house_id = GetCleanHouseType(t);
    00043     if (!HouseSpec::Get(house_id)->enabled && house_id >= NEW_HOUSE_OFFSET) {
    00044       /* The specs for this type of house are not available any more, so
    00045        * replace it with the substitute original house type. */
    00046       house_id = _house_mngr.GetSubstituteID(house_id);
    00047       SetHouseType(t, house_id);
    00048     }
    00049   }
    00050
    00051   /* Check for cases when a NewGRF has set a wrong house substitute type. */
    00052   for (TileIndex t = 0; t < MapSize(); t++) {
    00053     if (!IsTileType(t, MP_HOUSE)) continue;
    00054
    00055     HouseID house_type = GetCleanHouseType(t);
    00056     TileIndex north_tile = t + GetHouseNorthPart(house_type); // modifies 'house_type'!
    00057     if (t == north_tile) {
    00058       const HouseSpec *hs = HouseSpec::Get(house_type);
    00059       bool valid_house = true;
    00060       if (hs->building_flags & TILE_SIZE_2x1) {
    00061         TileIndex tile = t + TileDiffXY(1, 0);
    00062         if (!IsTileType(tile, MP_HOUSE) || GetCleanHouseType(tile) != house_type + 1) valid_house = false;
    00063       } else if (hs->building_flags & TILE_SIZE_1x2) {
    00064         TileIndex tile = t + TileDiffXY(0, 1);
    00065         if (!IsTileType(tile, MP_HOUSE) || GetCleanHouseType(tile) != house_type + 1) valid_house = false;
    00066       } else if (hs->building_flags & TILE_SIZE_2x2) {
    00067         TileIndex tile = t + TileDiffXY(0, 1);
    00068         if (!IsTileType(tile, MP_HOUSE) || GetCleanHouseType(tile) != house_type + 1) valid_house = false;
    00069         tile = t + TileDiffXY(1, 0);
    00070         if (!IsTileType(tile, MP_HOUSE) || GetCleanHouseType(tile) != house_type + 2) valid_house = false;
    00071         tile = t + TileDiffXY(1, 1);
    00072         if (!IsTileType(tile, MP_HOUSE) || GetCleanHouseType(tile) != house_type + 3) valid_house = false;
    00073       }
    00074       /* If not all tiles of this house are present remove the house.
    00075        * The other tiles will get removed later in this loop because
    00076        * their north tile is not the correct type anymore. */
    00077       if (!valid_house) DoClearSquare(t);
    00078     } else if (!IsTileType(north_tile, MP_HOUSE) || GetCleanHouseType(north_tile) != house_type) {
    00079       /* This tile should be part of a multi-tile building but the
    00080        * north tile of this house isn't on the map. */
    00081       DoClearSquare(t);
    00082     }
    00083   }
    00084
    00085   for (TileIndex t = 0; t < MapSize(); t++) {
    00086     if (!IsTileType(t, MP_HOUSE)) continue;
    00087
    00088     HouseID house_id = GetCleanHouseType(t);
    00089     town = Town::GetByTile(t);
    00090     IncreaseBuildingCount(town, house_id);
    00091     if (IsHouseCompleted(t)) town->population += HouseSpec::Get(house_id)->population;
    00092
    00093     /* Increase the number of houses for every house, but only once. */
    00094     if (GetHouseNorthPart(house_id) == 0) town->num_houses++;
    00095   }
    00096
    00097   /* Update the population and num_house dependant values */
    00098   FOR_ALL_TOWNS(town) {
    00099     UpdateTownRadius(town);
    00100     UpdateTownCargoes(town);
    00101   }
    00102   UpdateTownCargoBitmap();
    00103 }
    00104
    00106 static const SaveLoad _town_desc[] = {
    00107   SLE_CONDVAR(Town, xy,                    SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
    00108   SLE_CONDVAR(Town, xy,                    SLE_UINT32,                 6, SL_MAX_VERSION),
    00109
    00110   SLE_CONDNULL(2, 0, 2),
    00111   SLE_CONDNULL(4, 3, 84),
    00112   SLE_CONDNULL(2, 0, 91),
    00113
    00114   SLE_CONDVAR(Town, townnamegrfid,         SLE_UINT32, 66, SL_MAX_VERSION),
    00115       SLE_VAR(Town, townnametype,          SLE_UINT16),
    00116       SLE_VAR(Town, townnameparts,         SLE_UINT32),
    00117   SLE_CONDSTR(Town, name,                  SLE_STR | SLF_ALLOW_CONTROL, 0, 84, SL_MAX_VERSION),
    00118
    00119       SLE_VAR(Town, flags,                 SLE_UINT8),
    00120   SLE_CONDVAR(Town, statues,               SLE_FILE_U8  | SLE_VAR_U16, 0, 103),
    00121   SLE_CONDVAR(Town, statues,               SLE_UINT16,               104, SL_MAX_VERSION),
    00122
    00123   SLE_CONDNULL(1, 0, 1),
    00124
    00125   SLE_CONDVAR(Town, have_ratings,          SLE_FILE_U8  | SLE_VAR_U16, 0, 103),
    00126   SLE_CONDVAR(Town, have_ratings,          SLE_UINT16,               104, SL_MAX_VERSION),
    00127   SLE_CONDARR(Town, ratings,               SLE_INT16, 8,               0, 103),
    00128   SLE_CONDARR(Town, ratings,               SLE_INT16, MAX_COMPANIES, 104, SL_MAX_VERSION),
    00129   /* failed bribe attempts are stored since savegame format 4 */
    00130   SLE_CONDARR(Town, unwanted,              SLE_INT8,  8,               4, 103),
    00131   SLE_CONDARR(Town, unwanted,              SLE_INT8,  MAX_COMPANIES, 104, SL_MAX_VERSION),
    00132
    00133   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].old_max, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00134   SLE_CONDVAR(Town, supplied[CT_MAIL].old_max,       SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00135   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].new_max, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00136   SLE_CONDVAR(Town, supplied[CT_MAIL].new_max,       SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00137   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].old_act, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00138   SLE_CONDVAR(Town, supplied[CT_MAIL].old_act,       SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00139   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].new_act, SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00140   SLE_CONDVAR(Town, supplied[CT_MAIL].new_act,       SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00141
    00142   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].old_max, SLE_UINT32,                 9, 164),
    00143   SLE_CONDVAR(Town, supplied[CT_MAIL].old_max,       SLE_UINT32,                 9, 164),
    00144   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].new_max, SLE_UINT32,                 9, 164),
    00145   SLE_CONDVAR(Town, supplied[CT_MAIL].new_max,       SLE_UINT32,                 9, 164),
    00146   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].old_act, SLE_UINT32,                 9, 164),
    00147   SLE_CONDVAR(Town, supplied[CT_MAIL].old_act,       SLE_UINT32,                 9, 164),
    00148   SLE_CONDVAR(Town, supplied[CT_PASSENGERS].new_act, SLE_UINT32,                 9, 164),
    00149   SLE_CONDVAR(Town, supplied[CT_MAIL].new_act,       SLE_UINT32,                 9, 164),
    00150
    00151   SLE_CONDNULL(2, 0, 163),
    00152
    00153   SLE_CONDVAR(Town, received[TE_FOOD].old_act,       SLE_UINT16,                 0, 164),
    00154   SLE_CONDVAR(Town, received[TE_WATER].old_act,      SLE_UINT16,                 0, 164),
    00155   SLE_CONDVAR(Town, received[TE_FOOD].new_act,       SLE_UINT16,                 0, 164),
    00156   SLE_CONDVAR(Town, received[TE_WATER].new_act,      SLE_UINT16,                 0, 164),
    00157
    00158   SLE_CONDARR(Town, goal, SLE_UINT32, NUM_TE, 165, SL_MAX_VERSION),
    00159
    00160   SLE_CONDSTR(Town, text,                  SLE_STR | SLF_ALLOW_CONTROL, 0, 168, SL_MAX_VERSION),
    00161
    00162   SLE_CONDVAR(Town, time_until_rebuild,    SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
    00163   SLE_CONDVAR(Town, grow_counter,          SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
    00164   SLE_CONDVAR(Town, growth_rate,           SLE_FILE_U8 | SLE_VAR_I16,  0, 53),
    00165
    00166   SLE_CONDVAR(Town, time_until_rebuild,    SLE_UINT16,                54, SL_MAX_VERSION),
    00167   SLE_CONDVAR(Town, grow_counter,          SLE_UINT16,                54, SL_MAX_VERSION),
    00168
    00169   SLE_CONDVAR(Town, growth_rate,           SLE_FILE_I16 | SLE_VAR_U16, 54, 164),
    00170   SLE_CONDVAR(Town, growth_rate,           SLE_UINT16,                 165, SL_MAX_VERSION),
    00171
    00172       SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
    00173       SLE_VAR(Town, road_build_months,     SLE_UINT8),
    00174
    00175   SLE_CONDVAR(Town, exclusivity,           SLE_UINT8,                  2, SL_MAX_VERSION),
    00176   SLE_CONDVAR(Town, exclusive_counter,     SLE_UINT8,                  2, SL_MAX_VERSION),
    00177
    00178   SLE_CONDVAR(Town, larger_town,           SLE_BOOL,                  56, SL_MAX_VERSION),
    00179   SLE_CONDVAR(Town, layout,                SLE_UINT8,                113, SL_MAX_VERSION),
    00180
    00181   SLE_CONDLST(Town, psa_list,            REF_STORAGE,                161, SL_MAX_VERSION),
    00182
    00183   SLE_CONDVAR(Town, cargo_produced,       SLE_UINT32,                166, SL_MAX_VERSION),
    00184
    00185   /* reserve extra space in savegame here. (currently 30 bytes) */
    00186   SLE_CONDNULL(30, 2, SL_MAX_VERSION),
    00187
    00188   SLE_END()
    00189 };
    00190
    00191 static const SaveLoad _town_supplied_desc[] = {
    00192   SLE_CONDVAR(TransportedCargoStat<uint32>, old_max, SLE_UINT32, 165, SL_MAX_VERSION),
    00193   SLE_CONDVAR(TransportedCargoStat<uint32>, new_max, SLE_UINT32, 165, SL_MAX_VERSION),
    00194   SLE_CONDVAR(TransportedCargoStat<uint32>, old_act, SLE_UINT32, 165, SL_MAX_VERSION),
    00195   SLE_CONDVAR(TransportedCargoStat<uint32>, new_act, SLE_UINT32, 165, SL_MAX_VERSION),
    00196
    00197   SLE_END()
    00198 };
    00199
    00200 static const SaveLoad _town_received_desc[] = {
    00201   SLE_CONDVAR(TransportedCargoStat<uint16>, old_max, SLE_UINT16, 165, SL_MAX_VERSION),
    00202   SLE_CONDVAR(TransportedCargoStat<uint16>, new_max, SLE_UINT16, 165, SL_MAX_VERSION),
    00203   SLE_CONDVAR(TransportedCargoStat<uint16>, old_act, SLE_UINT16, 165, SL_MAX_VERSION),
    00204   SLE_CONDVAR(TransportedCargoStat<uint16>, new_act, SLE_UINT16, 165, SL_MAX_VERSION),
    00205
    00206   SLE_END()
    00207 };
    00208
    00209 static void Save_HIDS()
    00210 {
    00211   Save_NewGRFMapping(_house_mngr);
    00212 }
    00213
    00214 static void Load_HIDS()
    00215 {
    00216   Load_NewGRFMapping(_house_mngr);
    00217 }
    00218
    00219 const SaveLoad *GetTileMatrixDesc()
    00220 {
    00221   /* Here due to private member vars. */
    00222   static const SaveLoad _tilematrix_desc[] = {
    00223     SLE_VAR(AcceptanceMatrix, area.tile, SLE_UINT32),
    00224     SLE_VAR(AcceptanceMatrix, area.w,    SLE_UINT16),
    00225     SLE_VAR(AcceptanceMatrix, area.h,    SLE_UINT16),
    00226     SLE_END()
    00227   };
    00228
    00229   return _tilematrix_desc;
    00230 }
    00231
    00232 static void RealSave_Town(Town *t)
    00233 {
    00234   SlObject(t, _town_desc);
    00235
    00236   for (CargoID i = 0; i < NUM_CARGO; i++) {
    00237     SlObject(&t->supplied[i], _town_supplied_desc);
    00238   }
    00239   for (int i = TE_BEGIN; i < NUM_TE; i++) {
    00240     SlObject(&t->received[i], _town_received_desc);
    00241   }
    00242
    00243   if (IsSavegameVersionBefore(166)) return;
    00244
    00245   SlObject(&t->cargo_accepted, GetTileMatrixDesc());
    00246   if (t->cargo_accepted.area.w != 0) {
    00247     uint arr_len = t->cargo_accepted.area.w / AcceptanceMatrix::GRID * t->cargo_accepted.area.h / AcceptanceMatrix::GRID;
    00248     SlArray(t->cargo_accepted.data, arr_len, SLE_UINT32);
    00249   }
    00250 }
    00251
    00252 static void Save_TOWN()
    00253 {
    00254   Town *t;
    00255
    00256   FOR_ALL_TOWNS(t) {
    00257     SlSetArrayIndex(t->index);
    00258     SlAutolength((AutolengthProc*)RealSave_Town, t);
    00259   }
    00260 }
    00261
    00262 static void Load_TOWN()
    00263 {
    00264   int index;
    00265
    00266   while ((index = SlIterateArray()) != -1) {
    00267     Town *t = new (index) Town();
    00268     SlObject(t, _town_desc);
    00269
    00270     for (CargoID i = 0; i < NUM_CARGO; i++) {
    00271       SlObject(&t->supplied[i], _town_supplied_desc);
    00272     }
    00273     for (int i = TE_BEGIN; i < TE_END; i++) {
    00274       SlObject(&t->received[i], _town_received_desc);
    00275     }
    00276
    00277     if (t->townnamegrfid == 0 && !IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1) && GB(t->townnametype, 11, 5) != 15) {
    00278       SlErrorCorrupt("Invalid town name generator");
    00279     }
    00280
    00281     if (IsSavegameVersionBefore(166)) continue;
    00282
    00283     SlObject(&t->cargo_accepted, GetTileMatrixDesc());
    00284     if (t->cargo_accepted.area.w != 0) {
    00285       uint arr_len = t->cargo_accepted.area.w / AcceptanceMatrix::GRID * t->cargo_accepted.area.h / AcceptanceMatrix::GRID;
    00286       t->cargo_accepted.data = MallocT<uint32>(arr_len);
    00287       SlArray(t->cargo_accepted.data, arr_len, SLE_UINT32);
    00288
    00289       /* Rebuild total cargo acceptance. */
    00290       UpdateTownCargoTotal(t);
    00291     }
    00292   }
    00293 }
    00294
    00296 static void Ptrs_TOWN()
    00297 {
    00298   /* Don't run when savegame version lower than 161. */
    00299   if (IsSavegameVersionBefore(161)) return;
    00300
    00301   Town *t;
    00302   FOR_ALL_TOWNS(t) {
    00303     SlObject(t, _town_desc);
    00304   }
    00305 }
    00306
    00308 extern const ChunkHandler _town_chunk_handlers[] = {
    00309   { 'HIDS', Save_HIDS, Load_HIDS,      NULL, NULL, CH_ARRAY },
    00310   { 'CITY', Save_TOWN, Load_TOWN, Ptrs_TOWN, NULL, CH_ARRAY | CH_LAST},
    00311 };