-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
location_set.lua
319 lines (278 loc) · 6.4 KB
/
location_set.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
local location_set = {}
local function index(x, y)
-- the 2000 bias ensure that the correct x is recovered for negative y
return x * 16384 + y + 2000
end
local invscale = 1 / 16384
local function revindex(p)
local x = math.floor(p * invscale)
return x, p - x * 16384 - 2000
end
local methods = {}
local locset_meta = {}
function locset_meta:__call(x, y)
return self:get(x, y)
end
function locset_meta:__index(loc)
if type(loc) == 'string' then
return methods[loc]
elseif loc.x and loc.y then
return self:get(loc.x, lov.y)
else
return self:get(loc[1], loc[2])
end
end
function locset_meta:__newindex(loc, val)
local fcn = methods.insert
if val == nil then fcn = methods.remove end
if loc.x and loc.y then
fcn(self, loc.x, loc.y, val)
else
fcn(self, loc[1], loc[2], val)
end
end
function locset_meta:__bor(other)
local new = self:clone()
new:union(other)
return new
end
function locset_meta:__band(other)
local new = self:clone()
new:inter(other)
return new
end
function locset_meta:__bxor(other)
local new = self:clone()
new:symm(other)
return new
end
function locset_meta:__sub(other)
local new = self:clone()
new:diff(other)
return new
end
function locset_meta:__tostring()
local res = {}
self:iter(function(x, y, v) res[string.format('(%d,%d)', x, y)] = tostring(v) end)
return '{' .. stringx.join_map(res, '; ', ' = ') .. '}'
end
function methods:empty()
return (not next(self.values))
end
function methods:size()
local sz = 0
for p,v in pairs(self.values) do
sz = sz + 1
end
return sz
end
function methods:clear()
self.values = {}
end
function methods:get(...)
local loc = wesnoth.map.read_location(...)
return self.values[index(loc.x, loc.y)]
end
function methods:insert(...)
local loc, n = wesnoth.map.read_location(...)
local v = select(n + 1, ...)
self.values[index(loc.x, loc.y)] = v or true
end
function methods:remove(...)
local loc = wesnoth.map.read_location(...)
self.values[index(loc.x, loc.y)] = nil
end
function methods:clone()
local new = location_set.create()
for p,v in pairs(self.values) do
new.values[p] = v
end
return new
end
function methods:union(s)
local values = self.values
for p,v in pairs(s.values) do
values[p] = v
end
end
function methods:union_merge(s, f)
local values = self.values
for p,v in pairs(s.values) do
local x, y = revindex(p)
values[p] = f(x, y, values[p], v)
end
end
function methods:inter(s)
local values = self.values
local nvalues = {}
for p,v in pairs(s.values) do
nvalues[p] = values[p]
end
self.values = nvalues
end
function methods:inter_merge(s, f)
local values = s.values
local nvalues = {}
for p,v in pairs(self.values) do
local x, y = revindex(p)
nvalues[p] = f(x, y, v, values[p])
end
self.values = nvalues
end
function methods:diff(s)
local values = self.values
for p,v in pairs(s.values) do
values[p] = nil
end
end
function methods:symm(s)
local values = self.values
for p,v in pairs(s.values) do
if values[p] then
values[p] = nil
else
values[p] = v
end
end
end
function methods:filter(f)
local nvalues = {}
for p,v in pairs(self.values) do
local x, y = revindex(p)
if f(x, y, v) then nvalues[p] = v end
end
return setmetatable({ values = nvalues }, locset_meta)
end
function methods:iter(f)
if f == nil then
local locs = self
return coroutine.wrap(function()
locs:iter(coroutine.yield)
end)
end
for p,v in pairs(self.values) do
local x, y = revindex(p)
f(x, y, v)
end
end
function methods:stable_iter(f)
if f == nil then
local locs = self
return coroutine.wrap(function()
locs:stable_iter(coroutine.yield)
end)
end
local indices = {}
for p,v in pairs(self.values) do
table.insert(indices, p)
end
table.sort(indices)
for i,p in ipairs(indices) do
local x, y = revindex(p)
f(x, y, self.values[p])
end
end
function methods:of_pairs(t)
local values = self.values
for i,v in ipairs(t) do
local value_table = {}
local x_index
local y_index
if v.x and v.y then
x_index = "x"
y_index = "y"
else
x_index = 1
y_index = 2
end
for k,val in pairs(v) do
if k ~= x_index and k ~= y_index then
value_table[k] = val
end
end
if next(value_table) then
values[index(v[x_index], v[y_index])] = value_table
else
values[index(v[x_index], v[y_index])] = true
end
end
end
function methods:of_wml_var(name)
local values = self.values
for i = 0, wml.variables[name .. ".length"] - 1 do
local t = wml.variables[string.format("%s[%d]", name, i)]
local x, y = t.x, t.y
t.x, t.y = nil, nil
values[index(x, y)] = next(t) and t or true
end
end
function methods:of_triples(t)
-- Create a location set from a table of 3-element tables
-- Elements 1 and 2 are x,y coordinates, #3 is value to be inserted
for k,v in pairs(t) do
self:insert(v[1], v[2], v[3])
end
end
function methods:to_pairs()
local res = {}
self:iter(function(x, y) table.insert(res, { x, y }) end)
return res
end
function methods:to_stable_pairs()
local res = {}
self:stable_iter(function(x, y) table.insert(res, { x, y }) end)
return res
end
function methods:to_wml_var(name)
local i = 0
wml.variables[name] = nil
self:stable_iter(function(x, y, v)
if type(v) == 'table' then
wml.variables[string.format("%s[%d]", name, i)] = v
end
wml.variables[string.format("%s[%d].x", name, i)] = x
wml.variables[string.format("%s[%d].y", name, i)] = y
i = i + 1
end)
end
function methods:to_triples()
local res = {}
self:iter(function(x, y, v) table.insert(res, { x, y, v }) end)
return res
end
function methods:random()
-- Select a random hex from the hexes in the location set
-- This seems "inelegant", but I can't come up with another way without creating an extra array
-- Return -1, -1 if empty
local r = wesnoth.random(self:size())
local i, xr, yr = 1, -1, -1
self:iter( function(x, y, v)
if (i == r) then xr, yr = x, y end
i = i + 1
end)
return xr, yr
end
function location_set.create()
if wesnoth.get_map_size then
-- If called from the mapgen kernel, there's no map
local w,h,b = wesnoth.get_map_size()
assert(h + 2 * b < 9000)
end
return setmetatable({ values = {} }, locset_meta)
end
function location_set.of_pairs(t)
local s = location_set.create()
s:of_pairs(t)
return s
end
function location_set.of_wml_var(name)
local s = location_set.create()
s:of_wml_var(name)
return s
end
function location_set.of_triples(t)
local s = location_set.create()
s:of_triples(t)
return s
end
return location_set