diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/newgrf.cpp | 7 | ||||
-rw-r--r-- | src/newgrf_commons.cpp | 150 | ||||
-rw-r--r-- | src/newgrf_commons.h | 69 | ||||
-rw-r--r-- | src/newgrf_house.cpp | 94 | ||||
-rw-r--r-- | src/newgrf_house.h | 8 | ||||
-rw-r--r-- | src/openttd.cpp | 3 | ||||
-rw-r--r-- | src/town_cmd.cpp | 20 |
7 files changed, 245 insertions, 106 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp index d2e2c21e8..3dc46fcbf 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -40,6 +40,7 @@ #include "cargotype.h" #include "industry.h" #include "newgrf_canal.h" +#include "newgrf_commons.h" /* TTDPatch extended GRF format codec * (c) Petr Baudis 2004 (GPL'd) @@ -1246,7 +1247,7 @@ static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, in _cur_grffile->housespec = CallocT<HouseSpec*>(HOUSE_MAX); /* Reset any overrides that have been set. */ - ResetHouseOverrides(); + _house_mngr.ResetOverride(); } housespec = &_cur_grffile->housespec[hid]; @@ -1372,7 +1373,7 @@ static bool TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, in return false; } - AddHouseOverride(hid, override); + _house_mngr.Add(hid, override); } break; @@ -4591,7 +4592,7 @@ static void FinaliseHouseArray() for (int i = 0; i < HOUSE_MAX; i++) { HouseSpec *hs = file->housespec[i]; if (hs != NULL) { - SetHouseSpec(hs); + _house_mngr.SetEntitySpec(hs); if (hs->min_date < 1930) reset_dates = false; } } diff --git a/src/newgrf_commons.cpp b/src/newgrf_commons.cpp new file mode 100644 index 000000000..f52c1442a --- /dev/null +++ b/src/newgrf_commons.cpp @@ -0,0 +1,150 @@ +/* $Id$ */ + +/** @file newgrf_commons.cpp Implementation of the class OverrideManagerBase + * and its descendance, present and futur + */ + +#include "stdafx.h" +#include "openttd.h" +#include "town.h" +#include "industry.h" +#include "newgrf.h" +#include "newgrf_commons.h" + +/** Constructor of generic class + * @param offset end of original data for this entity. i.e: houses = 110 + * @param maximum of entities this manager can deal with. i.e: houses = 512 + * @param invalid is the ID used to identify an invalid entity id + */ +OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid) +{ + max_offset = offset; + max_new_entities = maximum; + invalid_ID = invalid; + + mapping_ID = CallocT<EntityIDMapping>(max_new_entities); + entity_overrides = MallocT<uint16>(max_offset); + memset(entity_overrides, invalid, sizeof(entity_overrides)); +} + +/** Destructor of the generic class. + * Frees allocated memory of constructor + */ +OverrideManagerBase::~OverrideManagerBase() +{ + free(mapping_ID); + free(entity_overrides); +} + +/** Since the entity IDs defined by the GRF file does not necessarily correlate + * to those used by the game, the IDs used for overriding old entities must be + * translated when the entity spec is set. + * @param local_id id in grf file + * @param entity_type original entity type + */ +void OverrideManagerBase::Add(uint8 local_id, uint entity_type) +{ + assert(entity_type < max_offset); + entity_overrides[entity_type] = local_id; +} + +/** Resets the mapping, which is used while initializing game */ +void OverrideManagerBase::ResetMapping() +{ + memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping)); +} + +/** Resets the override, which is used while initializing game */ +void OverrideManagerBase::ResetOverride() +{ + for (uint16 i = 0; i < max_offset; i++) { + entity_overrides[i] = invalid_ID; + } +} + +/** Return the ID (if ever available) of a previously inserted entity. + * @param grf_local_id ID of this enity withing the grfID + * @param grfid ID of the grf file + * @return the ID of the candidate, of the Invalid flag item ID + */ +uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid) +{ + const EntityIDMapping *map; + + for (uint16 id = max_offset; id < max_new_entities; id++) { + map = &mapping_ID[id]; + if (map->entity_id == grf_local_id && map->grfid == grfid) { + return id; + } + } + return invalid_ID; +} + +/** Reserves a place in the mapping array for an entity to be installed + * @param grf_local_id is an arbitrary id given by the grf's author. Also known as setid + * @param grfid is the id of the grf file itself + * @param substitute_id is the original entity from which data is copied for the new one + * @return the proper usable slot id, or invalid marker if none is found + */ +uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id) +{ + uint16 id = this->GetID(grf_local_id, grfid); + EntityIDMapping *map; + + /* Look to see if this entity has already been added. This is done + * separately from the loop below in case a GRF has been deleted, and there + * are any gaps in the array. + */ + if (id != invalid_ID) { + return id; + } + + /* This entity hasn't been defined before, so give it an ID now. */ + for (id = max_offset; id < max_new_entities; id++) { + map = &mapping_ID[id]; + + if (map->entity_id == 0 && map->grfid == 0) { + map->entity_id = grf_local_id; + map->grfid = grfid; + map->substitute_id = substitute_id; + return id; + } + } + + return invalid_ID; +} + +/** Gives the substitute of the entity, as specified by the grf file + * @param entity_id of the entity being queried + * @return mapped id + */ +uint16 OverrideManagerBase::GetSubstituteID(byte entity_id) +{ + return mapping_ID[entity_id].substitute_id; +} + +/** Install the specs into the HouseSpecs array + * It will find itself the proper slot onwhich it will go + * @param hs HouseSpec read from the grf file, ready for inclusion + */ +void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs) +{ + HouseID house_id = this->AddEntityID(hs->local_id, hs->grffile->grfid, hs->substitute_id); + + if (house_id == invalid_ID) { + grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring."); + return; + } + + memcpy(&_house_specs[house_id], hs, sizeof(*hs)); + + /* Now add the overrides. */ + for (int i = 0; i != max_offset; i++) { + HouseSpec *overridden_hs = GetHouseSpecs(i); + + if (entity_overrides[i] != hs->local_id) continue; + + overridden_hs->override = house_id; + entity_overrides[i] = invalid_ID; + } +} diff --git a/src/newgrf_commons.h b/src/newgrf_commons.h new file mode 100644 index 000000000..812f514a6 --- /dev/null +++ b/src/newgrf_commons.h @@ -0,0 +1,69 @@ +/* $Id$ */ + +/** @file newgrf_commons.h This file simplyfies and embeds a common mechanism of + * loading/saving and mapping of grf entities. + */ + +#ifndef NEWGRF_COMMONS_H +#define NEWGRF_COMMONS_H + +/** + * Maps an entity id stored on the map to a GRF file. + * Entities are objects used ingame (houses, industries, industry tiles) for + * which we need to correlate the ids from the grf files with the ones in the + * the savegames themselves. + * An array of EntityIDMapping structs is saved with the savegame so + * that those GRFs can be loaded in a different order, or removed safely. The + * index in the array is the entity's ID stored on the map. + * + * The substitute ID is the ID of an original entity that should be used instead + * if the GRF containing the new entity is not available. + */ +struct EntityIDMapping { + uint32 grfid; ///< The GRF ID of the file the entity belongs to + uint8 entity_id; ///< The entity ID within the GRF file + uint8 substitute_id; ///< The (original) entity ID to use if this GRF is not available +}; + +class OverrideManagerBase +{ +protected: + uint16 *entity_overrides; + + uint16 max_offset; ///< what is the length of the original entity's array of specs + uint16 max_new_entities; ///< what is the amount of entities, old and new summed + + uint16 invalid_ID; ///< ID used to dected invalid entities; + + virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id); +public: + EntityIDMapping *mapping_ID; ///< mapping of ids from grf files. Public out of convenience + + OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid); + virtual ~OverrideManagerBase(); + + void ResetOverride(); + void ResetMapping(); + + void Add(uint8 local_id, uint entity_type); + + uint16 GetSubstituteID(byte entity_id); + uint16 GetID(uint8 grf_local_id, uint32 grfid); + + inline uint16 GetMaxMapping() { return max_new_entities; }; + inline uint16 GetMaxOffset() { return max_offset; }; +}; + + +struct HouseSpec; +class HouseOverrideManager : public OverrideManagerBase +{ +public: + HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : OverrideManagerBase(offset, maximum, invalid) {}; + void SetEntitySpec(const HouseSpec *hs); +}; + + +extern HouseOverrideManager _house_mngr; + +#endif /* NEWGRF_COMMONS_H */ diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp index 79ad7f3d5..13ca035a1 100644 --- a/src/newgrf_house.cpp +++ b/src/newgrf_house.cpp @@ -24,92 +24,12 @@ #include "newgrf_callbacks.h" #include "newgrf_town.h" #include "newgrf_sound.h" +#include "newgrf_commons.h" static BuildingCounts _building_counts; static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX]; -HouseIDMapping _house_id_mapping[HOUSE_MAX]; -/* Since the house IDs defined by the GRF file don't necessarily correlate - * to those used by the game, the IDs used for overriding old houses must be - * translated when the house spec is set. */ -static uint16 _house_overrides[NEW_HOUSE_OFFSET]; - -void AddHouseOverride(uint8 local_id, uint house_type) -{ - assert(house_type < NEW_HOUSE_OFFSET); - _house_overrides[house_type] = local_id; -} - -void ResetHouseOverrides() -{ - for (int i = 0; i != lengthof(_house_overrides); i++) { - _house_overrides[i] = INVALID_HOUSE_ID; - } -} - -static HouseID GetHouseID(byte grf_local_id, uint32 grfid) -{ - const HouseIDMapping *map; - - for (HouseID house_id = NEW_HOUSE_OFFSET; house_id != lengthof(_house_id_mapping); house_id++) { - map = &_house_id_mapping[house_id]; - if (map->house_id == grf_local_id && map->grfid == grfid) return house_id; - } - return INVALID_HOUSE_ID; -} - -static HouseID AddHouseID(byte grf_local_id, uint32 grfid, byte substitute_id) -{ - HouseID house_id; - HouseIDMapping *map; - - /* Look to see if this house has already been added. This is done - * separately from the loop below in case a GRF has been deleted, and there - * are any gaps in the array. */ - house_id = GetHouseID(grf_local_id, grfid); - if (house_id != INVALID_HOUSE_ID) return house_id; - - /* This house hasn't been defined before, so give it an ID now. */ - for (house_id = NEW_HOUSE_OFFSET; house_id != lengthof(_house_id_mapping); house_id++) { - map = &_house_id_mapping[house_id]; - - if (map->house_id == 0 && map->grfid == 0) { - map->house_id = grf_local_id; - map->grfid = grfid; - map->substitute_id = substitute_id; - return house_id; - } - } - - return INVALID_HOUSE_ID; -} - -void SetHouseSpec(const HouseSpec *hs) -{ - HouseID house_id = AddHouseID(hs->local_id, hs->grffile->grfid, hs->substitute_id); - - if (house_id == INVALID_HOUSE_ID) { - grfmsg(1, "SetHouseSpec: Too many houses allocated. Ignoring."); - return; - } - - memcpy(&_house_specs[house_id], hs, sizeof(*hs)); - - /* Now add the overrides. */ - for (int i = 0; i != lengthof(_house_overrides); i++) { - HouseSpec *overridden_hs = GetHouseSpecs(i); - - if (_house_overrides[i] != hs->local_id) continue; - - overridden_hs->override = house_id; - _house_overrides[i] = INVALID_HOUSE_ID; - } -} - -void ResetHouseIDMapping() -{ - memset(&_house_id_mapping, 0, sizeof(_house_id_mapping)); -} +HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID); void CheckHouseIDs() { @@ -122,7 +42,7 @@ void CheckHouseIDs() if (!GetHouseSpecs(house_id)->enabled && house_id >= NEW_HOUSE_OFFSET) { /* The specs for this type of house are not available any more, so * replace it with the substitute original house type. */ - SetHouseType(t, _house_id_mapping[house_id].substitute_id); + SetHouseType(t, _house_mngr.GetSubstituteID(house_id)); } } @@ -313,7 +233,7 @@ static uint32 HouseGetVariable(const ResolverObject *object, byte variable, byte const HouseSpec *hs = GetHouseSpecs(house_id); if (hs->grffile == NULL) return 0; - HouseID new_house = GetHouseID(parameter, hs->grffile->grfid); + HouseID new_house = _house_mngr.GetID(parameter, hs->grffile->grfid); return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town); } @@ -577,7 +497,11 @@ bool NewHouseTileLoop(TileIndex tile) return true; } - /* @todo: Magic with triggers goes here. Got to implement that, one day. .. */ + /* @todo: Magic with triggers goes here. Got to implement that, one day. .. + * Process randomizing of tiles following specs. + * Once done, redraw the house + * MarkTileDirtyByTile(tile); + */ if (HASBIT(hs->callback_mask, CBM_ANIMATION_START_STOP)) { /* If this house is marked as having a synchronised callback, all the diff --git a/src/newgrf_house.h b/src/newgrf_house.h index 513b46094..bdd21b907 100644 --- a/src/newgrf_house.h +++ b/src/newgrf_house.h @@ -41,15 +41,7 @@ struct HouseClassMapping { uint8 class_id; ////< The class id within the grf file }; -extern HouseIDMapping _house_id_mapping[HOUSE_MAX]; ///< Declared in newgrf_house.cpp - -void AddHouseOverride(uint8 local_id, uint house_type); -void ResetHouseOverrides(); - -void SetHouseSpec(const HouseSpec *hs); - void CheckHouseIDs(); -void ResetHouseIDMapping(); HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid); diff --git a/src/openttd.cpp b/src/openttd.cpp index 088d9fce5..a2bb58b43 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -61,6 +61,7 @@ #include "newgrf.h" #include "newgrf_config.h" #include "newgrf_house.h" +#include "newgrf_commons.h" #include "player_face.h" #include "bridge_map.h" @@ -685,7 +686,7 @@ static void MakeNewGame(bool from_heightmap) _game_mode = GM_NORMAL; ResetGRFConfig(true); - ResetHouseIDMapping(); + _house_mngr.ResetMapping(); GenerateWorldSetCallback(&MakeNewGameDone); GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _patches.map_x, 1 << _patches.map_y); diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 4f1e29b8d..942b65c85 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -38,6 +38,7 @@ #include "newgrf.h" #include "newgrf_callbacks.h" #include "newgrf_house.h" +#include "newgrf_commons.h" /** * Called if a new block is added to the town-pool @@ -2371,19 +2372,19 @@ static const SaveLoad _town_desc[] = { /* Save and load the mapping between the house id on the map, and the grf file * it came from. */ static const SaveLoad _house_id_mapping_desc[] = { - SLE_VAR(HouseIDMapping, grfid, SLE_UINT32), - SLE_VAR(HouseIDMapping, house_id, SLE_UINT8), - SLE_VAR(HouseIDMapping, substitute_id, SLE_UINT8), + SLE_VAR(EntityIDMapping, grfid, SLE_UINT32), + SLE_VAR(EntityIDMapping, entity_id, SLE_UINT8), + SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8), SLE_END() }; static void Save_HOUSEIDS() { - uint i; + uint j = _house_mngr.GetMaxMapping(); - for (i = 0; i != lengthof(_house_id_mapping); i++) { + for (uint i = 0; i < j; i++) { SlSetArrayIndex(i); - SlObject(&_house_id_mapping[i], _house_id_mapping_desc); + SlObject(&_house_mngr.mapping_ID[i], _house_id_mapping_desc); } } @@ -2391,11 +2392,12 @@ static void Load_HOUSEIDS() { int index; - ResetHouseIDMapping(); + _house_mngr.ResetMapping(); + uint max_id = _house_mngr.GetMaxMapping(); while ((index = SlIterateArray()) != -1) { - if ((uint)index >= lengthof(_house_id_mapping)) break; - SlObject(&_house_id_mapping[index], _house_id_mapping_desc); + if ((uint)index >= max_id) break; + SlObject(&_house_mngr.mapping_ID[index], _house_id_mapping_desc); } } |