newgrf_cargo.cpp

00001 /* $Id: newgrf_cargo.cpp 13871 2008-07-29 22:37:54Z rubidium $ */
00002 
00003 #include "stdafx.h"
00004 #include "openttd.h"
00005 #include "debug.h"
00006 #include "cargotype.h"
00007 #include "newgrf.h"
00008 #include "newgrf_callbacks.h"
00009 #include "newgrf_spritegroup.h"
00010 #include "newgrf_cargo.h"
00011 
00012 
00013 static uint32 CargoGetRandomBits(const ResolverObject *object)
00014 {
00015   return 0;
00016 }
00017 
00018 
00019 static uint32 CargoGetTriggers(const ResolverObject *object)
00020 {
00021   return 0;
00022 }
00023 
00024 
00025 static void CargoSetTriggers(const ResolverObject *object, int triggers)
00026 {
00027   return;
00028 }
00029 
00030 
00031 static uint32 CargoGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00032 {
00033   DEBUG(grf, 1, "Unhandled cargo property 0x%X", variable);
00034 
00035   *available = false;
00036   return 0;
00037 }
00038 
00039 
00040 static const SpriteGroup *CargoResolveReal(const ResolverObject *object, const SpriteGroup *group)
00041 {
00042   /* Cargo action 2s should always have only 1 "loaded" state, but some
00043    * times things don't follow the spec... */
00044   if (group->g.real.num_loaded > 0) return group->g.real.loaded[0];
00045   if (group->g.real.num_loading > 0) return group->g.real.loading[0];
00046 
00047   return NULL;
00048 }
00049 
00050 
00051 static void NewCargoResolver(ResolverObject *res, const CargoSpec *cs)
00052 {
00053   res->GetRandomBits = &CargoGetRandomBits;
00054   res->GetTriggers   = &CargoGetTriggers;
00055   res->SetTriggers   = &CargoSetTriggers;
00056   res->GetVariable   = &CargoGetVariable;
00057   res->ResolveReal   = &CargoResolveReal;
00058 
00059   res->u.cargo.cs = cs;
00060 
00061   res->callback        = CBID_NO_CALLBACK;
00062   res->callback_param1 = 0;
00063   res->callback_param2 = 0;
00064   res->last_value      = 0;
00065   res->trigger         = 0;
00066   res->reseed          = 0;
00067 }
00068 
00069 
00070 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
00071 {
00072   const SpriteGroup *group;
00073   ResolverObject object;
00074 
00075   NewCargoResolver(&object, cs);
00076 
00077   group = Resolve(cs->group, &object);
00078   if (group == NULL || group->type != SGT_RESULT) return 0;
00079 
00080   return group->g.result.sprite;
00081 }
00082 
00083 
00084 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
00085 {
00086   ResolverObject object;
00087   const SpriteGroup *group;
00088 
00089   NewCargoResolver(&object, cs);
00090   object.callback = callback;
00091   object.callback_param1 = param1;
00092   object.callback_param2 = param2;
00093 
00094   group = Resolve(cs->group, &object);
00095   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00096 
00097   return group->g.callback.result;
00098 }
00099 
00100 
00101 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
00102 {
00103   /* Pre-version 7 uses the 'climate dependent' ID, i.e. cargo is the cargo ID */
00104   if (grffile->grf_version < 7) {
00105     if (!usebit) return cargo;
00106     /* Else the cargo value is a 'climate independent' 'bitnum' */
00107     if (HasBit(_cargo_mask, cargo)) return GetCargoIDByBitnum(cargo);
00108   } else {
00109     /* If the GRF contains a translation table... */
00110     if (grffile->cargo_max > 0) {
00111       /* ...and the cargo is in bounds, then get the cargo ID for
00112        * the label */
00113       if (cargo < grffile->cargo_max) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
00114     } else {
00115       /* Else the cargo value is a 'climate independent' 'bitnum' */
00116       if (HasBit(_cargo_mask, cargo)) return GetCargoIDByBitnum(cargo);
00117     }
00118   }
00119   return CT_INVALID;
00120 }
00121 
00122 uint8 GetReverseCargoTranslation(CargoID cargo, const GRFFile *grffile)
00123 {
00124   /* Note: All grf versions use CargoBit here. Pre-version 7 do NOT use the 'climate dependent' ID. */
00125   const CargoSpec *cs = GetCargo(cargo);
00126 
00127   /* If the GRF contains a translation table (and the cargo is in the table)
00128    * then get the cargo ID for the label */
00129   for (uint i = 0; i < grffile->cargo_max; i++) {
00130     if (cs->label == grffile->cargo_list[i]) return i;
00131   }
00132 
00133   /* No matching label was found, so we return the 'climate independent' 'bitnum' */
00134   return cs->bitnum;;
00135 }

Generated on Wed Oct 1 17:03:22 2008 for openttd by  doxygen 1.5.6