27 #include "formula/callable_objects.hpp" 42 #define DBG_NG LOG_STREAM(debug, log_engine) 43 #define LOG_NG LOG_STREAM(info, log_engine) 44 #define WRN_NG LOG_STREAM(warn, log_engine) 47 #define DBG_EH LOG_STREAM(debug, log_event_handler) 55 : first_time_only_(true)
56 , is_menu_item_(false)
70 std::vector<std::string> standardized_names;
81 for(
const std::string& subname :
utils::split(single_name)) {
86 return standardized_names;
96 assert(!
disabled_ &&
"Trying to disable a disabled event. Shouldn't happen!");
107 DBG_NG <<
"menu item " <<
id_ <<
" will now invoke the following command(s):\n" <<
args_;
120 return std::all_of(
filters_.begin(),
filters_.end(), [&ev](
const auto& filter) {
121 return (*filter)(ev);
128 WRN_NG <<
"Tried to serialize disabled event, skipping";
131 static const char* log_append_preload =
" - this will not break saves since it was registered during or before preload\n";
132 static const char* log_append_postload =
" - this will break saves because it was registered after preload\n";
134 if(include_nonserializable) {
135 cfg[
"nonserializable"] =
true;
136 cfg.
add_child(
"lua")[
"code"] =
"<function>";
138 static const char* log =
"Skipping serialization of an event with action bound to Lua code";
140 WRN_NG << log << log_append_postload;
143 LOG_NG << log << log_append_preload;
149 if(include_nonserializable) {
150 cfg[
"nonserializable"] =
true;
152 static const char* log =
"Skipping serialization of an event with filter bound to Lua code";
154 WRN_NG << log << log_append_postload;
157 LOG_NG << log << log_append_preload;
163 if(!
id_.empty()) cfg[
"id"] =
id_;
165 for(
const auto& filter :
filters_) {
166 filter->serialize(cfg);
173 WRN_NG <<
"Tried to serialize an event with a filter that cannot be serialized!";
189 cfg.
add_child(
"filter_condition", cfg_.get_config());
207 cfg.
add_child(
"filter_side", ssf_.get_config());
221 const auto& loc = first_ ? event_info.
loc1 : event_info.
loc2;
223 return loc.matches_unit_filter(
unit, suf_);
227 cfg.
add_child(first_ ?
"filter" :
"filter_second", suf_.to_config());
243 const auto& loc = first_ ? event_info.
loc1 : event_info.
loc2;
245 if(
unit != units.
end() && loc.matches_unit(
unit)) {
246 const config& attack = event_info.
data.
child(first_ ?
"first" :
"second");
253 cfg.
add_child(first_ ?
"filter_attack" :
"filter_second_attack", swf_.get_config());
271 return formula_.evaluate(data).as_bool();
275 std::string code = formula_.str();
278 code =
"(" + cfg[
"filter_formula"].str() +
") and (" + code +
")";
280 cfg[
"filter_formula"] = code;
292 if(key ==
"filter_condition") {
293 return std::make_unique<filter_condition>(contents);
294 }
else if(key ==
"filter_side") {
295 return std::make_unique<filter_side>(contents);
296 }
else if(key ==
"filter") {
297 return std::make_unique<filter_unit>(contents,
true);
298 }
else if(key ==
"filter_attack") {
299 return std::make_unique<filter_attack>(contents,
true);
300 }
else if(key ==
"filter_second") {
301 return std::make_unique<filter_unit>(contents,
false);
302 }
else if(key ==
"filter_second_attack") {
303 return std::make_unique<filter_attack>(contents,
false);
313 filter_dynamic(
const std::string& tag,
const std::string& var) : tag_(tag), var_(var) {}
319 return (*filter)(event_info);
327 tag[
"variable"] = var_;
341 if(
auto filter_ptr =
make_filter(filter.key, vcfg)) {
344 add_filter(std::make_unique<filter_dynamic>(vcfg[
"name"], vcfg[
"variable"]));
348 add_filter(std::make_unique<filter_formula>(cfg[
"filter_formula"]));
354 filters_.push_back(std::move(filter));
play_controller * controller
static std::unique_ptr< event_filter > make_filter(const std::string &key, const vconfig &contents)
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
const_all_children_itors all_children_range() const
In-order iteration over all children.
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
filter_condition(const vconfig &cfg)
virtual const unit_map & units() const override
This class represents a single unit of a specific type.
void append(const config &cfg)
Append data from another config object to this one.
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
bool is_lua_
Tracks whether the event was registered from the Lua API.
bool run_wml_event(int ref, const vconfig &args, const game_events::queued_event &ev, bool *out=nullptr)
Run a WML stored in the Lua registry.
bool has_attribute(config_key_type key) const
filter_attack(const vconfig &cfg, bool first)
event_handler(const std::string &types, const std::string &id="")
bool operator()(const queued_event &event_info) const override
Runs the filter and returns whether it passes on the given event.
maybe_const_t< config, V > & as_container() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
virtual void serialize(config &cfg) const
Serializes the filter into a config, if possible.
void handle_event(const queued_event &event_info, game_lua_kernel &lk)
Handles the queued event, according to our WML instructions.
bool can_serialize() const override
Returns true if it is possible to serialize the filter into a config.
int save_wml_event()
Store a WML event in the Lua registry, as a function.
bool filter_event(const queued_event &event_info) const
void serialize(config &cfg) const override
Serializes the filter into a config, if possible.
bool has_preloaded_
Tracks whether the event was registered before or after the Lua preload event fired.
virtual bool can_serialize() const
Returns true if it is possible to serialize the filter into a config.
std::vector< std::shared_ptr< event_filter > > filters_
This is a dynamic wrapper for any filter type, specified via [insert_tag].
void read_filters(const config &cfg)
void serialize(config &cfg) const override
Serializes the filter into a config, if possible.
bool can_serialize() const override
Returns true if it is possible to serialize the filter into a config.
void register_wml_event(game_lua_kernel &lk)
bool operator()(const queued_event &) const override
Runs the filter and returns whether it passes on the given event.
static lg::log_domain log_engine("engine")
bool operator()(const queued_event &event_info) const override
Runs the filter and returns whether it passes on the given event.
void set_event_ref(int idx, bool has_preloaded)
filter_unit(const vconfig &cfg, bool first)
void disable()
Flag this handler as disabled.
filter_dynamic(const std::string &tag, const std::string &var)
unit_iterator find(std::size_t id)
Define conditionals for the game's events mechanism, a.k.a.
void write_config(config &cfg, bool include_nonserializable=false) const
An object representing the state of the game, providing access to the map and basic information...
void serialize(config &cfg) const override
Serializes the filter into a config, if possible.
bool matches_special_filter(const config &cfg, const vconfig &filter)
bool can_serialize() const override
Returns true if it is possible to serialize the filter into a config.
static lg::log_domain log_event_handler("event_handler")
Define the game's event mechanism.
bool operator()(const queued_event &event_info) const override
Runs the filter and returns whether it passes on the given event.
bool operator()(const queued_event &) const override
Runs the filter and returns whether it passes on the given event.
Represents a single filter condition on an event.
bool can_serialize() const override
Returns true if it is possible to serialize the filter into a config.
config & add_child(config_key_type key)
void serialize(config &cfg) const override
Serializes the filter into a config, if possible.
bool might_contain_variables(const std::string &str)
Determines if a string might contain variables to interpolate.
bool conditional_passed(const vconfig &cond)
std::vector< std::string > names(const variable_set *vars) const
Define the handlers for the game's events mechanism.
Information on a WML variable.
An object representing the state of the current event; equivalent to Lua's wesnoth.current.event_context.
bool exists_as_container() const
std::vector< std::string > split(const config_attribute_value &val)
A variable-expanding proxy for the config class.
Standard logging facilities (interface).
Container associating units to locations.
static vconfig empty_vconfig()
void commit_music_changes()
void add_filter(std::unique_ptr< event_filter > &&filter)
A config object defines a single node in a WML file, with access to child nodes.
filter_side(const vconfig &cfg)
bool can_serialize() const override
Returns true if it is possible to serialize the filter into a config.
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
void serialize(config &cfg) const override
Serializes the filter into a config, if possible.