mail[Wesnoth-commits] r44680 - in /trunk: data/lua/wml-tags.lua src/game_events.cpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by guillaume . melquiond on July 25, 2010 - 17:34:
Author: silene
Date: Sun Jul 25 17:34:13 2010
New Revision: 44680

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44680&view=rev
Log:
Moved implementation of [store_unit] to Lua.

Modified:
    trunk/data/lua/wml-tags.lua
    trunk/src/game_events.cpp

Modified: trunk/data/lua/wml-tags.lua
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/lua/wml-tags.lua?rev=44680&r1=44679&r2=44680&view=diff
==============================================================================
--- trunk/data/lua/wml-tags.lua (original)
+++ trunk/data/lua/wml-tags.lua Sun Jul 25 17:34:13 2010
@@ -381,3 +381,34 @@
        local var = cfg.variable or "turns"
        wesnoth.set_variable(var, wesnoth.game_config.last_turn)
 end
+
+function wml_actions.store_unit(cfg)
+       local filter = helper.get_child(cfg, "filter") or
+               helper.wml_error "[store_unit] missing required [filter] tag"
+       local kill_units = cfg.kill
+
+       local var = cfg.variable or "unit"
+       local idx = 0
+       if cfg.mode == "append" then
+               index = wesnoth.get_variable(var .. ".length")
+       else
+               wesnoth.set_variable(var)
+       end
+
+       for i,u in ipairs(wesnoth.get_units(filter)) do
+               wesnoth.set_variable(string.format("%s[%d]", var, idx), 
u.__cfg)
+               idx = idx + 1
+               if kill_units then wesnoth.put_unit(u.x, u.y) end
+       end
+
+       if (not cfg.x or cfg.x == "recall") and (not cfg.y or cfg.y == 
"recall") then
+               for i,u in ipairs(wesnoth.get_recall_units(filter)) do
+                       local ucfg = u.__cfg
+                       ucfg.x = "recall"
+                       ucfg.y = "recall"
+                       wesnoth.set_variable(string.format("%s[%d]", var, 
idx), ucfg)
+                       idx = idx + 1
+                       if kill_units then wesnoth.extract_unit(u) end
+               end
+       end
+end

Modified: trunk/src/game_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.cpp?rev=44680&r1=44679&r2=44680&view=diff
==============================================================================
--- trunk/src/game_events.cpp (original)
+++ trunk/src/game_events.cpp Sun Jul 25 17:34:13 2010
@@ -2108,77 +2108,7 @@
        }
 }
 
-// Unit serialization to and from variables
-/** @todo FIXME: Check that store is automove bug safe */
-WML_HANDLER_FUNCTION(store_unit, /*event_info*/, cfg)
-{
-       const config empty_filter;
-       vconfig filter = cfg.child("filter");
-       if(filter.null()) {
-               filter = empty_filter;
-               lg::wml_error << "[store_unit] missing required [filter] 
tag\n";
-       }
-
-       std::string variable = cfg["variable"];
-       if(variable.empty()) {
-               variable="unit";
-       }
-       const std::string mode = cfg["mode"];
-       config to_store;
-       variable_info varinfo(variable, true, variable_info::TYPE_ARRAY);
-
-       const bool kill_units = utils::string_bool(cfg["kill"]);
-
-       for(unit_map::iterator i = resources::units->begin(); i != 
resources::units->end();) {
-               if (!game_events::unit_matches_filter(*i, filter)) {
-                       ++i;
-                       continue;
-               }
-
-               config& data = to_store.add_child(varinfo.key);
-               i->get_location().write(data);
-               i->write(data);
-
-               if(kill_units) {
-                       resources::units->erase(i++);
-               } else {
-                       ++i;
-               }
-       }
-
-       t_string const& filter_x = filter["x"];
-       t_string const& filter_y = filter["y"];
-       if((filter_x.empty() || filter_x == "recall")
-       && (filter_y.empty() || filter_y == "recall"))
-       {
-               for(std::vector<team>::iterator pi = 
resources::teams->begin();
-                               pi != resources::teams->end(); ++pi) {
-                       std::vector<unit>& avail_units = pi->recall_list();
-                       for(std::vector<unit>::iterator j = 
avail_units.begin(); j != avail_units.end();) {
-                               scoped_recall_unit auto_store("this_unit", 
pi->save_id(), j - avail_units.begin());
-                               if (!j->matches_filter(filter, 
map_location())) {
-                                       ++j;
-                                       continue;
-                               }
-                               config& data = 
to_store.add_child(varinfo.key);
-                               j->write(data);
-                               data["x"] = "recall";
-                               data["y"] = "recall";
-
-                               if(kill_units) {
-                                       j = avail_units.erase(j);
-                               } else {
-                                       ++j;
-                               }
-                       }
-               }
-       }
-       if(mode != "append") {
-               varinfo.vars->clear_children(varinfo.key);
-       }
-       varinfo.vars->append(to_store);
-}
-
+// Unit serialization to variables
 WML_HANDLER_FUNCTION(unstore_unit, /*event_info*/, cfg)
 {
        const config &var = 
resources::state_of_game->get_variable_cfg(cfg["variable"]);




Related Messages


Powered by MHonArc, Updated Sun Jul 25 18:40:13 2010