Skip to content

Commit

Permalink
add wml_conditionals support, in addition to wml_actions in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Apr 10, 2015
1 parent fb01510 commit 4bb26ce
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
52 changes: 35 additions & 17 deletions src/game_events/conditional_wml.cpp
Expand Up @@ -17,27 +17,28 @@
* Implementations of conditional action WML tags.
*/

#include "../global.hpp"
#include "global.hpp"
#include "conditional_wml.hpp"

#include "../config.hpp"
#include "../game_board.hpp"
#include "../game_data.hpp"
#include "../log.hpp"
#include "../recall_list_manager.hpp"
#include "../resources.hpp"
#include "../serialization/string_utils.hpp"
#include "../team.hpp"
#include "../terrain_filter.hpp"
#include "../unit.hpp"
#include "../unit_filter.hpp"
#include "../unit_map.hpp"
#include "../unit_types.hpp"
#include "../util.hpp"
#include "../variable.hpp"
#include "config.hpp"
#include "game_board.hpp"
#include "game_data.hpp"
#include "log.hpp"
#include "recall_list_manager.hpp"
#include "resources.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "serialization/string_utils.hpp"
#include "team.hpp"
#include "terrain_filter.hpp"
#include "unit.hpp"
#include "unit_filter.hpp"
#include "unit_map.hpp"
#include "unit_types.hpp"
#include "util.hpp"
#include "variable.hpp"

#include <boost/foreach.hpp>

#include <boost/assign/list_of.hpp>

static lg::log_domain log_engine("engine");
#define WRN_NG LOG_STREAM(warn, log_engine)
Expand Down Expand Up @@ -170,6 +171,23 @@ namespace { // Support functions
#undef TEST_NUM_ATTR
#undef TEST_BOL_ATTR
}

vconfig::all_children_iterator cond_end = cond.ordered_end();
static const boost::container::flat_set<std::string> hard_coded = boost::assign::list_of("true")("false")("have_unit")("have_location")("variable")
("then")("else")("elseif")("not")("and")("or")("do");

assert(resources::lua_kernel);

for (vconfig::all_children_iterator it = cond.ordered_begin(); it != cond_end; ++it) {
std::string key = it.get_key();
if (std::find(hard_coded.begin(), hard_coded.end(), key) == hard_coded.end()) {
bool result = resources::lua_kernel->run_wml_conditional(key, it.get_child());
if (!result) {
return false;
}
}
}

return true;
}

Expand Down
35 changes: 35 additions & 0 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -4172,6 +4172,14 @@ game_lua_kernel::game_lua_kernel(const config &cfg, CVideo * video, game_state &
lua_setfield(L, -2, "wml_actions");
lua_pop(L, 1);

// Create the wml_conditionals table.
cmd_log_ << "Adding wml_conditionals table...\n";

lua_getglobal(L, "wesnoth");
lua_newtable(L);
lua_setfield(L, -2, "wml_conditionals");
lua_pop(L, 1);

// Create the game_events table.
cmd_log_ << "Adding game_events table...\n";

Expand Down Expand Up @@ -4418,6 +4426,33 @@ bool game_lua_kernel::run_wml_action(std::string const &cmd, vconfig const &cfg,
}


/**
* Runs a command from an event handler.
* @return true if there is a handler for the command.
* @note @a cfg should be either volatile or long-lived since the Lua
* code may grab it for an arbitrary long time.
*/
bool game_lua_kernel::run_wml_conditional(std::string const &cmd, vconfig const &cfg)
{
lua_State *L = mState;


if (!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd.c_str(), NULL)) {
std::string err_msg = "unknown conditional wml: [";
err_msg += cmd;
err_msg += "]";
luaL_argerror(L, 1, err_msg.c_str());
}

luaW_pushvconfig(L, cfg);
luaW_pcall(L, 1, 0, true);

bool b = luaW_toboolean(L, -1);
lua_pop(L, 1);
return b;
}


/**
* Runs a script from a unit filter.
* The script is an already compiled function given by its name.
Expand Down
1 change: 1 addition & 0 deletions src/scripting/game_lua_kernel.hpp
Expand Up @@ -173,6 +173,7 @@ class game_lua_kernel : public lua_kernel_base
bool run_wml_action(std::string const &, vconfig const &,
game_events::queued_event const &);
bool run_filter(char const *name, unit const &u);
bool run_wml_conditional(std::string const &, vconfig const &);

virtual void log_error(char const* msg, char const* context = "Lua error");

Expand Down

0 comments on commit 4bb26ce

Please sign in to comment.