2007-03-23 20:55:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_cargo.cpp Implementation of NewGRF cargoes. */
|
|
|
|
|
2007-03-23 20:55:45 +00:00
|
|
|
#include "stdafx.h"
|
2007-03-26 08:43:14 +00:00
|
|
|
#include "debug.h"
|
2007-03-23 20:55:45 +00:00
|
|
|
#include "newgrf.h"
|
|
|
|
#include "newgrf_spritegroup.h"
|
|
|
|
#include "newgrf_cargo.h"
|
|
|
|
|
|
|
|
static uint32 CargoGetRandomBits(const ResolverObject *object)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint32 CargoGetTriggers(const ResolverObject *object)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CargoSetTriggers(const ResolverObject *object, int triggers)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint32 CargoGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
|
|
|
|
{
|
2007-03-26 08:43:14 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled cargo property 0x%X", variable);
|
|
|
|
|
2007-03-23 20:55:45 +00:00
|
|
|
*available = false;
|
2009-02-18 09:14:41 +00:00
|
|
|
return UINT_MAX;
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
static const SpriteGroup *CargoResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
|
2007-03-23 20:55:45 +00:00
|
|
|
{
|
2007-05-20 09:17:42 +00:00
|
|
|
/* Cargo action 2s should always have only 1 "loaded" state, but some
|
|
|
|
* times things don't follow the spec... */
|
2009-05-23 12:13:42 +00:00
|
|
|
if (group->num_loaded > 0) return group->loaded[0];
|
|
|
|
if (group->num_loading > 0) return group->loading[0];
|
2007-03-23 20:55:45 +00:00
|
|
|
|
2007-05-20 09:17:42 +00:00
|
|
|
return NULL;
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void NewCargoResolver(ResolverObject *res, const CargoSpec *cs)
|
|
|
|
{
|
|
|
|
res->GetRandomBits = &CargoGetRandomBits;
|
|
|
|
res->GetTriggers = &CargoGetTriggers;
|
|
|
|
res->SetTriggers = &CargoSetTriggers;
|
|
|
|
res->GetVariable = &CargoGetVariable;
|
|
|
|
res->ResolveReal = &CargoResolveReal;
|
|
|
|
|
|
|
|
res->u.cargo.cs = cs;
|
|
|
|
|
2007-07-25 19:06:29 +00:00
|
|
|
res->callback = CBID_NO_CALLBACK;
|
2007-03-23 20:55:45 +00:00
|
|
|
res->callback_param1 = 0;
|
|
|
|
res->callback_param2 = 0;
|
|
|
|
res->last_value = 0;
|
|
|
|
res->trigger = 0;
|
|
|
|
res->reseed = 0;
|
2008-03-27 21:36:16 +00:00
|
|
|
res->count = 0;
|
2008-07-30 18:23:12 +00:00
|
|
|
res->grffile = cs->grffile;
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
|
|
|
|
{
|
|
|
|
const SpriteGroup *group;
|
|
|
|
ResolverObject object;
|
|
|
|
|
|
|
|
NewCargoResolver(&object, cs);
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
group = SpriteGroup::Resolve(cs->group, &object);
|
2009-05-23 12:13:42 +00:00
|
|
|
if (group == NULL) return 0;
|
2007-03-23 20:55:45 +00:00
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
return group->GetResult();
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-25 19:06:29 +00:00
|
|
|
uint16 GetCargoCallback(CallbackID callback, uint32 param1, uint32 param2, const CargoSpec *cs)
|
2007-03-23 20:55:45 +00:00
|
|
|
{
|
|
|
|
ResolverObject object;
|
|
|
|
const SpriteGroup *group;
|
|
|
|
|
|
|
|
NewCargoResolver(&object, cs);
|
|
|
|
object.callback = callback;
|
|
|
|
object.callback_param1 = param1;
|
|
|
|
object.callback_param2 = param2;
|
|
|
|
|
2009-05-23 15:25:52 +00:00
|
|
|
group = SpriteGroup::Resolve(cs->group, &object);
|
2009-05-23 12:13:42 +00:00
|
|
|
if (group == NULL) return CALLBACK_FAILED;
|
2007-03-23 20:55:45 +00:00
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
return group->GetCallbackResult();
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
2007-04-13 19:32:18 +00:00
|
|
|
|
|
|
|
|
2007-10-13 02:23:11 +00:00
|
|
|
CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
|
2007-04-13 19:32:18 +00:00
|
|
|
{
|
|
|
|
/* Pre-version 7 uses the 'climate dependent' ID, i.e. cargo is the cargo ID */
|
2007-10-13 02:23:11 +00:00
|
|
|
if (grffile->grf_version < 7) {
|
|
|
|
if (!usebit) return cargo;
|
|
|
|
/* Else the cargo value is a 'climate independent' 'bitnum' */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(_cargo_mask, cargo)) return GetCargoIDByBitnum(cargo);
|
2007-10-13 02:23:11 +00:00
|
|
|
} else {
|
2008-07-28 06:16:34 +00:00
|
|
|
/* If the GRF contains a translation table... */
|
|
|
|
if (grffile->cargo_max > 0) {
|
|
|
|
/* ...and the cargo is in bounds, then get the cargo ID for
|
|
|
|
* the label */
|
|
|
|
if (cargo < grffile->cargo_max) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
|
|
|
|
} else {
|
|
|
|
/* Else the cargo value is a 'climate independent' 'bitnum' */
|
|
|
|
if (HasBit(_cargo_mask, cargo)) return GetCargoIDByBitnum(cargo);
|
|
|
|
}
|
2007-10-13 02:23:11 +00:00
|
|
|
}
|
|
|
|
return CT_INVALID;
|
2007-04-13 19:32:18 +00:00
|
|
|
}
|
2007-07-08 17:40:04 +00:00
|
|
|
|
|
|
|
uint8 GetReverseCargoTranslation(CargoID cargo, const GRFFile *grffile)
|
|
|
|
{
|
2008-02-15 10:52:10 +00:00
|
|
|
/* Note: All grf versions use CargoBit here. Pre-version 7 do NOT use the 'climate dependent' ID. */
|
2007-07-08 17:40:04 +00:00
|
|
|
const CargoSpec *cs = GetCargo(cargo);
|
|
|
|
|
|
|
|
/* If the GRF contains a translation table (and the cargo is in the table)
|
|
|
|
* then get the cargo ID for the label */
|
|
|
|
for (uint i = 0; i < grffile->cargo_max; i++) {
|
|
|
|
if (cs->label == grffile->cargo_list[i]) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No matching label was found, so we return the 'climate independent' 'bitnum' */
|
|
|
|
return cs->bitnum;;
|
|
|
|
}
|