# 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.h"
    00015 #include "newgrf_spritegroup.h"
    00016 #include <list>
    00017
    00018
    00019 struct GenericCallback {
    00020   const GRFFile *file;
    00021   const SpriteGroup *group;
    00022
    00023   GenericCallback(const GRFFile *file, const SpriteGroup *group) :
    00024     file(file),
    00025     group(group)
    00026   { }
    00027 };
    00028
    00029 typedef std::list<GenericCallback> GenericCallbackList;
    00030
    00031 static GenericCallbackList _gcl[GSF_END];
    00032
    00033
    00037 void ResetGenericCallbacks()
    00038 {
    00039   for (uint8 feature = 0; feature < lengthof(_gcl); feature++) {
    00040     _gcl[feature].clear();
    00041   }
    00042 }
    00043
    00044
    00051 void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group)
    00052 {
    00053   if (feature >= lengthof(_gcl)) {
    00054     grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature);
    00055     return;
    00056   }
    00057
    00058   /* Generic feature callbacks are evaluated in reverse (i.e. the last group
    00059    * to be added is evaluated first, etc) thus we push the group to the
    00060    * beginning of the list so a standard iterator will do the right thing. */
    00061   _gcl[feature].push_front(GenericCallback(file, group));
    00062 }
    00063
    00064
    00065 static uint32 GenericCallbackGetRandomBits(const ResolverObject *object)
    00066 {
    00067   return 0;
    00068 }
    00069
    00070
    00071 static uint32 GenericCallbackGetTriggers(const ResolverObject *object)
    00072 {
    00073   return 0;
    00074 }
    00075
    00076
    00077 static void GenericCallbackSetTriggers(const ResolverObject *object, int triggers)
    00078 {
    00079   return;
    00080 }
    00081
    00082
    00083 static uint32 GenericCallbackGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00084 {
    00085   switch (variable) {
    00086     case 0x40: return object->u.generic.cargo_type;
    00087
    00088     case 0x80: return object->u.generic.cargo_type;
    00089     case 0x81: return object->u.generic.cargo_type;
    00090     case 0x82: return object->u.generic.default_selection;
    00091     case 0x83: return object->u.generic.src_industry;
    00092     case 0x84: return object->u.generic.dst_industry;
    00093     case 0x85: return object->u.generic.distance;
    00094     case 0x86: return object->u.generic.event;
    00095     case 0x87: return object->u.generic.count;
    00096     case 0x88: return object->u.generic.station_size;
    00097
    00098     default: break;
    00099   }
    00100
    00101   DEBUG(grf, 1, "Unhandled generic feature property 0x%02X", variable);
    00102
    00103   *available = false;
    00104   return UINT_MAX;
    00105 }
    00106
    00107
    00108 static const SpriteGroup *GenericCallbackResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00109 {
    00110   if (group->num_loaded == 0) return NULL;
    00111
    00112   return group->loaded[0];
    00113 }
    00114
    00115
    00116 static inline void NewGenericResolver(ResolverObject *res, const GRFFile *grffile)
    00117 {
    00118   res->GetRandomBits = &GenericCallbackGetRandomBits;
    00119   res->GetTriggers   = &GenericCallbackGetTriggers;
    00120   res->SetTriggers   = &GenericCallbackSetTriggers;
    00121   res->GetVariable   = &GenericCallbackGetVariable;
    00122   res->ResolveReal   = &GenericCallbackResolveReal;
    00123
    00124   res->callback        = CBID_NO_CALLBACK;
    00125   res->callback_param1 = 0;
    00126   res->callback_param2 = 0;
    00127   res->last_value      = 0;
    00128   res->trigger         = 0;
    00129   res->reseed          = 0;
    00130   res->count           = 0;
    00131   res->grffile         = grffile;
    00132 }
    00133
    00134
    00142 static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject *object, const GRFFile **file)
    00143 {
    00144   assert(feature < lengthof(_gcl));
    00145
    00146   /* Test each feature callback sprite group. */
    00147   for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
    00148     const SpriteGroup *group = it->group;
    00149     group = SpriteGroup::Resolve(group, object);
    00150     if (group == NULL) continue;
    00151
    00152     /* Return NewGRF file if necessary */
    00153     if (file != NULL) *file = it->file;
    00154
    00155     return group->GetCallbackResult();
    00156   }
    00157
    00158   /* No callback returned a valid result, so we've failed. */
    00159   return CALLBACK_FAILED;
    00160 }
    00161
    00162
    00166 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)
    00167 {
    00168   ResolverObject object;
    00169
    00170   NewGenericResolver(&object, *file);
    00171
    00172   object.callback = CBID_GENERIC_AI_PURCHASE_SELECTION;
    00173   object.u.generic.cargo_type        = cargo_type;
    00174   object.u.generic.default_selection = default_selection;
    00175   object.u.generic.src_industry      = src_industry;
    00176   object.u.generic.dst_industry      = dst_industry;
    00177   object.u.generic.distance          = distance;
    00178   object.u.generic.event             = event;
    00179   object.u.generic.count             = count;
    00180   object.u.generic.station_size      = station_size;
    00181
    00182   uint16 callback = GetGenericCallbackResult(feature, &object, file);
    00183   if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
    00184   return callback;
    00185 }