Added [lua] conditional tag

Also made conditions fail if they encountered a syntax or runtime error. This seems the
more logical behavior than passing.

WML conditional tags were split into their own Lua file. The one in lua/wml/object.lua
relies on local variables so was left there.
This commit is contained in:
Charles Dang 2018-01-22 23:00:53 +11:00
parent ff3412b67a
commit da3a0ed7cf
4 changed files with 41 additions and 19 deletions

View file

@ -64,6 +64,8 @@ Version 1.13.10+dev:
* [show_if] is now usable in [objectives] subtag [gold_carryover].
* Macro SCEPTRE_OF_FIRE_EFFECT damage increased to 15x4 so Sceptre is an
improvement over the uncut ruby of fire (14x4) in TRoW.
* Added [lua] as a conditional tag.
* Lua errors now cause as a condition to fail instead of pass.
* Miscellaneous and bug fixes:
* Fixed crash after canceling add-on download (bug #2203)
* Fixed ingame help showing units you haven't encountered (bug #2135)
@ -71,7 +73,7 @@ Version 1.13.10+dev:
greater (bug #2185).
* Fix recalls updating shroud immediately when "Delay Shroud Updates" is set
(bug #2196)
* Fixed not being able to undo previous moves after entering planning mode
* Fixed not being able to undo previous moves after entering planning mode
(bug #2303)
Version 1.13.10:

View file

@ -0,0 +1,22 @@
local helper = wesnoth.require "helper"
function wesnoth.wml_conditionals.proceed_to_next_scenario(cfg)
local endlevel_data = wesnoth.get_end_level_data()
if not endlevel_data then
return false
else
return endlevel_data.proceed_to_next_level
end
end
function wesnoth.wml_conditionals.lua(cfg)
cfg = helper.shallow_literal(cfg)
local bytecode, message = load(cfg.code or "")
if not bytecode then
error("~lua:" .. message, 0)
else
return bytecode(helper.get_child(cfg, "args"))
end
end

View file

@ -15,6 +15,7 @@ function wesnoth.game_events.on_save()
return {}
end
wesnoth.require "wml-conditionals"
wesnoth.require "wml-flow"
wesnoth.require "wml"
@ -939,15 +940,6 @@ function wesnoth.wml_actions.story(cfg)
wesnoth.show_story(cfg, title)
end
function wesnoth.wml_conditionals.proceed_to_next_scenario(cfg)
local endlevel_data = wesnoth.get_end_level_data()
if not endlevel_data then
return false
else
return endlevel_data.proceed_to_next_level
end
end
function wesnoth.wml_actions.cancel_action(cfg)
wesnoth.cancel_action()
end

View file

@ -4502,25 +4502,31 @@ bool game_lua_kernel::run_wml_action(const std::string& 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.
* Evaluates a WML conidition.
*
* @returns Whether the condition passed.
* @note @a cfg should be either volatile or long-lived since the Lua
* code may grab it for an arbitrarily long time.
*/
bool game_lua_kernel::run_wml_conditional(const std::string& cmd, vconfig const &cfg)
bool game_lua_kernel::run_wml_conditional(const std::string& cmd, const vconfig& cfg)
{
lua_State *L = mState;
lua_State* L = mState;
if (!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd)) {
// If an invalid coniditional tag is used, consider it a pass.
if(!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd)) {
lg::wml_error() << "unknown conditional wml: [" << cmd << "]\n";
return true;
}
luaW_pushvconfig(L, cfg);
luaW_pcall(L, 1, 1, true);
// Any runtime error is considered a fail.
if(!luaW_pcall(L, 1, 1, true)) {
return false;
}
bool b = luaW_toboolean(L, -1);
lua_pop(L, 1);
return b;
}