src/newgrf_industries.cpp
author rubidium
Sat, 17 Nov 2007 12:42:15 +0000
changeset 7898 7c6a9c9030b5
parent 7860 5cdd0d914013
child 7922 a7e266f966d9
permissions -rw-r--r--
(svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     1
/* $Id$ */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     2
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     3
/** @file newgrf_industries.cpp */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     4
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     5
#include "stdafx.h"
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     6
#include "openttd.h"
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     7
#include "debug.h"
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     8
#include "functions.h"
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     9
#include "macros.h"
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
    10
#include "variables.h"
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
    11
#include "landscape.h"
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
    12
#include "table/strings.h"
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    13
#include "industry.h"
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    14
#include "industry_map.h"
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    15
#include "newgrf.h"
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    16
#include "newgrf_callbacks.h"
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    17
#include "newgrf_spritegroup.h"
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    18
#include "newgrf_industries.h"
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
    19
#include "newgrf_industrytiles.h"
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    20
#include "newgrf_commons.h"
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
    21
#include "newgrf_text.h"
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
    22
#include "newgrf_town.h"
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
    23
#include "date.h"
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    24
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    25
/* Since the industry IDs defined by the GRF file don't necessarily correlate
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    26
 * to those used by the game, the IDs used for overriding old industries must be
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    27
 * translated when the idustry spec is set. */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    28
IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
7000
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6884
diff changeset
    29
IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    30
7233
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    31
IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    32
{
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    33
	if (grf_type == IT_INVALID) return IT_INVALID;
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    34
	if (!HASBIT(grf_type, 7)) return GB(grf_type, 0, 6);
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    35
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    36
	return _industry_mngr.GetID(GB(grf_type, 0, 6), grf_id);
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    37
}
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    38
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    39
/**
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    40
 * Finds the distance for the closest tile with water/land given a tile
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    41
 * @param tile  the tile to find the distance too
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    42
 * @param water whether to find water or land
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    43
 * @note FAILS when an industry should be seen as water
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    44
 */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    45
static uint GetClosestWaterDistance(TileIndex tile, bool water)
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    46
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    47
	TileIndex t;
7236
07ceb37a1b63 (svn r10517) -Fix: the "closest distance to water/land" calculation gave the distance to the north-eastern border instead of the closest water/land.
rubidium
parents: 7234
diff changeset
    48
	int best_dist;
07ceb37a1b63 (svn r10517) -Fix: the "closest distance to water/land" calculation gave the distance to the north-eastern border instead of the closest water/land.
rubidium
parents: 7234
diff changeset
    49
	for (t = 0; t < MapSize(); t++) {
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    50
		if (IsTileType(t, MP_WATER) == water) break;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    51
	}
7599
0d6d44f403c5 (svn r11126) -Fix: the algorithm to determine the closest water/land was flawed.
rubidium
parents: 7462
diff changeset
    52
	if (t == MapSize() && !water) return 0x200;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    53
	best_dist = DistanceManhattan(tile, t);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    54
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    55
	for (; t < MapSize(); t++) {
7236
07ceb37a1b63 (svn r10517) -Fix: the "closest distance to water/land" calculation gave the distance to the north-eastern border instead of the closest water/land.
rubidium
parents: 7234
diff changeset
    56
		int dist = DistanceManhattan(tile, t);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    57
		if (dist < best_dist) {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    58
			if (IsTileType(t, MP_WATER) == water) best_dist = dist;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    59
		} else {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    60
			/* When the Y distance between the current row and the 'source' tile
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    61
			 * is larger than the best distance, we've found the best distance */
7236
07ceb37a1b63 (svn r10517) -Fix: the "closest distance to water/land" calculation gave the distance to the north-eastern border instead of the closest water/land.
rubidium
parents: 7234
diff changeset
    62
			if ((int)TileY(t) - (int)TileY(tile) > best_dist) break;
7604
16666b8e91a4 (svn r11131) -Fix (r11126): forgot to commit the changes that performed the actual fix.
rubidium
parents: 7599
diff changeset
    63
			if ((int)TileX(t) - (int)TileX(tile) > best_dist) {
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    64
				/* We can safely skip this many tiles; from here all tiles have a
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    65
				 * higher or equal distance than the best distance */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    66
				t |= MapMaxX();
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    67
				continue;
7604
16666b8e91a4 (svn r11131) -Fix (r11126): forgot to commit the changes that performed the actual fix.
rubidium
parents: 7599
diff changeset
    68
			} else if (TileX(tile) < TileX(t)) {
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    69
				/* We can safely skip this many tiles; up to here all tiles have a
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    70
				 * higher or equal distance than the best distance */
7236
07ceb37a1b63 (svn r10517) -Fix: the "closest distance to water/land" calculation gave the distance to the north-eastern border instead of the closest water/land.
rubidium
parents: 7234
diff changeset
    71
				t += max(best_dist - dist, 0);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    72
				continue;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    73
			}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    74
		}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    75
	}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    76
7599
0d6d44f403c5 (svn r11126) -Fix: the algorithm to determine the closest water/land was flawed.
rubidium
parents: 7462
diff changeset
    77
	return min(best_dist, water ? 0x7F : 0x1FF);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    78
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    79
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    80
/** Make an analysis of a tile and check for its belonging to the same
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    81
 * industry, and/or the same grf file
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    82
 * @param tile TileIndex of the tile to query
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    83
 * @param i Industry to which to compare the tile to
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    84
 * @return value encoded as per NFO specs */
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    85
uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i)
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    86
{
7706
bbf501d68106 (svn r11240) -Fix [FS#1323] (r11152): forgot to invert a test
glx
parents: 7685
diff changeset
    87
	if (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != i->index) {
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    88
		/* No industry and/or the tile does not have the same industry as the one we match it with */
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    89
		return 0xFFFF;
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    90
	}
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    91
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    92
	IndustryGfx gfx = GetCleanIndustryGfx(tile);
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    93
	const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    94
	const IndustrySpec *indold = GetIndustrySpec(i->type);
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    95
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    96
	if (gfx < NEW_INDUSTRYOFFSET) { // Does it belongs to an old type?
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    97
		/* It is an old tile.  We have to see if it's been overriden */
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    98
		if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) { // has it been overridden?
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
    99
			return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   100
		}
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   101
		/* Not overriden */
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   102
		const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   103
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   104
		if (tile_ovr->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   105
			return tile_ovr->grf_prop.local_id; // same grf file
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   106
		} else {
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   107
			return 0xFFFE; // not the same grf file
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   108
		}
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   109
	}
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   110
	/* Not an 'old type' tile */
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   111
	if (indtsp->grf_prop.spritegroup != NULL) { // tile has a spritegroup ?
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   112
		if (indtsp->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) { // same industry, same grf ?
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   113
			return indtsp->grf_prop.local_id;
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   114
		} else {
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   115
			return 0xFFFE; // Defined in another grf file
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   116
		}
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   117
	}
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   118
	/* The tile has no spritegroup */
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   119
	return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   120
}
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   121
7233
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   122
static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   123
{
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   124
	uint32 best_dist = MAX_UVALUE(uint32);
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   125
	const Industry *i;
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   126
	FOR_ALL_INDUSTRIES(i) {
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   127
		if (i->type != type || i == current) continue;
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   128
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   129
		best_dist = min(best_dist, DistanceManhattan(tile, i->xy));
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   130
	}
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   131
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   132
	return best_dist;
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   133
}
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   134
7451
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   135
/** Implementation of both var 67 and 68
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   136
 * since the mechanism is almost the same, it is easier to regroup them on the same
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   137
 * function.
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   138
 * @param param_setID parameter given to the callback, which is the set id, or the local id, in our terminology
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   139
 * @param layout_filter on what layout do we filter?
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   140
 * @param current Industry for which the inquiry is made
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   141
 * @return the formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   142
 */
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   143
static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, const Industry *current)
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   144
{
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   145
	uint32 GrfID = GetRegister(0x100);  ///< Get the GRFID of the definition to look for in register 100h
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   146
	IndustryType ind_index;
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   147
	uint32 closest_dist = MAX_UVALUE(uint32);
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   148
	byte count = 0;
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   149
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   150
	/* Determine what will be the industry type to look for */
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   151
	switch (GrfID) {
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   152
		case 0:  // this is a default industry type
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   153
			ind_index = param_setID;
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   154
			break;
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   155
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   156
		case 0xFFFFFFFF: // current grf
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   157
			GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   158
			/* Fall through */
7451
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   159
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   160
		default: //use the grfid specified in register 100h
7848
39d16f634f21 (svn r11398) -Fix [FS#1414]: industry variables 67 and 68 return was incorrect
glx
parents: 7750
diff changeset
   161
			SETBIT(param_setID, 7); // bit 7 means it is not an old type
7451
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   162
			ind_index = MapNewGRFIndustryType(param_setID, GrfID);
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   163
			break;
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   164
	}
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   165
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   166
	if (layout_filter == 0) {
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   167
		/* If the filter is 0, it could be because none was specified as well as being really a 0.
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   168
		 * In either case, just do the regular var67 */
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   169
		closest_dist = GetClosestIndustry(current->xy, ind_index, current);
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   170
		count = GetIndustryTypeCount(ind_index);
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   171
	} else {
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   172
		/* Count only those who match the same industry type and layout filter
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   173
		 * Unfortunately, we have to do it manually */
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   174
		const Industry *i;
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   175
		FOR_ALL_INDUSTRIES(i) {
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   176
			if (i->type == ind_index && i != current && i->selected_layout == layout_filter) {
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   177
				closest_dist = min(closest_dist, DistanceManhattan(current->xy, i->xy));
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   178
				count++;
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   179
			}
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   180
		}
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   181
	}
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   182
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   183
	return count << 16 | GB(closest_dist, 0, 16);
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   184
}
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   185
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   186
/** This function implements the industries variables that newGRF defines.
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   187
 * @param variable that is queried
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   188
 * @param parameter unused
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   189
 * @param available will return false if ever the variable asked for does not exist
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   190
 * @param ind is of course the industry we are inquiring
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   191
 * @return the value stored in the corresponding variable*/
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   192
uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   193
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   194
	const Industry *industry = object->u.industry.ind;
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   195
	TileIndex tile = object->u.industry.tile;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   196
	const IndustrySpec *indspec = GetIndustrySpec(industry->type);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   197
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   198
	switch (variable) {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   199
		case 0x40:
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   200
		case 0x41:
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   201
		case 0x42: { // waiting cargo, but only if those two callback flags are set
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   202
			uint16 callback = indspec->callback_flags;
7166
e714f327f70e (svn r10440) -Fix: CBM_IND defines bits to be set, not bitsets.
rubidium
parents: 7165
diff changeset
   203
			if (HASBIT(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HASBIT(callback, CBM_IND_PRODUCTION_256_TICKS)) {
7191
a877e27650aa (svn r10466) -Fix: the minimum amount of waiting cargo shouldn't be 65535; that should be the maximum amount of waiting cargo.
rubidium
parents: 7186
diff changeset
   204
				return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   205
			} else {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   206
				return 0;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   207
			}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   208
		}
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   209
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   210
		/* Manhattan distance of closes dry/water tile */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   211
		case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   212
7445
4d96e6e94d88 (svn r10903) -Feature: [NewGRF] Add support for variable 44 of Variational Action 2 for Industries
belugas
parents: 7337
diff changeset
   213
		/* Layout number */
4d96e6e94d88 (svn r10903) -Feature: [NewGRF] Add support for variable 44 of Variational Action 2 for Industries
belugas
parents: 7337
diff changeset
   214
		case 0x44: return industry->selected_layout;
4d96e6e94d88 (svn r10903) -Feature: [NewGRF] Add support for variable 44 of Variational Action 2 for Industries
belugas
parents: 7337
diff changeset
   215
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   216
		/* player info */
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   217
		case 0x45: {
7685
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   218
			byte colours;
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   219
			bool is_ai = false;
7685
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   220
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   221
			if (IsValidPlayer(industry->founder)) {
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   222
				const Player *p = GetPlayer(industry->founder);
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   223
				const Livery *l = &p->livery[LS_DEFAULT];
7685
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   224
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   225
				is_ai = p->is_ai;
7685
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   226
				colours = l->colour1 + l->colour2 * 16;
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   227
			} else {
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   228
				colours = GB(Random(), 0, 8);
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   229
			}
7685
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   230
20852b29c17b (svn r11216) -Codechange: Protect the callback who would access var 45 against a randomness that could eventually cause desynchs. Instead, use a controlled random value.
belugas
parents: 7671
diff changeset
   231
			return industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   232
		}
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   233
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   234
		/* Get industry ID at offset param */
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   235
		case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), industry);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   236
7621
09824471f144 (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: 7614
diff changeset
   237
		/* Get random tile bits at offset param */
09824471f144 (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: 7614
diff changeset
   238
		case 0x61:
09824471f144 (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: 7614
diff changeset
   239
			tile = GetNearbyTile(parameter, tile);
09824471f144 (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: 7614
diff changeset
   240
			return (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == industry) ? GetIndustryRandomBits(tile) : 0;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   241
7234
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   242
		/* Land info of nearby tiles */
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   243
		case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY);
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   244
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   245
		/* Animation stage of nearby tiles */
7621
09824471f144 (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: 7614
diff changeset
   246
		case 0x63:
7234
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   247
			tile = GetNearbyTile(parameter, tile);
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   248
			if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == industry) {
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   249
				return GetIndustryAnimationState(tile);
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   250
			}
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   251
			return 0xFFFFFFFF;
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   252
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   253
		/* Distance of nearest industry of given type */
7314
bf53f5fe26a1 (svn r10669) -Codechange: Removed redundant comment
belugas
parents: 7260
diff changeset
   254
		case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   255
		/* Get town zone and Manhattan distance of closest town */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   256
 		case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   257
		/* Get square of Euclidian distance of closes town */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   258
		case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   259
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   260
		/* Count of industry, distance of closest instance
7451
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   261
		 * 68 is the same as 67, but with a filtering on selected layout */
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   262
		case 0x67:
f2668c46aa21 (svn r10926) -Feature: [NewGRF] Add support for variable 67(for real this time) and 68 of Variational Action 2 for Industries
belugas
parents: 7445
diff changeset
   263
		case 0x68: return GetCountAndDistanceOfClosestInstance(parameter, variable == 0x68 ? GB(GetRegister(0x101), 0, 8) : 0, industry);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   264
7610
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   265
		/* Get a variable from the persistent storage */
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   266
		case 0x7C: return industry->psa.Get(parameter);
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   267
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   268
		/* Industry structure access*/
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   269
		case 0x80: return industry->xy;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   270
		case 0x81: return GB(industry->xy, 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   271
		/* Pointer to the town the industry is associated with */
7191
a877e27650aa (svn r10466) -Fix: the minimum amount of waiting cargo shouldn't be 65535; that should be the maximum amount of waiting cargo.
rubidium
parents: 7186
diff changeset
   272
		case 0x82: return industry->town->index;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   273
		case 0x83:
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   274
		case 0x84:
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   275
		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   276
		case 0x86: return industry->width;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   277
		case 0x87: return industry->height;// xy dimensions
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   278
		/*  */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   279
		case 0x88:
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7622
diff changeset
   280
		case 0x89: return industry->produced_cargo[variable - 0x88];
7165
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7102
diff changeset
   281
		case 0x8A: return industry->produced_cargo_waiting[0];
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7102
diff changeset
   282
		case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7102
diff changeset
   283
		case 0x8C: return industry->produced_cargo_waiting[1];
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7102
diff changeset
   284
		case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   285
		case 0x8E:
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   286
		case 0x8F: return industry->production_rate[variable - 0x8E];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   287
		case 0x90:
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   288
		case 0x91:
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7622
diff changeset
   289
		case 0x92: return industry->accepts_cargo[variable - 0x90];
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   290
		case 0x93: return industry->prod_level;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   291
		/* amount of cargo produced so far THIS month. */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   292
		case 0x94: return industry->this_month_production[0];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   293
		case 0x95: return GB(industry->this_month_production[0], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   294
		case 0x96: return industry->this_month_production[1];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   295
		case 0x97: return GB(industry->this_month_production[1], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   296
		/* amount of cargo transported so far THIS month. */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   297
		case 0x98: return industry->this_month_transported[0];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   298
		case 0x99: return GB(industry->this_month_transported[0], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   299
		case 0x9A: return industry->this_month_transported[1];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   300
		case 0x9B: return GB(industry->this_month_transported[0], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   301
		/* fraction of cargo transported LAST month. */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   302
		case 0x9C:
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   303
		case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   304
		/* amount of cargo produced LAST month. */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   305
		case 0x9E: return industry->last_month_production[0];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   306
		case 0x9F: return GB(industry->last_month_production[0], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   307
		case 0xA0: return industry->last_month_production[1];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   308
		case 0xA1: return GB(industry->last_month_production[1], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   309
		/* amount of cargo transported last month. */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   310
		case 0xA2: return industry->last_month_transported[0];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   311
		case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   312
		case 0xA4: return industry->last_month_transported[1];
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   313
		case 0xA5: return GB(industry->last_month_transported[0], 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   314
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   315
		case 0xA6: return industry->type;
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   316
		case 0xA7: return industry->founder;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   317
		case 0xA8: return industry->random_color;
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   318
		case 0xA9: return clamp(0, industry->last_prod_year - 1920, 255);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   319
		case 0xAA: return industry->counter;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   320
		case 0xAB: return GB(industry->counter, 8, 8);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   321
		case 0xAC: return industry->was_cargo_delivered;
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   322
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   323
		case 0xB0: return clamp(0, industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date when built since 1920 (in days)
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   324
		case 0xB3: return industry->construction_type; // Construction type
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   325
		case 0xB4: return clamp(0, industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date last cargo accepted since 1920 (in days)
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   326
	}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   327
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   328
	DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   329
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   330
	*available = false;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   331
	return (uint32)-1;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   332
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   333
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   334
static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const SpriteGroup *group)
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   335
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   336
	/* IndustryTile do not have 'real' groups */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   337
	return NULL;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   338
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   339
7621
09824471f144 (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: 7614
diff changeset
   340
static uint32 IndustryGetRandomBits(const ResolverObject *object)
09824471f144 (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: 7614
diff changeset
   341
{
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   342
	return object->u.industry.ind == NULL ? 0 : object->u.industry.ind->random;
7621
09824471f144 (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: 7614
diff changeset
   343
}
09824471f144 (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: 7614
diff changeset
   344
09824471f144 (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: 7614
diff changeset
   345
static uint32 IndustryGetTriggers(const ResolverObject *object)
09824471f144 (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: 7614
diff changeset
   346
{
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   347
	return object->u.industry.ind == NULL ? 0 : object->u.industry.ind->random_triggers;
7621
09824471f144 (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: 7614
diff changeset
   348
}
09824471f144 (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: 7614
diff changeset
   349
09824471f144 (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: 7614
diff changeset
   350
static void IndustrySetTriggers(const ResolverObject *object, int triggers)
09824471f144 (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: 7614
diff changeset
   351
{
09824471f144 (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: 7614
diff changeset
   352
	if (object->u.industry.ind == NULL) return;
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   353
	object->u.industry.ind->random_triggers = triggers;
7621
09824471f144 (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: 7614
diff changeset
   354
}
09824471f144 (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: 7614
diff changeset
   355
6884
a931841e9df4 (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 6868
diff changeset
   356
static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus)
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   357
{
7621
09824471f144 (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: 7614
diff changeset
   358
	res->GetRandomBits = IndustryGetRandomBits;
09824471f144 (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: 7614
diff changeset
   359
	res->GetTriggers   = IndustryGetTriggers;
09824471f144 (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: 7614
diff changeset
   360
	res->SetTriggers   = IndustrySetTriggers;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   361
	res->GetVariable   = IndustryGetVariable;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   362
	res->ResolveReal   = IndustryResolveReal;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   363
7610
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   364
	res->psa             = &indus->psa;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   365
	res->u.industry.tile = tile;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   366
	res->u.industry.ind  = indus;
6884
a931841e9df4 (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 6868
diff changeset
   367
	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   368
7327
d810677fb909 (svn r10690) -Codechange: use the enum that describes all callback IDs in favor of "just" using an untyped integer.
rubidium
parents: 7314
diff changeset
   369
	res->callback        = CBID_NO_CALLBACK;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   370
	res->callback_param1 = 0;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   371
	res->callback_param2 = 0;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   372
	res->last_value      = 0;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   373
	res->trigger         = 0;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   374
	res->reseed          = 0;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   375
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   376
7327
d810677fb909 (svn r10690) -Codechange: use the enum that describes all callback IDs in favor of "just" using an untyped integer.
rubidium
parents: 7314
diff changeset
   377
uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   378
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   379
	ResolverObject object;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   380
	const SpriteGroup *group;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   381
6884
a931841e9df4 (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 6868
diff changeset
   382
	NewIndustryResolver(&object, tile, industry);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   383
	object.callback = callback;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   384
	object.callback_param1 = param1;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   385
	object.callback_param2 = param2;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   386
7102
fe01c264132b (svn r10369) -Codechange: Add the IndustryType parameter to the GetIndustryCallback function.
belugas
parents: 7000
diff changeset
   387
	group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   388
	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   389
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   390
	return group->g.callback.result;
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   391
}
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   392
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   393
uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   394
{
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   395
	const Industry *industry = object->u.industry.ind;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   396
	TileIndex tile = object->u.industry.tile;
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   397
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   398
	if (object->scope == VSG_SCOPE_PARENT) {
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   399
		return TownGetVariable(variable, parameter, available, industry->town);
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   400
	}
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   401
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   402
	switch (variable) {
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   403
		case 0x80: return tile;
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   404
		case 0x81: return GB(tile, 8, 8);
7234
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   405
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   406
		/* Pointer to the town the industry is associated with */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   407
		case 0x82: return industry->town->index;
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   408
		case 0x83:
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   409
		case 0x84:
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   410
		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   411
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   412
		/* Number of the layout */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   413
		case 0x86: return industry->selected_layout;
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   414
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   415
		/* Ground type */
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   416
		case 0x87: return GetTerrainType(tile);
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   417
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   418
		/* Town zone */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   419
		case 0x88: return GetTownRadiusGroup(industry->town, tile);
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   420
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   421
		/* Manhattan distance of the closest town */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   422
		case 0x89: return min(DistanceManhattan(industry->town->xy, tile), 255);
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   423
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   424
		/* Lowest height of the tile */
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   425
		case 0x8A: return GetTileZ(tile);
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   426
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   427
		/* Distance to the nearest water/land tile */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   428
		case 0x8B: return GetClosestWaterDistance(tile, (GetIndustrySpec(industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   429
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   430
		/* Square of Euclidian distance from town */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   431
		case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   432
	}
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   433
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   434
	/* None of the special ones, so try the general ones */
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   435
	return IndustryGetVariable(object, variable, parameter, available);
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   436
}
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   437
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   438
bool CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint itspec_index)
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   439
{
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   440
	const IndustrySpec *indspec = GetIndustrySpec(type);
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   441
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   442
	ResolverObject object;
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   443
	const SpriteGroup *group;
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   444
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   445
	Industry ind;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   446
	ind.index = INVALID_INDUSTRY;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   447
	ind.xy = tile;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   448
	ind.width = 0;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   449
	ind.type = type;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   450
	ind.selected_layout = itspec_index;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   451
	ind.town = ClosestTownFromTile(tile, (uint)-1);
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   452
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   453
	NewIndustryResolver(&object, tile, &ind);
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   454
	object.GetVariable = IndustryLocationGetVariable;
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   455
	object.callback = CBID_INDUSTRY_LOCATION;
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   456
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   457
	group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   458
7658
509348d18665 (svn r11189) -Fix: if the location check callback results in something invalid (i.e. not a callback result), the industry should be allowed to be build as that's how TTDP does it.
rubidium
parents: 7645
diff changeset
   459
	/* Unlike the "normal" cases, not having a valid result means we allow
509348d18665 (svn r11189) -Fix: if the location check callback results in something invalid (i.e. not a callback result), the industry should be allowed to be build as that's how TTDP does it.
rubidium
parents: 7645
diff changeset
   460
	 * the building of the industry, as that's how it's done in TTDP. */
509348d18665 (svn r11189) -Fix: if the location check callback results in something invalid (i.e. not a callback result), the industry should be allowed to be build as that's how TTDP does it.
rubidium
parents: 7645
diff changeset
   461
	if (group == NULL || group->type != SGT_CALLBACK) return true;
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   462
7750
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   463
	/* Copy some parameters from the registers to the error message text ref. stack */
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   464
	SwitchToErrorRefStack();
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   465
	PrepareTextRefStackUsage(4);
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   466
	SwitchToNormalRefStack();
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   467
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   468
	switch (group->g.callback.result) {
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   469
		case 0x400: return true;
7209
30b3bc8c177b (svn r10487) -Fix: forgot some breaks in a switch statement.
rubidium
parents: 7207
diff changeset
   470
		case 0x401: _error_message = STR_0239_SITE_UNSUITABLE; break;
30b3bc8c177b (svn r10487) -Fix: forgot some breaks in a switch statement.
rubidium
parents: 7207
diff changeset
   471
		case 0x402: _error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST; break;
30b3bc8c177b (svn r10487) -Fix: forgot some breaks in a switch statement.
rubidium
parents: 7207
diff changeset
   472
		case 0x403: _error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT; break;
30b3bc8c177b (svn r10487) -Fix: forgot some breaks in a switch statement.
rubidium
parents: 7207
diff changeset
   473
		default: _error_message = GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + group->g.callback.result); break;
7207
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   474
	}
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   475
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   476
	return false;
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   477
}
277c6c315403 (svn r10485) -Codechange: add a callback mechanism to determine whether building an industry is allowed at a given location.
rubidium
parents: 7195
diff changeset
   478
7260
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   479
bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type)
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   480
{
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   481
	const IndustrySpec *indspec = GetIndustrySpec(type);
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   482
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   483
	if (HASBIT(indspec->callback_flags, CBM_IND_AVAILABLE)) {
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   484
		uint16 res = GetIndustryCallback(CBID_INDUSTRY_AVAILABLE, 0, creation_type, NULL, type, INVALID_TILE);
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   485
		if (res != CALLBACK_FAILED) {
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   486
			return (res == 0);
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   487
		}
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   488
	}
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   489
	return true;
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   490
}
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   491
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   492
static int32 DerefIndProd(uint field, bool use_register)
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   493
{
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   494
	return use_register ? (int32)GetRegister(field) : field;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   495
}
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   496
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   497
/**
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   498
 * Get the industry production callback and apply it to the industry.
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   499
 * @param ind    the industry this callback has to be called for
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   500
 * @param reason the reason it is called (0 = incoming cargo, 1 = periodic tick callback)
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   501
 */
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   502
void IndustryProductionCallback(Industry *ind, int reason)
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   503
{
7614
edf90d673e04 (svn r11143) -Fix: the random bits were not set for the production callback when the NewGRF asked for it.
rubidium
parents: 7610
diff changeset
   504
	const IndustrySpec *spec = GetIndustrySpec(ind->type);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   505
	ResolverObject object;
7237
cdc0991e323a (svn r10518) -Fix: use the location of the industry and not INVALID_TILE for production callbacks; makes looking at the environment of an industry a lot more useful.
rubidium
parents: 7236
diff changeset
   506
	NewIndustryResolver(&object, ind->xy, ind);
7614
edf90d673e04 (svn r11143) -Fix: the random bits were not set for the production callback when the NewGRF asked for it.
rubidium
parents: 7610
diff changeset
   507
	if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   508
	object.callback_param2 = reason;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   509
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   510
	for (uint loop = 0;; loop++) {
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   511
		SB(object.callback_param2, 8, 16, loop);
7614
edf90d673e04 (svn r11143) -Fix: the random bits were not set for the production callback when the NewGRF asked for it.
rubidium
parents: 7610
diff changeset
   512
		const SpriteGroup *group = Resolve(spec->grf_prop.spritegroup, &object);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   513
		if (group == NULL || group->type != SGT_INDUSTRY_PRODUCTION) break;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   514
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   515
		bool deref = (group->g.indprod.version == 1);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   516
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   517
		for (uint i = 0; i < 3; i++) {
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   518
			ind->incoming_cargo_waiting[i] = clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->g.indprod.substract_input[i], deref), 0, 0xFFFF);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   519
		}
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   520
		for (uint i = 0; i < 2; i++) {
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   521
			ind->produced_cargo_waiting[i] = clamp(ind->produced_cargo_waiting[i] + DerefIndProd(group->g.indprod.add_output[i], deref), 0, 0xFFFF);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   522
		}
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   523
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   524
		int32 again = DerefIndProd(group->g.indprod.again, deref);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   525
		if (again == 0) break;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   526
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   527
		SB(object.callback_param2, 24, 8, again);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   528
	}
7195
8c728b5fef43 (svn r10473) -Codechange: make the industry "window", the one that shows when you click on an industry, more flexible to allow easier integration with newindustries.
rubidium
parents: 7191
diff changeset
   529
8c728b5fef43 (svn r10473) -Codechange: make the industry "window", the one that shows when you click on an industry, more flexible to allow easier integration with newindustries.
rubidium
parents: 7191
diff changeset
   530
	InvalidateWindow(WC_INDUSTRY_VIEW, ind->index);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   531
}
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   532
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   533
void DoTriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   534
{
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   535
	ResolverObject object;
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   536
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   537
	NewIndustryResolver(&object, ind->xy, ind);
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   538
	object.callback = CBID_RANDOM_TRIGGER;
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   539
	object.trigger = trigger;
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   540
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   541
	const SpriteGroup *group = Resolve(GetIndustrySpec(ind->type)->grf_prop.spritegroup, &object);
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   542
	if (group == NULL) return;
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   543
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   544
	byte new_random_bits = Random();
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   545
	ind->random &= ~object.reseed;
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   546
	ind->random |= new_random_bits & object.reseed;
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   547
}