Author: crab
Date: Sun May  8 13:44:30 2011
New Revision: 49423

URL: http://svn.gna.org/viewcvs/wesnoth?rev=49423&view=rev
Log:
commit second part of patch #2637 by anonymissimus - unified style of side= and 
[filter_side] for lua wml tags]

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

Modified: trunk/data/lua/wml-tags.lua
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/lua/wml-tags.lua?rev=49423&r1=49422&r2=49423&view=diff
==============================================================================
--- trunk/data/lua/wml-tags.lua (original)
+++ trunk/data/lua/wml-tags.lua Sun May  8 13:44:30 2011
@@ -71,13 +71,10 @@
 end
 
 function wml_actions.gold(cfg)
-       local side_list = cfg.side or 1
        local amount = tonumber(cfg.amount) or
                helper.wml_error "[gold] missing required amount= attribute."
-       side_list = side_list .. ","
-       for v in string.gmatch(side_list, "(%w+),") do
-               local side = wesnoth.get_side(tonumber(v))
-               side.gold = side.gold + amount
+       for side_number, team in ipairs(wesnoth.get_sides(nil, cfg)) do
+               team.gold = team.gold + amount
        end
 end
 
@@ -137,8 +134,7 @@
 end
 
 function wml_actions.disallow_recruit(cfg)
-       for side in string.gmatch(cfg.side or 1, "[^%s,][^,]*") do
-               local team = get_team(side, "[disallow_recruit]")
+       for side_number, team in ipairs(wesnoth.get_sides(nil, cfg)) do
                local v = team.recruit
                for w in string.gmatch(cfg.type, "[^%s,][^,]*") do
                        for i, r in ipairs(v) do
@@ -153,8 +149,7 @@
 end
 
 function wml_actions.set_recruit(cfg)
-       for side in string.gmatch(cfg.side or 1, "[^%s,][^,]*") do
-               local team = get_team(side, "[set_recruit]")
+       for side_number, team in ipairs(wesnoth.get_sides(nil, cfg)) do
                local v = {}
                local recruit = cfg.recruit or helper.wml_error("[set_recruit] 
missing required recruit= attribute")
                for w in string.gmatch(recruit, "[^%s,][^,]*") do

Modified: trunk/src/game_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.cpp?rev=49423&r1=49422&r2=49423&view=diff
==============================================================================
--- trunk/src/game_events.cpp (original)
+++ trunk/src/game_events.cpp Sun May  8 13:44:30 2011
@@ -512,6 +512,32 @@
                lg::wml_error << message << '\n';
        }
 
+       std::set<int> get_sides_set(const vconfig& cfg, const bool only_ssf)
+       {
+               std::set<int> result;
+
+               if(only_ssf) {
+                       side_filter filter(cfg);
+                       return filter.get_teams();
+               }
+
+               const config::attribute_value sides = cfg["side"];
+               const vconfig &ssf = cfg.child("filter_side");
+
+               if (!ssf.null()) {
+                       side_filter filter(ssf,sides.str());
+                       return filter.get_teams();
+               }
+
+               if (sides.blank()) {
+                       put_wml_message("error","empty side= and no 
[filter_side] is deprecated");
+                       result.insert(1); // we make sure the set is not empty 
and the current default is maintained
+                       return result;
+               }
+               side_filter filter(sides.str());
+               return filter.get_teams();
+       }
+
 } // end namespace game_events (1)
 
 namespace {
@@ -543,32 +569,9 @@
 
 } // end anonymous namespace (4)
 
-/** Gets a set of sides from side= attribute in a given config node.
-    Promotes consistent behaviour and returns always non-empty set with valid 
teams.
-    Default side, when in doubt is currently side 1. */
-static std::set<int> get_sides_set(const vconfig& cfg)
-{
-       std::set<int> result;
-       const config::attribute_value sides = cfg["side"];
-       const vconfig &ssf = cfg.child("filter_side");
-
-       if (!ssf.null()) {
-               side_filter filter(ssf,sides.str());
-               return filter.get_teams();
-       }
-
-       if (sides.blank()) {
-               put_wml_message("error","empty side= and no [filter_side] is 
deprecated");
-               result.insert(1); // we make sure the set is not empty and the 
current default is maintained
-               return result;
-       }
-       side_filter filter(sides.str());
-       return filter.get_teams();
-}
-
 static void toggle_shroud(const bool remove, const vconfig& cfg)
 {
-       std::set<int> sides = get_sides_set(cfg);
+       std::set<int> sides = game_events::get_sides_set(cfg);
        size_t index;
 
        foreach (const int &side_num, sides)
@@ -684,7 +687,7 @@
 
 WML_HANDLER_FUNCTION(allow_recruit, /*event_info*/, cfg)
 {
-       std::set<int> sides = get_sides_set(cfg);
+       std::set<int> sides = game_events::get_sides_set(cfg);
 
        foreach(const int &side_num, sides)
        {
@@ -784,7 +787,7 @@
 
 WML_HANDLER_FUNCTION(modify_ai, /*event_info*/, cfg)
 {
-       std::set<int> sides = get_sides_set(cfg);
+       std::set<int> sides = game_events::get_sides_set(cfg);
        foreach (const int &side_num, sides)
        {
                
ai::manager::modify_active_ai_for_side(side_num,cfg.get_parsed_config());
@@ -809,7 +812,7 @@
         */
        std::string switch_ai = cfg["switch_ai"];
 
-       std::set<int> sides = get_sides_set(cfg);
+       std::set<int> sides = game_events::get_sides_set(cfg);
        size_t team_index;
 
        foreach (const int &side_num, sides)

Modified: trunk/src/game_events.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.hpp?rev=49423&r1=49422&r2=49423&view=diff
==============================================================================
--- trunk/src/game_events.hpp (original)
+++ trunk/src/game_events.hpp Sun May  8 13:44:30 2011
@@ -123,6 +123,11 @@
        /** Used for [deprecated_message]. */
        void handle_deprecated_message(const config& cfg);
 
+       /** Gets a set of sides from side= attribute in a given config node.
+    Promotes consistent behaviour and returns always non-empty set with valid 
teams.
+    Default side, when in doubt is currently side 1. */
+       std::set<int> get_sides_set(const vconfig& cfg, const bool only_ssf = 
false);
+
        /**
         * Function to fire an event.
         *

Modified: trunk/src/scripting/lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.cpp?rev=49423&r1=49422&r2=49423&view=diff
==============================================================================
--- trunk/src/scripting/lua.cpp (original)
+++ trunk/src/scripting/lua.cpp Sun May  8 13:44:30 2011
@@ -2903,6 +2903,46 @@
        return 1;
 }
 
+static int intf_get_sides(lua_State* L)
+{
+       std::set<int> sides;
+       if(!lua_isnoneornil(L, 2)) {
+               const vconfig ssf_with_filter_tag = luaW_checkvconfig(L, 2);
+               sides = game_events::get_sides_set(ssf_with_filter_tag);
+       }
+       else {
+               const vconfig ssf = luaW_checkvconfig(L, 1, true);
+               if (ssf.null()) {
+                       for(unsigned side_number = 1; side_number <= 
resources::teams->size(); ++side_number) {
+                               sides.insert(side_number);
+                       }
+               }
+               else sides = game_events::get_sides_set(ssf, true);
+       }
+
+       //keep this stack in the loop:
+       //1: getsideKey getmetatable
+       //2: return table
+       //3: userdata for a side
+       //4: getsideKey metatable copy (of index 1)
+
+       lua_settop(L, 0);
+       lua_pushlightuserdata(L, (void*)&getsideKey);
+       lua_rawget(L, LUA_REGISTRYINDEX);
+       lua_createtable(L, sides.size(), 0);
+       unsigned index = 1;
+       foreach(int side, sides) {
+               team** t = static_cast<team**>(lua_newuserdata(L, 
sizeof(team*)));
+               *t = &((*resources::teams)[side - 1]);
+               lua_pushvalue(L, 1);
+               lua_setmetatable(L, 3);
+               lua_rawseti(L, 2, index);
+               ++index;
+       }
+
+       return 1;
+}
+
 /**
  * Adds a modification to a unit.
  * - Arg 1: unit.
@@ -3093,6 +3133,7 @@
                { "get_mouseover_tile",       &intf_get_mouseover_tile       },
                { "get_recall_units",         &intf_get_recall_units         },
                { "get_selected_tile",        &intf_get_selected_tile        },
+               { "get_sides",                &intf_get_sides                },
                { "get_terrain",              &intf_get_terrain              },
                { "get_terrain_info",         &intf_get_terrain_info         },
                { "get_unit",                 &intf_get_unit                 },


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

Reply via email to