diff --git a/projects/openttd.vcproj b/projects/openttd.vcproj --- a/projects/openttd.vcproj +++ b/projects/openttd.vcproj @@ -926,6 +926,9 @@ RelativePath=".\..\src\newgrf.cpp"> + + + + diff --git a/source.list b/source.list --- a/source.list +++ b/source.list @@ -279,6 +279,7 @@ ai/trolly/trolly.cpp # NewGRF newgrf.cpp +newgrf_cargo.cpp newgrf_config.cpp newgrf_engine.cpp newgrf_house.cpp diff --git a/src/cargotype.h b/src/cargotype.h --- a/src/cargotype.h +++ b/src/cargotype.h @@ -42,6 +42,7 @@ struct CargoSpec { SpriteID sprite; uint16 classes; + const struct SpriteGroup *group; bool IsValid() const; }; diff --git a/src/newgrf.cpp b/src/newgrf.cpp --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -2217,6 +2217,7 @@ static void NewSpriteGroup(byte *buf, in case GSF_SHIP: case GSF_AIRCRAFT: case GSF_STATION: + case GSF_CARGOS: { byte sprites = _cur_grffile->spriteset_numents; byte num_loaded = type; @@ -2556,6 +2557,33 @@ static void TownHouseMapSpriteGroup(byte } } + +static void CargoMapSpriteGroup(byte *buf, uint8 idcount, uint8 cidcount) +{ + byte *bp = &buf[4 + idcount + cidcount * 3]; + uint16 groupid = grf_load_word(&bp); + + if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) { + grfmsg(1, "FeatureMapSpriteGroup: Spriteset 0x%04X out of range 0x%X or empty, skipping.", + groupid, _cur_grffile->spritegroups_count); + return; + } + + for (uint i = 0; i < idcount; i++) { + CargoID cid = buf[3 + i]; + + if (cid >= NUM_CARGO) { + grfmsg(1, "FeatureMapSpriteGroup: Cargo ID %d out of range, skipping"); + continue; + } + + CargoSpec *cs = &_cargo[cid]; + cs->grfid = _cur_grffile->grfid; + cs->group = _cur_grffile->spritegroups[groupid]; + } +} + + /* Action 0x03 */ static void FeatureMapSpriteGroup(byte *buf, int len) { @@ -2614,6 +2642,10 @@ static void FeatureMapSpriteGroup(byte * TownHouseMapSpriteGroup(buf, idcount, cidcount); return; + case GSF_CARGOS: + CargoMapSpriteGroup(buf, idcount, cidcount); + return; + default: grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature); return; diff --git a/src/newgrf_cargo.cpp b/src/newgrf_cargo.cpp new file mode 100644 --- /dev/null +++ b/src/newgrf_cargo.cpp @@ -0,0 +1,93 @@ +/* $Id$ */ + +#include "stdafx.h" +#include "openttd.h" +#include "cargotype.h" +#include "newgrf.h" +#include "newgrf_callbacks.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) +{ + *available = false; + return 0; +} + + +static const SpriteGroup *CargoResolveReal(const ResolverObject *object, const SpriteGroup *group) +{ + /* Cargo action 2s should always have only 1 "loaded" state */ + if (group->g.real.num_loaded == 0) return NULL; + + return group->g.real.loaded[0]; +} + + +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; + + res->callback = 0; + res->callback_param1 = 0; + res->callback_param2 = 0; + res->last_value = 0; + res->trigger = 0; + res->reseed = 0; +} + + +SpriteID GetCustomCargoSprite(const CargoSpec *cs) +{ + const SpriteGroup *group; + ResolverObject object; + + NewCargoResolver(&object, cs); + + group = Resolve(cs->group, &object); + if (group == NULL || group->type != SGT_RESULT) return 0; + + return group->g.result.sprite; +} + + +uint16 GetCargoCallback(uint16 callback, uint32 param1, uint32 param2, const CargoSpec *cs) +{ + ResolverObject object; + const SpriteGroup *group; + + NewCargoResolver(&object, cs); + object.callback = callback; + object.callback_param1 = param1; + object.callback_param2 = param2; + + group = Resolve(cs->group, &object); + if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED; + + return group->g.callback.result; +} diff --git a/src/newgrf_cargo.h b/src/newgrf_cargo.h --- a/src/newgrf_cargo.h +++ b/src/newgrf_cargo.h @@ -21,4 +21,9 @@ static const CargoID CT_DEFAULT = N static const CargoID CT_PURCHASE = NUM_CARGO + 1; static const CargoID CT_DEFAULT_NA = NUM_CARGO + 2; +typedef struct CargoSpec; + +SpriteID GetCustomCargoSprite(const CargoSpec *cs); +uint16 GetCargoCallback(uint16 callback, uint32 param1, uint32 param2, const CargoSpec *cs); + #endif /* NEWGRF_CARGO_H */ diff --git a/src/newgrf_spritegroup.h b/src/newgrf_spritegroup.h --- a/src/newgrf_spritegroup.h +++ b/src/newgrf_spritegroup.h @@ -191,6 +191,9 @@ struct ResolverObject { Town *town; HouseID house_id; } house; + struct { + const struct CargoSpec *cs; + } cargo; } u; uint32 (*GetRandomBits)(const struct ResolverObject*); diff --git a/src/station_gui.cpp b/src/station_gui.cpp --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -674,7 +674,16 @@ static void DrawCargoIcons(CargoID i, ui if (num == 0) return; const CargoSpec *cs = GetCargo(i); - SpriteID sprite = cs->sprite; + SpriteID sprite; + + if (cs->sprite == 0xFFFF) { + /* A value of 0xFFFF indicates we should draw a custom icon */ + sprite = GetCustomCargoSprite(cs); + } else { + sprite = cs->sprite; + } + + if (sprite == 0) return; do { DrawSprite(sprite, PAL_NONE, x, y); diff --git a/src/table/cargo_const.h b/src/table/cargo_const.h --- a/src/table/cargo_const.h +++ b/src/table/cargo_const.h @@ -3,7 +3,7 @@ /* Table of all default cargo types */ #define MK(bt, label, c, e, f, g, h, fr, te, ks1, ks2, ks3, ks4, ks5, l, m) \ - {bt, label, 0, c, c, e, f, {g, h}, fr, te, 0, 0, ks1, ks2, ks3, ks4, ks5, l, m} + {bt, label, 0, c, c, e, f, {g, h}, fr, te, 0, 0, ks1, ks2, ks3, ks4, ks5, l, m, NULL} static const CargoSpec _default_cargo[] = { MK( 0, 'PASS', 152, 1, 3185, 0, 24, false, TE_PASSENGERS, STR_000F_PASSENGERS, STR_002F_PASSENGER, STR_PASSENGERS, STR_QUANTITY_PASSENGERS, STR_ABBREV_PASSENGERS,