changeset 15917:052cfee737ea draft

(svn r20601) -Feature: [NewGRF] Add 'DEFA' field to set parameter defaults with action 14
author yexo <yexo@openttd.org>
date Mon, 23 Aug 2010 21:47:07 +0000
parents f13c23f052f1
children cb631c63789f
files src/newgrf.cpp src/newgrf_config.cpp src/newgrf_config.h src/newgrf_gui.cpp
diffstat 4 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -6183,6 +6183,18 @@
 	return true;
 }
 
+/** Callback function for 'INFO'->'PARAM'->param_num->'DEFA' to set the default value. */
+static bool ChangeGRFParamDefault(size_t len, ByteReader *buf)
+{
+	if (len != 4) {
+		grfmsg(2, "StaticGRFInfo: expected 4 bytes for 'INFO'->'PARA'->'DEFA' but got " PRINTF_SIZE ", ignoring this field", len);
+		buf->Skip(len);
+	} else {
+		_cur_parameter->def_value = buf->ReadDWord();
+	}
+	_cur_grfconfig->has_param_defaults = true;
+	return true;
+}
 
 typedef bool (*DataHandler)(size_t, ByteReader *);  ///< Type of callback function for binary nodes
 typedef bool (*TextHandler)(byte, const char *str); ///< Type of callback function for text nodes
@@ -6310,6 +6322,7 @@
 	AllowedSubtags('LIMI', ChangeGRFParamLimits),
 	AllowedSubtags('MASK', ChangeGRFParamMask),
 	AllowedSubtags('VALU', ChangeGRFParamValueNames),
+	AllowedSubtags('DEFA', ChangeGRFParamDefault),
 	AllowedSubtags()
 };
 
--- a/src/newgrf_config.cpp
+++ b/src/newgrf_config.cpp
@@ -46,7 +46,8 @@
 	grf_bugs(config.grf_bugs),
 	num_params(config.num_params),
 	num_valid_params(config.num_valid_params),
-	palette(config.palette)
+	palette(config.palette),
+	has_param_defaults(config.has_param_defaults)
 {
 	MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
 	MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
@@ -97,6 +98,17 @@
 	return GetGRFStringFromGRFText(this->info);
 }
 
+/** Set the default value for all parameters as specified by action14. */
+void GRFConfig::SetParameterDefaults()
+{
+	if (!this->has_param_defaults) return;
+
+	for (uint i = 0; i < this->param_info.Length(); i++) {
+		if (this->param_info[i] == NULL) continue;
+		this->param_info[i]->SetValue(this, this->param_info[i]->def_value);
+	}
+}
+
 /**
  * Set the palette of this GRFConfig to something suitable.
  * That is either the setting coming from the NewGRF or
@@ -162,6 +174,7 @@
 	type(PTYPE_UINT_ENUM),
 	min_value(0),
 	max_value(UINT32_MAX),
+	def_value(0),
 	param_nr(nr),
 	first_bit(0),
 	num_bit(32)
@@ -178,6 +191,7 @@
 	type(info.type),
 	min_value(info.min_value),
 	max_value(info.max_value),
+	def_value(info.def_value),
 	param_nr(info.param_nr),
 	first_bit(info.first_bit),
 	num_bit(info.num_bit)
--- a/src/newgrf_config.h
+++ b/src/newgrf_config.h
@@ -119,6 +119,7 @@
 	GRFParameterType type; ///< The type of this parameter
 	uint32 min_value;      ///< The minimal value this parameter can have
 	uint32 max_value;      ///< The maximal value of this parameter
+	uint32 def_value;      ///< Default value of this parameter
 	byte param_nr;         ///< GRF parameter to store content in
 	byte first_bit;        ///< First bit to use in the GRF parameter
 	byte num_bit;          ///< Number of bits to use for this parameter
@@ -150,6 +151,7 @@
 	uint8 num_valid_params;    ///< NOSAVE: Number of valid parameters (action 0x14)
 	uint8 palette;             ///< GRFPalette, bitset
 	SmallVector<GRFParameterInfo *, 4> param_info; ///< NOSAVE: extra information about the parameters
+	bool has_param_defaults;   ///< NOSAVE: did this newgrf specify any defaults for it's parameters
 
 	struct GRFConfig *next;    ///< NOSAVE: Next item in the linked list
 
@@ -158,6 +160,7 @@
 	const char *GetName() const;
 	const char *GetDescription() const;
 
+	void SetParameterDefaults();
 	void SetSuitablePalette();
 };
 
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -803,6 +803,7 @@
 				}
 
 				GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
+				c->SetParameterDefaults();
 				*list = c; // Append GRF config to configuration list.
 
 				/* Select next (or previous, if last one) item in the list. */