Author: fendrin
Date: Fri May 13 13:23:22 2011
New Revision: 49498
URL: http://svn.gna.org/viewcvs/wesnoth?rev=49498&view=rev
Log:
Ported the [allow_recruit] tag to lua and removed the c++ implementation.
Create an accessor for the help systems known units list for the lua coder.
(used by allow_recruit)
Added an accessor for the new unit attribute "extra_recruit" for the lua coder.
Implemented [allow_extra_recruit], [dissallow_extra_recruit] and
[set_extra_recruit]
by lua defined wml tags.
AI awareness is still to be done, the code locations are marked TODO in
unit.cpp.
Modified:
trunk/data/lua/wml-tags.lua
trunk/src/game_events.cpp
trunk/src/scripting/lua.cpp
trunk/src/unit.cpp
trunk/src/unit.hpp
Modified: trunk/data/lua/wml-tags.lua
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/data/lua/wml-tags.lua?rev=49498&r1=49497&r2=49498&view=diff
==============================================================================
--- trunk/data/lua/wml-tags.lua (original)
+++ trunk/data/lua/wml-tags.lua Fri May 13 13:23:22 2011
@@ -128,6 +128,31 @@
wesnoth.fire_event(cfg.name, x1, y1, x2, y2, w1, w2)
end
+function wml_actions.allow_recruit(cfg)
+ for side_number, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do
+ local v = team.recruit
+ for type in string.gmatch(cfg.type, "[^%s,][^,]*") do
+ table.insert(v, type)
+ wesnoth.add_known_unit(type)
+ end
+ team.recruit = v
+ end
+end
+
+function wml_actions.allow_extra_recruit(cfg)
+ local filter = helper.get_child(cfg, "filter") or
helper.wml_error("[allow_extra_recruit] missing required [filter] tag")
+ local types = cfg.type or helper.wml_error("[allow_extra_recruit] missing
required type= attribute")
+
+ for index, unit in ipairs(wesnoth.get_units(filter)) do
+ local v = unit.extra_recruit
+ for type in string.gmatch(types, "[^%s,][^,]*") do
+ table.insert(v, type)
+ wesnoth.add_known_unit(type)
+ end
+ unit.extra_recruit = v
+ end
+end
+
function wml_actions.disallow_recruit(cfg)
for side_number, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do
local v = team.recruit
@@ -143,15 +168,46 @@
end
end
+function wml_actions.disallow_extra_recruit(cfg)
+ local filter = helper.get_child(cfg, "filter") or
helper.wml_error("[disallow_extra_recruit] missing required [filter] tag")
+ local types = cfg.type or helper.wml_error("[allow_extra_recruit] missing
required type= attribute")
+ for index, unit in ipairs(wesnoth.get_units(filter)) do
+ local v = unit.extra_recruit
+ for w in string.gmatch(types, "[^%s,][^,]*") do
+ for i, r in ipairs(v) do
+ if r == w then
+ table.remove(v, i)
+ break
+ end
+ end
+ end
+ unit.extra_recruit = v
+ end
+end
+
function wml_actions.set_recruit(cfg)
+ local recruit = cfg.recruit or helper.wml_error("[set_recruit] missing
required recruit= attribute")
for side_number, team in ipairs(wesnoth.get_sides(nil, cfg, true)) 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
table.insert(v, w)
end
team.recruit = v
end
+end
+
+function wml_actions.set_extra_recruit(cfg)
+ local filter = helper.get_child(cfg, "filter") or
helper.wml_error("[disallow_extra_recruit] missing required [filter] tag")
+ local recruit = cfg.extra_recruit or helper.wml_error("[set_extra_recruit]
missing required extra_recruit= attribute")
+ local v = {}
+
+ for w in string.gmatch(recruit, "[^%s,][^,]*") do
+ table.insert(v, w)
+ end
+
+ for index, unit in ipairs(wesnoth.get_units(filter)) do
+ unit.extra_recruit = v
+ end
end
function wml_actions.store_map_dimensions(cfg)
Modified: trunk/src/game_events.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.cpp?rev=49498&r1=49497&r2=49498&view=diff
==============================================================================
--- trunk/src/game_events.cpp (original)
+++ trunk/src/game_events.cpp Fri May 13 13:23:22 2011
@@ -690,22 +690,6 @@
resources::screen->draw();
}
-WML_HANDLER_FUNCTION(allow_recruit, /*event_info*/, cfg)
-{
- std::set<int> sides = game_events::get_sides_set(cfg, false, true);
-
- foreach(const int &side_num, sides)
- {
- const std::vector<std::string> types =
utils::split(cfg["type"]);
- foreach(const std::string &type, types)
- {
- const unsigned index = side_num - 1;
- (*resources::teams)[index].add_recruit(type);
- preferences::encountered_units().insert(type);
- }
- }
-}
-
WML_HANDLER_FUNCTION(volume, /*event_info*/, cfg)
{
Modified: trunk/src/scripting/lua.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.cpp?rev=49498&r1=49497&r2=49498&view=diff
==============================================================================
--- trunk/src/scripting/lua.cpp (original)
+++ trunk/src/scripting/lua.cpp Fri May 13 13:23:22 2011
@@ -40,6 +40,7 @@
#include "filesystem.hpp"
#include "foreach.hpp"
#include "game_display.hpp"
+#include "game_preferences.hpp"
#include "gamestatus.hpp"
#include "log.hpp"
#include "lua_jailbreak_exception.hpp"
@@ -889,6 +890,19 @@
return_int_attrib("attacks_left", u.attacks_left());
return_tstring_attrib("name", u.name());
return_bool_attrib("canrecruit", u.can_recruit());
+
+ if (strcmp(m, "extra_recruit") == 0) {
+ std::set<std::string> const &recruits = u.recruits();
+ lua_createtable(L, recruits.size(), 0);
+ int i = 1;
+ foreach (std::string const &r, u.recruits()) {
+ lua_pushstring(L, r.c_str());
+ lua_rawseti(L, -2, i++);
+ }
+ return 1;
+ }
+
+
if (strcmp(m, "status") == 0) {
lua_createtable(L, 1, 0);
lua_pushvalue(L, 1);
@@ -941,6 +955,19 @@
modify_string_attrib("role", u.set_role(value));
modify_string_attrib("facing",
u.set_facing(map_location::parse_direction(value)));
modify_bool_attrib("hidden", u.set_hidden(value));
+
+ if (strcmp(m, "extra_recruit") == 0) {
+ u.set_recruits(std::set<std::string>());
+ if (!lua_istable(L, 3)) return 0;
+ for (int i = 1;; ++i) {
+ lua_rawgeti(L, 3, i);
+ if (lua_isnil(L, -1)) break;
+ u.add_recruit(lua_tostring(L, -1));
+ lua_pop(L, 1);
+ }
+ return 0;
+ }
+
if (!lu->on_map()) {
map_location loc = u.get_location();
modify_int_attrib("x", loc.x = value - 1; u.set_location(loc));
@@ -2971,6 +2998,18 @@
}
/**
+ * Adds a new known unit type to the help system.
+ * - Arg 1: string.
+ */
+static int intf_add_known_unit(lua_State *L)
+{
+ char const *ty = luaL_checkstring(L, 1);
+ std::string type = ty;
+ preferences::encountered_units().insert(type);
+ return 0;
+}
+
+/**
* Adds an overlay on a tile.
* - Args 1,2: location.
* - Arg 3: WML table.
@@ -3118,6 +3157,7 @@
// Put some callback functions in the scripting environment.
static luaL_reg const callbacks[] = {
+ { "add_known_unit", &intf_add_known_unit },
{ "add_modification", &intf_add_modification },
{ "add_tile_overlay", &intf_add_tile_overlay },
{ "clear_messages", &intf_clear_messages },
Modified: trunk/src/unit.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=49498&r1=49497&r2=49498&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Fri May 13 13:23:22 2011
@@ -989,6 +989,22 @@
return(color);
}
+void unit::set_recruits(const std::set<std::string>& recruits)
+{
+ recruit_list_ = recruits;
+ //TODO
+ //info_.minimum_recruit_price = 0;
+ //ai::manager::raise_recruit_list_changed();
+}
+
+void unit::add_recruit(const std::string &recruit)
+{
+ recruit_list_.insert(recruit);
+ //TODO
+ //info_.minimum_recruit_price = 0;
+ //ai::manager::raise_recruit_list_changed();
+}
+
std::string unit::side_id() const {return
teams_manager::get_teams()[side()-1].save_id(); }
void unit::set_movement(int moves)
Modified: trunk/src/unit.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.hpp?rev=49498&r1=49497&r2=49498&view=diff
==============================================================================
--- trunk/src/unit.hpp (original)
+++ trunk/src/unit.hpp Fri May 13 13:23:22 2011
@@ -129,6 +129,9 @@
bool can_recruit() const { return canrecruit_; }
const std::set<std::string>& recruits() const
{ return recruit_list_; }
+ void add_recruit(const std::string &);
+ void set_recruits(const std::set<std::string>& recruits);
+
bool incapacitated() const { return get_state(STATE_PETRIFIED); }
int total_movement() const { return max_movement_; }
int movement_left() const { return (movement_ == 0 || incapacitated())
? 0 : movement_; }
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits