src/newgrf_industries.cpp
author terom@frrb.lan
Fri, 19 Dec 2008 01:38:09 +0200
changeset 10439 50f056aa3024
parent 10318 d436fee569bf
permissions -rw-r--r--
industries, unmoveables... everything but the landscape
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
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8970
diff changeset
     3
/** @file newgrf_industries.cpp Handling of NewGRF industries. */
6835
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"
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
     8
#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
     9
#include "landscape.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
    10
#include "industry.h"
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    11
#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
    12
#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
    13
#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
    14
#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
    15
#include "newgrf_industries.h"
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
    16
#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
    17
#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
    18
#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
    19
#include "newgrf_town.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8113
diff changeset
    20
#include "window_func.h"
8213
7bdd7593eb9b (svn r11776) -Codechange: more header splittings to reduce the dependencies.
rubidium
parents: 8163
diff changeset
    21
#include "town.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    22
#include "company_func.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    23
#include "company_base.h"
8275
f17d0c863ee3 (svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents: 8264
diff changeset
    24
#include "command_func.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
    25
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    26
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    27
8689
af0a4064b83e (svn r12362) -Fix (r11985, r12006): Randomize variable 8F only once per callback 28.
frosch
parents: 8634
diff changeset
    28
static uint32 _industry_creation_random_bits;
8436
c2600da0d3e3 (svn r12006) -Fix: implement FS#1697 without the nasty side effects as specified in FS#1712.
rubidium
parents: 8427
diff changeset
    29
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
/* 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
    31
 * 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
    32
 * 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
    33
IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
7000
603a41cb93f8 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 6884
diff changeset
    34
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
    35
7233
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    36
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
    37
{
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    38
	if (grf_type == IT_INVALID) return IT_INVALID;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
    39
	if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 6);
7233
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    40
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    41
	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
    42
}
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
    43
6835
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
 * 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
    46
 * @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
    47
 * @param water whether to find water or land
10204
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    48
 * @return distance to nearest water (max 0x7F) / land (max 0x1FF; 0x200 if there is no land)
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    49
 * @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
    50
 */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    51
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
    52
{
10204
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    53
	if (IsTileType(tile, MP_WATER) == water) return 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
    54
10204
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    55
	uint max_dist = water ? 0x7F : 0x200;
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    56
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    57
	int x = TileX(tile);
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    58
	int y = TileY(tile);
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    59
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    60
	uint max_x = MapMaxX();
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    61
	uint max_y = MapMaxY();
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    62
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    63
	/* go in a 'spiral' with increasing manhattan distance in each iteration */
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    64
	for (uint dist = 1; dist < max_dist; dist++) {
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    65
		/* next 'diameter' */
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    66
		y--;
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    67
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    68
		/* going counter-clockwise around this square */
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    69
		for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    70
			static const int8 ddx[DIAGDIR_END] = { -1,  1,  1, -1};
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    71
			static const int8 ddy[DIAGDIR_END] = {  1,  1, -1, -1};
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    72
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    73
			int dx = ddx[dir];
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    74
			int dy = ddy[dir];
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    75
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    76
			/* each side of this square has length 'dist' */
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    77
			for (uint a = 0; a < dist; a++) {
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    78
				/* MP_VOID tiles are not checked (interval is [0; max) for IsInsideMM())*/
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    79
				if (IsInsideMM(x, 0, max_x) && IsInsideMM(y, 0, max_y)) {
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    80
					TileIndex t = TileXY(x, y);
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    81
					if (IsTileType(t, MP_WATER) == water) return dist;
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    82
				}
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    83
				x += dx;
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    84
				y += dy;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    85
			}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    86
		}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    87
	}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    88
10204
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    89
	if (!water) {
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    90
		/* no land found - is this a water-only map? */
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    91
		for (TileIndex t = 0; t < MapSize(); t++) {
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    92
			if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    93
		}
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    94
	}
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    95
38f5dd6697e2 (svn r14417) -Codechange: rewrite GetClosestWaterDistance(), now it is ~100 times faster than pre-r14416 in average case
smatz
parents: 10203
diff changeset
    96
	return max_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
    97
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    98
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
    99
/** 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
   100
 * 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
   101
 * @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
   102
 * @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
   103
 * @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
   104
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
   105
{
7706
bbf501d68106 (svn r11240) -Fix [FS#1323] (r11152): forgot to invert a test
glx
parents: 7685
diff changeset
   106
	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
   107
		/* 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
   108
		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
   109
	}
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   110
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   111
	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
   112
	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
   113
	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
   114
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   115
	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
   116
		/* 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
   117
		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
   118
			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
   119
		}
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   120
		/* 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
   121
		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
   122
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   123
		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
   124
			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
   125
		} else {
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   126
			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
   127
		}
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   128
	}
7622
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   129
	/* 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
   130
	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
   131
		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
   132
			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
   133
		} else {
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   134
			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
   135
		}
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   136
	}
12388e4d86bb (svn r11152) -Fix: GetIndustryIDAtOffset crashed when translation of the GFX ID would be needed. Fix by Belugas.
rubidium
parents: 7621
diff changeset
   137
	/* 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
   138
	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
   139
}
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   140
7233
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   141
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
   142
{
9605
5a1897abd925 (svn r13647) -Codechange: replace MAX_UVALUE() for std types with the equivalent constant
skidd13
parents: 9487
diff changeset
   143
	uint32 best_dist = UINT32_MAX;
7233
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   144
	const Industry *i;
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   145
	FOR_ALL_INDUSTRIES(i) {
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   146
		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
   147
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   148
		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
   149
	}
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   150
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   151
	return best_dist;
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   152
}
aadfbb505671 (svn r10514) -Codechange: add support for getting the nearest industry with a given type.
rubidium
parents: 7209
diff changeset
   153
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
   154
/** 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
   155
 * 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
   156
 * 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
   157
 * @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
   158
 * @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
   159
 * @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
   160
 * @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
   161
 */
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
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
   163
{
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
	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
   165
	IndustryType ind_index;
9605
5a1897abd925 (svn r13647) -Codechange: replace MAX_UVALUE() for std types with the equivalent constant
skidd13
parents: 9487
diff changeset
   166
	uint32 closest_dist = UINT32_MAX;
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
   167
	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
   168
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
	/* 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
   170
	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
   171
		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
   172
			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
   173
			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
   174
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
		case 0xFFFFFFFF: // current grf
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   176
			GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   177
			/* 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
   178
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
		default: //use the grfid specified in register 100h
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   180
			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
   181
			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
   182
			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
   183
	}
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
	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
   186
		/* 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
   187
		 * 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
   188
		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
   189
		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
   190
	} 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
   191
		/* 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
   192
		 * 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
   193
		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
   194
		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
   195
			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
   196
				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
   197
				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
   198
			}
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
   199
		}
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
   200
	}
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
   201
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
   202
	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
   203
}
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
   204
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
/** 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
   206
 * @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
   207
 * @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
   208
 * @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
   209
 * @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
   210
 * @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
   211
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
   212
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   213
	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
   214
	TileIndex tile = object->u.industry.tile;
8233
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   215
	IndustryType type = object->u.industry.type;
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   216
	const IndustrySpec *indspec = GetIndustrySpec(type);
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   217
9736
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   218
	/* Shall the variable get resolved in parent scope and are we not yet in parent scope? */
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   219
	if (object->u.industry.gfx == INVALID_INDUSTRYTILE && object->scope == VSG_SCOPE_PARENT) {
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   220
		/* Pass the request on to the town of the industry */
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   221
		const Town *t;
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   222
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   223
		if (industry != NULL) {
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   224
			t = industry->town;
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   225
		} else if (tile != INVALID_TILE) {
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   226
			t = ClosestTownFromTile(tile, UINT_MAX);
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   227
		} else {
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   228
			*available = false;
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   229
			return UINT_MAX;
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   230
		}
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   231
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   232
		return TownGetVariable(variable, parameter, available, t);
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   233
	}
1dde9f49db84 (svn r13868) -Fix: Parent scope of industry variables.
frosch
parents: 9652
diff changeset
   234
8233
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   235
	if (industry == NULL) {
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   236
		/* industry does not exist, only use those variables that are "safe" */
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   237
		switch (variable) {
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   238
			/* Manhattan distance of closes dry/water tile */
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   239
			case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   240
		}
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   241
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   242
		DEBUG(grf, 1, "Unhandled property 0x%X (no available industry) in callback 0x%x", variable, object->callback);
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   243
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   244
		*available = false;
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   245
		return UINT_MAX;
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   246
	}
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   247
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   248
	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
   249
		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
   250
		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
   251
		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
   252
			uint16 callback = indspec->callback_flags;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   253
			if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
7958
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   254
				if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   255
					return min(industry->incoming_cargo_waiting[variable - 0x40] / industry->prod_level, (uint16)0xFFFF);
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   256
				} else {
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   257
					return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   258
				}
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   259
			} else {
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   260
				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
   261
			}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   262
		}
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
   263
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
   264
		/* Manhattan distance of closes dry/water tile */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   265
		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
   266
7445
4d96e6e94d88 (svn r10903) -Feature: [NewGRF] Add support for variable 44 of Variational Action 2 for Industries
belugas
parents: 7337
diff changeset
   267
		/* Layout number */
4d96e6e94d88 (svn r10903) -Feature: [NewGRF] Add support for variable 44 of Variational Action 2 for Industries
belugas
parents: 7337
diff changeset
   268
		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
   269
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10204
diff changeset
   270
		/* Company info */
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   271
		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
   272
			byte colours;
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   273
			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
   274
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10204
diff changeset
   275
			if (IsValidCompanyID(industry->founder)) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10204
diff changeset
   276
				const Company *c = GetCompany(industry->founder);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10204
diff changeset
   277
				const Livery *l = &c->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
   278
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10204
diff changeset
   279
				is_ai = c->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
   280
				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
   281
			} 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
   282
				colours = GB(Random(), 0, 8);
7671
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   283
			}
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
   284
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
   285
			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
   286
		}
21bc11c5265d (svn r11202) -Feature: [NewGRF] Add support for var 45 for Industries.
belugas
parents: 7668
diff changeset
   287
9487
4b8dae4806bf (svn r13446) -Fix(r13443): changed the var previously introduced from 69 to 46, as no param was required (thanks frosch)
belugas
parents: 9484
diff changeset
   288
		case 0x46: return industry->construction_date; // Date when built - long format - (in days)
4b8dae4806bf (svn r13446) -Fix(r13443): changed the var previously introduced from 69 to 46, as no param was required (thanks frosch)
belugas
parents: 9484
diff changeset
   289
6868
abaf9e30691e (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 6835
diff changeset
   290
		/* 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
   291
		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
   292
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
   293
		/* 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
   294
		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
   295
			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
   296
			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
   297
7234
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   298
		/* 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
   299
		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
   300
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   301
		/* 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
   302
		case 0x63:
7234
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   303
			tile = GetNearbyTile(parameter, tile);
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   304
			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
   305
				return GetIndustryAnimationState(tile);
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   306
			}
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   307
			return 0xFFFFFFFF;
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   308
c6d0e14ae80b (svn r10515) -Codechange: allow getting some more data about the environment of an industry.
rubidium
parents: 7233
diff changeset
   309
		/* Distance of nearest industry of given type */
7314
bf53f5fe26a1 (svn r10669) -Codechange: Removed redundant comment
belugas
parents: 7260
diff changeset
   310
		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
   311
		/* Get town zone and Manhattan distance of closest town */
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8756
diff changeset
   312
		case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 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
   313
		/* 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
   314
		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
   315
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   316
		/* 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
   317
		 * 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
   318
		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
   319
		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
   320
7610
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   321
		/* Get a variable from the persistent storage */
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   322
		case 0x7C: return industry->psa.Get(parameter);
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   323
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   324
		/* 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
   325
		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
   326
		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
   327
		/* 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
   328
		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
   329
		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
   330
		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
   331
		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
   332
		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
   333
		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
   334
		/*  */
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   335
		case 0x88:
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7622
diff changeset
   336
		case 0x89: return industry->produced_cargo[variable - 0x88];
7165
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7102
diff changeset
   337
		case 0x8A: return industry->produced_cargo_waiting[0];
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7102
diff changeset
   338
		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
   339
		case 0x8C: return industry->produced_cargo_waiting[1];
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7102
diff changeset
   340
		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
   341
		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
   342
		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
   343
		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
   344
		case 0x91:
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7622
diff changeset
   345
		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
   346
		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
   347
		/* 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
   348
		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
   349
		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
   350
		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
   351
		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
   352
		/* 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
   353
		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
   354
		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
   355
		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
   356
		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
   357
		/* 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
   358
		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
   359
		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
   360
		/* 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
   361
		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
   362
		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
   363
		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
   364
		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
   365
		/* 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
   366
		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
   367
		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
   368
		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
   369
		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
   370
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   371
		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
   372
		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
   373
		case 0xA8: return industry->random_color;
8634
496c3fb47daf (svn r12246) -Fix (r10460): Proper usage of Clamp().
frosch
parents: 8436
diff changeset
   374
		case 0xA9: return Clamp(industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 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
   375
		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
   376
		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
   377
		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
   378
8634
496c3fb47daf (svn r12246) -Fix (r10460): Proper usage of Clamp().
frosch
parents: 8436
diff changeset
   379
		case 0xB0: return Clamp(industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
   380
		case 0xB3: return industry->construction_type; // Construction type
8634
496c3fb47daf (svn r12246) -Fix (r10460): Proper usage of Clamp().
frosch
parents: 8436
diff changeset
   381
		case 0xB4: return Clamp(industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 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
   382
	}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   383
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   384
	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
   385
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   386
	*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
   387
	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
   388
}
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
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
   391
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   392
	/* 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
   393
	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
   394
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   395
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
   396
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
   397
{
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   398
	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
   399
}
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
   400
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
   401
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
   402
{
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   403
	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
   404
}
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
   405
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
   406
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
   407
{
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
   408
	if (object->u.industry.ind == NULL) return;
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7848
diff changeset
   409
	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
   410
}
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
   411
8233
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   412
static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus, IndustryType type)
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   413
{
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
   414
	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
   415
	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
   416
	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
   417
	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
   418
	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
   419
7610
13b7d9e247d2 (svn r11139) -Codechange: add support for persistent storage for NewGRFs.
rubidium
parents: 7604
diff changeset
   420
	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
   421
	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
   422
	res->u.industry.ind  = indus;
6884
a931841e9df4 (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 6868
diff changeset
   423
	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
8233
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   424
	res->u.industry.type = type;
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   425
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
   426
	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
   427
	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
   428
	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
   429
	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
   430
	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
   431
	res->reseed          = 0;
8756
b6f8dc6a246f (svn r12452) -Feature: [NewGRF] Add random action 2 type 84. For vehicles only.
glx
parents: 8689
diff changeset
   432
	res->count           = 0;
9750
7114408dd1fa (svn r13885) -Fix [FS#2168]: Var 0x7F is not feature-specific.
frosch
parents: 9736
diff changeset
   433
7114408dd1fa (svn r13885) -Fix [FS#2168]: Var 0x7F is not feature-specific.
frosch
parents: 9736
diff changeset
   434
	const IndustrySpec *indspec = GetIndustrySpec(type);
7114408dd1fa (svn r13885) -Fix [FS#2168]: Var 0x7F is not feature-specific.
frosch
parents: 9736
diff changeset
   435
	res->grffile         = (indspec != NULL ? indspec->grf_prop.grffile : NULL);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   436
}
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   437
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
   438
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
   439
{
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   440
	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
   441
	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
   442
8233
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   443
	NewIndustryResolver(&object, tile, industry, type);
6835
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   444
	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
   445
	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
   446
	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
   447
7102
fe01c264132b (svn r10369) -Codechange: Add the IndustryType parameter to the GetIndustryCallback function.
belugas
parents: 7000
diff changeset
   448
	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
   449
	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
   450
31c89aab2d61 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   451
	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
   452
}
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   453
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
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
   455
{
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   456
	const Industry *industry = object->u.industry.ind;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   457
	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
   458
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
   459
	if (object->scope == VSG_SCOPE_PARENT) {
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   460
		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
   461
	}
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
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
   463
	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
   464
		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
   465
		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
   466
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
   467
		/* 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
   468
		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
   469
		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
   470
		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
   471
		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
   472
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
   473
		/* Number of the layout */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   474
		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
   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
		/* 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
   477
		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
   478
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
   479
		/* Town zone */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   480
		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
   481
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
   482
		/* Manhattan distance of the closest town */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   483
		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
   484
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
   485
		/* 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
   486
		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
   487
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
   488
		/* Distance to the nearest water/land tile */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   489
		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
   490
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
   491
		/* Square of Euclidian distance from town */
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   492
		case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);
8415
41a623a9fdb5 (svn r11985) -Feature[FS#1697, newGRF]: Implement var 8F(random bits) during callback 28 (Industry location permissibility)
belugas
parents: 8275
diff changeset
   493
8436
c2600da0d3e3 (svn r12006) -Fix: implement FS#1697 without the nasty side effects as specified in FS#1712.
rubidium
parents: 8427
diff changeset
   494
		/* 32 random bits */
8689
af0a4064b83e (svn r12362) -Fix (r11985, r12006): Randomize variable 8F only once per callback 28.
frosch
parents: 8634
diff changeset
   495
		case 0x8F: return _industry_creation_random_bits;
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
   496
	}
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
   497
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   498
	/* 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
   499
	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
   500
}
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
   501
8436
c2600da0d3e3 (svn r12006) -Fix: implement FS#1697 without the nasty side effects as specified in FS#1712.
rubidium
parents: 8427
diff changeset
   502
bool CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint itspec_index, uint32 seed)
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
   503
{
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
   504
	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
   505
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
   506
	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
   507
	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
   508
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   509
	Industry ind;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   510
	ind.index = INVALID_INDUSTRY;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   511
	ind.xy = tile;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   512
	ind.width = 0;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   513
	ind.type = type;
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   514
	ind.selected_layout = itspec_index;
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10208
diff changeset
   515
	ind.town = ClosestTownFromTile(tile, UINT_MAX);
7668
fd98cd8a83fd (svn r11199) -Fix: variable 67 and 68 not working correctly.
rubidium
parents: 7658
diff changeset
   516
8233
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   517
	NewIndustryResolver(&object, tile, &ind, type);
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
   518
	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
   519
	object.callback = CBID_INDUSTRY_LOCATION;
8689
af0a4064b83e (svn r12362) -Fix (r11985, r12006): Randomize variable 8F only once per callback 28.
frosch
parents: 8634
diff changeset
   520
	_industry_creation_random_bits = seed;
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
   521
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
   522
	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
   523
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
   524
	/* 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
   525
	 * the building of the industry, as that's how it's done in TTDP. */
10318
d436fee569bf (svn r14563) -Fix [FS#2395]: in the case that an industry NewGRF, a shared TTDPatch and
rubidium
parents: 10236
diff changeset
   526
	if (group == NULL || group->type != SGT_CALLBACK || group->g.callback.result == 0x400) 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
   527
7750
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   528
	/* 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
   529
	SwitchToErrorRefStack();
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   530
	PrepareTextRefStackUsage(4);
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   531
	SwitchToNormalRefStack();
7afbef1235e0 (svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
rubidium
parents: 7706
diff changeset
   532
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
   533
	switch (group->g.callback.result) {
7209
30b3bc8c177b (svn r10487) -Fix: forgot some breaks in a switch statement.
rubidium
parents: 7207
diff changeset
   534
		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
   535
		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
   536
		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
   537
		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
   538
	}
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
   539
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
   540
	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
   541
}
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
   542
7260
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   543
bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type)
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   544
{
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   545
	const IndustrySpec *indspec = GetIndustrySpec(type);
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   546
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   547
	if (HasBit(indspec->callback_flags, CBM_IND_AVAILABLE)) {
7260
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   548
		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
   549
		if (res != CALLBACK_FAILED) {
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   550
			return (res == 0);
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   551
		}
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   552
	}
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   553
	return true;
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   554
}
487c5de4fe2c (svn r10572) -Codechange: Add function CheckIfCallBackAllowsAvailability for industries
belugas
parents: 7237
diff changeset
   555
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   556
static int32 DerefIndProd(uint field, bool use_register)
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   557
{
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   558
	return use_register ? (int32)GetRegister(field) : field;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   559
}
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   560
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   561
/**
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   562
 * 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
   563
 * @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
   564
 * @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
   565
 */
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   566
void IndustryProductionCallback(Industry *ind, int reason)
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   567
{
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
   568
	const IndustrySpec *spec = GetIndustrySpec(ind->type);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   569
	ResolverObject object;
8233
560ddd54f068 (svn r11797) -Fix: Add protection against not already created industry while doing industry callback.
belugas
parents: 8213
diff changeset
   570
	NewIndustryResolver(&object, ind->xy, ind, ind->type);
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
   571
	if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
7958
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   572
	int multiplier = 1;
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   573
	if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   574
	object.callback_param2 = reason;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   575
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   576
	for (uint loop = 0;; loop++) {
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   577
		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
   578
		const SpriteGroup *group = Resolve(spec->grf_prop.spritegroup, &object);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   579
		if (group == NULL || group->type != SGT_INDUSTRY_PRODUCTION) break;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   580
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   581
		bool deref = (group->g.indprod.version == 1);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   582
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   583
		for (uint i = 0; i < 3; i++) {
7958
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   584
			ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->g.indprod.substract_input[i], deref) * multiplier, 0, 0xFFFF);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   585
		}
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   586
		for (uint i = 0; i < 2; i++) {
7958
e42fa05eb045 (svn r11514) -Change: implement the automatic multiplier handler for NewGRF industries.
rubidium
parents: 7931
diff changeset
   587
			ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->g.indprod.add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   588
		}
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   589
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   590
		int32 again = DerefIndProd(group->g.indprod.again, deref);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   591
		if (again == 0) break;
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   592
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   593
		SB(object.callback_param2, 24, 8, again);
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   594
	}
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
   595
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
   596
	InvalidateWindow(WC_INDUSTRY_VIEW, ind->index);
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7166
diff changeset
   597
}