2008-02-12 13:23:57 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_generic.cpp Handling of generic feature callbacks. */
|
|
|
|
|
2008-02-12 13:23:57 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "newgrf_spritegroup.h"
|
2010-07-10 10:55:16 +00:00
|
|
|
#include "industrytype.h"
|
2011-11-04 21:05:08 +00:00
|
|
|
#include "core/random_func.hpp"
|
|
|
|
#include "newgrf_sound.h"
|
2011-11-08 17:29:32 +00:00
|
|
|
#include "water_map.h"
|
2008-02-12 13:23:57 +00:00
|
|
|
#include <list>
|
|
|
|
|
2012-11-10 20:43:40 +00:00
|
|
|
/** Scope resolver for generic objects and properties. */
|
|
|
|
struct GenericScopeResolver : public ScopeResolver {
|
|
|
|
CargoID cargo_type;
|
|
|
|
uint8 default_selection;
|
|
|
|
uint8 src_industry; ///< Source industry substitute type. 0xFF for "town", 0xFE for "unknown".
|
|
|
|
uint8 dst_industry; ///< Destination industry substitute type. 0xFF for "town", 0xFE for "unknown".
|
|
|
|
uint8 distance;
|
|
|
|
AIConstructionEvent event;
|
|
|
|
uint8 count;
|
|
|
|
uint8 station_size;
|
|
|
|
|
|
|
|
GenericScopeResolver(ResolverObject *ro, bool ai_callback);
|
|
|
|
|
|
|
|
/* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool ai_callback; ///< Callback comes from the AI.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Resolver object for generic objects/properties. */
|
|
|
|
struct GenericResolverObject : public ResolverObject {
|
|
|
|
GenericScopeResolver generic_scope;
|
|
|
|
|
|
|
|
GenericResolverObject(bool ai_callback, CallbackID callback = CBID_NO_CALLBACK);
|
|
|
|
|
|
|
|
/* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
|
|
|
|
{
|
|
|
|
switch (scope) {
|
|
|
|
case VSG_SCOPE_SELF: return &this->generic_scope;
|
2012-11-10 20:45:59 +00:00
|
|
|
default: return ResolverObject::GetScope(scope, relative);
|
2012-11-10 20:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
|
|
|
|
};
|
2008-02-12 13:23:57 +00:00
|
|
|
|
|
|
|
struct GenericCallback {
|
|
|
|
const GRFFile *file;
|
|
|
|
const SpriteGroup *group;
|
|
|
|
|
|
|
|
GenericCallback(const GRFFile *file, const SpriteGroup *group) :
|
|
|
|
file(file),
|
|
|
|
group(group)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::list<GenericCallback> GenericCallbackList;
|
|
|
|
|
|
|
|
static GenericCallbackList _gcl[GSF_END];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset all generic feature callback sprite groups.
|
|
|
|
*/
|
|
|
|
void ResetGenericCallbacks()
|
|
|
|
{
|
|
|
|
for (uint8 feature = 0; feature < lengthof(_gcl); feature++) {
|
|
|
|
_gcl[feature].clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a generic feature callback sprite group to the appropriate feature list.
|
2011-05-02 16:14:23 +00:00
|
|
|
* @param feature The feature for the callback.
|
|
|
|
* @param file The GRF of the callback.
|
|
|
|
* @param group The sprite group of the callback.
|
2008-02-12 13:23:57 +00:00
|
|
|
*/
|
|
|
|
void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group)
|
|
|
|
{
|
|
|
|
if (feature >= lengthof(_gcl)) {
|
|
|
|
grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generic feature callbacks are evaluated in reverse (i.e. the last group
|
|
|
|
* to be added is evaluated first, etc) thus we push the group to the
|
|
|
|
* beginning of the list so a standard iterator will do the right thing. */
|
|
|
|
_gcl[feature].push_front(GenericCallback(file, group));
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:43:40 +00:00
|
|
|
/* virtual */ uint32 GenericScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
|
2008-02-12 13:23:57 +00:00
|
|
|
{
|
2012-11-10 20:43:40 +00:00
|
|
|
if (this->ai_callback) {
|
|
|
|
switch (variable) {
|
|
|
|
case 0x40: return this->ro->grffile->cargo_map[this->cargo_type];
|
|
|
|
|
|
|
|
case 0x80: return this->cargo_type;
|
|
|
|
case 0x81: return CargoSpec::Get(this->cargo_type)->bitnum;
|
|
|
|
case 0x82: return this->default_selection;
|
|
|
|
case 0x83: return this->src_industry;
|
|
|
|
case 0x84: return this->dst_industry;
|
|
|
|
case 0x85: return this->distance;
|
|
|
|
case 0x86: return this->event;
|
|
|
|
case 0x87: return this->count;
|
|
|
|
case 0x88: return this->station_size;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
2008-02-12 13:23:57 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 16:43:46 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled generic feature variable 0x%02X", variable);
|
2008-02-12 13:23:57 +00:00
|
|
|
|
|
|
|
*available = false;
|
2009-02-18 09:14:41 +00:00
|
|
|
return UINT_MAX;
|
2008-02-12 13:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-10 20:43:40 +00:00
|
|
|
/* virtual */ const SpriteGroup *GenericResolverObject::ResolveReal(const RealSpriteGroup *group) const
|
2008-02-12 13:23:57 +00:00
|
|
|
{
|
2009-05-23 12:13:42 +00:00
|
|
|
if (group->num_loaded == 0) return NULL;
|
2008-02-12 13:23:57 +00:00
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
return group->loaded[0];
|
2008-02-12 13:23:57 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Generic resolver.
|
|
|
|
* @param ai_callback Callback comes from the AI.
|
|
|
|
* @param callback Callback ID.
|
|
|
|
*/
|
2012-11-10 20:43:40 +00:00
|
|
|
GenericResolverObject::GenericResolverObject(bool ai_callback, CallbackID callback) : ResolverObject(NULL, callback), generic_scope(this, ai_callback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Generic scope resolver.
|
|
|
|
* @param ro Surrounding resolver.
|
|
|
|
* @param ai_callback Callback comes from the AI.
|
|
|
|
*/
|
2012-11-10 20:43:40 +00:00
|
|
|
GenericScopeResolver::GenericScopeResolver(ResolverObject *ro, bool ai_callback) : ScopeResolver(ro)
|
2008-02-12 13:23:57 +00:00
|
|
|
{
|
2012-11-10 20:43:40 +00:00
|
|
|
this->cargo_type = 0;
|
|
|
|
this->default_selection = 0;
|
|
|
|
this->src_industry = 0;
|
|
|
|
this->dst_industry = 0;
|
|
|
|
this->distance = 0;
|
|
|
|
this->event = (AIConstructionEvent)0;
|
|
|
|
this->count = 0;
|
|
|
|
this->station_size = 0;
|
|
|
|
this->ai_callback = ai_callback;
|
2008-02-12 13:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Follow a generic feature callback list and return the first successful
|
2008-02-12 13:23:57 +00:00
|
|
|
* answer
|
|
|
|
* @param feature GRF Feature of callback
|
|
|
|
* @param object pre-populated resolver object
|
2011-11-08 17:29:32 +00:00
|
|
|
* @param param1_grfv7 callback_param1 for GRFs up to version 7.
|
|
|
|
* @param param1_grfv8 callback_param1 for GRFs from version 8 on.
|
2011-09-01 19:37:21 +00:00
|
|
|
* @param [out] file Optionally returns the GRFFile which made the final decision for the callback result.
|
|
|
|
* May be NULL if not required.
|
2008-02-12 13:23:57 +00:00
|
|
|
* @return callback value if successful or CALLBACK_FAILED
|
|
|
|
*/
|
2011-11-08 17:29:32 +00:00
|
|
|
static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject *object, uint32 param1_grfv7, uint32 param1_grfv8, const GRFFile **file)
|
2008-02-12 13:23:57 +00:00
|
|
|
{
|
|
|
|
assert(feature < lengthof(_gcl));
|
|
|
|
|
|
|
|
/* Test each feature callback sprite group. */
|
|
|
|
for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
|
|
|
|
const SpriteGroup *group = it->group;
|
2011-09-01 19:37:21 +00:00
|
|
|
object->grffile = it->file;
|
2011-11-08 17:29:32 +00:00
|
|
|
/* Set callback param based on GRF version. */
|
|
|
|
object->callback_param1 = it->file->grf_version >= 8 ? param1_grfv8 : param1_grfv7;
|
2009-05-23 15:25:52 +00:00
|
|
|
group = SpriteGroup::Resolve(group, object);
|
2011-09-01 19:40:51 +00:00
|
|
|
if (group == NULL || group->GetCallbackResult() == CALLBACK_FAILED) continue;
|
2008-02-12 13:23:57 +00:00
|
|
|
|
|
|
|
/* Return NewGRF file if necessary */
|
|
|
|
if (file != NULL) *file = it->file;
|
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
return group->GetCallbackResult();
|
2008-02-12 13:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No callback returned a valid result, so we've failed. */
|
|
|
|
return CALLBACK_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Execute' an AI purchase selection callback
|
2011-09-01 19:37:21 +00:00
|
|
|
*
|
|
|
|
* @param feature GRF Feature to call callback for.
|
|
|
|
* @param cargo_type Cargotype to pass to callback. (Variable 80)
|
|
|
|
* @param default_selection 'Default selection' to pass to callback. (Variable 82)
|
|
|
|
* @param src_industry 'Source industry type' to pass to callback. (Variable 83)
|
|
|
|
* @param dst_industry 'Destination industry type' to pass to callback. (Variable 84)
|
|
|
|
* @param distance 'Distance between source and destination' to pass to callback. (Variable 85)
|
|
|
|
* @param event 'AI construction event' to pass to callback. (Variable 86)
|
|
|
|
* @param count 'Construction number' to pass to callback. (Variable 87)
|
|
|
|
* @param station_size 'Station size' to pass to callback. (Variable 88)
|
|
|
|
* @param [out] file Optionally returns the GRFFile which made the final decision for the callback result.
|
|
|
|
* May be NULL if not required.
|
|
|
|
* @return callback value if successful or CALLBACK_FAILED
|
2008-02-12 13:23:57 +00:00
|
|
|
*/
|
|
|
|
uint16 GetAiPurchaseCallbackResult(uint8 feature, CargoID cargo_type, uint8 default_selection, IndustryType src_industry, IndustryType dst_industry, uint8 distance, AIConstructionEvent event, uint8 count, uint8 station_size, const GRFFile **file)
|
|
|
|
{
|
2012-11-10 20:43:40 +00:00
|
|
|
GenericResolverObject object(true, CBID_GENERIC_AI_PURCHASE_SELECTION);
|
2008-02-12 13:23:57 +00:00
|
|
|
|
2010-07-10 10:55:16 +00:00
|
|
|
if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
|
|
|
|
const IndustrySpec *is = GetIndustrySpec(src_industry);
|
|
|
|
/* If this is no original industry, use the substitute type */
|
|
|
|
if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) src_industry = is->grf_prop.subst_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
|
|
|
|
const IndustrySpec *is = GetIndustrySpec(dst_industry);
|
|
|
|
/* If this is no original industry, use the substitute type */
|
|
|
|
if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) dst_industry = is->grf_prop.subst_id;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:43:40 +00:00
|
|
|
object.generic_scope.cargo_type = cargo_type;
|
|
|
|
object.generic_scope.default_selection = default_selection;
|
|
|
|
object.generic_scope.src_industry = src_industry;
|
|
|
|
object.generic_scope.dst_industry = dst_industry;
|
|
|
|
object.generic_scope.distance = distance;
|
|
|
|
object.generic_scope.event = event;
|
|
|
|
object.generic_scope.count = count;
|
|
|
|
object.generic_scope.station_size = station_size;
|
2008-02-12 13:23:57 +00:00
|
|
|
|
2011-11-08 17:29:32 +00:00
|
|
|
uint16 callback = GetGenericCallbackResult(feature, &object, 0, 0, file);
|
2008-04-21 14:33:33 +00:00
|
|
|
if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
|
|
|
|
return callback;
|
2008-02-12 13:23:57 +00:00
|
|
|
}
|
2011-11-04 21:05:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Execute' the ambient sound effect callback.
|
|
|
|
* @param tile Tile the sound effect should be generated for.
|
|
|
|
*/
|
|
|
|
void AmbientSoundEffectCallback(TileIndex tile)
|
|
|
|
{
|
|
|
|
assert(IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES) || IsTileType(tile, MP_WATER));
|
|
|
|
|
|
|
|
/* Only run every 1/200-th time. */
|
|
|
|
uint32 r; // Save for later
|
2012-12-23 21:09:09 +00:00
|
|
|
if (!Chance16R(1, 200, r) || !_settings_client.sound.ambient) return;
|
2011-11-04 21:05:08 +00:00
|
|
|
|
|
|
|
/* Prepare resolver object. */
|
2012-11-10 20:43:40 +00:00
|
|
|
GenericResolverObject object(false, CBID_SOUNDS_AMBIENT_EFFECT);
|
2011-11-08 17:29:32 +00:00
|
|
|
|
|
|
|
uint32 param1_v7 = GetTileType(tile) << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
|
|
|
|
uint32 param1_v8 = GetTileType(tile) << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? GetWaterClass(tile) : 0) << 3 | GetTerrainType(tile);
|
2011-11-04 21:05:08 +00:00
|
|
|
|
|
|
|
/* Run callback. */
|
|
|
|
const GRFFile *grf_file;
|
2011-11-08 17:29:32 +00:00
|
|
|
uint16 callback = GetGenericCallbackResult(GSF_SOUNDFX, &object, param1_v7, param1_v8, &grf_file);
|
2011-11-04 21:05:08 +00:00
|
|
|
|
|
|
|
if (callback != CALLBACK_FAILED) PlayTileSound(grf_file, callback, tile);
|
|
|
|
}
|