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/>.
8 /** @file newgrf_house.cpp Implementation of NewGRF houses. */
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"
21 #include "newgrf_animation_base.h"
22 #include "newgrf_cargo.h"
23 #include "station_base.h"
25 #include "safeguards.h"
27 static BuildingCounts
<uint32_t> _building_counts
{};
28 static std::vector
<HouseClassMapping
> _class_mapping
{};
30 HouseOverrideManager
_house_mngr(NEW_HOUSE_OFFSET
, NUM_HOUSES
, INVALID_HOUSE_ID
);
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 extern const HouseSpec _original_house_specs
[NEW_HOUSE_OFFSET
];
44 std::vector
<HouseSpec
> _house_specs
;
47 * Get a reference to all HouseSpecs.
48 * @return Reference to vector of all HouseSpecs.
50 std::vector
<HouseSpec
> &HouseSpec::Specs()
56 * Gets the index of this spec.
59 HouseID
HouseSpec::Index() const
61 return static_cast<HouseID
>(this - _house_specs
.data());
65 * Get the spec for a house ID.
66 * @param house_id The ID of the house.
67 * @return The HouseSpec associated with the ID.
69 HouseSpec
*HouseSpec::Get(size_t house_id
)
71 /* Empty house if index is out of range -- this might happen if NewGRFs are changed. */
72 static HouseSpec empty
= {};
74 assert(house_id
< NUM_HOUSES
);
75 if (house_id
>= _house_specs
.size()) return &empty
;
76 return &_house_specs
[house_id
];
79 /* Reset and initialise house specs. */
83 _house_specs
.reserve(std::size(_original_house_specs
));
87 /* Copy default houses. */
88 _house_specs
.insert(std::end(_house_specs
), std::begin(_original_house_specs
), std::end(_original_house_specs
));
90 /* Reset any overrides that have been set. */
91 _house_mngr
.ResetOverride();
95 * Construct a resolver for a house.
96 * @param house_id House to query.
97 * @param tile %Tile containing the house.
98 * @param town %Town containing the house.
99 * @param callback Callback ID.
100 * @param param1 First parameter (var 10) of the callback.
101 * @param param2 Second parameter (var 18) of the callback.
102 * @param not_yet_constructed House is still under construction.
103 * @param initial_random_bits Random bits during construction checks.
104 * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback.
106 HouseResolverObject::HouseResolverObject(HouseID house_id
, TileIndex tile
, Town
*town
,
107 CallbackID callback
, uint32_t param1
, uint32_t param2
,
108 bool not_yet_constructed
, uint8_t initial_random_bits
, CargoTypes watched_cargo_triggers
, int view
)
109 : ResolverObject(GetHouseSpecGrf(house_id
), callback
, param1
, param2
),
110 house_scope(*this, house_id
, tile
, town
, not_yet_constructed
, initial_random_bits
, watched_cargo_triggers
, view
),
111 town_scope(*this, town
, not_yet_constructed
) // Don't access StorePSA if house is not yet constructed.
113 /* Tile must be valid and a house tile, unless not yet constructed in which case it may also be INVALID_TILE. */
114 assert((IsValidTile(tile
) && (not_yet_constructed
|| IsTileType(tile
, MP_HOUSE
))) || (not_yet_constructed
&& tile
== INVALID_TILE
));
116 this->root_spritegroup
= HouseSpec::Get(house_id
)->grf_prop
.spritegroup
[0];
119 GrfSpecFeature
HouseResolverObject::GetFeature() const
124 uint32_t HouseResolverObject::GetDebugID() const
126 return HouseSpec::Get(this->house_scope
.house_id
)->grf_prop
.local_id
;
129 void ResetHouseClassIDs()
131 _class_mapping
.clear();
133 /* Add initial entry for HOUSE_NO_CLASS. */
134 _class_mapping
.emplace_back();
137 HouseClassID
AllocateHouseClassID(uint8_t grf_class_id
, uint32_t grfid
)
139 /* Start from 1 because 0 means that no class has been assigned. */
140 auto it
= std::find_if(std::next(std::begin(_class_mapping
)), std::end(_class_mapping
), [grf_class_id
, grfid
](const HouseClassMapping
&map
) { return map
.class_id
== grf_class_id
&& map
.grfid
== grfid
; });
142 /* HouseClass not found, allocate a new one. */
143 if (it
== std::end(_class_mapping
)) it
= _class_mapping
.insert(it
, {.grfid
= grfid
, .class_id
= grf_class_id
});
145 return static_cast<HouseClassID
>(std::distance(std::begin(_class_mapping
), it
));
149 * Initialise building counts for a town.
150 * @param t Town cache to initialise.
152 void InitializeBuildingCounts(Town
*t
)
154 t
->cache
.building_counts
.id_count
.clear();
155 t
->cache
.building_counts
.class_count
.clear();
156 t
->cache
.building_counts
.id_count
.resize(HouseSpec::Specs().size());
157 t
->cache
.building_counts
.class_count
.resize(_class_mapping
.size());
161 * Initialise global building counts and all town building counts.
163 void InitializeBuildingCounts()
165 _building_counts
.id_count
.clear();
166 _building_counts
.class_count
.clear();
167 _building_counts
.id_count
.resize(HouseSpec::Specs().size());
168 _building_counts
.class_count
.resize(_class_mapping
.size());
170 for (Town
*t
: Town::Iterate()) {
171 InitializeBuildingCounts(t
);
176 * Get read-only span of total HouseID building counts.
177 * @return span of HouseID building counts.
179 std::span
<const uint
> GetBuildingHouseIDCounts()
181 return _building_counts
.id_count
;
185 * IncreaseBuildingCount()
186 * Increase the count of a building when it has been added by a town.
187 * @param t The town that the building is being built in
188 * @param house_id The id of the house being added
190 void IncreaseBuildingCount(Town
*t
, HouseID house_id
)
192 HouseClassID class_id
= HouseSpec::Get(house_id
)->class_id
;
194 t
->cache
.building_counts
.id_count
[house_id
]++;
195 _building_counts
.id_count
[house_id
]++;
197 if (class_id
== HOUSE_NO_CLASS
) return;
199 t
->cache
.building_counts
.class_count
[class_id
]++;
200 _building_counts
.class_count
[class_id
]++;
204 * DecreaseBuildingCount()
205 * Decrease the number of a building when it is deleted.
206 * @param t The town that the building was built in
207 * @param house_id The id of the house being removed
209 void DecreaseBuildingCount(Town
*t
, HouseID house_id
)
211 HouseClassID class_id
= HouseSpec::Get(house_id
)->class_id
;
213 if (t
->cache
.building_counts
.id_count
[house_id
] > 0) t
->cache
.building_counts
.id_count
[house_id
]--;
214 if (_building_counts
.id_count
[house_id
] > 0) _building_counts
.id_count
[house_id
]--;
216 if (class_id
== HOUSE_NO_CLASS
) return;
218 if (t
->cache
.building_counts
.class_count
[class_id
] > 0) t
->cache
.building_counts
.class_count
[class_id
]--;
219 if (_building_counts
.class_count
[class_id
] > 0) _building_counts
.class_count
[class_id
]--;
222 /* virtual */ uint32_t HouseScopeResolver::GetRandomBits() const
224 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
225 return this->not_yet_constructed
? this->initial_random_bits
: GetHouseRandomBits(this->tile
);
228 /* virtual */ uint32_t HouseScopeResolver::GetTriggers() const
230 /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
231 return this->not_yet_constructed
? 0 : GetHouseTriggers(this->tile
);
234 static uint32_t GetNumHouses(HouseID house_id
, const Town
*town
)
236 HouseClassID class_id
= HouseSpec::Get(house_id
)->class_id
;
238 uint8_t map_id_count
= ClampTo
<uint8_t>(_building_counts
.id_count
[house_id
]);
239 uint8_t map_class_count
= ClampTo
<uint8_t>(_building_counts
.class_count
[class_id
]);
240 uint8_t town_id_count
= ClampTo
<uint8_t>(town
->cache
.building_counts
.id_count
[house_id
]);
241 uint8_t town_class_count
= ClampTo
<uint8_t>(town
->cache
.building_counts
.class_count
[class_id
]);
243 return map_class_count
<< 24 | town_class_count
<< 16 | map_id_count
<< 8 | town_id_count
;
247 * Get information about a nearby tile.
248 * @param parameter from callback. It's in fact a pair of coordinates
249 * @param tile TileIndex from which the callback was initiated
250 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
251 * @return a construction of bits obeying the newgrf format
253 static uint32_t GetNearbyTileInformation(uint8_t parameter
, TileIndex tile
, bool grf_version8
)
255 tile
= GetNearbyTile(parameter
, tile
);
256 return GetNearbyTileInformation(tile
, grf_version8
);
259 /** Structure with user-data for SearchNearbyHouseXXX - functions */
260 struct SearchNearbyHouseData
{
261 const HouseSpec
*hs
; ///< Specs of the house that started the search.
262 TileIndex north_tile
; ///< Northern tile of the house.
266 * Callback function to search a house by its HouseID
267 * @param tile TileIndex to be examined
268 * @param user_data SearchNearbyHouseData
269 * @return true or false, if found or not
271 static bool SearchNearbyHouseID(TileIndex tile
, void *user_data
)
273 if (IsTileType(tile
, MP_HOUSE
)) {
274 HouseID house
= GetHouseType(tile
); // tile been examined
275 const HouseSpec
*hs
= HouseSpec::Get(house
);
276 if (hs
->grf_prop
.grffile
!= nullptr) { // must be one from a grf file
277 SearchNearbyHouseData
*nbhd
= (SearchNearbyHouseData
*)user_data
;
279 TileIndex north_tile
= tile
+ GetHouseNorthPart(house
); // modifies 'house'!
280 if (north_tile
== nbhd
->north_tile
) return false; // Always ignore origin house
282 return hs
->grf_prop
.local_id
== nbhd
->hs
->grf_prop
.local_id
&& // same local id as the one requested
283 hs
->grf_prop
.grffile
->grfid
== nbhd
->hs
->grf_prop
.grffile
->grfid
; // from the same grf
290 * Callback function to search a house by its classID
291 * @param tile TileIndex to be examined
292 * @param user_data SearchNearbyHouseData
293 * @return true or false, if found or not
295 static bool SearchNearbyHouseClass(TileIndex tile
, void *user_data
)
297 if (IsTileType(tile
, MP_HOUSE
)) {
298 HouseID house
= GetHouseType(tile
); // tile been examined
299 const HouseSpec
*hs
= HouseSpec::Get(house
);
300 if (hs
->grf_prop
.grffile
!= nullptr) { // must be one from a grf file
301 SearchNearbyHouseData
*nbhd
= (SearchNearbyHouseData
*)user_data
;
303 TileIndex north_tile
= tile
+ GetHouseNorthPart(house
); // modifies 'house'!
304 if (north_tile
== nbhd
->north_tile
) return false; // Always ignore origin house
306 return hs
->class_id
== nbhd
->hs
->class_id
&& // same classid as the one requested
307 hs
->grf_prop
.grffile
->grfid
== nbhd
->hs
->grf_prop
.grffile
->grfid
; // from the same grf
314 * Callback function to search a house by its grfID
315 * @param tile TileIndex to be examined
316 * @param user_data SearchNearbyHouseData
317 * @return true or false, if found or not
319 static bool SearchNearbyHouseGRFID(TileIndex tile
, void *user_data
)
321 if (IsTileType(tile
, MP_HOUSE
)) {
322 HouseID house
= GetHouseType(tile
); // tile been examined
323 const HouseSpec
*hs
= HouseSpec::Get(house
);
324 if (hs
->grf_prop
.grffile
!= nullptr) { // must be one from a grf file
325 SearchNearbyHouseData
*nbhd
= (SearchNearbyHouseData
*)user_data
;
327 TileIndex north_tile
= tile
+ GetHouseNorthPart(house
); // modifies 'house'!
328 if (north_tile
== nbhd
->north_tile
) return false; // Always ignore origin house
330 return hs
->grf_prop
.grffile
->grfid
== nbhd
->hs
->grf_prop
.grffile
->grfid
; // from the same grf
337 * This function will activate a search around a central tile, looking for some houses
338 * that fit the requested characteristics
339 * @param parameter that is given by the callback.
340 * bits 0..6 radius of the search
341 * bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID
342 * @param tile TileIndex from which to start the search
343 * @param house the HouseID that is associated to the house, the callback is called for
344 * @return the Manhattan distance from the center tile, if any, and 0 if failure
346 static uint32_t GetDistanceFromNearbyHouse(uint8_t parameter
, TileIndex tile
, HouseID house
)
348 static TestTileOnSearchProc
* const search_procs
[3] = {
350 SearchNearbyHouseClass
,
351 SearchNearbyHouseGRFID
,
353 TileIndex found_tile
= tile
;
354 uint8_t searchtype
= GB(parameter
, 6, 2);
355 uint8_t searchradius
= GB(parameter
, 0, 6);
356 if (searchtype
>= lengthof(search_procs
)) return 0; // do not run on ill-defined code
357 if (searchradius
< 1) return 0; // do not use a too low radius
359 SearchNearbyHouseData nbhd
;
360 nbhd
.hs
= HouseSpec::Get(house
);
361 nbhd
.north_tile
= tile
+ GetHouseNorthPart(house
); // modifies 'house'!
363 /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
364 if (CircularTileSearch(&found_tile
, 2 * searchradius
+ 1, search_procs
[searchtype
], &nbhd
)) {
365 return DistanceManhattan(found_tile
, tile
);
371 * @note Used by the resolver to get values for feature 07 deterministic spritegroups.
373 /* virtual */ uint32_t HouseScopeResolver::GetVariable(uint8_t variable
, [[maybe_unused
]] uint32_t parameter
, bool &available
) const
375 if (this->tile
== INVALID_TILE
) {
376 /* House does not yet exist, nor is it being planned to exist. Provide some default values intead. */
378 case 0x40: return TOWN_HOUSE_COMPLETED
| this->view
<< 2; /* Construction stage. */
383 case 0x45: return _generating_world
? 1 : 0;
392 case 0x66: return 0xFFFFFFFF; /* Class and ID of nearby house. */
396 Debug(grf
, 1, "Unhandled house variable 0x{:X}", variable
);
402 /* Construction stage. */
403 case 0x40: return (IsTileType(this->tile
, MP_HOUSE
) ? GetHouseBuildingStage(this->tile
) : 0) | TileHash2Bit(TileX(this->tile
), TileY(this->tile
)) << 2;
406 case 0x41: return IsTileType(this->tile
, MP_HOUSE
) ? GetHouseAge(this->tile
).base() : 0;
409 case 0x42: return GetTownRadiusGroup(this->town
, this->tile
);
412 case 0x43: return GetTerrainType(this->tile
);
414 /* Number of this type of building on the map. */
415 case 0x44: return GetNumHouses(this->house_id
, this->town
);
417 /* Whether the town is being created or just expanded. */
418 case 0x45: return _generating_world
? 1 : 0;
420 /* Current animation frame. */
421 case 0x46: return IsTileType(this->tile
, MP_HOUSE
) ? GetAnimationFrame(this->tile
) : 0;
423 /* Position of the house */
424 case 0x47: return TileY(this->tile
) << 16 | TileX(this->tile
);
426 /* Building counts for old houses with id = parameter. */
427 case 0x60: return parameter
< NEW_HOUSE_OFFSET
? GetNumHouses(parameter
, this->town
) : 0;
429 /* Building counts for new houses with id = parameter. */
431 const HouseSpec
*hs
= HouseSpec::Get(this->house_id
);
432 if (hs
->grf_prop
.grffile
== nullptr) return 0;
434 HouseID new_house
= _house_mngr
.GetID(parameter
, hs
->grf_prop
.grffile
->grfid
);
435 return new_house
== INVALID_HOUSE_ID
? 0 : GetNumHouses(new_house
, this->town
);
438 /* Land info for nearby tiles. */
439 case 0x62: return GetNearbyTileInformation(parameter
, this->tile
, this->ro
.grffile
->grf_version
>= 8);
441 /* Current animation frame of nearby house tiles */
443 TileIndex testtile
= GetNearbyTile(parameter
, this->tile
);
444 return IsTileType(testtile
, MP_HOUSE
) ? GetAnimationFrame(testtile
) : 0;
447 /* Cargo acceptance history of nearby stations */
449 CargoID cid
= GetCargoTranslation(parameter
, this->ro
.grffile
);
450 if (!IsValidCargoID(cid
)) return 0;
452 /* Extract tile offset. */
453 int8_t x_offs
= GB(GetRegister(0x100), 0, 8);
454 int8_t y_offs
= GB(GetRegister(0x100), 8, 8);
455 TileIndex testtile
= Map::WrapToMap(this->tile
+ TileDiffXY(x_offs
, y_offs
));
457 StationFinder
stations(TileArea(testtile
, 1, 1));
458 const StationList
*sl
= stations
.GetStations();
460 /* Collect acceptance stats. */
462 for (Station
*st
: *sl
) {
463 if (HasBit(st
->goods
[cid
].status
, GoodsEntry::GES_EVER_ACCEPTED
)) SetBit(res
, 0);
464 if (HasBit(st
->goods
[cid
].status
, GoodsEntry::GES_LAST_MONTH
)) SetBit(res
, 1);
465 if (HasBit(st
->goods
[cid
].status
, GoodsEntry::GES_CURRENT_MONTH
)) SetBit(res
, 2);
466 if (HasBit(st
->goods
[cid
].status
, GoodsEntry::GES_ACCEPTED_BIGTICK
)) SetBit(res
, 3);
469 /* Cargo triggered CB 148? */
470 if (HasBit(this->watched_cargo_triggers
, cid
)) SetBit(res
, 4);
475 /* Distance test for some house types */
476 case 0x65: return GetDistanceFromNearbyHouse(parameter
, this->tile
, this->house_id
);
478 /* Class and ID of nearby house tile */
480 TileIndex testtile
= GetNearbyTile(parameter
, this->tile
);
481 if (!IsTileType(testtile
, MP_HOUSE
)) return 0xFFFFFFFF;
482 HouseID nearby_house_id
= GetHouseType(testtile
);
483 HouseSpec
*hs
= HouseSpec::Get(nearby_house_id
);
484 /* Information about the grf local classid if the house has a class */
486 if (hs
->class_id
!= HOUSE_NO_CLASS
) {
487 houseclass
= (hs
->grf_prop
.grffile
== this->ro
.grffile
? 1 : 2) << 8;
488 houseclass
|= _class_mapping
[hs
->class_id
].class_id
;
490 /* old house type or grf-local houseid */
491 uint local_houseid
= 0;
492 if (nearby_house_id
< NEW_HOUSE_OFFSET
) {
493 local_houseid
= nearby_house_id
;
495 local_houseid
= (hs
->grf_prop
.grffile
== this->ro
.grffile
? 1 : 2) << 8;
496 local_houseid
|= hs
->grf_prop
.local_id
;
498 return houseclass
<< 16 | local_houseid
;
501 /* GRFID of nearby house tile */
503 TileIndex testtile
= GetNearbyTile(parameter
, this->tile
);
504 if (!IsTileType(testtile
, MP_HOUSE
)) return 0xFFFFFFFF;
505 HouseID house_id
= GetHouseType(testtile
);
506 if (house_id
< NEW_HOUSE_OFFSET
) return 0;
507 /* Checking the grffile information via HouseSpec doesn't work
508 * in case the newgrf was removed. */
509 return _house_mngr
.GetGRFID(house_id
);
513 Debug(grf
, 1, "Unhandled house variable 0x{:X}", variable
);
519 uint16_t GetHouseCallback(CallbackID callback
, uint32_t param1
, uint32_t param2
, HouseID house_id
, Town
*town
, TileIndex tile
,
520 bool not_yet_constructed
, uint8_t initial_random_bits
, CargoTypes watched_cargo_triggers
, int view
)
522 HouseResolverObject
object(house_id
, tile
, town
, callback
, param1
, param2
,
523 not_yet_constructed
, initial_random_bits
, watched_cargo_triggers
, view
);
524 return object
.ResolveCallback();
527 static void DrawTileLayout(const TileInfo
*ti
, const TileLayoutSpriteGroup
*group
, uint8_t stage
, HouseID house_id
)
529 const DrawTileSprites
*dts
= group
->ProcessRegisters(&stage
);
531 const HouseSpec
*hs
= HouseSpec::Get(house_id
);
532 PaletteID palette
= GENERAL_SPRITE_COLOUR(hs
->random_colour
[TileHash2Bit(ti
->x
, ti
->y
)]);
533 if (HasBit(hs
->callback_mask
, CBM_HOUSE_COLOUR
)) {
534 uint16_t callback
= GetHouseCallback(CBID_HOUSE_COLOUR
, 0, 0, house_id
, Town::GetByTile(ti
->tile
), ti
->tile
);
535 if (callback
!= CALLBACK_FAILED
) {
536 /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
537 palette
= HasBit(callback
, 14) ? GB(callback
, 0, 8) + SPR_2CCMAP_BASE
: callback
;
541 SpriteID image
= dts
->ground
.sprite
;
542 PaletteID pal
= dts
->ground
.pal
;
544 if (HasBit(image
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) image
+= stage
;
545 if (HasBit(pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) pal
+= stage
;
547 if (GB(image
, 0, SPRITE_WIDTH
) != 0) {
548 DrawGroundSprite(image
, GroundSpritePaletteTransform(image
, pal
, palette
));
551 DrawNewGRFTileSeq(ti
, dts
, TO_HOUSES
, stage
, palette
);
554 void DrawNewHouseTile(TileInfo
*ti
, HouseID house_id
)
556 const HouseSpec
*hs
= HouseSpec::Get(house_id
);
558 if (ti
->tileh
!= SLOPE_FLAT
) {
559 bool draw_old_one
= true;
560 if (HasBit(hs
->callback_mask
, CBM_HOUSE_DRAW_FOUNDATIONS
)) {
561 /* Called to determine the type (if any) of foundation to draw for the house tile */
562 uint32_t callback_res
= GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS
, 0, 0, house_id
, Town::GetByTile(ti
->tile
), ti
->tile
);
563 if (callback_res
!= CALLBACK_FAILED
) draw_old_one
= ConvertBooleanCallback(hs
->grf_prop
.grffile
, CBID_HOUSE_DRAW_FOUNDATIONS
, callback_res
);
566 if (draw_old_one
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
569 HouseResolverObject
object(house_id
, ti
->tile
, Town::GetByTile(ti
->tile
));
571 const SpriteGroup
*group
= object
.Resolve();
572 if (group
!= nullptr && group
->type
== SGT_TILELAYOUT
) {
573 /* Limit the building stage to the number of stages supplied. */
574 const TileLayoutSpriteGroup
*tlgroup
= (const TileLayoutSpriteGroup
*)group
;
575 uint8_t stage
= GetHouseBuildingStage(ti
->tile
);
576 DrawTileLayout(ti
, tlgroup
, stage
, house_id
);
580 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
581 uint16_t GetSimpleHouseCallback(CallbackID callback
, uint32_t param1
, uint32_t param2
, const HouseSpec
*spec
, Town
*town
, TileIndex tile
, CargoTypes extra_data
)
583 return GetHouseCallback(callback
, param1
, param2
, spec
- HouseSpec::Get(0), town
, tile
, false, 0, extra_data
);
586 /** Helper class for animation control. */
587 struct HouseAnimationBase
: public AnimationBase
<HouseAnimationBase
, HouseSpec
, Town
, CargoTypes
, GetSimpleHouseCallback
, TileAnimationFrameAnimationHelper
<Town
> > {
588 static const CallbackID cb_animation_speed
= CBID_HOUSE_ANIMATION_SPEED
;
589 static const CallbackID cb_animation_next_frame
= CBID_HOUSE_ANIMATION_NEXT_FRAME
;
591 static const HouseCallbackMask cbm_animation_speed
= CBM_HOUSE_ANIMATION_SPEED
;
592 static const HouseCallbackMask cbm_animation_next_frame
= CBM_HOUSE_ANIMATION_NEXT_FRAME
;
595 void AnimateNewHouseTile(TileIndex tile
)
597 const HouseSpec
*hs
= HouseSpec::Get(GetHouseType(tile
));
598 if (hs
== nullptr) return;
600 HouseAnimationBase::AnimateTile(hs
, Town::GetByTile(tile
), tile
, HasFlag(hs
->extra_flags
, CALLBACK_1A_RANDOM_BITS
));
603 void AnimateNewHouseConstruction(TileIndex tile
)
605 const HouseSpec
*hs
= HouseSpec::Get(GetHouseType(tile
));
607 if (HasBit(hs
->callback_mask
, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE
)) {
608 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE
, hs
, Town::GetByTile(tile
), tile
, 0, 0);
612 bool CanDeleteHouse(TileIndex tile
)
614 const HouseSpec
*hs
= HouseSpec::Get(GetHouseType(tile
));
616 /* Humans are always allowed to remove buildings, as is water and disasters and
617 * anyone using the scenario editor. */
618 if (Company::IsValidHumanID(_current_company
) || _current_company
== OWNER_WATER
|| _current_company
== OWNER_NONE
|| _game_mode
== GM_EDITOR
|| _generating_world
) {
622 if (HasBit(hs
->callback_mask
, CBM_HOUSE_DENY_DESTRUCTION
)) {
623 uint16_t callback_res
= GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION
, 0, 0, GetHouseType(tile
), Town::GetByTile(tile
), tile
);
624 return (callback_res
== CALLBACK_FAILED
|| !ConvertBooleanCallback(hs
->grf_prop
.grffile
, CBID_HOUSE_DENY_DESTRUCTION
, callback_res
));
626 return !(hs
->extra_flags
& BUILDING_IS_PROTECTED
);
630 static void AnimationControl(TileIndex tile
, uint16_t random_bits
)
632 const HouseSpec
*hs
= HouseSpec::Get(GetHouseType(tile
));
634 if (HasBit(hs
->callback_mask
, CBM_HOUSE_ANIMATION_START_STOP
)) {
635 uint32_t param
= (hs
->extra_flags
& SYNCHRONISED_CALLBACK_1B
) ? (GB(Random(), 0, 16) | random_bits
<< 16) : Random();
636 HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP
, hs
, Town::GetByTile(tile
), tile
, param
, 0);
640 bool NewHouseTileLoop(TileIndex tile
)
642 const HouseSpec
*hs
= HouseSpec::Get(GetHouseType(tile
));
644 if (GetHouseProcessingTime(tile
) > 0) {
645 DecHouseProcessingTime(tile
);
649 TriggerHouse(tile
, HOUSE_TRIGGER_TILE_LOOP
);
650 if (hs
->building_flags
& BUILDING_HAS_1_TILE
) TriggerHouse(tile
, HOUSE_TRIGGER_TILE_LOOP_TOP
);
652 if (HasBit(hs
->callback_mask
, CBM_HOUSE_ANIMATION_START_STOP
)) {
653 /* If this house is marked as having a synchronised callback, all the
654 * tiles will have the callback called at once, rather than when the
655 * tile loop reaches them. This should only be enabled for the northern
656 * tile, or strange things will happen (here, and in TTDPatch). */
657 if (hs
->extra_flags
& SYNCHRONISED_CALLBACK_1B
) {
658 uint16_t random
= GB(Random(), 0, 16);
660 if (hs
->building_flags
& BUILDING_HAS_1_TILE
) AnimationControl(tile
, random
);
661 if (hs
->building_flags
& BUILDING_2_TILES_Y
) AnimationControl(TileAddXY(tile
, 0, 1), random
);
662 if (hs
->building_flags
& BUILDING_2_TILES_X
) AnimationControl(TileAddXY(tile
, 1, 0), random
);
663 if (hs
->building_flags
& BUILDING_HAS_4_TILES
) AnimationControl(TileAddXY(tile
, 1, 1), random
);
665 AnimationControl(tile
, 0);
669 /* Check callback 21, which determines if a house should be destroyed. */
670 if (HasBit(hs
->callback_mask
, CBM_HOUSE_DESTRUCTION
)) {
671 uint16_t callback_res
= GetHouseCallback(CBID_HOUSE_DESTRUCTION
, 0, 0, GetHouseType(tile
), Town::GetByTile(tile
), tile
);
672 if (callback_res
!= CALLBACK_FAILED
&& Convert8bitBooleanCallback(hs
->grf_prop
.grffile
, CBID_HOUSE_DESTRUCTION
, callback_res
)) {
673 ClearTownHouse(Town::GetByTile(tile
), tile
);
678 SetHouseProcessingTime(tile
, hs
->processing_time
);
679 MarkTileDirtyByTile(tile
);
683 static void DoTriggerHouse(TileIndex tile
, HouseTrigger trigger
, uint8_t base_random
, bool first
)
685 /* We can't trigger a non-existent building... */
686 assert(IsTileType(tile
, MP_HOUSE
));
688 HouseID hid
= GetHouseType(tile
);
689 HouseSpec
*hs
= HouseSpec::Get(hid
);
691 if (hs
->grf_prop
.spritegroup
[0] == nullptr) return;
693 HouseResolverObject
object(hid
, tile
, Town::GetByTile(tile
), CBID_RANDOM_TRIGGER
);
694 object
.waiting_triggers
= GetHouseTriggers(tile
) | trigger
;
695 SetHouseTriggers(tile
, object
.waiting_triggers
); // store now for var 5F
697 const SpriteGroup
*group
= object
.Resolve();
698 if (group
== nullptr) return;
700 /* Store remaining triggers. */
701 SetHouseTriggers(tile
, object
.GetRemainingTriggers());
703 /* Rerandomise bits. Scopes other than SELF are invalid for houses. For bug-to-bug-compatibility with TTDP we ignore the scope. */
704 uint8_t new_random_bits
= Random();
705 uint8_t random_bits
= GetHouseRandomBits(tile
);
706 uint32_t reseed
= object
.GetReseedSum();
707 random_bits
&= ~reseed
;
708 random_bits
|= (first
? new_random_bits
: base_random
) & reseed
;
709 SetHouseRandomBits(tile
, random_bits
);
712 case HOUSE_TRIGGER_TILE_LOOP
:
713 /* Random value already set. */
716 case HOUSE_TRIGGER_TILE_LOOP_TOP
:
718 /* The top tile is marked dirty by the usual TileLoop */
719 MarkTileDirtyByTile(tile
);
722 /* Random value of first tile already set. */
723 if (hs
->building_flags
& BUILDING_2_TILES_Y
) DoTriggerHouse(TileAddXY(tile
, 0, 1), trigger
, random_bits
, false);
724 if (hs
->building_flags
& BUILDING_2_TILES_X
) DoTriggerHouse(TileAddXY(tile
, 1, 0), trigger
, random_bits
, false);
725 if (hs
->building_flags
& BUILDING_HAS_4_TILES
) DoTriggerHouse(TileAddXY(tile
, 1, 1), trigger
, random_bits
, false);
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
, CargoTypes trigger_cargoes
, uint16_t random
)
744 TileIndexDiffC diff
= TileIndexToTileIndexDiffC(origin
, tile
);
745 uint32_t cb_info
= random
<< 16 | (uint8_t)diff
.y
<< 8 | (uint8_t)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 IsTileType(t, MP_HOUSE)
755 void WatchedCargoCallback(TileIndex tile
, CargoTypes trigger_cargoes
)
757 assert(IsTileType(tile
, MP_HOUSE
));
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_t 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(TileAddXY(north
, 0, 1), tile
, trigger_cargoes
, r
);
774 if (hs
->building_flags
& BUILDING_2_TILES_X
) DoWatchedCargoCallback(TileAddXY(north
, 1, 0), tile
, trigger_cargoes
, r
);
775 if (hs
->building_flags
& BUILDING_HAS_4_TILES
) DoWatchedCargoCallback(TileAddXY(north
, 1, 1), tile
, trigger_cargoes
, r
);