(svn r24689) -Codechange: Add resolver classes for industries.

pull/155/head
alberth 12 years ago
parent 189758f2ef
commit 0b8097be6b

@ -33,6 +33,7 @@
#include "newgrf_station.h"
#include "newgrf_town.h"
#include "newgrf_railtype.h"
#include "newgrf_industries.h"
#include "widgets/newgrf_debug_widget.h"

@ -24,8 +24,6 @@
#include "table/strings.h"
static uint32 _industry_creation_random_bits;
/* Since the industry IDs defined by the GRF file don't necessarily correlate
* to those used by the game, the IDs used for overriding old industries must be
* translated when the idustry spec is set. */
@ -159,14 +157,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
return count << 16 | GB(closest_dist, 0, 16);
}
/**
* This function implements the industries variables that newGRF defines.
* @param object the object that we want to query
* @param variable that is queried
* @param parameter unused
* @param available will return false if ever the variable asked for does not exist
* @return the value stored in the corresponding variable
*/
/* XXX Temporarily kept for access by the industry tile resolver. */
uint32 IndustryGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
{
const Industry *industry = object->u.industry.ind;
@ -348,83 +339,298 @@ uint32 IndustryGetVariable(const ResolverObject *object, byte variable, uint32 p
return UINT_MAX;
}
static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
/* virtual */ uint32 IndustriesScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
{
if (this->ro->callback == CBID_INDUSTRY_LOCATION) {
/* Variables available during construction check. */
switch (variable) {
case 0x80: return this->tile;
case 0x81: return GB(this->tile, 8, 8);
/* Pointer to the town the industry is associated with */
case 0x82: return this->industry->town->index;
case 0x83:
case 0x84:
case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
/* Number of the layout */
case 0x86: return this->industry->selected_layout;
/* Ground type */
case 0x87: return GetTerrainType(this->tile);
/* Town zone */
case 0x88: return GetTownRadiusGroup(this->industry->town, this->tile);
/* Manhattan distance of the closest town */
case 0x89: return min(DistanceManhattan(this->industry->town->xy, this->tile), 255);
/* Lowest height of the tile */
case 0x8A: return Clamp(GetTileZ(this->tile) * (this->ro->grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF);
/* Distance to the nearest water/land tile */
case 0x8B: return GetClosestWaterDistance(this->tile, (GetIndustrySpec(this->industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
/* Square of Euclidian distance from town */
case 0x8D: return min(DistanceSquare(this->industry->town->xy, this->tile), 65535);
/* 32 random bits */
case 0x8F: return this->random_bits;
}
}
const IndustrySpec *indspec = GetIndustrySpec(this->type);
if (this->industry == NULL) {
DEBUG(grf, 1, "Unhandled variable 0x%X (no available industry) in callback 0x%x", variable, this->ro->callback);
*available = false;
return UINT_MAX;
}
switch (variable) {
case 0x40:
case 0x41:
case 0x42: { // waiting cargo, but only if those two callback flags are set
uint16 callback = indspec->callback_mask;
if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
if (this->industry->prod_level == 0) return 0;
return min(this->industry->incoming_cargo_waiting[variable - 0x40] / this->industry->prod_level, (uint16)0xFFFF);
} else {
return min(this->industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
}
} else {
return 0;
}
}
/* Manhattan distance of closes dry/water tile */
case 0x43: return GetClosestWaterDistance(this->tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
/* Layout number */
case 0x44: return this->industry->selected_layout;
/* Company info */
case 0x45: {
byte colours = 0;
bool is_ai = false;
const Company *c = Company::GetIfValid(this->industry->founder);
if (c != NULL) {
const Livery *l = &c->livery[LS_DEFAULT];
is_ai = c->is_ai;
colours = l->colour1 + l->colour2 * 16;
}
return this->industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
}
case 0x46: return this->industry->construction_date; // Date when built - long format - (in days)
/* Get industry ID at offset param */
case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro->grffile->grfid);
/* Get random tile bits at offset param */
case 0x61: {
TileIndex tile = GetNearbyTile(parameter, this->tile, false);
return this->industry->TileBelongsToIndustry(tile) ? GetIndustryRandomBits(tile) : 0;
}
/* Land info of nearby tiles */
case 0x62: return GetNearbyIndustryTileInformation(parameter, this->tile, INVALID_INDUSTRY, false, this->ro->grffile->grf_version >= 8);
/* Animation stage of nearby tiles */
case 0x63: {
TileIndex tile = GetNearbyTile(parameter, this->tile, false);
if (this->industry->TileBelongsToIndustry(tile)) {
return GetAnimationFrame(tile);
}
return 0xFFFFFFFF;
}
/* Distance of nearest industry of given type */
case 0x64: return GetClosestIndustry(this->tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), this->industry);
/* Get town zone and Manhattan distance of closest town */
case 0x65: return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceManhattan(this->tile, this->industry->town->xy), 0xFFFF);
/* Get square of Euclidian distance of closes town */
case 0x66: return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceSquare(this->tile, this->industry->town->xy), 0xFFFF);
/* Count of industry, distance of closest instance
* 68 is the same as 67, but with a filtering on selected layout */
case 0x67:
case 0x68: {
byte layout_filter = 0;
bool town_filter = false;
if (variable == 0x68) {
uint32 reg = GetRegister(0x101);
layout_filter = GB(reg, 0, 8);
town_filter = HasBit(reg, 8);
}
return GetCountAndDistanceOfClosestInstance(parameter, layout_filter, town_filter, this->industry);
}
/* Get a variable from the persistent storage */
case 0x7C: return (this->industry->psa != NULL) ? this->industry->psa->GetValue(parameter) : 0;
/* Industry structure access*/
case 0x80: return this->industry->location.tile;
case 0x81: return GB(this->industry->location.tile, 8, 8);
/* Pointer to the town the industry is associated with */
case 0x82: return this->industry->town->index;
case 0x83:
case 0x84:
case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
case 0x86: return this->industry->location.w;
case 0x87: return this->industry->location.h;// xy dimensions
case 0x88:
case 0x89: return this->industry->produced_cargo[variable - 0x88];
case 0x8A: return this->industry->produced_cargo_waiting[0];
case 0x8B: return GB(this->industry->produced_cargo_waiting[0], 8, 8);
case 0x8C: return this->industry->produced_cargo_waiting[1];
case 0x8D: return GB(this->industry->produced_cargo_waiting[1], 8, 8);
case 0x8E:
case 0x8F: return this->industry->production_rate[variable - 0x8E];
case 0x90:
case 0x91:
case 0x92: return this->industry->accepts_cargo[variable - 0x90];
case 0x93: return this->industry->prod_level;
/* amount of cargo produced so far THIS month. */
case 0x94: return this->industry->this_month_production[0];
case 0x95: return GB(this->industry->this_month_production[0], 8, 8);
case 0x96: return this->industry->this_month_production[1];
case 0x97: return GB(this->industry->this_month_production[1], 8, 8);
/* amount of cargo transported so far THIS month. */
case 0x98: return this->industry->this_month_transported[0];
case 0x99: return GB(this->industry->this_month_transported[0], 8, 8);
case 0x9A: return this->industry->this_month_transported[1];
case 0x9B: return GB(this->industry->this_month_transported[1], 8, 8);
/* fraction of cargo transported LAST month. */
case 0x9C:
case 0x9D: return this->industry->last_month_pct_transported[variable - 0x9C];
/* amount of cargo produced LAST month. */
case 0x9E: return this->industry->last_month_production[0];
case 0x9F: return GB(this->industry->last_month_production[0], 8, 8);
case 0xA0: return this->industry->last_month_production[1];
case 0xA1: return GB(this->industry->last_month_production[1], 8, 8);
/* amount of cargo transported last month. */
case 0xA2: return this->industry->last_month_transported[0];
case 0xA3: return GB(this->industry->last_month_transported[0], 8, 8);
case 0xA4: return this->industry->last_month_transported[1];
case 0xA5: return GB(this->industry->last_month_transported[1], 8, 8);
case 0xA6: return this->industry->type;
case 0xA7: return this->industry->founder;
case 0xA8: return this->industry->random_colour;
case 0xA9: return Clamp(this->industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
case 0xAA: return this->industry->counter;
case 0xAB: return GB(this->industry->counter, 8, 8);
case 0xAC: return this->industry->was_cargo_delivered;
case 0xB0: return Clamp(this->industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
case 0xB3: return this->industry->construction_type; // Construction type
case 0xB4: return Clamp(this->industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date last cargo accepted since 1920 (in days)
}
DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
*available = false;
return UINT_MAX;
}
/* virtual */ const SpriteGroup *IndustriesResolverObject::ResolveReal(const RealSpriteGroup *group) const
{
/* IndustryTile do not have 'real' groups */
return NULL;
}
static uint32 IndustryGetRandomBits(const ResolverObject *object)
/* virtual */ uint32 IndustriesScopeResolver::GetRandomBits() const
{
const Industry *ind = object->u.industry.ind;
return ind != NULL ? ind->random: 0;
return this->industry != NULL ? this->industry->random : 0;
}
static uint32 IndustryGetTriggers(const ResolverObject *object)
/* virtual */ uint32 IndustriesScopeResolver::GetTriggers() const
{
const Industry *ind = object->u.industry.ind;
return ind != NULL ? ind->random_triggers : 0;
return this->industry != NULL ? this->industry->random_triggers : 0;
}
static void IndustrySetTriggers(const ResolverObject *object, int triggers)
/* virtual */ void IndustriesScopeResolver::SetTriggers(int triggers) const
{
Industry *ind = object->u.industry.ind;
assert(ind != NULL && ind->index != INVALID_INDUSTRY);
ind->random_triggers = triggers;
assert(this->industry != NULL && this->industry->index != INVALID_INDUSTRY);
this->industry->random_triggers = triggers;
}
/**
* Store a value into the object's persistent storage.
* @param object Object that we want to query.
* @param pos Position in the persistent storage to use.
* @param value Value to store.
*/
void IndustryStorePSA(ResolverObject *object, uint pos, int32 value)
/* virtual */ void IndustriesScopeResolver::StorePSA(uint pos, int32 value)
{
Industry *ind = object->u.industry.ind;
if (ind->index == INVALID_INDUSTRY) return;
if (this->industry->index == INVALID_INDUSTRY) return;
if (object->scope != VSG_SCOPE_SELF) {
/* Pass the request on to the town of the industry. */
TownStorePSA(ind->town, object->grffile, pos, value);
return;
}
if (ind->psa == NULL) {
if (this->industry->psa == NULL) {
/* There is no need to create a storage if the value is zero. */
if (value == 0) return;
/* Create storage on first modification. */
const IndustrySpec *indsp = GetIndustrySpec(ind->type);
const IndustrySpec *indsp = GetIndustrySpec(this->industry->type);
uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
assert(PersistentStorage::CanAllocateItem());
ind->psa = new PersistentStorage(grfid);
this->industry->psa = new PersistentStorage(grfid);
}
ind->psa->StoreValue(pos, value);
this->industry->psa->StoreValue(pos, value);
}
static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus, IndustryType type)
/**
* Get the grf file associated with the given industry type.
* @param type Industry type to query.
* @return The associated GRF file, if any.
*/
static const GRFFile *GetGrffile(IndustryType type)
{
res->GetRandomBits = IndustryGetRandomBits;
res->GetTriggers = IndustryGetTriggers;
res->SetTriggers = IndustrySetTriggers;
res->GetVariable = IndustryGetVariable;
res->ResolveRealMethod = IndustryResolveReal;
res->StorePSA = IndustryStorePSA;
res->u.industry.tile = tile;
res->u.industry.ind = indus;
res->u.industry.gfx = INVALID_INDUSTRYTILE;
res->u.industry.type = type;
res->callback = CBID_NO_CALLBACK;
res->callback_param1 = 0;
res->callback_param2 = 0;
res->ResetState();
const IndustrySpec *indspec = GetIndustrySpec(type);
res->grffile = (indspec != NULL ? indspec->grf_prop.grffile : NULL);
return (indspec != NULL) ? indspec->grf_prop.grffile : NULL;
}
IndustriesResolverObject::IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32 random_bits,
CallbackID callback, uint32 callback_param1, uint32 callback_param2)
: ResolverObject(GetGrffile(type), callback, callback_param1, callback_param2),
industries_scope(this, tile, indus, type, random_bits),
town_scope(NULL)
{
}
IndustriesResolverObject::~IndustriesResolverObject()
{
delete this->town_scope;
}
/**
* Get or create the town scope object associated with the industry.
* @return The associated town scope, if it exists.
*/
TownScopeResolver *IndustriesResolverObject::GetTown()
{
if (this->town_scope == NULL) {
Town *t = NULL;
if (this->industries_scope.industry != NULL) {
t = this->industries_scope.industry->town;
} else if (this->industries_scope.tile != INVALID_TILE) {
t = ClosestTownFromTile(this->industries_scope.tile, UINT_MAX);
}
if (t == NULL) return NULL;
this->town_scope = new TownScopeResolver(this, t, this->industries_scope.industry->index == INVALID_INDUSTRY);
}
return this->town_scope;
}
IndustriesScopeResolver::IndustriesScopeResolver(ResolverObject *ro, TileIndex tile, Industry *industry, IndustryType type, uint32 random_bits)
: ScopeResolver(ro)
{
this->tile = tile;
this->industry = industry;
this->type = type;
this->random_bits = random_bits;
}
/**
@ -439,68 +645,13 @@ static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *i
*/
uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
{
ResolverObject object;
const SpriteGroup *group;
NewIndustryResolver(&object, tile, industry, type);
object.callback = callback;
object.callback_param1 = param1;
object.callback_param2 = param2;
group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
IndustriesResolverObject object(tile, industry, type, 0, callback, param1, param2);
const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
if (group == NULL) return CALLBACK_FAILED;
return group->GetCallbackResult();
}
uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
{
const Industry *industry = object->u.industry.ind;
TileIndex tile = object->u.industry.tile;
if (object->scope == VSG_SCOPE_PARENT) {
return TownGetVariable(variable, parameter, available, industry->town, object->grffile);
}
switch (variable) {
case 0x80: return tile;
case 0x81: return GB(tile, 8, 8);
/* Pointer to the town the industry is associated with */
case 0x82: return industry->town->index;
case 0x83:
case 0x84:
case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
/* Number of the layout */
case 0x86: return industry->selected_layout;
/* Ground type */
case 0x87: return GetTerrainType(tile);
/* Town zone */
case 0x88: return GetTownRadiusGroup(industry->town, tile);
/* Manhattan distance of the closest town */
case 0x89: return min(DistanceManhattan(industry->town->xy, tile), 255);
/* Lowest height of the tile */
case 0x8A: return Clamp(GetTileZ(tile) * (object->grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF);
/* Distance to the nearest water/land tile */
case 0x8B: return GetClosestWaterDistance(tile, (GetIndustrySpec(industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
/* Square of Euclidian distance from town */
case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);
/* 32 random bits */
case 0x8F: return _industry_creation_random_bits;
}
/* None of the special ones, so try the general ones */
return IndustryGetVariable(object, variable, parameter, available);
}
/**
* Check that the industry callback allows creation of the industry.
* @param tile %Tile to build the industry.
@ -516,9 +667,6 @@ CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uin
{
const IndustrySpec *indspec = GetIndustrySpec(type);
ResolverObject object;
const SpriteGroup *group;
Industry ind;
ind.index = INVALID_INDUSTRY;
ind.location.tile = tile;
@ -530,13 +678,8 @@ CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uin
ind.founder = founder;
ind.psa = NULL;
NewIndustryResolver(&object, tile, &ind, type);
object.GetVariable = IndustryLocationGetVariable;
object.callback = CBID_INDUSTRY_LOCATION;
object.callback_param2 = creation_type;
_industry_creation_random_bits = seed;
group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
IndustriesResolverObject object(tile, &ind, type, seed, CBID_INDUSTRY_LOCATION, 0, creation_type);
const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
/* Unlike the "normal" cases, not having a valid result means we allow
* the building of the industry, as that's how it's done in TTDP. */
@ -589,8 +732,7 @@ static int32 DerefIndProd(int field, bool use_register)
void IndustryProductionCallback(Industry *ind, int reason)
{
const IndustrySpec *spec = GetIndustrySpec(ind->type);
ResolverObject object;
NewIndustryResolver(&object, ind->location.tile, ind, ind->type);
IndustriesResolverObject object(ind->location.tile, ind, ind->type);
if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
int multiplier = 1;
if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
@ -632,17 +774,6 @@ void IndustryProductionCallback(Industry *ind, int reason)
SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
}
/**
* Resolve a industry's spec and such so we can get a variable.
* @param ro The resolver object to fill.
* @param index The industry ID to get the data from.
*/
void GetIndustryResolver(ResolverObject *ro, uint index)
{
Industry *i = Industry::Get(index);
NewIndustryResolver(ro, i->location.tile, i, i->type);
}
/**
* Check whether an industry temporarily refuses to accept a certain cargo.
* @param ind The industry to query.

@ -12,7 +12,48 @@
#ifndef NEWGRF_INDUSTRIES_H
#define NEWGRF_INDUSTRIES_H
#include "newgrf_spritegroup.h"
#include "newgrf_town.h"
struct IndustriesScopeResolver : public ScopeResolver {
TileIndex tile;
Industry *industry;
IndustryType type;
uint32 random_bits; ///< Random bits of the new industry.
IndustriesScopeResolver(ResolverObject *ro, TileIndex tile, Industry *industry, IndustryType type, uint32 random_bits = 0);
/* virtual */ uint32 GetRandomBits() const;
/* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
/* virtual */ uint32 GetTriggers() const;
/* virtual */ void SetTriggers(int triggers) const;
/* virtual */ void StorePSA(uint pos, int32 value);
};
struct IndustriesResolverObject : public ResolverObject {
IndustriesScopeResolver industries_scope;
TownScopeResolver *town_scope;
IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32 random_bits = 0,
CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
~IndustriesResolverObject();
TownScopeResolver *GetTown();
/* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
{
switch (scope) {
case VSG_SCOPE_SELF: return &industries_scope;
case VSG_SCOPE_PARENT: {
TownScopeResolver *tsr = this->GetTown();
if (tsr != NULL) return tsr;
/* FALL-THROUGH */
}
default: return &this->default_scope; // XXX ResolverObject::GetScope(scope, relative);
}
}
/* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
};
/** When should the industry(tile) be triggered for random bits? */
enum IndustryTrigger {

@ -308,7 +308,14 @@ class NIHIndustry : public NIHelper {
const void *GetSpec(uint index) const { return GetIndustrySpec(Industry::Get(index)->type); }
void SetStringParameters(uint index) const { this->SetSimpleStringParameters(STR_INDUSTRY_NAME, index); }
uint32 GetGRFID(uint index) const { return (this->IsInspectable(index)) ? GetIndustrySpec(Industry::Get(index)->type)->grf_prop.grffile->grfid : 0; }
void Resolve(ResolverObject *ro, uint32 index) const { extern void GetIndustryResolver(ResolverObject *ro, uint index); GetIndustryResolver(ro, index); }
/* virtual */ uint Resolve(uint index, uint var, uint param, bool *avail) const
{
Industry *i = Industry::Get(index);
IndustriesResolverObject ro(i->location.tile, i, i->type);
return ro.GetScope(ro.scope)->GetVariable(var, param, avail);
}
uint GetPSASize(uint index, uint32 grfid) const { return cpp_lengthof(PersistentStorage, storage); }
const int32 *GetPSAFirstPosition(uint index, uint32 grfid) const

Loading…
Cancel
Save