newgrf_cargo.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_cargo.cpp 23917 2012-02-08 18:11:49Z frosch $ */
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_spritegroup.h"
00015 
00016 static uint32 CargoGetRandomBits(const ResolverObject *object)
00017 {
00018   return 0;
00019 }
00020 
00021 
00022 static uint32 CargoGetTriggers(const ResolverObject *object)
00023 {
00024   return 0;
00025 }
00026 
00027 
00028 static void CargoSetTriggers(const ResolverObject *object, int triggers)
00029 {
00030   return;
00031 }
00032 
00033 
00034 static uint32 CargoGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00035 {
00036   DEBUG(grf, 1, "Unhandled cargo variable 0x%X", variable);
00037 
00038   *available = false;
00039   return UINT_MAX;
00040 }
00041 
00042 
00043 static const SpriteGroup *CargoResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00044 {
00045   /* Cargo action 2s should always have only 1 "loaded" state, but some
00046    * times things don't follow the spec... */
00047   if (group->num_loaded > 0) return group->loaded[0];
00048   if (group->num_loading > 0) return group->loading[0];
00049 
00050   return NULL;
00051 }
00052 
00053 
00054 static void NewCargoResolver(ResolverObject *res, const CargoSpec *cs)
00055 {
00056   res->GetRandomBits = &CargoGetRandomBits;
00057   res->GetTriggers   = &CargoGetTriggers;
00058   res->SetTriggers   = &CargoSetTriggers;
00059   res->GetVariable   = &CargoGetVariable;
00060   res->ResolveReal   = &CargoResolveReal;
00061 
00062   res->u.cargo.cs = cs;
00063 
00064   res->callback        = CBID_NO_CALLBACK;
00065   res->callback_param1 = 0;
00066   res->callback_param2 = 0;
00067   res->ResetState();
00068 
00069   res->grffile         = cs->grffile;
00070 }
00071 
00072 
00073 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
00074 {
00075   const SpriteGroup *group;
00076   ResolverObject object;
00077 
00078   NewCargoResolver(&object, cs);
00079 
00080   group = SpriteGroup::Resolve(cs->group, &object);
00081   if (group == NULL) return 0;
00082 
00083   return group->GetResult();
00084 }
00085 
00086 
00087 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
00088 {
00089   ResolverObject object;
00090   const SpriteGroup *group;
00091 
00092   NewCargoResolver(&object, cs);
00093   object.callback = callback;
00094   object.callback_param1 = param1;
00095   object.callback_param2 = param2;
00096 
00097   group = SpriteGroup::Resolve(cs->group, &object);
00098   if (group == NULL) return CALLBACK_FAILED;
00099 
00100   return group->GetCallbackResult();
00101 }
00102 
00112 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
00113 {
00114   /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
00115   if (grffile->grf_version < 7 && !usebit) return cargo;
00116 
00117   /* Other cases use (possibly translated) cargobits */
00118 
00119   if (grffile->cargo_max > 0) {
00120     /* ...and the cargo is in bounds, then get the cargo ID for
00121      * the label */
00122     if (cargo < grffile->cargo_max) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
00123   } else {
00124     /* Else the cargo value is a 'climate independent' 'bitnum' */
00125     return GetCargoIDByBitnum(cargo);
00126   }
00127   return CT_INVALID;
00128 }
00129 
00130 uint8 GetReverseCargoTranslation(CargoID cargo, const GRFFile *grffile)
00131 {
00132   /* Note: All grf versions use CargoBit here. Pre-version 7 do NOT use the 'climate dependent' ID. */
00133   const CargoSpec *cs = CargoSpec::Get(cargo);
00134 
00135   /* If the GRF contains a translation table (and the cargo is in the table)
00136    * then get the cargo ID for the label */
00137   for (uint i = 0; i < grffile->cargo_max; i++) {
00138     if (cs->label == grffile->cargo_list[i]) return i;
00139   }
00140 
00141   /* No matching label was found, so we return the 'climate independent' 'bitnum' */
00142   return cs->bitnum;
00143 }