# 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 "../newgrf_commons.h"
    00015 #include "../town.h"
    00016
    00017 #include "saveload.h"
    00018
    00027 void UpdateHousesAndTowns()
    00028 {
    00029   Town *town;
    00030   InitializeBuildingCounts();
    00031
    00032   /* Reset town population and num_houses */
    00033   FOR_ALL_TOWNS(town) {
    00034     town->population = 0;
    00035     town->num_houses = 0;
    00036   }
    00037
    00038   for (TileIndex t = 0; t < MapSize(); t++) {
    00039     HouseID house_id;
    00040
    00041     if (!IsTileType(t, MP_HOUSE)) continue;
    00042
    00043     house_id = GetHouseType(t);
    00044     if (!HouseSpec::Get(house_id)->enabled && house_id >= NEW_HOUSE_OFFSET) {
    00045       /* The specs for this type of house are not available any more, so
    00046        * replace it with the substitute original house type. */
    00047       house_id = _house_mngr.GetSubstituteID(house_id);
    00048       SetHouseType(t, house_id);
    00049     }
    00050
    00051     town = Town::GetByTile(t);
    00052     IncreaseBuildingCount(town, house_id);
    00053     if (IsHouseCompleted(t)) town->population += HouseSpec::Get(house_id)->population;
    00054
    00055     /* Increase the number of houses for every house, but only once. */
    00056     if (GetHouseNorthPart(house_id) == 0) town->num_houses++;
    00057   }
    00058
    00059   /* Update the population and num_house dependant values */
    00060   FOR_ALL_TOWNS(town) {
    00061     UpdateTownRadius(town);
    00062   }
    00063 }
    00064
    00066 static const SaveLoad _town_desc[] = {
    00067   SLE_CONDVAR(Town, xy,                    SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
    00068   SLE_CONDVAR(Town, xy,                    SLE_UINT32,                 6, SL_MAX_VERSION),
    00069
    00070   SLE_CONDNULL(2, 0, 2),
    00071   SLE_CONDNULL(4, 3, 84),
    00072   SLE_CONDNULL(2, 0, 91),
    00073
    00074   SLE_CONDVAR(Town, townnamegrfid,         SLE_UINT32, 66, SL_MAX_VERSION),
    00075       SLE_VAR(Town, townnametype,          SLE_UINT16),
    00076       SLE_VAR(Town, townnameparts,         SLE_UINT32),
    00077   SLE_CONDSTR(Town, name,                  SLE_STR, 0, 84, SL_MAX_VERSION),
    00078
    00079       SLE_VAR(Town, flags,                 SLE_UINT8),
    00080   SLE_CONDVAR(Town, statues,               SLE_FILE_U8  | SLE_VAR_U16, 0, 103),
    00081   SLE_CONDVAR(Town, statues,               SLE_UINT16,               104, SL_MAX_VERSION),
    00082
    00083   SLE_CONDNULL(1, 0, 1),
    00084
    00085   SLE_CONDVAR(Town, have_ratings,          SLE_FILE_U8  | SLE_VAR_U16, 0, 103),
    00086   SLE_CONDVAR(Town, have_ratings,          SLE_UINT16,               104, SL_MAX_VERSION),
    00087   SLE_CONDARR(Town, ratings,               SLE_INT16, 8,               0, 103),
    00088   SLE_CONDARR(Town, ratings,               SLE_INT16, MAX_COMPANIES, 104, SL_MAX_VERSION),
    00089   /* failed bribe attempts are stored since savegame format 4 */
    00090   SLE_CONDARR(Town, unwanted,              SLE_INT8,  8,               4, 103),
    00091   SLE_CONDARR(Town, unwanted,              SLE_INT8,  MAX_COMPANIES, 104, SL_MAX_VERSION),
    00092
    00093   SLE_CONDVAR(Town, max_pass,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00094   SLE_CONDVAR(Town, max_mail,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00095   SLE_CONDVAR(Town, new_max_pass,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00096   SLE_CONDVAR(Town, new_max_mail,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00097   SLE_CONDVAR(Town, act_pass,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00098   SLE_CONDVAR(Town, act_mail,              SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00099   SLE_CONDVAR(Town, new_act_pass,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00100   SLE_CONDVAR(Town, new_act_mail,          SLE_FILE_U16 | SLE_VAR_U32, 0, 8),
    00101
    00102   SLE_CONDVAR(Town, max_pass,              SLE_UINT32,                 9, SL_MAX_VERSION),
    00103   SLE_CONDVAR(Town, max_mail,              SLE_UINT32,                 9, SL_MAX_VERSION),
    00104   SLE_CONDVAR(Town, new_max_pass,          SLE_UINT32,                 9, SL_MAX_VERSION),
    00105   SLE_CONDVAR(Town, new_max_mail,          SLE_UINT32,                 9, SL_MAX_VERSION),
    00106   SLE_CONDVAR(Town, act_pass,              SLE_UINT32,                 9, SL_MAX_VERSION),
    00107   SLE_CONDVAR(Town, act_mail,              SLE_UINT32,                 9, SL_MAX_VERSION),
    00108   SLE_CONDVAR(Town, new_act_pass,          SLE_UINT32,                 9, SL_MAX_VERSION),
    00109   SLE_CONDVAR(Town, new_act_mail,          SLE_UINT32,                 9, SL_MAX_VERSION),
    00110
    00111       SLE_VAR(Town, pct_pass_transported,  SLE_UINT8),
    00112       SLE_VAR(Town, pct_mail_transported,  SLE_UINT8),
    00113
    00114       SLE_VAR(Town, act_food,              SLE_UINT16),
    00115       SLE_VAR(Town, act_water,             SLE_UINT16),
    00116       SLE_VAR(Town, new_act_food,          SLE_UINT16),
    00117       SLE_VAR(Town, new_act_water,         SLE_UINT16),
    00118
    00119   SLE_CONDVAR(Town, time_until_rebuild,    SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
    00120   SLE_CONDVAR(Town, grow_counter,          SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
    00121   SLE_CONDVAR(Town, growth_rate,           SLE_FILE_U8 | SLE_VAR_I16,  0, 53),
    00122
    00123   SLE_CONDVAR(Town, time_until_rebuild,    SLE_UINT16,                54, SL_MAX_VERSION),
    00124   SLE_CONDVAR(Town, grow_counter,          SLE_UINT16,                54, SL_MAX_VERSION),
    00125   SLE_CONDVAR(Town, growth_rate,           SLE_INT16,                 54, SL_MAX_VERSION),
    00126
    00127       SLE_VAR(Town, fund_buildings_months, SLE_UINT8),
    00128       SLE_VAR(Town, road_build_months,     SLE_UINT8),
    00129
    00130   SLE_CONDVAR(Town, exclusivity,           SLE_UINT8,                  2, SL_MAX_VERSION),
    00131   SLE_CONDVAR(Town, exclusive_counter,     SLE_UINT8,                  2, SL_MAX_VERSION),
    00132
    00133   SLE_CONDVAR(Town, larger_town,           SLE_BOOL,                  56, SL_MAX_VERSION),
    00134   SLE_CONDVAR(Town, layout,                SLE_UINT8,                113, SL_MAX_VERSION),
    00135
    00136   /* reserve extra space in savegame here. (currently 30 bytes) */
    00137   SLE_CONDNULL(30, 2, SL_MAX_VERSION),
    00138
    00139   SLE_END()
    00140 };
    00141
    00142 /* Save and load the mapping between the house id on the map, and the grf file
    00143  * it came from. */
    00144 static const SaveLoad _house_id_mapping_desc[] = {
    00145   SLE_VAR(EntityIDMapping, grfid,         SLE_UINT32),
    00146   SLE_VAR(EntityIDMapping, entity_id,     SLE_UINT8),
    00147   SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8),
    00148   SLE_END()
    00149 };
    00150
    00151 static void Save_HOUSEIDS()
    00152 {
    00153   uint j = _house_mngr.GetMaxMapping();
    00154
    00155   for (uint i = 0; i < j; i++) {
    00156     SlSetArrayIndex(i);
    00157     SlObject(&_house_mngr.mapping_ID[i], _house_id_mapping_desc);
    00158   }
    00159 }
    00160
    00161 static void Load_HOUSEIDS()
    00162 {
    00163   int index;
    00164
    00165   _house_mngr.ResetMapping();
    00166   uint max_id = _house_mngr.GetMaxMapping();
    00167
    00168   while ((index = SlIterateArray()) != -1) {
    00169     if ((uint)index >= max_id) break;
    00170     SlObject(&_house_mngr.mapping_ID[index], _house_id_mapping_desc);
    00171   }
    00172 }
    00173
    00174 static void Save_TOWN()
    00175 {
    00176   Town *t;
    00177
    00178   FOR_ALL_TOWNS(t) {
    00179     SlSetArrayIndex(t->index);
    00180     SlObject(t, _town_desc);
    00181   }
    00182 }
    00183
    00184 static void Load_TOWN()
    00185 {
    00186   int index;
    00187
    00188   while ((index = SlIterateArray()) != -1) {
    00189     Town *t = new (index) Town();
    00190     SlObject(t, _town_desc);
    00191   }
    00192 }
    00193
    00194 extern const ChunkHandler _town_chunk_handlers[] = {
    00195   { 'HIDS', Save_HOUSEIDS, Load_HOUSEIDS, NULL, CH_ARRAY },
    00196   { 'CITY', Save_TOWN,     Load_TOWN,     NULL, CH_ARRAY | CH_LAST},
    00197 };