29static std::vector<HouseClassMapping> _class_mapping{};
45std::vector<HouseSpec> _house_specs;
62 return static_cast<HouseID>(
this - _house_specs.data());
76 if (house_id >= _house_specs.size())
return ∅
77 return &_house_specs[house_id];
84 _house_specs.reserve(std::size(_original_house_specs));
89 _house_specs.insert(std::end(_house_specs), std::begin(_original_house_specs), std::end(_original_house_specs));
108 CallbackID callback, uint32_t param1, uint32_t param2,
109 bool not_yet_constructed, uint8_t initial_random_bits, CargoTypes watched_cargo_triggers,
int view)
111 house_scope(*this, house_id, tile, town, not_yet_constructed, initial_random_bits, watched_cargo_triggers, view),
112 town_scope(*this, town, not_yet_constructed)
130void ResetHouseClassIDs()
132 _class_mapping.clear();
135 _class_mapping.emplace_back();
138HouseClassID AllocateHouseClassID(uint8_t grf_class_id, uint32_t grfid)
141 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; });
144 if (it == std::end(_class_mapping)) it = _class_mapping.insert(it, {.grfid = grfid, .class_id = grf_class_id});
146 return static_cast<HouseClassID>(std::distance(std::begin(_class_mapping), it));
166 _building_counts.id_count.clear();
167 _building_counts.class_count.clear();
169 _building_counts.class_count.resize(_class_mapping.size());
182 return _building_counts.id_count;
196 _building_counts.id_count[house_id]++;
198 if (class_id == HOUSE_NO_CLASS)
return;
201 _building_counts.class_count[class_id]++;
215 if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--;
217 if (class_id == HOUSE_NO_CLASS)
return;
220 if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
235static uint32_t GetNumHouses(
HouseID house_id,
const Town *town)
239 uint8_t map_id_count = ClampTo<uint8_t>(_building_counts.id_count[house_id]);
240 uint8_t map_class_count = ClampTo<uint8_t>(_building_counts.class_count[class_id]);
244 return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
272 uint8_t searchtype =
GB(parameter, 6, 2);
273 uint8_t searchradius =
GB(parameter, 0, 6);
274 if (searchradius < 1)
return 0;
287 switch (searchtype) {
293 match = hs->
class_id == start_hs->class_id &&
329 case 0x66:
return 0xFFFFFFFF;
335 Debug(grf, 1,
"Unhandled house variable 0x{:X}", variable);
354 case 0x44:
return GetNumHouses(this->
house_id, this->
town);
374 return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, this->
town);
401 res |= st->goods[cargo_type].ConvertState();
421 if (hs->
class_id != HOUSE_NO_CLASS) {
423 houseclass |= _class_mapping[hs->
class_id].class_id;
426 uint local_houseid = 0;
428 local_houseid = nearby_house_id;
433 return houseclass << 16 | local_houseid;
450 Debug(grf, 1,
"Unhandled house variable 0x{:X}", variable);
456uint16_t GetHouseCallback(
CallbackID callback, uint32_t param1, uint32_t param2,
HouseID house_id,
Town *town,
TileIndex tile, std::span<int32_t> regs100,
457 bool not_yet_constructed, uint8_t initial_random_bits, CargoTypes watched_cargo_triggers,
int view)
460 not_yet_constructed, initial_random_bits, watched_cargo_triggers, view);
461 return object.ResolveCallback(regs100);
472 palette =
HasBit(callback, 14) ?
GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
494 bool draw_old_one =
true;
507 if (group !=
nullptr) {
510 auto processor = group->ProcessRegisters(
object, &stage);
511 auto dts = processor.GetLayout();
528 if (group ==
nullptr)
return;
532 auto dts = processor.GetLayout();
539 palette =
HasBit(callback, 14) ?
GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
557static uint16_t GetSimpleHouseCallback(
CallbackID callback, uint32_t param1, uint32_t param2,
const HouseSpec *spec,
Town *town,
TileIndex tile, CargoTypes extra_data)
559 return GetHouseCallback(callback, param1, param2, spec -
HouseSpec::Get(0), town, tile, {},
false, 0, extra_data);
574 if (hs ==
nullptr)
return;
579void TriggerHouseAnimation_ConstructionStageChanged(
TileIndex tile,
bool first_call)
619 uint32_t param = sync ? (
GB(
Random(), 0, 16) | random_bits << 16) :
Random();
633 TriggerHouseRandomisation(tile, HouseRandomTrigger::TileLoop);
634 if (hs->
building_flags.
Any(BUILDING_HAS_1_TILE)) TriggerHouseRandomisation(tile, HouseRandomTrigger::TileLoopNorth);
641 uint16_t random =
GB(
Random(), 0, 16);
674 waiting_random_triggers.Set(trigger);
676 object.SetWaitingRandomTriggers(waiting_random_triggers);
678 object.ResolveRerandomisation();
681 waiting_random_triggers.Reset(
object.GetUsedRandomTriggers());
685 uint8_t new_random_bits =
Random();
687 uint32_t reseed =
object.GetReseedSum();
688 random_bits &= ~reseed;
689 random_bits |= (first ? new_random_bits : base_random) & reseed;
693 case HouseRandomTrigger::TileLoop:
697 case HouseRandomTrigger::TileLoopNorth:
704 if (hs->
building_flags.
Any(BUILDING_2_TILES_Y)) DoTriggerHouseRandomisation(
TileAddXY(tile, 0, 1), trigger, random_bits,
false);
705 if (hs->
building_flags.
Any(BUILDING_2_TILES_X)) DoTriggerHouseRandomisation(
TileAddXY(tile, 1, 0), trigger, random_bits,
false);
706 if (hs->
building_flags.
Any(BUILDING_HAS_4_TILES)) DoTriggerHouseRandomisation(
TileAddXY(tile, 1, 1), trigger, random_bits,
false);
713 DoTriggerHouseRandomisation(t, trigger, 0,
true);
726 uint32_t cb_info = random << 16 | (uint8_t)diff.
y << 8 | (uint8_t)diff.
x;
744 if (trigger_cargoes == 0)
return;
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
uint32_t GetGRFID(uint16_t entity_id) const
Gives the GRFID of the file the entity belongs to.
virtual uint16_t GetID(uint16_t grf_local_id, uint32_t grfid) const
Return the ID (if ever available) of a previously inserted entity.
void ResetOverride()
Resets the override, which is used while initializing game.
Generate TileIndices around a center tile or tile area, with increasing distance.
Structure contains cached list of stations nearby.
const StationList & GetStations()
Run a tile loop to find stations around a tile, on demand.
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
static constexpr Owner OWNER_NONE
The tile has no ownership.
static constexpr Owner OWNER_WATER
The tile/execution is done by "water".
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
bool _generating_world
Whether we are generating the map or not.
Functions related to world/map generation.
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
uint32_t PaletteID
The number of the palette.
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
@ SynchronisedCallback1B
synchronized callback 1B will be performed, on multi tile houses
@ Callback1ARandomBits
callback 1A needs random bits
static const HouseID NUM_HOUSES
Total number of houses.
static const HouseID NEW_HOUSE_OFFSET
Offset for new houses.
static const uint8_t TOWN_HOUSE_COMPLETED
Simple value that indicates the house has reached the final stage of construction.
HouseRandomTrigger
Randomisation triggers for houses.
uint16_t HouseClassID
Classes of houses.
uint16_t HouseID
OpenTTD ID of house types.
void DrawFoundation(TileInfo *ti, Foundation f)
Draw foundation f at tile ti.
Functions related to OTTD's landscape.
@ Random
Randomise borders.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
Returns the diff between two tiles.
Function implementations related to NewGRF animation.
uint32_t GetBadgeVariableResult(const GRFFile &grffile, std::span< const BadgeID > badges, uint32_t parameter)
Test for a matching badge in a list of badges, returning the number of matching bits.
Functions related to NewGRF badges.
@ DrawTileLayout
Use callback to select a tile layout to use when drawing.
CallbackID
List of implemented NewGRF callbacks.
@ CBID_HOUSE_ANIMATION_TRIGGER_WATCHED_CARGO_ACCEPTED
Called when a cargo type specified in property 20 is accepted.
@ CBID_HOUSE_ANIMATION_TRIGGER_TILE_LOOP
Called for periodically starting or stopping the animation.
@ CBID_HOUSE_COLOUR
Called to determine the colour of a town building.
@ CBID_HOUSE_DRAW_FOUNDATIONS
Called to determine the type (if any) of foundation to draw for house tile.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
@ CBID_HOUSE_ANIMATION_SPEED
Called to indicate how long the current animation frame should last.
@ CBID_HOUSE_ANIMATION_NEXT_FRAME
Determine the next animation frame for a house.
@ CBID_HOUSE_DENY_DESTRUCTION
Called to determine whether a town building can be destroyed.
@ CBID_RANDOM_TRIGGER
Set when calling a randomizing trigger (almost undocumented).
@ CBID_HOUSE_ANIMATION_TRIGGER_CONSTRUCTION_STAGE_CHANGED
Called whenever the construction stage of a house changes.
@ CBID_HOUSE_DESTRUCTION
Called periodically to determine if a house should be destroyed.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
HouseCallbackMask
Callback masks for houses.
@ Destruction
trigger destruction of building
@ DrawFoundations
decides if default foundations need to be drawn
@ AnimationNextFrame
decides next animation frame
@ DenyDestruction
conditional protection
@ AnimationSpeed
decides animation speed
@ Colour
decide the colour of the building
@ AnimationTriggerConstructionStageChanged
change animation when construction stage changes
@ AnimationTriggerTileLoop
periodically start/stop the animation
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
Cargo support for NewGRFs.
bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t cb_res)
Converts a callback result into a boolean.
uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8)
Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
bool ConvertBooleanCallback(const GRFFile *grffile, uint16_t cbid, uint16_t cb_res)
Converts a callback result into a boolean.
uint32_t GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets, Axis axis)
Get the tile at the given offset.
void DecreaseBuildingCount(Town *t, HouseID house_id)
DecreaseBuildingCount() Decrease the number of a building when it is deleted.
void IncreaseBuildingCount(Town *t, HouseID house_id)
IncreaseBuildingCount() Increase the count of a building when it has been added by a town.
static void DoTriggerHouseAnimation_WatchedCargoAccepted(TileIndex tile, TileIndex origin, CargoTypes trigger_cargoes, uint16_t random)
Run the watched cargo accepted callback for a single house tile.
static void TriggerHouseAnimation_TileLoop(TileIndex tile, bool sync, uint16_t random_bits)
Call the tile loop animation trigger for houses, if enabled.
std::span< const uint > GetBuildingHouseIDCounts()
Get read-only span of total HouseID building counts.
void DrawNewHouseTileInGUI(int x, int y, const HouseSpec *spec, HouseID house_id, int view)
Draw representation of a house tile for GUI purposes.
static uint32_t GetDistanceFromNearbyHouse(uint8_t parameter, TileIndex start_tile, HouseID start_house)
This function will activate a search around a central tile, looking for some houses that fit the requ...
void InitializeBuildingCounts()
Initialise global building counts and all town building counts.
void TriggerHouseAnimation_WatchedCargoAccepted(TileIndex tile, CargoTypes trigger_cargoes)
Run watched cargo accepted callback for a house.
static const GRFFile * GetHouseSpecGrf(HouseID house_id)
Retrieve the grf file associated with a house.
static uint32_t GetNearbyTileInformation(uint8_t parameter, TileIndex tile, bool grf_version8)
Get information about a nearby tile.
Functions related to NewGRF houses.
Functions related to NewGRF provided sounds.
Functions to handle the town part of NewGRF towns.
A number of safeguards to prevent using unsafe methods.
@ FOUNDATION_LEVELED
The tile is leveled up to a flat slope.
static PaletteID GetColourPalette(Colours colour)
Get recolour palette for a colour.
void DrawNewGRFTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, uint32_t stage, PaletteID default_palette)
Draw NewGRF industrytile or house sprite layout.
void DrawNewGRFTileSeqInGUI(int x, int y, const DrawTileSprites *dts, uint32_t stage, PaletteID default_palette)
Draw NewGRF object in GUI.
PaletteID GroundSpritePaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
Applies PALETTE_MODIFIER_COLOUR to a palette entry of a ground sprite.
static constexpr uint8_t SPRITE_MODIFIER_CUSTOM_SPRITE
these masks change the colours of the palette for a sprite.
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Base classes/functions for stations.
Definition of base types and functions in a cross-platform compatible way.
Helper class for a unified approach to NewGRF animation.
static void AnimateTile(const HouseSpec *spec, Town *obj, TileIndex tile, bool random_animation, CargoTypes extra_data=0)
Animate a single tile.
static void ChangeAnimationFrame(CallbackID cb, const HouseSpec *spec, Town *obj, TileIndex tile, uint32_t random_bits, uint32_t trigger, CargoTypes extra_data=0)
Check a callback to determine what the next animation step is and execute that step.
static bool IsValidHumanID(auto index)
Is this company a valid company, not controlled by a NoAI program?
Ground palette sprite of a tile, together with its sprite layout.
PalSpriteID ground
Palette and sprite for the ground.
const struct GRFFile * grffile
grf file that introduced this entity
uint16_t local_id
id defined by the grf file for this entity
uint32_t grfid
grfid that introduced this entity.
bool HasGrfFile() const
Test if this entity was introduced by NewGRF.
Dynamic data of a loaded NewGRF.
Helper class for animation control.
Makes class IDs unique to each GRF file.
uint8_t class_id
The class id within the grf file.
uint32_t grfid
The GRF ID of the file this class belongs to.
Resolver object to be used for houses (feature 07 spritegroups).
HouseResolverObject(HouseID house_id, TileIndex tile, Town *town, CallbackID callback=CBID_NO_CALLBACK, uint32_t param1=0, uint32_t param2=0, bool not_yet_constructed=false, uint8_t initial_random_bits=0, CargoTypes watched_cargo_triggers=0, int view=0)
Construct a resolver for a house.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
int view
View when house does yet exist.
Town * town
Town of this house.
TileIndex tile
Tile of this house.
CargoTypes watched_cargo_triggers
Cargo types that triggered the watched cargo callback.
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
uint16_t initial_random_bits
Random bits during construction checks.
uint32_t GetRandomBits() const override
Get a few random bits.
HouseID house_id
Type of house being queried.
uint32_t GetRandomTriggers() const override
Get the triggers.
bool not_yet_constructed
True for construction check.
SubstituteGRFFileProps grf_prop
Properties related the the grf file.
CargoTypes watched_cargoes
Cargo types watched for acceptance.
static HouseSpec * Get(size_t house_id)
Get the spec for a house ID.
BuildingFlags building_flags
some flags that describe the house (size, stadium etc...)
uint8_t processing_time
Periodic refresh multiplier.
HouseCallbackMasks callback_mask
Bitmask of house callbacks that have to be called.
HouseExtraFlags extra_flags
some more flags
HouseID Index() const
Gets the index of this spec.
HouseClassID class_id
defines the class this house has (not grf file based)
Colours random_colour[4]
4 "random" colours
static std::vector< HouseSpec > & Specs()
Get a reference to all HouseSpecs.
static TileIndex WrapToMap(TileIndex tile)
'Wraps' the given "tile" so it is within the map.
SpriteID sprite
The 'real' sprite.
PaletteID pal
The palette (use PAL_NONE) if not needed)
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
int32_t GetRegister(uint i) const
Gets the value of a so-called newgrf "register".
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
ResolverObject & ro
Surrounding resolver object.
Specialization of ResolverObject with type-safe access to RandomTriggers.
const struct SpriteGroup * GetSpriteGroup(bool entity_exists) const
Get the standard sprite group.
bool HasSpriteGroups() const
Check whether the entity has sprite groups.
A pair-construct of a TileIndexDiff.
int16_t x
The x value of the coordinate.
int16_t y
The y value of the coordinate.
Tile information, used while rendering the tile.
int x
X position of the tile in unit coordinates.
Slope tileh
Slope of the tile.
TileIndex tile
Tile index.
int y
Y position of the tile in unit coordinates.
Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
SpriteLayoutProcessor ProcessRegisters(const ResolverObject &object, uint8_t *stage) const
Process registers and the construction stage into the sprite layout.
BuildingCounts< uint16_t > building_counts
The number of each type of building in the town.
TownCache cache
Container for all cacheable data.
uint8_t GetAnimationFrame(Tile t)
Get the current animation frame.
bool IsValidTile(Tile tile)
Checks if a tile is valid.
uint TileHash2Bit(uint x, uint y)
Get the last two bits of the TileHash from a tile position.
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
@ MP_HOUSE
A house by a town.
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
TileIndexDiff GetHouseNorthPart(HouseID &house)
Determines if a given HouseID is part of a multitile house.
void ClearTownHouse(Town *t, TileIndex tile)
Clear a town house.
HouseZone GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
HouseID GetHouseType(Tile t)
Get the type of this house, which is an index into the house spec array.
void SetHouseRandomBits(Tile t, uint8_t random)
Set the random bits for this house.
uint8_t GetHouseRandomBits(Tile t)
Get the random bits for this house.
TimerGameCalendar::Year GetHouseAge(Tile t)
Get the age of the house.
void DecHouseProcessingTime(Tile t)
Decrease the amount of time remaining before the tile loop processes this tile.
uint8_t GetHouseBuildingStage(Tile t)
House Construction Scheme.
void SetHouseRandomTriggers(Tile t, HouseRandomTriggers triggers)
Set the activated triggers bits for this house.
HouseRandomTriggers GetHouseRandomTriggers(Tile t)
Get the already activated triggers bits for this house.
void SetHouseProcessingTime(Tile t, uint8_t time)
Set the amount of time remaining before the tile loop processes this tile.
bool IsHouseProtected(Tile t)
Check if the house is protected from removal by towns.
uint8_t GetHouseProcessingTime(Tile t)
Get the amount of time remaining before the tile loop processes this tile.
@ TO_HOUSES
town buildings
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.