# newgrf_generic.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "newgrf_spritegroup.h"
    00015 #include "industrytype.h"
    00016 #include "core/random_func.hpp"
    00017 #include "newgrf_sound.h"
    00018 #include "water_map.h"
    00019 #include <list>
    00020
    00021
    00022 struct GenericCallback {
    00023   const GRFFile *file;
    00024   const SpriteGroup *group;
    00025
    00026   GenericCallback(const GRFFile *file, const SpriteGroup *group) :
    00027     file(file),
    00028     group(group)
    00029   { }
    00030 };
    00031
    00032 typedef std::list<GenericCallback> GenericCallbackList;
    00033
    00034 static GenericCallbackList _gcl[GSF_END];
    00035
    00036
    00040 void ResetGenericCallbacks()
    00041 {
    00042   for (uint8 feature = 0; feature < lengthof(_gcl); feature++) {
    00043     _gcl[feature].clear();
    00044   }
    00045 }
    00046
    00047
    00054 void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group)
    00055 {
    00056   if (feature >= lengthof(_gcl)) {
    00057     grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature);
    00058     return;
    00059   }
    00060
    00061   /* Generic feature callbacks are evaluated in reverse (i.e. the last group
    00062    * to be added is evaluated first, etc) thus we push the group to the
    00063    * beginning of the list so a standard iterator will do the right thing. */
    00064   _gcl[feature].push_front(GenericCallback(file, group));
    00065 }
    00066
    00067
    00068 static uint32 GenericCallbackGetRandomBits(const ResolverObject *object)
    00069 {
    00070   return 0;
    00071 }
    00072
    00073
    00074 static uint32 GenericCallbackGetTriggers(const ResolverObject *object)
    00075 {
    00076   return 0;
    00077 }
    00078
    00079
    00080 static void GenericCallbackSetTriggers(const ResolverObject *object, int triggers)
    00081 {
    00082   return;
    00083 }
    00084
    00085
    00086 static uint32 GenericCallbackGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
    00087 {
    00088   DEBUG(grf, 1, "Unhandled generic feature variable 0x%02X", variable);
    00089
    00090   *available = false;
    00091   return UINT_MAX;
    00092 }
    00093
    00094 static uint32 GenericAiCallbackGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
    00095 {
    00096   switch (variable) {
    00097     case 0x40: return object->grffile->cargo_map[object->u.generic.cargo_type];
    00098
    00099     case 0x80: return object->u.generic.cargo_type;
    00100     case 0x81: return CargoSpec::Get(object->u.generic.cargo_type)->bitnum;
    00101     case 0x82: return object->u.generic.default_selection;
    00102     case 0x83: return object->u.generic.src_industry;
    00103     case 0x84: return object->u.generic.dst_industry;
    00104     case 0x85: return object->u.generic.distance;
    00105     case 0x86: return object->u.generic.event;
    00106     case 0x87: return object->u.generic.count;
    00107     case 0x88: return object->u.generic.station_size;
    00108
    00109     default: break;
    00110   }
    00111
    00112   DEBUG(grf, 1, "Unhandled generic feature variable 0x%02X", variable);
    00113
    00114   *available = false;
    00115   return UINT_MAX;
    00116 }
    00117
    00118
    00119 static const SpriteGroup *GenericCallbackResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00120 {
    00121   if (group->num_loaded == 0) return NULL;
    00122
    00123   return group->loaded[0];
    00124 }
    00125
    00126
    00127 static inline void NewGenericResolver(ResolverObject *res, bool ai_callback)
    00128 {
    00129   res->GetRandomBits = &GenericCallbackGetRandomBits;
    00130   res->GetTriggers   = &GenericCallbackGetTriggers;
    00131   res->SetTriggers   = &GenericCallbackSetTriggers;
    00132   res->GetVariable   = ai_callback ? &GenericAiCallbackGetVariable : &GenericCallbackGetVariable;
    00133   res->ResolveReal   = &GenericCallbackResolveReal;
    00134
    00135   res->callback        = CBID_NO_CALLBACK;
    00136   res->callback_param1 = 0;
    00137   res->callback_param2 = 0;
    00138   res->ResetState();
    00139 }
    00140
    00141
    00153 static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject *object, uint32 param1_grfv7, uint32 param1_grfv8, const GRFFile **file)
    00154 {
    00155   assert(feature < lengthof(_gcl));
    00156
    00157   /* Test each feature callback sprite group. */
    00158   for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
    00159     const SpriteGroup *group = it->group;
    00160     object->grffile = it->file;
    00161     /* Set callback param based on GRF version. */
    00162     object->callback_param1 = it->file->grf_version >= 8 ? param1_grfv8 : param1_grfv7;
    00163     group = SpriteGroup::Resolve(group, object);
    00164     if (group == NULL || group->GetCallbackResult() == CALLBACK_FAILED) continue;
    00165
    00166     /* Return NewGRF file if necessary */
    00167     if (file != NULL) *file = it->file;
    00168
    00169     return group->GetCallbackResult();
    00170   }
    00171
    00172   /* No callback returned a valid result, so we've failed. */
    00173   return CALLBACK_FAILED;
    00174 }
    00175
    00176
    00193 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)
    00194 {
    00195   ResolverObject object;
    00196
    00197   NewGenericResolver(&object, true);
    00198
    00199   if (src_industry != IT_AI_UNKNOWN && src_industry != IT_AI_TOWN) {
    00200     const IndustrySpec *is = GetIndustrySpec(src_industry);
    00201     /* If this is no original industry, use the substitute type */
    00202     if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) src_industry = is->grf_prop.subst_id;
    00203   }
    00204
    00205   if (dst_industry != IT_AI_UNKNOWN && dst_industry != IT_AI_TOWN) {
    00206     const IndustrySpec *is = GetIndustrySpec(dst_industry);
    00207     /* If this is no original industry, use the substitute type */
    00208     if (is->grf_prop.subst_id != INVALID_INDUSTRYTYPE) dst_industry = is->grf_prop.subst_id;
    00209   }
    00210
    00211   object.callback = CBID_GENERIC_AI_PURCHASE_SELECTION;
    00212   object.u.generic.cargo_type        = cargo_type;
    00213   object.u.generic.default_selection = default_selection;
    00214   object.u.generic.src_industry      = src_industry;
    00215   object.u.generic.dst_industry      = dst_industry;
    00216   object.u.generic.distance          = distance;
    00217   object.u.generic.event             = event;
    00218   object.u.generic.count             = count;
    00219   object.u.generic.station_size      = station_size;
    00220
    00221   uint16 callback = GetGenericCallbackResult(feature, &object, 0, 0, file);
    00222   if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
    00223   return callback;
    00224 }
    00225
    00226
    00231 void AmbientSoundEffectCallback(TileIndex tile)
    00232 {
    00233   assert(IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES) || IsTileType(tile, MP_WATER));
    00234
    00235   /* Only run every 1/200-th time. */
    00236   uint32 r; // Save for later
    00237   if (!Chance16R(1, 200, r)) return;
    00238
    00239   ResolverObject object;
    00240
    00241   /* Prepare resolver object. */
    00242   NewGenericResolver(&object, false);
    00243   object.callback = CBID_SOUNDS_AMBIENT_EFFECT;
    00244
    00245   uint32 param1_v7 = GetTileType(tile) << 28 | Clamp(TileHeight(tile), 0, 15) << 24 | GB(r, 16, 8) << 16 | GetTerrainType(tile);
    00246   uint32 param1_v8 = GetTileType(tile) << 24 | GetTileZ(tile) << 16 | GB(r, 16, 8) << 8 | (HasTileWaterClass(tile) ? GetWaterClass(tile) : 0) << 3 | GetTerrainType(tile);
    00247
    00248   /* Run callback. */
    00249   const GRFFile *grf_file;
    00250   uint16 callback = GetGenericCallbackResult(GSF_SOUNDFX, &object, param1_v7, param1_v8, &grf_file);
    00251
    00252   if (callback != CALLBACK_FAILED) PlayTileSound(grf_file, callback, tile);
    00253 }