2007-06-13 02:29:08 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_industrytiles.cpp NewGRF handling of industry tiles. */
|
2007-06-13 02:29:08 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "variables.h"
|
|
|
|
#include "debug.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "viewport_func.h"
|
2007-06-13 02:29:08 +00:00
|
|
|
#include "landscape.h"
|
|
|
|
#include "newgrf.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "core/random_func.hpp"
|
2007-06-13 02:29:08 +00:00
|
|
|
#include "newgrf_commons.h"
|
|
|
|
#include "newgrf_industries.h"
|
2007-07-11 15:03:29 +00:00
|
|
|
#include "newgrf_industrytiles.h"
|
2008-04-21 11:29:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2007-07-09 18:53:43 +00:00
|
|
|
#include "newgrf_text.h"
|
2007-06-13 02:29:08 +00:00
|
|
|
#include "industry_map.h"
|
|
|
|
#include "sprite.h"
|
2007-11-10 01:17:15 +00:00
|
|
|
#include "transparency.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
2008-01-07 14:02:26 +00:00
|
|
|
#include "town.h"
|
2008-01-13 21:41:24 +00:00
|
|
|
#include "command_func.h"
|
2008-04-20 08:22:59 +00:00
|
|
|
#include "animated_tile_func.h"
|
2008-07-26 16:14:10 +00:00
|
|
|
#include "water.h"
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2007-06-13 02:29:08 +00:00
|
|
|
/**
|
|
|
|
* Based on newhouses equivalent, but adapted for newindustries
|
|
|
|
* @param parameter from callback. It's in fact a pair of coordinates
|
|
|
|
* @param tile TileIndex from which the callback was initiated
|
|
|
|
* @param index of the industry been queried for
|
|
|
|
* @return a construction of bits obeying the newgrf format
|
|
|
|
*/
|
2007-07-11 23:10:22 +00:00
|
|
|
uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
2007-08-15 01:50:13 +00:00
|
|
|
if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
|
2008-01-31 17:46:08 +00:00
|
|
|
bool is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2008-01-31 17:46:08 +00:00
|
|
|
return GetNearbyTileInformation(tile) | (is_same_industry ? 1 : 0) << 8;
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** This is the position of the tile relative to the northernmost tile of the industry.
|
|
|
|
* Format: 00yxYYXX
|
|
|
|
* Variable Content
|
|
|
|
* x the x offset from the northernmost tile
|
|
|
|
* XX same, but stored in a byte instead of a nibble
|
|
|
|
* y the y offset from the northernmost tile
|
|
|
|
* YY same, but stored in a byte instead of a nibble
|
|
|
|
* @param tile TileIndex of the tile to evaluate
|
|
|
|
* @param ind_tile northernmost tile of the industry
|
|
|
|
*/
|
|
|
|
static uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
|
|
|
|
{
|
2007-06-17 01:01:15 +00:00
|
|
|
byte x = TileX(tile) - TileX(ind_tile);
|
|
|
|
byte y = TileY(tile) - TileY(ind_tile);
|
2007-06-13 02:29:08 +00:00
|
|
|
|
|
|
|
return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32 IndustryTileGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
|
|
|
|
{
|
|
|
|
const Industry *inds = object->u.industry.ind;
|
|
|
|
TileIndex tile = object->u.industry.tile;
|
|
|
|
|
|
|
|
if (object->scope == VSG_SCOPE_PARENT) {
|
|
|
|
return IndustryGetVariable(object, variable, parameter, available);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
2009-02-18 09:10:02 +00:00
|
|
|
/* Construction state of the tile: a value between 0 and 3 */
|
|
|
|
case 0x40: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2009-02-18 09:10:02 +00:00
|
|
|
/* Terrain type */
|
|
|
|
case 0x41: return GetTerrainType(tile);
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2007-06-13 18:35:06 +00:00
|
|
|
/* Current town zone of the tile in the nearest town */
|
2009-02-18 09:10:02 +00:00
|
|
|
case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2007-06-13 18:35:06 +00:00
|
|
|
/* Relative position */
|
2009-02-18 09:10:02 +00:00
|
|
|
case 0x43: return GetRelativePosition(tile, inds->xy);
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2007-06-13 18:35:06 +00:00
|
|
|
/* Animation frame. Like house variable 46 but can contain anything 0..FF. */
|
2009-02-18 09:10:02 +00:00
|
|
|
case 0x44: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryAnimationState(tile) : 0;
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2007-06-13 18:35:06 +00:00
|
|
|
/* Land info of nearby tiles */
|
2009-02-18 09:10:02 +00:00
|
|
|
case 0x60: return GetNearbyIndustryTileInformation(parameter, tile, inds == NULL ? (IndustryID)INVALID_INDUSTRY : inds->index);
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2007-07-11 23:10:22 +00:00
|
|
|
/* Animation stage of nearby tiles */
|
2009-02-18 09:10:02 +00:00
|
|
|
case 0x61:
|
2007-06-13 02:29:08 +00:00
|
|
|
tile = GetNearbyTile(parameter, tile);
|
2007-06-13 18:35:06 +00:00
|
|
|
if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == inds) {
|
|
|
|
return GetIndustryAnimationState(tile);
|
|
|
|
}
|
2009-02-18 09:10:02 +00:00
|
|
|
return UINT_MAX;
|
2007-06-13 02:29:08 +00:00
|
|
|
|
|
|
|
/* Get industry tile ID at offset */
|
2009-02-18 09:10:02 +00:00
|
|
|
case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), inds);
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-11 22:57:47 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled industry tile property 0x%X", variable);
|
|
|
|
|
|
|
|
*available = false;
|
2009-02-18 09:14:41 +00:00
|
|
|
return UINT_MAX;
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
|
|
|
/* IndustryTile do not have 'real' groups. Or do they?? */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-23 19:27:35 +00:00
|
|
|
static uint32 IndustryTileGetRandomBits(const ResolverObject *object)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
|
|
|
const TileIndex tile = object->u.industry.tile;
|
2007-09-23 19:27:35 +00:00
|
|
|
if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
|
2007-11-11 17:56:37 +00:00
|
|
|
return (object->scope == VSG_SCOPE_SELF) ? GetIndustryRandomBits(tile) : GetIndustryByTile(tile)->random;
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2007-09-23 19:27:35 +00:00
|
|
|
static uint32 IndustryTileGetTriggers(const ResolverObject *object)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
|
|
|
const TileIndex tile = object->u.industry.tile;
|
2007-09-23 19:27:35 +00:00
|
|
|
if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
|
2007-11-11 17:56:37 +00:00
|
|
|
return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : GetIndustryByTile(tile)->random_triggers;
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2007-09-23 19:27:35 +00:00
|
|
|
static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
|
|
|
const TileIndex tile = object->u.industry.tile;
|
2007-09-23 19:27:35 +00:00
|
|
|
if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return;
|
|
|
|
|
2007-11-11 17:56:37 +00:00
|
|
|
if (object->scope == VSG_SCOPE_SELF) {
|
2007-09-23 19:27:35 +00:00
|
|
|
SetIndustryTriggers(tile, triggers);
|
|
|
|
} else {
|
2007-11-11 17:56:37 +00:00
|
|
|
GetIndustryByTile(tile)->random_triggers = triggers;
|
2007-09-23 19:27:35 +00:00
|
|
|
}
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
|
|
|
|
{
|
|
|
|
res->GetRandomBits = IndustryTileGetRandomBits;
|
|
|
|
res->GetTriggers = IndustryTileGetTriggers;
|
|
|
|
res->SetTriggers = IndustryTileSetTriggers;
|
|
|
|
res->GetVariable = IndustryTileGetVariable;
|
|
|
|
res->ResolveReal = IndustryTileResolveReal;
|
|
|
|
|
2007-09-22 13:56:38 +00:00
|
|
|
res->psa = &indus->psa;
|
2007-06-13 02:29:08 +00:00
|
|
|
res->u.industry.tile = tile;
|
|
|
|
res->u.industry.ind = indus;
|
|
|
|
res->u.industry.gfx = gfx;
|
2008-01-10 00:53:17 +00:00
|
|
|
res->u.industry.type = indus->type;
|
2007-06-13 02:29:08 +00:00
|
|
|
|
2007-07-25 19:06:29 +00:00
|
|
|
res->callback = CBID_NO_CALLBACK;
|
2007-06-13 02:29:08 +00:00
|
|
|
res->callback_param1 = 0;
|
|
|
|
res->callback_param2 = 0;
|
|
|
|
res->last_value = 0;
|
|
|
|
res->trigger = 0;
|
|
|
|
res->reseed = 0;
|
2008-03-27 21:36:16 +00:00
|
|
|
res->count = 0;
|
2008-07-30 18:23:12 +00:00
|
|
|
|
|
|
|
const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
|
|
|
|
res->grffile = (its != NULL ? its->grf_prop.grffile : NULL);
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
|
2007-06-19 17:33:12 +00:00
|
|
|
{
|
2009-05-23 12:13:42 +00:00
|
|
|
const DrawTileSprites *dts = group->dts;
|
2007-06-19 17:33:12 +00:00
|
|
|
const DrawTileSeqStruct *dtss;
|
|
|
|
|
2008-02-15 18:40:42 +00:00
|
|
|
SpriteID image = dts->ground.sprite;
|
|
|
|
SpriteID pal = dts->ground.pal;
|
2007-06-19 17:33:12 +00:00
|
|
|
|
2007-09-30 15:37:24 +00:00
|
|
|
if (IS_CUSTOM_SPRITE(image)) image += stage;
|
|
|
|
|
2008-07-26 16:14:10 +00:00
|
|
|
if (GB(image, 0, SPRITE_WIDTH) != 0) {
|
|
|
|
/* If the ground sprite is the default flat water sprite, draw also canal/river borders
|
|
|
|
* Do not do this if the tile's WaterClass is 'land'. */
|
|
|
|
if (image == SPR_FLAT_WATER_TILE && IsIndustryTileOnWater(ti->tile)) {
|
|
|
|
DrawWaterClassGround(ti);
|
|
|
|
} else {
|
2009-02-09 02:57:15 +00:00
|
|
|
DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
|
2008-07-26 16:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-19 17:33:12 +00:00
|
|
|
|
|
|
|
foreach_draw_tile_seq(dtss, dts->seq) {
|
2008-02-15 18:34:26 +00:00
|
|
|
if (GB(dtss->image.sprite, 0, SPRITE_WIDTH) == 0) continue;
|
2007-06-19 17:33:12 +00:00
|
|
|
|
2008-02-15 18:34:26 +00:00
|
|
|
image = dtss->image.sprite;
|
|
|
|
pal = dtss->image.pal;
|
2007-06-19 17:33:12 +00:00
|
|
|
|
2008-06-18 16:48:58 +00:00
|
|
|
/* Stop drawing sprite sequence once we meet a sprite that doesn't have to be opaque */
|
|
|
|
if (IsInvisibilitySet(TO_INDUSTRIES) && !HasBit(image, SPRITE_MODIFIER_OPAQUE)) return;
|
|
|
|
|
2007-09-30 15:37:24 +00:00
|
|
|
if (IS_CUSTOM_SPRITE(image)) image += stage;
|
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
pal = SpriteLayoutPaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour));
|
2007-06-19 17:33:12 +00:00
|
|
|
|
|
|
|
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 14:07:11 +00:00
|
|
|
dtss->size_z, ti->z + dtss->delta_z,
|
2007-11-19 21:02:30 +00:00
|
|
|
!HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_INDUSTRIES)
|
2007-06-19 17:33:12 +00:00
|
|
|
);
|
|
|
|
} else {
|
2008-10-18 17:21:56 +00:00
|
|
|
/* For industries and houses delta_x and delta_y are unsigned */
|
|
|
|
AddChildSpriteScreen(image, pal, (byte)dtss->delta_x, (byte)dtss->delta_y, !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_INDUSTRIES));
|
2007-06-19 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-25 19:06:29 +00:00
|
|
|
uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
|
|
|
ResolverObject object;
|
|
|
|
const SpriteGroup *group;
|
|
|
|
|
|
|
|
NewIndustryTileResolver(&object, gfx_id, tile, industry);
|
|
|
|
object.callback = callback;
|
|
|
|
object.callback_param1 = param1;
|
|
|
|
object.callback_param2 = param2;
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
|
2007-06-13 02:29:08 +00:00
|
|
|
if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
|
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
return group->GetCallbackResult();
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
2007-06-19 17:33:12 +00:00
|
|
|
|
|
|
|
bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
|
|
|
|
{
|
|
|
|
const SpriteGroup *group;
|
|
|
|
ResolverObject object;
|
|
|
|
|
|
|
|
if (ti->tileh != SLOPE_FLAT) {
|
|
|
|
bool draw_old_one = true;
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(inds->callback_flags, CBM_INDT_DRAW_FOUNDATIONS)) {
|
2007-06-19 17:33:12 +00:00
|
|
|
/* Called to determine the type (if any) of foundation to draw for industry tile */
|
|
|
|
uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
|
2007-07-09 13:59:07 +00:00
|
|
|
draw_old_one = callback_res != 0;
|
2007-06-19 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2007-07-26 16:51:10 +00:00
|
|
|
if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
|
2007-06-19 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NewIndustryTileResolver(&object, gfx, ti->tile, i);
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
group = SpriteGroup::Resolve(inds->grf_prop.spritegroup, &object);
|
2007-06-19 17:33:12 +00:00
|
|
|
if (group == NULL || group->type != SGT_TILELAYOUT) {
|
|
|
|
return false;
|
|
|
|
} else {
|
2009-05-23 12:13:42 +00:00
|
|
|
const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
|
2007-06-19 17:33:12 +00:00
|
|
|
/* Limit the building stage to the number of stages supplied. */
|
|
|
|
byte stage = GetIndustryConstructionStage(ti->tile);
|
2009-05-23 12:13:42 +00:00
|
|
|
stage = Clamp(stage - 4 + tlgroup->num_sprites, 0, tlgroup->num_sprites - 1);
|
|
|
|
IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
|
2007-06-19 17:33:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-07-09 18:53:43 +00:00
|
|
|
|
2007-10-02 16:56:45 +00:00
|
|
|
extern bool IsSlopeRefused(Slope current, Slope refused);
|
|
|
|
|
|
|
|
bool PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, uint itspec_index)
|
2007-07-09 18:53:43 +00:00
|
|
|
{
|
2007-07-09 20:08:25 +00:00
|
|
|
Industry ind;
|
2007-10-02 16:56:45 +00:00
|
|
|
ind.index = INVALID_INDUSTRY;
|
|
|
|
ind.xy = ind_base_tile;
|
2007-08-15 01:50:13 +00:00
|
|
|
ind.width = 0;
|
2007-07-09 20:08:25 +00:00
|
|
|
ind.type = type;
|
|
|
|
|
2007-10-02 16:56:45 +00:00
|
|
|
uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, itspec_index, gfx, &ind, ind_tile);
|
|
|
|
if (callback_res == CALLBACK_FAILED) {
|
|
|
|
return !IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused);
|
|
|
|
}
|
2007-07-09 18:53:43 +00:00
|
|
|
if (its->grf_prop.grffile->grf_version < 7) {
|
2008-04-18 03:17:22 +00:00
|
|
|
return callback_res != 0;
|
2007-07-09 18:53:43 +00:00
|
|
|
}
|
(svn r14563) -Fix [FS#2395]: in the case that an industry NewGRF, a shared TTDPatch and
OpenTTD feature with it's origin in TTDPatch to replace/add/change vehicles
(including e.g. maximum speed, graphics and introduction year), stations,
bridges, industries, town houses or any other graphics used by either
TTDPatch or OpenTTD, would tell that building an industry is okay, which
is queried using a so-called callback that allows the NewGRF author to
test all kinds of information about the neighbourhood where the industry
might get build, it would corrupt, i.e. overwrite with invalid data, the
structure that is used to build up an error message. This then might result
in trying to resolve an invalid StringID, an internal numeric representation
of all translateable strings, which would in it's turn trigger a safety
check that is added to ensure invalid StringIDs are never resolved.
2008-11-03 23:42:07 +00:00
|
|
|
if (callback_res == 0x400) return true;
|
2007-07-09 18:53:43 +00:00
|
|
|
|
2007-10-17 20:09:16 +00:00
|
|
|
/* Copy some parameters from the registers to the error message text ref. stack */
|
|
|
|
SwitchToErrorRefStack();
|
|
|
|
PrepareTextRefStackUsage(4);
|
|
|
|
SwitchToNormalRefStack();
|
|
|
|
|
2007-07-09 18:53:43 +00:00
|
|
|
switch (callback_res) {
|
2009-04-21 23:40:56 +00:00
|
|
|
case 0x401: _error_message = STR_ERROR_SITE_UNSUITABLE; return false;
|
|
|
|
case 0x402: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST; return false;
|
|
|
|
case 0x403: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT; return false;
|
2007-07-09 18:53:43 +00:00
|
|
|
default: _error_message = GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res); return false;
|
|
|
|
}
|
|
|
|
}
|
2007-07-11 15:03:29 +00:00
|
|
|
|
|
|
|
void AnimateNewIndustryTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
Industry *ind = GetIndustryByTile(tile);
|
|
|
|
IndustryGfx gfx = GetIndustryGfx(tile);
|
|
|
|
const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
|
|
|
|
byte animation_speed = itspec->animation_speed;
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(itspec->callback_flags, CBM_INDT_ANIM_SPEED)) {
|
2007-07-11 15:03:29 +00:00
|
|
|
uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
|
2007-11-19 18:38:10 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 0, 16);
|
2007-07-11 15:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* An animation speed of 2 means the animation frame changes 4 ticks, and
|
|
|
|
* increasing this value by one doubles the wait. 0 is the minimum value
|
|
|
|
* allowed for animation_speed, which corresponds to 30ms, and 16 is the
|
|
|
|
* maximum, corresponding to around 33 minutes. */
|
|
|
|
if ((_tick_counter % (1 << animation_speed)) != 0) return;
|
|
|
|
|
|
|
|
bool frame_set_by_callback = false;
|
|
|
|
byte frame = GetIndustryAnimationState(tile);
|
2007-10-10 00:42:52 +00:00
|
|
|
uint16 num_frames = GB(itspec->animation_info, 0, 8);
|
2007-07-11 15:03:29 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(itspec->callback_flags, CBM_INDT_ANIM_NEXT_FRAME)) {
|
|
|
|
uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_NEXT_FRAME, HasBit(itspec->animation_special_flags, 0) ? Random() : 0, 0, gfx, ind, tile);
|
2007-07-11 15:03:29 +00: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;
|
|
|
|
}
|
2008-04-21 11:29:01 +00:00
|
|
|
|
|
|
|
/* If the lower 7 bits of the upper byte of the callback
|
|
|
|
* result are not empty, it is a sound effect. */
|
|
|
|
if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
|
2007-07-11 15:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame_set_by_callback) {
|
|
|
|
if (frame < num_frames) {
|
|
|
|
frame++;
|
|
|
|
} else if (frame == num_frames && GB(itspec->animation_info, 8, 8) == 1) {
|
|
|
|
/* This animation loops, so start again from the beginning */
|
|
|
|
frame = 0;
|
|
|
|
} else {
|
|
|
|
/* This animation doesn't loop, so stay here */
|
|
|
|
DeleteAnimatedTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetIndustryAnimationState(tile, frame);
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
2008-04-21 11:29:01 +00:00
|
|
|
static void ChangeIndustryTileAnimationFrame(const IndustryTileSpec *itspec, TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
|
2007-07-11 15:03:29 +00:00
|
|
|
{
|
|
|
|
uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_START_STOP, random_bits, iat, gfx, ind, tile);
|
|
|
|
if (callback_res == CALLBACK_FAILED) return;
|
|
|
|
|
|
|
|
switch (callback_res & 0xFF) {
|
|
|
|
case 0xFD: /* Do nothing. */ break;
|
|
|
|
case 0xFE: AddAnimatedTile(tile); break;
|
|
|
|
case 0xFF: DeleteAnimatedTile(tile); break;
|
|
|
|
default:
|
|
|
|
SetIndustryAnimationState(tile, callback_res & 0xFF);
|
|
|
|
AddAnimatedTile(tile);
|
|
|
|
break;
|
|
|
|
}
|
2008-04-21 11:29:01 +00:00
|
|
|
|
|
|
|
/* If the lower 7 bits of the upper byte of the callback
|
|
|
|
* result are not empty, it is a sound effect. */
|
|
|
|
if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
|
2007-07-11 15:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
|
|
|
|
{
|
|
|
|
IndustryGfx gfx = GetIndustryGfx(tile);
|
|
|
|
const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(itspec->animation_triggers, iat)) return false;
|
2007-07-11 15:03:29 +00:00
|
|
|
|
|
|
|
Industry *ind = GetIndustryByTile(tile);
|
2008-04-21 11:29:01 +00:00
|
|
|
ChangeIndustryTileAnimationFrame(itspec, tile, iat, random, gfx, ind);
|
2007-07-11 15:03:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
uint32 random = Random();
|
|
|
|
BEGIN_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
|
|
|
|
if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
|
2007-09-24 20:56:33 +00:00
|
|
|
if (StartStopIndustryTileAnimation(tile, iat, random)) {
|
|
|
|
SB(random, 0, 16, Random());
|
|
|
|
} else {
|
|
|
|
ret = false;
|
|
|
|
}
|
2007-07-11 15:03:29 +00:00
|
|
|
}
|
|
|
|
END_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-11-11 17:56:37 +00:00
|
|
|
|
|
|
|
static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind)
|
|
|
|
{
|
|
|
|
ResolverObject object;
|
|
|
|
|
|
|
|
IndustryGfx gfx = GetIndustryGfx(tile);
|
|
|
|
const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
|
|
|
|
|
2008-04-23 00:14:49 +00:00
|
|
|
if (itspec->grf_prop.spritegroup == NULL) return;
|
|
|
|
|
2007-11-11 17:56:37 +00:00
|
|
|
NewIndustryTileResolver(&object, gfx, tile, ind);
|
|
|
|
|
|
|
|
object.callback = CBID_RANDOM_TRIGGER;
|
|
|
|
object.trigger = trigger;
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup, &object);
|
2007-11-11 17:56:37 +00:00
|
|
|
if (group == NULL) return;
|
|
|
|
|
|
|
|
byte new_random_bits = Random();
|
|
|
|
byte random_bits = GetIndustryRandomBits(tile);
|
|
|
|
random_bits &= ~object.reseed;
|
|
|
|
random_bits |= new_random_bits & object.reseed;
|
|
|
|
SetIndustryRandomBits(tile, random_bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
|
|
|
|
{
|
|
|
|
DoTriggerIndustryTile(tile, trigger, GetIndustryByTile(tile));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
|
|
|
|
{
|
|
|
|
BEGIN_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
|
|
|
|
if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
|
|
|
|
DoTriggerIndustryTile(tile, trigger, ind);
|
|
|
|
}
|
|
|
|
END_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
|
|
|
|
}
|