-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxy_map.lua
86 lines (70 loc) · 2.2 KB
/
xy_map.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
xy_map = {}
-------------------------------------------------------------------------------
-- A collection that maps (x,y) coordinates to arbitrary objects
-------------------------------------------------------------------------------
function xy_map.newXYMap()
local xy_map = {}
xy_map.storage = {}
xy_map.size = 0
-- Same as add, but won't add anything if the space is already occupied
function xy_map:safeAdd(x, y, data)
if (not xy_map:contains(x, y)) then
xy_map:add(x, y, data)
end
end
function xy_map:add(x, y, data)
data = data or true
if (not xy_map.storage[x]) then
xy_map.storage[x] = {}
end
xy_map.storage[x][y] = data
xy_map.size = xy_map.size + 1
end
function xy_map:forEach(fun)
local index = 1
for x, yArray in pairs(xy_map.storage) do
for y, thing in pairs(yArray) do
assert (thing)
local result = fun(x, y, thing, index)
if (result) then
return result
end
index = index + 1
end
end
end
function xy_map:contains(x, y)
return (xy_map.storage[x] or {})[y]
end
function xy_map:get(x, y)
return (xy_map.storage[x] and xy_map.storage[x][y] or false)
end
function xy_map:remove(x, y)
if (xy_map.storage[x] or {})[y] then
xy_map.storage[x][y] = nil
xy_map.size = xy_map.size - 1
assert(xy_map.size >= 0)
-- clean up if xy_map[x] is empty
if (not xy_map.storage[x]) then
xy_map.storage[x] = nil
end
end
end
function xy_map:randomPosition()
local function newFunction()
local randomChoice = math.random(xy_map.size)
return function(x, y, _, index)
if (index == randomChoice) then
return {x = x, y = y}
end
end
end
return xy_map:forEach(newFunction())
end
function xy_map:clear()
xy_map.storage = {}
xy_map.size = 0
end
return xy_map
end
newXYMap = xy_map.newXYMap