(svn r9418) -Codechange: Implement actions 1/2/3 for cargos, callback handler and custom icon sprites
authorpeter1138
Fri, 23 Mar 2007 20:55:45 +0000
changeset 6365 b52bd4ca10b6
parent 6364 ec741938a286
child 6366 155956db09e9
(svn r9418) -Codechange: Implement actions 1/2/3 for cargos, callback handler and custom icon sprites
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/cargotype.h
src/newgrf.cpp
src/newgrf_cargo.cpp
src/newgrf_cargo.h
src/newgrf_spritegroup.h
src/station_gui.cpp
src/table/cargo_const.h
--- a/projects/openttd.vcproj	Fri Mar 23 20:51:42 2007 +0000
+++ b/projects/openttd.vcproj	Fri Mar 23 20:55:45 2007 +0000
@@ -926,6 +926,9 @@
 				RelativePath=".\..\src\newgrf.cpp">
 			</File>
 			<File
+				RelativePath=".\..\src\newgrf_cargo.cpp">
+			</File>
+			<File
 				RelativePath=".\..\src\newgrf_config.cpp">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj	Fri Mar 23 20:51:42 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Fri Mar 23 20:55:45 2007 +0000
@@ -448,11 +448,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\autoreplace_cmd.cpp"
+				RelativePath=".\..\src\aystar.cpp"
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\aystar.cpp"
+				RelativePath=".\..\src\autoreplace_cmd.cpp"
 				>
 			</File>
 			<File
@@ -1456,6 +1456,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\newgrf_cargo.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\newgrf_config.cpp"
 				>
 			</File>
--- a/source.list	Fri Mar 23 20:51:42 2007 +0000
+++ b/source.list	Fri Mar 23 20:55:45 2007 +0000
@@ -279,6 +279,7 @@
 
 # NewGRF
 newgrf.cpp
+newgrf_cargo.cpp
 newgrf_config.cpp
 newgrf_engine.cpp
 newgrf_house.cpp
--- a/src/cargotype.h	Fri Mar 23 20:51:42 2007 +0000
+++ b/src/cargotype.h	Fri Mar 23 20:55:45 2007 +0000
@@ -42,6 +42,7 @@
 	SpriteID sprite;
 
 	uint16 classes;
+	const struct SpriteGroup *group;
 
 	bool IsValid() const;
 };
--- a/src/newgrf.cpp	Fri Mar 23 20:51:42 2007 +0000
+++ b/src/newgrf.cpp	Fri Mar 23 20:55:45 2007 +0000
@@ -2217,6 +2217,7 @@
 				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 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 @@
 			TownHouseMapSpriteGroup(buf, idcount, cidcount);
 			return;
 
+		case GSF_CARGOS:
+			CargoMapSpriteGroup(buf, idcount, cidcount);
+			return;
+
 		default:
 			grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
 			return;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/newgrf_cargo.cpp	Fri Mar 23 20:55:45 2007 +0000
@@ -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;
+}
--- a/src/newgrf_cargo.h	Fri Mar 23 20:51:42 2007 +0000
+++ b/src/newgrf_cargo.h	Fri Mar 23 20:55:45 2007 +0000
@@ -21,4 +21,9 @@
 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 */
--- a/src/newgrf_spritegroup.h	Fri Mar 23 20:51:42 2007 +0000
+++ b/src/newgrf_spritegroup.h	Fri Mar 23 20:55:45 2007 +0000
@@ -191,6 +191,9 @@
 			Town *town;
 			HouseID house_id;
 		} house;
+		struct {
+			const struct CargoSpec *cs;
+		} cargo;
 	} u;
 
 	uint32 (*GetRandomBits)(const struct ResolverObject*);
--- a/src/station_gui.cpp	Fri Mar 23 20:51:42 2007 +0000
+++ b/src/station_gui.cpp	Fri Mar 23 20:55:45 2007 +0000
@@ -674,7 +674,16 @@
 	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);
--- a/src/table/cargo_const.h	Fri Mar 23 20:51:42 2007 +0000
+++ b/src/table/cargo_const.h	Fri Mar 23 20:55:45 2007 +0000
@@ -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,