move WML [event] impl to lua, fixup core wml vs lua loading order

This commit moves [event] to be implemented in lua/wml-tags.lua.
It turns out that because of some questionable ordering in
data/core/_main.cfg, none of the tags defined in
data/lua/wml-tags.lua are actually defined at the time that core
is read, instead they are defined right after this. This is broken,
the entire wml library should be defined before core is read. Thus
we reorder some directives in data/core/_main.cfg to ensure this.

This commit adds lua callbacks `wesnoth.add_event_handler`,
`wesnoth.remove_event_handler` as well.
This commit is contained in:
Chris Beck 2014-12-24 04:21:58 -05:00
parent 0e365efc1a
commit 0246026940
5 changed files with 42 additions and 21 deletions

View file

@ -3,6 +3,14 @@
# wmlscope: set export=yes
[lua]
code=<<
wesnoth.dofile 'lua/backwards-compatibility.lua'
wesnoth.dofile 'lua/wml-tags.lua'
wesnoth.dofile 'ai/micro_ais/micro_ai_wml_tag.lua'
>>
[/lua]
#ifndef NO_TERRAIN_GFX
{core/terrain-graphics/}
#endif
@ -16,11 +24,3 @@
#endif
{core/units.cfg}
{core/editor/}
[lua]
code=<<
wesnoth.dofile 'lua/backwards-compatibility.lua'
wesnoth.dofile 'lua/wml-tags.lua'
wesnoth.dofile 'ai/micro_ais/micro_ai_wml_tag.lua'
>>
[/lua]

View file

@ -1363,6 +1363,15 @@ function wml_actions.end_turn(cfg)
wesnoth.end_turn()
end
function wml_actions.event(cfg)
local remove = cfg.remove
if remove then
wesnoth.remove_event_handler(cfg.id)
else
wesnoth.add_event_handler(cfg)
end
end
function wml_actions.label(cfg)
wesnoth.label(cfg)
end

View file

@ -670,19 +670,6 @@ WML_HANDLER_FUNCTION(endlevel, /*event_info*/, cfg)
}
}
/// Adding new events
WML_HANDLER_FUNCTION(event, /*event_info*/, cfg)
{
assert(resources::game_events);
if (cfg["remove"].to_bool(false)) {
resources::game_events->remove_event_handler(cfg["id"]);
} else if (!cfg["delayed_variable_substitution"].to_bool(true)) {
resources::game_events->add_event_handler(cfg.get_parsed_config());
} else {
resources::game_events->add_event_handler(cfg.get_config());
}
}
/// Experimental data persistence
/// @todo Finish experimenting.
WML_HANDLER_FUNCTION(get_global_variable,/**/,pcfg)

View file

@ -51,6 +51,7 @@
#include "game_display.hpp" // for game_display
#include "game_errors.hpp" // for game_error
#include "game_events/conditional_wml.hpp" // for conditional_passed
#include "game_events/manager.hpp" // for add_event_handler
#include "game_events/pump.hpp" // for queued_event
#include "game_preferences.hpp" // for encountered_units
#include "image.hpp" // for get_image, locator
@ -2729,6 +2730,26 @@ int game_lua_kernel::intf_remove_tile_overlay(lua_State *L)
return 0;
}
/// Adding new events
int game_lua_kernel::intf_add_event(lua_State *L)
{
vconfig cfg(luaW_checkvconfig(L, 1));
game_events::manager & man = *game_state_.events_manager_;
if (!cfg["delayed_variable_substitution"].to_bool(true)) {
man.add_event_handler(cfg.get_parsed_config());
} else {
man.add_event_handler(cfg.get_config());
}
return 0;
}
int game_lua_kernel::intf_remove_event(lua_State *L)
{
game_state_.events_manager_->remove_event_handler(luaL_checkstring(L, 1));
return 0;
}
int game_lua_kernel::intf_color_adjust(lua_State *L)
{
if (game_display_) {
@ -3219,6 +3240,7 @@ game_lua_kernel::game_lua_kernel(const config &cfg, CVideo * video, game_state &
{ NULL, NULL }
};
lua_cpp::Reg const cpp_callbacks[] = {
{ "add_event_handler", boost::bind(&game_lua_kernel::intf_add_event, this, _1) },
{ "add_tile_overlay", boost::bind(&game_lua_kernel::intf_add_tile_overlay, this, _1) },
{ "allow_end_turn", boost::bind(&game_lua_kernel::intf_allow_end_turn, this, _1) },
{ "allow_undo", boost::bind(&game_lua_kernel::intf_allow_undo, this, _1) },
@ -3264,6 +3286,7 @@ game_lua_kernel::game_lua_kernel(const config &cfg, CVideo * video, game_state &
{ "put_recall_unit", boost::bind(&game_lua_kernel::intf_put_recall_unit, this, _1) },
{ "put_unit", boost::bind(&game_lua_kernel::intf_put_unit, this, _1) },
{ "redraw", boost::bind(&game_lua_kernel::intf_redraw, this, _1) },
{ "remove_event_handler", boost::bind(&game_lua_kernel::intf_remove_event, this, _1) },
{ "remove_shroud", boost::bind(&game_lua_kernel::intf_shroud_op, this, _1, false) },
{ "remove_tile_overlay", boost::bind(&game_lua_kernel::intf_remove_tile_overlay, this, _1) },
{ "replace_schedule", boost::bind(&game_lua_kernel::intf_replace_schedule, this, _1) },

View file

@ -124,6 +124,8 @@ class game_lua_kernel : public lua_kernel_base
int intf_get_sides(lua_State* L);
int intf_add_tile_overlay(lua_State *L);
int intf_remove_tile_overlay(lua_State *L);
int intf_add_event(lua_State *L);
int intf_remove_event(lua_State *L);
int intf_color_adjust(lua_State *L);
int intf_delay(lua_State *L);
int intf_label(lua_State *L);