Merge branch 'development' into master_joker
[openttd-joker.git] / src / town_map.h
blobbf0ebeb55d36869a31981e1e8f0295762aca5558
1 /* $Id: town_map.h 24900 2013-01-08 22:46:42Z planetmaker $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file town_map.h Accessors for towns */
12 #ifndef TOWN_MAP_H
13 #define TOWN_MAP_H
15 #include "road_map.h"
16 #include "house.h"
18 /**
19 * Get the index of which town this house/street is attached to.
20 * @param t the tile
21 * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) but not a road depot
22 * @return TownID
24 static inline TownID GetTownIndex(TileIndex t)
26 assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
27 return _m[t].m2;
30 /**
31 * Set the town index for a road or house tile.
32 * @param t the tile
33 * @param index the index of the town
34 * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) but not a road depot
36 static inline void SetTownIndex(TileIndex t, TownID index)
38 assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
39 _m[t].m2 = index;
42 /**
43 * Get the type of this house, which is an index into the house spec array
44 * without doing any NewGRF related translations.
45 * @param t the tile
46 * @pre IsTileType(t, MP_HOUSE)
47 * @return house type
49 static inline HouseID GetCleanHouseType(TileIndex t)
51 assert(IsTileType(t, MP_HOUSE));
52 return _m[t].m4 | (GB(_m[t].m3, 6, 1) << 8);
55 /**
56 * Get the type of this house, which is an index into the house spec array
57 * @param t the tile
58 * @pre IsTileType(t, MP_HOUSE)
59 * @return house type
61 static inline HouseID GetHouseType(TileIndex t)
63 return GetTranslatedHouseID(GetCleanHouseType(t));
66 /**
67 * Set the house type.
68 * @param t the tile
69 * @param house_id the new house type
70 * @pre IsTileType(t, MP_HOUSE)
72 static inline void SetHouseType(TileIndex t, HouseID house_id)
74 assert(IsTileType(t, MP_HOUSE));
75 _m[t].m4 = GB(house_id, 0, 8);
76 SB(_m[t].m3, 6, 1, GB(house_id, 8, 1));
79 /**
80 * Check if the lift of this animated house has a destination
81 * @param t the tile
82 * @return has destination
84 static inline bool LiftHasDestination(TileIndex t)
86 return HasBit(_me[t].m7, 0);
89 /**
90 * Set the new destination of the lift for this animated house, and activate
91 * the LiftHasDestination bit.
92 * @param t the tile
93 * @param dest new destination
95 static inline void SetLiftDestination(TileIndex t, byte dest)
97 SetBit(_me[t].m7, 0);
98 SB(_me[t].m7, 1, 3, dest);
102 * Get the current destination for this lift
103 * @param t the tile
104 * @return destination
106 static inline byte GetLiftDestination(TileIndex t)
108 return GB(_me[t].m7, 1, 3);
112 * Stop the lift of this animated house from moving.
113 * Clears the first 4 bits of m7 at once, clearing the LiftHasDestination bit
114 * and the destination.
115 * @param t the tile
117 static inline void HaltLift(TileIndex t)
119 SB(_me[t].m7, 0, 4, 0);
123 * Get the position of the lift on this animated house
124 * @param t the tile
125 * @return position, from 0 to 36
127 static inline byte GetLiftPosition(TileIndex t)
129 return GB(_me[t].m6, 2, 6);
133 * Set the position of the lift on this animated house
134 * @param t the tile
135 * @param pos position, from 0 to 36
137 static inline void SetLiftPosition(TileIndex t, byte pos)
139 SB(_me[t].m6, 2, 6, pos);
143 * Get the completion of this house
144 * @param t the tile
145 * @return true if it is, false if it is not
147 static inline bool IsHouseCompleted(TileIndex t)
149 assert(IsTileType(t, MP_HOUSE));
150 return HasBit(_m[t].m3, 7);
154 * Mark this house as been completed
155 * @param t the tile
156 * @param status
158 static inline void SetHouseCompleted(TileIndex t, bool status)
160 assert(IsTileType(t, MP_HOUSE));
161 SB(_m[t].m3, 7, 1, !!status);
165 * House Construction Scheme.
166 * Construction counter, for buildings under construction. Incremented on every
167 * periodic tile processing.
168 * On wraparound, the stage of building in is increased.
169 * GetHouseBuildingStage is taking care of the real stages,
170 * (as the sprite for the next phase of house building)
171 * (Get|Inc)HouseConstructionTick is simply a tick counter between the
172 * different stages
176 * Gets the building stage of a house
177 * Since the stage is used for determining what sprite to use,
178 * if the house is complete (and that stage no longer is available),
179 * fool the system by returning the TOWN_HOUSE_COMPLETE (3),
180 * thus showing a beautiful complete house.
181 * @param t the tile of the house to get the building stage of
182 * @pre IsTileType(t, MP_HOUSE)
183 * @return the building stage of the house
185 static inline byte GetHouseBuildingStage(TileIndex t)
187 assert(IsTileType(t, MP_HOUSE));
188 return IsHouseCompleted(t) ? (byte)TOWN_HOUSE_COMPLETED : GB(_m[t].m5, 3, 2);
192 * Gets the construction stage of a house
193 * @param t the tile of the house to get the construction stage of
194 * @pre IsTileType(t, MP_HOUSE)
195 * @return the construction stage of the house
197 static inline byte GetHouseConstructionTick(TileIndex t)
199 assert(IsTileType(t, MP_HOUSE));
200 return IsHouseCompleted(t) ? 0 : GB(_m[t].m5, 0, 3);
204 * Sets the increment stage of a house
205 * It is working with the whole counter + stage 5 bits, making it
206 * easier to work: the wraparound is automatic.
207 * @param t the tile of the house to increment the construction stage of
208 * @pre IsTileType(t, MP_HOUSE)
210 static inline void IncHouseConstructionTick(TileIndex t)
212 assert(IsTileType(t, MP_HOUSE));
213 AB(_m[t].m5, 0, 5, 1);
215 if (GB(_m[t].m5, 3, 2) == TOWN_HOUSE_COMPLETED) {
216 /* House is now completed.
217 * Store the year of construction as well, for newgrf house purpose */
218 SetHouseCompleted(t, true);
223 * Sets the age of the house to zero.
224 * Needs to be called after the house is completed. During construction stages the map space is used otherwise.
225 * @param t the tile of this house
226 * @pre IsTileType(t, MP_HOUSE) && IsHouseCompleted(t)
228 static inline void ResetHouseAge(TileIndex t)
230 assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
231 _m[t].m5 = 0;
235 * Increments the age of the house.
236 * @param t the tile of this house
237 * @pre IsTileType(t, MP_HOUSE)
239 static inline void IncrementHouseAge(TileIndex t)
241 assert(IsTileType(t, MP_HOUSE));
242 if (IsHouseCompleted(t) && _m[t].m5 < 0xFF) _m[t].m5++;
246 * Get the age of the house
247 * @param t the tile of this house
248 * @pre IsTileType(t, MP_HOUSE)
249 * @return year
251 static inline Year GetHouseAge(TileIndex t)
253 assert(IsTileType(t, MP_HOUSE));
254 return IsHouseCompleted(t) ? _m[t].m5 : 0;
258 * Set the random bits for this house.
259 * This is required for newgrf house
260 * @param t the tile of this house
261 * @param random the new random bits
262 * @pre IsTileType(t, MP_HOUSE)
264 static inline void SetHouseRandomBits(TileIndex t, byte random)
266 assert(IsTileType(t, MP_HOUSE));
267 _m[t].m1 = random;
271 * Get the random bits for this house.
272 * This is required for newgrf house
273 * @param t the tile of this house
274 * @pre IsTileType(t, MP_HOUSE)
275 * @return random bits
277 static inline byte GetHouseRandomBits(TileIndex t)
279 assert(IsTileType(t, MP_HOUSE));
280 return _m[t].m1;
284 * Set the activated triggers bits for this house.
285 * This is required for newgrf house
286 * @param t the tile of this house
287 * @param triggers the activated triggers
288 * @pre IsTileType(t, MP_HOUSE)
290 static inline void SetHouseTriggers(TileIndex t, byte triggers)
292 assert(IsTileType(t, MP_HOUSE));
293 SB(_m[t].m3, 0, 5, triggers);
297 * Get the already activated triggers bits for this house.
298 * This is required for newgrf house
299 * @param t the tile of this house
300 * @pre IsTileType(t, MP_HOUSE)
301 * @return triggers
303 static inline byte GetHouseTriggers(TileIndex t)
305 assert(IsTileType(t, MP_HOUSE));
306 return GB(_m[t].m3, 0, 5);
310 * Get the amount of time remaining before the tile loop processes this tile.
311 * @param t the house tile
312 * @pre IsTileType(t, MP_HOUSE)
313 * @return time remaining
315 static inline byte GetHouseProcessingTime(TileIndex t)
317 assert(IsTileType(t, MP_HOUSE));
318 return GB(_me[t].m6, 2, 6);
322 * Set the amount of time remaining before the tile loop processes this tile.
323 * @param t the house tile
324 * @param time the time to be set
325 * @pre IsTileType(t, MP_HOUSE)
327 static inline void SetHouseProcessingTime(TileIndex t, byte time)
329 assert(IsTileType(t, MP_HOUSE));
330 SB(_me[t].m6, 2, 6, time);
334 * Decrease the amount of time remaining before the tile loop processes this tile.
335 * @param t the house tile
336 * @pre IsTileType(t, MP_HOUSE)
338 static inline void DecHouseProcessingTime(TileIndex t)
340 assert(IsTileType(t, MP_HOUSE));
341 _me[t].m6 -= 1 << 2;
345 * Make the tile a house.
346 * @param t tile index
347 * @param tid Town index
348 * @param counter of construction step
349 * @param stage of construction (used for drawing)
350 * @param type of house. Index into house specs array
351 * @param random_bits required for newgrf houses
352 * @pre IsTileType(t, MP_CLEAR)
354 static inline void MakeHouseTile(TileIndex t, TownID tid, byte counter, byte stage, HouseID type, byte random_bits)
356 assert(IsTileType(t, MP_CLEAR));
358 SetTileType(t, MP_HOUSE);
359 _m[t].m1 = random_bits;
360 _m[t].m2 = tid;
361 _m[t].m3 = 0;
362 SetHouseType(t, type);
363 SetHouseCompleted(t, stage == TOWN_HOUSE_COMPLETED);
364 _m[t].m5 = IsHouseCompleted(t) ? 0 : (stage << 3 | counter);
365 SetAnimationFrame(t, 0);
366 SetHouseProcessingTime(t, HouseSpec::Get(type)->processing_time);
369 #endif /* TOWN_MAP_H */