src/newgrf_industries.cpp
author rubidium
Sat, 07 Jul 2007 08:53:19 +0000
changeset 7682 263dbeceba12
parent 7664 81c35390400e
child 7687 0ed68892bc29
permissions -rw-r--r--
(svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     1
/* $Id$ */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     2
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     3
/** @file newgrf_industries.cpp */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     4
0a7f00fed4e6 (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"
0a7f00fed4e6 (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"
0a7f00fed4e6 (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"
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     8
#include "functions.h"
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
     9
#include "macros.h"
0a7f00fed4e6 (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"
7364
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    11
#include "industry_map.h"
7331
0a7f00fed4e6 (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"
0a7f00fed4e6 (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"
0a7f00fed4e6 (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"
0a7f00fed4e6 (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"
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    16
#include "newgrf_commons.h"
7682
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
    17
#include "date.h"
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    18
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    19
/* Since the industry IDs defined by the GRF file don't necessarily correlate
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    20
 * to those used by the game, the IDs used for overriding old industries must be
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    21
 * translated when the idustry spec is set. */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    22
IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
7496
628e1191ee45 (svn r10256) -Add: Addition of IndustryTileOverrideManager
belugas
parents: 7380
diff changeset
    23
IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    24
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    25
/**
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    26
 * Finds the distance for the closest tile with water/land given a tile
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    27
 * @param tile  the tile to find the distance too
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    28
 * @param water whether to find water or land
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    29
 * @note FAILS when an industry should be seen as water
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    30
 */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    31
static uint GetClosestWaterDistance(TileIndex tile, bool water)
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    32
{
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    33
	TileIndex t;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    34
	uint best_dist;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    35
	for (t = 1; t < MapSize(); t++) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    36
		if (IsTileType(t, MP_WATER) == water) break;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    37
	}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    38
	best_dist = DistanceManhattan(tile, t);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    39
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    40
	for (; t < MapSize(); t++) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    41
		uint dist = DistanceManhattan(tile, t);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    42
		if (dist < best_dist) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    43
			if (IsTileType(t, MP_WATER) == water) best_dist = dist;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    44
		} else {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    45
			/* When the Y distance between the current row and the 'source' tile
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    46
			 * is larger than the best distance, we've found the best distance */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    47
			if (TileY(t) - TileY(tile) > best_dist) return best_dist;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    48
			if (TileX(tile) > TileX(t)) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    49
				/* We can safely skip this many tiles; from here all tiles have a
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    50
				 * higher or equal distance than the best distance */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    51
				t |= MapMaxX();
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    52
				continue;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    53
			} else {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    54
				/* We can safely skip this many tiles; up to here all tiles have a
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    55
				 * higher or equal distance than the best distance */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    56
				t += best_dist - dist;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    57
				continue;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    58
			}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    59
		}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    60
	}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    61
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    62
	return best_dist;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    63
}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
    64
7364
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    65
/** Make an analysis of a tile and check for its belonging to the same
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    66
 * industry, and/or the same grf file
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    67
 * @param new_tile TileIndex of the tile to query
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    68
 * @param old_tile TileINdex of teh reference tile
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    69
 * @param i Industry to which old_tile belongs to
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    70
 * @return value encoded as per NFO specs */
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    71
uint32 GetIndustryIDAtOffset(TileIndex new_tile, TileIndex old_tile, const Industry *i)
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    72
{
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    73
	if (IsTileType(new_tile, MP_INDUSTRY)) {  // Is this an industry tile?
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    74
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    75
		if (GetIndustryIndex(new_tile) == i->index) {  // Does it belong to the same industry?
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    76
			IndustryGfx gfx = GetIndustryGfx(new_tile);
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    77
			const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    78
			const IndustryTileSpec *indold = GetIndustryTileSpec(GetIndustryGfx(old_tile));
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    79
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    80
			if (gfx < NEW_INDUSTRYOFFSET) {  // Does it belongs to an old type?
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    81
				/* It is an old tile.  We have to see if it's been overriden */
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    82
				if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) {  // has it been overridden?
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    83
					return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    84
				} else { // yes.  FInd out if it is from the same grf file or not
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    85
					const IndustryTileSpec *old_tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    86
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    87
					if (old_tile_ovr->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    88
						return old_tile_ovr->grf_prop.local_id; // same grf file
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    89
					} else {
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    90
						return 0xFFFE; // not the same grf file
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    91
					}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    92
				}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    93
			} else {
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    94
				if (indtsp->grf_prop.spritegroup != NULL) {  // tile has a spritegroup ?
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    95
					if (indtsp->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {  // same industry, same grf ?
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    96
						return indtsp->grf_prop.local_id;
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    97
					} else {
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    98
						return 0xFFFE;  // Defined in another grf file
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
    99
					}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   100
				} else {  // tile has no spritegroup
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   101
					return 0xFF << 8 | indtsp->grf_prop.subst_id;  // so just give him the substitute
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   102
				}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   103
			}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   104
		}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   105
	}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   106
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   107
	return 0xFFFF; // tile is not an industry one or  does not belong to the current industry
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   108
}
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   109
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   110
/** This function implements the industries variables that newGRF defines.
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   111
 * @param variable that is queried
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   112
 * @param parameter unused
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   113
 * @param available will return false if ever the variable asked for does not exist
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   114
 * @param ind is of course the industry we are inquiring
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   115
 * @return the value stored in the corresponding variable*/
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   116
uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   117
{
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   118
	const Industry *industry = object->u.industry.ind;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   119
	TileIndex tile   = object->u.industry.tile;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   120
	const IndustrySpec *indspec = GetIndustrySpec(industry->type);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   121
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   122
	switch (variable) {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   123
		case 0x40:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   124
		case 0x41:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   125
		case 0x42: { // waiting cargo, but only if those two callback flags are set
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   126
			uint16 callback = indspec->callback_flags;
7662
60f877c832d9 (svn r10440) -Fix: CBM_IND defines bits to be set, not bitsets.
rubidium
parents: 7661
diff changeset
   127
			if (HASBIT(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HASBIT(callback, CBM_IND_PRODUCTION_256_TICKS)) {
7664
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   128
				return max(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   129
			} else {
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   130
				return 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   131
			}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   132
		}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   133
		/* TODO: somehow determine whether we're in water or not */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   134
		case 0x43: return GetClosestWaterDistance(tile, true); // Manhattan distance of closes dry/water tile
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   135
7364
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   136
		/* Get industry ID at offset param */
f72f9b742c34 (svn r10108) -Codechange: implement variable 0x60 (Get industry tile ID at offset) for industries.
belugas
parents: 7331
diff changeset
   137
		case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), tile, industry);
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   138
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   139
		case 0x61: return 0; // Get random tile bits at offset param
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   140
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   141
		case 0x62: // Land info of nearby tiles
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   142
		case 0x63: // Animation stage of nerby tiles
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   143
		case 0x64: break; // Distance of nearest industry of given type
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   144
		/* Get town zone and Manhattan distance of closest town */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   145
 		case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   146
		/* Get square of Euclidian distance of closes town */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   147
		case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   148
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   149
		/* Count of industry, distance of closest instance
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   150
		 * format is rr(reserved) cc(count)  dddd(manhattan distance of closest sister)
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   151
		 * A lot more should be done, since it has to check for local id, grf id etc...
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   152
		 * let's just say it is a beginning ;) */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   153
		case 0x67: return GetIndustryTypeCount(industry->type) << 16 | 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   154
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   155
		/* Industry structure access*/
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   156
		case 0x80: return industry->xy;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   157
		case 0x81: return GB(industry->xy, 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   158
		/* Pointer to the town the industry is associated with */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   159
		case 0x82:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   160
		case 0x83:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   161
		case 0x84:
7682
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
   162
		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   163
		case 0x86: return industry->width;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   164
		case 0x87: return industry->height;// xy dimensions
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   165
		/*  */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   166
		case 0x88:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   167
		case 0x89: return indspec->produced_cargo[variable - 0x88];
7661
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7598
diff changeset
   168
		case 0x8A: return industry->produced_cargo_waiting[0];
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7598
diff changeset
   169
		case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7598
diff changeset
   170
		case 0x8C: return industry->produced_cargo_waiting[1];
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7598
diff changeset
   171
		case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   172
		case 0x8E:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   173
		case 0x8F: return industry->production_rate[variable - 0x8E];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   174
		case 0x90:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   175
		case 0x91:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   176
		case 0x92: return indspec->accepts_cargo[variable - 0x90];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   177
		case 0x93: return industry->prod_level;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   178
		/* amount of cargo produced so far THIS month. */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   179
		case 0x94: return industry->this_month_production[0];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   180
		case 0x95: return GB(industry->this_month_production[0], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   181
		case 0x96: return industry->this_month_production[1];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   182
		case 0x97: return GB(industry->this_month_production[1], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   183
		/* amount of cargo transported so far THIS month. */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   184
		case 0x98: return industry->this_month_transported[0];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   185
		case 0x99: return GB(industry->this_month_transported[0], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   186
		case 0x9A: return industry->this_month_transported[1];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   187
		case 0x9B: return GB(industry->this_month_transported[0], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   188
		/* fraction of cargo transported LAST month. */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   189
		case 0x9C:
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   190
		case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   191
		/* amount of cargo produced LAST month. */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   192
		case 0x9E: return industry->last_month_production[0];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   193
		case 0x9F: return GB(industry->last_month_production[0], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   194
		case 0xA0: return industry->last_month_production[1];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   195
		case 0xA1: return GB(industry->last_month_production[1], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   196
		/* amount of cargo transported last month. */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   197
		case 0xA2: return industry->last_month_transported[0];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   198
		case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   199
		case 0xA4: return industry->last_month_transported[1];
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   200
		case 0xA5: return GB(industry->last_month_transported[0], 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   201
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   202
		case 0xA6: return industry->type;
7682
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
   203
		case 0xA7: return industry->founder;
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   204
		case 0xA8: return industry->random_color;
7682
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
   205
		case 0xA9: return clamp(0, industry->last_prod_year - 1920, 255);
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   206
		case 0xAA: return industry->counter;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   207
		case 0xAB: return GB(industry->counter, 8, 8);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   208
		case 0xAC: return industry->was_cargo_delivered;
7682
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
   209
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
   210
		case 0xB0: return clamp(0, industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date when built since 1920 (in days)
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
   211
		case 0xB3: return industry->construction_type; // Construction type
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
   212
		case 0xB4: return clamp(0, industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date last cargo accepted since 1920 (in days)
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   213
	}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   214
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   215
	DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   216
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   217
	*available = false;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   218
	return (uint32)-1;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   219
}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   220
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   221
static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const SpriteGroup *group)
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   222
{
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   223
	/* IndustryTile do not have 'real' groups */
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   224
	return NULL;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   225
}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   226
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 7364
diff changeset
   227
static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus)
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   228
{
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 7364
diff changeset
   229
	res->GetRandomBits = IndustryTileGetRandomBits;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 7364
diff changeset
   230
	res->GetTriggers   = IndustryTileGetTriggers;
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 7364
diff changeset
   231
	res->SetTriggers   = IndustryTileSetTriggers;
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   232
	res->GetVariable   = IndustryGetVariable;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   233
	res->ResolveReal   = IndustryResolveReal;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   234
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   235
	res->u.industry.tile = tile;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   236
	res->u.industry.ind  = indus;
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 7364
diff changeset
   237
	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   238
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   239
	res->callback        = 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   240
	res->callback_param1 = 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   241
	res->callback_param2 = 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   242
	res->last_value      = 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   243
	res->trigger         = 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   244
	res->reseed          = 0;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   245
}
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   246
7598
c12c2d7e0b55 (svn r10369) -Codechange: Add the IndustryType parameter to the GetIndustryCallback function.
belugas
parents: 7496
diff changeset
   247
uint16 GetIndustryCallback(uint16 callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   248
{
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   249
	ResolverObject object;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   250
	const SpriteGroup *group;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   251
7380
a9d0047b015c (svn r10127) -Add: Addition of basic structure for industry tiles callbacks (unfinished).
belugas
parents: 7364
diff changeset
   252
	NewIndustryResolver(&object, tile, industry);
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   253
	object.callback = callback;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   254
	object.callback_param1 = param1;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   255
	object.callback_param2 = param2;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   256
7598
c12c2d7e0b55 (svn r10369) -Codechange: Add the IndustryType parameter to the GetIndustryCallback function.
belugas
parents: 7496
diff changeset
   257
	group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
7331
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   258
	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   259
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   260
	return group->g.callback.result;
0a7f00fed4e6 (svn r10074) -Add: Addition of IndustryOverrideManager as well as the basic (and unfinished) support for callbacks for industries
belugas
parents:
diff changeset
   261
}
7664
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   262
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   263
static int32 DerefIndProd(uint field, bool use_register)
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   264
{
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   265
	return use_register ? (int32)GetRegister(field) : field;
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   266
}
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   267
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   268
/**
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   269
 * Get the industry production callback and apply it to the industry.
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   270
 * @param ind    the industry this callback has to be called for
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   271
 * @param reason the reason it is called (0 = incoming cargo, 1 = periodic tick callback)
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   272
 */
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   273
void IndustryProductionCallback(Industry *ind, int reason)
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   274
{
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   275
	ResolverObject object;
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   276
	NewIndustryResolver(&object, INVALID_TILE, ind);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   277
	object.callback_param2 = reason;
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   278
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   279
	for (uint loop = 0;; loop++) {
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   280
		SB(object.callback_param2, 8, 16, loop);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   281
		const SpriteGroup *group = Resolve(GetIndustrySpec(ind->type)->grf_prop.spritegroup, &object);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   282
		if (group == NULL || group->type != SGT_INDUSTRY_PRODUCTION) break;
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   283
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   284
		bool deref = (group->g.indprod.version == 1);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   285
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   286
		for (uint i = 0; i < 3; i++) {
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   287
			ind->incoming_cargo_waiting[i] = clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->g.indprod.substract_input[i], deref), 0, 0xFFFF);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   288
		}
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   289
		for (uint i = 0; i < 2; i++) {
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   290
			ind->produced_cargo_waiting[i] = clamp(ind->produced_cargo_waiting[i] + DerefIndProd(group->g.indprod.add_output[i], deref), 0, 0xFFFF);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   291
		}
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   292
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   293
		int32 again = DerefIndProd(group->g.indprod.again, deref);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   294
		if (again == 0) break;
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   295
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   296
		SB(object.callback_param2, 24, 8, again);
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   297
	}
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7662
diff changeset
   298
}