Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / newgrf_house.cpp
blob8ae287020d635d963c717f5b60be072028ae04d2
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 ro Surrounding resolver.
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(ResolverObject &ro, HouseID house_id, TileIndex tile, Town *town,
43 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
44 : ScopeResolver(ro)
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, house_id, tile, town, not_yet_constructed, initial_random_bits, watched_cargo_triggers),
82 town_scope(*this, town, not_yet_constructed) // Don't access StorePSA if house is not yet constructed.
86 HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
88 /* Start from 1 because 0 means that no class has been assigned. */
89 for (int i = 1; i != lengthof(_class_mapping); i++) {
90 HouseClassMapping *map = &_class_mapping[i];
92 if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
94 if (map->class_id == 0 && map->grfid == 0) {
95 map->class_id = grf_class_id;
96 map->grfid = grfid;
97 return (HouseClassID)i;
100 return HOUSE_NO_CLASS;
103 void InitializeBuildingCounts()
105 memset(&_building_counts, 0, sizeof(_building_counts));
107 Town *t;
108 FOR_ALL_TOWNS(t) {
109 memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts));
114 * IncreaseBuildingCount()
115 * Increase the count of a building when it has been added by a town.
116 * @param t The town that the building is being built in
117 * @param house_id The id of the house being added
119 void IncreaseBuildingCount(Town *t, HouseID house_id)
121 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
123 if (!_loaded_newgrf_features.has_newhouses) return;
125 t->cache.building_counts.id_count[house_id]++;
126 _building_counts.id_count[house_id]++;
128 if (class_id == HOUSE_NO_CLASS) return;
130 t->cache.building_counts.class_count[class_id]++;
131 _building_counts.class_count[class_id]++;
135 * DecreaseBuildingCount()
136 * Decrease the number of a building when it is deleted.
137 * @param t The town that the building was built in
138 * @param house_id The id of the house being removed
140 void DecreaseBuildingCount(Town *t, HouseID house_id)
142 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
144 if (!_loaded_newgrf_features.has_newhouses) return;
146 if (t->cache.building_counts.id_count[house_id] > 0) t->cache.building_counts.id_count[house_id]--;
147 if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--;
149 if (class_id == HOUSE_NO_CLASS) return;
151 if (t->cache.building_counts.class_count[class_id] > 0) t->cache.building_counts.class_count[class_id]--;
152 if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
155 /* virtual */ uint32 HouseScopeResolver::GetRandomBits() const
157 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
158 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsHouseTile(this->tile)));
159 return this->not_yet_constructed ? this->initial_random_bits : GetHouseRandomBits(this->tile);
162 /* virtual */ uint32 HouseScopeResolver::GetTriggers() const
164 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
165 assert(IsValidTile(this->tile) && (this->not_yet_constructed || IsHouseTile(this->tile)));
166 return this->not_yet_constructed ? 0 : GetHouseTriggers(this->tile);
169 /* virtual */ void HouseScopeResolver::SetTriggers(int triggers) const
171 assert(!this->not_yet_constructed && IsValidTile(this->tile) && IsHouseTile(this->tile));
172 SetHouseTriggers(this->tile, triggers);
175 static uint32 GetNumHouses(HouseID house_id, const Town *town)
177 uint8 map_id_count, town_id_count, map_class_count, town_class_count;
178 HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
180 map_id_count = ClampU(_building_counts.id_count[house_id], 0, 255);
181 map_class_count = ClampU(_building_counts.class_count[class_id], 0, 255);
182 town_id_count = ClampU(town->cache.building_counts.id_count[house_id], 0, 255);
183 town_class_count = ClampU(town->cache.building_counts.class_count[class_id], 0, 255);
185 return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
189 * Get information about a nearby tile.
190 * @param parameter from callback. It's in fact a pair of coordinates
191 * @param tile TileIndex from which the callback was initiated
192 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
193 * @return a construction of bits obeying the newgrf format
195 static uint32 GetNearbyTileInformation(byte parameter, TileIndex tile, bool grf_version8)
197 tile = GetNearbyTile(parameter, tile);
198 return GetNearbyTileInformation(tile, grf_version8);
201 /** Structure with user-data for SearchNearbyHouseXXX - functions */
202 struct SearchNearbyHouseData {
203 const HouseSpec *hs; ///< Specs of the house that started the search.
204 TileIndex north_tile; ///< Northern tile of the house.
208 * Callback function to search a house by its HouseID
209 * @param tile TileIndex to be examined
210 * @param user_data SearchNearbyHouseData
211 * @return true or false, if found or not
213 static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
215 if (IsHouseTile(tile)) {
216 HouseID house = GetHouseType(tile); // tile been examined
217 const HouseSpec *hs = HouseSpec::Get(house);
218 if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
219 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
221 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
222 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
224 return hs->grf_prop.local_id == nbhd->hs->grf_prop.local_id && // same local id as the one requested
225 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
228 return false;
232 * Callback function to search a house by its classID
233 * @param tile TileIndex to be examined
234 * @param user_data SearchNearbyHouseData
235 * @return true or false, if found or not
237 static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
239 if (IsHouseTile(tile)) {
240 HouseID house = GetHouseType(tile); // tile been examined
241 const HouseSpec *hs = HouseSpec::Get(house);
242 if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
243 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
245 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
246 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
248 return hs->class_id == nbhd->hs->class_id && // same classid as the one requested
249 hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
252 return false;
256 * Callback function to search a house by its grfID
257 * @param tile TileIndex to be examined
258 * @param user_data SearchNearbyHouseData
259 * @return true or false, if found or not
261 static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
263 if (IsHouseTile(tile)) {
264 HouseID house = GetHouseType(tile); // tile been examined
265 const HouseSpec *hs = HouseSpec::Get(house);
266 if (hs->grf_prop.grffile != NULL) { // must be one from a grf file
267 SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data;
269 TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
270 if (north_tile == nbhd->north_tile) return false; // Always ignore origin house
272 return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
275 return false;
279 * This function will activate a search around a central tile, looking for some houses
280 * that fit the requested characteristics
281 * @param parameter that is given by the callback.
282 * bits 0..6 radius of the search
283 * bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID
284 * @param tile TileIndex from which to start the search
285 * @param house the HouseID that is associated to the house, the callback is called for
286 * @return the Manhattan distance from the center tile, if any, and 0 if failure
288 static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseID house)
290 static TestTileOnSearchProc * const search_procs[3] = {
291 SearchNearbyHouseID,
292 SearchNearbyHouseClass,
293 SearchNearbyHouseGRFID,
295 TileIndex found_tile = tile;
296 uint8 searchtype = GB(parameter, 6, 2);
297 uint8 searchradius = GB(parameter, 0, 6);
298 if (searchtype >= lengthof(search_procs)) return 0; // do not run on ill-defined code
299 if (searchradius < 1) return 0; // do not use a too low radius
301 SearchNearbyHouseData nbhd;
302 nbhd.hs = HouseSpec::Get(house);
303 nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
305 /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
306 if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
307 return DistanceManhattan(found_tile, tile);
309 return 0;
313 * @note Used by the resolver to get values for feature 07 deterministic spritegroups.
315 /* virtual */ uint32 HouseScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
317 switch (variable) {
318 /* Construction stage. */
319 case 0x40: return (IsHouseTile(this->tile) ? GetHouseBuildingStage(this->tile) : 0) | TileHash2Bit(TileX(this->tile), TileY(this->tile)) << 2;
321 /* Building age. */
322 case 0x41: return IsHouseTile(this->tile) ? GetHouseAge(this->tile) : 0;
324 /* Town zone */
325 case 0x42: return GetTownRadiusGroup(this->town, this->tile);
327 /* Terrain type */
328 case 0x43: return GetTerrainType(this->tile);
330 /* Number of this type of building on the map. */
331 case 0x44: return GetNumHouses(this->house_id, this->town);
333 /* Whether the town is being created or just expanded. */
334 case 0x45: return _generating_world ? 1 : 0;
336 /* Current animation frame. */
337 case 0x46: return IsHouseTile(this->tile) ? GetAnimationFrame(this->tile) : 0;
339 /* Position of the house */
340 case 0x47: return TileY(this->tile) << 16 | TileX(this->tile);
342 /* Building counts for old houses with id = parameter. */
343 case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, this->town) : 0;
345 /* Building counts for new houses with id = parameter. */
346 case 0x61: {
347 const HouseSpec *hs = HouseSpec::Get(this->house_id);
348 if (hs->grf_prop.grffile == NULL) return 0;
350 HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
351 return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, this->town);
354 /* Land info for nearby tiles. */
355 case 0x62: return GetNearbyTileInformation(parameter, this->tile, this->ro.grffile->grf_version >= 8);
357 /* Current animation frame of nearby house tiles */
358 case 0x63: {
359 TileIndex testtile = GetNearbyTile(parameter, this->tile);
360 return IsHouseTile(testtile) ? GetAnimationFrame(testtile) : 0;
363 /* Cargo acceptance history of nearby stations */
364 case 0x64: {
365 CargoID cid = GetCargoTranslation(parameter, this->ro.grffile);
366 if (cid == CT_INVALID) return 0;
368 /* Extract tile offset. */
369 int8 x_offs = GB(GetRegister(0x100), 0, 8);
370 int8 y_offs = GB(GetRegister(0x100), 8, 8);
371 TileIndex testtile = TILE_MASK(this->tile + TileDiffXY(x_offs, y_offs));
373 StationFinder stations(TileArea(testtile, 1, 1));
374 const StationList *sl = stations.GetStations();
376 /* Collect acceptance stats. */
377 uint32 res = 0;
378 for (Station * const * st_iter = sl->Begin(); st_iter != sl->End(); st_iter++) {
379 const Station *st = *st_iter;
380 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_EVER_ACCEPTED)) SetBit(res, 0);
381 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_LAST_MONTH)) SetBit(res, 1);
382 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_CURRENT_MONTH)) SetBit(res, 2);
383 if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(res, 3);
386 /* Cargo triggered CB 148? */
387 if (HasBit(this->watched_cargo_triggers, cid)) SetBit(res, 4);
389 return res;
392 /* Distance test for some house types */
393 case 0x65: return GetDistanceFromNearbyHouse(parameter, this->tile, this->house_id);
395 /* Class and ID of nearby house tile */
396 case 0x66: {
397 TileIndex testtile = GetNearbyTile(parameter, this->tile);
398 if (!IsHouseTile(testtile)) return 0xFFFFFFFF;
399 HouseSpec *hs = HouseSpec::Get(GetHouseType(testtile));
400 /* Information about the grf local classid if the house has a class */
401 uint houseclass = 0;
402 if (hs->class_id != HOUSE_NO_CLASS) {
403 houseclass = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8;
404 houseclass |= _class_mapping[hs->class_id].class_id;
406 /* old house type or grf-local houseid */
407 uint local_houseid = 0;
408 if (this->house_id < NEW_HOUSE_OFFSET) {
409 local_houseid = this->house_id;
410 } else {
411 local_houseid = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8;
412 local_houseid |= hs->grf_prop.local_id;
414 return houseclass << 16 | local_houseid;
417 /* GRFID of nearby house tile */
418 case 0x67: {
419 TileIndex testtile = GetNearbyTile(parameter, this->tile);
420 if (!IsHouseTile(testtile)) return 0xFFFFFFFF;
421 HouseID house_id = GetHouseType(testtile);
422 if (house_id < NEW_HOUSE_OFFSET) return 0;
423 /* Checking the grffile information via HouseSpec doesn't work
424 * in case the newgrf was removed. */
425 return _house_mngr.GetGRFID(house_id);
429 DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
431 *available = false;
432 return UINT_MAX;
435 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile,
436 bool not_yet_constructed, uint8 initial_random_bits, uint32 watched_cargo_triggers)
438 assert(IsValidTile(tile) && (not_yet_constructed || IsHouseTile(tile)));
440 HouseResolverObject object(house_id, tile, town, callback, param1, param2,
441 not_yet_constructed, initial_random_bits, watched_cargo_triggers);
443 const SpriteGroup *group = SpriteGroup::Resolve(HouseSpec::Get(house_id)->grf_prop.spritegroup[0], object);
444 if (group == NULL) return CALLBACK_FAILED;
446 return group->GetCallbackResult();
449 static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
451 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
453 const HouseSpec *hs = HouseSpec::Get(house_id);
454 PaletteID palette = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOUR_START;
455 if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
456 uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
457 if (callback != CALLBACK_FAILED) {
458 /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
459 palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
463 SpriteID image = dts->ground.sprite;
464 PaletteID pal = dts->ground.pal;
466 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
467 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
469 if (GB(image, 0, SPRITE_WIDTH) != 0) {
470 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
473 DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
476 void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
478 const HouseSpec *hs = HouseSpec::Get(house_id);
480 if (ti->tileh != SLOPE_FLAT) {
481 bool draw_old_one = true;
482 if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) {
483 /* Called to determine the type (if any) of foundation to draw for the house tile */
484 uint32 callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
485 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res);
488 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
491 HouseResolverObject object(house_id, ti->tile, Town::GetByTile(ti->tile));
493 const SpriteGroup *group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], object);
494 if (group != NULL && group->type == SGT_TILELAYOUT) {
495 /* Limit the building stage to the number of stages supplied. */
496 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
497 byte stage = GetHouseBuildingStage(ti->tile);
498 DrawTileLayout(ti, tlgroup, stage, house_id);
502 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
503 uint16 GetSimpleHouseCallback(CallbackID callback, uint32 param1, uint32 param2, const HouseSpec *spec, Town *town, TileIndex tile, uint32 extra_data)
505 return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data);
508 /** Helper class for animation control. */
509 struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, uint32, GetSimpleHouseCallback> {
510 static const CallbackID cb_animation_speed = CBID_HOUSE_ANIMATION_SPEED;
511 static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
513 static const HouseCallbackMask cbm_animation_speed = CBM_HOUSE_ANIMATION_SPEED;
514 static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
517 void AnimateNewHouseTile(TileIndex tile)
519 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
520 if (hs == NULL) return;
522 HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
525 void AnimateNewHouseConstruction(TileIndex tile)
527 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
529 if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
530 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0);
534 bool CanDeleteHouse(TileIndex tile)
536 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
538 /* Humans are always allowed to remove buildings, as is water and disasters and
539 * anyone using the scenario editor. */
540 if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE || _game_mode == GM_EDITOR || _generating_world) {
541 return true;
544 if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
545 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
546 return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res));
547 } else {
548 return !(hs->extra_flags & BUILDING_IS_PROTECTED);
552 static void AnimationControl(TileIndex tile, uint16 random_bits)
554 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
556 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
557 uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
558 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
562 bool NewHouseTileLoop(TileIndex tile)
564 const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
566 if (GetHouseProcessingTime(tile) > 0) {
567 DecHouseProcessingTime(tile);
568 return true;
571 TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
572 if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
574 if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
575 /* If this house is marked as having a synchronised callback, all the
576 * tiles will have the callback called at once, rather than when the
577 * tile loop reaches them. This should only be enabled for the northern
578 * tile, or strange things will happen (here, and in TTDPatch). */
579 if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) {
580 uint16 random = GB(Random(), 0, 16);
582 if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random);
583 if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TILE_ADDXY(tile, 0, 1), random);
584 if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TILE_ADDXY(tile, 1, 0), random);
585 if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
586 } else {
587 AnimationControl(tile, 0);
591 /* Check callback 21, which determines if a house should be destroyed. */
592 if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
593 uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
594 if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) {
595 ClearTownHouse(Town::GetByTile(tile), tile);
596 return false;
600 SetHouseProcessingTime(tile, hs->processing_time);
601 MarkTileDirtyByTile(tile);
602 return true;
605 static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
607 /* We can't trigger a non-existent building... */
608 assert(IsHouseTile(tile));
610 HouseID hid = GetHouseType(tile);
611 HouseSpec *hs = HouseSpec::Get(hid);
613 if (hs->grf_prop.spritegroup[0] == NULL) return;
615 HouseResolverObject object(hid, tile, Town::GetByTile(tile), CBID_RANDOM_TRIGGER);
616 object.trigger = trigger;
618 const SpriteGroup *group = SpriteGroup::Resolve(hs->grf_prop.spritegroup[0], object);
619 if (group == NULL) return;
621 byte new_random_bits = Random();
622 byte random_bits = GetHouseRandomBits(tile);
623 uint32 reseed = object.GetReseedSum(); // The scope only affects triggers, not the reseeding
624 random_bits &= ~reseed;
625 random_bits |= (first ? new_random_bits : base_random) & reseed;
626 SetHouseRandomBits(tile, random_bits);
628 switch (trigger) {
629 case HOUSE_TRIGGER_TILE_LOOP:
630 /* Random value already set. */
631 break;
633 case HOUSE_TRIGGER_TILE_LOOP_TOP:
634 if (!first) {
635 /* The top tile is marked dirty by the usual TileLoop */
636 MarkTileDirtyByTile(tile);
637 break;
639 /* Random value of first tile already set. */
640 if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
641 if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
642 if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
643 break;
647 void TriggerHouse(TileIndex t, HouseTrigger trigger)
649 DoTriggerHouse(t, trigger, 0, true);
653 * Run the watched cargo accepted callback for a single house tile.
654 * @param tile The house tile.
655 * @param origin The triggering tile.
656 * @param trigger_cargoes Cargo types that triggered the callback.
657 * @param random Random bits.
659 void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, uint32 trigger_cargoes, uint16 random)
661 CoordDiff diff = TileCoordDiff(origin, tile);
662 uint32 cb_info = random << 16 | (uint8)diff.y << 8 | (uint8)diff.x;
663 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_WATCHED_CARGO_ACCEPTED, HouseSpec::Get(GetHouseType(tile)), Town::GetByTile(tile), tile, 0, cb_info, trigger_cargoes);
667 * Run watched cargo accepted callback for a house.
668 * @param tile House tile.
669 * @param trigger_cargoes Triggering cargo types.
670 * @pre IsHouseTile(t)
672 void WatchedCargoCallback(TileIndex tile, uint32 trigger_cargoes)
674 assert(IsHouseTile(tile));
675 HouseID id = GetHouseType(tile);
676 const HouseSpec *hs = HouseSpec::Get(id);
678 trigger_cargoes &= hs->watched_cargoes;
679 /* None of the trigger cargoes is watched? */
680 if (trigger_cargoes == 0) return;
682 /* Same random value for all tiles of a multi-tile house. */
683 uint16 r = Random();
685 /* Do the callback, start at northern tile. */
686 TileIndex north = tile + GetHouseNorthPart(id);
687 hs = HouseSpec::Get(id);
689 DoWatchedCargoCallback(north, tile, trigger_cargoes, r);
690 if (hs->building_flags & BUILDING_2_TILES_Y) DoWatchedCargoCallback(TILE_ADDXY(north, 0, 1), tile, trigger_cargoes, r);
691 if (hs->building_flags & BUILDING_2_TILES_X) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 0), tile, trigger_cargoes, r);
692 if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r);