2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file town_map.h Accessors for towns */
15 #include "timer/timer_game_calendar.h"
18 * Get the index of which town this house/street is attached to.
20 * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) but not a road depot
23 inline TownID
GetTownIndex(Tile t
)
25 assert(IsTileType(t
, MP_HOUSE
) || (IsTileType(t
, MP_ROAD
) && !IsRoadDepot(t
)));
30 * Set the town index for a road or house tile.
32 * @param index the index of the town
33 * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) but not a road depot
35 inline void SetTownIndex(Tile t
, TownID index
)
37 assert(IsTileType(t
, MP_HOUSE
) || (IsTileType(t
, MP_ROAD
) && !IsRoadDepot(t
)));
42 * Get the type of this house, which is an index into the house spec array
43 * without doing any NewGRF related translations.
45 * @pre IsTileType(t, MP_HOUSE)
48 inline HouseID
GetCleanHouseType(Tile t
)
50 assert(IsTileType(t
, MP_HOUSE
));
51 return t
.m4() | (GB(t
.m3(), 6, 1) << 8);
55 * Get the type of this house, which is an index into the house spec array
57 * @pre IsTileType(t, MP_HOUSE)
60 inline HouseID
GetHouseType(Tile t
)
62 return GetTranslatedHouseID(GetCleanHouseType(t
));
68 * @param house_id the new house type
69 * @pre IsTileType(t, MP_HOUSE)
71 inline void SetHouseType(Tile t
, HouseID house_id
)
73 assert(IsTileType(t
, MP_HOUSE
));
74 t
.m4() = GB(house_id
, 0, 8);
75 SB(t
.m3(), 6, 1, GB(house_id
, 8, 1));
79 * Check if the lift of this animated house has a destination
81 * @return has destination
83 inline bool LiftHasDestination(Tile t
)
85 return HasBit(t
.m7(), 0);
89 * Set the new destination of the lift for this animated house, and activate
90 * the LiftHasDestination bit.
92 * @param dest new destination
94 inline void SetLiftDestination(Tile t
, uint8_t dest
)
97 SB(t
.m7(), 1, 3, dest
);
101 * Get the current destination for this lift
103 * @return destination
105 inline uint8_t GetLiftDestination(Tile t
)
107 return GB(t
.m7(), 1, 3);
111 * Stop the lift of this animated house from moving.
112 * Clears the first 4 bits of m7 at once, clearing the LiftHasDestination bit
113 * and the destination.
116 inline void HaltLift(Tile t
)
122 * Get the position of the lift on this animated house
124 * @return position, from 0 to 36
126 inline uint8_t GetLiftPosition(Tile t
)
128 return GB(t
.m6(), 2, 6);
132 * Set the position of the lift on this animated house
134 * @param pos position, from 0 to 36
136 inline void SetLiftPosition(Tile t
, uint8_t pos
)
138 SB(t
.m6(), 2, 6, pos
);
142 * Get the completion of this house
144 * @return true if it is, false if it is not
146 inline bool IsHouseCompleted(Tile t
)
148 assert(IsTileType(t
, MP_HOUSE
));
149 return HasBit(t
.m3(), 7);
153 * Mark this house as been completed
157 inline void SetHouseCompleted(Tile t
, bool status
)
159 assert(IsTileType(t
, MP_HOUSE
));
160 SB(t
.m3(), 7, 1, !!status
);
164 * House Construction Scheme.
165 * Construction counter, for buildings under construction. Incremented on every
166 * periodic tile processing.
167 * On wraparound, the stage of building in is increased.
168 * GetHouseBuildingStage is taking care of the real stages,
169 * (as the sprite for the next phase of house building)
170 * (Get|Inc)HouseConstructionTick is simply a tick counter between the
175 * Gets the building stage of a house
176 * Since the stage is used for determining what sprite to use,
177 * if the house is complete (and that stage no longer is available),
178 * fool the system by returning the TOWN_HOUSE_COMPLETE (3),
179 * thus showing a beautiful complete house.
180 * @param t the tile of the house to get the building stage of
181 * @pre IsTileType(t, MP_HOUSE)
182 * @return the building stage of the house
184 inline uint8_t GetHouseBuildingStage(Tile t
)
186 assert(IsTileType(t
, MP_HOUSE
));
187 return IsHouseCompleted(t
) ? (uint8_t)TOWN_HOUSE_COMPLETED
: GB(t
.m5(), 3, 2);
191 * Gets the construction stage of a house
192 * @param t the tile of the house to get the construction stage of
193 * @pre IsTileType(t, MP_HOUSE)
194 * @return the construction stage of the house
196 inline uint8_t GetHouseConstructionTick(Tile t
)
198 assert(IsTileType(t
, MP_HOUSE
));
199 return IsHouseCompleted(t
) ? 0 : GB(t
.m5(), 0, 3);
203 * Sets the increment stage of a house
204 * It is working with the whole counter + stage 5 bits, making it
205 * easier to work: the wraparound is automatic.
206 * @param t the tile of the house to increment the construction stage of
207 * @pre IsTileType(t, MP_HOUSE)
209 inline void IncHouseConstructionTick(Tile t
)
211 assert(IsTileType(t
, MP_HOUSE
));
214 if (GB(t
.m5(), 3, 2) == TOWN_HOUSE_COMPLETED
) {
215 /* House is now completed.
216 * Store the year of construction as well, for newgrf house purpose */
217 SetHouseCompleted(t
, true);
222 * Sets the age of the house to zero.
223 * Needs to be called after the house is completed. During construction stages the map space is used otherwise.
224 * @param t the tile of this house
225 * @pre IsTileType(t, MP_HOUSE) && IsHouseCompleted(t)
227 inline void ResetHouseAge(Tile t
)
229 assert(IsTileType(t
, MP_HOUSE
) && IsHouseCompleted(t
));
234 * Increments the age of the house.
235 * @param t the tile of this house
236 * @pre IsTileType(t, MP_HOUSE)
238 inline void IncrementHouseAge(Tile t
)
240 assert(IsTileType(t
, MP_HOUSE
));
241 if (IsHouseCompleted(t
) && t
.m5() < 0xFF) t
.m5()++;
245 * Get the age of the house
246 * @param t the tile of this house
247 * @pre IsTileType(t, MP_HOUSE)
250 inline TimerGameCalendar::Year
GetHouseAge(Tile t
)
252 assert(IsTileType(t
, MP_HOUSE
));
253 return IsHouseCompleted(t
) ? t
.m5() : 0;
257 * Set the random bits for this house.
258 * This is required for newgrf house
259 * @param t the tile of this house
260 * @param random the new random bits
261 * @pre IsTileType(t, MP_HOUSE)
263 inline void SetHouseRandomBits(Tile t
, uint8_t random
)
265 assert(IsTileType(t
, MP_HOUSE
));
270 * Get the random bits for this house.
271 * This is required for newgrf house
272 * @param t the tile of this house
273 * @pre IsTileType(t, MP_HOUSE)
274 * @return random bits
276 inline uint8_t GetHouseRandomBits(Tile t
)
278 assert(IsTileType(t
, MP_HOUSE
));
283 * Set the activated triggers bits for this house.
284 * This is required for newgrf house
285 * @param t the tile of this house
286 * @param triggers the activated triggers
287 * @pre IsTileType(t, MP_HOUSE)
289 inline void SetHouseTriggers(Tile t
, uint8_t triggers
)
291 assert(IsTileType(t
, MP_HOUSE
));
292 SB(t
.m3(), 0, 5, triggers
);
296 * Get the already activated triggers bits for this house.
297 * This is required for newgrf house
298 * @param t the tile of this house
299 * @pre IsTileType(t, MP_HOUSE)
302 inline uint8_t GetHouseTriggers(Tile t
)
304 assert(IsTileType(t
, MP_HOUSE
));
305 return GB(t
.m3(), 0, 5);
309 * Get the amount of time remaining before the tile loop processes this tile.
310 * @param t the house tile
311 * @pre IsTileType(t, MP_HOUSE)
312 * @return time remaining
314 inline uint8_t GetHouseProcessingTime(Tile t
)
316 assert(IsTileType(t
, MP_HOUSE
));
317 return GB(t
.m6(), 2, 6);
321 * Set the amount of time remaining before the tile loop processes this tile.
322 * @param t the house tile
323 * @param time the time to be set
324 * @pre IsTileType(t, MP_HOUSE)
326 inline void SetHouseProcessingTime(Tile t
, uint8_t time
)
328 assert(IsTileType(t
, MP_HOUSE
));
329 SB(t
.m6(), 2, 6, time
);
333 * Decrease the amount of time remaining before the tile loop processes this tile.
334 * @param t the house tile
335 * @pre IsTileType(t, MP_HOUSE)
337 inline void DecHouseProcessingTime(Tile t
)
339 assert(IsTileType(t
, MP_HOUSE
));
344 * Make the tile a house.
345 * @param t tile index
346 * @param tid Town index
347 * @param counter of construction step
348 * @param stage of construction (used for drawing)
349 * @param type of house. Index into house specs array
350 * @param random_bits required for newgrf houses
351 * @pre IsTileType(t, MP_CLEAR)
353 inline void MakeHouseTile(Tile t
, TownID tid
, uint8_t counter
, uint8_t stage
, HouseID type
, uint8_t random_bits
)
355 assert(IsTileType(t
, MP_CLEAR
));
357 SetTileType(t
, MP_HOUSE
);
358 t
.m1() = random_bits
;
361 SetHouseType(t
, type
);
362 SetHouseCompleted(t
, stage
== TOWN_HOUSE_COMPLETED
);
363 t
.m5() = IsHouseCompleted(t
) ? 0 : (stage
<< 3 | counter
);
364 SetAnimationFrame(t
, 0);
365 SetHouseProcessingTime(t
, HouseSpec::Get(type
)->processing_time
);
368 #endif /* TOWN_MAP_H */