# town_sl.cpp

Go to the documentation of this file.

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