Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / newgrf_house.cpp
blob6a7d191f72da7ab6731ca36c1181c5e7b5685629
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 newgrf_house.cpp Implementation of NewGRF houses. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "landscape.h"
13 #include "newgrf_house.h"
14 #include "newgrf_spritegroup.h"
15 #include "newgrf_town.h"
16 #include "newgrf_sound.h"
17 #include "company_func.h"
18 #include "company_base.h"
19 #include "town.h"
20 #include "genworld.h"
21 #include "newgrf_animation_base.h"
22 #include "newgrf_cargo.h"
23 #include "station_base.h"
25 #include "safeguards.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 * Retrieve the grf file associated with a house.
34 * @param house_id House to query.
35 * @return The associated GRF file (may be \c nullptr).
37 static const GRFFile *GetHouseSpecGrf(HouseID house_id)
39 const HouseSpec *hs = HouseSpec::Get(house_id);
40 return (hs != nullptr) ? hs->grf_prop.grffile : nullptr;
43 /**
44 * Construct a resolver for a house.
45 * @param house_id House to query.
46 * @param tile %Tile containing the house.
47 * @param town %Town containing the house.
48 * @param callback Callback ID.
49 * @param param1 First parameter (var 10) of the callback.
50 * @param param2 Second parameter (var 18) of the callback.
51 * @param not_yet_constructed House is still under construction.
52 * @param initial_random_bits Random bits during construction checks.
53 * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback.
55 HouseResolverObject::HouseResolverObject(HouseID house_id, TileIndex tile, Town *town,
56 CallbackID callback, uint32 param1, uint32 param2,
57 bool not_yet_constructed, uint8 initial_random_bits, CargoTypes watched_cargo_triggers)
58 : ResolverObject(GetHouseSpecGrf(house_id), callback, param1, param2),
59 house_scope(*this, house_id, tile, town, not_yet_constructed, initial_random_bits, watched_cargo_triggers),
60 town_scope(*this, town, not_yet_constructed) // Don't access StorePSA if house is not yet constructed.
62 this->root_spritegroup = HouseSpec::Get(house_id)->grf_prop.spritegroup[0];
65 GrfSpecFeature HouseResolverObject::GetFeature() const
67 return GSF_HOUSES;
70 uint32 HouseResolverObject::GetDebugID() const
72 return HouseSpec::Get(this->house_scope.house_id)->grf_prop.local_id;
75 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
77 /* Start from 1 because 0 means that no class has been assigned. */
78 for (int i = 1; i != lengthof(_class_mapping); i++) {
79 HouseClassMapping *map = &_class_mapping[i];
81 if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
83 if (map->class_id == 0 && map->grfid == 0) {
84 map->class_id = grf_class_id;
85 map->grfid = grfid;
86 return (HouseClassID)i;
89 return HOUSE_NO_CLASS;
92 void InitializeBuildingCounts()
94 memset(&_building_counts, 0, sizeof(_building_counts));
96 for (Town *t : Town::Iterate()) {
97 memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts));
102 * IncreaseBuildingCount()
103 * Increase the count of a building when it has been added by a town.
104 * @param t The town that the building is being built in
105 * @param house_id The id of the house being added
107 void IncreaseBuildingCount(Town *t, HouseID house_id)
109 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
111 t->cache.building_counts.id_count[house_id]++;
112 _building_counts.id_count[house_id]++;
114 if (class_id == HOUSE_NO_CLASS) return;
116 t->cache.building_counts.class_count[class_id]++;
117 _building_counts.class_count[class_id]++;
121 * DecreaseBuildingCount()
122 * Decrease the number of a building when it is deleted.
123 * @param t The town that the building was built in
124 * @param house_id The id of the house being removed
126 void DecreaseBuildingCount(Town *t, HouseID house_id)
128 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
130 if (t->cache.building_counts.id_count[house_id] > 0) t->cache.building_counts.id_count[house_id]--;
131 if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--;
133 if (class_id == HOUSE_NO_CLASS) return;
135 if (t->cache.building_counts.class_count[class_id] > 0) t->cache.building_counts.class_count[class_id]--;
136 if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
139 /* virtual */ uint32 HouseScopeResolver::GetRandomBits() const
141 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
142 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)));
143 return this->not_yet_constructed ? this->initial_random_bits : GetHouseRandomBits(this->tile);
146 /* virtual */ uint32 HouseScopeResolver::GetTriggers() const
148 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
149 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)));
150 return this->not_yet_constructed ? 0 : GetHouseTriggers(this->tile);
153 static uint32 GetNumHouses(HouseID house_id, const Town *town)
155 uint8 map_id_count, town_id_count, map_class_count, town_class_count;
156 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
158 map_id_count = ClampU(_building_counts.id_count[house_id], 0, 255);
159 map_class_count = ClampU(_building_counts.class_count[class_id], 0, 255);
160 town_id_count = ClampU(town->cache.building_counts.id_count[house_id], 0, 255);
161 town_class_count = ClampU(town->cache.building_counts.class_count[class_id], 0, 255);
163 return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
167 * Get information about a nearby tile.
168 * @param parameter from callback. It's in fact a pair of coordinates
169 * @param tile TileIndex from which the callback was initiated
170 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
171 * @return a construction of bits obeying the newgrf format
173 static uint32 GetNearbyTileInformation(byte parameter, TileIndex tile, bool grf_version8)
175 tile = GetNearbyTile(parameter, tile);
176 return GetNearbyTileInformation(tile, grf_version8);
179 /** Structure with user-data for SearchNearbyHouseXXX - functions */
180 struct SearchNearbyHouseData {
181 const HouseSpec *hs; ///< Specs of the house that started the search.
182 TileIndex north_tile; ///< Northern tile of the house.
186 * Callback function to search a house by its HouseID
187 * @param tile TileIndex to be examined
188 * @param user_data SearchNearbyHouseData
189 * @return true or false, if found or not
191 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
193 if (IsTileType(tile, MP_HOUSE)) {
194 HouseID house = GetHouseType(tile); // tile been examined
195 const HouseSpec *hs = HouseSpec::Get(house);
196 if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file
197 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
199 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
200 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
202 return hs->grf_prop.local_id == nbhd->hs->grf_prop.local_id && // same local id as the one requested
203 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
206 return false;
210 * Callback function to search a house by its classID
211 * @param tile TileIndex to be examined
212 * @param user_data SearchNearbyHouseData
213 * @return true or false, if found or not
215 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
217 if (IsTileType(tile, MP_HOUSE)) {
218 HouseID house = GetHouseType(tile); // tile been examined
219 const HouseSpec *hs = HouseSpec::Get(house);
220 if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file
221 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
223 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
224 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
226 return hs->class_id == nbhd->hs->class_id && // same classid as the one requested
227 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
230 return false;
234 * Callback function to search a house by its grfID
235 * @param tile TileIndex to be examined
236 * @param user_data SearchNearbyHouseData
237 * @return true or false, if found or not
239 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
241 if (IsTileType(tile, MP_HOUSE)) {
242 HouseID house = GetHouseType(tile); // tile been examined
243 const HouseSpec *hs = HouseSpec::Get(house);
244 if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file
245 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
247 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
248 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
250 return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
253 return false;
257 * This function will activate a search around a central tile, looking for some houses
258 * that fit the requested characteristics
259 * @param parameter that is given by the callback.
260 * bits 0..6 radius of the search
261 * bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID
262 * @param tile TileIndex from which to start the search
263 * @param house the HouseID that is associated to the house, the callback is called for
264 * @return the Manhattan distance from the center tile, if any, and 0 if failure
266 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
268 static TestTileOnSearchProc * const search_procs[3] = {
269 SearchNearbyHouseID,
270 SearchNearbyHouseClass,
271 SearchNearbyHouseGRFID,
273 TileIndex found_tile = tile;
274 uint8 searchtype = GB(parameter, 6, 2);
275 uint8 searchradius = GB(parameter, 0, 6);
276 if (searchtype >= lengthof(search_procs)) return 0; // do not run on ill-defined code
277 if (searchradius < 1) return 0; // do not use a too low radius
279 SearchNearbyHouseData nbhd;
280 nbhd.hs = HouseSpec::Get(house);
281 nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
283 /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
284 if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
285 return DistanceManhattan(found_tile, tile);
287 return 0;
291 * @note Used by the resolver to get values for feature 07 deterministic spritegroups.
293 /* virtual */ uint32 HouseScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
295 switch (variable) {
296 /* Construction stage. */
297 case 0x40: return (IsTileType(this->tile, MP_HOUSE) ? GetHouseBuildingStage(this->tile) : 0) | TileHash2Bit(TileX(this->tile), TileY(this->tile)) << 2;
299 /* Building age. */
300 case 0x41: return IsTileType(this->tile, MP_HOUSE) ? GetHouseAge(this->tile) : 0;
302 /* Town zone */
303 case 0x42: return GetTownRadiusGroup(this->town, this->tile);
305 /* Terrain type */
306 case 0x43: return GetTerrainType(this->tile);
308 /* Number of this type of building on the map. */
309 case 0x44: return GetNumHouses(this->house_id, this->town);
311 /* Whether the town is being created or just expanded. */
312 case 0x45: return _generating_world ? 1 : 0;
314 /* Current animation frame. */
315 case 0x46: return IsTileType(this->tile, MP_HOUSE) ? GetAnimationFrame(this->tile) : 0;
317 /* Position of the house */
318 case 0x47: return TileY(this->tile) << 16 | TileX(this->tile);
320 /* Building counts for old houses with id = parameter. */
321 case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, this->town) : 0;
323 /* Building counts for new houses with id = parameter. */
324 case 0x61: {
325 const HouseSpec *hs = HouseSpec::Get(this->house_id);
326 if (hs->grf_prop.grffile == nullptr) return 0;
328 HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
329 return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, this->town);
332 /* Land info for nearby tiles. */
333 case 0x62: return GetNearbyTileInformation(parameter, this->tile, this->ro.grffile->grf_version >= 8);
335 /* Current animation frame of nearby house tiles */
336 case 0x63: {
337 TileIndex testtile = GetNearbyTile(parameter, this->tile);
338 return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0;
341 /* Cargo acceptance history of nearby stations */
342 case 0x64: {
343 CargoID cid = GetCargoTranslation(parameter, this->ro.grffile);
344 if (cid == CT_INVALID) return 0;
346 /* Extract tile offset. */
347 int8 x_offs = GB(GetRegister(0x100), 0, 8);
348 int8 y_offs = GB(GetRegister(0x100), 8, 8);
349 TileIndex testtile = TILE_MASK(this->tile + TileDiffXY(x_offs, y_offs));
351 StationFinder stations(TileArea(testtile, 1, 1));
352 const StationList *sl = stations.GetStations();
354 /* Collect acceptance stats. */
355 uint32 res = 0;
356 for (Station *st : *sl) {
357 if (HasBit(st->goods[cid].status, GoodsEntry::GES_EVER_ACCEPTED)) SetBit(res, 0);
358 if (HasBit(st->goods[cid].status, GoodsEntry::GES_LAST_MONTH)) SetBit(res, 1);
359 if (HasBit(st->goods[cid].status, GoodsEntry::GES_CURRENT_MONTH)) SetBit(res, 2);
360 if (HasBit(st->goods[cid].status, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(res, 3);
363 /* Cargo triggered CB 148? */
364 if (HasBit(this->watched_cargo_triggers, cid)) SetBit(res, 4);
366 return res;
369 /* Distance test for some house types */
370 case 0x65: return GetDistanceFromNearbyHouse(parameter, this->tile, this->house_id);
372 /* Class and ID of nearby house tile */
373 case 0x66: {
374 TileIndex testtile = GetNearbyTile(parameter, this->tile);
375 if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
376 HouseID nearby_house_id = GetHouseType(testtile);
377 HouseSpec *hs = HouseSpec::Get(nearby_house_id);
378 /* Information about the grf local classid if the house has a class */
379 uint houseclass = 0;
380 if (hs->class_id != HOUSE_NO_CLASS) {
381 houseclass = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8;
382 houseclass |= _class_mapping[hs->class_id].class_id;
384 /* old house type or grf-local houseid */
385 uint local_houseid = 0;
386 if (nearby_house_id < NEW_HOUSE_OFFSET) {
387 local_houseid = nearby_house_id;
388 } else {
389 local_houseid = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8;
390 local_houseid |= hs->grf_prop.local_id;
392 return houseclass << 16 | local_houseid;
395 /* GRFID of nearby house tile */
396 case 0x67: {
397 TileIndex testtile = GetNearbyTile(parameter, this->tile);
398 if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
399 HouseID house_id = GetHouseType(testtile);
400 if (house_id < NEW_HOUSE_OFFSET) return 0;
401 /* Checking the grffile information via HouseSpec doesn't work
402 * in case the newgrf was removed. */
403 return _house_mngr.GetGRFID(house_id);
407 Debug(grf, 1, "Unhandled house variable 0x{:X}", variable);
409 *available = false;
410 return UINT_MAX;
413 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile,
414 bool not_yet_constructed, uint8 initial_random_bits, CargoTypes watched_cargo_triggers)
416 assert(IsValidTile(tile) && (not_yet_constructed || IsTileType(tile, MP_HOUSE)));
418 HouseResolverObject object(house_id, tile, town, callback, param1, param2,
419 not_yet_constructed, initial_random_bits, watched_cargo_triggers);
420 return object.ResolveCallback();
423 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
425 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
427 const HouseSpec *hs = HouseSpec::Get(house_id);
428 PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
429 if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
430 uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
431 if (callback != CALLBACK_FAILED) {
432 /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
433 palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
437 SpriteID image = dts->ground.sprite;
438 PaletteID pal = dts->ground.pal;
440 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
441 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
443 if (GB(image, 0, SPRITE_WIDTH) != 0) {
444 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
447 DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
450 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
452 const HouseSpec *hs = HouseSpec::Get(house_id);
454 if (ti->tileh != SLOPE_FLAT) {
455 bool draw_old_one = true;
456 if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
457 /* Called to determine the type (if any) of foundation to draw for the house tile */
458 uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
459 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res);
462 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
465 HouseResolverObject object(house_id, ti->tile, Town::GetByTile(ti->tile));
467 const SpriteGroup *group = object.Resolve();
468 if (group != nullptr && group->type == SGT_TILELAYOUT) {
469 /* Limit the building stage to the number of stages supplied. */
470 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
471 byte stage = GetHouseBuildingStage(ti->tile);
472 DrawTileLayout(ti, tlgroup, stage, house_id);
476 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
477 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, Town *town, TileIndex tile, CargoTypes extra_data)
479 return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data);
482 /** Helper class for animation control. */
483 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, CargoTypes, GetSimpleHouseCallback> {
484 static const CallbackID cb_animation_speed = CBID_HOUSE_ANIMATION_SPEED;
485 static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
487 static const HouseCallbackMask cbm_animation_speed = CBM_HOUSE_ANIMATION_SPEED;
488 static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
491 void AnimateNewHouseTile(TileIndex tile)
493 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
494 if (hs == nullptr) return;
496 HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
499 void AnimateNewHouseConstruction(TileIndex tile)
501 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
503 if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
504 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
508 bool CanDeleteHouse(TileIndex tile)
510 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
512 /* Humans are always allowed to remove buildings, as is water and disasters and
513 * anyone using the scenario editor. */
514 if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE || _game_mode == GM_EDITOR || _generating_world) {
515 return true;
518 if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
519 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
520 return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res));
521 } else {
522 return !(hs->extra_flags & BUILDING_IS_PROTECTED);
526 static void AnimationControl(TileIndex tile, uint16 random_bits)
528 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
530 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
531 uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
532 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
536 bool NewHouseTileLoop(TileIndex tile)
538 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
540 if (GetHouseProcessingTime(tile) > 0) {
541 DecHouseProcessingTime(tile);
542 return true;
545 TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
546 if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
548 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
549 /* If this house is marked as having a synchronised callback, all the
550 * tiles will have the callback called at once, rather than when the
551 * tile loop reaches them. This should only be enabled for the northern
552 * tile, or strange things will happen (here, and in TTDPatch). */
553 if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
554 uint16 random = GB(Random(), 0, 16);
556 if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random);
557 if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TILE_ADDXY(tile, 0, 1), random);
558 if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TILE_ADDXY(tile, 1, 0), random);
559 if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
560 } else {
561 AnimationControl(tile, 0);
565 /* Check callback 21, which determines if a house should be destroyed. */
566 if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
567 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
568 if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) {
569 ClearTownHouse(Town::GetByTile(tile), tile);
570 return false;
574 SetHouseProcessingTime(tile, hs->processing_time);
575 MarkTileDirtyByTile(tile);
576 return true;
579 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
581 /* We can't trigger a non-existent building... */
582 assert(IsTileType(tile, MP_HOUSE));
584 HouseID hid = GetHouseType(tile);
585 HouseSpec *hs = HouseSpec::Get(hid);
587 if (hs->grf_prop.spritegroup[0] == nullptr) return;
589 HouseResolverObject object(hid, tile, Town::GetByTile(tile), CBID_RANDOM_TRIGGER);
590 object.waiting_triggers = GetHouseTriggers(tile) | trigger;
591 SetHouseTriggers(tile, object.waiting_triggers); // store now for var 5F
593 const SpriteGroup *group = object.Resolve();
594 if (group == nullptr) return;
596 /* Store remaining triggers. */
597 SetHouseTriggers(tile, object.GetRemainingTriggers());
599 /* Rerandomise bits. Scopes other than SELF are invalid for houses. For bug-to-bug-compatibility with TTDP we ignore the scope. */
600 byte new_random_bits = Random();
601 byte random_bits = GetHouseRandomBits(tile);
602 uint32 reseed = object.GetReseedSum();
603 random_bits &= ~reseed;
604 random_bits |= (first ? new_random_bits : base_random) & reseed;
605 SetHouseRandomBits(tile, random_bits);
607 switch (trigger) {
608 case HOUSE_TRIGGER_TILE_LOOP:
609 /* Random value already set. */
610 break;
612 case HOUSE_TRIGGER_TILE_LOOP_TOP:
613 if (!first) {
614 /* The top tile is marked dirty by the usual TileLoop */
615 MarkTileDirtyByTile(tile);
616 break;
618 /* Random value of first tile already set. */
619 if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
620 if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
621 if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
622 break;
626 void TriggerHouse(TileIndex t, HouseTrigger trigger)
628 DoTriggerHouse(t, trigger, 0, true);
632 * Run the watched cargo accepted callback for a single house tile.
633 * @param tile The house tile.
634 * @param origin The triggering tile.
635 * @param trigger_cargoes Cargo types that triggered the callback.
636 * @param random Random bits.
638 void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, CargoTypes trigger_cargoes, uint16 random)
640 TileIndexDiffC diff = TileIndexToTileIndexDiffC(origin, tile);
641 uint32 cb_info = random << 16 | (uint8)diff.y << 8 | (uint8)diff.x;
642 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_WATCHED_CARGO_ACCEPTED, HouseSpec::Get(GetHouseType(tile)), Town::GetByTile(tile), tile, 0, cb_info, trigger_cargoes);
646 * Run watched cargo accepted callback for a house.
647 * @param tile House tile.
648 * @param trigger_cargoes Triggering cargo types.
649 * @pre IsTileType(t, MP_HOUSE)
651 void WatchedCargoCallback(TileIndex tile, CargoTypes trigger_cargoes)
653 assert(IsTileType(tile, MP_HOUSE));
654 HouseID id = GetHouseType(tile);
655 const HouseSpec *hs = HouseSpec::Get(id);
657 trigger_cargoes &= hs->watched_cargoes;
658 /* None of the trigger cargoes is watched? */
659 if (trigger_cargoes == 0) return;
661 /* Same random value for all tiles of a multi-tile house. */
662 uint16 r = Random();
664 /* Do the callback, start at northern tile. */
665 TileIndex north = tile + GetHouseNorthPart(id);
666 hs = HouseSpec::Get(id);
668 DoWatchedCargoCallback(north, tile, trigger_cargoes, r);
669 if (hs->building_flags & BUILDING_2_TILES_Y) DoWatchedCargoCallback(TILE_ADDXY(north, 0, 1), tile, trigger_cargoes, r);
670 if (hs->building_flags & BUILDING_2_TILES_X) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 0), tile, trigger_cargoes, r);
671 if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r);