From a36159614a20c19b78b524e412a69b8a6f1abaa2 Mon Sep 17 00:00:00 2001 From: yexo Date: Mon, 23 Aug 2010 21:47:07 +0000 Subject: (svn r20601) -Feature: [NewGRF] Add 'DEFA' field to set parameter defaults with action 14 --- src/newgrf.cpp | 13 +++++++++++++ src/newgrf_config.cpp | 16 +++++++++++++++- src/newgrf_config.h | 3 +++ src/newgrf_gui.cpp | 1 + 4 files changed, 32 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 7a636ae73..30df408b2 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -6183,6 +6183,18 @@ static bool ChangeGRFParamMask(size_t len, ByteReader *buf) 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 _tags_parameters[] = { AllowedSubtags('LIMI', ChangeGRFParamLimits), AllowedSubtags('MASK', ChangeGRFParamMask), AllowedSubtags('VALU', ChangeGRFParamValueNames), + AllowedSubtags('DEFA', ChangeGRFParamDefault), AllowedSubtags() }; diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index b12c688aa..01521cd54 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -46,7 +46,8 @@ GRFConfig::GRFConfig(const GRFConfig &config) : 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(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum)); MemCpyT(this->param, config.param, lengthof(this->param)); @@ -97,6 +98,17 @@ const char *GRFConfig::GetDescription() const 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 @@ GRFParameterInfo::GRFParameterInfo(uint nr) : 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 @@ GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) : 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) diff --git a/src/newgrf_config.h b/src/newgrf_config.h index 69c219809..e8b6bc86a 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -119,6 +119,7 @@ struct GRFParameterInfo { 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 @@ struct GRFConfig : ZeroedMemoryAllocator { uint8 num_valid_params; ///< NOSAVE: Number of valid parameters (action 0x14) uint8 palette; ///< GRFPalette, bitset SmallVector 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 @@ struct GRFConfig : ZeroedMemoryAllocator { const char *GetName() const; const char *GetDescription() const; + void SetParameterDefaults(); void SetSuitablePalette(); }; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 7ba86b6e9..8bfc02843 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -803,6 +803,7 @@ struct NewGRFWindow : public QueryStringBaseWindow { } 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. */ -- cgit v1.2.3-70-g09d2