The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pump.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Define the game's event mechanism.
18  *
19  * Events might be units moving or fighting, or when victory or defeat occurs.
20  * A scenario's configuration file will define actions to take when certain events occur.
21  * This module is responsible for the processing of events.
22  *
23  * Note that game events have nothing to do with SDL events,
24  * like mouse movement, keyboard events, etc.
25  * See events.hpp for how they are handled.
26  */
27 
28 #pragma once
29 
30 #include "game_events/fwd.hpp"
32 #include "game_events/manager.hpp"
33 
34 #include "config.hpp"
35 
36 #include <sstream>
37 #include <string>
38 
39 namespace lg
40 {
41 class logger;
42 }
43 
44 namespace game_events
45 {
47 {
49  const std::string& id,
50  const entity_location& loc1,
51  const entity_location& loc2,
52  const config& data)
53  : name(name)
54  , id(id)
55  , loc1(loc1)
56  , loc2(loc2)
57  , data(data)
58  {
59  std::replace(this->name.begin(), this->name.end(), ' ', '_');
60  }
61 
67 };
68 
69 struct pump_impl;
70 class manager;
71 
73 {
74  const std::unique_ptr<pump_impl> impl_;
75 
76 public:
79 
80  /**
81  * Context: The general environment within which events are processed.
82  * Returns whether or not audoing is impossible due to wml.
83  */
84  bool undo_disabled();
85 
86  /** [allow_undo] implementation */
87  void set_undo_disabled(bool mutated);
88 
89  /**
90  * Returns whether or not wml wants to abort the currently executed user action.
91  */
92  bool action_canceled();
93 
94  /** Sets whether or not wml wants to abort the currently executed user action. */
95  void set_action_canceled();
96 
97  /** Returns whether or not we are skipping messages. */
98  bool context_skip_messages();
99 
100  /** Sets whether or not we are skipping messages. */
101  void context_skip_messages(bool skip);
102 
103  /*
104  * Helper function which determines whether a wml_message text can
105  * really be pushed into the wml_messages_stream, and does it.
106  */
107  void put_wml_message(const std::string& logger, const std::string& message, bool in_chat);
108 
109  /**
110  * Function to fire an event.
111  *
112  * Events may have up to two arguments, both of which must be locations.
113  */
114  pump_result_t fire(const std::string& event,
117  const config& data = config());
118 
119  pump_result_t fire(const std::string& event,
120  const std::string& id,
123  const config& data = config());
124 
125  void raise(const std::string& event,
126  const std::string& id,
129  const config& data = config());
130 
131  inline void raise(const std::string& event,
134  const config& data = config())
135  {
136  raise(event, "", loc1, loc2, data);
137  }
138 
140 
141  /** Flushes WML messages and errors. */
142  void flush_messages();
143 
144  /** This function can be used to detect when no WML/Lua has been executed. */
145  size_t wml_tracking();
146 
147 private:
148  bool filter_event(const event_handler& handler, const queued_event& ev);
149 
150  void process_event(handler_ptr& handler_p, const queued_event& ev);
151 
152  void fill_wml_messages_map(std::map<std::string, int>& msg_map, std::stringstream& source);
153 
154  void show_wml_messages(std::stringstream& source, const std::string& caption, bool to_cerr);
155 
156  void show_wml_errors();
157 
158  void show_wml_messages();
159 
160  void put_wml_message(lg::logger& logger, const std::string& prefix, const std::string& message, bool in_chat);
161 };
162 }
std::vector< char_t > string
entity_location loc2
Definition: pump.hpp:65
wml_event_pump(manager &)
Definition: pump.cpp:650
std::string name
Definition: pump.hpp:62
bool action_canceled()
Returns whether or not wml wants to abort the currently executed user action.
Definition: pump.cpp:449
Definitions for the interface to Wesnoth Markup Language (WML).
size_t wml_tracking()
This function can be used to detect when no WML/Lua has been executed.
Definition: pump.cpp:645
static const entity_location null_entity
void show_wml_messages()
Shows a summary of the messages generated so far by WML.
Definition: pump.cpp:400
void flush_messages()
Flushes WML messages and errors.
Definition: pump.cpp:621
void show_wml_errors()
Shows a summary of the errors encountered in WML so far, to avoid a lot of the same messages to be sh...
Definition: pump.cpp:386
Instruction that causes us to skip upcoming instructions.
queued_event(const std::string &name, const std::string &id, const entity_location &loc1, const entity_location &loc2, const config &data)
Definition: pump.hpp:48
Domain specific events.
Definition: action_wml.cpp:88
Definition: pump.hpp:39
void put_wml_message(const std::string &logger, const std::string &message, bool in_chat)
Helper function which determines whether a wml_message text can really be pushed into the wml_message...
Definition: pump.cpp:478
bool undo_disabled()
Context: The general environment within which events are processed.
Definition: pump.cpp:437
void set_action_canceled()
Sets whether or not wml wants to abort the currently executed user action.
Definition: pump.cpp:455
pump_result_t operator()()
Definition: pump.cpp:522
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:43
void process_event(handler_ptr &handler_p, const queued_event &ev)
Processes an event through a single event handler.
Definition: pump.cpp:280
pump_result_t fire(const std::string &event, const entity_location &loc1=entity_location::null_entity, const entity_location &loc2=entity_location::null_entity, const config &data=config())
Function to fire an event.
Definition: pump.cpp:491
entity_location loc1
Definition: pump.hpp:64
const std::unique_ptr< pump_impl > impl_
Definition: pump.hpp:74
bool filter_event(const event_handler &handler, const queued_event &ev)
Returns true iff the given event passes all its filters.
Definition: pump.cpp:196
void set_undo_disabled(bool mutated)
[allow_undo] implementation
Definition: pump.cpp:443
void fill_wml_messages_map(std::map< std::string, int > &msg_map, std::stringstream &source)
Helper function for show_wml_messages(), which gathers the messages from a stringstream.
Definition: pump.cpp:320
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:24
std::tuple< bool, bool > pump_result_t
Definition: fwd.hpp:28
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
Define locations as used by the game's events mechanism.
bool context_skip_messages()
Returns whether or not we are skipping messages.
Definition: pump.cpp:462