OpenTTD: src/newgrf_house.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_house.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_house.cpp 27928 2017-10-25 15:38:14Z frosch $ */
2
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 */
9
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"
26
27 #include "safeguards.h"
28
29 static BuildingCounts<uint32> _building_counts;
30 static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
31
32 HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, NUM_HOUSES, INVALID_HOUSE_ID);
33
44 HouseScopeResolver::HouseScopeResolver(ResolverObject &ro, HouseID house_id, TileIndex tile, Town *town,
45 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
46 : ScopeResolver(ro)
47 {
48 this->house_id = house_id;
49 this->tile = tile;
50 this->town = town;
51 this->not_yet_constructed = not_yet_constructed;
52 this->initial_random_bits = initial_random_bits;
53 this->watched_cargo_triggers = watched_cargo_triggers;
54 }
55
61 static const GRFFile *GetHouseSpecGrf(HouseID house_id)
62 {
63 const HouseSpec *hs = HouseSpec::Get(house_id);
64 return (hs != NULL) ? hs->grf_prop.grffile : NULL;
65 }
66
79 HouseResolverObject::HouseResolverObject(HouseID house_id, TileIndex tile, Town *town,
80 CallbackID callback, uint32 param1, uint32 param2,
81 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
82 : ResolverObject(GetHouseSpecGrf(house_id), callback, param1, param2),
83 house_scope(*this, house_id, tile, town, not_yet_constructed, initial_random_bits, watched_cargo_triggers),
84 town_scope(*this, town, not_yet_constructed) // Don't access StorePSA if house is not yet constructed.
85 {
86 this->root_spritegroup = HouseSpec::Get(house_id)->grf_prop.spritegroup[0];
87 }
88
89 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
90 {
91 /* Start from 1 because 0 means that no class has been assigned. */
92 for (int i = 1; i != lengthof(_class_mapping); i++) {
93 HouseClassMapping *map = &_class_mapping[i];
94
95 if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
96
97 if (map->class_id == 0 && map->grfid == 0) {
98 map->class_id = grf_class_id;
99 map->grfid = grfid;
100 return (HouseClassID)i;
101 }
102 }
103 return HOUSE_NO_CLASS;
104 }
105
106 void InitializeBuildingCounts()
107 {
108 memset(&_building_counts, 0, sizeof(_building_counts));
109
110 Town *t;
111 FOR_ALL_TOWNS(t) {
112 memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts));
113 }
114 }
115
122 void IncreaseBuildingCount(Town *t, HouseID house_id)
123 {
124 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
125
126 if (!_loaded_newgrf_features.has_newhouses) return;
127
128 t->cache.building_counts.id_count[house_id]++;
129 _building_counts.id_count[house_id]++;
130
131 if (class_id == HOUSE_NO_CLASS) return;
132
133 t->cache.building_counts.class_count[class_id]++;
134 _building_counts.class_count[class_id]++;
135 }
136
143 void DecreaseBuildingCount(Town *t, HouseID house_id)
144 {
145 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
146
147 if (!_loaded_newgrf_features.has_newhouses) return;
148
149 if (t->cache.building_counts.id_count[house_id] > 0) t->cache.building_counts.id_count[house_id]--;
150 if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--;
151
152 if (class_id == HOUSE_NO_CLASS) return;
153
154 if (t->cache.building_counts.class_count[class_id] > 0) t->cache.building_counts.class_count[class_id]--;
155 if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
156 }
157
158 /* virtual */ uint32 HouseScopeResolver::GetRandomBits() const
159 {
160 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
161 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)));
162 return this->not_yet_constructed ? this->initial_random_bits : GetHouseRandomBits(this->tile);
163 }
164
165 /* virtual */ uint32 HouseScopeResolver::GetTriggers() const
166 {
167 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
168 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)));
169 return this->not_yet_constructed ? 0 : GetHouseTriggers(this->tile);
170 }
171
172 static uint32 GetNumHouses(HouseID house_id, const Town *town)
173 {
174 uint8 map_id_count, town_id_count, map_class_count, town_class_count;
175 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
176
177 map_id_count = ClampU(_building_counts.id_count[house_id], 0, 255);
178 map_class_count = ClampU(_building_counts.class_count[class_id], 0, 255);
179 town_id_count = ClampU(town->cache.building_counts.id_count[house_id], 0, 255);
180 town_class_count = ClampU(town->cache.building_counts.class_count[class_id], 0, 255);
181
182 return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
183 }
184
192 static uint32 GetNearbyTileInformation(byte parameter, TileIndex tile, bool grf_version8)
193 {
194 tile = GetNearbyTile(parameter, tile);
195 return GetNearbyTileInformation(tile, grf_version8);
196 }
197
199 struct SearchNearbyHouseData {
200 const HouseSpec *hs;
201 TileIndex north_tile;
202 };
203
210 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
211 {
212 if (IsTileType(tile, MP_HOUSE)) {
213 HouseID house = GetHouseType(tile); // tile been examined
214 const HouseSpec *hs = HouseSpec::Get(house);
215 if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
216 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
217
218 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
219 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
220
221 return hs->grf_prop.local_id == nbhd->hs->grf_prop.local_id && // same local id as the one requested
222 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
223 }
224 }
225 return false;
226 }
227
234 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
235 {
236 if (IsTileType(tile, MP_HOUSE)) {
237 HouseID house = GetHouseType(tile); // tile been examined
238 const HouseSpec *hs = HouseSpec::Get(house);
239 if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
240 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
241
242 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
243 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
244
245 return hs->class_id == nbhd->hs->class_id && // same classid as the one requested
246 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
247 }
248 }
249 return false;
250 }
251
258 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
259 {
260 if (IsTileType(tile, MP_HOUSE)) {
261 HouseID house = GetHouseType(tile); // tile been examined
262 const HouseSpec *hs = HouseSpec::Get(house);
263 if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
264 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
265
266 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
267 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
268
269 return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
270 }
271 }
272 return false;
273 }
274
285 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
286 {
287 static TestTileOnSearchProc * const search_procs[3] = {
288 SearchNearbyHouseID,
289 SearchNearbyHouseClass,
290 SearchNearbyHouseGRFID,
291 };
292 TileIndex found_tile = tile;
293 uint8 searchtype = GB(parameter, 6, 2);
294 uint8 searchradius = GB(parameter, 0, 6);
295 if (searchtype >= lengthof(search_procs)) return 0; // do not run on ill-defined code
296 if (searchradius < 1) return 0; // do not use a too low radius
297
298 SearchNearbyHouseData nbhd;
299 nbhd.hs = HouseSpec::Get(house);
300 nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
301
302 /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
303 if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
304 return DistanceManhattan(found_tile, tile);
305 }
306 return 0;
307 }
308
312 /* virtual */ uint32 HouseScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
313 {
314 switch (variable) {
315 /* Construction stage. */
316 case 0x40: return (IsTileType(this->tile, MP_HOUSE) ? GetHouseBuildingStage(this->tile) : 0) | TileHash2Bit(TileX(this->tile), TileY(this->tile)) << 2;
317
318 /* Building age. */
319 case 0x41: return IsTileType(this->tile, MP_HOUSE) ? GetHouseAge(this->tile) : 0;
320
321 /* Town zone */
322 case 0x42: return GetTownRadiusGroup(this->town, this->tile);
323
324 /* Terrain type */
325 case 0x43: return GetTerrainType(this->tile);
326
327 /* Number of this type of building on the map. */
328 case 0x44: return GetNumHouses(this->house_id, this->town);
329
330 /* Whether the town is being created or just expanded. */
331 case 0x45: return _generating_world ? 1 : 0;
332
333 /* Current animation frame. */
334 case 0x46: return IsTileType(this->tile, MP_HOUSE) ? GetAnimationFrame(this->tile) : 0;
335
336 /* Position of the house */
337 case 0x47: return TileY(this->tile) << 16 | TileX(this->tile);
338
339 /* Building counts for old houses with id = parameter. */
340 case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, this->town) : 0;
341
342 /* Building counts for new houses with id = parameter. */
343 case 0x61: {
344 const HouseSpec *hs = HouseSpec::Get(this->house_id);
345 if (hs->grf_prop.grffile == NULL) return 0;
346
347 HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
348 return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, this->town);
349 }
350
351 /* Land info for nearby tiles. */
352 case 0x62: return GetNearbyTileInformation(parameter, this->tile, this->ro.grffile->grf_version >= 8);
353
354 /* Current animation frame of nearby house tiles */
355 case 0x63: {
356 TileIndex testtile = GetNearbyTile(parameter, this->tile);
357 return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0;
358 }
359
360 /* Cargo acceptance history of nearby stations */
361 case 0x64: {
362 CargoID cid = GetCargoTranslation(parameter, this->ro.grffile);
363 if (cid == CT_INVALID) return 0;
364
365 /* Extract tile offset. */
366 int8 x_offs = GB(GetRegister(0x100), 0, 8);
367 int8 y_offs = GB(GetRegister(0x100), 8, 8);
368 TileIndex testtile = TILE_MASK(this->tile + TileDiffXY(x_offs, y_offs));
369
370 StationFinder stations(TileArea(testtile, 1, 1));
371 const StationList *sl = stations.GetStations();
372
373 /* Collect acceptance stats. */
374 uint32 res = 0;
375 for (Station * const * st_iter = sl->Begin(); st_iter != sl->End(); st_iter++) {
376 const Station *st = *st_iter;
377 if (HasBit(st->goods[cid].status, GoodsEntry::GES_EVER_ACCEPTED)) SetBit(res, 0);
378 if (HasBit(st->goods[cid].status, GoodsEntry::GES_LAST_MONTH)) SetBit(res, 1);
379 if (HasBit(st->goods[cid].status, GoodsEntry::GES_CURRENT_MONTH)) SetBit(res, 2);
380 if (HasBit(st->goods[cid].status, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(res, 3);
381 }
382
383 /* Cargo triggered CB 148? */
384 if (HasBit(this->watched_cargo_triggers, cid)) SetBit(res, 4);
385
386 return res;
387 }
388
389 /* Distance test for some house types */
390 case 0x65: return GetDistanceFromNearbyHouse(parameter, this->tile, this->house_id);
391
392 /* Class and ID of nearby house tile */
393 case 0x66: {
394 TileIndex testtile = GetNearbyTile(parameter, this->tile);
395 if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
396 HouseID nearby_house_id = GetHouseType(testtile);
397 HouseSpec *hs = HouseSpec::Get(nearby_house_id);
398 /* Information about the grf local classid if the house has a class */
399 uint houseclass = 0;
400 if (hs->class_id != HOUSE_NO_CLASS) {
401 houseclass = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8;
402 houseclass |= _class_mapping[hs->class_id].class_id;
403 }
404 /* old house type or grf-local houseid */
405 uint local_houseid = 0;
406 if (nearby_house_id < NEW_HOUSE_OFFSET) {
407 local_houseid = nearby_house_id;
408 } else {
409 local_houseid = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8;
410 local_houseid |= hs->grf_prop.local_id;
411 }
412 return houseclass << 16 | local_houseid;
413 }
414
415 /* GRFID of nearby house tile */
416 case 0x67: {
417 TileIndex testtile = GetNearbyTile(parameter, this->tile);
418 if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
419 HouseID house_id = GetHouseType(testtile);
420 if (house_id < NEW_HOUSE_OFFSET) return 0;
421 /* Checking the grffile information via HouseSpec doesn't work
422 * in case the newgrf was removed. */
423 return _house_mngr.GetGRFID(house_id);
424 }
425 }
426
427 DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
428
429 *available = false;
430 return UINT_MAX;
431 }
432
433 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile,
434 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
435 {
436 assert(IsValidTile(tile) && (not_yet_constructed || IsTileType(tile, MP_HOUSE)));
437
438 HouseResolverObject object(house_id, tile, town, callback, param1, param2,
439 not_yet_constructed, initial_random_bits, watched_cargo_triggers);
440 return object.ResolveCallback();
441 }
442
443 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
444 {
445 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
446
447 const HouseSpec *hs = HouseSpec::Get(house_id);
448 PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
449 if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
450 uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
451 if (callback != CALLBACK_FAILED) {
452 /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
453 palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
454 }
455 }
456
457 SpriteID image = dts->ground.sprite;
458 PaletteID pal = dts->ground.pal;
459
460 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
461 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
462
463 if (GB(image, 0, SPRITE_WIDTH) != 0) {
464 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
465 }
466
467 DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
468 }
469
470 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
471 {
472 const HouseSpec *hs = HouseSpec::Get(house_id);
473
474 if (ti->tileh != SLOPE_FLAT) {
475 bool draw_old_one = true;
476 if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
477 /* Called to determine the type (if any) of foundation to draw for the house tile */
478 uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
479 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res);
480 }
481
482 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
483 }
484
485 HouseResolverObject object(house_id, ti->tile, Town::GetByTile(ti->tile));
486
487 const SpriteGroup *group = object.Resolve();
488 if (group != NULL && group->type == SGT_TILELAYOUT) {
489 /* Limit the building stage to the number of stages supplied. */
490 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
491 byte stage = GetHouseBuildingStage(ti->tile);
492 DrawTileLayout(ti, tlgroup, stage, house_id);
493 }
494 }
495
496 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
497 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, Town *town, TileIndex tile, uint32 extra_data)
498 {
499 return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data);
500 }
501
503 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, uint32, GetSimpleHouseCallback> {
504 static const CallbackID cb_animation_speed = CBID_HOUSE_ANIMATION_SPEED;
505 static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
506
507 static const HouseCallbackMask cbm_animation_speed = CBM_HOUSE_ANIMATION_SPEED;
508 static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
509 };
510
511 void AnimateNewHouseTile(TileIndex tile)
512 {
513 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
514 if (hs == NULL) return;
515
516 HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
517 }
518
519 void AnimateNewHouseConstruction(TileIndex tile)
520 {
521 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
522
523 if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
524 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
525 }
526 }
527
528 bool CanDeleteHouse(TileIndex tile)
529 {
530 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
531
532 /* Humans are always allowed to remove buildings, as is water and disasters and
533 * anyone using the scenario editor. */
534 if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE || _game_mode == GM_EDITOR || _generating_world) {
535 return true;
536 }
537
538 if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
539 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
540 return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res));
541 } else {
542 return !(hs->extra_flags & BUILDING_IS_PROTECTED);
543 }
544 }
545
546 static void AnimationControl(TileIndex tile, uint16 random_bits)
547 {
548 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
549
550 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
551 uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
552 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
553 }
554 }
555
556 bool NewHouseTileLoop(TileIndex tile)
557 {
558 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
559
560 if (GetHouseProcessingTime(tile) > 0) {
561 DecHouseProcessingTime(tile);
562 return true;
563 }
564
565 TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
566 if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
567
568 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
569 /* If this house is marked as having a synchronised callback, all the
570 * tiles will have the callback called at once, rather than when the
571 * tile loop reaches them. This should only be enabled for the northern
572 * tile, or strange things will happen (here, and in TTDPatch). */
573 if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
574 uint16 random = GB(Random(), 0, 16);
575
576 if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random);
577 if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TILE_ADDXY(tile, 0, 1), random);
578 if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TILE_ADDXY(tile, 1, 0), random);
579 if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
580 } else {
581 AnimationControl(tile, 0);
582 }
583 }
584
585 /* Check callback 21, which determines if a house should be destroyed. */
586 if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
587 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
588 if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) {
589 ClearTownHouse(Town::GetByTile(tile), tile);
590 return false;
591 }
592 }
593
594 SetHouseProcessingTime(tile, hs->processing_time);
595 MarkTileDirtyByTile(tile);
596 return true;
597 }
598
599 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
600 {
601 /* We can't trigger a non-existent building... */
602 assert(IsTileType(tile, MP_HOUSE));
603
604 HouseID hid = GetHouseType(tile);
605 HouseSpec *hs = HouseSpec::Get(hid);
606
607 if (hs->grf_prop.spritegroup[0] == NULL) return;
608
609 HouseResolverObject object(hid, tile, Town::GetByTile(tile), CBID_RANDOM_TRIGGER);
610 object.waiting_triggers = GetHouseTriggers(tile) | trigger;
611 SetHouseTriggers(tile, object.waiting_triggers); // store now for var 5F
612
613 const SpriteGroup *group = object.Resolve();
614 if (group == NULL) return;
615
616 /* Store remaining triggers. */
617 SetHouseTriggers(tile, object.GetRemainingTriggers());
618
619 /* Rerandomise bits. Scopes other than SELF are invalid for houses. For bug-to-bug-compatibility with TTDP we ignore the scope. */
620 byte new_random_bits = Random();
621 byte random_bits = GetHouseRandomBits(tile);
622 uint32 reseed = object.GetReseedSum();
623 random_bits &= ~reseed;
624 random_bits |= (first ? new_random_bits : base_random) & reseed;
625 SetHouseRandomBits(tile, random_bits);
626
627 switch (trigger) {
628 case HOUSE_TRIGGER_TILE_LOOP:
629 /* Random value already set. */
630 break;
631
632 case HOUSE_TRIGGER_TILE_LOOP_TOP:
633 if (!first) {
634 /* The top tile is marked dirty by the usual TileLoop */
635 MarkTileDirtyByTile(tile);
636 break;
637 }
638 /* Random value of first tile already set. */
639 if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
640 if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
641 if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
642 break;
643 }
644 }
645
646 void TriggerHouse(TileIndex t, HouseTrigger trigger)
647 {
648 DoTriggerHouse(t, trigger, 0, true);
649 }
650
658 void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, uint32 trigger_cargoes, uint16 random)
659 {
660 TileIndexDiffC diff = TileIndexToTileIndexDiffC(origin, tile);
661 uint32 cb_info = random << 16 | (uint8)diff.y << 8 | (uint8)diff.x;
662 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_WATCHED_CARGO_ACCEPTED, HouseSpec::Get(GetHouseType(tile)), Town::GetByTile(tile), tile, 0, cb_info, trigger_cargoes);
663 }
664
671 void WatchedCargoCallback(TileIndex tile, uint32 trigger_cargoes)
672 {
673 assert(IsTileType(tile, MP_HOUSE));
674 HouseID id = GetHouseType(tile);
675 const HouseSpec *hs = HouseSpec::Get(id);
676
677 trigger_cargoes &= hs->watched_cargoes;
678 /* None of the trigger cargoes is watched? */
679 if (trigger_cargoes == 0) return;
680
681 /* Same random value for all tiles of a multi-tile house. */
682 uint16 r = Random();
683
684 /* Do the callback, start at northern tile. */
685 TileIndex north = tile + GetHouseNorthPart(id);
686 hs = HouseSpec::Get(id);
687
688 DoWatchedCargoCallback(north, tile, trigger_cargoes, r);
689 if (hs->building_flags & BUILDING_2_TILES_Y) DoWatchedCargoCallback(TILE_ADDXY(north, 0, 1), tile, trigger_cargoes, r);
690 if (hs->building_flags & BUILDING_2_TILES_X) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 0), tile, trigger_cargoes, r);
691 if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r);
692 }
693