From f69549f1ebb7458bc7e6d6bbd81449884cdcaf4e Mon Sep 17 00:00:00 2001 From: peter1138 Date: Fri, 23 Mar 2007 20:55:45 +0000 Subject: (svn r9418) -Codechange: Implement actions 1/2/3 for cargos, callback handler and custom icon sprites --- projects/openttd.vcproj | 3 ++ projects/openttd_vs80.vcproj | 8 +++- source.list | 1 + src/cargotype.h | 1 + src/newgrf.cpp | 32 +++++++++++++++ src/newgrf_cargo.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++ src/newgrf_cargo.h | 5 +++ src/newgrf_spritegroup.h | 3 ++ src/station_gui.cpp | 11 +++++- src/table/cargo_const.h | 2 +- 10 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 src/newgrf_cargo.cpp diff --git a/projects/openttd.vcproj b/projects/openttd.vcproj index 2e01dfaca..c23dfcf05 100644 --- a/projects/openttd.vcproj +++ b/projects/openttd.vcproj @@ -925,6 +925,9 @@ + + diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 668094f1a..360f08155 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -448,11 +448,11 @@ > + + diff --git a/source.list b/source.list index 36347e9d3..ac2eeb57d 100644 --- 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 index 1bb157202..d74811c9a 100644 --- 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 index 384ada128..55751b151 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -2217,6 +2217,7 @@ static void NewSpriteGroup(byte *buf, int len) 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 *buf, uint8 idcount, uint8 cidcount) } } + +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 *buf, int len) 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 index 000000000..2e79d02c1 --- /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 index 2711d1352..e37d45ee0 100644 --- a/src/newgrf_cargo.h +++ b/src/newgrf_cargo.h @@ -21,4 +21,9 @@ static const CargoID CT_DEFAULT = NUM_CARGO + 0; 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 index a62e23c82..e01bc2a9e 100644 --- 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 index bb26460b4..6188130a0 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -674,7 +674,16 @@ static void DrawCargoIcons(CargoID i, uint waiting, int x, int y) 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 index e79450bed..11f5d4b2c 100644 --- 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, -- cgit v1.2.3-54-g00ecf