# newgrf_cargo.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_cargo.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "debug.h"
    00007 #include "newgrf.h"
    00008 #include "newgrf_spritegroup.h"
    00009 #include "newgrf_cargo.h"
    00010
    00011 static uint32 CargoGetRandomBits(const ResolverObject *object)
    00012 {
    00013   return 0;
    00014 }
    00015
    00016
    00017 static uint32 CargoGetTriggers(const ResolverObject *object)
    00018 {
    00019   return 0;
    00020 }
    00021
    00022
    00023 static void CargoSetTriggers(const ResolverObject *object, int triggers)
    00024 {
    00025   return;
    00026 }
    00027
    00028
    00029 static uint32 CargoGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00030 {
    00031   DEBUG(grf, 1, "Unhandled cargo property 0x%X", variable);
    00032
    00033   *available = false;
    00034   return 0;
    00035 }
    00036
    00037
    00038 static const SpriteGroup *CargoResolveReal(const ResolverObject *object, const SpriteGroup *group)
    00039 {
    00040   /* Cargo action 2s should always have only 1 "loaded" state, but some
    00041    * times things don't follow the spec... */
    00042   if (group->g.real.num_loaded > 0) return group->g.real.loaded[0];
    00043   if (group->g.real.num_loading > 0) return group->g.real.loading[0];
    00044
    00045   return NULL;
    00046 }
    00047
    00048
    00049 static void NewCargoResolver(ResolverObject *res, const CargoSpec *cs)
    00050 {
    00051   res->GetRandomBits = &CargoGetRandomBits;
    00052   res->GetTriggers   = &CargoGetTriggers;
    00053   res->SetTriggers   = &CargoSetTriggers;
    00054   res->GetVariable   = &CargoGetVariable;
    00055   res->ResolveReal   = &CargoResolveReal;
    00056
    00057   res->u.cargo.cs = cs;
    00058
    00059   res->callback        = CBID_NO_CALLBACK;
    00060   res->callback_param1 = 0;
    00061   res->callback_param2 = 0;
    00062   res->last_value      = 0;
    00063   res->trigger         = 0;
    00064   res->reseed          = 0;
    00065   res->count           = 0;
    00066   res->grffile         = cs->grffile;
    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 }