-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwlp_utils.lua
164 lines (146 loc) · 4.61 KB
/
wlp_utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
local utils = {}
--! Function removes the first child with a given name.
utils.remove_child = wesnoth.deprecate_api('utils.remove_child', 'wml.remove_child', 1, nil, wml.remove_child)
--! Function checks recursively if all values contained in table a are also contained in table b.
--! melinath
function utils.filter_wml( f_initial, t_initial, indent_initial )
std_print( indent_initial .. "Matching..." )
local function sub_filter( f, t )
local indent = indent_initial .. " "
local attr_match = true
local tables = 0
for key, value in pairs(f) do
if ( type(value) == "table" ) then
tables = tables + 1
else
std_print( string.format( "%sChecking filter attribute: %s", indent, key ) )
std_print( string.format( "%sfilter: %s", indent, tostring( f[key] ) ) )
std_print( string.format( "%schecking: %s", indent, tostring( t[key] ) ) )
if ( f[key] ~= t[key]) then attr_match = false end
std_print( string.format( "%sMatch: %s", indent, tostring( f[key] == t[key] ) ) )
end
end
if ( attr_match == false ) then return false
else
if ( tables==0 ) then return true end
for key, value in pairs(f) do
if ( type(value) == "table" ) then
std_print( string.format( "%sEntering filter table: %s", indent, value.tag ) )
for index = 1, #t do
std_print( string.format( "%sChecking against: %s", indent, t[index].tag ) )
std_print( string.format( "%sName Match: %s", indent, tostring( value.tag == t[index].tag ) ) )
if( t[index].tag == value.tag ) then
local x=sub_filter( value.contents, t[index].contents )
std_print( string.format( "%sTable match: %s", indent, tostring(x) ) )
if x then return true end
end
end
end
end
return false
end
end
return sub_filter(f_initial,t_initial)
end
function utils.extract_side(side_num)
local team = wesnoth.sides[side_num]
local debug_utils = wesnoth.require("~add-ons/Wesnoth_Lua_Pack/debug_utils.lua")
debug_utils.dbms(wesnoth.sides[side_num], false, string.format("Extraction of side %u", side_num), true)
end
function utils.extract_unit(filter)
local char = wesnoth.get_units(filter)
local debug_utils = wesnoth.require("~add-ons/Wesnoth_Lua_Pack/debug_utils.lua")
debug_utils.dbms(char, false, string.format("Extraction of units (first character id: %s)", char[1].id), true)
end
--like wml_actions.message but without text wrap
function utils.message(cfg)
local dialog =
{
{ "helptip", { id = "helptip_large" } },
{ "tooltip", { id = "tooltip_large" } },
{ "grid",
{
{ "row",
{
{ "column",
{ horizontal_alignment = "left",
{ "grid",
{
{ "row",
{
{ "column",
{ border_size = 30, border = "all",
{ "label",
{ label = cfg.caption, definition = "title" }
}
}
},
{"column",
{ border_size = 5, border = "all",
{ "button",
{ label = "abort", return_value = -2 }
}
}
},
{"column",
{ border_size = 5, border = "all",
{ "button",
{ label = "continue", return_value = -1 }
}
}
}
}
}
}
}
}
}
}
},
{ "row",
{
{ "column",
{ border_size = 10, border = "all",
{ "label", { label = cfg.message,
use_markup = false }}
}
}
}
}
}--grid
}--grid
}--dialog
return wesnoth.sync.evaluate_single(function() local value = gui.show_dialog(dialog); return { key = value } end).key
end
--! Displays a WML text input message box with attributes from table @attr
--! with optional table @options containing optional label, max_length, and text key/value pairs.
--! @returns the entered text.
function utils.get_text_input(attr, options)
options = options or {}
local msg = {}
for k,v in pairs(attr) do
msg[k] = attr[k]
end
local ti = {}
for k,v in pairs(options) do
ti[k] = options[k]
end
ti["variable"]="LUA_text_input"
table.insert(msg, { "text_input", ti })
wesnoth.wml_actions.message(msg)
local result = wml.variables["LUA_text_input"]
wml.variables["LUA_text_input"] = nil
return result
end
-- two support functions for handling strings
function utils.split( str, char )
char = char or ","
local pattern = "[^" .. char .. "]+"
return string.gmatch( str, pattern )
end
function utils.chop( str )
local temp = string.gsub( str, "^%s+", "" )
temp = string.gsub( temp, "%s+$", "" )
return temp
end
return utils