Skip to content

Commit

Permalink
pickadvance: Update deprecated things and other updates
Browse files Browse the repository at this point in the history
- Use the functional library instead of a hand-rolled filter
- Use stringx.split
- No need for an object ID if using remove_modifications
  • Loading branch information
CelticMinstrel committed Feb 17, 2021
1 parent 5d67c19 commit 174f736
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 52 deletions.
32 changes: 17 additions & 15 deletions data/modifications/pick_advance/dialog.lua
@@ -1,7 +1,7 @@
-- << pickadvance_dialog

pickadvance = {}
local T = wesnoth.require("lua/helper.lua").set_wml_tag_metatable {}
local T = wml.tag
local _ = wesnoth.textdomain "wesnoth"

function pickadvance.show_dialog_unsynchronized(advance_info, unit)
Expand Down Expand Up @@ -162,49 +162,51 @@ function pickadvance.show_dialog_unsynchronized(advance_info, unit)
}

-- dialog preshow function
local function preshow()
wesnoth.set_dialog_visible(not unit.canrecruit, "apply_to_all")
local function preshow(window)
window.apply_to_all.visible = not unit.canrecruit

local selection = 0

local empty_icon_unit = "misc/blank-hex.png"

wesnoth.set_dialog_value(empty_icon_unit, "the_list", 1, "the_icon")
wesnoth.set_dialog_value( _ "No planned advancement", "the_list", 1, "the_label")
wesnoth.set_dialog_visible(false, "the_list", 1, "global_icon")
local null_row = window.the_list[1]
null_row.the_icon.label = empty_icon_unit
null_row.the_label.label = _ "No planned advancement"
null_row.global_icon.visible = false

for i, advance_type in ipairs(options) do
local n = i + 1
local text = advance_type.name
if advance_type.id == game_override_one or advance_type.id == unit_override_one then
selection = n
end
wesnoth.set_dialog_value(text, "the_list", n, "the_label")
local this_row = window.the_list[n]
this_row.the_label.label = text
local img = advance_type.__cfg.image
if img then
img = ("%s~TC(%d,%s)"):format(img, unit.side, advance_type.__cfg.flag_rgb or "magenta")
else
img = empty_icon_unit
end
wesnoth.set_dialog_value(img, "the_list", n, "the_icon")
wesnoth.set_dialog_visible(not not (advance_type.id == game_override_one) or "hidden", "the_list", n, "global_icon")
this_row.the_icon.label = img
this_row.global_icon.visible = not not (advance_type.id == game_override_one) or "hidden"
end

wesnoth.set_dialog_focus("the_list")
window.the_list:focus()
if selection > 0 then
wesnoth.set_dialog_value(selection, "the_list")
window.the_list.selected_index = selection
end
end

-- dialog postshow function
local item_result
local apply_to_all
local function postshow()
item_result = wesnoth.get_dialog_value("the_list") - 1
apply_to_all = wesnoth.get_dialog_value("apply_to_all")
local function postshow(window)
item_result = window.the_list.selected_index - 1
apply_to_all = window.apply_to_all.selected
end

local dialog_exit_code = wesnoth.show_dialog(dialog, preshow, postshow)
local dialog_exit_code = gui.show_dialog(dialog, preshow, postshow)

if dialog_exit_code == cancel_code then
return { ignore = true }
Expand Down
57 changes: 20 additions & 37 deletions data/modifications/pick_advance/main.lua
@@ -1,7 +1,8 @@
-- << pick_advance/main.lua

local on_event = wesnoth.require("lua/on_event.lua")
local T = wesnoth.require("lua/helper.lua").set_wml_tag_metatable {}
local on_event = wesnoth.require "on_event"
local F = wesnoth.require "functional"
local T = wml.tag
local _ = wesnoth.textdomain "wesnoth"

wesnoth.wml_actions.set_menu_item {
Expand All @@ -27,15 +28,10 @@ end
-- splits a comma delimited string of unit types
-- returns a table of unit types that aren't blank, "null", and that exist
local function split_comma_units(string_to_split)
local result = {}
local n = 1
for s in string.gmatch(string_to_split or "", "[^,]+") do
if s ~= "" and s ~= "null" and wesnoth.unit_types[s] then
result[n] = s
n = n + 1
end
end
return result
return F.filter(
stringx.split(string_to_split or ""),
function(s) return s ~= "" and s ~= "null" and wesnoth.unit_types[s] end
)
end

-- returns a table of the original unit types
Expand All @@ -48,13 +44,13 @@ end

-- replace the unit's current advancements with the new set of units via object/effect
local function set_advances(unit, array)
wesnoth.add_modification(unit, "object", {
id = "pickadvance",
unit:add_modification("object", {
pickadvance = true,
take_only_once = false,
T.effect {
apply_to = "new_advancement",
replace = true,
types = table.concat(array, ",")
types = array
}
})
end
Expand All @@ -69,23 +65,11 @@ local function array_to_set(arr)
return result
end

-- for table "arr" containing sets of [unit_type,true]
-- return table containing sets of [index,unit_type]
local function array_filter(arr, func)
local result = {}
for _, v in ipairs(arr) do
if func(v) then
result[#result + 1] = v
end
end
return result
end

-- works as anti-cheat and fixes tricky bugs in [male]/[female]/undead variation overrides
local function filter_overrides(unit, overrides)
local possible_advances_array = original_advances(unit)
local possible_advances = array_to_set(possible_advances_array)
local filtered = array_filter(overrides, function(e) return possible_advances[e] end)
local filtered = F.filter(overrides, function(e) return possible_advances[e] end)
return #filtered > 0 and filtered or possible_advances_array
end

Expand All @@ -95,7 +79,7 @@ end
local function get_advance_info(unit)
local type_advances, orig_options_sanitized = original_advances(unit)
local game_override_key = "pickadvance_side" .. unit.side .. "_" .. orig_options_sanitized
local game_override = wesnoth.get_variable(game_override_key)
local game_override = wml.variables[game_override_key]
local function correct(override)
return override and #override > 0 and #override < #type_advances and override or nil
end
Expand All @@ -112,7 +96,7 @@ end
-- the unit is on a local human controlled side
-- the unit has multiple options in either its original set of advancements or current set of advancements
function pickadvance.menu_available()
local unit = wesnoth.get_unit(wml.variables.x1, wml.variables.y1)
local unit = wesnoth.units.get(wml.variables.x1, wml.variables.y1)
return unit and
#unit.advances_to > 0
and wesnoth.sides[unit.side].is_local and wesnoth.sides[unit.side].controller == "human"
Expand All @@ -125,9 +109,8 @@ end
local function initialize_unit(unit)
local clean_type = clean_type_func(unit.type)
if unit.variables["pickadvance_orig_" .. clean_type] == nil then
wesnoth.wml_actions.remove_object {
object_id = "pickadvance",
id = unit.id
unit:remove_modifications{
pickadvance = true
}
unit.variables["pickadvance_orig_" .. clean_type] = table.concat(unit.advances_to, ",")
local advance_info = get_advance_info(unit)
Expand All @@ -139,7 +122,7 @@ end

-- let the player select the unit's advancement via dialog
function pickadvance.pick_advance(unit)
unit = unit or wesnoth.get_unit(wml.variables.x1, wml.variables.y1)
unit = unit or wesnoth.units.get(wml.variables.x1, wml.variables.y1)
initialize_unit(unit)
local _, orig_options_sanitized = original_advances(unit)
local dialog_result = wesnoth.synchronize_choice(function()
Expand All @@ -158,7 +141,7 @@ function pickadvance.pick_advance(unit)
end
if dialog_result.is_game_override then
local key = "pickadvance_side" .. unit.side .. "_" .. orig_options_sanitized
wesnoth.set_variable(key, table.concat(dialog_result.game_override, ","))
wml.variables[key] = table.concat(dialog_result.game_override, ",")
end
end

Expand All @@ -178,7 +161,7 @@ end
-- make its advancements viewable
-- force picking an advancement if it has multiple and the force option was specified
local function initialize_unit_x1y1(ctx)
local unit = wesnoth.get_unit(ctx.x1, ctx.y1)
local unit = wesnoth.units.get(ctx.x1, ctx.y1)
if not wesnoth.sides[unit.side].__cfg.allow_player then return end
initialize_unit(unit)
make_unit_known(unit)
Expand All @@ -190,7 +173,7 @@ end
-- return true if the side can be played and has either a recruit list set or non-leader units
local function humans_can_recruit()
for _, side in ipairs(wesnoth.sides) do
local units = wesnoth.get_units { side = side.side, canrecruit = false }
local units = wesnoth.units.find { side = side.side, canrecruit = false }
if side.__cfg.allow_player and (#side.recruit ~= 0 or #units > 0) then
return true
end
Expand Down Expand Up @@ -227,7 +210,7 @@ on_event("moveto", function()
if fresh_turn then
fresh_turn = false
if not wesnoth.sides[wesnoth.current.side].__cfg.allow_player then return end
for _, unit in ipairs(wesnoth.get_units { side = wesnoth.current.side }) do
for _, unit in ipairs(wesnoth.units.find { side = wesnoth.current.side }) do
if #unit.advances_to > 1 and wml.variables.pickadvance_force_choice and wesnoth.current.turn > 1 then
pickadvance.pick_advance(unit)
if #unit.advances_to > 1 then
Expand Down

0 comments on commit 174f736

Please sign in to comment.