Translations update
[openttd/fttd.git] / src / newgrf_house.cpp
blob37e30c8db960788be8f681d2085e477f8581c8d6
1 /* $Id$ */
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 newgrf_house.cpp Implementation of NewGRF houses. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "landscape.h"
15 #include "newgrf_house.h"
16 #include "newgrf_spritegroup.h"
17 #include "newgrf_town.h"
18 #include "newgrf_sound.h"
19 #include "company_func.h"
20 #include "company_base.h"
21 #include "town.h"
22 #include "genworld.h"
23 #include "newgrf_animation_base.h"
24 #include "newgrf_cargo.h"
25 #include "station_base.h"
27 static BuildingCounts<uint32> _building_counts;
28 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
30 HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, NUM_HOUSES, INVALID_HOUSE_ID);
32 /**
33 * Constructor of a house scope resolver.
34 * @param grffile GRFFile the resolved SpriteGroup belongs to.
35 * @param house_id House type being queried.
36 * @param tile %Tile containing the house.
37 * @param town %Town containing the house.
38 * @param not_yet_constructed House is still under construction.
39 * @param initial_random_bits Random bits during construction checks.
40 * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback.
42 HouseScopeResolver::HouseScopeResolver (const GRFFile *grffile, HouseID house_id, TileIndex tile, Town *town,
43 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
44 : ScopeResolver(), grffile(grffile)
46 this->house_id = house_id;
47 this->tile = tile;
48 this->town = town;
49 this->not_yet_constructed = not_yet_constructed;
50 this->initial_random_bits = initial_random_bits;
51 this->watched_cargo_triggers = watched_cargo_triggers;
54 /**
55 * Retrieve the grf file associated with a house.
56 * @param house_id House to query.
57 * @return The associated GRF file (may be \c NULL).
59 static const GRFFile *GetHouseSpecGrf(HouseID house_id)
61 const HouseSpec *hs = HouseSpec::Get(house_id);
62 return (hs != NULL) ? hs->grf_prop.grffile : NULL;
65 /**
66 * Construct a resolver for a house.
67 * @param house_id House to query.
68 * @param tile %Tile containing the house.
69 * @param town %Town containing the house.
70 * @param callback Callback ID.
71 * @param param1 First parameter (var 10) of the callback.
72 * @param param2 Second parameter (var 18) of the callback.
73 * @param not_yet_constructed House is still under construction.
74 * @param initial_random_bits Random bits during construction checks.
75 * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback.
77 HouseResolverObject::HouseResolverObject(HouseID house_id, TileIndex tile, Town *town,
78 CallbackID callback, uint32 param1, uint32 param2,
79 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
80 : ResolverObject(GetHouseSpecGrf(house_id), callback, param1, param2),
81 house_scope (this->grffile, house_id, tile, town, not_yet_constructed, initial_random_bits, watched_cargo_triggers),
82 town_scope (this->grffile, town, not_yet_constructed) // Don't access StorePSA if house is not yet constructed.
84 this->root_spritegroup = HouseSpec::Get(house_id)->grf_prop.spritegroup[0];
87 /**
88 * Construct a fake resolver for a house.
89 * @param hs HouseSpec of the house to query.
90 * @param tile %Tile containing the house.
91 * @param town %Town containing the house.
92 * @param callback Callback ID.
93 * @param param1 First parameter (var 10) of the callback.
94 * @param param2 Second parameter (var 18) of the callback.
95 * @param not_yet_constructed House is still under construction.
96 * @param initial_random_bits Random bits during construction checks.
97 * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback.
99 FakeHouseResolverObject::FakeHouseResolverObject (const HouseSpec *hs,
100 CallbackID callback, uint32 param1, uint32 param2)
101 : ResolverObject ((hs != NULL) ? hs->grf_prop.grffile : NULL, callback, param1, param2),
102 house_scope (hs), town_scope()
106 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
108 /* Start from 1 because 0 means that no class has been assigned. */
109 for (int i = 1; i != lengthof(_class_mapping); i++) {
110 HouseClassMapping *map = &_class_mapping[i];
112 if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
114 if (map->class_id == 0 && map->grfid == 0) {
115 map->class_id = grf_class_id;
116 map->grfid = grfid;
117 return (HouseClassID)i;
120 return HOUSE_NO_CLASS;
123 void InitializeBuildingCounts()
125 memset(&_building_counts, 0, sizeof(_building_counts));
127 Town *t;
128 FOR_ALL_TOWNS(t) {
129 memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts));
134 * IncreaseBuildingCount()
135 * Increase the count of a building when it has been added by a town.
136 * @param t The town that the building is being built in
137 * @param house_id The id of the house being added
139 void IncreaseBuildingCount(Town *t, HouseID house_id)
141 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
143 if (!_loaded_newgrf_features.has_newhouses) return;
145 t->cache.building_counts.id_count[house_id]++;
146 _building_counts.id_count[house_id]++;
148 if (class_id == HOUSE_NO_CLASS) return;
150 t->cache.building_counts.class_count[class_id]++;
151 _building_counts.class_count[class_id]++;
155 * DecreaseBuildingCount()
156 * Decrease the number of a building when it is deleted.
157 * @param t The town that the building was built in
158 * @param house_id The id of the house being removed
160 void DecreaseBuildingCount(Town *t, HouseID house_id)
162 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
164 if (!_loaded_newgrf_features.has_newhouses) return;
166 if (t->cache.building_counts.id_count[house_id] > 0) t->cache.building_counts.id_count[house_id]--;
167 if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--;
169 if (class_id == HOUSE_NO_CLASS) return;
171 if (t->cache.building_counts.class_count[class_id] > 0) t->cache.building_counts.class_count[class_id]--;
172 if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
175 /* virtual */ uint32 HouseScopeResolver::GetRandomBits() const
177 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
178 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsHouseTile(this->tile)));
179 return this->not_yet_constructed ? this->initial_random_bits : GetHouseRandomBits(this->tile);
182 /* virtual */ uint32 HouseScopeResolver::GetTriggers() const
184 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
185 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsHouseTile(this->tile)));
186 return this->not_yet_constructed ? 0 : GetHouseTriggers(this->tile);
189 /* virtual */ void HouseScopeResolver::SetTriggers(int triggers) const
191 assert(!this->not_yet_constructed && IsValidTile(this->tile) && IsHouseTile(this->tile));
192 SetHouseTriggers(this->tile, triggers);
195 static uint32 GetNumHouses(HouseID house_id, const Town *town)
197 uint8 map_id_count, town_id_count, map_class_count, town_class_count;
198 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
200 map_id_count = ClampU(_building_counts.id_count[house_id], 0, 255);
201 map_class_count = ClampU(_building_counts.class_count[class_id], 0, 255);
202 town_id_count = ClampU(town->cache.building_counts.id_count[house_id], 0, 255);
203 town_class_count = ClampU(town->cache.building_counts.class_count[class_id], 0, 255);
205 return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
209 * Get information about a nearby tile.
210 * @param parameter from callback. It's in fact a pair of coordinates
211 * @param tile TileIndex from which the callback was initiated
212 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
213 * @return a construction of bits obeying the newgrf format
215 static uint32 GetNearbyTileInformation(byte parameter, TileIndex tile, bool grf_version8)
217 tile = GetNearbyTile(parameter, tile);
218 return GetNearbyTileInformation(tile, grf_version8);
222 * This function will activate a search around a central tile, looking for some houses
223 * that fit the requested characteristics
224 * @param parameter that is given by the callback.
225 * bits 0..6 radius of the search
226 * bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID
227 * @param tile TileIndex from which to start the search
228 * @param house the HouseID that is associated to the house, the callback is called for
229 * @return the Manhattan distance from the center tile, if any, and 0 if failure
231 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
233 enum SearchType {
234 SEARCH_BY_ID,
235 SEARCH_BY_CLASS,
236 SEARCH_BY_GRFID,
237 SEARCH_BY_END,
240 uint8 searchtype = GB(parameter, 6, 2);
241 uint8 searchradius = GB(parameter, 0, 6);
243 if (searchtype >= SEARCH_BY_END) return 0; // do not run on ill-defined code
244 if (searchradius < 1) return 0; // do not use a too low radius
246 const HouseSpec *orig_hs = HouseSpec::Get(house);
247 TileIndex orig_north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
249 CircularTileIterator iter (tile, 2 * searchradius + 1);
250 for (TileIndex t = iter; t != INVALID_TILE; t = ++iter) {
251 if (!IsHouseTile(t)) continue;
253 HouseID house = GetHouseType(t);
254 const HouseSpec *hs = HouseSpec::Get(house);
256 /* house must be from the same grf file */
257 if (hs->grf_prop.grffile == NULL) continue;
258 if (hs->grf_prop.grffile->grfid != orig_hs->grf_prop.grffile->grfid) continue;
260 TileIndex north = t + GetHouseNorthPart(house); // modifies 'house'!
261 /* always ignore origin house */
262 if (north == orig_north_tile) continue;
264 bool match;
265 switch (searchtype) {
266 case SEARCH_BY_ID:
267 match = (hs->grf_prop.local_id == orig_hs->grf_prop.local_id); // same local id as the one requested
268 break;
270 case SEARCH_BY_CLASS:
271 match = (hs->class_id == orig_hs->class_id); // same classid as the one requested
272 break;
274 case SEARCH_BY_GRFID:
275 match = true; // from the same grf
276 break;
278 default: NOT_REACHED();
281 if (match) return DistanceManhattan (t, tile);
283 return 0;
287 * @note Used by the resolver to get values for feature 07 deterministic spritegroups.
289 /* virtual */ uint32 HouseScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
291 switch (variable) {
292 /* Construction stage. */
293 case 0x40: return (IsHouseTile(this->tile) ? GetHouseBuildingStage(this->tile) : 0) | TileHash2Bit(TileX(this->tile), TileY(this->tile)) << 2;
295 /* Building age. */
296 case 0x41: return IsHouseTile(this->tile) ? GetHouseAge(this->tile) : 0;
298 /* Town zone */
299 case 0x42: return GetTownRadiusGroup(this->town, this->tile);
301 /* Terrain type */
302 case 0x43: return GetTerrainType(this->tile);
304 /* Number of this type of building on the map. */
305 case 0x44: return GetNumHouses(this->house_id, this->town);
307 /* Whether the town is being created or just expanded. */
308 case 0x45: return _generating_world ? 1 : 0;
310 /* Current animation frame. */
311 case 0x46: return IsHouseTile(this->tile) ? GetAnimationFrame(this->tile) : 0;
313 /* Position of the house */
314 case 0x47: return TileY(this->tile) << 16 | TileX(this->tile);
316 /* Building counts for old houses with id = parameter. */
317 case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, this->town) : 0;
319 /* Building counts for new houses with id = parameter. */
320 case 0x61: {
321 const HouseSpec *hs = HouseSpec::Get(this->house_id);
322 if (hs->grf_prop.grffile == NULL) return 0;
324 HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
325 return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, this->town);
328 /* Land info for nearby tiles. */
329 case 0x62: return GetNearbyTileInformation (parameter, this->tile, this->grffile->grf_version >= 8);
331 /* Current animation frame of nearby house tiles */
332 case 0x63: {
333 TileIndex testtile = GetNearbyTile(parameter, this->tile);
334 return IsHouseTile(testtile) ? GetAnimationFrame(testtile) : 0;
337 /* Cargo acceptance history of nearby stations */
338 case 0x64: {
339 CargoID cid = GetCargoTranslation (parameter, this->grffile);
340 if (cid == CT_INVALID) return 0;
342 /* Extract tile offset. */
343 int8 x_offs = GB(GetRegister(0x100), 0, 8);
344 int8 y_offs = GB(GetRegister(0x100), 8, 8);
345 TileIndex testtile = TILE_MASK(this->tile + TileDiffXY(x_offs, y_offs));
347 StationFinder stations(TileArea(testtile, 1, 1));
348 const StationList *sl = stations.GetStations();
350 /* Collect acceptance stats. */
351 uint32 res = 0;
352 for (Station * const * st_iter = sl->Begin(); st_iter != sl->End(); st_iter++) {
353 const Station *st = *st_iter;
354 if (HasBit(st->goods[cid].status, GoodsEntry::GES_EVER_ACCEPTED)) SetBit(res, 0);
355 if (HasBit(st->goods[cid].status, GoodsEntry::GES_LAST_MONTH)) SetBit(res, 1);
356 if (HasBit(st->goods[cid].status, GoodsEntry::GES_CURRENT_MONTH)) SetBit(res, 2);
357 if (HasBit(st->goods[cid].status, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(res, 3);
360 /* Cargo triggered CB 148? */
361 if (HasBit(this->watched_cargo_triggers, cid)) SetBit(res, 4);
363 return res;
366 /* Distance test for some house types */
367 case 0x65: return GetDistanceFromNearbyHouse(parameter, this->tile, this->house_id);
369 /* Class and ID of nearby house tile */
370 case 0x66: {
371 TileIndex testtile = GetNearbyTile(parameter, this->tile);
372 if (!IsHouseTile(testtile)) return 0xFFFFFFFF;
373 HouseID nearby_house_id = GetHouseType(testtile);
374 HouseSpec *hs = HouseSpec::Get(nearby_house_id);
375 /* Information about the grf local classid if the house has a class */
376 uint houseclass = 0;
377 if (hs->class_id != HOUSE_NO_CLASS) {
378 houseclass = (hs->grf_prop.grffile == this->grffile ? 1 : 2) << 8;
379 houseclass |= _class_mapping[hs->class_id].class_id;
381 /* old house type or grf-local houseid */
382 uint local_houseid = 0;
383 if (nearby_house_id < NEW_HOUSE_OFFSET) {
384 local_houseid = nearby_house_id;
385 } else {
386 local_houseid = (hs->grf_prop.grffile == this->grffile ? 1 : 2) << 8;
387 local_houseid |= hs->grf_prop.local_id;
389 return houseclass << 16 | local_houseid;
392 /* GRFID of nearby house tile */
393 case 0x67: {
394 TileIndex testtile = GetNearbyTile(parameter, this->tile);
395 if (!IsHouseTile(testtile)) return 0xFFFFFFFF;
396 HouseID house_id = GetHouseType(testtile);
397 if (house_id < NEW_HOUSE_OFFSET) return 0;
398 /* Checking the grffile information via HouseSpec doesn't work
399 * in case the newgrf was removed. */
400 return _house_mngr.GetGRFID(house_id);
404 DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
406 *available = false;
407 return UINT_MAX;
412 * @note Used by the resolver to get values for feature 07 deterministic spritegroups.
414 /* virtual */ uint32 FakeHouseScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
416 switch (variable) {
417 /* Construction stage. */
418 case 0x40: return TOWN_HOUSE_COMPLETED;
420 /* Building age. */
421 case 0x41: return 0;
423 /* Town zone */
424 case 0x42: return FIND_FIRST_BIT(this->hs->building_availability & HZ_ZONALL); // first available
426 /* Terrain type */
427 case 0x43: return _settings_game.game_creation.landscape == LT_ARCTIC && (this->hs->building_availability & (HZ_SUBARTC_ABOVE | HZ_SUBARTC_BELOW)) == HZ_SUBARTC_ABOVE ? 4 : 0;
429 /* Number of this type of building on the map. */
430 case 0x44: return 0;
432 /* Whether the town is being created or just expanded. */
433 case 0x45: return 0;
435 /* Current animation frame. */
436 case 0x46: return 0;
438 /* Position of the house */
439 case 0x47: return 0xFFFFFFFF;
441 /* Building counts for old houses with id = parameter. */
442 case 0x60: return 0;
444 /* Building counts for new houses with id = parameter. */
445 case 0x61: return 0;
447 /* Land info for nearby tiles. */
448 case 0x62: return 0;
450 /* Current animation frame of nearby house tiles */
451 case 0x63: return 0;
453 /* Cargo acceptance history of nearby stations */
454 case 0x64: return 0;
456 /* Distance test for some house types */
457 case 0x65: return 0;
459 /* Class and ID of nearby house tile */
460 case 0x66: return 0xFFFFFFFF;
462 /* GRFID of nearby house tile */
463 case 0x67: return 0xFFFFFFFF;
466 DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
468 *available = false;
469 return UINT_MAX;
472 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile,
473 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
475 assert(IsValidTile(tile) && (not_yet_constructed || IsHouseTile(tile)));
477 HouseResolverObject object(house_id, tile, town, callback, param1, param2,
478 not_yet_constructed, initial_random_bits, watched_cargo_triggers);
479 return SpriteGroup::CallbackResult (object.Resolve());
482 static inline const SpriteGroup *FakeHouseResolve (HouseID house_id,
483 CallbackID callback = CBID_NO_CALLBACK, uint32 param1 = 0, uint32 param2 = 0)
485 const HouseSpec *hs = HouseSpec::Get (house_id);
486 FakeHouseResolverObject object (hs, callback, param1, param2);
487 return SpriteGroup::Resolve (hs->grf_prop.spritegroup[0], object);
490 uint16 GetHouseCallback (CallbackID callback, uint32 param1, uint32 param2, HouseID house_id)
492 return SpriteGroup::CallbackResult (FakeHouseResolve (house_id, callback, param1, param2));
495 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
497 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
499 const HouseSpec *hs = HouseSpec::Get(house_id);
500 PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
501 if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
502 uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
503 if (callback != CALLBACK_FAILED) {
504 /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
505 palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
509 SpriteID image = dts->ground.sprite;
510 PaletteID pal = dts->ground.pal;
512 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
513 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
515 if (GB(image, 0, SPRITE_WIDTH) != 0) {
516 DrawGroundSprite (ti, image, GroundSpritePaletteTransform(image, pal, palette));
519 DrawNewGRFTileSeq (ti, dts->seq, TO_HOUSES, stage, palette);
522 static void DrawTileLayoutInGUI (BlitArea *dpi, int x, int y,
523 const TileLayoutSpriteGroup *group, HouseID house_id, bool ground)
525 byte stage = TOWN_HOUSE_COMPLETED;
526 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
528 const HouseSpec *hs = HouseSpec::Get(house_id);
529 PaletteID palette = hs->random_colour[0] + PALETTE_RECOLOUR_START;
530 if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
531 uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id);
532 if (callback != CALLBACK_FAILED) {
533 /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
534 palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
538 if (ground) {
539 PalSpriteID image = dts->ground;
540 if (HasBit(image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) image.sprite += stage;
541 if (HasBit(image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) image.pal += stage;
543 if (GB(image.sprite, 0, SPRITE_WIDTH) != 0) {
544 DrawSprite (dpi, image.sprite, GroundSpritePaletteTransform (image.sprite, image.pal, palette), x, y);
546 } else {
547 DrawNewGRFTileSeqInGUI (dpi, x, y, dts->seq, stage, palette);
551 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
553 const HouseSpec *hs = HouseSpec::Get(house_id);
555 if (ti->tileh != SLOPE_FLAT) {
556 bool draw_old_one = true;
557 if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
558 /* Called to determine the type (if any) of foundation to draw for the house tile */
559 uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
560 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res);
563 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
566 HouseResolverObject object(house_id, ti->tile, Town::GetByTile(ti->tile));
568 const SpriteGroup *group = object.Resolve();
569 if (group != NULL && group->type == SGT_TILELAYOUT) {
570 /* Limit the building stage to the number of stages supplied. */
571 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
572 byte stage = GetHouseBuildingStage(ti->tile);
573 DrawTileLayout(ti, tlgroup, stage, house_id);
577 void DrawNewHouseTileInGUI (BlitArea *dpi, int x, int y, HouseID house_id, bool ground)
579 const SpriteGroup *group = FakeHouseResolve (house_id);
580 if (group != NULL && group->type == SGT_TILELAYOUT) {
581 DrawTileLayoutInGUI (dpi, x, y, (const TileLayoutSpriteGroup*)group, house_id, ground);
585 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
586 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, Town *town, TileIndex tile, uint32 extra_data)
588 return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data);
591 /** Helper class for animation control. */
592 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, uint32, GetSimpleHouseCallback> {
593 static const CallbackID cb_animation_speed = CBID_HOUSE_ANIMATION_SPEED;
594 static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
596 static const HouseCallbackMask cbm_animation_speed = CBM_HOUSE_ANIMATION_SPEED;
597 static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
600 void AnimateNewHouseTile(TileIndex tile)
602 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
603 if (hs == NULL) return;
605 HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
608 void AnimateNewHouseConstruction(TileIndex tile)
610 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
612 if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
613 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
617 bool CanDeleteHouse(TileIndex tile)
619 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
621 /* Humans are always allowed to remove buildings, as is water and disasters and
622 * anyone using the scenario editor. */
623 if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE || _game_mode == GM_EDITOR || _generating_world) {
624 return true;
627 if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
628 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
629 return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res));
630 } else {
631 return !(hs->extra_flags & BUILDING_IS_PROTECTED);
635 static void AnimationControl(TileIndex tile, uint16 random_bits)
637 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
639 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
640 uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
641 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
645 bool NewHouseTileLoop(TileIndex tile)
647 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
649 if (GetHouseProcessingTime(tile) > 0) {
650 DecHouseProcessingTime(tile);
651 return true;
654 TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
655 if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
657 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
658 /* If this house is marked as having a synchronised callback, all the
659 * tiles will have the callback called at once, rather than when the
660 * tile loop reaches them. This should only be enabled for the northern
661 * tile, or strange things will happen (here, and in TTDPatch). */
662 if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
663 uint16 random = GB(Random(), 0, 16);
665 if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random);
666 if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TILE_ADDXY(tile, 0, 1), random);
667 if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TILE_ADDXY(tile, 1, 0), random);
668 if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
669 } else {
670 AnimationControl(tile, 0);
674 /* Check callback 21, which determines if a house should be destroyed. */
675 if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
676 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
677 if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) {
678 ClearTownHouse(Town::GetByTile(tile), tile);
679 return false;
683 SetHouseProcessingTime(tile, hs->processing_time);
684 MarkTileDirtyByTile(tile);
685 return true;
688 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
690 /* We can't trigger a non-existent building... */
691 assert(IsHouseTile(tile));
693 HouseID hid = GetHouseType(tile);
694 HouseSpec *hs = HouseSpec::Get(hid);
696 if (hs->grf_prop.spritegroup[0] == NULL) return;
698 HouseResolverObject object(hid, tile, Town::GetByTile(tile), CBID_RANDOM_TRIGGER);
699 object.trigger = trigger;
701 const SpriteGroup *group = object.Resolve();
702 if (group == NULL) return;
704 byte new_random_bits = Random();
705 byte random_bits = GetHouseRandomBits(tile);
706 uint32 reseed = object.GetReseedSum(); // The scope only affects triggers, not the reseeding
707 random_bits &= ~reseed;
708 random_bits |= (first ? new_random_bits : base_random) & reseed;
709 SetHouseRandomBits(tile, random_bits);
711 switch (trigger) {
712 case HOUSE_TRIGGER_TILE_LOOP:
713 /* Random value already set. */
714 break;
716 case HOUSE_TRIGGER_TILE_LOOP_TOP:
717 if (!first) {
718 /* The top tile is marked dirty by the usual TileLoop */
719 MarkTileDirtyByTile(tile);
720 break;
722 /* Random value of first tile already set. */
723 if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
724 if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
725 if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
726 break;
730 void TriggerHouse(TileIndex t, HouseTrigger trigger)
732 DoTriggerHouse(t, trigger, 0, true);
736 * Run the watched cargo accepted callback for a single house tile.
737 * @param tile The house tile.
738 * @param origin The triggering tile.
739 * @param trigger_cargoes Cargo types that triggered the callback.
740 * @param random Random bits.
742 void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, uint32 trigger_cargoes, uint16 random)
744 CoordDiff diff = TileCoordDiff(origin, tile);
745 uint32 cb_info = random << 16 | (uint8)diff.y << 8 | (uint8)diff.x;
746 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_WATCHED_CARGO_ACCEPTED, HouseSpec::Get(GetHouseType(tile)), Town::GetByTile(tile), tile, 0, cb_info, trigger_cargoes);
750 * Run watched cargo accepted callback for a house.
751 * @param tile House tile.
752 * @param trigger_cargoes Triggering cargo types.
753 * @pre IsHouseTile(t)
755 void WatchedCargoCallback(TileIndex tile, uint32 trigger_cargoes)
757 assert(IsHouseTile(tile));
758 HouseID id = GetHouseType(tile);
759 const HouseSpec *hs = HouseSpec::Get(id);
761 trigger_cargoes &= hs->watched_cargoes;
762 /* None of the trigger cargoes is watched? */
763 if (trigger_cargoes == 0) return;
765 /* Same random value for all tiles of a multi-tile house. */
766 uint16 r = Random();
768 /* Do the callback, start at northern tile. */
769 TileIndex north = tile + GetHouseNorthPart(id);
770 hs = HouseSpec::Get(id);
772 DoWatchedCargoCallback(north, tile, trigger_cargoes, r);
773 if (hs->building_flags & BUILDING_2_TILES_Y) DoWatchedCargoCallback(TILE_ADDXY(north, 0, 1), tile, trigger_cargoes, r);
774 if (hs->building_flags & BUILDING_2_TILES_X) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 0), tile, trigger_cargoes, r);
775 if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r);