add enable_if= to mod and era events

http://gna.org/bugs/?23721
enable_if = in [event] contains a condition that is evalulated before
the scenario is startd (specially before the [replay_start] snapshot is
taken) to check whether the event should be added to the scenario.
[event]
enable_if="$($scenario_num -1 > -1)"
[/event]
will for example add the event only if it's not the first scenario (assuming
wml sets the variable) 'scenario_num' to the current scenario number.
This commit is contained in:
gfgtdf 2015-10-05 22:12:48 +00:00
parent 11d6c3f199
commit c9e23dc733

View File

@ -44,6 +44,9 @@
#include "statistics.hpp"
#include "serialization/binary_or_text.hpp"
#include "util.hpp"
#include "variable_info.hpp"
#include "formula_string_utils.hpp"
#include "config.hpp" //Also for variable_set
#include <boost/assign/list_of.hpp>
#include <boost/range/adaptors.hpp>
@ -205,26 +208,53 @@ void saved_game::expand_scenario()
}
}
}
//helper objects for saved_game::expand_mp_events()
struct modevents_entry
namespace
{
modevents_entry(const std::string& _type, const std::string& _id) : type(_type), id(_id) {}
std::string type;
std::string id;
};
struct modevents_entry_for
{
//this typedef is used by boost.
typedef modevents_entry result_type;
modevents_entry_for(const std::string& type ) : type_(type) {}
modevents_entry operator()(const std::string& id) const
struct config_variable_set : public variable_set
{
return modevents_entry(type_, id);
const config& cfg_;
config_variable_set(const config& cfg) : cfg_(cfg) {}
virtual config::attribute_value get_variable_const(const std::string &id) const
{
try
{
variable_access_const variable(id, cfg_);
return variable.as_scalar();
}
catch(const invalid_variablename_exception&)
{
ERR_NG << "invalid variablename " << id << "\n";
return config::attribute_value();
}
};
};
bool variable_to_bool(const config& vars, const std::string& expression)
{
std::string res = utils::interpolate_variables_into_string(expression, config_variable_set(vars));
return res == "true" || res == "yes" || res == "1";
}
private:
std::string type_;
};
//helper objects for saved_game::expand_mp_events()
struct modevents_entry
{
modevents_entry(const std::string& _type, const std::string& _id) : type(_type), id(_id) {}
std::string type;
std::string id;
};
struct modevents_entry_for
{
//this typedef is used by boost.
typedef modevents_entry result_type;
modevents_entry_for(const std::string& type ) : type_(type) {}
modevents_entry operator()(const std::string& id) const
{
return modevents_entry(type_, id);
}
private:
std::string type_;
};
}
// Gets the ids of the mp_era and modifications which were set to be active, then fetches these configs from the game_config and copies their [event] and [lua] to the starting_pos_.
// At this time, also collect the addon_id attributes which appeared in them and put this list in the addon_ids attribute of the mp_settings.
@ -258,7 +288,10 @@ void saved_game::expand_mp_events()
// Copy events
BOOST_FOREACH(const config& modevent, cfg.child_range("event"))
{
this->starting_pos_.add_child("event", modevent);
if(modevent["enable_if"].empty() || variable_to_bool(carryover_.child_or_empty("variables"), modevent["enable_if"]))
{
this->starting_pos_.add_child("event", modevent);
}
}
// Copy lua
BOOST_FOREACH(const config& modlua, cfg.child_range("lua"))