tron@3319: /* $Id$ */ tron@3319: belugas@3432: /** @file town_map.h Accessors for towns */ belugas@3432: celestar@3426: #ifndef TOWN_MAP_H celestar@3426: #define TOWN_MAP_H celestar@3426: tron@3319: #include "town.h" rubidium@9723: #include "date_func.h" rubidium@9723: #include "tile_map.h" rubidium@9723: #include "functions.h" tron@3319: truelight@9476: /** truelight@9476: * Get the index of which town this house/street is attached to. truelight@9476: * @param t the tile rubidium@9694: * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) truelight@9476: * @return TownID truelight@9476: */ belugas@3432: static inline TownID GetTownIndex(TileIndex t) tron@3319: { rubidium@9694: assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_ROAD)); // XXX incomplete tron@3319: return _m[t].m2; tron@3319: } tron@3319: belugas@3432: /** tron@3983: * Set the town index for a road or house tile. truelight@9476: * @param t the tile rubidium@9694: * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) belugas@3432: * @param index the index of the town rubidium@9694: * @pre IsTileType(t, MP_ROAD) || IsTileType(t, MP_HOUSE) belugas@3432: */ belugas@3432: static inline void SetTownIndex(TileIndex t, TownID index) belugas@3432: { rubidium@9694: assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_ROAD)); belugas@3432: _m[t].m2 = index; belugas@3432: } belugas@3432: rubidium@6220: /** rubidium@6220: * Gets the town associated with the house or road tile rubidium@6220: * @param t the tile to get the town of rubidium@6220: * @return the town rubidium@6220: */ rubidium@9694: static inline Town *GetTownByTile(TileIndex t) rubidium@6220: { rubidium@6220: return GetTown(GetTownIndex(t)); rubidium@6220: } rubidium@6220: truelight@9476: /** truelight@9476: * Get the type of this house, which is an index into the house spec array truelight@9476: * Since m4 is only a byte and we want to support 512 houses, we use the bit 6 truelight@9476: * of m3 as an additional bit to house type. truelight@9476: * @param t the tile truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: * @return house type truelight@9476: */ truelight@9476: static inline HouseID GetHouseType(TileIndex t) rubidium@6220: { rubidium@6220: assert(IsTileType(t, MP_HOUSE)); truelight@9476: return _m[t].m4 | (GB(_m[t].m3, 6, 1) << 8); celestar@3426: } celestar@3426: truelight@9476: /** truelight@9476: * Set the house type. truelight@9476: * @param t the tile truelight@9476: * @param house_id the new house type truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: */ truelight@9476: static inline void SetHouseType(TileIndex t, HouseID house_id) celestar@3426: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: _m[t].m4 = GB(house_id, 0, 8); truelight@9476: SB(_m[t].m3, 6, 1, GB(house_id, 8, 1)); celestar@3426: } celestar@3426: truelight@9476: /** truelight@9476: * Check if the lift of this animated house has a destination truelight@9476: * @param t the tile truelight@9476: * @return has destination truelight@9476: */ truelight@9476: static inline bool LiftHasDestination(TileIndex t) celestar@3426: { rubidium@9722: return HasBit(_me[t].m7, 0); celestar@3426: } celestar@3426: truelight@9476: /** truelight@9476: * Set the new destination of the lift for this animated house, and activate truelight@9476: * the LiftHasDestination bit. truelight@9476: * @param t the tile truelight@9476: * @param dest new destination truelight@9476: */ truelight@9476: static inline void SetLiftDestination(TileIndex t, byte dest) truelight@9476: { rubidium@9722: SetBit(_me[t].m7, 0); truelight@9476: SB(_me[t].m7, 1, 3, dest); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Get the current destination for this lift truelight@9476: * @param t the tile truelight@9476: * @return destination truelight@9476: */ truelight@9476: static inline byte GetLiftDestination(TileIndex t) truelight@9476: { truelight@9476: return GB(_me[t].m7, 1, 3); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Stop the lift of this animated house from moving. truelight@9476: * Clears the first 4 bits of m7 at once, clearing the LiftHasDestination bit truelight@9476: * and the destination. truelight@9476: * @param t the tile truelight@9476: */ celestar@3426: static inline void HaltLift(TileIndex t) celestar@3426: { truelight@9476: SB(_me[t].m7, 0, 4, 0); celestar@3426: DeleteAnimatedTile(t); celestar@3426: } celestar@3426: truelight@9476: /** truelight@9476: * Get the position of the lift on this animated house truelight@9476: * @param t the tile truelight@9476: * @return position, from 0 to 36 truelight@9476: */ celestar@3426: static inline byte GetLiftPosition(TileIndex t) celestar@3426: { truelight@9476: return GB(_m[t].m6, 2, 6); celestar@3426: } celestar@3426: truelight@9476: /** truelight@9476: * Set the position of the lift on this animated house truelight@9476: * @param t the tile glx@9574: * @param pos position, from 0 to 36 truelight@9476: */ celestar@3426: static inline void SetLiftPosition(TileIndex t, byte pos) celestar@3426: { truelight@9476: SB(_m[t].m6, 2, 6, pos); celestar@3426: } celestar@3426: truelight@9476: /** truelight@9476: * Get the current animation frame for this house truelight@9476: * @param t the tile truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: * @return frame number truelight@9476: */ truelight@9476: static inline byte GetHouseAnimationFrame(TileIndex t) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); rubidium@9826: return GB(_m[t].m6, 2, 6) | (GB(_m[t].m3, 5, 1) << 6); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Set a new animation frame for this house truelight@9476: * @param t the tile truelight@9476: * @param frame the new frame number truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: */ truelight@9476: static inline void SetHouseAnimationFrame(TileIndex t, byte frame) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); rubidium@9826: SB(_m[t].m6, 2, 6, GB(frame, 0, 6)); rubidium@9826: SB(_m[t].m3, 5, 1, GB(frame, 6, 1)); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Get the completion of this house truelight@9476: * @param t the tile truelight@9476: * @return true if it is, false if it is not truelight@9476: */ truelight@9476: static inline bool IsHouseCompleted(TileIndex t) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); rubidium@9722: return HasBit(_m[t].m3, 7); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Mark this house as been completed truelight@9476: * @param t the tile truelight@9476: * @param status truelight@9476: */ truelight@9476: static inline void SetHouseCompleted(TileIndex t, bool status) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: SB(_m[t].m3, 7, 1, !!status); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Make the tile a house. truelight@9476: * @param t tile index truelight@9476: * @param tid Town index truelight@9476: * @param counter of construction step truelight@9476: * @param stage of construction (used for drawing) truelight@9476: * @param type of house. Index into house specs array truelight@9476: * @param random_bits required for newgrf houses truelight@9476: * @pre IsTileType(t, MP_CLEAR) truelight@9476: */ truelight@9476: static inline void MakeHouseTile(TileIndex t, TownID tid, byte counter, byte stage, HouseID type, byte random_bits) celestar@3382: { celestar@3382: assert(IsTileType(t, MP_CLEAR)); celestar@3382: celestar@3382: SetTileType(t, MP_HOUSE); truelight@9476: _m[t].m1 = random_bits; celestar@3382: _m[t].m2 = tid; truelight@9476: _m[t].m3 = 0; truelight@9476: SetHouseType(t, type); truelight@9476: SetHouseCompleted(t, stage == TOWN_HOUSE_COMPLETED); truelight@9476: _m[t].m5 = IsHouseCompleted(t) ? 0 : (stage << 3 | counter); truelight@9476: SetHouseAnimationFrame(t, 0); truelight@9476: _me[t].m7 = GetHouseSpecs(type)->processing_time; celestar@3382: truelight@9476: if (GetHouseSpecs(type)->building_flags & BUILDING_IS_ANIMATED) AddAnimatedTile(t); celestar@3382: MarkTileDirtyByTile(t); celestar@3382: } celestar@3382: truelight@9476: /** belugas@3432: * House Construction Scheme. belugas@3432: * Construction counter, for buildings under construction. Incremented on every belugas@3432: * periodic tile processing. belugas@3432: * On wraparound, the stage of building in is increased. truelight@9476: * GetHouseBuildingStage is taking care of the real stages, belugas@3432: * (as the sprite for the next phase of house building) truelight@9476: * (Get|Inc)HouseConstructionTick is simply a tick counter between the belugas@3432: * different stages belugas@3432: */ belugas@3432: belugas@3432: /** belugas@3432: * Gets the building stage of a house truelight@9476: * Since the stage is used for determining what sprite to use, truelight@9476: * if the house is complete (and that stage no longuer is available), truelight@9476: * fool the system by returning the TOWN_HOUSE_COMPLETE (3), truelight@9476: * thus showing a beautiful complete house. truelight@9476: * @param t the tile of the house to get the building stage of belugas@3432: * @pre IsTileType(t, MP_HOUSE) belugas@3432: * @return the building stage of the house belugas@3432: */ belugas@3432: static inline byte GetHouseBuildingStage(TileIndex t) belugas@3432: { belugas@3432: assert(IsTileType(t, MP_HOUSE)); truelight@9476: return IsHouseCompleted(t) ? (byte)TOWN_HOUSE_COMPLETED : GB(_m[t].m5, 3, 2); belugas@3432: } belugas@3432: belugas@3432: /** belugas@3432: * Gets the construction stage of a house truelight@9476: * @param t the tile of the house to get the construction stage of belugas@3432: * @pre IsTileType(t, MP_HOUSE) belugas@3432: * @return the construction stage of the house belugas@3432: */ belugas@3432: static inline byte GetHouseConstructionTick(TileIndex t) belugas@3432: { belugas@3432: assert(IsTileType(t, MP_HOUSE)); truelight@9476: return IsHouseCompleted(t) ? 0 : GB(_m[t].m5, 0, 3); belugas@3432: } belugas@3432: belugas@3432: /** belugas@3432: * Sets the increment stage of a house truelight@9476: * It is working with the whole counter + stage 5 bits, making it truelight@9476: * easier to work: the wraparound is automatic. truelight@9476: * @param t the tile of the house to increment the construction stage of belugas@3432: * @pre IsTileType(t, MP_HOUSE) belugas@3432: */ belugas@3432: static inline void IncHouseConstructionTick(TileIndex t) belugas@3432: { belugas@3432: assert(IsTileType(t, MP_HOUSE)); truelight@9476: AB(_m[t].m5, 0, 5, 1); truelight@9476: truelight@9476: if (GB(_m[t].m5, 3, 2) == TOWN_HOUSE_COMPLETED) { truelight@9476: /* House is now completed. truelight@9476: * Store the year of construction as well, for newgrf house purpose */ truelight@9476: SetHouseCompleted(t, true); rubidium@9722: _m[t].m5 = Clamp(_cur_year - ORIGINAL_BASE_YEAR, 0, 0xFF); truelight@9476: } belugas@3432: } belugas@3432: truelight@9476: /** truelight@9476: * Get the year that this house was constructed (between 1920 and 2175). truelight@9476: * @param t the tile of this house truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: * @return year truelight@9476: */ truelight@9476: static inline Year GetHouseConstructionYear(TileIndex t) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: return IsHouseCompleted(t) ? _m[t].m5 + ORIGINAL_BASE_YEAR : 0; truelight@9476: } truelight@9476: truelight@9476: /** rubidium@9722: * Set the random bits for this house. rubidium@9722: * This is required for newgrf house rubidium@9722: * @param t the tile of this house rubidium@9722: * @param random the new random bits rubidium@9722: * @pre IsTileType(t, MP_HOUSE) rubidium@9722: */ rubidium@9722: static inline void SetHouseRandomBits(TileIndex t, byte random) rubidium@9722: { rubidium@9722: assert(IsTileType(t, MP_HOUSE)); rubidium@9722: _m[t].m1 = random; rubidium@9722: } rubidium@9722: rubidium@9722: /** truelight@9476: * Get the random bits for this house. truelight@9476: * This is required for newgrf house truelight@9476: * @param t the tile of this house truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: * @return random bits truelight@9476: */ truelight@9476: static inline byte GetHouseRandomBits(TileIndex t) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: return _m[t].m1; truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Set the activated triggers bits for this house. truelight@9476: * This is required for newgrf house glx@9574: * @param t the tile of this house glx@9574: * @param triggers the activated triggers truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: */ truelight@9476: static inline void SetHouseTriggers(TileIndex t, byte triggers) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: SB(_m[t].m3, 0, 5, triggers); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Get the already activated triggers bits for this house. truelight@9476: * This is required for newgrf house truelight@9476: * @param t the tile of this house truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: * @return triggers truelight@9476: */ truelight@9476: static inline byte GetHouseTriggers(TileIndex t) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: return GB(_m[t].m3, 0, 5); truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Get the amount of time remaining before the tile loop processes this tile. truelight@9476: * @param t the house tile truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: * @return time remaining truelight@9476: */ truelight@9476: static inline byte GetHouseProcessingTime(TileIndex t) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: return _me[t].m7; truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Set the amount of time remaining before the tile loop processes this tile. truelight@9476: * @param t the house tile truelight@9476: * @param time the time to be set truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: */ truelight@9476: static inline void SetHouseProcessingTime(TileIndex t, byte time) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: _me[t].m7 = time; truelight@9476: } truelight@9476: truelight@9476: /** truelight@9476: * Decrease the amount of time remaining before the tile loop processes this tile. truelight@9476: * @param t the house tile truelight@9476: * @pre IsTileType(t, MP_HOUSE) truelight@9476: */ truelight@9476: static inline void DecHouseProcessingTime(TileIndex t) truelight@9476: { truelight@9476: assert(IsTileType(t, MP_HOUSE)); truelight@9476: _me[t].m7--; truelight@9476: } belugas@3432: belugas@3432: #endif /* TOWN_MAP_H */