Files @ r17613:a9b2554a5d79
Branch filter:

Location: cpp/openttd-patchpack/source/src/newgrf_industrytiles.cpp - annotation

rubidium
(svn r22387) -Fix-ish [FS#4601]: Windows' recv seems to return "graceful closed" before having passed the remaining buffer which causes OpenTTD to think all connections are "incorrectly" terminated, i.e. without the "I'm leaving" packet from the client. So let the client wait a tiny bit after sending the "I'm leaving" packet and before gracefully closing the connection
r6884:06c993c360e4
r6884:06c993c360e4
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r8225:491804378826
r6884:06c993c360e4
r6884:06c993c360e4
r7229:58b7db9e5145
r9018:0a11b607e602
r7212:15205d2e5142
r12816:33099b7286d4
r8213:466402e95092
r8275:5932486ddd21
r9718:c3fa0e5e369d
r14284:8fe3cab84763
r15929:5d5037945a0e
r6884:06c993c360e4
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r16279:c9054b28cdd6
r6884:06c993c360e4
r6884:06c993c360e4
r16279:c9054b28cdd6
r6884:06c993c360e4
r16279:c9054b28cdd6
r8458:f7e90b7c0926
r6884:06c993c360e4
r8458:f7e90b7c0926
r6884:06c993c360e4
r6884:06c993c360e4
r15610:623a23fb6560
r15610:623a23fb6560
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r14608:a81032019c31
r6884:06c993c360e4
r6919:aea636af9c8e
r6919:aea636af9c8e
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r11162:b56c3cec6367
r11162:b56c3cec6367
r6884:06c993c360e4
r11162:b56c3cec6367
r11162:b56c3cec6367
r6884:06c993c360e4
r6899:aa4751e3d864
r11162:b56c3cec6367
r6884:06c993c360e4
r6899:aa4751e3d864
r14159:6cb75763eb03
r6884:06c993c360e4
r6899:aa4751e3d864
r15924:f5bba173626e
r6884:06c993c360e4
r6899:aa4751e3d864
r11162:b56c3cec6367
r6884:06c993c360e4
r7234:89807e0412b9
r11162:b56c3cec6367
r6884:06c993c360e4
r12816:33099b7286d4
r15924:f5bba173626e
r6899:aa4751e3d864
r11162:b56c3cec6367
r6884:06c993c360e4
r6884:06c993c360e4
r14315:8b21eb31a313
r6884:06c993c360e4
r6884:06c993c360e4
r16464:ae3f342df07b
r7233:d578434a488b
r7233:d578434a488b
r11163:407814b6138f
r6884:06c993c360e4
r6884:06c993c360e4
r11985:d8202dea2203
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r7621:16fbec405049
r6884:06c993c360e4
r6884:06c993c360e4
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r6884:06c993c360e4
r6884:06c993c360e4
r7621:16fbec405049
r6884:06c993c360e4
r6884:06c993c360e4
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r6884:06c993c360e4
r6884:06c993c360e4
r7621:16fbec405049
r6884:06c993c360e4
r6884:06c993c360e4
r15104:1752997daf93
r15104:1752997daf93
r7621:16fbec405049
r7860:c73688ce46ca
r7621:16fbec405049
r7621:16fbec405049
r15104:1752997daf93
r7621:16fbec405049
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r7610:6031d09c03ef
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r8240:2b0d126ae152
r6884:06c993c360e4
r7327:28855024ff6c
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r8756:3b4554f1d3d4
r9750:1e2535e15958
r9750:1e2535e15958
r9750:1e2535e15958
r6884:06c993c360e4
r6884:06c993c360e4
r11985:d8202dea2203
r6970:377763da9c24
r11985:d8202dea2203
r6970:377763da9c24
r8571:ede148e241b1
r14309:f445232b17f6
r6970:377763da9c24
r14280:9e2593121da6
r7656:8a3acdcc26d6
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r15933:39e6324db57a
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r11085:c87a330fb4c2
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r6970:377763da9c24
r14284:8fe3cab84763
r6970:377763da9c24
r6970:377763da9c24
r7327:28855024ff6c
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r15104:1752997daf93
r15104:1752997daf93
r15104:1752997daf93
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r15751:f4fe02ef4406
r6884:06c993c360e4
r6884:06c993c360e4
r11985:d8202dea2203
r6884:06c993c360e4
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r13026:0c3f00ec0a74
r6970:377763da9c24
r14948:064e798608f8
r13050:69cb7cb85163
r6970:377763da9c24
r6970:377763da9c24
r7335:93bf5f4b6ce4
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r15751:f4fe02ef4406
r14200:1807a9ce785f
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r14200:1807a9ce785f
r6970:377763da9c24
r13966:e189dfb4ebfa
r11985:d8202dea2203
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r7212:15205d2e5142
r7662:914446395fe3
r7662:914446395fe3
r15610:623a23fb6560
r15610:623a23fb6560
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r15105:18f362fd63e2
r15256:e22f3389fd3a
r16231:76e02ca8f4e5
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r16231:76e02ca8f4e5
r7212:15205d2e5142
r7216:15c27d8bad4a
r7662:914446395fe3
r14159:6cb75763eb03
r14159:6cb75763eb03
r7216:15c27d8bad4a
r15105:18f362fd63e2
r15256:e22f3389fd3a
r7216:15c27d8bad4a
r16231:76e02ca8f4e5
r7662:914446395fe3
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r7662:914446395fe3
r7212:15205d2e5142
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r7212:15205d2e5142
r14677:2cb23a1debe3
r7212:15205d2e5142
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7212:15205d2e5142
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r14677:2cb23a1debe3
r7212:15205d2e5142
r7212:15205d2e5142
r7229:58b7db9e5145
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r15929:5d5037945a0e
r7229:58b7db9e5145
r7229:58b7db9e5145
r15929:5d5037945a0e
r15929:5d5037945a0e
r9018:0a11b607e602
r15929:5d5037945a0e
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r15929:5d5037945a0e
r7229:58b7db9e5145
r15925:9a7fe46227a0
r7229:58b7db9e5145
r15929:5d5037945a0e
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r14160:24d7cfd54d1c
r7229:58b7db9e5145
r7627:149960d8e202
r7627:149960d8e202
r7627:149960d8e202
r7627:149960d8e202
r7627:149960d8e202
r7229:58b7db9e5145
r12519:e52fcaadf579
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r15104:1752997daf93
r15104:1752997daf93
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r16331:77622ea88835
r9028:43b29ac0a74d
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r15751:f4fe02ef4406
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r12653:17768ec4dbe2
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r12816:33099b7286d4
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r14160:24d7cfd54d1c
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r12519:e52fcaadf579
r7860:c73688ce46ca
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
r15071:e236d80508bb
/* $Id$ */

/*
 * 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/>.
 */

/** @file newgrf_industrytiles.cpp NewGRF handling of industry tiles. */

#include "stdafx.h"
#include "debug.h"
#include "viewport_func.h"
#include "landscape.h"
#include "newgrf.h"
#include "newgrf_industrytiles.h"
#include "newgrf_sound.h"
#include "newgrf_text.h"
#include "industry.h"
#include "town.h"
#include "command_func.h"
#include "water.h"
#include "sprite.h"
#include "newgrf_animation_base.h"

#include "table/strings.h"

/**
 * 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
 * @param signed_offsets Are the x and y offset encoded in parameter signed?
 * @return a construction of bits obeying the newgrf format
 */
uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets)
{
	if (parameter != 0) tile = GetNearbyTile(parameter, tile, signed_offsets); // only perform if it is required
	bool is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);

	return GetNearbyTileInformation(tile) | (is_same_industry ? 1 : 0) << 8;
}

/**
 * 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
 */
uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
{
	byte x = TileX(tile) - TileX(ind_tile);
	byte y = TileY(tile) - TileY(ind_tile);

	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) {
		/* Construction state of the tile: a value between 0 and 3 */
		case 0x40: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;

		/* Terrain type */
		case 0x41: return GetTerrainType(tile);

		/* Current town zone of the tile in the nearest town */
		case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);

		/* Relative position */
		case 0x43: return GetRelativePosition(tile, inds->location.tile);

		/* Animation frame. Like house variable 46 but can contain anything 0..FF. */
		case 0x44: return (IsTileType(tile, MP_INDUSTRY)) ? GetAnimationFrame(tile) : 0;

		/* Land info of nearby tiles */
		case 0x60: return GetNearbyIndustryTileInformation(parameter, tile, inds == NULL ? (IndustryID)INVALID_INDUSTRY : inds->index);

		/* Animation stage of nearby tiles */
		case 0x61:
			tile = GetNearbyTile(parameter, tile);
			if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == inds) {
				return GetAnimationFrame(tile);
			}
			return UINT_MAX;

		/* Get industry tile ID at offset */
		case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), inds, object->grffile->grfid);
	}

	DEBUG(grf, 1, "Unhandled industry tile variable 0x%X", variable);

	*available = false;
	return UINT_MAX;
}

static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
{
	/* IndustryTile do not have 'real' groups.  Or do they?? */
	return NULL;
}

static uint32 IndustryTileGetRandomBits(const ResolverObject *object)
{
	const TileIndex tile = object->u.industry.tile;
	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;
}

static uint32 IndustryTileGetTriggers(const ResolverObject *object)
{
	const TileIndex tile = object->u.industry.tile;
	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;
}

static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
{
	const TileIndex tile = object->u.industry.tile;
	Industry *ind = object->u.industry.ind;
	assert(ind != NULL && ind->index != INVALID_INDUSTRY && IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));

	if (object->scope == VSG_SCOPE_SELF) {
		SetIndustryTriggers(tile, triggers);
	} else {
		ind->random_triggers = triggers;
	}
}

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;

	res->psa             = &indus->psa;
	res->u.industry.tile = tile;
	res->u.industry.ind  = indus;
	res->u.industry.gfx  = gfx;
	res->u.industry.type = indus->type;

	res->callback        = CBID_NO_CALLBACK;
	res->callback_param1 = 0;
	res->callback_param2 = 0;
	res->last_value      = 0;
	res->trigger         = 0;
	res->reseed          = 0;
	res->count           = 0;

	const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
	res->grffile         = (its != NULL ? its->grf_prop.grffile : NULL);
}

static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
{
	const DrawTileSprites *dts = group->dts;

	SpriteID image = dts->ground.sprite;
	PaletteID pal  = dts->ground.pal;

	if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;

	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 && IsTileOnWater(ti->tile)) {
			DrawWaterClassGround(ti);
		} else {
			DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
		}
	}

	DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
}

uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
{
	ResolverObject object;
	const SpriteGroup *group;

	assert(industry != NULL && IsValidTile(tile));
	assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));

	NewIndustryTileResolver(&object, gfx_id, tile, industry);
	object.callback = callback;
	object.callback_param1 = param1;
	object.callback_param2 = param2;

	group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup[0], &object);
	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;

	return group->GetCallbackResult();
}

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;
		if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
			/* Called to determine the type (if any) of foundation to draw for industry tile */
			uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
			draw_old_one = (callback_res != 0);
		}

		if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
	}

	NewIndustryTileResolver(&object, gfx, ti->tile, i);

	group = SpriteGroup::Resolve(inds->grf_prop.spritegroup[0], &object);
	if (group == NULL || group->type != SGT_TILELAYOUT) {
		return false;
	} else {
		/* Limit the building stage to the number of stages supplied. */
		const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
		byte stage = GetIndustryConstructionStage(ti->tile);
		stage = Clamp(stage - 4 + tlgroup->num_building_stages, 0, tlgroup->num_building_stages - 1);
		IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
		return true;
	}
}

extern bool IsSlopeRefused(Slope current, Slope refused);

/**
 * Check the slope of a tile of a new industry.
 * @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.
 * @param initial_random_bits Random bits of industry after construction
 * @param founder       Industry founder
 * @param creation_type The circumstances the industry is created under.
 * @return Suceeded or failed command.
 */
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)
{
	Industry ind;
	ind.index = INVALID_INDUSTRY;
	ind.location.tile = ind_base_tile;
	ind.location.w = 0;
	ind.type = type;
	ind.random = initial_random_bits;
	ind.founder = founder;

	uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
	if (callback_res == CALLBACK_FAILED) {
		if (!IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused)) return CommandCost();
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
	}
	if (its->grf_prop.grffile->grf_version < 7) {
		if (callback_res != 0) return CommandCost();
		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
	}
	if (callback_res == 0x400) return CommandCost();

	/* Copy some parameters from the registers to the error message text ref. stack */
	SwitchToErrorRefStack();
	PrepareTextRefStackUsage(4);
	SwitchToNormalRefStack();

	switch (callback_res) {
		case 0x401: return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
		case 0x402: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
		case 0x403: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
		default:    return_cmd_error(GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res));
	}
}

/* Simple wrapper for GetHouseCallback to keep the animation unified. */
uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, const Industry *ind, TileIndex tile)
{
	return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), const_cast<Industry *>(ind), tile);
}

/** 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;

	static const IndustryTileCallbackMask cbm_animation_speed      = CBM_INDT_ANIM_SPEED;
	static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
};

void AnimateNewIndustryTile(TileIndex tile)
{
	const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
	if (itspec == NULL) return;

	IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
}

bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
{
	const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));

	if (!HasBit(itspec->animation.triggers, iat)) return false;

	IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
	return true;
}

bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
{
	bool ret = true;
	uint32 random = Random();
	TILE_AREA_LOOP(tile, ind->location) {
		if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
			if (StartStopIndustryTileAnimation(tile, iat, random)) {
				SB(random, 0, 16, Random());
			} else {
				ret = false;
			}
		}
	}

	return ret;
}

static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind)
{
	ResolverObject object;

	assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));

	IndustryGfx gfx = GetIndustryGfx(tile);
	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);

	if (itspec->grf_prop.spritegroup[0] == NULL) return;

	NewIndustryTileResolver(&object, gfx, tile, ind);

	object.callback = CBID_RANDOM_TRIGGER;
	object.trigger = trigger;

	const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], &object);
	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);
	MarkTileDirtyByTile(tile);
}

void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
{
	DoTriggerIndustryTile(tile, trigger, Industry::GetByTile(tile));
}

void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
{
	TILE_AREA_LOOP(tile, ind->location) {
		if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
			DoTriggerIndustryTile(tile, trigger, ind);
		}
	}
}

/**
 * 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));
}