2007-06-13 02:29:08 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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_industrytiles.cpp NewGRF handling of industry tiles. */
|
2007-06-13 02:29:08 +00:00
|
|
|
|
|
|
|
#include "stdafx.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"
|
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"
|
2009-08-30 11:47:41 +00:00
|
|
|
#include "industry.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-07-26 16:14:10 +00:00
|
|
|
#include "water.h"
|
2010-01-17 15:05:25 +00:00
|
|
|
#include "sprite.h"
|
2010-08-26 17:01:17 +00:00
|
|
|
#include "newgrf_animation_base.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
|
2010-10-19 21:00:45 +00:00
|
|
|
* @param signed_offsets Are the x and y offset encoded in parameter signed?
|
2007-06-13 02:29:08 +00:00
|
|
|
* @return a construction of bits obeying the newgrf format
|
|
|
|
*/
|
2010-10-19 21:00:45 +00:00
|
|
|
uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
2010-10-19 21:00:45 +00:00
|
|
|
if (parameter != 0) tile = GetNearbyTile(parameter, tile, signed_offsets); // 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
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* This is the position of the tile relative to the northernmost tile of the industry.
|
2007-06-13 02:29:08 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2010-02-22 14:16:41 +00:00
|
|
|
uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
|
2007-06-13 02:29:08 +00:00
|
|
|
{
|
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 */
|
2010-01-04 18:21:07 +00:00
|
|
|
case 0x43: return GetRelativePosition(tile, inds->location.tile);
|
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. */
|
2010-08-26 14:45:45 +00:00
|
|
|
case 0x44: return (IsTileType(tile, MP_INDUSTRY)) ? GetAnimationFrame(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);
|
2009-08-30 11:47:41 +00:00
|
|
|
if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == inds) {
|
2010-08-26 14:45:45 +00:00
|
|
|
return GetAnimationFrame(tile);
|
2007-06-13 18:35:06 +00:00
|
|
|
}
|
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 */
|
2010-01-21 18:32:44 +00:00
|
|
|
case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), inds, object->grffile->grfid);
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 16:43:46 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled industry tile variable 0x%X", variable);
|
2007-07-11 22:57:47 +00:00
|
|
|
|
|
|
|
*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;
|
2010-05-01 13:01:21 +00:00
|
|
|
const Industry *ind = object->u.industry.ind;
|
|
|
|
assert(ind != NULL && IsValidTile(tile));
|
|
|
|
assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
|
|
|
|
|
|
|
|
return (object->scope == VSG_SCOPE_SELF) ?
|
|
|
|
(ind->index != INVALID_INDUSTRY ? GetIndustryRandomBits(tile) : 0) :
|
|
|
|
ind->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;
|
2010-05-01 13:01:21 +00:00
|
|
|
const Industry *ind = object->u.industry.ind;
|
|
|
|
assert(ind != NULL && IsValidTile(tile));
|
|
|
|
assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
|
|
|
|
if (ind->index == INVALID_INDUSTRY) return 0;
|
|
|
|
return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : ind->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;
|
2010-05-01 13:01:21 +00:00
|
|
|
Industry *ind = object->u.industry.ind;
|
|
|
|
assert(ind != NULL && ind->index != INVALID_INDUSTRY && IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
|
2007-09-23 19:27:35 +00:00
|
|
|
|
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 {
|
2010-05-01 13:01:21 +00:00
|
|
|
ind->random_triggers = triggers;
|
2007-09-23 19:27:35 +00:00
|
|
|
}
|
2007-06-13 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2011-06-12 20:38:46 +00:00
|
|
|
/**
|
|
|
|
* Store a value into the persistent storage of the object's parent.
|
|
|
|
* @param object Object that we want to query.
|
|
|
|
* @param pos Position in the persistent storage to use.
|
|
|
|
* @param value Value to store.
|
|
|
|
*/
|
|
|
|
void IndustryTileStorePSA(ResolverObject *object, uint pos, int32 value)
|
|
|
|
{
|
|
|
|
Industry *ind = object->u.industry.ind;
|
|
|
|
if (object->scope != VSG_SCOPE_PARENT || ind->index == INVALID_INDUSTRY) return;
|
2011-06-12 20:47:45 +00:00
|
|
|
|
|
|
|
if (ind->psa == NULL) {
|
|
|
|
/* There is no need to create a storage if the value is zero. */
|
|
|
|
if (value == 0) return;
|
|
|
|
|
|
|
|
/* Create storage on first modification. */
|
|
|
|
const IndustrySpec *indsp = GetIndustrySpec(ind->type);
|
|
|
|
uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
|
|
|
|
assert(PersistentStorage::CanAllocateItem());
|
|
|
|
ind->psa = new PersistentStorage(grfid);
|
|
|
|
}
|
|
|
|
|
|
|
|
ind->psa->StoreValue(pos, value);
|
2011-06-12 20:38:46 +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;
|
2011-06-12 20:38:46 +00:00
|
|
|
res->StorePSA = IndustryTileStorePSA;
|
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;
|
2011-07-04 20:31:57 +00:00
|
|
|
res->ResetState();
|
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
|
|
|
{
|
2011-05-29 16:56:22 +00:00
|
|
|
const DrawTileSprites *dts = group->ProcessRegisters(&stage);
|
2007-06-19 17:33:12 +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-06-19 17:33:12 +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-30 15:37:24 +00:00
|
|
|
|
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'. */
|
2010-08-26 19:29:20 +00:00
|
|
|
if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
|
2008-07-26 16:14:10 +00:00
|
|
|
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
|
|
|
|
2010-01-17 15:05:25 +00:00
|
|
|
DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
|
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;
|
|
|
|
|
2010-05-01 13:01:21 +00:00
|
|
|
assert(industry != NULL && IsValidTile(tile));
|
|
|
|
assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
|
|
|
|
|
2007-06-13 02:29:08 +00:00
|
|
|
NewIndustryTileResolver(&object, gfx_id, tile, industry);
|
|
|
|
object.callback = callback;
|
|
|
|
object.callback_param1 = param1;
|
|
|
|
object.callback_param2 = param2;
|
|
|
|
|
2010-08-10 15:49:35 +00:00
|
|
|
group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup[0], &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;
|
2009-09-14 12:22:57 +00:00
|
|
|
if (HasBit(inds->callback_mask, 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 */
|
2010-04-05 21:01:02 +00:00
|
|
|
uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
|
2009-09-16 19:10:50 +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);
|
|
|
|
|
2010-08-10 15:49:35 +00:00
|
|
|
group = SpriteGroup::Resolve(inds->grf_prop.spritegroup[0], &object);
|
2010-01-08 20:42:12 +00:00
|
|
|
if (group == NULL || group->type != SGT_TILELAYOUT) {
|
2007-06-19 17:33:12 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
/* 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-06-19 17:33:12 +00:00
|
|
|
byte stage = GetIndustryConstructionStage(ti->tile);
|
2009-05-23 12:13:42 +00:00
|
|
|
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);
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Check the slope of a tile of a new industry.
|
2010-02-27 10:21:59 +00:00
|
|
|
* @param ind_base_tile Base tile of the industry.
|
|
|
|
* @param ind_tile Tile to check.
|
|
|
|
* @param its Tile specification.
|
|
|
|
* @param type Industry type.
|
|
|
|
* @param gfx Gfx of the tile.
|
|
|
|
* @param itspec_index Layout.
|
2010-05-01 13:09:49 +00:00
|
|
|
* @param initial_random_bits Random bits of industry after construction
|
2010-05-29 13:08:50 +00:00
|
|
|
* @param founder Industry founder
|
2010-10-16 13:15:54 +00:00
|
|
|
* @param creation_type The circumstances the industry is created under.
|
2010-02-27 10:21:59 +00:00
|
|
|
* @return Suceeded or failed command.
|
|
|
|
*/
|
2010-10-16 13:15:54 +00:00
|
|
|
CommandCost PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, uint itspec_index, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
|
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;
|
2010-01-04 18:21:07 +00:00
|
|
|
ind.location.tile = ind_base_tile;
|
|
|
|
ind.location.w = 0;
|
2007-07-09 20:08:25 +00:00
|
|
|
ind.type = type;
|
2010-05-01 13:09:49 +00:00
|
|
|
ind.random = initial_random_bits;
|
2010-05-29 13:08:50 +00:00
|
|
|
ind.founder = founder;
|
2007-07-09 20:08:25 +00:00
|
|
|
|
2010-10-16 13:15:54 +00:00
|
|
|
uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
|
2007-10-02 16:56:45 +00:00
|
|
|
if (callback_res == CALLBACK_FAILED) {
|
2010-02-27 10:21:59 +00:00
|
|
|
if (!IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused)) return CommandCost();
|
|
|
|
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2007-10-02 16:56:45 +00:00
|
|
|
}
|
2007-07-09 18:53:43 +00:00
|
|
|
if (its->grf_prop.grffile->grf_version < 7) {
|
2010-02-27 10:21:59 +00:00
|
|
|
if (callback_res != 0) return CommandCost();
|
|
|
|
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2007-07-09 18:53:43 +00:00
|
|
|
}
|
2011-07-03 14:32:15 +00:00
|
|
|
|
2011-07-11 16:32:19 +00:00
|
|
|
return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
|
2007-07-09 18:53:43 +00:00
|
|
|
}
|
2007-07-11 15:03:29 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
/* Simple wrapper for GetHouseCallback to keep the animation unified. */
|
2011-06-12 20:32:52 +00:00
|
|
|
uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile)
|
2007-07-11 15:03:29 +00:00
|
|
|
{
|
2011-06-12 20:32:52 +00:00
|
|
|
return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
|
2010-08-26 17:01:17 +00:00
|
|
|
}
|
2007-07-11 15:03:29 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
/** Helper class for animation control. */
|
|
|
|
struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, GetSimpleIndustryCallback> {
|
|
|
|
static const CallbackID cb_animation_speed = CBID_INDTILE_ANIMATION_SPEED;
|
|
|
|
static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
|
2007-07-11 15:03:29 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
static const IndustryTileCallbackMask cbm_animation_speed = CBM_INDT_ANIM_SPEED;
|
|
|
|
static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
|
|
|
|
};
|
2007-07-11 15:03:29 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
void AnimateNewIndustryTile(TileIndex tile)
|
2007-07-11 15:03:29 +00:00
|
|
|
{
|
2010-08-26 17:01:17 +00:00
|
|
|
const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
|
|
|
|
if (itspec == NULL) return;
|
2008-04-21 11:29:01 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
|
2007-07-11 15:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
|
|
|
|
{
|
2010-08-26 17:01:17 +00:00
|
|
|
const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
|
2007-07-11 15:03:29 +00:00
|
|
|
|
2010-08-26 15:31:40 +00:00
|
|
|
if (!HasBit(itspec->animation.triggers, iat)) return false;
|
2007-07-11 15:03:29 +00:00
|
|
|
|
2010-08-26 17:01:17 +00:00
|
|
|
IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
|
2007-07-11 15:03:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
uint32 random = Random();
|
2010-01-04 18:30:10 +00:00
|
|
|
TILE_AREA_LOOP(tile, ind->location) {
|
2007-07-11 15:03:29 +00:00
|
|
|
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
|
|
|
}
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2007-07-11 15:03:29 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-11-11 17:56:37 +00:00
|
|
|
|
2011-07-04 20:37:20 +00:00
|
|
|
/**
|
|
|
|
* Trigger random triggers for an industry tile and reseed its random bits.
|
|
|
|
* @param tile Industry tile to trigger.
|
|
|
|
* @param trigger Trigger to trigger.
|
|
|
|
* @param ind Industry of the tile.
|
|
|
|
* @param [in,out] reseed_industry Collects bits to reseed for the industry.
|
|
|
|
*/
|
|
|
|
static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
|
2007-11-11 17:56:37 +00:00
|
|
|
{
|
|
|
|
ResolverObject object;
|
|
|
|
|
2010-05-01 13:01:21 +00:00
|
|
|
assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
|
|
|
|
|
2007-11-11 17:56:37 +00:00
|
|
|
IndustryGfx gfx = GetIndustryGfx(tile);
|
|
|
|
const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
|
|
|
|
|
2010-10-28 11:10:12 +00:00
|
|
|
if (itspec->grf_prop.spritegroup[0] == NULL) return;
|
2008-04-23 00:14:49 +00:00
|
|
|
|
2007-11-11 17:56:37 +00:00
|
|
|
NewIndustryTileResolver(&object, gfx, tile, ind);
|
|
|
|
|
|
|
|
object.callback = CBID_RANDOM_TRIGGER;
|
|
|
|
object.trigger = trigger;
|
|
|
|
|
2010-08-10 15:49:35 +00:00
|
|
|
const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], &object);
|
2007-11-11 17:56:37 +00:00
|
|
|
if (group == NULL) return;
|
|
|
|
|
|
|
|
byte new_random_bits = Random();
|
|
|
|
byte random_bits = GetIndustryRandomBits(tile);
|
2011-07-04 20:37:20 +00:00
|
|
|
random_bits &= ~object.reseed[VSG_SCOPE_SELF];
|
|
|
|
random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
|
2007-11-11 17:56:37 +00:00
|
|
|
SetIndustryRandomBits(tile, random_bits);
|
2009-08-08 18:24:48 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
2011-07-04 20:37:20 +00:00
|
|
|
|
|
|
|
reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
|
2007-11-11 17:56:37 +00:00
|
|
|
}
|
|
|
|
|
2011-07-04 20:37:20 +00:00
|
|
|
/**
|
|
|
|
* Reseeds the random bits of an industry.
|
|
|
|
* @param ind Industry.
|
|
|
|
* @param reseed Bits to reseed.
|
|
|
|
*/
|
|
|
|
static void DoReseedIndustry(Industry *ind, uint32 reseed)
|
|
|
|
{
|
|
|
|
if (reseed == 0 || ind == NULL) return;
|
|
|
|
|
|
|
|
uint16 random_bits = Random();
|
|
|
|
ind->random &= reseed;
|
|
|
|
ind->random |= random_bits & reseed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a random trigger for a single industry tile.
|
|
|
|
* @param tile Industry tile to trigger.
|
|
|
|
* @param trigger Trigger to trigger.
|
|
|
|
*/
|
2007-11-11 17:56:37 +00:00
|
|
|
void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
|
|
|
|
{
|
2011-07-04 20:37:20 +00:00
|
|
|
uint32 reseed_industry = 0;
|
|
|
|
Industry *ind = Industry::GetByTile(tile);
|
|
|
|
DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
|
|
|
|
DoReseedIndustry(ind, reseed_industry);
|
2007-11-11 17:56:37 +00:00
|
|
|
}
|
|
|
|
|
2011-07-04 20:37:20 +00:00
|
|
|
/**
|
|
|
|
* Trigger a random trigger for all industry tiles.
|
|
|
|
* @param ind Industry to trigger.
|
|
|
|
* @param trigger Trigger to trigger.
|
|
|
|
*/
|
2007-11-11 17:56:37 +00:00
|
|
|
void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
|
|
|
|
{
|
2011-07-04 20:37:20 +00:00
|
|
|
uint32 reseed_industry = 0;
|
2010-01-04 18:30:10 +00:00
|
|
|
TILE_AREA_LOOP(tile, ind->location) {
|
2007-11-11 17:56:37 +00:00
|
|
|
if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
|
2011-07-04 20:37:20 +00:00
|
|
|
DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
|
2007-11-11 17:56:37 +00:00
|
|
|
}
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2011-07-04 20:37:20 +00:00
|
|
|
DoReseedIndustry(ind, reseed_industry);
|
2007-11-11 17:56:37 +00:00
|
|
|
}
|
2010-04-24 13:35:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a industry tile's spec and such so we can get a variable.
|
|
|
|
* @param ro The resolver object to fill.
|
|
|
|
* @param index The industry tile to get the data from.
|
|
|
|
*/
|
|
|
|
void GetIndustryTileResolver(ResolverObject *ro, uint index)
|
|
|
|
{
|
|
|
|
NewIndustryTileResolver(ro, GetIndustryGfx(index), index, Industry::GetByTile(index));
|
|
|
|
}
|