OpenTTD
newgrf_cargo.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_cargo.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "newgrf_spritegroup.h"
15 
16 #include "safeguards.h"
17 
21 
22  /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
23 };
24 
25 /* virtual */ const SpriteGroup *CargoResolverObject::ResolveReal(const RealSpriteGroup *group) const
26 {
27  /* Cargo action 2s should always have only 1 "loaded" state, but some
28  * times things don't follow the spec... */
29  if (group->num_loaded > 0) return group->loaded[0];
30  if (group->num_loading > 0) return group->loading[0];
31 
32  return NULL;
33 }
34 
43  : ResolverObject(cs->grffile, callback, callback_param1, callback_param2)
44 {
45  this->root_spritegroup = cs->group;
46 }
47 
54 {
55  CargoResolverObject object(cs);
56  const SpriteGroup *group = object.Resolve();
57  if (group == NULL) return 0;
58 
59  return group->GetResult();
60 }
61 
62 
63 uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
64 {
65  CargoResolverObject object(cs, callback, param1, param2);
66  return object.ResolveCallback();
67 }
68 
78 CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
79 {
80  /* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
81  if (grffile->grf_version < 7 && !usebit) return cargo;
82 
83  /* Other cases use (possibly translated) cargobits */
84 
85  if (grffile->cargo_list.Length() > 0) {
86  /* ...and the cargo is in bounds, then get the cargo ID for
87  * the label */
88  if (cargo < grffile->cargo_list.Length()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
89  } else {
90  /* Else the cargo value is a 'climate independent' 'bitnum' */
91  return GetCargoIDByBitnum(cargo);
92  }
93  return CT_INVALID;
94 }
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const
Get the real sprites of the grf.
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
CargoID GetCargoIDByLabel(CargoLabel cl)
Get the cargo ID by cargo label.
Definition: cargotype.cpp:88
Functions related to debugging.
CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoID.
Interface for SpriteGroup-s to access the gamestate.
Specification of a cargo type.
Definition: cargotype.h:56
Set when using the callback resolve system, but not to resolve a callback.
byte num_loaded
Number of loaded groups.
uint32 callback_param1
First parameter (var 10) of the callback.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
uint32 callback_param2
Second parameter (var 18) of the callback.
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Action 2 handling.
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
uint Length() const
Get the number of items in the list.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
Resolver of cargo.
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
Invalid cargo type.
Definition: cargo_type.h:70
byte num_loading
Number of loading groups.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
CargoID GetCargoIDByBitnum(uint8 bitnum)
Find the CargoID of a &#39;bitnum&#39; value.
Definition: cargotype.cpp:105
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
SmallVector< CargoLabel, 4 > cargo_list
Cargo translation table (local ID -> label)
Definition: newgrf.h:125
CallbackID callback
Callback being resolved.
CallbackID
List of implemented NewGRF callbacks.
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:104
CargoResolverObject(const CargoSpec *cs, CallbackID callback=CBID_NO_CALLBACK, uint32 callback_param1=0, uint32 callback_param2=0)
Constructor of the cargo resolver.