newgrf_cargo.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_cargo.cpp 26388 2014-03-03 20:02:31Z 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 
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   this->root_spritegroup = cs->group;
00044 }
00045 
00051 SpriteID GetCustomCargoSprite(const CargoSpec *cs)
00052 {
00053   CargoResolverObject object(cs);
00054   const SpriteGroup *group = object.Resolve();
00055   if (group == NULL) return 0;
00056 
00057   return group->GetResult();
00058 }
00059 
00060 
00061 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
00062 {
00063   CargoResolverObject object(cs, callback, param1, param2);
00064   return object.ResolveCallback();
00065 }
00066 
00076 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
00077 {
00078   /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
00079   if (grffile->grf_version < 7 && !usebit) return cargo;
00080 
00081   /* Other cases use (possibly translated) cargobits */
00082 
00083   if (grffile->cargo_list.Length() > 0) {
00084     /* ...and the cargo is in bounds, then get the cargo ID for
00085      * the label */
00086     if (cargo < grffile->cargo_list.Length()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
00087   } else {
00088     /* Else the cargo value is a 'climate independent' 'bitnum' */
00089     return GetCargoIDByBitnum(cargo);
00090   }
00091   return CT_INVALID;
00092 }