# newgrf_generic.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_generic.cpp 15513 2009-02-18 09:14:41Z peter1138 $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "debug.h"
    00007 #include "newgrf.h"
    00008 #include "newgrf_spritegroup.h"
    00009 #include <list>
    00010
    00011
    00012 struct GenericCallback {
    00013   const GRFFile *file;
    00014   const SpriteGroup *group;
    00015
    00016   GenericCallback(const GRFFile *file, const SpriteGroup *group) :
    00017     file(file),
    00018     group(group)
    00019   { }
    00020 };
    00021
    00022 typedef std::list<GenericCallback> GenericCallbackList;
    00023
    00024 static GenericCallbackList _gcl[GSF_END];
    00025
    00026
    00030 void ResetGenericCallbacks()
    00031 {
    00032   for (uint8 feature = 0; feature < lengthof(_gcl); feature++) {
    00033     _gcl[feature].clear();
    00034   }
    00035 }
    00036
    00037
    00044 void AddGenericCallback(uint8 feature, const GRFFile *file, const SpriteGroup *group)
    00045 {
    00046   if (feature >= lengthof(_gcl)) {
    00047     grfmsg(5, "AddGenericCallback: Unsupported feature 0x%02X", feature);
    00048     return;
    00049   }
    00050
    00051   /* Generic feature callbacks are evaluated in reverse (i.e. the last group
    00052    * to be added is evaluated first, etc) thus we push the group to the
    00053    * beginning of the list so a standard iterator will do the right thing. */
    00054   _gcl[feature].push_front(GenericCallback(file, group));
    00055 }
    00056
    00057
    00058 static uint32 GenericCallbackGetRandomBits(const ResolverObject *object)
    00059 {
    00060   return 0;
    00061 }
    00062
    00063
    00064 static uint32 GenericCallbackGetTriggers(const ResolverObject *object)
    00065 {
    00066   return 0;
    00067 }
    00068
    00069
    00070 static void GenericCallbackSetTriggers(const ResolverObject *object, int triggers)
    00071 {
    00072   return;
    00073 }
    00074
    00075
    00076 static uint32 GenericCallbackGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00077 {
    00078   switch (variable) {
    00079     case 0x40: return object->u.generic.cargo_type;
    00080
    00081     case 0x80: return object->u.generic.cargo_type;
    00082     case 0x81: return object->u.generic.cargo_type;
    00083     case 0x82: return object->u.generic.default_selection;
    00084     case 0x83: return object->u.generic.src_industry;
    00085     case 0x84: return object->u.generic.dst_industry;
    00086     case 0x85: return object->u.generic.distance;
    00087     case 0x86: return object->u.generic.event;
    00088     case 0x87: return object->u.generic.count;
    00089     case 0x88: return object->u.generic.station_size;
    00090
    00091     default: break;
    00092   }
    00093
    00094   DEBUG(grf, 1, "Unhandled generic feature property 0x%02X", variable);
    00095
    00096   *available = false;
    00097   return UINT_MAX;
    00098 }
    00099
    00100
    00101 static const SpriteGroup *GenericCallbackResolveReal(const ResolverObject *object, const SpriteGroup *group)
    00102 {
    00103   if (group->g.real.num_loaded == 0) return NULL;
    00104
    00105   return group->g.real.loaded[0];
    00106 }
    00107
    00108
    00109 static inline void NewGenericResolver(ResolverObject *res, const GRFFile *grffile)
    00110 {
    00111   res->GetRandomBits = &GenericCallbackGetRandomBits;
    00112   res->GetTriggers   = &GenericCallbackGetTriggers;
    00113   res->SetTriggers   = &GenericCallbackSetTriggers;
    00114   res->GetVariable   = &GenericCallbackGetVariable;
    00115   res->ResolveReal   = &GenericCallbackResolveReal;
    00116
    00117   res->callback        = CBID_NO_CALLBACK;
    00118   res->callback_param1 = 0;
    00119   res->callback_param2 = 0;
    00120   res->last_value      = 0;
    00121   res->trigger         = 0;
    00122   res->reseed          = 0;
    00123   res->count           = 0;
    00124   res->grffile         = grffile;
    00125 }
    00126
    00127
    00135 static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject *object, const GRFFile **file)
    00136 {
    00137   assert(feature < lengthof(_gcl));
    00138
    00139   /* Test each feature callback sprite group. */
    00140   for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) {
    00141     const SpriteGroup *group = it->group;
    00142     group = Resolve(group, object);
    00143     if (group == NULL || group->type != SGT_CALLBACK) continue;
    00144
    00145     /* Return NewGRF file if necessary */
    00146     if (file != NULL) *file = it->file;
    00147
    00148     return group->g.callback.result;
    00149   }
    00150
    00151   /* No callback returned a valid result, so we've failed. */
    00152   return CALLBACK_FAILED;
    00153 }
    00154
    00155
    00159 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)
    00160 {
    00161   ResolverObject object;
    00162
    00163   NewGenericResolver(&object, *file);
    00164
    00165   object.callback = CBID_GENERIC_AI_PURCHASE_SELECTION;
    00166   object.u.generic.cargo_type        = cargo_type;
    00167   object.u.generic.default_selection = default_selection;
    00168   object.u.generic.src_industry      = src_industry;
    00169   object.u.generic.dst_industry      = dst_industry;
    00170   object.u.generic.distance          = distance;
    00171   object.u.generic.event             = event;
    00172   object.u.generic.count             = count;
    00173   object.u.generic.station_size      = station_size;
    00174
    00175   uint16 callback = GetGenericCallbackResult(feature, &object, file);
    00176   if (callback != CALLBACK_FAILED) callback = GB(callback, 0, 8);
    00177   return callback;
    00178 }