Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / town_map.h
blob248b1bd3233611f8487601ef532f7c06484bdf69
1 /*
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/>.
6 */
8 /** @file town_map.h Accessors for towns */
10 #ifndef TOWN_MAP_H
11 #define TOWN_MAP_H
13 #include "road_map.h"
14 #include "house.h"
16 /**
17 * Get the index of which town this house/street is attached to.
18 * @param t the tile
19 * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) but not a road depot
20 * @return TownID
22 static inline TownID GetTownIndex(TileIndex t)
24 assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
25 return _m[t].m2;
28 /**
29 * Set the town index for a road or house tile.
30 * @param t the tile
31 * @param index the index of the town
32 * @pre IsTileType(t, MP_HOUSE) or IsTileType(t, MP_ROAD) but not a road depot
34 static inline void SetTownIndex(TileIndex t, TownID index)
36 assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
37 _m[t].m2 = index;
40 /**
41 * Get the type of this house, which is an index into the house spec array
42 * without doing any NewGRF related translations.
43 * @param t the tile
44 * @pre IsTileType(t, MP_HOUSE)
45 * @return house type
47 static inline HouseID GetCleanHouseType(TileIndex t)
49 assert(IsTileType(t, MP_HOUSE));
50 return _m[t].m4 | (GB(_m[t].m3, 6, 1) << 8);
53 /**
54 * Get the type of this house, which is an index into the house spec array
55 * @param t the tile
56 * @pre IsTileType(t, MP_HOUSE)
57 * @return house type
59 static inline HouseID GetHouseType(TileIndex t)
61 return GetTranslatedHouseID(GetCleanHouseType(t));
64 /**
65 * Set the house type.
66 * @param t the tile
67 * @param house_id the new house type
68 * @pre IsTileType(t, MP_HOUSE)
70 static inline void SetHouseType(TileIndex t, HouseID house_id)
72 assert(IsTileType(t, MP_HOUSE));
73 _m[t].m4 = GB(house_id, 0, 8);
74 SB(_m[t].m3, 6, 1, GB(house_id, 8, 1));
77 /**
78 * Check if the lift of this animated house has a destination
79 * @param t the tile
80 * @return has destination
82 static inline bool LiftHasDestination(TileIndex t)
84 return HasBit(_me[t].m7, 0);
87 /**
88 * Set the new destination of the lift for this animated house, and activate
89 * the LiftHasDestination bit.
90 * @param t the tile
91 * @param dest new destination
93 static inline void SetLiftDestination(TileIndex t, byte dest)
95 SetBit(_me[t].m7, 0);
96 SB(_me[t].m7, 1, 3, dest);
99 /**
100 * Get the current destination for this lift
101 * @param t the tile
102 * @return destination
104 static inline byte GetLiftDestination(TileIndex t)
106 return GB(_me[t].m7, 1, 3);
110 * Stop the lift of this animated house from moving.
111 * Clears the first 4 bits of m7 at once, clearing the LiftHasDestination bit
112 * and the destination.
113 * @param t the tile
115 static inline void HaltLift(TileIndex t)
117 SB(_me[t].m7, 0, 4, 0);
121 * Get the position of the lift on this animated house
122 * @param t the tile
123 * @return position, from 0 to 36
125 static inline byte GetLiftPosition(TileIndex t)
127 return GB(_me[t].m6, 2, 6);
131 * Set the position of the lift on this animated house
132 * @param t the tile
133 * @param pos position, from 0 to 36
135 static inline void SetLiftPosition(TileIndex t, byte pos)
137 SB(_me[t].m6, 2, 6, pos);
141 * Get the completion of this house
142 * @param t the tile
143 * @return true if it is, false if it is not
145 static inline bool IsHouseCompleted(TileIndex t)
147 assert(IsTileType(t, MP_HOUSE));
148 return HasBit(_m[t].m3, 7);
152 * Mark this house as been completed
153 * @param t the tile
154 * @param status
156 static inline void SetHouseCompleted(TileIndex t, bool status)
158 assert(IsTileType(t, MP_HOUSE));
159 SB(_m[t].m3, 7, 1, !!status);
163 * House Construction Scheme.
164 * Construction counter, for buildings under construction. Incremented on every
165 * periodic tile processing.
166 * On wraparound, the stage of building in is increased.
167 * GetHouseBuildingStage is taking care of the real stages,
168 * (as the sprite for the next phase of house building)
169 * (Get|Inc)HouseConstructionTick is simply a tick counter between the
170 * different stages
174 * Gets the building stage of a house
175 * Since the stage is used for determining what sprite to use,
176 * if the house is complete (and that stage no longer is available),
177 * fool the system by returning the TOWN_HOUSE_COMPLETE (3),
178 * thus showing a beautiful complete house.
179 * @param t the tile of the house to get the building stage of
180 * @pre IsTileType(t, MP_HOUSE)
181 * @return the building stage of the house
183 static inline byte GetHouseBuildingStage(TileIndex t)
185 assert(IsTileType(t, MP_HOUSE));
186 return IsHouseCompleted(t) ? (byte)TOWN_HOUSE_COMPLETED : GB(_m[t].m5, 3, 2);
190 * Gets the construction stage of a house
191 * @param t the tile of the house to get the construction stage of
192 * @pre IsTileType(t, MP_HOUSE)
193 * @return the construction stage of the house
195 static inline byte GetHouseConstructionTick(TileIndex t)
197 assert(IsTileType(t, MP_HOUSE));
198 return IsHouseCompleted(t) ? 0 : GB(_m[t].m5, 0, 3);
202 * Sets the increment stage of a house
203 * It is working with the whole counter + stage 5 bits, making it
204 * easier to work: the wraparound is automatic.
205 * @param t the tile of the house to increment the construction stage of
206 * @pre IsTileType(t, MP_HOUSE)
208 static inline void IncHouseConstructionTick(TileIndex t)
210 assert(IsTileType(t, MP_HOUSE));
211 AB(_m[t].m5, 0, 5, 1);
213 if (GB(_m[t].m5, 3, 2) == TOWN_HOUSE_COMPLETED) {
214 /* House is now completed.
215 * Store the year of construction as well, for newgrf house purpose */
216 SetHouseCompleted(t, true);
221 * Sets the age of the house to zero.
222 * Needs to be called after the house is completed. During construction stages the map space is used otherwise.
223 * @param t the tile of this house
224 * @pre IsTileType(t, MP_HOUSE) && IsHouseCompleted(t)
226 static inline void ResetHouseAge(TileIndex t)
228 assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
229 _m[t].m5 = 0;
233 * Increments the age of the house.
234 * @param t the tile of this house
235 * @pre IsTileType(t, MP_HOUSE)
237 static inline void IncrementHouseAge(TileIndex t)
239 assert(IsTileType(t, MP_HOUSE));
240 if (IsHouseCompleted(t) && _m[t].m5 < 0xFF) _m[t].m5++;
244 * Get the age of the house
245 * @param t the tile of this house
246 * @pre IsTileType(t, MP_HOUSE)
247 * @return year
249 static inline Year GetHouseAge(TileIndex t)
251 assert(IsTileType(t, MP_HOUSE));
252 return IsHouseCompleted(t) ? _m[t].m5 : 0;
256 * Set the random bits for this house.
257 * This is required for newgrf house
258 * @param t the tile of this house
259 * @param random the new random bits
260 * @pre IsTileType(t, MP_HOUSE)
262 static inline void SetHouseRandomBits(TileIndex t, byte random)
264 assert(IsTileType(t, MP_HOUSE));
265 _m[t].m1 = random;
269 * Get the random bits for this house.
270 * This is required for newgrf house
271 * @param t the tile of this house
272 * @pre IsTileType(t, MP_HOUSE)
273 * @return random bits
275 static inline byte GetHouseRandomBits(TileIndex t)
277 assert(IsTileType(t, MP_HOUSE));
278 return _m[t].m1;
282 * Set the activated triggers bits for this house.
283 * This is required for newgrf house
284 * @param t the tile of this house
285 * @param triggers the activated triggers
286 * @pre IsTileType(t, MP_HOUSE)
288 static inline void SetHouseTriggers(TileIndex t, byte triggers)
290 assert(IsTileType(t, MP_HOUSE));
291 SB(_m[t].m3, 0, 5, triggers);
295 * Get the already activated triggers bits for this house.
296 * This is required for newgrf house
297 * @param t the tile of this house
298 * @pre IsTileType(t, MP_HOUSE)
299 * @return triggers
301 static inline byte GetHouseTriggers(TileIndex t)
303 assert(IsTileType(t, MP_HOUSE));
304 return GB(_m[t].m3, 0, 5);
308 * Get the amount of time remaining before the tile loop processes this tile.
309 * @param t the house tile
310 * @pre IsTileType(t, MP_HOUSE)
311 * @return time remaining
313 static inline byte GetHouseProcessingTime(TileIndex t)
315 assert(IsTileType(t, MP_HOUSE));
316 return GB(_me[t].m6, 2, 6);
320 * Set the amount of time remaining before the tile loop processes this tile.
321 * @param t the house tile
322 * @param time the time to be set
323 * @pre IsTileType(t, MP_HOUSE)
325 static inline void SetHouseProcessingTime(TileIndex t, byte time)
327 assert(IsTileType(t, MP_HOUSE));
328 SB(_me[t].m6, 2, 6, time);
332 * Decrease the amount of time remaining before the tile loop processes this tile.
333 * @param t the house tile
334 * @pre IsTileType(t, MP_HOUSE)
336 static inline void DecHouseProcessingTime(TileIndex t)
338 assert(IsTileType(t, MP_HOUSE));
339 _me[t].m6 -= 1 << 2;
343 * Make the tile a house.
344 * @param t tile index
345 * @param tid Town index
346 * @param counter of construction step
347 * @param stage of construction (used for drawing)
348 * @param type of house. Index into house specs array
349 * @param random_bits required for newgrf houses
350 * @pre IsTileType(t, MP_CLEAR)
352 static inline void MakeHouseTile(TileIndex t, TownID tid, byte counter, byte stage, HouseID type, byte random_bits)
354 assert(IsTileType(t, MP_CLEAR));
356 SetTileType(t, MP_HOUSE);
357 _m[t].m1 = random_bits;
358 _m[t].m2 = tid;
359 _m[t].m3 = 0;
360 SetHouseType(t, type);
361 SetHouseCompleted(t, stage == TOWN_HOUSE_COMPLETED);
362 _m[t].m5 = IsHouseCompleted(t) ? 0 : (stage << 3 | counter);
363 SetAnimationFrame(t, 0);
364 SetHouseProcessingTime(t, HouseSpec::Get(type)->processing_time);
367 #endif /* TOWN_MAP_H */