Skip to content

Commit

Permalink
Port "trivial" WML tags to Lua
Browse files Browse the repository at this point in the history
This is more about adding Lua API for things that should have it than porting WML tags to Lua.
The following new Lua API functions have been added to the wesnoth table:
- add_fog, remove_fog
- add_sound_source, remove_sound_source, get_sound_source
- log
  • Loading branch information
CelticMinstrel committed Apr 5, 2016
1 parent ff618e7 commit 5cf1279
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 117 deletions.
7 changes: 7 additions & 0 deletions changelog
Expand Up @@ -120,6 +120,13 @@ Version 1.13.4+dev:
of the time in the overall schedule (eg, 1 would be dawn in the default
schedule). Optional second argument takes a time area ID, to set
local instead of global time.
* New wesnoth.add_fog and wesnoth.remove_fog functions allow changing fog
on the map. The [lift_fog] and [clear_fog] tags now use this.
* New wesnoth.add_sound_source, wesnoth.remove_sound_source, and
wesnoth.get_sound_source functions to allow manipulation of sound
sources. The [sound_source] and [remove_sound_source] now use these.
* New wesnoth.log function for printing log messages. The [wml_message]
and [deprecated_message] tags now use this.
* WML tables defined in Lua now accept string keys with array values
(where "array" is a table whose keys are all integers). This joins
the elements of the array with commas and produces a single string
Expand Down
36 changes: 36 additions & 0 deletions data/lua/wml-tags.lua
Expand Up @@ -1003,3 +1003,39 @@ wml_actions.teleport = function(cfg)
end
wesnoth.teleport(unit, cfg.check_passability == false, cfg.clear_shroud ~= false, cfg.animate)
end

function wml_actions.remove_sound_source(cfg)
wesnoth.remove_sound_source(cfg.id)
end

function wml_actions.sound_source(cfg)
wesnoth.add_sound_source(cfg)
end

function wml_actions.deprecated_message(cfg)
wesnoth.log('wml', cfg.message)
end

function wml_actions.wml_message(cfg)
local logger = logger_aliases[cfg.logger] or ''
wesnoth.log(cfg.logger or 'warn', cfg.message. cfg.to_chat)
end

local function parse_fog_cfg(cfg)
-- Side filter
local ssf = helper.child(cfg, "filter_side")
local sides = wesnoth.get_sides(ssf or {})
-- Location filter
local locs = wesnoth.get_locations(cfg)
return locs, sides
end

function wml_actions.lift_fog(cfg)
local locs, sides = parse_fog_cfg(cfg)
wesnoth.remove_fog(sides, locs, not cfg.multiturn)
end

function wml_actions.reset_fog(cfg)
local locs, sides = parse_fog_cfg(cfg)
wesnoth.add_fog(sides, locs, cfg.reset_view)
end
5 changes: 1 addition & 4 deletions data/lua/wml/message.lua
Expand Up @@ -5,10 +5,7 @@ local location_set = wesnoth.require "lua/location_set.lua"
local _ = wesnoth.textdomain "wesnoth"

local function log(msg, level)
wesnoth.wml_actions.wml_message({
message = msg,
logger = level,
})
wesnoth.log(level, msg, true)
end

local function get_image(cfg, speaker)
Expand Down
101 changes: 1 addition & 100 deletions src/game_events/action_wml.cpp
Expand Up @@ -205,71 +205,8 @@ namespace { // Support functions
}
return path;
}

/**
* Implements the lifting and resetting of fog via WML.
* Keeping affect_normal_fog as false causes only the fog override to be affected.
* Otherwise, fog lifting will be implemented similar to normal sight (cannot be
* individually reset and ends at the end of the turn), and fog resetting will, in
* addition to removing overrides, extend the specified teams' normal fog to all
* hexes.
*/
void toggle_fog(const bool clear, const vconfig& cfg, const bool affect_normal_fog=false)
{
// Filter the sides.
const vconfig &ssf = cfg.child("filter_side");
const side_filter s_filter(ssf.null() ? vconfig::empty_vconfig() : ssf, resources::filter_con);
const std::vector<int> sides = s_filter.get_teams();

// Filter the locations.
std::set<map_location> locs;
const terrain_filter t_filter(cfg, resources::filter_con);
t_filter.get_locations(locs, true);

// Loop through sides.
for (const int &side_num : sides)
{
team &t = (*resources::teams)[side_num-1];
if ( !clear )
{
// Extend fog.
t.remove_fog_override(locs);
if ( affect_normal_fog )
t.refog();
}
else if ( !affect_normal_fog )
// Force the locations clear of fog.
t.add_fog_override(locs);
else
// Simply clear fog from the locations.
for (const map_location &hex : locs) {
t.clear_fog(hex);
}
}

// Flag a screen update.
resources::screen->recalculate_minimap();
resources::screen->invalidate_all();
}
} // end anonymous namespace (support functions)

void handle_deprecated_message(const config& cfg)
{
// Note: no need to translate the string, since only used for deprecated things.
const std::string& message = cfg["message"];
lg::wml_error() << message << '\n';
}

void handle_wml_log_message(const config& cfg)
{
const std::string& logger = cfg["logger"];
const std::string& msg = cfg["message"];
bool in_chat = cfg["to_chat"].to_bool(true);

resources::game_events->pump().put_wml_message(logger,msg,in_chat);
}


/**
* Using this constructor for a static object outside action_wml.cpp
* will likely lead to a static initialization fiasco.
Expand Down Expand Up @@ -324,11 +261,6 @@ WML_HANDLER_FUNCTION(clear_global_variable,,pcfg)
verify_and_clear_global_variable(pcfg);
}

WML_HANDLER_FUNCTION(deprecated_message,, cfg)
{
handle_deprecated_message( cfg.get_parsed_config() );
}

static void on_replay_error(const std::string& message, bool /*b*/)
{
ERR_NG << "Error via [do_command]:" << std::endl;
Expand Down Expand Up @@ -385,11 +317,6 @@ WML_HANDLER_FUNCTION(get_global_variable,,pcfg)
verify_and_get_global_variable(pcfg);
}

WML_HANDLER_FUNCTION(lift_fog,, cfg)
{
toggle_fog(true, cfg, !cfg["multiturn"].to_bool(false));
}

WML_HANDLER_FUNCTION(modify_turns,, cfg)
{
config::attribute_value value = cfg["value"];
Expand Down Expand Up @@ -573,10 +500,6 @@ WML_HANDLER_FUNCTION(recall,, cfg)
LOG_WML << "A [recall] tag with the following content failed:\n" << cfg.get_config().debug();
}

WML_HANDLER_FUNCTION(remove_sound_source,, cfg)
{
resources::soundsources->remove(cfg["id"]);
}
namespace {
struct map_choice : public mp_sync::user_choice
{
Expand Down Expand Up @@ -616,6 +539,7 @@ namespace {

};
}

/// Experimental map replace
/// @todo Finish experimenting.
WML_HANDLER_FUNCTION(replace_map,, cfg)
Expand Down Expand Up @@ -676,11 +600,6 @@ WML_HANDLER_FUNCTION(replace_map,, cfg)
ai::manager::raise_map_changed();
}

WML_HANDLER_FUNCTION(reset_fog,, cfg)
{
toggle_fog(false, cfg, cfg["reset_view"].to_bool(false));
}

/// Experimental data persistence
/// @todo Finish experimenting.
WML_HANDLER_FUNCTION(set_global_variable,,pcfg)
Expand Down Expand Up @@ -1029,19 +948,6 @@ WML_HANDLER_FUNCTION(set_variables,, cfg)
}
}

WML_HANDLER_FUNCTION(sound_source,, cfg)
{
config parsed = cfg.get_parsed_config();
try {
soundsource::sourcespec spec(parsed);
resources::soundsources->add(spec);
} catch (bad_lexical_cast &) {
ERR_NG << "Error when parsing sound_source config: bad lexical cast." << std::endl;
ERR_NG << "sound_source config was: " << parsed.debug() << std::endl;
ERR_NG << "Skipping this sound source..." << std::endl;
}
}

/// Store the relative direction from one hex to another in a WML variable.
/// This is mainly useful as a diagnostic tool, but could be useful
/// for some kind of scenario.
Expand Down Expand Up @@ -1274,11 +1180,6 @@ WML_HANDLER_FUNCTION(volume,, cfg)

}

WML_HANDLER_FUNCTION(wml_message,, cfg)
{
handle_wml_log_message( cfg.get_parsed_config() );
}

WML_HANDLER_FUNCTION(on_undo,, cfg)
{
if(cfg["delayed_variable_substitution"].to_bool(false)) {
Expand Down
5 changes: 0 additions & 5 deletions src/game_events/action_wml.hpp
Expand Up @@ -72,11 +72,6 @@ namespace game_events
*/
void change_terrain(const map_location &loc, const t_translation::t_terrain &t,
terrain_type_data::tmerge_mode mode, bool replace_if_failed);

/** Used for [deprecated_message]. */
void handle_deprecated_message(const config& cfg);
/** Used for [wml_message]. */
void handle_wml_log_message(const config& cfg);
}

#endif // GAME_EVENTS_ACTION_WML_H_INCLUDED
Expand Down

0 comments on commit 5cf1279

Please sign in to comment.