Files @ r13191:af1540e675da
Branch filter:

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

michi_cc
(svn r17708) -Feature [FS#2053]: [OSX] Implement clipboard support for OS X.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
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
r6884:06c993c360e4
r6884:06c993c360e4
r8225:491804378826
r6884:06c993c360e4
r6884:06c993c360e4
r10960:e97ebf9cf99b
r6884:06c993c360e4
r6884:06c993c360e4
r7229:58b7db9e5145
r9018:0a11b607e602
r7212:15205d2e5142
r12816:33099b7286d4
r6884:06c993c360e4
r7849:cfd99cb69695
r8131:7a50db7be0ff
r8213:466402e95092
r8275:5932486ddd21
r9005:e59c0b10c56a
r9718:c3fa0e5e369d
r6884:06c993c360e4
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r7234:89807e0412b9
r6884:06c993c360e4
r8458:f7e90b7c0926
r8458:f7e90b7c0926
r6884:06c993c360e4
r8458:f7e90b7c0926
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
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
r11162:b56c3cec6367
r6884:06c993c360e4
r6899:aa4751e3d864
r11162:b56c3cec6367
r6884:06c993c360e4
r6899:aa4751e3d864
r11162:b56c3cec6367
r6884:06c993c360e4
r7234:89807e0412b9
r11162:b56c3cec6367
r6884:06c993c360e4
r12816:33099b7286d4
r6899:aa4751e3d864
r6899:aa4751e3d864
r11162:b56c3cec6367
r6884:06c993c360e4
r6884:06c993c360e4
r11162:b56c3cec6367
r6884:06c993c360e4
r6884:06c993c360e4
r7233:d578434a488b
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
r7621:16fbec405049
r12816:33099b7286d4
r6884:06c993c360e4
r6884:06c993c360e4
r7621:16fbec405049
r6884:06c993c360e4
r6884:06c993c360e4
r7621:16fbec405049
r12816:33099b7286d4
r6884:06c993c360e4
r6884:06c993c360e4
r7621:16fbec405049
r6884:06c993c360e4
r6884:06c993c360e4
r7621:16fbec405049
r7621:16fbec405049
r7860:c73688ce46ca
r7621:16fbec405049
r7621:16fbec405049
r12816:33099b7286d4
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
r6970:377763da9c24
r8571:ede148e241b1
r8571:ede148e241b1
r6970:377763da9c24
r7656:8a3acdcc26d6
r7656:8a3acdcc26d6
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r11085:c87a330fb4c2
r9718:c3fa0e5e369d
r9718:c3fa0e5e369d
r6970:377763da9c24
r6970:377763da9c24
r8570:8edf335fd1b4
r6970:377763da9c24
r8570:8edf335fd1b4
r8570:8edf335fd1b4
r6970:377763da9c24
r9549:382f2ad80529
r9549:382f2ad80529
r9549:382f2ad80529
r7656:8a3acdcc26d6
r7656:8a3acdcc26d6
r11085:c87a330fb4c2
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r7333:94b25d4d956c
r7928:a80e7e05d6c5
r6970:377763da9c24
r6970:377763da9c24
r10257:e663952b858a
r10257:e663952b858a
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r7327:28855024ff6c
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r6884:06c993c360e4
r11991:9a2b087e564c
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
r6970:377763da9c24
r13050:69cb7cb85163
r6970:377763da9c24
r6970:377763da9c24
r7335:93bf5f4b6ce4
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r11991:9a2b087e564c
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r11985:d8202dea2203
r6970:377763da9c24
r6970:377763da9c24
r11985:d8202dea2203
r11985:d8202dea2203
r6970:377763da9c24
r6970:377763da9c24
r6970:377763da9c24
r7212:15205d2e5142
r7662:914446395fe3
r7662:914446395fe3
r7662:914446395fe3
r7212:15205d2e5142
r7216:15c27d8bad4a
r7662:914446395fe3
r7662:914446395fe3
r7446:06805c50a303
r7216:15c27d8bad4a
r7216:15c27d8bad4a
r7662:914446395fe3
r7662:914446395fe3
r7662:914446395fe3
r7662:914446395fe3
r7212:15205d2e5142
r8967:2959cdce0df3
r7212:15205d2e5142
r10318:d96f83f83c61
r7212:15205d2e5142
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7750:5f71f85fde96
r7212:15205d2e5142
r11725:57bc99fdc1bc
r11725:57bc99fdc1bc
r11725:57bc99fdc1bc
r7212:15205d2e5142
r7212:15205d2e5142
r7212:15205d2e5142
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r12816:33099b7286d4
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r13026:0c3f00ec0a74
r7229:58b7db9e5145
r7922:d7c3cc15726d
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7707:0f25fce9ac45
r7229:58b7db9e5145
r13026:0c3f00ec0a74
r7928:a80e7e05d6c5
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r9018:0a11b607e602
r9018:0a11b607e602
r9018:0a11b607e602
r9018:0a11b607e602
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r9018:0a11b607e602
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r9018:0a11b607e602
r9018:0a11b607e602
r9018:0a11b607e602
r9018:0a11b607e602
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7928:a80e7e05d6c5
r7229:58b7db9e5145
r12816:33099b7286d4
r9018:0a11b607e602
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r7229:58b7db9e5145
r12519:e52fcaadf579
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
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r9028:43b29ac0a74d
r9028:43b29ac0a74d
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r11991:9a2b087e564c
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
r12519:e52fcaadf579
r7860:c73688ce46ca
r7860:c73688ce46ca
r7860:c73688ce46ca
r12519:e52fcaadf579
r7860:c73688ce46ca
/* $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 "openttd.h"
#include "variables.h"
#include "debug.h"
#include "viewport_func.h"
#include "landscape.h"
#include "newgrf.h"
#include "core/random_func.hpp"
#include "newgrf_commons.h"
#include "newgrf_industries.h"
#include "newgrf_industrytiles.h"
#include "newgrf_sound.h"
#include "newgrf_text.h"
#include "industry.h"
#include "sprite.h"
#include "transparency.h"
#include "functions.h"
#include "town.h"
#include "command_func.h"
#include "animated_tile_func.h"
#include "water.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
 * @return a construction of bits obeying the newgrf format
 */
uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index)
{
	if (parameter != 0) tile = GetNearbyTile(parameter, tile); // 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
 */
static 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->xy);

		/* Animation frame. Like house variable 46 but can contain anything 0..FF. */
		case 0x44: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryAnimationState(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 GetIndustryAnimationState(tile);
			}
			return UINT_MAX;

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

	DEBUG(grf, 1, "Unhandled industry tile property 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;
	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryRandomBits(tile) : Industry::GetByTile(tile)->random;
}

static uint32 IndustryTileGetTriggers(const ResolverObject *object)
{
	const TileIndex tile = object->u.industry.tile;
	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : Industry::GetByTile(tile)->random_triggers;
}

static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
{
	const TileIndex tile = object->u.industry.tile;
	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return;

	if (object->scope == VSG_SCOPE_SELF) {
		SetIndustryTriggers(tile, triggers);
	} else {
		Industry::GetByTile(tile)->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;
	const DrawTileSeqStruct *dtss;

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

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

	foreach_draw_tile_seq(dtss, dts->seq) {
		if (GB(dtss->image.sprite, 0, SPRITE_WIDTH) == 0) continue;

		image = dtss->image.sprite;
		pal   = dtss->image.pal;

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

		if (IS_CUSTOM_SPRITE(image)) image += stage;

		pal = SpriteLayoutPaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour));

		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,
				dtss->size_z, ti->z + dtss->delta_z,
				!HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_INDUSTRIES)
			);
		} else {
			/* 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));
		}
	}
}

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

	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, &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_INDUSTRY_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, &object);
	if (group == NULL || group->type != SGT_TILELAYOUT) {
		return false;
	} else {
		const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
		/* Limit the building stage to the number of stages supplied. */
		byte stage = GetIndustryConstructionStage(ti->tile);
		stage = Clamp(stage - 4 + tlgroup->num_sprites, 0, tlgroup->num_sprites - 1);
		IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
		return true;
	}
}

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)
{
	Industry ind;
	ind.index = INVALID_INDUSTRY;
	ind.xy = ind_base_tile;
	ind.width = 0;
	ind.type = type;

	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);
	}
	if (its->grf_prop.grffile->grf_version < 7) {
		return callback_res != 0;
	}
	if (callback_res == 0x400) return true;

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

	switch (callback_res) {
		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;
		default: _error_message = GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res); return false;
	}
}

void AnimateNewIndustryTile(TileIndex tile)
{
	Industry *ind = Industry::GetByTile(tile);
	IndustryGfx gfx = GetIndustryGfx(tile);
	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
	byte animation_speed = itspec->animation_speed;

	if (HasBit(itspec->callback_mask, CBM_INDT_ANIM_SPEED)) {
		uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
		if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 0, 16);
	}

	/* 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);
	uint16 num_frames = GB(itspec->animation_info, 0, 8);

	if (HasBit(itspec->callback_mask, 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);

		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. */
			if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
		}
	}

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

static void ChangeIndustryTileAnimationFrame(const IndustryTileSpec *itspec, TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
{
	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;
	}

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

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

	if (!HasBit(itspec->animation_triggers, iat)) return false;

	Industry *ind = Industry::GetByTile(tile);
	ChangeIndustryTileAnimationFrame(itspec, tile, iat, random, gfx, ind);
	return true;
}

bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
{
	bool ret = true;
	uint32 random = Random();
	TILE_LOOP(tile, ind->width, ind->height, ind->xy) {
		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;

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

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

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

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

	const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup, &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_LOOP(tile, ind->width, ind->height, ind->xy) {
		if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
			DoTriggerIndustryTile(tile, trigger, ind);
		}
	}
}