28 #define DBG_NG LOG_STREAM(debug, log_engine)
29 #define LOG_NG LOG_STREAM(info, log_engine)
30 #define WRN_NG LOG_STREAM(warn, log_engine)
33 #define LOG_EH LOG_STREAM(info, log_event_handler)
34 #define DBG_EH LOG_STREAM(debug, log_event_handler)
39 class event_handler_list_lock
42 event_handler_list_lock()
47 ~event_handler_list_lock()
54 return num_locks_ == 0u;
57 static unsigned int num_locks_;
60 unsigned int event_handler_list_lock::num_locks_ = 0u;
71 !handler[
"first_time_only"].to_bool(
true),
72 handler[
"priority"].to_double(0.),
75 if(new_handler.valid()) {
76 new_handler->read_filters(handler);
81 if(attr ==
"id" || attr ==
"name" || attr ==
"first_time_only" || attr ==
"priority" || attr.compare(0, 6,
"filter") == 0) {
87 if(key.compare(0, 6,
"filter") != 0) {
91 new_handler->set_arguments(args);
92 new_handler->register_wml_event(lk);
93 DBG_EH <<
"Registered WML event "
94 << (new_handler->names_raw().empty() ?
"" :
"'" + new_handler->names_raw() +
"'")
95 << (new_handler->id().empty() ?
"" :
"{id=" + new_handler->id() +
"}")
96 << (new_handler->repeatable() ?
" (repeating" :
" (first time only")
97 <<
"; priority " + std::to_string(new_handler->priority())
98 << (is_menu_item ?
"; menu item)" :
")")
99 <<
" with the following actions:\n"
102 LOG_EH <<
"Content of failed event:\n" << handler.
debug();
108 return event_handlers_->add_event_handler(name,
id, repeat, priority, is_menu_item);
139 for(
const std::string&
id :
utils::split(scenario_cfg[
"unit_wml_ids"])) {
163 for(
const config& new_ev : cfgs) {
164 if(
type.empty() && new_ev[
"id"].empty()) {
165 WRN_NG <<
"attempt to add an [event] with empty id= from [unit], ignoring ";
176 if(!eh || eh->is_menu_item()) {
191 assert(!eh->disabled());
195 eh->write_config(event_cfg, include_nonserializable);
196 if(!event_cfg.
empty()) {
197 cfg.
add_child(
"event", std::move(event_cfg));
214 event_handler_list_lock lock;
216 for (
unsigned i = 0;
i < active_handlers.size(); ++
i) {
220 handler = active_handlers.at(
i);
222 catch (
const std::out_of_range&) {
227 if (!handler || handler->disabled()) {
232 for(
const std::string& name : handler->names(gd)) {
233 if(standardized_event_id == name) {
234 func(*
this, handler);
242 if(event_handler_list_lock::none()) {
250 return !event_handler_list_lock::none();
A config object defines a single node in a WML file, with access to child nodes.
const_attr_itors attribute_range() const
auto all_children_view() const
In-order iteration over all children.
child_itors child_range(config_key_type key)
std::string debug() const
boost::iterator_range< const_child_iterator > const_child_itors
config & add_child(config_key_type key)
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
void execute_on_events(const std::string &event_id, const event_func_t &func)
const std::unique_ptr< game_events::wml_event_pump > pump_
game_events::wml_event_pump & pump()
void write_events(config &cfg, bool include_nonserializable=false) const
void remove_event_handler(const std::string &id)
Removes an event handler.
void read_scenario(const config &scenario_cfg, game_lua_kernel &lk)
void add_event_handler_from_wml(const config &handler, game_lua_kernel &lk, bool is_menu_item=false)
Create an event handler from an [event] tag.
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type=std::string())
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
bool is_event_running() const
const std::unique_ptr< event_handlers > event_handlers_
game_events::wmi_manager wml_menu_items_
pending_event_handler add_event_handler_from_lua(const std::string &name, const std::string &id, bool repeat=false, double priority=0., bool is_menu_item=false)
Create an empty event handler.
std::set< std::string > unit_wml_ids_
Represents a handler that is about to be added to the events manager but is still waiting for some da...
void init_handlers(game_lua_kernel &lk) const
Initializes the implicit event handlers for inlined [command]s.
void to_config(config &cfg) const
void set_menu_items(const config &cfg)
Sets the current menu items to the "menu_item" children of cfg.
static lg::log_domain log_engine("engine")
static lg::log_domain log_event_handler("event_handler")
Define the handlers for the game's events mechanism.
Standard logging facilities (interface).
std::shared_ptr< event_handler > handler_ptr
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Define the game's event mechanism.