newgrf_cargo.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00017 struct CargoResolverObject : public ResolverObject {
00018   CargoResolverObject(const CargoSpec *cs, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
00019 
00020   /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
00021 };
00022 
00023 /* virtual */ const SpriteGroup *CargoResolverObject::ResolveReal(const RealSpriteGroup *group) const
00024 {
00025   /* Cargo action 2s should always have only 1 "loaded" state, but some
00026    * times things don't follow the spec... */
00027   if (group->num_loaded > 0) return group->loaded[0];
00028   if (group->num_loading > 0) return group->loading[0];
00029 
00030   return NULL;
00031 }
00032 
00040 CargoResolverObject::CargoResolverObject(const CargoSpec *cs, CallbackID callback, uint32 callback_param1, uint32 callback_param2)
00041     : ResolverObject(cs->grffile, callback, callback_param1, callback_param2)
00042 {
00043 }
00044 
00050 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
00051 {
00052   CargoResolverObject object(cs);
00053   const SpriteGroup *group = SpriteGroup::Resolve(cs->group, &object);
00054   if (group == NULL) return 0;
00055 
00056   return group->GetResult();
00057 }
00058 
00059 
00060 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
00061 {
00062   CargoResolverObject object(cs, callback, param1, param2);
00063   const SpriteGroup *group = SpriteGroup::Resolve(cs->group, &object);
00064   if (group == NULL) return CALLBACK_FAILED;
00065 
00066   return group->GetCallbackResult();
00067 }
00068 
00078 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
00079 {
00080   /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
00081   if (grffile->grf_version < 7 && !usebit) return cargo;
00082 
00083   /* Other cases use (possibly translated) cargobits */
00084 
00085   if (grffile->cargo_list.Length() > 0) {
00086     /* ...and the cargo is in bounds, then get the cargo ID for
00087      * the label */
00088     if (cargo < grffile->cargo_list.Length()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
00089   } else {
00090     /* Else the cargo value is a 'climate independent' 'bitnum' */
00091     return GetCargoIDByBitnum(cargo);
00092   }
00093   return CT_INVALID;
00094 }