(svn r20601) -Feature: [NewGRF] Add 'DEFA' field to set parameter defaults with action 14

This commit is contained in:
yexo 2010-08-23 21:47:07 +00:00
parent 8ce06a09b9
commit a36159614a
4 changed files with 32 additions and 1 deletions

View File

@ -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()
};

View File

@ -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<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
MemCpyT<uint32>(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)

View File

@ -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<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 @@ struct GRFConfig : ZeroedMemoryAllocator {
const char *GetName() const;
const char *GetDescription() const;
void SetParameterDefaults();
void SetSuitablePalette();
};

View File

@ -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. */