Author: shadowmaster
Date: Sun Jul 20 22:15:56 2008
New Revision: 28108

URL: http://svn.gna.org/viewcvs/wesnoth?rev=28108&view=rev
Log:
* Make it possible to define and remove [time_area]s dinamically. For
* removing them, associating them with an id. is required.

Modified:
    trunk/src/game_events.cpp
    trunk/src/gamestatus.cpp
    trunk/src/gamestatus.hpp

Modified: trunk/src/game_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.cpp?rev=28108&r1=28107&r2=28108&view=diff
==============================================================================
--- trunk/src/game_events.cpp (original)
+++ trunk/src/game_events.cpp Sun Jul 20 22:15:56 2008
@@ -3028,6 +3028,18 @@
                                variable_name="input";
                        state_of_game->set_variable(variable_name, 
text_input_result);
                }
+       }
+
+       // Adding/removing new time_areas dinamically
+       WML_HANDLER_FUNCTION(time_area,/*handler*/,/*event_info*/,cfg)
+       {
+               assert(state_of_game != NULL);
+               assert(status_ptr != NULL);
+               const bool remove = utils::string_bool(cfg["remove"],false);
+               if(remove)
+                       status_ptr->remove_time_area(cfg["id"]);
+               else
+                       status_ptr->add_time_area(cfg.get_parsed_config());
        }
 
                // Adding of new events
@@ -3116,6 +3128,7 @@
                function_call_map.insert(std::make_pair("while", 
&wml_func_while));
                function_call_map.insert(std::make_pair("switch", 
&wml_func_switch));
                function_call_map.insert(std::make_pair("message", 
&wml_func_message));
+               function_call_map.insert(std::make_pair("time_area", 
&wml_func_time_area));
                function_call_map.insert(std::make_pair("event", 
&wml_func_event));
        }
 #endif

Modified: trunk/src/gamestatus.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gamestatus.cpp?rev=28108&r1=28107&r2=28108&view=diff
==============================================================================
--- trunk/src/gamestatus.cpp (original)
+++ trunk/src/gamestatus.cpp Sun Jul 20 22:15:56 2008
@@ -143,6 +143,33 @@
 }
 #endif
 
+void gamestatus::add_time_area(const config& cfg)
+{
+       const std::vector<gamemap::location> locs = 
parse_location_range(cfg["x"],cfg["y"]);
+       area_time_of_day area;
+       area.xsrc = cfg["x"];
+       area.ysrc = cfg["y"];
+       area.id   = cfg["id"];
+       
std::copy(locs.begin(),locs.end(),std::inserter(area.hexes,area.hexes.end()));
+       time_of_day::parse_times(cfg,area.times);
+       areas_.push_back(area);
+}
+
+void gamestatus::remove_time_area(const std::string& area_id)
+{
+       if(area_id.empty()) {
+               areas_.clear();
+       } else {
+               // search for all time areas that match the id.
+               std::vector<area_time_of_day>::iterator i = areas_.begin();
+               while(i != areas_.end()) {
+                       if((*i).id == area_id)
+                               areas_.erase(i);
+                       else ++i;
+               }
+       }
+}
+
 //! Reads turns and time information from parameters.
 //! It sets random starting ToD and current_tod to config.
 gamestatus::gamestatus(const config& time_cfg, int num_turns, game_state* 
s_o_g) :
@@ -173,15 +200,8 @@
        set_start_ToD(const_cast<config&>(time_cfg),s_o_g);
 
        const config::child_list& times_range = 
time_cfg.get_children("time_area");
-       for(config::child_list::const_iterator t = times_range.begin(); t != 
times_range.end(); ++t) {
-               const std::vector<gamemap::location> locs = 
parse_location_range((**t)["x"],(**t)["y"]);
-               area_time_of_day area;
-               area.xsrc = (**t)["x"];
-               area.ysrc = (**t)["y"];
-               
std::copy(locs.begin(),locs.end(),std::inserter(area.hexes,area.hexes.end()));
-               time_of_day::parse_times(**t,area.times);
-               areas_.push_back(area);
-       }
+       for(config::child_list::const_iterator t = times_range.begin(); t != 
times_range.end(); ++t)
+               this->add_time_area(**t);
 }
 
 void gamestatus::write(config& cfg) const

Modified: trunk/src/gamestatus.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gamestatus.hpp?rev=28108&r1=28107&r2=28108&view=diff
==============================================================================
--- trunk/src/gamestatus.hpp (original)
+++ trunk/src/gamestatus.hpp Sun Jul 20 22:15:56 2008
@@ -200,6 +200,18 @@
        bool next_turn();
 
        static bool is_start_ToD(const std::string&);
+       
+       //! Adds a new local time area from config, making it follow its own
+       //! time-of-day sequence.
+       //! @param cfg Config object containing x,y range/list of locations,
+       //!            and desired [time]/[illuminated_time] information.
+       void add_time_area(const config& cfg);
+       
+       //! Removes a time area from config, making it follow the scenario's
+       //! normal time-of-day sequence.
+       //! @param id Identifier of time_area to remove. Supply an empty one
+       //!           to remove all local time areas.
+       void remove_time_area(const std::string& id);
 
        //! @todo FIXME: since gamestatus may be constructed with NULL 
game_state* (by default),
        //! you should not rely on this function to return the current 
game_state.
@@ -218,11 +230,13 @@
                area_time_of_day() :
                        xsrc(),
                        ysrc(),
+                       id(),
                        times(),
                        hexes()
                        {}
 
                std::string xsrc, ysrc;
+               std::string id;
                std::vector<time_of_day> times;
                std::set<gamemap::location> hexes;
        };


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to