-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.lua
248 lines (138 loc) · 4.32 KB
/
generator.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
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local table = _tl_compat and _tl_compat.table or table; require("common")
require("cmn")
require("nbtypes")
local inspect = require("inspect")
local serpent = require("serpent")
local logfile = love.filesystem.newFile("log-generator.txt", "w")
local GenFunction = {}
local CmpFunction = {}
local function generate(
sig_count,
level,
gen,
cmp)
local ret = {}
local ratio = 5
local range = { 1, 3 }
local null = {}
local retLen = ratio * sig_count
for _ = 1, retLen do
table.insert(ret, null)
end
logfile:write('retLen: ' .. tonumber(retLen) .. '\n')
logfile:write('empty ret: ' .. inspect(ret) .. '\n')
for i = 1, retLen do
if i < 5 then
ret[i] = gen()
end
end
logfile:write('ret after: ' .. serpent.block(ret) .. '\n')
return ret
end
local function make_hit_arr(signals, level, comparator)
local ret = {}
if signals then
for k, v in ipairs(signals) do
ret[#ret + 1] = k > level and comparator(v, signals[k - level])
end
else
error('Something gone wrong, signals is nil')
end
return ret
end
local function makeEqArrays(signals, level)
local ret = {
pos = make_hit_arr(signals.pos, level, isPositionEqual),
sound = make_hit_arr(signals.sound, level,
function(a, b)
return a == b
end),
color = make_hit_arr(signals.color, level, function(a, b)
return a == b
end),
form = make_hit_arr(signals.form, level, function(a, b)
return a == b
end),
}
logfile:write("makeEqArrays: " .. inspect(ret) .. "\n")
return ret
end
function generateAll(sig_count, level, dim, soundsNum, map)
print('generateAll()')
print('sig_count:', sig_count)
print('level:', level)
print('dim:', dim)
print('soundsNum', soundsNum)
print('map:', inspect(map))
local colorArr = require("colorconstants").makeNamesArray()
function genArrays(sig_count, level, _, soundsNum)
local signals = {}
logfile:write("--begin pos\n")
signals.pos = generate(sig_count, level,
function()
local result = {}
local x, y = math.random(1, #map), math.random(1, #map[1])
print('x, y', x, y)
result.x, result.y = x, y
return result
end,
function(a, b)
return a.x == b.x and a.y == b.y
end)
logfile:write("--end pos\n\n")
logfile:write("--begin form\n")
signals.form = generate(sig_count, level,
function()
local arr = { "trup", "trdown", "trupdown", "quad", "circle", "rhombus" }
return arr[math.random(1, 6)]
end,
function(a, b)
return a == b
end)
logfile:write("--end form\n\n")
logfile:write("--begin sound\n")
signals.sound = generate(sig_count, level,
function()
return math.random(1, soundsNum)
end,
function(a, b)
return a == b
end)
logfile:write("--end sound\n\n")
logfile:write("--begin color\n")
signals.color = generate(sig_count, level,
function() return colorArr[math.random(1, 6)] end,
function(a, b)
return a == b
end)
logfile:write("--end color\n\n")
signals.eq = makeEqArrays(signals, level)
return signals
end
function balance(forIterCount)
local i, changed = 0, false
local signals
repeat
i = i + 1
signals = genArrays(sig_count, level, dim, soundsNum)
for k, v in ipairs(signals.eq.pos) do
local n = 0
n = n + (v and 1 or 0)
n = n + (signals.eq.sound[k] and 1 or 0)
n = n + (signals.eq.form[k] and 1 or 0)
n = n + (signals.eq.color[k] and 1 or 0)
if n > 2 then
changed = true
end
end
until i >= forIterCount or not changed
return signals
end
local result = balance(1)
logfile:close()
return result
end
return {
makeEqArrays = makeEqArrays,
generateAll = generateAll,
}