Changeset - r6365:410001496130
[Not reviewed]
master
0 9 1
peter1138 - 18 years ago 2007-03-23 20:55:45
peter1138@openttd.org
(svn r9418) -Codechange: Implement actions 1/2/3 for cargos, callback handler and custom icon sprites
10 files changed with 155 insertions and 4 deletions:
0 comments (0 inline, 0 general)
projects/openttd.vcproj
Show inline comments
 
@@ -926,6 +926,9 @@
 
				RelativePath=".\..\src\newgrf.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\newgrf_cargo.cpp">
 
			</File>
 
			<File
 
				RelativePath=".\..\src\newgrf_config.cpp">
 
			</File>
 
			<File
projects/openttd_vs80.vcproj
Show inline comments
 
@@ -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>
source.list
Show inline comments
 
@@ -279,6 +279,7 @@ ai/trolly/trolly.cpp
 

	
 
# NewGRF
 
newgrf.cpp
 
newgrf_cargo.cpp
 
newgrf_config.cpp
 
newgrf_engine.cpp
 
newgrf_house.cpp
src/cargotype.h
Show inline comments
 
@@ -42,6 +42,7 @@ struct CargoSpec {
 
	SpriteID sprite;
 

	
 
	uint16 classes;
 
	const struct SpriteGroup *group;
 

	
 
	bool IsValid() const;
 
};
src/newgrf.cpp
Show inline comments
 
@@ -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;
src/newgrf_cargo.cpp
Show inline comments
 
new file 100644
 
/* $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;
 
}
src/newgrf_cargo.h
Show inline comments
 
@@ -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 */
src/newgrf_spritegroup.h
Show inline comments
 
@@ -191,6 +191,9 @@ struct ResolverObject {
 
			Town *town;
 
			HouseID house_id;
 
		} house;
 
		struct {
 
			const struct CargoSpec *cs;
 
		} cargo;
 
	} u;
 

	
 
	uint32 (*GetRandomBits)(const struct ResolverObject*);
src/station_gui.cpp
Show inline comments
 
@@ -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);
src/table/cargo_const.h
Show inline comments
 
@@ -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,
0 comments (0 inline, 0 general)