# newgrf_cargo.cpp

Go to the documentation of this file.

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