richk@6719: /* $Id$ */ richk@6719: richk@6719: /** @file newgrf_commons.h This file simplyfies and embeds a common mechanism of richk@6719: * loading/saving and mapping of grf entities. richk@6719: */ richk@6719: richk@6719: #ifndef NEWGRF_COMMONS_H richk@6719: #define NEWGRF_COMMONS_H richk@6719: richk@6719: /** richk@6719: * Maps an entity id stored on the map to a GRF file. richk@6719: * Entities are objects used ingame (houses, industries, industry tiles) for richk@6719: * which we need to correlate the ids from the grf files with the ones in the richk@6719: * the savegames themselves. richk@6719: * An array of EntityIDMapping structs is saved with the savegame so richk@6719: * that those GRFs can be loaded in a different order, or removed safely. The richk@6719: * index in the array is the entity's ID stored on the map. richk@6719: * richk@6719: * The substitute ID is the ID of an original entity that should be used instead richk@6719: * if the GRF containing the new entity is not available. richk@6719: */ richk@6719: struct EntityIDMapping { richk@6719: uint32 grfid; ///< The GRF ID of the file the entity belongs to richk@6719: uint8 entity_id; ///< The entity ID within the GRF file richk@6719: uint8 substitute_id; ///< The (original) entity ID to use if this GRF is not available richk@6719: }; richk@6719: richk@6743: class OverrideManagerBase { richk@6719: protected: richk@6719: uint16 *entity_overrides; rubidium@6871: uint32 *grfid_overrides; richk@6719: richk@6719: uint16 max_offset; ///< what is the length of the original entity's array of specs richk@6719: uint16 max_new_entities; ///< what is the amount of entities, old and new summed richk@6719: richk@6719: uint16 invalid_ID; ///< ID used to dected invalid entities; rubidium@6870: virtual bool CheckValidNewID(uint16 testid) { return true; } richk@6719: richk@6719: public: richk@6719: EntityIDMapping *mapping_ID; ///< mapping of ids from grf files. Public out of convenience richk@6719: richk@6719: OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid); richk@6719: virtual ~OverrideManagerBase(); richk@6719: richk@6719: void ResetOverride(); richk@6719: void ResetMapping(); richk@6719: rubidium@6871: void Add(uint8 local_id, uint32 grfid, uint entity_type); richk@6720: virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id); richk@6719: richk@6719: uint16 GetSubstituteID(byte entity_id); richk@6719: uint16 GetID(uint8 grf_local_id, uint32 grfid); richk@6719: richk@6743: inline uint16 GetMaxMapping() { return max_new_entities; } richk@6743: inline uint16 GetMaxOffset() { return max_offset; } richk@6719: }; richk@6719: richk@6719: richk@6719: struct HouseSpec; richk@6743: class HouseOverrideManager : public OverrideManagerBase { richk@6719: public: richk@6720: HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : richk@6743: OverrideManagerBase(offset, maximum, invalid) {} richk@6719: void SetEntitySpec(const HouseSpec *hs); richk@6719: }; richk@6719: richk@6719: richk@6720: struct IndustrySpec; richk@6743: class IndustryOverrideManager : public OverrideManagerBase { richk@6743: public: richk@6743: IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : richk@6743: OverrideManagerBase(offset, maximum, invalid) {} richk@6720: richk@6743: virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id); rubidium@6870: void SetEntitySpec(IndustrySpec *inds); richk@6720: }; richk@6720: richk@6720: richk@6720: struct IndustryTileSpec; richk@6743: class IndustryTileOverrideManager : public OverrideManagerBase { rubidium@6870: protected: rubidium@6870: virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; } richk@6743: public: richk@6743: IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : richk@6743: OverrideManagerBase(offset, maximum, invalid) {} richk@6743: richk@6743: void SetEntitySpec(const IndustryTileSpec *indts); richk@6720: }; richk@6720: richk@6719: extern HouseOverrideManager _house_mngr; richk@6720: extern IndustryOverrideManager _industry_mngr; richk@6720: extern IndustryTileOverrideManager _industile_mngr; richk@6720: richk@6720: uint32 GetTerrainType(TileIndex tile); richk@6720: TileIndex GetNearbyTile(byte parameter, TileIndex tile); rubidium@6877: uint32 GetNearbyTileInformation(TileIndex tile); richk@6719: richk@6719: #endif /* NEWGRF_COMMONS_H */