newgrf_generic.cpp

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

Generated on Wed Mar 3 23:32:23 2010 for OpenTTD by  doxygen 1.6.1