2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_house.cpp Implementation of NewGRF houses. */
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "debug.h"
|
2007-03-20 13:47:00 +00:00
|
|
|
#include "landscape.h"
|
2007-03-19 11:27:30 +00:00
|
|
|
#include "newgrf_house.h"
|
|
|
|
#include "newgrf_spritegroup.h"
|
2015-08-03 20:29:03 +00:00
|
|
|
#include "newgrf_text.h"
|
2007-03-19 11:27:30 +00:00
|
|
|
#include "newgrf_town.h"
|
|
|
|
#include "newgrf_sound.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_base.h"
|
2009-06-26 13:44:14 +00:00
|
|
|
#include "town.h"
|
2010-07-19 17:17:36 +00:00
|
|
|
#include "genworld.h"
|
2010-08-26 17:01:17 +00:00
|
|
|
#include "newgrf_animation_base.h"
|
2011-10-31 22:31:31 +00:00
|
|
|
#include "newgrf_cargo.h"
|
|
|
|
#include "station_base.h"
|
2022-11-24 02:38:10 +00:00
|
|
|
#include "newgrf_analysis.h"
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2015-08-03 20:29:03 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
static BuildingCounts<uint32_t> _building_counts;
|
2023-09-10 17:55:37 +00:00
|
|
|
static std::array<HouseClassMapping, HOUSE_CLASS_MAX> _class_mapping;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2013-10-12 16:33:19 +00:00
|
|
|
HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, NUM_HOUSES, INVALID_HOUSE_ID);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2012-11-10 20:40:05 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the grf file associated with a house.
|
|
|
|
* @param house_id House to query.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return The associated GRF file (may be \c nullptr).
|
2012-11-10 20:40:05 +00:00
|
|
|
*/
|
|
|
|
static const GRFFile *GetHouseSpecGrf(HouseID house_id)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = HouseSpec::Get(house_id);
|
2019-04-10 21:07:06 +00:00
|
|
|
return (hs != nullptr) ? hs->grf_prop.grffile : nullptr;
|
2012-11-10 20:40:05 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
2016-05-12 21:35:39 +00:00
|
|
|
* Construct a resolver for a house.
|
2012-11-10 20:46:39 +00:00
|
|
|
* @param house_id House to query.
|
2020-02-03 18:31:03 +00:00
|
|
|
* @param tile %Tile containing the house.
|
2016-05-12 21:35:39 +00:00
|
|
|
* @param town %Town containing the house.
|
2012-11-10 20:46:39 +00:00
|
|
|
* @param callback Callback ID.
|
|
|
|
* @param param1 First parameter (var 10) of the callback.
|
|
|
|
* @param param2 Second parameter (var 18) of the callback.
|
2016-05-12 21:35:39 +00:00
|
|
|
* @param not_yet_constructed House is still under construction.
|
2012-11-10 20:46:39 +00:00
|
|
|
* @param initial_random_bits Random bits during construction checks.
|
|
|
|
* @param watched_cargo_triggers Cargo types that triggered the watched cargo callback.
|
|
|
|
*/
|
2012-11-10 20:40:05 +00:00
|
|
|
HouseResolverObject::HouseResolverObject(HouseID house_id, TileIndex tile, Town *town,
|
2024-01-07 16:41:53 +00:00
|
|
|
CallbackID callback, uint32_t param1, uint32_t param2,
|
|
|
|
bool not_yet_constructed, uint8_t initial_random_bits, CargoTypes watched_cargo_triggers)
|
2020-02-03 18:31:03 +00:00
|
|
|
: 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.
|
2012-11-10 20:40:05 +00:00
|
|
|
{
|
2020-02-03 18:31:03 +00:00
|
|
|
this->root_spritegroup = HouseSpec::Get(house_id)->grf_prop.spritegroup[0];
|
|
|
|
}
|
2015-08-03 20:29:03 +00:00
|
|
|
|
2020-02-03 18:31:03 +00:00
|
|
|
GrfSpecFeature HouseResolverObject::GetFeature() const
|
|
|
|
{
|
|
|
|
return GSF_HOUSES;
|
|
|
|
}
|
2015-08-03 20:29:03 +00:00
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t HouseResolverObject::GetDebugID() const
|
2020-02-03 18:31:03 +00:00
|
|
|
{
|
|
|
|
return HouseSpec::Get(this->house_scope.house_id)->grf_prop.local_id;
|
2012-11-10 20:40:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:31:03 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
2024-01-07 16:41:53 +00:00
|
|
|
CallbackID callback, uint32_t param1, uint32_t param2)
|
2020-02-03 18:31:03 +00:00
|
|
|
: 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.
|
2015-08-03 20:29:03 +00:00
|
|
|
{
|
2020-02-03 18:31:03 +00:00
|
|
|
this->root_spritegroup = HouseSpec::Get(house_id)->grf_prop.spritegroup[0];
|
2015-08-03 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:31:03 +00:00
|
|
|
GrfSpecFeature FakeHouseResolverObject::GetFeature() const
|
2020-01-26 12:45:51 +00:00
|
|
|
{
|
|
|
|
return GSF_HOUSES;
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t FakeHouseResolverObject::GetDebugID() const
|
2020-01-26 12:45:51 +00:00
|
|
|
{
|
2020-02-03 18:31:03 +00:00
|
|
|
return HouseSpec::Get(this->house_scope.house_id)->grf_prop.local_id;
|
2020-01-26 12:45:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 17:55:37 +00:00
|
|
|
void ResetHouseClassIDs()
|
|
|
|
{
|
|
|
|
_class_mapping = {};
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
HouseClassID AllocateHouseClassID(byte grf_class_id, uint32_t grfid)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
|
|
|
/* Start from 1 because 0 means that no class has been assigned. */
|
|
|
|
for (int i = 1; i != lengthof(_class_mapping); i++) {
|
|
|
|
HouseClassMapping *map = &_class_mapping[i];
|
|
|
|
|
|
|
|
if (map->class_id == grf_class_id && map->grfid == grfid) return (HouseClassID)i;
|
|
|
|
|
|
|
|
if (map->class_id == 0 && map->grfid == 0) {
|
|
|
|
map->class_id = grf_class_id;
|
|
|
|
map->grfid = grfid;
|
|
|
|
return (HouseClassID)i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return HOUSE_NO_CLASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeBuildingCounts()
|
|
|
|
{
|
|
|
|
memset(&_building_counts, 0, sizeof(_building_counts));
|
2012-04-25 20:50:13 +00:00
|
|
|
|
2019-12-17 21:04:09 +00:00
|
|
|
for (Town *t : Town::Iterate()) {
|
2012-04-25 20:50:13 +00:00
|
|
|
memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts));
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.building_counts.id_count[house_id]++;
|
2009-03-17 19:40:10 +00:00
|
|
|
_building_counts.id_count[house_id]++;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2009-03-17 19:40:10 +00:00
|
|
|
if (class_id == HOUSE_NO_CLASS) return;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
t->cache.building_counts.class_count[class_id]++;
|
2009-03-17 19:40:10 +00:00
|
|
|
_building_counts.class_count[class_id]++;
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
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]--;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
if (class_id == HOUSE_NO_CLASS) return;
|
|
|
|
|
2012-04-25 20:50:13 +00:00
|
|
|
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]--;
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
/* virtual */ uint32_t HouseScopeResolver::GetRandomBits() const
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2010-05-01 13:01:21 +00:00
|
|
|
/* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
|
2018-07-26 18:13:35 +00:00
|
|
|
assert_tile(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)), this->tile);
|
2012-11-10 20:40:05 +00:00
|
|
|
return this->not_yet_constructed ? this->initial_random_bits : GetHouseRandomBits(this->tile);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
/* virtual */ uint32_t HouseScopeResolver::GetTriggers() const
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2010-05-01 13:01:21 +00:00
|
|
|
/* Note: Towns build houses over houses. So during construction checks 'tile' may be a valid but unrelated house. */
|
2018-07-26 18:13:35 +00:00
|
|
|
assert_tile(IsValidTile(this->tile) && (this->not_yet_constructed || IsTileType(this->tile, MP_HOUSE)), this->tile);
|
2012-11-10 20:40:05 +00:00
|
|
|
return this->not_yet_constructed ? 0 : GetHouseTriggers(this->tile);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
static uint32_t GetNumHouses(HouseID house_id, const Town *town)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2023-04-29 08:19:43 +00:00
|
|
|
uint8_t map_id_count = ClampTo<uint8_t>(_building_counts.id_count[house_id]);
|
|
|
|
uint8_t map_class_count = ClampTo<uint8_t>(_building_counts.class_count[class_id]);
|
|
|
|
uint8_t town_id_count = ClampTo<uint8_t>(town->cache.building_counts.id_count[house_id]);
|
|
|
|
uint8_t town_class_count = ClampTo<uint8_t>(town->cache.building_counts.class_count[class_id]);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
|
|
|
|
}
|
|
|
|
|
2011-11-08 17:29:01 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
static uint32_t GetNearbyTileInformation(byte parameter, TileIndex tile, bool grf_version8, uint32_t mask)
|
2007-06-08 17:54:18 +00:00
|
|
|
{
|
|
|
|
tile = GetNearbyTile(parameter, tile);
|
2022-05-31 00:32:18 +00:00
|
|
|
return GetNearbyTileInformation(tile, grf_version8, mask);
|
2007-06-08 17:54:18 +00:00
|
|
|
}
|
|
|
|
|
2008-06-25 20:04:27 +00:00
|
|
|
/** Structure with user-data for SearchNearbyHouseXXX - functions */
|
2013-11-23 21:31:00 +00:00
|
|
|
struct SearchNearbyHouseData {
|
2010-10-30 17:51:07 +00:00
|
|
|
const HouseSpec *hs; ///< Specs of the house that started the search.
|
2008-06-25 20:04:27 +00:00
|
|
|
TileIndex north_tile; ///< Northern tile of the house.
|
2013-11-23 21:31:00 +00:00
|
|
|
};
|
2008-06-25 20:04:27 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Callback function to search a house by its HouseID
|
2008-06-21 23:59:38 +00:00
|
|
|
* @param tile TileIndex to be examined
|
2008-06-25 20:04:27 +00:00
|
|
|
* @param user_data SearchNearbyHouseData
|
2008-06-21 23:59:38 +00:00
|
|
|
* @return true or false, if found or not
|
|
|
|
*/
|
2008-06-25 18:46:05 +00:00
|
|
|
static bool SearchNearbyHouseID(TileIndex tile, void *user_data)
|
2008-06-21 23:59:38 +00:00
|
|
|
{
|
|
|
|
if (IsTileType(tile, MP_HOUSE)) {
|
2008-06-25 20:04:27 +00:00
|
|
|
HouseID house = GetHouseType(tile); // tile been examined
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file
|
2008-06-25 20:04:27 +00:00
|
|
|
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
|
|
|
|
|
2010-08-07 21:03:38 +00:00
|
|
|
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
|
2008-06-21 23:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Callback function to search a house by its classID
|
2008-06-21 23:59:38 +00:00
|
|
|
* @param tile TileIndex to be examined
|
2008-06-25 20:04:27 +00:00
|
|
|
* @param user_data SearchNearbyHouseData
|
2008-06-21 23:59:38 +00:00
|
|
|
* @return true or false, if found or not
|
|
|
|
*/
|
2008-06-25 18:46:05 +00:00
|
|
|
static bool SearchNearbyHouseClass(TileIndex tile, void *user_data)
|
2008-06-21 23:59:38 +00:00
|
|
|
{
|
|
|
|
if (IsTileType(tile, MP_HOUSE)) {
|
2008-06-25 20:04:27 +00:00
|
|
|
HouseID house = GetHouseType(tile); // tile been examined
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file
|
2008-06-25 20:04:27 +00:00
|
|
|
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
|
2010-08-07 21:03:38 +00:00
|
|
|
hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
|
2008-06-21 23:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Callback function to search a house by its grfID
|
2008-06-21 23:59:38 +00:00
|
|
|
* @param tile TileIndex to be examined
|
2008-06-25 20:04:27 +00:00
|
|
|
* @param user_data SearchNearbyHouseData
|
2008-06-21 23:59:38 +00:00
|
|
|
* @return true or false, if found or not
|
|
|
|
*/
|
2008-06-25 18:46:05 +00:00
|
|
|
static bool SearchNearbyHouseGRFID(TileIndex tile, void *user_data)
|
2008-06-21 23:59:38 +00:00
|
|
|
{
|
|
|
|
if (IsTileType(tile, MP_HOUSE)) {
|
2008-06-25 20:04:27 +00:00
|
|
|
HouseID house = GetHouseType(tile); // tile been examined
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs->grf_prop.grffile != nullptr) { // must be one from a grf file
|
2008-06-25 20:04:27 +00:00
|
|
|
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
|
|
|
|
|
2010-08-07 21:03:38 +00:00
|
|
|
return hs->grf_prop.grffile->grfid == nbhd->hs->grf_prop.grffile->grfid; // from the same grf
|
2008-06-21 23:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* This function will activate a search around a central tile, looking for some houses
|
2008-06-21 23:59:38 +00:00
|
|
|
* 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
|
2008-06-25 18:46:05 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
static uint32_t GetDistanceFromNearbyHouse(uint8_t parameter, TileIndex tile, HouseID house)
|
2008-06-21 23:59:38 +00:00
|
|
|
{
|
|
|
|
static TestTileOnSearchProc * const search_procs[3] = {
|
|
|
|
SearchNearbyHouseID,
|
|
|
|
SearchNearbyHouseClass,
|
|
|
|
SearchNearbyHouseGRFID,
|
|
|
|
};
|
|
|
|
TileIndex found_tile = tile;
|
2024-01-07 16:41:53 +00:00
|
|
|
uint8_t searchtype = GB(parameter, 6, 2);
|
|
|
|
uint8_t searchradius = GB(parameter, 0, 6);
|
2008-06-21 23:59:38 +00:00
|
|
|
if (searchtype >= lengthof(search_procs)) return 0; // do not run on ill-defined code
|
2008-06-25 17:30:53 +00:00
|
|
|
if (searchradius < 1) return 0; // do not use a too low radius
|
2008-06-21 23:59:38 +00:00
|
|
|
|
2008-06-25 20:04:27 +00:00
|
|
|
SearchNearbyHouseData nbhd;
|
2009-06-26 14:02:27 +00:00
|
|
|
nbhd.hs = HouseSpec::Get(house);
|
2008-06-25 20:04:27 +00:00
|
|
|
nbhd.north_tile = tile + GetHouseNorthPart(house); // modifies 'house'!
|
|
|
|
|
2008-06-21 23:59:38 +00:00
|
|
|
/* Use a pointer for the tile to start the search. Will be required for calculating the distance*/
|
2008-06-25 20:04:27 +00:00
|
|
|
if (CircularTileSearch(&found_tile, 2 * searchradius + 1, search_procs[searchtype], &nbhd)) {
|
2008-06-21 23:59:38 +00:00
|
|
|
return DistanceManhattan(found_tile, tile);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
/**
|
2012-11-10 20:40:05 +00:00
|
|
|
* @note Used by the resolver to get values for feature 07 deterministic spritegroups.
|
2007-03-19 11:27:30 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
/* virtual */ uint32_t HouseScopeResolver::GetVariable(uint16_t variable, uint32_t parameter, GetVariableExtra *extra) const
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
|
|
|
switch (variable) {
|
|
|
|
/* Construction stage. */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x40: return (IsTileType(this->tile, MP_HOUSE) ? GetHouseBuildingStage(this->tile) : 0) | TileHash2Bit(TileX(this->tile), TileY(this->tile)) << 2;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Building age. */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x41: return IsTileType(this->tile, MP_HOUSE) ? GetHouseAge(this->tile) : 0;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Town zone */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x42: return GetTownRadiusGroup(this->town, this->tile);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Terrain type */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x43: return GetTerrainType(this->tile);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Number of this type of building on the map. */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x44: return GetNumHouses(this->house_id, this->town);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2016-05-12 21:35:39 +00:00
|
|
|
/* Whether the town is being created or just expanded. */
|
|
|
|
case 0x45: return _generating_world ? 1 : 0;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Current animation frame. */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x46: return IsTileType(this->tile, MP_HOUSE) ? GetAnimationFrame(this->tile) : 0;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2008-09-12 17:02:22 +00:00
|
|
|
/* Position of the house */
|
2023-04-13 19:56:28 +00:00
|
|
|
case 0x47: return TileY(this->tile) << 16 | (TileX(this->tile) & 0xFFFF);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Building counts for old houses with id = parameter. */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x60: return parameter < NEW_HOUSE_OFFSET ? GetNumHouses(parameter, this->town) : 0;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Building counts for new houses with id = parameter. */
|
|
|
|
case 0x61: {
|
2012-11-10 20:40:05 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(this->house_id);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs->grf_prop.grffile == nullptr) return 0;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-08-07 21:03:38 +00:00
|
|
|
HouseID new_house = _house_mngr.GetID(parameter, hs->grf_prop.grffile->grfid);
|
2012-11-10 20:40:05 +00:00
|
|
|
return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, this->town);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Land info for nearby tiles. */
|
2022-05-31 00:32:18 +00:00
|
|
|
case 0x62: return GetNearbyTileInformation(parameter, this->tile, this->ro.grffile->grf_version >= 8, extra->mask);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2008-06-15 02:48:25 +00:00
|
|
|
/* Current animation frame of nearby house tiles */
|
|
|
|
case 0x63: {
|
2012-11-10 20:40:05 +00:00
|
|
|
TileIndex testtile = GetNearbyTile(parameter, this->tile);
|
2010-08-26 14:45:45 +00:00
|
|
|
return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0;
|
2008-06-15 02:48:25 +00:00
|
|
|
}
|
|
|
|
|
2008-06-21 23:59:38 +00:00
|
|
|
/* Cargo acceptance history of nearby stations */
|
2011-10-31 22:31:31 +00:00
|
|
|
case 0x64: {
|
2013-11-24 14:41:19 +00:00
|
|
|
CargoID cid = GetCargoTranslation(parameter, this->ro.grffile);
|
2024-01-11 17:55:16 +00:00
|
|
|
if (cid == INVALID_CARGO) return 0;
|
2011-10-31 22:31:31 +00:00
|
|
|
|
|
|
|
/* Extract tile offset. */
|
2024-01-07 16:41:53 +00:00
|
|
|
int8_t x_offs = GB(GetRegister(0x100), 0, 8);
|
|
|
|
int8_t y_offs = GB(GetRegister(0x100), 8, 8);
|
2012-11-10 20:40:05 +00:00
|
|
|
TileIndex testtile = TILE_MASK(this->tile + TileDiffXY(x_offs, y_offs));
|
2011-10-31 22:31:31 +00:00
|
|
|
|
2012-01-02 22:58:15 +00:00
|
|
|
StationFinder stations(TileArea(testtile, 1, 1));
|
|
|
|
const StationList *sl = stations.GetStations();
|
2011-10-31 22:31:31 +00:00
|
|
|
|
|
|
|
/* Collect acceptance stats. */
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t res = 0;
|
2019-02-24 18:52:15 +00:00
|
|
|
for (Station *st : *sl) {
|
2014-05-11 18:02:11 +00:00
|
|
|
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);
|
2011-10-31 22:31:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 22:31:40 +00:00
|
|
|
/* Cargo triggered CB 148? */
|
2012-11-10 20:40:05 +00:00
|
|
|
if (HasBit(this->watched_cargo_triggers, cid)) SetBit(res, 4);
|
2011-10-31 22:31:40 +00:00
|
|
|
|
2011-10-31 22:31:31 +00:00
|
|
|
return res;
|
|
|
|
}
|
2008-06-21 23:59:38 +00:00
|
|
|
|
|
|
|
/* Distance test for some house types */
|
2012-11-10 20:40:05 +00:00
|
|
|
case 0x65: return GetDistanceFromNearbyHouse(parameter, this->tile, this->house_id);
|
2010-01-05 21:36:05 +00:00
|
|
|
|
|
|
|
/* Class and ID of nearby house tile */
|
|
|
|
case 0x66: {
|
2012-11-10 20:40:05 +00:00
|
|
|
TileIndex testtile = GetNearbyTile(parameter, this->tile);
|
2010-01-05 21:36:05 +00:00
|
|
|
if (!IsTileType(testtile, MP_HOUSE)) return 0xFFFFFFFF;
|
2016-05-22 10:23:22 +00:00
|
|
|
HouseID nearby_house_id = GetHouseType(testtile);
|
|
|
|
HouseSpec *hs = HouseSpec::Get(nearby_house_id);
|
2016-05-12 21:35:39 +00:00
|
|
|
/* 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;
|
2016-05-22 10:23:22 +00:00
|
|
|
if (nearby_house_id < NEW_HOUSE_OFFSET) {
|
|
|
|
local_houseid = nearby_house_id;
|
2016-05-12 21:35:39 +00:00
|
|
|
} 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;
|
2010-01-05 21:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GRFID of nearby house tile */
|
|
|
|
case 0x67: {
|
2012-11-10 20:40:05 +00:00
|
|
|
TileIndex testtile = GetNearbyTile(parameter, this->tile);
|
2010-01-05 21:36:05 +00:00
|
|
|
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. */
|
2011-06-12 20:42:23 +00:00
|
|
|
return _house_mngr.GetGRFID(house_id);
|
2010-01-05 21:36:05 +00:00
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 16:43:46 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2020-08-25 00:26:44 +00:00
|
|
|
extra->available = false;
|
2007-03-19 11:27:30 +00:00
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:29:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @note Used by the resolver to get values for feature 07 deterministic spritegroups.
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
/* virtual */ uint32_t FakeHouseScopeResolver::GetVariable(uint16_t variable, uint32_t parameter, GetVariableExtra *extra) const
|
2015-08-03 20:29:03 +00:00
|
|
|
{
|
|
|
|
switch (variable) {
|
|
|
|
/* Construction stage. */
|
|
|
|
case 0x40: return TOWN_HOUSE_COMPLETED;
|
|
|
|
|
|
|
|
/* Building age. */
|
|
|
|
case 0x41: return 0;
|
|
|
|
|
|
|
|
/* Town zone */
|
2016-05-12 21:35:39 +00:00
|
|
|
case 0x42: return FIND_FIRST_BIT(HouseSpec::Get(this->house_id)->building_availability & HZ_ZONALL); // first available
|
2015-08-03 20:29:03 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2016-05-12 21:35:39 +00:00
|
|
|
case 0x44: return 0;
|
2015-08-03 20:29:03 +00:00
|
|
|
|
2016-05-12 21:35:39 +00:00
|
|
|
/* Whether the town is being created or just expanded. */
|
|
|
|
case 0x45: return 0;
|
2015-08-03 20:29:03 +00:00
|
|
|
|
|
|
|
/* Current animation frame. */
|
2016-05-12 21:35:39 +00:00
|
|
|
case 0x46: return 0;
|
2015-08-03 20:29:03 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2016-05-12 21:35:39 +00:00
|
|
|
case 0x61: return 0;
|
2015-08-03 20:29:03 +00:00
|
|
|
|
|
|
|
/* 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 */
|
2016-05-12 21:35:39 +00:00
|
|
|
case 0x66: return 0xFFFFFFFF;
|
2015-08-03 20:29:03 +00:00
|
|
|
|
|
|
|
/* GRFID of nearby house tile */
|
2016-05-12 21:35:39 +00:00
|
|
|
case 0x67: return 0xFFFFFFFF;
|
2015-08-03 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(grf, 1, "Unhandled house variable 0x%X", variable);
|
|
|
|
|
2020-08-25 00:26:44 +00:00
|
|
|
extra->available = false;
|
2015-08-03 20:29:03 +00:00
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
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)
|
2016-04-08 18:42:10 +00:00
|
|
|
{
|
2020-02-25 21:31:10 +00:00
|
|
|
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();
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 20:29:03 +00:00
|
|
|
/**
|
|
|
|
* Get the name of a house.
|
2016-05-12 21:35:39 +00:00
|
|
|
* @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.
|
2015-08-03 20:29:03 +00:00
|
|
|
* @return Name of the house.
|
|
|
|
*/
|
2016-05-12 21:35:39 +00:00
|
|
|
StringID GetHouseName(HouseID house_id, TileIndex tile)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2015-08-03 20:29:03 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house_id);
|
2016-05-12 21:35:39 +00:00
|
|
|
bool house_completed = (tile == INVALID_TILE) || IsHouseCompleted(tile);
|
2019-04-11 17:14:13 +00:00
|
|
|
Town *t = (tile == INVALID_TILE) ? nullptr : Town::GetByTile(tile);
|
2015-08-03 20:29:03 +00:00
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t callback_res = GetHouseCallback(CBID_HOUSE_CUSTOM_NAME, house_completed ? 1 : 0, 0, house_id, t, tile);
|
2015-08-03 20:29:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2016-05-12 21:35:39 +00:00
|
|
|
static inline PaletteID GetHouseColour(HouseID house_id, TileIndex tile = INVALID_TILE)
|
2015-08-03 20:29:03 +00:00
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house_id);
|
2008-11-22 16:04:11 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
|
2019-04-11 17:14:13 +00:00
|
|
|
Town *t = (tile != INVALID_TILE) ? Town::GetByTile(tile) : nullptr;
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, t, tile);
|
2008-11-22 16:04:11 +00:00
|
|
|
if (callback != CALLBACK_FAILED) {
|
|
|
|
/* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
|
2015-08-03 20:29:03 +00:00
|
|
|
return HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
|
2008-11-22 16:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-03 20:29:03 +00:00
|
|
|
return hs->random_colour[TileHash2Bit(TileX(tile), TileY(tile))] + PALETTE_RECOLOUR_START;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte stage, HouseID house_id)
|
|
|
|
{
|
|
|
|
const DrawTileSprites *dts = group->ProcessRegisters(&stage);
|
|
|
|
|
2016-05-12 21:35:39 +00:00
|
|
|
PaletteID palette = GetHouseColour(house_id, ti->tile);
|
2008-11-22 16:04:11 +00:00
|
|
|
|
2008-02-15 18:40:42 +00:00
|
|
|
SpriteID image = dts->ground.sprite;
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID pal = dts->ground.pal;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-01-17 01:01:56 +00:00
|
|
|
if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
|
2011-05-29 16:56:22 +00:00
|
|
|
if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
|
2007-09-23 10:54:11 +00:00
|
|
|
|
2008-11-22 16:04:11 +00:00
|
|
|
if (GB(image, 0, SPRITE_WIDTH) != 0) {
|
|
|
|
DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-01-17 15:05:25 +00:00
|
|
|
DrawNewGRFTileSeq(ti, dts, TO_HOUSES, stage, palette);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:35:39 +00:00
|
|
|
static void DrawTileLayoutInGUI(int x, int y, const TileLayoutSpriteGroup *group, HouseID house_id, bool ground)
|
2015-08-03 20:29:03 +00:00
|
|
|
{
|
|
|
|
byte stage = TOWN_HOUSE_COMPLETED;
|
|
|
|
const DrawTileSprites *dts = group->ProcessRegisters(&stage);
|
|
|
|
|
2016-05-12 21:35:39 +00:00
|
|
|
PaletteID palette = GetHouseColour(house_id);
|
2015-08-03 20:29:03 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
|
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(house_id);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2009-09-16 19:10:50 +00:00
|
|
|
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 */
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t callback_res = GetHouseCallback(CBID_HOUSE_DRAW_FOUNDATIONS, 0, 0, house_id, Town::GetByTile(ti->tile), ti->tile);
|
2011-11-08 17:26:49 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DRAW_FOUNDATIONS, callback_res);
|
2009-09-16 19:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2012-11-10 20:40:05 +00:00
|
|
|
HouseResolverObject object(house_id, ti->tile, Town::GetByTile(ti->tile));
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2014-03-03 20:02:31 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group != nullptr && group->type == SGT_TILELAYOUT) {
|
2007-03-19 11:27:30 +00:00
|
|
|
/* Limit the building stage to the number of stages supplied. */
|
2010-01-08 20:42:12 +00:00
|
|
|
const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
|
2007-03-19 11:27:30 +00:00
|
|
|
byte stage = GetHouseBuildingStage(ti->tile);
|
2009-05-23 12:13:42 +00:00
|
|
|
DrawTileLayout(ti, tlgroup, stage, house_id);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 21:35:39 +00:00
|
|
|
void DrawNewHouseTileInGUI(int x, int y, HouseID house_id, bool ground)
|
2015-08-03 20:29:03 +00:00
|
|
|
{
|
2020-02-03 18:31:03 +00:00
|
|
|
FakeHouseResolverObject object(house_id);
|
2015-08-03 20:29:03 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-11 17:14:13 +00:00
|
|
|
if (group != nullptr && group->type == SGT_TILELAYOUT) {
|
2016-05-12 21:35:39 +00:00
|
|
|
DrawTileLayoutInGUI(x, y, (const TileLayoutSpriteGroup*)group, house_id, ground);
|
2015-08-03 20:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
/* Simple wrapper for GetHouseCallback to keep the animation unified. */
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t GetSimpleHouseCallback(CallbackID callback, uint32_t param1, uint32_t param2, const HouseSpec *spec, Town *town, TileIndex tile, CargoTypes extra_data)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2016-05-12 21:35:39 +00:00
|
|
|
return GetHouseCallback(callback, param1, param2, spec - HouseSpec::Get(0), town, tile, false, 0, extra_data);
|
2010-08-26 17:01:17 +00:00
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
/** Helper class for animation control. */
|
2022-02-15 00:22:18 +00:00
|
|
|
struct HouseAnimationBase : public AnimationBase<HouseAnimationBase, HouseSpec, Town, CargoTypes, GetSimpleHouseCallback, TileAnimationFrameAnimationHelper<Town> > {
|
2010-08-26 17:01:17 +00:00
|
|
|
static const CallbackID cb_animation_speed = CBID_HOUSE_ANIMATION_SPEED;
|
|
|
|
static const CallbackID cb_animation_next_frame = CBID_HOUSE_ANIMATION_NEXT_FRAME;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
static const HouseCallbackMask cbm_animation_speed = CBM_HOUSE_ANIMATION_SPEED;
|
|
|
|
static const HouseCallbackMask cbm_animation_next_frame = CBM_HOUSE_ANIMATION_NEXT_FRAME;
|
|
|
|
};
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
void AnimateNewHouseTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs == nullptr) return;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
HouseAnimationBase::AnimateTile(hs, Town::GetByTile(tile), tile, HasBit(hs->extra_flags, CALLBACK_1A_RANDOM_BITS));
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
void AnimateNewHouseConstruction(TileIndex tile)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2010-08-26 17:01:17 +00:00
|
|
|
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);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
uint8_t GetNewHouseTileAnimationSpeed(TileIndex tile)
|
2020-11-24 19:02:38 +00:00
|
|
|
{
|
|
|
|
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
|
|
|
|
if (hs == nullptr) return 0;
|
|
|
|
|
|
|
|
return HouseAnimationBase::GetAnimationSpeed(hs);
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:29:03 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-05-12 21:35:39 +00:00
|
|
|
bool HouseAllowsConstruction(HouseID house_id, TileIndex tile, Town *t, byte random_bits)
|
2015-08-03 20:29:03 +00:00
|
|
|
{
|
|
|
|
const HouseSpec *hs = HouseSpec::Get(house_id);
|
2016-05-12 21:35:39 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house_id, t, tile, true, random_bits);
|
2016-05-12 21:35:39 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED && !Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_ALLOW_CONSTRUCTION, callback_res)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-03 20:29:03 +00:00
|
|
|
}
|
2016-05-12 21:35:39 +00:00
|
|
|
return true;
|
2015-08-03 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
bool CanDeleteHouse(TileIndex tile)
|
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2013-06-19 20:03:10 +00:00
|
|
|
/* Humans are always allowed to remove buildings, as is water and disasters and
|
2007-03-19 11:27:30 +00:00
|
|
|
* anyone using the scenario editor. */
|
2013-06-19 20:03:10 +00:00
|
|
|
if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE || _game_mode == GM_EDITOR || _generating_world) {
|
2009-06-10 22:05:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), Town::GetByTile(tile), tile);
|
2011-11-08 17:26:49 +00:00
|
|
|
return (callback_res == CALLBACK_FAILED || !ConvertBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DENY_DESTRUCTION, callback_res));
|
2007-03-19 11:27:30 +00:00
|
|
|
} else {
|
|
|
|
return !(hs->extra_flags & BUILDING_IS_PROTECTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
static void AnimationControl(TileIndex tile, uint16_t random_bits)
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
|
2010-08-26 17:01:17 +00:00
|
|
|
HouseAnimationBase::ChangeAnimationFrame(CBID_HOUSE_ANIMATION_START_STOP, hs, Town::GetByTile(tile), tile, param, 0);
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NewHouseTileLoop(TileIndex tile)
|
|
|
|
{
|
2009-06-26 14:02:27 +00:00
|
|
|
const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile));
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
if (GetHouseProcessingTime(tile) > 0) {
|
|
|
|
DecHouseProcessingTime(tile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-24 02:38:10 +00:00
|
|
|
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);
|
|
|
|
}
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
|
2007-03-19 11:27:30 +00:00
|
|
|
/* 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) {
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t random = GB(Random(), 0, 16);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
if (hs->building_flags & BUILDING_HAS_1_TILE) AnimationControl(tile, random);
|
|
|
|
if (hs->building_flags & BUILDING_2_TILES_Y) AnimationControl(TILE_ADDXY(tile, 0, 1), random);
|
|
|
|
if (hs->building_flags & BUILDING_2_TILES_X) AnimationControl(TILE_ADDXY(tile, 1, 0), random);
|
|
|
|
if (hs->building_flags & BUILDING_HAS_4_TILES) AnimationControl(TILE_ADDXY(tile, 1, 1), random);
|
|
|
|
} else {
|
|
|
|
AnimationControl(tile, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check callback 21, which determines if a house should be destroyed. */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
|
2019-08-20 00:47:07 +00:00
|
|
|
Town *t = Town::GetByTile(tile);
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), t, tile);
|
2011-11-08 17:26:49 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED && Convert8bitBooleanCallback(hs->grf_prop.grffile, CBID_HOUSE_DESTRUCTION, callback_res)) {
|
2019-08-20 00:47:07 +00:00
|
|
|
ClearTownHouse(t, tile);
|
2007-03-19 11:27:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetHouseProcessingTime(tile, hs->processing_time);
|
2022-11-24 02:38:10 +00:00
|
|
|
if (do_triggers) MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE);
|
2007-03-19 11:27:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-11 17:58:05 +00:00
|
|
|
|
|
|
|
static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
|
|
|
|
{
|
|
|
|
/* We can't trigger a non-existent building... */
|
2018-07-26 18:13:35 +00:00
|
|
|
assert_tile(IsTileType(tile, MP_HOUSE), tile);
|
2007-11-11 17:58:05 +00:00
|
|
|
|
|
|
|
HouseID hid = GetHouseType(tile);
|
2009-06-26 14:02:27 +00:00
|
|
|
HouseSpec *hs = HouseSpec::Get(hid);
|
2007-11-11 17:58:05 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (hs->grf_prop.spritegroup[0] == nullptr) return;
|
2008-04-23 00:14:49 +00:00
|
|
|
|
2012-11-10 20:40:05 +00:00
|
|
|
HouseResolverObject object(hid, tile, Town::GetByTile(tile), CBID_RANDOM_TRIGGER);
|
2017-10-25 15:38:14 +00:00
|
|
|
object.waiting_triggers = GetHouseTriggers(tile) | trigger;
|
|
|
|
SetHouseTriggers(tile, object.waiting_triggers); // store now for var 5F
|
2007-11-11 17:58:05 +00:00
|
|
|
|
2014-03-03 20:02:31 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr) return;
|
2007-11-11 17:58:05 +00:00
|
|
|
|
2017-10-25 15:38:14 +00:00
|
|
|
/* 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. */
|
2007-11-11 17:58:05 +00:00
|
|
|
byte new_random_bits = Random();
|
|
|
|
byte random_bits = GetHouseRandomBits(tile);
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t reseed = object.GetReseedSum();
|
2011-07-04 20:37:20 +00:00
|
|
|
random_bits &= ~reseed;
|
|
|
|
random_bits |= (first ? new_random_bits : base_random) & reseed;
|
2007-11-11 17:58:05 +00:00
|
|
|
SetHouseRandomBits(tile, random_bits);
|
|
|
|
|
|
|
|
switch (trigger) {
|
|
|
|
case HOUSE_TRIGGER_TILE_LOOP:
|
|
|
|
/* Random value already set. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HOUSE_TRIGGER_TILE_LOOP_TOP:
|
2009-08-02 19:49:22 +00:00
|
|
|
if (!first) {
|
|
|
|
/* The top tile is marked dirty by the usual TileLoop */
|
2020-10-01 17:50:31 +00:00
|
|
|
MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE);
|
2009-08-02 19:49:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-11 17:58:05 +00:00
|
|
|
/* Random value of first tile already set. */
|
2007-11-12 01:05:35 +00:00
|
|
|
if (hs->building_flags & BUILDING_2_TILES_Y) DoTriggerHouse(TILE_ADDXY(tile, 0, 1), trigger, random_bits, false);
|
|
|
|
if (hs->building_flags & BUILDING_2_TILES_X) DoTriggerHouse(TILE_ADDXY(tile, 1, 0), trigger, random_bits, false);
|
|
|
|
if (hs->building_flags & BUILDING_HAS_4_TILES) DoTriggerHouse(TILE_ADDXY(tile, 1, 1), trigger, random_bits, false);
|
2007-11-11 17:58:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerHouse(TileIndex t, HouseTrigger trigger)
|
|
|
|
{
|
2007-11-12 01:38:32 +00:00
|
|
|
DoTriggerHouse(t, trigger, 0, true);
|
2007-11-11 17:58:05 +00:00
|
|
|
}
|
2010-04-24 13:35:18 +00:00
|
|
|
|
2011-10-31 22:31:40 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
void DoWatchedCargoCallback(TileIndex tile, TileIndex origin, CargoTypes trigger_cargoes, uint16_t random)
|
2011-10-31 22:31:40 +00:00
|
|
|
{
|
|
|
|
TileIndexDiffC diff = TileIndexToTileIndexDiffC(origin, tile);
|
2024-01-07 16:41:53 +00:00
|
|
|
uint32_t cb_info = random << 16 | (uint8_t)diff.y << 8 | (uint8_t)diff.x;
|
2011-10-31 22:31:40 +00:00
|
|
|
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)
|
|
|
|
*/
|
2018-05-21 21:08:39 +00:00
|
|
|
void WatchedCargoCallback(TileIndex tile, CargoTypes trigger_cargoes)
|
2011-10-31 22:31:40 +00:00
|
|
|
{
|
2018-07-26 18:13:35 +00:00
|
|
|
assert_tile(IsTileType(tile, MP_HOUSE), tile);
|
2011-10-31 22:31:40 +00:00
|
|
|
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. */
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t r = Random();
|
2011-10-31 22:31:40 +00:00
|
|
|
|
|
|
|
/* 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(TILE_ADDXY(north, 0, 1), tile, trigger_cargoes, r);
|
|
|
|
if (hs->building_flags & BUILDING_2_TILES_X) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 0), tile, trigger_cargoes, r);
|
|
|
|
if (hs->building_flags & BUILDING_HAS_4_TILES) DoWatchedCargoCallback(TILE_ADDXY(north, 1, 1), tile, trigger_cargoes, r);
|
|
|
|
}
|
|
|
|
|
2022-11-24 02:38:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|