belugas@6629: /* $Id$ */ belugas@6629: belugas@6629: /** @file newgrf_commons.h This file simplyfies and embeds a common mechanism of belugas@6629: * loading/saving and mapping of grf entities. belugas@6629: */ belugas@6629: belugas@6629: #ifndef NEWGRF_COMMONS_H belugas@6629: #define NEWGRF_COMMONS_H belugas@6629: frosch@10355: #include "core/bitmath_func.hpp" frosch@10355: frosch@10355: #include "table/sprites.h" frosch@10355: belugas@6629: /** belugas@6629: * Maps an entity id stored on the map to a GRF file. belugas@6629: * Entities are objects used ingame (houses, industries, industry tiles) for belugas@6629: * which we need to correlate the ids from the grf files with the ones in the belugas@6629: * the savegames themselves. belugas@6629: * An array of EntityIDMapping structs is saved with the savegame so belugas@6629: * that those GRFs can be loaded in a different order, or removed safely. The belugas@6629: * index in the array is the entity's ID stored on the map. belugas@6629: * belugas@6629: * The substitute ID is the ID of an original entity that should be used instead belugas@6629: * if the GRF containing the new entity is not available. belugas@6629: */ belugas@6629: struct EntityIDMapping { belugas@6629: uint32 grfid; ///< The GRF ID of the file the entity belongs to belugas@6629: uint8 entity_id; ///< The entity ID within the GRF file belugas@6629: uint8 substitute_id; ///< The (original) entity ID to use if this GRF is not available belugas@6629: }; belugas@6629: rubidium@7325: class OverrideManagerBase { belugas@6629: protected: belugas@6629: uint16 *entity_overrides; glx@7873: uint32 *grfid_overrides; belugas@6629: belugas@6629: uint16 max_offset; ///< what is the length of the original entity's array of specs belugas@6629: uint16 max_new_entities; ///< what is the amount of entities, old and new summed belugas@6629: belugas@6629: uint16 invalid_ID; ///< ID used to dected invalid entities; belugas@7632: virtual bool CheckValidNewID(uint16 testid) { return true; } belugas@6629: belugas@6629: public: belugas@6629: EntityIDMapping *mapping_ID; ///< mapping of ids from grf files. Public out of convenience belugas@6629: belugas@6629: OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid); belugas@6629: virtual ~OverrideManagerBase(); belugas@6629: belugas@6629: void ResetOverride(); belugas@6629: void ResetMapping(); belugas@6629: glx@7873: void Add(uint8 local_id, uint32 grfid, uint entity_type); belugas@6835: virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id); belugas@6629: belugas@6629: uint16 GetSubstituteID(byte entity_id); glx@8511: virtual uint16 GetID(uint8 grf_local_id, uint32 grfid); belugas@6629: rubidium@7325: inline uint16 GetMaxMapping() { return max_new_entities; } rubidium@7325: inline uint16 GetMaxOffset() { return max_offset; } belugas@6629: }; belugas@6629: belugas@6629: belugas@6629: struct HouseSpec; rubidium@7325: class HouseOverrideManager : public OverrideManagerBase { belugas@6629: public: belugas@6835: HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : rubidium@7325: OverrideManagerBase(offset, maximum, invalid) {} belugas@6629: void SetEntitySpec(const HouseSpec *hs); belugas@6629: }; belugas@6629: belugas@6629: belugas@6835: struct IndustrySpec; rubidium@7325: class IndustryOverrideManager : public OverrideManagerBase { rubidium@7325: public: rubidium@7325: IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : rubidium@7325: OverrideManagerBase(offset, maximum, invalid) {} belugas@6835: rubidium@7325: virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id); glx@8511: virtual uint16 GetID(uint8 grf_local_id, uint32 grfid); belugas@7667: void SetEntitySpec(IndustrySpec *inds); belugas@6835: }; belugas@6835: belugas@7000: belugas@7000: struct IndustryTileSpec; rubidium@7325: class IndustryTileOverrideManager : public OverrideManagerBase { belugas@7632: protected: belugas@7632: virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; } rubidium@7325: public: rubidium@7325: IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : rubidium@7325: OverrideManagerBase(offset, maximum, invalid) {} rubidium@7325: rubidium@7325: void SetEntitySpec(const IndustryTileSpec *indts); belugas@7000: }; belugas@7000: belugas@6629: extern HouseOverrideManager _house_mngr; belugas@6835: extern IndustryOverrideManager _industry_mngr; belugas@7000: extern IndustryTileOverrideManager _industile_mngr; belugas@6629: belugas@6801: uint32 GetTerrainType(TileIndex tile); belugas@6827: TileIndex GetNearbyTile(byte parameter, TileIndex tile); frosch@8458: uint32 GetNearbyTileInformation(TileIndex tile); belugas@6801: frosch@10355: /** frosch@10355: * Applies PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOR to a palette entry of a sprite layout entry frosch@10355: * @Note for ground sprites use #GroundSpritePaletteTransform frosch@10355: * @Note Not useable for OTTD internal spritelayouts from table/xxx_land.h as PALETTE_MODIFIER_TRANSPARENT is only set frosch@10355: * when to use the default palette. frosch@10355: * frosch@10355: * @param image The sprite to draw frosch@10355: * @param pal The palette from the sprite layout frosch@10355: * @param default_pal The default recolour sprite to use (typically company color resp. random industry/house color) frosch@10355: * @return The palette to use frosch@10355: */ frosch@10355: static inline SpriteID SpriteLayoutPaletteTransform(SpriteID image, SpriteID pal, SpriteID default_pal) frosch@10355: { frosch@10355: if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) { frosch@10355: return (pal != 0 ? pal : default_pal); frosch@10355: } else { frosch@10355: return PAL_NONE; frosch@10355: } frosch@10355: } frosch@10355: frosch@10355: /** frosch@10355: * Applies PALETTE_MODIFIER_COLOR to a palette entry of a ground sprite frosch@10355: * @Note Not useable for OTTD internal spritelayouts from table/xxx_land.h as PALETTE_MODIFIER_TRANSPARENT is only set frosch@10355: * when to use the default palette. frosch@10355: * frosch@10355: * @param image The sprite to draw frosch@10355: * @param pal The palette from the sprite layout frosch@10355: * @param default_pal The default recolour sprite to use (typically company color resp. random industry/house color) frosch@10355: * @return The palette to use frosch@10355: */ frosch@10355: static inline SpriteID GroundSpritePaletteTransform(SpriteID image, SpriteID pal, SpriteID default_pal) frosch@10355: { frosch@10355: if (HasBit(image, PALETTE_MODIFIER_COLOR)) { frosch@10355: return (pal != 0 ? pal : default_pal); frosch@10355: } else { frosch@10355: return PAL_NONE; frosch@10355: } frosch@10355: } frosch@10355: belugas@6629: #endif /* NEWGRF_COMMONS_H */