/* * This file is part of OpenTTD. * 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. * 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. * 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 . */ /** @file newgrf_house.cpp Implementation of NewGRF houses. */ #include "stdafx.h" #include "debug.h" #include "landscape.h" #include "newgrf_house.h" #include "newgrf_spritegroup.h" #include "newgrf_text.h" #include "newgrf_town.h" #include "newgrf_sound.h" #include "company_func.h" #include "company_base.h" #include "town.h" #include "genworld.h" #include "newgrf_animation_base.h" #include "newgrf_cargo.h" #include "station_base.h" #include "newgrf_analysis.h" #include "newgrf_extension.h" #include "safeguards.h" #include "table/strings.h" static BuildingCounts _building_counts{}; static std::vector _class_mapping{}; HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, NUM_HOUSES, INVALID_HOUSE_ID); /** * Retrieve the grf file associated with a house. * @param house_id House to query. * @return The associated GRF file (may be \c nullptr). */ static const GRFFile *GetHouseSpecGrf(HouseID house_id) { const HouseSpec *hs = HouseSpec::Get(house_id); return (hs != nullptr) ? hs->grf_prop.grffile : nullptr; } extern const HouseSpec _original_house_specs[NEW_HOUSE_OFFSET]; std::vector _house_specs; /** * Get a reference to all HouseSpecs. * @return Reference to vector of all HouseSpecs. */ std::vector &HouseSpec::Specs() { return _house_specs; } /** * Gets the index of this spec. * @return The index. */ HouseID HouseSpec::Index() const { return static_cast(this - _house_specs.data()); } /** * Get the spec for a house ID. * @param house_id The ID of the house. * @return The HouseSpec associated with the ID. */ HouseSpec *HouseSpec::Get(size_t house_id) { /* Empty house if index is out of range -- this might happen if NewGRFs are changed. */ static HouseSpec empty = {}; assert(house_id < NUM_HOUSES); if (house_id >= _house_specs.size()) return ∅ return &_house_specs[house_id]; } /* Reset and initialise house specs. */ void ResetHouses() { _house_specs.clear(); _house_specs.reserve(std::size(_original_house_specs)); ResetHouseClassIDs(); /* Copy default houses. */ _house_specs.insert(std::end(_house_specs), std::begin(_original_house_specs), std::end(_original_house_specs)); /* Reset any overrides that have been set. */ _house_mngr.ResetOverride(); } /** * Construct a resolver for a house. * @param house_id House to query. * @param tile %Tile containing the house. * @param town %Town containing the house. * @param callback Callback ID. * @param param1 First parameter (var 10) of the callback. * @param param2 Second parameter (var 18) of the callback. * @param not_yet_constructed House is still under construction. * @param initial_random_bits Random bits during construction checks. * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback. */ HouseResolverObject::HouseResolverObject(HouseID house_id, TileIndex tile, Town *town, CallbackID callback, uint32_t param1, uint32_t param2, bool not_yet_constructed, uint8_t initial_random_bits, CargoTypes watched_cargo_triggers) : ResolverObject(GetHouseSpecGrf(house_id), callback, param1, param2), house_scope(*this, house_id, tile, town, not_yet_constructed, initial_random_bits, watched_cargo_triggers), town_scope(*this, town, not_yet_constructed) // Don't access StorePSA if house is not yet constructed. { this->root_spritegroup = HouseSpec::Get(house_id)->grf_prop.spritegroup[0]; } GrfSpecFeature HouseResolverObject::GetFeature() const { return GSF_HOUSES; } uint32_t HouseResolverObject::GetDebugID() const { return HouseSpec::Get(this->house_scope.house_id)->grf_prop.local_id; } /** * Construct a resolver for a fake house. * @param house_id House to query. * @param callback Callback ID. * @param param1 First parameter (var 10) of the callback. * @param param2 Second parameter (var 18) of the callback. * @param not_yet_constructed House is still under construction. * @param initial_random_bits Random bits during construction checks. * @param watched_cargo_triggers Cargo types that triggered the watched cargo callback. */ FakeHouseResolverObject::FakeHouseResolverObject(HouseID house_id, CallbackID callback, uint32_t param1, uint32_t param2) : ResolverObject(GetHouseSpecGrf(house_id), callback, param1, param2), house_scope(*this, house_id), town_scope(*this) // Don't access StorePSA if house is not yet constructed. { this->root_spritegroup = HouseSpec::Get(house_id)->grf_prop.spritegroup[0]; } GrfSpecFeature FakeHouseResolverObject::GetFeature() const { return GSF_HOUSES; } uint32_t FakeHouseResolverObject::GetDebugID() const { return HouseSpec::Get(this->house_scope.house_id)->grf_prop.local_id; } void ResetHouseClassIDs() { _class_mapping.clear(); /* Add initial entry for HOUSE_NO_CLASS. */ _class_mapping.emplace_back(); } HouseClassID AllocateHouseClassID(uint8_t grf_class_id, uint32_t grfid) { /* Start from 1 because 0 means that no class has been assigned. */ 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; }); /* HouseClass not found, allocate a new one. */ if (it == std::end(_class_mapping)) it = _class_mapping.insert(it, {.grfid = grfid, .class_id = grf_class_id}); return static_cast(std::distance(std::begin(_class_mapping), it)); } /** * Initialise building counts for a town. * @param t Town cache to initialise. */ void InitializeBuildingCounts(Town *t) { t->cache.building_counts.id_count.clear(); t->cache.building_counts.class_count.clear(); t->cache.building_counts.id_count.resize(HouseSpec::Specs().size()); t->cache.building_counts.class_count.resize(_class_mapping.size()); } /** * Initialise global building counts and all town building counts. */ void InitializeBuildingCounts() { _building_counts.id_count.clear(); _building_counts.class_count.clear(); _building_counts.id_count.resize(HouseSpec::Specs().size()); _building_counts.class_count.resize(_class_mapping.size()); for (Town *t : Town::Iterate()) { InitializeBuildingCounts(t); } } /** * IncreaseBuildingCount() * Increase the count of a building when it has been added by a town. * @param t The town that the building is being built in * @param house_id The id of the house being added */ void IncreaseBuildingCount(Town *t, HouseID house_id) { HouseClassID class_id = HouseSpec::Get(house_id)->class_id; t->cache.building_counts.id_count[house_id]++; _building_counts.id_count[house_id]++; if (class_id == HOUSE_NO_CLASS) return; t->cache.building_counts.class_count[class_id]++; _building_counts.class_count[class_id]++; } /** * DecreaseBuildingCount() * Decrease the number of a building when it is deleted. * @param t The town that the building was built in * @param house_id The id of the house being removed */ void DecreaseBuildingCount(Town *t, HouseID house_id) { HouseClassID class_id = HouseSpec::Get(house_id)->class_id; if (t->cache.building_counts.id_count[house_id] > 0) t->cache.building_counts.id_count[house_id]--; if (_building_counts.id_count[house_id] > 0) _building_counts.id_count[house_id]--; if (class_id == HOUSE_NO_CLASS) return; if (t->cache.building_counts.class_count[class_id] > 0) t->cache.building_counts.class_count[class_id]--; if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--; } /* virtual */ uint32_t HouseScopeResolver::GetRandomBits() const { /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */ assert_tile(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)), this->tile); return this->not_yet_constructed ? this->initial_random_bits : GetHouseRandomBits(this->tile); } /* virtual */ uint32_t HouseScopeResolver::GetTriggers() const { /* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */ assert_tile(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)), this->tile); return this->not_yet_constructed ? 0 : GetHouseTriggers(this->tile); } static uint32_t GetNumHouses(HouseID house_id, const Town *town) { HouseClassID class_id = HouseSpec::Get(house_id)->class_id; uint8_t map_id_count = ClampTo(_building_counts.id_count[house_id]); uint8_t map_class_count = ClampTo(_building_counts.class_count[class_id]); uint8_t town_id_count = ClampTo(town->cache.building_counts.id_count[house_id]); uint8_t town_class_count = ClampTo(town->cache.building_counts.class_count[class_id]); return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count; } /** * Get information about a nearby tile. * @param parameter from callback. It's in fact a pair of coordinates * @param tile TileIndex from which the callback was initiated * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8. * @return a construction of bits obeying the newgrf format */ static uint32_t GetNearbyTileInformation(uint8_t parameter, TileIndex tile, bool grf_version8, uint32_t mask) { tile = GetNearbyTile(parameter, tile); return GetNearbyTileInformation(tile, grf_version8, mask); } /** Structure with user-data for SearchNearbyHouseXXX - functions */ struct SearchNearbyHouseData { const HouseSpec *hs; ///< Specs of the house that started the search. TileIndex north_tile; ///< Northern tile of the house. }; /** * Callback function to search a house by its HouseID * @param tile TileIndex to be examined * @param user_data SearchNearbyHouseData * @return true or false, if found or not */ static bool SearchNearbyHouseID(TileIndex tile, void *user_data) { if (IsTileType(tile, MP_HOUSE)) { HouseID house = GetHouseType(tile); // tile been examined const HouseSpec *hs = HouseSpec::Get(house); if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data; TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'! if (north_tile == nbhd->north_tile) return false; // Always ignore origin house return hs->grf_prop.local_id == nbhd->hs->grf_prop.local_id && // same local id as the one requested hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf } } return false; } /** * Callback function to search a house by its classID * @param tile TileIndex to be examined * @param user_data SearchNearbyHouseData * @return true or false, if found or not */ static bool SearchNearbyHouseClass(TileIndex tile, void *user_data) { if (IsTileType(tile, MP_HOUSE)) { HouseID house = GetHouseType(tile); // tile been examined const HouseSpec *hs = HouseSpec::Get(house); if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data; TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'! if (north_tile == nbhd->north_tile) return false; // Always ignore origin house return hs->class_id == nbhd->hs->class_id && // same classid as the one requested hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf } } return false; } /** * Callback function to search a house by its grfID * @param tile TileIndex to be examined * @param user_data SearchNearbyHouseData * @return true or false, if found or not */ static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data) { if (IsTileType(tile, MP_HOUSE)) { HouseID house = GetHouseType(tile); // tile been examined const HouseSpec *hs = HouseSpec::Get(house); if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file SearchNearbyHouseData *nbhd = (SearchNearbyHouseData *)user_data; TileIndex north_tile = tile + GetHouseNorthPart(house); // modifies 'house'! if (north_tile == nbhd->north_tile) return false; // Always ignore origin house return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf } } return false; } /** * This function will activate a search around a central tile, looking for some houses * that fit the requested characteristics * @param parameter that is given by the callback. * bits 0..6 radius of the search * bits 7..8 search type i.e.: 0 = houseID/ 1 = classID/ 2 = grfID * @param tile TileIndex from which to start the search * @param house the HouseID that is associated to the house, the callback is called for * @return the Manhattan distance from the center tile, if any, and 0 if failure */ static uint32_t GetDistanceFromNearbyHouse(uint8_t parameter, TileIndex tile, HouseID house) { static TestTileOnSearchProc * const search_procs[3] = { SearchNearbyHouseID, SearchNearbyHouseClass, SearchNearbyHouseGRFID, }; TileIndex found_tile = tile; uint8_t searchtype = GB(parameter, 6, 2); uint8_t searchradius = GB(parameter, 0, 6); if (searchtype >= lengthof(search_procs)) return 0; // do not run on ill-defined code if (searchradius < 1) return 0; // do not use a too low radius SearchNearbyHouseData nbhd; nbhd.hs = HouseSpec::Get(house); nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'! /* Use a pointer for the tile to start the search. Will be required for calculating the distance*/ if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) { return DistanceManhattan(found_tile, tile); } return 0; } HouseID HouseScopeResolver::GetOtherHouseID(uint32_t parameter) const { const HouseSpec *hs = HouseSpec::Get(this->house_id); if (hs->grf_prop.grffile == nullptr) return INVALID_HOUSE_ID; return _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid); } template uint32_t HouseScopeResolver::OtherHouseIDVariable(uint32_t parameter, F func) const { HouseID new_house = this->GetOtherHouseID(parameter); return new_house == INVALID_HOUSE_ID ? 0 : func(new_house); } /** * @note Used by the resolver to get values for feature 07 deterministic spritegroups. */ /* virtual */ uint32_t HouseScopeResolver::GetVariable(uint16_t variable, uint32_t parameter, GetVariableExtra *extra) const { switch (variable) { /* Construction stage. */ case 0x40: return (IsTileType(this->tile, MP_HOUSE) ? GetHouseBuildingStage(this->tile) : 0) | TileHash2Bit(TileX(this->tile), TileY(this->tile)) << 2; /* Building age. */ case 0x41: return IsTileType(this->tile, MP_HOUSE) ? GetHouseAge(this->tile).base() : 0; /* Town zone */ case 0x42: return GetTownRadiusGroup(this->town, this->tile); /* Terrain type */ case 0x43: return GetTerrainType(this->tile); /* Number of this type of building on the map. */ case 0x44: return GetNumHouses(this->house_id, this->town); case A2VRI_HOUSE_SAME_ID_MAP_COUNT: return _building_counts.id_count[this->house_id]; case A2VRI_HOUSE_SAME_CLASS_MAP_COUNT: return _building_counts.class_count[HouseSpec::Get(this->house_id)->class_id]; case A2VRI_HOUSE_SAME_ID_TOWN_COUNT: return this->town->cache.building_counts.id_count[this->house_id]; case A2VRI_HOUSE_SAME_CLASS_TOWN_COUNT: return this->town->cache.building_counts.class_count[HouseSpec::Get(this->house_id)->class_id]; /* Whether the town is being created or just expanded. */ case 0x45: return _generating_world ? 1 : 0; /* Current animation frame. */ case 0x46: return IsTileType(this->tile, MP_HOUSE) ? GetAnimationFrame(this->tile) : 0; /* Position of the house */ case 0x47: return TileY(this->tile) << 16 | (TileX(this->tile) & 0xFFFF); /* Building counts for old houses with id = parameter. */ case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, this->town) : 0; case A2VRI_HOUSE_OTHER_OLD_ID_MAP_COUNT: return parameter < NEW_HOUSE_OFFSET ? _building_counts.id_count[parameter] : 0; case A2VRI_HOUSE_OTHER_OLD_ID_TOWN_COUNT: return parameter < NEW_HOUSE_OFFSET ? this->town->cache.building_counts.id_count[parameter] : 0; /* Building counts for new houses with id = parameter. */ case 0x61: return this->OtherHouseIDVariable(parameter, [&](HouseID new_house) { return GetNumHouses(new_house, this->town); }); case A2VRI_HOUSE_OTHER_ID_MAP_COUNT: return this->OtherHouseIDVariable(parameter, [&](HouseID new_house) { return _building_counts.id_count[new_house]; }); case A2VRI_HOUSE_OTHER_CLASS_MAP_COUNT: return this->OtherHouseIDVariable(parameter, [&](HouseID new_house) { return _building_counts.class_count[HouseSpec::Get(new_house)->class_id]; }); case A2VRI_HOUSE_OTHER_ID_TOWN_COUNT: return this->OtherHouseIDVariable(parameter, [&](HouseID new_house) { return this->town->cache.building_counts.id_count[new_house]; }); case A2VRI_HOUSE_OTHER_CLASS_TOWN_COUNT: return this->OtherHouseIDVariable(parameter, [&](HouseID new_house) { return this->town->cache.building_counts.class_count[HouseSpec::Get(new_house)->class_id]; }); /* Land info for nearby tiles. */ case 0x62: return GetNearbyTileInformation(parameter, this->tile, this->ro.grffile->grf_version >= 8, extra->mask); /* Current animation frame of nearby house tiles */ case 0x63: { TileIndex testtile = GetNearbyTile(parameter, this->tile); return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0; } /* Cargo acceptance history of nearby stations */ case 0x64: { CargoID cid = GetCargoTranslation(parameter, this->ro.grffile); if (cid == INVALID_CARGO) return 0; /* Extract tile offset. */ int8_t x_offs = GB(GetRegister(0x100), 0, 8); int8_t y_offs = GB(GetRegister(0x100), 8, 8); TileIndex testtile = TILE_MASK(this->tile + TileDiffXY(x_offs, y_offs)); StationFinder stations(TileArea(testtile, 1, 1)); const StationList *sl = stations.GetStations(); /* Collect acceptance stats. */ uint32_t res = 0; for (Station *st : *sl) { if (HasBit(st->goods[cid].status, GoodsEntry::GES_EVER_ACCEPTED)) SetBit(res, 0); if (HasBit(st->goods[cid].status, GoodsEntry::GES_LAST_MONTH)) SetBit(res, 1); if (HasBit(st->goods[cid].status, GoodsEntry::GES_CURRENT_MONTH)) SetBit(res, 2); if (HasBit(st->goods[cid].status, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(res, 3); } /* Cargo triggered CB 148? */ if (HasBit(this->watched_cargo_triggers, cid)) SetBit(res, 4); return res; } /* Distance test for some house types */ case 0x65: return GetDistanceFromNearbyHouse(parameter, this->tile, this->house_id); /* Class and ID of nearby house tile */ case 0x66: { TileIndex testtile = GetNearbyTile(parameter, this->tile); if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF; HouseID nearby_house_id = GetHouseType(testtile); HouseSpec *hs = HouseSpec::Get(nearby_house_id); /* Information about the grf local classid if the house has a class */ uint houseclass = 0; if (hs->class_id != HOUSE_NO_CLASS) { houseclass = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8; houseclass |= _class_mapping[hs->class_id].class_id; } /* old house type or grf-local houseid */ uint local_houseid = 0; if (nearby_house_id < NEW_HOUSE_OFFSET) { local_houseid = nearby_house_id; } else { local_houseid = (hs->grf_prop.grffile == this->ro.grffile ? 1 : 2) << 8; local_houseid |= hs->grf_prop.local_id; } return houseclass << 16 | local_houseid; } /* GRFID of nearby house tile */ case 0x67: { TileIndex testtile = GetNearbyTile(parameter, this->tile); if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF; HouseID house_id = GetHouseType(testtile); if (house_id < NEW_HOUSE_OFFSET) return 0; /* Checking the grffile information via HouseSpec doesn't work * in case the newgrf was removed. */ return _house_mngr.GetGRFID(house_id); } } DEBUG(grf, 1, "Unhandled house variable 0x%X", variable); extra->available = false; return UINT_MAX; } /** * @note Used by the resolver to get values for feature 07 deterministic spritegroups. */ /* virtual */ uint32_t FakeHouseScopeResolver::GetVariable(uint16_t variable, uint32_t parameter, GetVariableExtra *extra) const { switch (variable) { /* Construction stage. */ case 0x40: return TOWN_HOUSE_COMPLETED; /* Building age. */ case 0x41: return 0; /* Town zone */ case 0x42: return FindFirstBit(HouseSpec::Get(this->house_id)->building_availability & HZ_ZONALL); // first available /* Terrain type */ case 0x43: return _settings_game.game_creation.landscape == LT_ARCTIC && (HouseSpec::Get(house_id)->building_availability & (HZ_SUBARTC_ABOVE | HZ_SUBARTC_BELOW)) == HZ_SUBARTC_ABOVE ? 4 : 0; /* Number of this type of building on the map. */ case 0x44: return 0; /* Whether the town is being created or just expanded. */ case 0x45: return 0; /* Current animation frame. */ case 0x46: return 0; /* Position of the house */ case 0x47: return 0xFFFFFFFF; /* Building counts for old houses with id = parameter. */ case 0x60: return 0; /* Building counts for new houses with id = parameter. */ case 0x61: return 0; /* Land info for nearby tiles. */ case 0x62: return 0; /* Current animation frame of nearby house tiles */ case 0x63: return 0; /* Cargo acceptance history of nearby stations */ case 0x64: return 0; /* Distance test for some house types */ case 0x65: return 0; /* Class and ID of nearby house tile */ case 0x66: return 0xFFFFFFFF; /* GRFID of nearby house tile */ case 0x67: return 0xFFFFFFFF; } DEBUG(grf, 1, "Unhandled house variable 0x%X", variable); extra->available = false; return UINT_MAX; } uint16_t GetHouseCallback(CallbackID callback, uint32_t param1, uint32_t param2, HouseID house_id, Town *town, TileIndex tile, bool not_yet_constructed, uint8_t initial_random_bits, CargoTypes watched_cargo_triggers) { if (tile != INVALID_TILE) { HouseResolverObject object(house_id, tile, town, callback, param1, param2, not_yet_constructed, initial_random_bits, watched_cargo_triggers); return object.ResolveCallback(); } else { FakeHouseResolverObject object(house_id, callback, param1, param2); return object.ResolveCallback(); } } /** * Get the name of a house. * @param house House type. * @param tile Tile where the house is located. INVALID_TILE to get the general name of houses of the given type. * @return Name of the house. */ StringID GetHouseName(HouseID house_id, TileIndex tile) { const HouseSpec *hs = HouseSpec::Get(house_id); bool house_completed = (tile == INVALID_TILE) || IsHouseCompleted(tile); Town *t = (tile == INVALID_TILE) ? nullptr : Town::GetByTile(tile); uint16_t callback_res = GetHouseCallback(CBID_HOUSE_CUSTOM_NAME, house_completed ? 1 : 0, 0, house_id, t, tile); if (callback_res != CALLBACK_FAILED && callback_res != 0x400) { if (callback_res > 0x400) { ErrorUnknownCallbackResult(hs->grf_prop.grffile->grfid, CBID_HOUSE_CUSTOM_NAME, callback_res); } else { StringID ret = GetGRFStringID(hs->grf_prop.grffile->grfid, 0xD000 + callback_res); if (ret != STR_NULL && ret != STR_UNDEFINED) return ret; } } return hs->building_name; } static inline PaletteID GetHouseColour(HouseID house_id, TileIndex tile = INVALID_TILE) { const HouseSpec *hs = HouseSpec::Get(house_id); if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) { Town *t = (tile != INVALID_TILE) ? Town::GetByTile(tile) : nullptr; uint16_t callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, t, tile); if (callback != CALLBACK_FAILED) { /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */ return HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback; } } return GENERAL_SPRITE_COLOUR(hs->random_colour[TileHash2Bit(TileX(tile), TileY(tile))]); } static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, uint8_t stage, HouseID house_id) { const DrawTileSprites *dts = group->ProcessRegisters(&stage); PaletteID palette = GetHouseColour(house_id, ti->tile); SpriteID image = dts->ground.sprite; PaletteID pal = dts->ground.pal; if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage; if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage; if (GB(image, 0, SPRITE_WIDTH) != 0) { DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette)); } DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette); } static void DrawTileLayoutInGUI(int x, int y, const TileLayoutSpriteGroup *group, HouseID house_id, bool ground) { uint8_t stage = TOWN_HOUSE_COMPLETED; const DrawTileSprites *dts = group->ProcessRegisters(&stage); PaletteID palette = GetHouseColour(house_id); if (ground) { PalSpriteID image = dts->ground; if (HasBit(image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) image.sprite += stage; if (HasBit(image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) image.pal += stage; if (GB(image.sprite, 0, SPRITE_WIDTH) != 0) { DrawSprite(image.sprite, GroundSpritePaletteTransform(image.sprite, image.pal, palette), x, y); } } else { DrawNewGRFTileSeqInGUI(x, y, dts, stage, palette); } } void DrawNewHouseTile(TileInfo *ti, HouseID house_id) { const HouseSpec *hs = HouseSpec::Get(house_id); if (ti->tileh != SLOPE_FLAT) { bool draw_old_one = true; if (HasBit(hs->callback_mask, CBM_HOUSE_DRAW_FOUNDATIONS)) { /* Called to determine the type (if any) of foundation to draw for the house tile */ uint32_t callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile); if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res); } if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED); } HouseResolverObject object(house_id, ti->tile, Town::GetByTile(ti->tile)); const SpriteGroup *group = object.Resolve(); if (group != nullptr && group->type == SGT_TILELAYOUT) { /* Limit the building stage to the number of stages supplied. */ const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group; uint8_t stage = GetHouseBuildingStage(ti->tile); DrawTileLayout(ti, tlgroup, stage, house_id); } } void DrawNewHouseTileInGUI(int x, int y, HouseID house_id, bool ground) { FakeHouseResolverObject object(house_id); const SpriteGroup *group = object.Resolve(); if (group != nullptr && group->type == SGT_TILELAYOUT) { DrawTileLayoutInGUI(x, y, (const TileLayoutSpriteGroup*)group, house_id, ground); } } /* Simple wrapper for GetHouseCallback to keep the animation unified. */ uint16_t GetSimpleHouseCallback(CallbackID callback, uint32_t param1, uint32_t param2, const HouseSpec *spec, Town *town, TileIndex tile, CargoTypes extra_data) { return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data); } /** Helper class for animation control. */ struct HouseAnimationBase : public AnimationBase > { static const CallbackID cb_animation_speed = CBID_HOUSE_ANIMATION_SPEED; static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME; static const HouseCallbackMask cbm_animation_speed = CBM_HOUSE_ANIMATION_SPEED; static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME; }; void AnimateNewHouseTile(TileIndex tile) { const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile)); if (hs == nullptr) return; HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS)); } void AnimateNewHouseConstruction(TileIndex tile) { const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile)); if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) { HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, hs, Town::GetByTile(tile), tile, 0, 0); } } uint8_t GetNewHouseTileAnimationSpeed(TileIndex tile) { const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile)); if (hs == nullptr) return 0; return HouseAnimationBase::GetAnimationSpeed(hs); } /** * Check if GRF allows a given house to be constructed (callback 17) * @param house_id house type * @param tile tile where the house is about to be placed * @param t town in which we are building * @param random_bits feature random bits for the house * @return false if callback 17 disallows construction, true in other cases */ bool HouseAllowsConstruction(HouseID house_id, TileIndex tile, Town *t, uint8_t random_bits) { const HouseSpec *hs = HouseSpec::Get(house_id); if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) { uint16_t callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house_id, t, tile, true, random_bits); if (callback_res != CALLBACK_FAILED && !Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_ALLOW_CONSTRUCTION, callback_res)) { return false; } } return true; } bool CanDeleteHouse(TileIndex tile) { const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile)); /* Humans are always allowed to remove buildings, as is water and disasters and * anyone using the scenario editor. */ if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE || _game_mode == GM_EDITOR || _generating_world) { return true; } if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) { uint16_t callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile); return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res)); } else { return !(hs->extra_flags & BUILDING_IS_PROTECTED); } } static void AnimationControl(TileIndex tile, uint16_t random_bits) { const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile)); if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) { uint32_t param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random(); HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0); } } bool NewHouseTileLoop(TileIndex tile) { const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile)); if (GetHouseProcessingTime(tile) > 0) { DecHouseProcessingTime(tile); return true; } bool do_triggers = !(hs->ctrl_flags & HCF_NO_TRIGGERS); if (do_triggers) { TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP); if (hs->building_flags & BUILDING_HAS_1_TILE) TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP); } if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) { /* If this house is marked as having a synchronised callback, all the * tiles will have the callback called at once, rather than when the * tile loop reaches them. This should only be enabled for the northern * tile, or strange things will happen (here, and in TTDPatch). */ if (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) { uint16_t random = GB(Random(), 0, 16); if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random); if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TileAddXY(tile, 0, 1), random); if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TileAddXY(tile, 1, 0), random); if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TileAddXY(tile, 1, 1), random); } else { AnimationControl(tile, 0); } } /* Check callback 21, which determines if a house should be destroyed. */ if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) { Town *t = Town::GetByTile(tile); uint16_t callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), t, tile); if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) { ClearTownHouse(t, tile); return false; } } SetHouseProcessingTime(tile, hs->processing_time); if (do_triggers) MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE); return true; } static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, uint8_t base_random, bool first) { /* We can't trigger a non-existent building... */ assert_tile(IsTileType(tile, MP_HOUSE), tile); HouseID hid = GetHouseType(tile); HouseSpec *hs = HouseSpec::Get(hid); if (hs->grf_prop.spritegroup[0] == nullptr) return; HouseResolverObject object(hid, tile, Town::GetByTile(tile), CBID_RANDOM_TRIGGER); object.waiting_triggers = GetHouseTriggers(tile) | trigger; SetHouseTriggers(tile, object.waiting_triggers); // store now for var 5F const SpriteGroup *group = object.Resolve(); if (group == nullptr) return; /* Store remaining triggers. */ SetHouseTriggers(tile, object.GetRemainingTriggers()); /* Rerandomise bits. Scopes other than SELF are invalid for houses. For bug-to-bug-compatibility with TTDP we ignore the scope. */ uint8_t new_random_bits = Random(); uint8_t random_bits = GetHouseRandomBits(tile); uint32_t reseed = object.GetReseedSum(); random_bits &= ~reseed; random_bits |= (first ? new_random_bits : base_random) & reseed; SetHouseRandomBits(tile, random_bits); switch (trigger) { case HOUSE_TRIGGER_TILE_LOOP: /* Random value already set. */ break; case HOUSE_TRIGGER_TILE_LOOP_TOP: if (!first) { /* The top tile is marked dirty by the usual TileLoop */ MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE); break; } /* Random value of first tile already set. */ if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TileAddXY(tile, 0, 1), trigger, random_bits, false); if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TileAddXY(tile, 1, 0), trigger, random_bits, false); if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TileAddXY(tile, 1, 1), trigger, random_bits, false); break; } } void TriggerHouse(TileIndex t, HouseTrigger trigger) { DoTriggerHouse(t, trigger, 0, true); } /** * Run the watched cargo accepted callback for a single house tile. * @param tile The house tile. * @param origin The triggering tile. * @param trigger_cargoes Cargo types that triggered the callback. * @param random Random bits. */ void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, CargoTypes trigger_cargoes, uint16_t random) { TileIndexDiffC diff = TileIndexToTileIndexDiffC(origin, tile); uint32_t cb_info = random << 16 | (uint8_t)diff.y << 8 | (uint8_t)diff.x; HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_WATCHED_CARGO_ACCEPTED, HouseSpec::Get(GetHouseType(tile)), Town::GetByTile(tile), tile, 0, cb_info, trigger_cargoes); } /** * Run watched cargo accepted callback for a house. * @param tile House tile. * @param trigger_cargoes Triggering cargo types. * @pre IsTileType(t, MP_HOUSE) */ void WatchedCargoCallback(TileIndex tile, CargoTypes trigger_cargoes) { assert_tile(IsTileType(tile, MP_HOUSE), tile); HouseID id = GetHouseType(tile); const HouseSpec *hs = HouseSpec::Get(id); trigger_cargoes &= hs->watched_cargoes; /* None of the trigger cargoes is watched? */ if (trigger_cargoes == 0) return; /* Same random value for all tiles of a multi-tile house. */ uint16_t r = Random(); /* Do the callback, start at northern tile. */ TileIndex north = tile + GetHouseNorthPart(id); hs = HouseSpec::Get(id); DoWatchedCargoCallback(north, tile, trigger_cargoes, r); if (hs->building_flags & BUILDING_2_TILES_Y) DoWatchedCargoCallback(TileAddXY(north, 0, 1), tile, trigger_cargoes, r); if (hs->building_flags & BUILDING_2_TILES_X) DoWatchedCargoCallback(TileAddXY(north, 1, 0), tile, trigger_cargoes, r); if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TileAddXY(north, 1, 1), tile, trigger_cargoes, r); } void AnalyseHouseSpriteGroups() { for (uint i = 0; i < NUM_HOUSES; i++) { HouseSpec *spec = HouseSpec::Get(i); spec->ctrl_flags = HCF_NONE; if (spec->grf_prop.spritegroup[0] == nullptr) { spec->ctrl_flags |= HCF_NO_TRIGGERS; continue; } AnalyseCallbackOperation find_triggers_op(ACOM_FIND_RANDOM_TRIGGER); spec->grf_prop.spritegroup[0]->AnalyseCallbacks(find_triggers_op); if ((find_triggers_op.callbacks_used & SGCU_RANDOM_TRIGGER) == 0) { spec->ctrl_flags |= HCF_NO_TRIGGERS; } } }