-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
79 lines (66 loc) · 1.97 KB
/
main.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
local data = { data = {} }
for line in io.lines() do
local field = "data"
local root = data
local i = 1
while true do
::continue::
local field_start, field_end = line:find("^%.[^.=[]*", i)
if field_start ~= nil then
if root[field] == nil then
root[field] = {}
end
root = root[field]
field = line:sub(field_start + 1, field_end)
i = field_end + 1
goto continue
end
local array_start, array_end = line:find("^%[%d*%]", i)
if array_start ~= nil then
if root[field] == nil then
root[field] = {}
end
root = root[field]
field = tonumber(line:sub(array_start + 1, array_end - 1))
i = array_end + 1
goto continue
end
local value_start, value_end = line:find("^=.*$", i)
if value_start ~= nil then
root[field] = line:sub(value_start + 1, value_end)
else
print("Error")
end
break
end
end
local root = data.data
-- `root` does not contain anything lua considers an array, so we cannot do
-- `items[math.random(#items)]`. To workaround this, we construct actualy arrays for the maps we
-- wonna access random values from.
local starters = {}
for k, _ in pairs(root.starters) do
table.insert(starters, k)
end
local pokemons = {}
for k, _ in pairs(root.pokemons) do
table.insert(pokemons, k)
end
for _, k in pairs(starters) do
root.starters[k] = pokemons[math.random(#pokemons)]
print(k, root.starters[k])
end
function output(prefix, value)
if type(value) == "table" then
for k, v in pairs(value) do
if type(k) == "number" then
output(prefix .. "[" .. tostring(k) .. "]", v)
else
output(prefix .. "." .. k, v)
end
end
else
io.write(prefix, "=", value, "\n")
end
end
output("", root)