src/newgrf_industrytiles.cpp
author rubidium
Sun, 23 Sep 2007 19:27:35 +0000
changeset 8117 6f265223b5fa
parent 8106 9f527cc360cf
child 8118 644c9acc640c
permissions -rw-r--r--
(svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     1
/* $Id$ */
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     2
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     3
/** @file newgrf_industrytiles.cpp */
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     4
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     5
#include "stdafx.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     6
#include "openttd.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     7
#include "functions.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     8
#include "variables.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
     9
#include "debug.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    10
#include "viewport.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    11
#include "landscape.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    12
#include "newgrf.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    13
#include "industry.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    14
#include "newgrf_commons.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    15
#include "newgrf_spritegroup.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    16
#include "newgrf_callbacks.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    17
#include "newgrf_industries.h"
7725
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
    18
#include "newgrf_industrytiles.h"
7708
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
    19
#include "newgrf_text.h"
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    20
#include "industry_map.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    21
#include "clear_map.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    22
#include "table/sprites.h"
7708
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
    23
#include "table/strings.h"
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    24
#include "sprite.h"
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    25
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    26
/**
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    27
 * Based on newhouses equivalent, but adapted for newindustries
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    28
 * @param parameter from callback.  It's in fact a pair of coordinates
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    29
 * @param tile TileIndex from which the callback was initiated
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    30
 * @param index of the industry been queried for
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    31
 * @return a construction of bits obeying the newgrf format
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    32
 */
7730
a7b31d4fcc40 (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7729
diff changeset
    33
uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index)
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    34
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    35
	byte tile_type;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    36
	bool is_same_industry;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    37
7942
9f7797968d6d (svn r10904) -Fix: When using a temporary industry, make sure you do not clear some unreachable tiles upon its destruction.
belugas
parents: 7831
diff changeset
    38
	if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    39
	is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    40
	tile_type = GetTerrainType(tile) << 2 | (IsTileType(tile, MP_WATER) ? 1 : 0) << 1 | (is_same_industry ? 1 : 0);
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    41
8019
35497d94c8d2 (svn r11042) -Fix [FS#1196]: so newgrf callbacks returned slightly wrong heights (height of nothern corner instead of height of lowest corner). Patch by frosch.
rubidium
parents: 7942
diff changeset
    42
	uint z;
35497d94c8d2 (svn r11042) -Fix [FS#1196]: so newgrf callbacks returned slightly wrong heights (height of nothern corner instead of height of lowest corner). Patch by frosch.
rubidium
parents: 7942
diff changeset
    43
	Slope tileh = GetTileSlope(tile, &z);
35497d94c8d2 (svn r11042) -Fix [FS#1196]: so newgrf callbacks returned slightly wrong heights (height of nothern corner instead of height of lowest corner). Patch by frosch.
rubidium
parents: 7942
diff changeset
    44
	return GetTileType(tile) << 24 | z << 16 | tile_type << 8 | tileh;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    45
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    46
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    47
/** This is the position of the tile relative to the northernmost tile of the industry.
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    48
 * Format: 00yxYYXX
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    49
 * Variable  Content
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    50
 * x         the x offset from the northernmost tile
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    51
 * XX        same, but stored in a byte instead of a nibble
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    52
 * y         the y offset from the northernmost tile
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    53
 * YY        same, but stored in a byte instead of a nibble
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    54
 * @param tile TileIndex of the tile to evaluate
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    55
 * @param ind_tile northernmost tile of the industry
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    56
 */
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    57
static uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    58
{
7415
36ea2aa930eb (svn r10172) -Fix(r10127): Wrong evaluation order on variable 0x41 for industry tile.
belugas
parents: 7395
diff changeset
    59
	byte x = TileX(tile) - TileX(ind_tile);
36ea2aa930eb (svn r10172) -Fix(r10127): Wrong evaluation order on variable 0x41 for industry tile.
belugas
parents: 7395
diff changeset
    60
	byte y = TileY(tile) - TileY(ind_tile);
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    61
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    62
	return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    63
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    64
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    65
static uint32 IndustryTileGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    66
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    67
	const Industry *inds = object->u.industry.ind;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    68
	TileIndex tile       = object->u.industry.tile;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    69
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    70
	if (object->scope == VSG_SCOPE_PARENT) {
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    71
		return IndustryGetVariable(object, variable, parameter, available);
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    72
	}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    73
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    74
	switch (variable) {
7395
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    75
		 /* Construction state of the tile: a value between 0 and 3 */
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    76
		case 0x40 : return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    77
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    78
		case 0x41 : return GetTerrainType(tile);
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    79
7395
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    80
		/* Current town zone of the tile in the nearest town */
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    81
		case 0x42 : return GetTownRadiusGroup(ClosestTownFromTile(tile, (uint)-1), tile);
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    82
7395
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    83
		/* Relative position */
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    84
		case 0x43 : return GetRelativePosition(tile, inds->xy);
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    85
7395
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    86
		/* Animation frame. Like house variable 46 but can contain anything 0..FF. */
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    87
		case 0x44 : return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryAnimationState(tile) : 0;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    88
7395
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    89
		/* Land info of nearby tiles */
7708
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
    90
		case 0x60 : return GetNearbyIndustryTileInformation(parameter, tile, inds == NULL ? (IndustryID)INVALID_INDUSTRY : inds->index);
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    91
7730
a7b31d4fcc40 (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7729
diff changeset
    92
		/* Animation stage of nearby tiles */
a7b31d4fcc40 (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7729
diff changeset
    93
		case 0x61 : {
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    94
			tile = GetNearbyTile(parameter, tile);
7395
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    95
			if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == inds) {
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    96
				return GetIndustryAnimationState(tile);
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    97
			}
ed1cf28fb1b4 (svn r10146) -Codechange: Silence a compiler warning
belugas
parents: 7380
diff changeset
    98
			return 0xFFFFFFFF;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
    99
		}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   100
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   101
		/* Get industry tile ID at offset */
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   102
		case 0x62 : return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), tile, inds);
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   103
	}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   104
7729
21b3ddd80928 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7725
diff changeset
   105
	DEBUG(grf, 1, "Unhandled industry tile property 0x%X", variable);
21b3ddd80928 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7725
diff changeset
   106
21b3ddd80928 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7725
diff changeset
   107
	*available = false;
21b3ddd80928 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7725
diff changeset
   108
	return (uint32)-1;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   109
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   110
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   111
static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const SpriteGroup *group)
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   112
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   113
	/* IndustryTile do not have 'real' groups.  Or do they?? */
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   114
	return NULL;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   115
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   116
8117
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   117
static uint32 IndustryTileGetRandomBits(const ResolverObject *object)
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   118
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   119
	const TileIndex tile = object->u.industry.tile;
8117
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   120
	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   121
	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryRandomBits(tile) : 0; //GetIndustryByTile(tile)->random_bits;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   122
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   123
8117
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   124
static uint32 IndustryTileGetTriggers(const ResolverObject *object)
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   125
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   126
	const TileIndex tile = object->u.industry.tile;
8117
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   127
	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   128
	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : 0; //GetIndustryByTile(tile)->triggers;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   129
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   130
8117
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   131
static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   132
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   133
	const TileIndex tile = object->u.industry.tile;
8117
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   134
	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return;
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   135
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   136
	if (object->scope != VSG_SCOPE_SELF) {
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   137
		SetIndustryTriggers(tile, triggers);
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   138
	} else {
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   139
		//GetIndustryByTile(tile)->triggers = triggers;
6f265223b5fa (svn r11151) -Codechange: add (partial) support for randomizing industry triggers (part of the backend for it). Furthermore update the documentation of the map's bits wrt to industries.
rubidium
parents: 8106
diff changeset
   140
	}
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   141
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   142
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   143
static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   144
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   145
	res->GetRandomBits = IndustryTileGetRandomBits;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   146
	res->GetTriggers   = IndustryTileGetTriggers;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   147
	res->SetTriggers   = IndustryTileSetTriggers;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   148
	res->GetVariable   = IndustryTileGetVariable;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   149
	res->ResolveReal   = IndustryTileResolveReal;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   150
8106
9f527cc360cf (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 8019
diff changeset
   151
	res->psa             = &indus->psa;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   152
	res->u.industry.tile = tile;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   153
	res->u.industry.ind  = indus;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   154
	res->u.industry.gfx  = gfx;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   155
7823
cad4dec2b899 (svn r10690) -Codechange: use the enum that describes all callback IDs in favor of "just" using an untyped integer.
rubidium
parents: 7735
diff changeset
   156
	res->callback        = CBID_NO_CALLBACK;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   157
	res->callback_param1 = 0;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   158
	res->callback_param2 = 0;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   159
	res->last_value      = 0;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   160
	res->trigger         = 0;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   161
	res->reseed          = 0;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   162
}
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   163
7466
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   164
void IndustryDrawTileLayout(const TileInfo *ti, const SpriteGroup *group, byte rnd_color, byte stage, IndustryGfx gfx)
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   165
{
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   166
	const DrawTileSprites *dts = group->g.layout.dts;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   167
	const DrawTileSeqStruct *dtss;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   168
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   169
	SpriteID image = dts->ground_sprite;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   170
	SpriteID pal   = dts->ground_pal;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   171
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   172
	if (GB(image, 0, SPRITE_WIDTH) != 0) DrawGroundSprite(image, pal);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   173
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   174
	foreach_draw_tile_seq(dtss, dts->seq) {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   175
		if (GB(dtss->image, 0, SPRITE_WIDTH) == 0) continue;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   176
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   177
		image = dtss->image + stage;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   178
		pal   = dtss->pal;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   179
7829
88883899c9e6 (svn r10696) -Codechange: remove duplication of the "make sprite transparent" code.
rubidium
parents: 7823
diff changeset
   180
		if (HASBIT(image, PALETTE_MODIFIER_COLOR)) {
7466
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   181
			pal = GENERAL_SPRITE_COLOR(rnd_color);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   182
		} else {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   183
			pal = PAL_NONE;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   184
		}
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   185
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   186
		if ((byte)dtss->delta_z != 0x80) {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   187
			AddSortableSpriteToDraw(
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   188
				image, pal,
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   189
				ti->x + dtss->delta_x, ti->y + dtss->delta_y,
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   190
				dtss->size_x, dtss->size_y,
7829
88883899c9e6 (svn r10696) -Codechange: remove duplication of the "make sprite transparent" code.
rubidium
parents: 7823
diff changeset
   191
				dtss->size_z, ti->z + dtss->delta_z,
88883899c9e6 (svn r10696) -Codechange: remove duplication of the "make sprite transparent" code.
rubidium
parents: 7823
diff changeset
   192
				!HASBIT(image, SPRITE_MODIFIER_OPAQUE) && HASBIT(_transparent_opt, TO_INDUSTRIES)
7466
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   193
			);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   194
		} else {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   195
			AddChildSpriteScreen(image, pal, dtss->delta_x, dtss->delta_y);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   196
		}
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   197
	}
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   198
}
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   199
7823
cad4dec2b899 (svn r10690) -Codechange: use the enum that describes all callback IDs in favor of "just" using an untyped integer.
rubidium
parents: 7735
diff changeset
   200
uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   201
{
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   202
	ResolverObject object;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   203
	const SpriteGroup *group;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   204
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   205
	NewIndustryTileResolver(&object, gfx_id, tile, industry);
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   206
	object.callback = callback;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   207
	object.callback_param1 = param1;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   208
	object.callback_param2 = param2;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   209
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   210
	group = Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   211
	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   212
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   213
	return group->g.callback.result;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents:
diff changeset
   214
}
7466
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   215
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   216
bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   217
{
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   218
	const SpriteGroup *group;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   219
	ResolverObject object;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   220
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   221
	if (ti->tileh != SLOPE_FLAT) {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   222
		bool draw_old_one = true;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   223
		if (HASBIT(inds->callback_flags, CBM_INDT_DRAW_FOUNDATIONS)) {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   224
			/* Called to determine the type (if any) of foundation to draw for industry tile */
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   225
			uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
7704
f5961e82f08a (svn r10486) -Fix: do not draw foundations when the "should I draw foundations" callbacks says to do so.
rubidium
parents: 7466
diff changeset
   226
			draw_old_one = callback_res != 0;
7466
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   227
		}
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   228
7831
5dded9b03500 (svn r10698) -Codechange [FS#1082]: simplify the code related to foundations. Primarily removal of (duplicated|magic) code and introduction of few helper functions to ease foundation determination. Patch by frosch.
rubidium
parents: 7829
diff changeset
   229
		if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
7466
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   230
	}
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   231
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   232
	NewIndustryTileResolver(&object, gfx, ti->tile, i);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   233
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   234
	group = Resolve(inds->grf_prop.spritegroup, &object);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   235
	if (group == NULL || group->type != SGT_TILELAYOUT) {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   236
		return false;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   237
	} else {
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   238
		/* Limit the building stage to the number of stages supplied. */
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   239
		byte stage = GetIndustryConstructionStage(ti->tile);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   240
		stage = clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   241
		IndustryDrawTileLayout(ti, group, i->random_color, stage, gfx);
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   242
		return true;
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   243
	}
01be88d1c0a6 (svn r10226) -Codechange: Add support for newindustry tiles drawing.
belugas
parents: 7415
diff changeset
   244
}
7708
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   245
7712
e217909bd0d5 (svn r10494) -Fix (r10490): possible null dereference in some exotic newgrfs.
rubidium
parents: 7708
diff changeset
   246
bool PerformIndustryTileSlopeCheck(TileIndex tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx)
7708
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   247
{
7712
e217909bd0d5 (svn r10494) -Fix (r10490): possible null dereference in some exotic newgrfs.
rubidium
parents: 7708
diff changeset
   248
	Industry ind;
e217909bd0d5 (svn r10494) -Fix (r10490): possible null dereference in some exotic newgrfs.
rubidium
parents: 7708
diff changeset
   249
	ind.xy = 0;
7942
9f7797968d6d (svn r10904) -Fix: When using a temporary industry, make sure you do not clear some unreachable tiles upon its destruction.
belugas
parents: 7831
diff changeset
   250
	ind.width = 0;
7712
e217909bd0d5 (svn r10494) -Fix (r10490): possible null dereference in some exotic newgrfs.
rubidium
parents: 7708
diff changeset
   251
	ind.type = type;
e217909bd0d5 (svn r10494) -Fix (r10490): possible null dereference in some exotic newgrfs.
rubidium
parents: 7708
diff changeset
   252
e217909bd0d5 (svn r10494) -Fix (r10490): possible null dereference in some exotic newgrfs.
rubidium
parents: 7708
diff changeset
   253
	uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, 0, gfx, &ind, tile);
7708
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   254
	if (its->grf_prop.grffile->grf_version < 7) {
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   255
		return callback_res != 0;
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   256
	}
7735
6c600a540a44 (svn r10520) -Fix: when a "can I build this industry tile here" callback did not fail, it doesn't automatically mean that the industry tile can be built there.
rubidium
parents: 7730
diff changeset
   257
	if (callback_res == CALLBACK_FAILED) return false;
7708
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   258
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   259
	switch (callback_res) {
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   260
		case 0x400: return true;
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   261
		case 0x401: _error_message = STR_0239_SITE_UNSUITABLE;                 return false;
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   262
		case 0x402: _error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST; return false;
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   263
		case 0x403: _error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT;     return false;
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   264
		default: _error_message = GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res); return false;
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   265
	}
595630b38fbf (svn r10490) -Codechange: add support for industries on non-flat surfaces.
rubidium
parents: 7704
diff changeset
   266
}
7725
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   267
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   268
void AnimateNewIndustryTile(TileIndex tile)
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   269
{
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   270
	Industry *ind = GetIndustryByTile(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   271
	IndustryGfx gfx = GetIndustryGfx(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   272
	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   273
	byte animation_speed = itspec->animation_speed;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   274
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   275
	if (HASBIT(itspec->callback_flags, CBM_INDT_ANIM_SPEED)) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   276
		uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   277
		if (callback_res != CALLBACK_FAILED) animation_speed = clamp(callback_res & 0xFF, 0, 16);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   278
	}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   279
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   280
	/* An animation speed of 2 means the animation frame changes 4 ticks, and
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   281
	 * increasing this value by one doubles the wait. 0 is the minimum value
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   282
	 * allowed for animation_speed, which corresponds to 30ms, and 16 is the
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   283
	 * maximum, corresponding to around 33 minutes. */
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   284
	if ((_tick_counter % (1 << animation_speed)) != 0) return;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   285
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   286
	bool frame_set_by_callback = false;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   287
	byte frame = GetIndustryAnimationState(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   288
	uint16 num_frames = GB(itspec->animation_info, 0, 8) + 1;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   289
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   290
	if (HASBIT(itspec->callback_flags, CBM_INDT_ANIM_NEXT_FRAME)) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   291
		uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_NEXT_FRAME, HASBIT(itspec->animation_special_flags, 0) ? Random() : 0, 0, gfx, ind, tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   292
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   293
		if (callback_res != CALLBACK_FAILED) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   294
			frame_set_by_callback = true;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   295
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   296
			switch (callback_res & 0xFF) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   297
				case 0xFF:
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   298
					DeleteAnimatedTile(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   299
					break;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   300
				case 0xFE:
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   301
					/* Carry on as normal. */
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   302
					frame_set_by_callback = false;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   303
					break;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   304
				default:
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   305
					frame = callback_res & 0xFF;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   306
					break;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   307
			}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   308
		}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   309
	}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   310
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   311
	if (!frame_set_by_callback) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   312
		if (frame < num_frames) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   313
			frame++;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   314
		} else if (frame == num_frames && GB(itspec->animation_info, 8, 8) == 1) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   315
			/* This animation loops, so start again from the beginning */
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   316
			frame = 0;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   317
		} else {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   318
			/* This animation doesn't loop, so stay here */
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   319
			DeleteAnimatedTile(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   320
		}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   321
	}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   322
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   323
	SetIndustryAnimationState(tile, frame);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   324
	MarkTileDirtyByTile(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   325
}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   326
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   327
static void ChangeIndustryTileAnimationFrame(TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   328
{
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   329
	uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_START_STOP, random_bits, iat, gfx, ind, tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   330
	if (callback_res == CALLBACK_FAILED) return;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   331
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   332
	switch (callback_res & 0xFF) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   333
		case 0xFD: /* Do nothing. */         break;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   334
		case 0xFE: AddAnimatedTile(tile);    break;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   335
		case 0xFF: DeleteAnimatedTile(tile); break;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   336
		default:
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   337
			SetIndustryAnimationState(tile, callback_res & 0xFF);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   338
			AddAnimatedTile(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   339
			break;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   340
	}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   341
}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   342
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   343
bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   344
{
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   345
	IndustryGfx gfx = GetIndustryGfx(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   346
	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   347
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   348
	if (!HASBIT(itspec->animation_triggers, iat)) return false;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   349
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   350
	Industry *ind = GetIndustryByTile(tile);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   351
	ChangeIndustryTileAnimationFrame(tile, iat, random, gfx, ind);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   352
	return true;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   353
}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   354
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   355
bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   356
{
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   357
	bool ret = true;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   358
	uint32 random = Random();
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   359
	BEGIN_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   360
		if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   361
			ret &= StartStopIndustryTileAnimation(tile, iat, random);
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   362
			SB(random, 0, 16, Random());
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   363
		}
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   364
	END_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   365
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   366
	return ret;
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7712
diff changeset
   367
}