2007-03-19 12:27:30 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/** @file newgrf_house.cpp */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "variables.h"
|
|
|
|
#include "debug.h"
|
2008-01-09 10:57:48 +01:00
|
|
|
#include "viewport_func.h"
|
2007-03-20 14:47:00 +01:00
|
|
|
#include "landscape.h"
|
2007-03-19 12:27:30 +01:00
|
|
|
#include "town.h"
|
|
|
|
#include "town_map.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "newgrf.h"
|
|
|
|
#include "newgrf_house.h"
|
|
|
|
#include "newgrf_spritegroup.h"
|
|
|
|
#include "newgrf_callbacks.h"
|
|
|
|
#include "newgrf_town.h"
|
|
|
|
#include "newgrf_sound.h"
|
2007-05-15 23:36:58 +02:00
|
|
|
#include "newgrf_commons.h"
|
2007-11-10 02:17:15 +01:00
|
|
|
#include "transparency.h"
|
2007-12-25 12:26:07 +01:00
|
|
|
#include "functions.h"
|
2008-01-12 15:10:35 +01:00
|
|
|
#include "player_func.h"
|
2008-04-20 10:22:59 +02:00
|
|
|
#include "animated_tile_func.h"
|
2008-04-20 10:43:31 +02:00
|
|
|
#include "date_func.h"
|
2007-03-19 12:27:30 +01:00
|
|
|
|
2008-01-13 02:21:35 +01:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/town_land.h"
|
|
|
|
|
2007-03-19 12:27:30 +01:00
|
|
|
static BuildingCounts _building_counts;
|
|
|
|
static HouseClassMapping _class_mapping[HOUSE_CLASS_MAX];
|
|
|
|
|
2007-05-15 23:36:58 +02:00
|
|
|
HouseOverrideManager _house_mngr(NEW_HOUSE_OFFSET, HOUSE_MAX, INVALID_HOUSE_ID);
|
2007-03-19 12:27:30 +01:00
|
|
|
|
2008-03-18 13:28:21 +01:00
|
|
|
/**
|
|
|
|
* Check and update town and house values.
|
|
|
|
*
|
|
|
|
* Checked are the HouseIDs. Updated are the
|
|
|
|
* town population the number of houses per
|
|
|
|
* town, the town radius and the max passengers
|
|
|
|
* of the town.
|
|
|
|
*/
|
|
|
|
void UpdateHousesAndTowns()
|
2007-03-19 12:27:30 +01:00
|
|
|
{
|
2008-01-15 01:03:48 +01:00
|
|
|
Town *town;
|
2007-08-23 20:05:51 +02:00
|
|
|
InitializeBuildingCounts();
|
|
|
|
|
2008-03-18 13:28:21 +01:00
|
|
|
/* Reset town population and num_houses */
|
|
|
|
FOR_ALL_TOWNS(town) {
|
|
|
|
town->population = 0;
|
|
|
|
town->num_houses = 0;
|
|
|
|
}
|
2008-01-15 01:03:48 +01:00
|
|
|
|
2007-03-19 12:27:30 +01:00
|
|
|
for (TileIndex t = 0; t < MapSize(); t++) {
|
|
|
|
HouseID house_id;
|
|
|
|
|
|
|
|
if (!IsTileType(t, MP_HOUSE)) continue;
|
|
|
|
|
|
|
|
house_id = GetHouseType(t);
|
|
|
|
if (!GetHouseSpecs(house_id)->enabled && house_id >= NEW_HOUSE_OFFSET) {
|
|
|
|
/* The specs for this type of house are not available any more, so
|
|
|
|
* replace it with the substitute original house type. */
|
2007-08-23 20:05:51 +02:00
|
|
|
house_id = _house_mngr.GetSubstituteID(house_id);
|
|
|
|
SetHouseType(t, house_id);
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
2008-03-18 13:28:21 +01:00
|
|
|
|
2008-01-15 01:03:48 +01:00
|
|
|
town = GetTownByTile(t);
|
|
|
|
IncreaseBuildingCount(town, house_id);
|
|
|
|
if (IsHouseCompleted(t)) town->population += GetHouseSpecs(house_id)->population;
|
2008-03-18 13:28:21 +01:00
|
|
|
|
|
|
|
/* Increase the number of houses for every house tile which
|
|
|
|
* has a size bit set. Multi tile buildings have got only
|
|
|
|
* one tile with such a bit set, so there is no problem. */
|
|
|
|
if (GetHouseSpecs(GetHouseType(t))->building_flags & BUILDING_HAS_1_TILE) {
|
|
|
|
town->num_houses++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the population and num_house dependant values */
|
|
|
|
FOR_ALL_TOWNS(town) {
|
|
|
|
UpdateTownRadius(town);
|
|
|
|
UpdateTownMaxPass(town);
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HouseClassID AllocateHouseClassID(byte grf_class_id, uint32 grfid)
|
|
|
|
{
|
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = GetHouseSpecs(house_id)->class_id;
|
|
|
|
|
2007-06-16 01:55:52 +02:00
|
|
|
if (!_loaded_newgrf_features.has_newhouses) return;
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
/* If there are 255 buildings of this type in this town, there are also
|
|
|
|
* at least that many houses of the same class in the town, and
|
|
|
|
* therefore on the map as well. */
|
|
|
|
if (t->building_counts.id_count[house_id] == 255) return;
|
|
|
|
|
|
|
|
t->building_counts.id_count[house_id]++;
|
|
|
|
if (_building_counts.id_count[house_id] < 255) _building_counts.id_count[house_id]++;
|
|
|
|
|
|
|
|
/* Similarly, if there are 255 houses of this class in this town, there
|
|
|
|
* must be at least that number on the map too. */
|
|
|
|
if (class_id == HOUSE_NO_CLASS || t->building_counts.class_count[class_id] == 255) return;
|
|
|
|
|
|
|
|
t->building_counts.class_count[class_id]++;
|
|
|
|
if (_building_counts.class_count[class_id] < 255) _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 = GetHouseSpecs(house_id)->class_id;
|
|
|
|
|
2007-06-16 01:55:52 +02:00
|
|
|
if (!_loaded_newgrf_features.has_newhouses) return;
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
if (t->building_counts.id_count[house_id] > 0) t->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->building_counts.class_count[class_id] > 0) t->building_counts.class_count[class_id]--;
|
|
|
|
if (_building_counts.class_count[class_id] > 0) _building_counts.class_count[class_id]--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32 HouseGetRandomBits(const ResolverObject *object)
|
|
|
|
{
|
|
|
|
const TileIndex tile = object->u.house.tile;
|
|
|
|
return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseRandomBits(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32 HouseGetTriggers(const ResolverObject *object)
|
|
|
|
{
|
|
|
|
const TileIndex tile = object->u.house.tile;
|
|
|
|
return (tile == INVALID_TILE || !IsTileType(tile, MP_HOUSE)) ? 0 : GetHouseTriggers(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HouseSetTriggers(const ResolverObject *object, int triggers)
|
|
|
|
{
|
|
|
|
const TileIndex tile = object->u.house.tile;
|
|
|
|
if (IsTileType(tile, MP_HOUSE)) SetHouseTriggers(tile, triggers);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32 GetNumHouses(HouseID house_id, const Town *town)
|
|
|
|
{
|
|
|
|
uint8 map_id_count, town_id_count, map_class_count, town_class_count;
|
|
|
|
HouseClassID class_id = GetHouseSpecs(house_id)->class_id;
|
|
|
|
|
|
|
|
map_id_count = _building_counts.id_count[house_id];
|
|
|
|
map_class_count = _building_counts.class_count[class_id];
|
|
|
|
town_id_count = town->building_counts.id_count[house_id];
|
|
|
|
town_class_count = town->building_counts.class_count[class_id];
|
|
|
|
|
|
|
|
return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32 GetGRFParameter(HouseID house_id, byte parameter)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(house_id);
|
|
|
|
const GRFFile *file = hs->grffile;
|
|
|
|
|
|
|
|
if (parameter >= file->param_end) return 0;
|
|
|
|
return file->param[parameter];
|
|
|
|
}
|
|
|
|
|
2007-06-08 19:54:18 +02:00
|
|
|
uint32 GetNearbyTileInformation(byte parameter, TileIndex tile)
|
|
|
|
{
|
|
|
|
tile = GetNearbyTile(parameter, tile);
|
2008-01-31 18:46:08 +01:00
|
|
|
return GetNearbyTileInformation(tile);
|
2007-06-08 19:54:18 +02:00
|
|
|
}
|
|
|
|
|
2007-03-19 12:27:30 +01:00
|
|
|
/**
|
|
|
|
* HouseGetVariable():
|
|
|
|
*
|
|
|
|
* Used by the resolver to get values for feature 07 deterministic spritegroups.
|
|
|
|
*/
|
|
|
|
static uint32 HouseGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
|
|
|
|
{
|
|
|
|
const Town *town = object->u.house.town;
|
|
|
|
TileIndex tile = object->u.house.tile;
|
|
|
|
HouseID house_id = object->u.house.house_id;
|
|
|
|
|
|
|
|
if (object->scope == VSG_SCOPE_PARENT) {
|
|
|
|
return TownGetVariable(variable, parameter, available, town);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
|
|
|
/* Construction stage. */
|
2008-03-31 17:10:24 +02:00
|
|
|
case 0x40: return (IsTileType(tile, MP_HOUSE) ? GetHouseBuildingStage(tile) : 0) | TileHash2Bit(TileX(tile), TileY(tile)) << 2;
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
/* Building age. */
|
2007-11-19 19:38:10 +01:00
|
|
|
case 0x41: return Clamp(_cur_year - GetHouseConstructionYear(tile), 0, 0xFF);
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
/* Town zone */
|
|
|
|
case 0x42: return GetTownRadiusGroup(town, tile);
|
|
|
|
|
|
|
|
/* Terrain type */
|
|
|
|
case 0x43: return GetTerrainType(tile);
|
|
|
|
|
|
|
|
/* Number of this type of building on the map. */
|
|
|
|
case 0x44: return GetNumHouses(house_id, town);
|
|
|
|
|
|
|
|
/* Whether the town is being created or just expanded. */
|
|
|
|
case 0x45: return _generating_world ? 1 : 0;
|
|
|
|
|
|
|
|
/* Current animation frame. */
|
|
|
|
case 0x46: return IsTileType(tile, MP_HOUSE) ? GetHouseAnimationFrame(tile) : 0;
|
|
|
|
|
|
|
|
|
|
|
|
/* Building counts for old houses with id = parameter. */
|
|
|
|
case 0x60: return GetNumHouses(parameter, town);
|
|
|
|
|
|
|
|
/* Building counts for new houses with id = parameter. */
|
|
|
|
case 0x61: {
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(house_id);
|
|
|
|
if (hs->grffile == NULL) return 0;
|
|
|
|
|
2007-05-15 23:36:58 +02:00
|
|
|
HouseID new_house = _house_mngr.GetID(parameter, hs->grffile->grfid);
|
2007-03-19 12:27:30 +01:00
|
|
|
return new_house == INVALID_HOUSE_ID ? 0 : GetNumHouses(new_house, town);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Land info for nearby tiles. */
|
2007-06-08 19:54:18 +02:00
|
|
|
case 0x62: return GetNearbyTileInformation(parameter, tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
/* Read GRF parameter */
|
|
|
|
case 0x7F: return GetGRFParameter(object->u.house.house_id, parameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(grf, 1, "Unhandled house property 0x%X", variable);
|
|
|
|
|
|
|
|
*available = false;
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const SpriteGroup *HouseResolveReal(const ResolverObject *object, const SpriteGroup *group)
|
|
|
|
{
|
|
|
|
/* Houses do not have 'real' groups */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NewHouseResolver():
|
|
|
|
*
|
|
|
|
* Returns a resolver object to be used with feature 07 spritegroups.
|
|
|
|
*/
|
|
|
|
static void NewHouseResolver(ResolverObject *res, HouseID house_id, TileIndex tile, Town *town)
|
|
|
|
{
|
|
|
|
res->GetRandomBits = HouseGetRandomBits;
|
|
|
|
res->GetTriggers = HouseGetTriggers;
|
|
|
|
res->SetTriggers = HouseSetTriggers;
|
|
|
|
res->GetVariable = HouseGetVariable;
|
|
|
|
res->ResolveReal = HouseResolveReal;
|
|
|
|
|
|
|
|
res->u.house.tile = tile;
|
|
|
|
res->u.house.town = town;
|
|
|
|
res->u.house.house_id = house_id;
|
|
|
|
|
2007-07-25 21:06:29 +02:00
|
|
|
res->callback = CBID_NO_CALLBACK;
|
2007-03-19 12:27:30 +01:00
|
|
|
res->callback_param1 = 0;
|
|
|
|
res->callback_param2 = 0;
|
|
|
|
res->last_value = 0;
|
|
|
|
res->trigger = 0;
|
|
|
|
res->reseed = 0;
|
2008-03-27 22:36:16 +01:00
|
|
|
res->count = 0;
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
|
2007-07-25 21:06:29 +02:00
|
|
|
uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile)
|
2007-03-19 12:27:30 +01:00
|
|
|
{
|
|
|
|
ResolverObject object;
|
|
|
|
const SpriteGroup *group;
|
|
|
|
|
|
|
|
NewHouseResolver(&object, house_id, tile, town);
|
|
|
|
object.callback = callback;
|
|
|
|
object.callback_param1 = param1;
|
2007-05-19 11:13:08 +02:00
|
|
|
object.callback_param2 = param2;
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
group = Resolve(GetHouseSpecs(house_id)->spritegroup, &object);
|
|
|
|
if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
|
|
|
|
|
|
|
|
return group->g.callback.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte stage, HouseID house_id)
|
|
|
|
{
|
|
|
|
const DrawTileSprites *dts = group->g.layout.dts;
|
|
|
|
const DrawTileSeqStruct *dtss;
|
|
|
|
|
2008-02-15 19:40:42 +01:00
|
|
|
SpriteID image = dts->ground.sprite;
|
|
|
|
SpriteID pal = dts->ground.pal;
|
2007-03-19 12:27:30 +01:00
|
|
|
|
2007-09-23 12:54:11 +02:00
|
|
|
if (IS_CUSTOM_SPRITE(image)) image += stage;
|
|
|
|
|
2007-03-19 12:27:30 +01:00
|
|
|
if (GB(image, 0, SPRITE_WIDTH) != 0) DrawGroundSprite(image, pal);
|
|
|
|
|
2008-04-03 21:55:40 +02:00
|
|
|
/* End now, if houses are invisible */
|
|
|
|
if (IsInvisibilitySet(TO_HOUSES)) return;
|
|
|
|
|
2007-03-19 12:27:30 +01:00
|
|
|
foreach_draw_tile_seq(dtss, dts->seq) {
|
2008-02-15 19:34:26 +01:00
|
|
|
if (GB(dtss->image.sprite, 0, SPRITE_WIDTH) == 0) continue;
|
2007-03-19 12:27:30 +01:00
|
|
|
|
2008-02-15 19:34:26 +01:00
|
|
|
image = dtss->image.sprite;
|
|
|
|
pal = dtss->image.pal;
|
2007-03-19 12:27:30 +01:00
|
|
|
|
2007-09-23 12:54:11 +02:00
|
|
|
if (IS_CUSTOM_SPRITE(image)) image += stage;
|
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
if ((HasBit(image, SPRITE_MODIFIER_OPAQUE) || !IsTransparencySet(TO_HOUSES)) && HasBit(image, PALETTE_MODIFIER_COLOR)) {
|
2007-03-19 12:27:30 +01:00
|
|
|
if (pal == 0) {
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(house_id);
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_COLOUR)) {
|
2007-09-26 19:33:32 +02:00
|
|
|
uint16 callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, GetTownByTile(ti->tile), ti->tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
if (callback != CALLBACK_FAILED) {
|
|
|
|
/* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */
|
2007-11-19 22:02:30 +01:00
|
|
|
pal = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback;
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
2008-03-31 17:10:24 +02:00
|
|
|
pal = hs->random_colour[TileHash2Bit(ti->x, ti->y)] + PALETTE_RECOLOR_START;
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pal = PAL_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((byte)dtss->delta_z != 0x80) {
|
|
|
|
AddSortableSpriteToDraw(
|
|
|
|
image, pal,
|
|
|
|
ti->x + dtss->delta_x, ti->y + dtss->delta_y,
|
|
|
|
dtss->size_x, dtss->size_y,
|
2007-07-26 16:07:11 +02:00
|
|
|
dtss->size_z, ti->z + dtss->delta_z,
|
2007-11-10 02:17:15 +01:00
|
|
|
IsTransparencySet(TO_HOUSES)
|
2007-03-19 12:27:30 +01:00
|
|
|
);
|
|
|
|
} else {
|
2007-11-10 02:17:15 +01:00
|
|
|
AddChildSpriteScreen(image, pal, (byte)dtss->delta_x, (byte)dtss->delta_y, IsTransparencySet(TO_HOUSES));
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNewHouseTile(TileInfo *ti, HouseID house_id)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(house_id);
|
|
|
|
const SpriteGroup *group;
|
|
|
|
ResolverObject object;
|
|
|
|
|
2007-07-26 18:51:10 +02:00
|
|
|
if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
NewHouseResolver(&object, house_id, ti->tile, GetTownByTile(ti->tile));
|
|
|
|
|
|
|
|
group = Resolve(hs->spritegroup, &object);
|
|
|
|
if (group == NULL || group->type != SGT_TILELAYOUT) {
|
|
|
|
/* XXX: This is for debugging purposes really, and shouldn't stay. */
|
|
|
|
DrawGroundSprite(SPR_SHADOW_CELL, PAL_NONE);
|
|
|
|
} else {
|
|
|
|
/* Limit the building stage to the number of stages supplied. */
|
|
|
|
byte stage = GetHouseBuildingStage(ti->tile);
|
2007-11-19 19:38:10 +01:00
|
|
|
stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
|
2007-03-19 12:27:30 +01:00
|
|
|
DrawTileLayout(ti, group, stage, house_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimateNewHouseTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
|
|
|
|
byte animation_speed = hs->animation_speed;
|
|
|
|
bool frame_set_by_callback = false;
|
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_SPEED)) {
|
2007-05-19 11:13:08 +02:00
|
|
|
uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_SPEED, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
|
2007-11-19 19:38:10 +01:00
|
|
|
if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 2, 16);
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* An animation speed of 2 means the animation frame changes 4 ticks, and
|
|
|
|
* increasing this value by one doubles the wait. 2 is the minimum value
|
|
|
|
* allowed for animation_speed, which corresponds to 120ms, and 16 is the
|
|
|
|
* maximum, corresponding to around 33 minutes. */
|
|
|
|
if (_tick_counter % (1 << animation_speed) != 0) return;
|
|
|
|
|
|
|
|
byte frame = GetHouseAnimationFrame(tile);
|
|
|
|
byte num_frames = GB(hs->animation_frames, 0, 7);
|
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_NEXT_FRAME)) {
|
2007-03-19 12:27:30 +01:00
|
|
|
uint32 param = (hs->extra_flags & CALLBACK_1A_RANDOM_BITS) ? Random() : 0;
|
2007-05-19 11:13:08 +02:00
|
|
|
uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_NEXT_FRAME, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
|
|
|
|
if (callback_res != CALLBACK_FAILED) {
|
|
|
|
frame_set_by_callback = true;
|
|
|
|
|
|
|
|
switch (callback_res & 0xFF) {
|
|
|
|
case 0xFF:
|
|
|
|
DeleteAnimatedTile(tile);
|
|
|
|
break;
|
|
|
|
case 0xFE:
|
|
|
|
/* Carry on as normal. */
|
|
|
|
frame_set_by_callback = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
frame = callback_res & 0xFF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the lower 7 bits of the upper byte of the callback
|
|
|
|
* result are not empty, it is a sound effect. */
|
2008-04-21 13:29:01 +02:00
|
|
|
if (GB(callback_res, 8, 7) != 0) PlayTileSound(hs->grffile, GB(callback_res, 8, 7), tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame_set_by_callback) {
|
|
|
|
if (frame < num_frames) {
|
|
|
|
frame++;
|
2007-11-19 22:02:30 +01:00
|
|
|
} else if (frame == num_frames && HasBit(hs->animation_frames, 7)) {
|
2007-03-19 12:27:30 +01:00
|
|
|
/* This animation loops, so start again from the beginning */
|
|
|
|
frame = 0;
|
|
|
|
} else {
|
|
|
|
/* This animation doesn't loop, so stay here */
|
|
|
|
DeleteAnimatedTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetHouseAnimationFrame(tile, frame);
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
2008-04-21 13:29:01 +02:00
|
|
|
void ChangeHouseAnimationFrame(const GRFFile *file, TileIndex tile, uint16 callback_result)
|
2007-03-19 12:27:30 +01:00
|
|
|
{
|
|
|
|
switch (callback_result & 0xFF) {
|
|
|
|
case 0xFD: /* Do nothing. */ break;
|
|
|
|
case 0xFE: AddAnimatedTile(tile); break;
|
|
|
|
case 0xFF: DeleteAnimatedTile(tile); break;
|
|
|
|
default:
|
|
|
|
SetHouseAnimationFrame(tile, callback_result & 0xFF);
|
|
|
|
AddAnimatedTile(tile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* If the lower 7 bits of the upper byte of the callback
|
|
|
|
* result are not empty, it is a sound effect. */
|
2008-04-21 13:29:01 +02:00
|
|
|
if (GB(callback_result, 8, 7) != 0) PlayTileSound(file, GB(callback_result, 8, 7), tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CanDeleteHouse(TileIndex tile)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
|
|
|
|
|
|
|
|
/* Human players are always allowed to remove buildings, as is water and
|
|
|
|
* anyone using the scenario editor. */
|
|
|
|
if ((IsValidPlayer(_current_player) && IsHumanPlayer(_current_player))
|
|
|
|
|| _current_player == OWNER_WATER || _current_player == OWNER_NONE) return true;
|
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
|
2007-05-19 11:13:08 +02:00
|
|
|
uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
return (callback_res == CALLBACK_FAILED || callback_res == 0);
|
|
|
|
} else {
|
|
|
|
return !(hs->extra_flags & BUILDING_IS_PROTECTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AnimationControl(TileIndex tile, uint16 random_bits)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
|
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
|
2007-03-19 12:27:30 +01:00
|
|
|
uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
|
2007-05-19 11:13:08 +02:00
|
|
|
uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
|
2008-04-21 13:29:01 +02:00
|
|
|
if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
|
2007-03-19 12:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NewHouseTileLoop(TileIndex tile)
|
|
|
|
{
|
|
|
|
const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
|
|
|
|
|
|
|
|
if (GetHouseProcessingTime(tile) > 0) {
|
|
|
|
DecHouseProcessingTime(tile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-11 18:58:05 +01:00
|
|
|
TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP);
|
|
|
|
TriggerHouse(tile, HOUSE_TRIGGER_TILE_LOOP_TOP);
|
2007-03-19 12:27:30 +01:00
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_ANIMATION_START_STOP)) {
|
2007-03-19 12:27:30 +01: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) {
|
|
|
|
uint16 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(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 22:02:30 +01:00
|
|
|
if (HasBit(hs->callback_mask, CBM_HOUSE_DESTRUCTION)) {
|
2007-05-19 11:13:08 +02:00
|
|
|
uint16 callback_res = GetHouseCallback(CBID_HOUSE_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
|
2007-03-19 12:27:30 +01:00
|
|
|
if (callback_res != CALLBACK_FAILED && callback_res > 0) {
|
|
|
|
ClearTownHouse(GetTownByTile(tile), tile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetHouseProcessingTime(tile, hs->processing_time);
|
|
|
|
return true;
|
|
|
|
}
|
2007-11-11 18:58:05 +01:00
|
|
|
|
|
|
|
static void DoTriggerHouse(TileIndex tile, HouseTrigger trigger, byte base_random, bool first)
|
|
|
|
{
|
|
|
|
ResolverObject object;
|
|
|
|
|
|
|
|
/* We can't trigger a non-existent building... */
|
|
|
|
assert(IsTileType(tile, MP_HOUSE));
|
|
|
|
|
|
|
|
HouseID hid = GetHouseType(tile);
|
|
|
|
HouseSpec *hs = GetHouseSpecs(hid);
|
|
|
|
|
|
|
|
NewHouseResolver(&object, hid, tile, GetTownByTile(tile));
|
|
|
|
|
|
|
|
object.callback = CBID_RANDOM_TRIGGER;
|
|
|
|
object.trigger = trigger;
|
|
|
|
|
|
|
|
const SpriteGroup *group = Resolve(hs->spritegroup, &object);
|
|
|
|
if (group == NULL) return;
|
|
|
|
|
|
|
|
byte new_random_bits = Random();
|
|
|
|
byte random_bits = GetHouseRandomBits(tile);
|
|
|
|
random_bits &= ~object.reseed;
|
|
|
|
random_bits |= (first ? new_random_bits : base_random) & object.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) break;
|
|
|
|
/* Random value of first tile already set. */
|
2007-11-12 02:05:35 +01: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 18:58:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerHouse(TileIndex t, HouseTrigger trigger)
|
|
|
|
{
|
2007-11-12 02:38:32 +01:00
|
|
|
DoTriggerHouse(t, trigger, 0, true);
|
2007-11-11 18:58:05 +01:00
|
|
|
}
|