From 7fa5dfdeaefde91c66d9dce991601c79bdc7c67e Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Wed, 28 Jul 2010 14:06:45 +0000 Subject: [PATCH] Added function for matching a single unit. --- src/scripting/lua.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 51dd065429e..0f39fd1f305 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -927,6 +927,47 @@ static int intf_get_units(lua_State *L) return 1; } +/** + * Matches a unit against the given filter. + * - Arg 1: full userdata. + * - Arg 2: table containing a filter + * - Ret 1: boolean. + */ +static int intf_match_unit(lua_State *L) +{ + if (!luaW_hasmetatable(L, 1, getunitKey)) { + return luaL_typerror(L, 1, "unit"); + error_call_destructors_1: + return luaL_argerror(L, 1, "unit not found"); + error_call_destructors_2: + return luaL_typerror(L, 2, "WML table"); + } + + lua_unit *lu = static_cast(lua_touserdata(L, 1)); + unit *u = lu->get(); + if (!u) goto error_call_destructors_1; + + vconfig filter = vconfig::unconstructed_vconfig(); + if (!luaW_tovconfig(L, 2, filter, false)) + goto error_call_destructors_2; + + if (filter.null()) { + lua_pushboolean(L, true); + return 1; + } + + if (int side = lu->on_recall_list()) { + team &t = (*resources::teams)[side - 1]; + scoped_recall_unit auto_store("this_unit", + t.save_id(), u - &t.recall_list()[0]); + lua_pushboolean(L, u->matches_filter(filter, map_location())); + return 1; + } + + lua_pushboolean(L, u->matches_filter(filter, u->get_location())); + return 1; +} + /** * Gets the numeric ids of all the units matching a given filter on the recall lists. * - Arg 1: optional table containing a filter @@ -2669,6 +2710,7 @@ LuaKernel::LuaKernel() { "get_variable", &intf_get_variable }, { "get_village_owner", &intf_get_village_owner }, { "is_enemy", &intf_is_enemy }, + { "match_unit", &intf_match_unit }, { "message", &intf_message }, { "play_sound", &intf_play_sound }, { "put_recall_unit", &intf_put_recall_unit },