game_events.hpp

Go to the documentation of this file.
00001 /* $Id: game_events.hpp 53026 2012-02-14 21:24:31Z anonymissimus $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef GAME_EVENTS_H_INCLUDED
00017 #define GAME_EVENTS_H_INCLUDED
00018 
00019 #include "map.hpp"
00020 #include "variable.hpp"
00021 
00022 /**
00023  * @file
00024  * Define the game's events mechanism.
00025  *
00026  * Events might be units moving or fighting, or when victory or defeat occurs.
00027  * A scenario's configuration file will define actions to take when certain events occur.
00028  * This module is responsible for making sure that when the events occur, the actions take place.
00029  *
00030  * Note that game events have nothing to do with SDL events,
00031  * like mouse movement, keyboard events, etc.
00032  * See events.hpp for how they are handled.
00033  */
00034 
00035 class unit;
00036 
00037 /**
00038  * Changes a terrain location.
00039  * Ensures that villages are properly lost and that new terrains are discovered.
00040  */
00041 void change_terrain(const map_location &loc, const t_translation::t_terrain &t,
00042     gamemap::tmerge_mode mode, bool replace_if_failed);
00043 
00044 namespace game_events
00045 {
00046 
00047     // The game event manager loads the scenario configuration object,
00048     // and ensures that events are handled according to the
00049     // scenario configuration for its lifetime.
00050     //
00051     // Thus, a manager object should be created when a scenario is played,
00052     // and destroyed at the end of the scenario.
00053     struct manager {
00054         // Note that references will be maintained,
00055         // and must remain valid for the life of the object.
00056         manager(const config& scenario_cfg);
00057         ~manager();
00058 
00059     private:
00060         variable::manager variable_manager;
00061     };
00062 
00063     struct entity_location : public map_location {
00064         entity_location(const map_location &loc, size_t id = 0);
00065         explicit entity_location(const unit &);
00066         bool requires_unit() const;
00067         bool matches_unit(const unit& u) const;
00068         private:
00069         size_t id_;
00070     };
00071 
00072 
00073     struct queued_event {
00074         queued_event(const std::string& name, const game_events::entity_location& loc1,
00075                 const game_events::entity_location& loc2,
00076                 const config& data)
00077             : name(name), loc1(loc1), loc2(loc2),data(data) {}
00078 
00079         std::string name;
00080         game_events::entity_location loc1;
00081         game_events::entity_location loc2;
00082         config data;
00083     };
00084 
00085     class event_handler
00086     {
00087         public:
00088             event_handler(const config &cfg, bool is_menu_item = false);
00089 
00090             bool matches_name(const std::string& name) const;
00091 
00092             bool disabled() const { return disabled_; }
00093             bool is_menu_item() const { return is_menu_item_; }
00094 
00095             void handle_event(const queued_event& event_info);
00096 
00097             const config &get_config() const { return cfg_; }
00098         private:
00099             bool first_time_only_;
00100             bool disabled_;
00101             bool is_menu_item_;
00102             config cfg_;
00103     };
00104 
00105     /**
00106      * Runs the action handler associated to the command sequence @a cfg.
00107      */
00108     void handle_event_commands(const queued_event &event_info, const vconfig &cfg);
00109 
00110     /**
00111      * Runs the action handler associated to @a cmd with parameters @a cfg.
00112      */
00113     void handle_event_command(const std::string &cmd,
00114         const queued_event &event_info, const vconfig &cfg);
00115 
00116     void write_events(config& cfg);
00117     void add_events(
00118         const config::const_child_itors &cfgs,
00119         const std::string& type = std::string());
00120 
00121     bool unit_matches_filter(const unit &, const vconfig& filter);
00122 
00123     /** Used for [wml_message]. */
00124     void handle_wml_log_message(const config& cfg);
00125     /** Used for [deprecated_message]. */
00126     void handle_deprecated_message(const config& cfg);
00127 
00128     /** Gets a vector of sides from side= attribute in a given config node.
00129      * Promotes consistent behaviour.
00130     */
00131     std::vector<int> get_sides_vector(const vconfig& cfg);
00132 
00133     /**
00134      * Function to fire an event.
00135      *
00136      * Events may have up to two arguments, both of which must be locations.
00137      */
00138     bool fire(const std::string& event,
00139             const entity_location& loc1=map_location::null_location,
00140             const entity_location& loc2=map_location::null_location,
00141             const config& data=config());
00142 
00143     void raise(const std::string& event,
00144             const entity_location& loc1=map_location::null_location,
00145             const entity_location& loc2=map_location::null_location,
00146             const config& data=config());
00147 
00148     bool conditional_passed(const vconfig& cond, bool backwards_compat=true);
00149 
00150     /**
00151      * Handles newly-created handlers. Flushes WML messages and errors.
00152      */
00153     void commit();
00154 
00155     bool pump();
00156 
00157     typedef void (*action_handler)(const game_events::queued_event &, const vconfig &);
00158 
00159 } // end namespace game_events
00160 
00161 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:52 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs