# newgrf_generic.cpp

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