-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
297 lines (240 loc) · 7.75 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
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
-- Doctor's Notepad
-- A project by DPS2004
--extensions to load
-- As default-config.lua now exists, this is not needed here anymore -- keeping it commented out instead of removing it for now
--[[
extensions = {
'core',
'row',
'room',
'decoration',
'color',
'sound',
'classybeat',
'conditional'
}
]]
-- helpful functions
function tget(t, f)
for i, v in ipairs(t) do
if f(v) then
return v
end
end
end
function tset(t, f, val)
for i, v in ipairs(t) do
if f(v) then
v = val
end
end
end
function setvalue(self, vname, beat, state)
beat = beat + level.eos
self.values[vname] = self.values[vname] or {}
for i, v in ipairs(self.values[vname]) do
if v.beat == beat then
table.remove(self.values[vname], i)
end
end
table.insert(self.values[vname], {beat = beat, state = state})
end
null = "_DN_NULL"
function getvalue(self, vname, beat)
beat = beat + level.eos
local matchbeat = 0
local matchval = nil
for i, v in ipairs(self.values[vname]) do
if v.beat >= matchbeat and v.beat <= beat then
matchbeat = v.beat
matchval = v.state
end
end
return matchval
end
function quoteifstring(v)
if type(v) == 'string' then return ('"' .. tostring(v) .. '"')
else return tostring(v)
end
end
local defaultstacklevel = 4
checkvar_override = false
-- utility error methods
function checkvar_throw(text, stackLevel)
error(text, stackLevel or defaultstacklevel)
end
-- method that checks if a variable is a certain type
-- n is the original variable name, a string
function checkvar_type(v, n, t, nilAccepted, stackLevel)
if checkvar_override then return end
if v == nil and nilAccepted then return end
local vt = type(v)
if vt ~= t then
checkvar_throw('invalid type exception: ' .. n .. ' is a ' .. vt .. ' but it should be a ' .. t, stackLevel)
end
end
shown_ease_warning = false
function checkvar_enum(v, n, enum, nilAccepted, stackLevel)
if checkvar_override then return end
if v == nil and nilAccepted then return end
if enum == enums.ease then
if enums.ease_lower[v] then
if not shown_ease_warning then
print('WARNING: camelCase for eases may not be supported for all events.')
shown_ease_warning = true
end
return
end
end
if not inenum(enum, v) then
checkvar_throw('invalid type exception: ' .. n .. ' is ' .. quoteifstring(v) .. ' but must be one of ' .. enum.__stringformat, stackLevel)
end
end
function checkvar_color(c, n, nilAccepted, stackLevel)
if checkvar_override then return end
if c == nil and nilAccepted then return end
stackLevel = stackLevel or defaultstacklevel
checkvar_type(c, n, 'string', false, stackLevel + 1)
if not c:match('^#?%x%x%x%x%x%x$') and not c:match('^#?%x%x%x%x%x%x%x%x$') then
checkvar_throw(n .. ' not in valid hexadecimal format: ' .. c, stackLevel)
end
end
local function isspecialrdcodevar(v)
return v:match('^[ifb]%-?%d+$')
end
local function isinttype(t)
return t == 'integer' or t == 'number'
end
local function isfloattype(t)
return t == 'float' or t == 'number'
end
local function isbooltype(t)
return t == 'boolean'
end
function checkvar_rdcodevar(c, n, t, nilAccepted, stackLevel)
if checkvar_override then return end
if c == nil and nilAccepted then return end
stackLevel = stackLevel or defaultstacklevel
checkvar_type(c, n, 'string', false, stackLevel + 1)
local ch = c:sub(1,1)
if isspecialrdcodevar(c) then
if #c - 1 > 1 then
checkvar_throw('the variable ' .. n .. ' (' .. c .. ') does not exist - only ' .. ch .. '0-' .. ch .. '9 is accepted', stackLevel)
end
if (ch == 'i' and not isinttype(t))
or (ch == 'f' and not isfloattype(t))
or (ch == 'b' and not isbooltype(t)) then
checkvar_throw('the variable ' .. n .. ' (' .. c .. ') is not a ' .. t, stackLevel)
end
end
end
function checkvar_validrdcodenum(v, n, nilAccepted, stackLevel)
if checkvar_override then return end
if v == nil and nilAccepted then return end
stackLevel = stackLevel or defaultstacklevel
if type(v) == 'string' then
checkvar_rdcodevar(v, n, 'number', false, stackLevel+1)
return
end
if type(v) ~= 'number' then
checkvar_throw(n .. ' should be a number or formula, but is a ' .. type(v), stackLevel)
end
end
function checkvar_validrdcodetype(v, n, nilAccepted, stackLevel)
if checkvar_override then return end
if v == nil and nilAccepted then return end
stackLevel = stackLevel or defaultstacklevel
if type(v) ~= 'number' and type(v) ~= 'string' and type(v) ~= 'boolean' then
checkvar_throw(n .. ' (' .. type(v) .. ') is not a valid rdcode type (number, formula or boolean)')
end
end
function checkvar_room(r, n, t, nilAccepted, stackLevel)
if checkvar_override then return end
if c == nil and nilAccepted then return end
stackLevel = stackLevel or defaultstacklevel
checkvar_type(r, n, 'number', false, stackLevel + 1)
if r < 0 or r > 3 then
checkvar_throw('the room ' .. r .. ' does not exist!', stackLevel)
end
end
function checkvar_typewithrdcode(v, n, t, ct, nilAccepted, stackLevel)
if checkvar_override then return end
stackLevel = stackLevel or defaultstacklevel
if type(v) == 'string' and v:match('^{.+}$') then
checkvar_rdcodevar(v:sub(2, -2), n, ct, nilAccepted, stackLevel + 1)
return
end
checkvar_type(v, n, t, nilAccepted, stackLevel + 1)
end
function disable_checkvar()
if checkvar_override then return end
print('Parameter safety checks disabled!')
checkvar_override = true
end
enums = {}
function create_enum(name, list)
local new_enum = {}
local builder = {}
for i, v in ipairs(list) do
new_enum[v] = i
builder[i] = quoteifstring(v)
end
new_enum.__stringformat = table.concat(builder, ', ')
enums[name] = new_enum
return new_enum
end
function inenum(enum, value)
if not type(enum) == 'table' then
error("invalid type exception: 'enum' is not an enum", 2)
end
return enum[value] ~= nil
end
function getenumvalueindex(enum, value)
if not type(enum) == 'table' then
error("invalid type exception: 'enum' is not an enum", 2)
end
return enum[value]
end
create_enum('ease', {
'Linear',
'InSine', 'OutSine', 'InOutSine',
'InQuad', 'OutQuad', 'InOutQuad',
'InCubic', 'OutCubic', 'InOutCubic',
'InQuart', 'OutQuart', 'InOutQuart',
'InQuint', 'OutQuint', 'InOutQuint',
'InExpo', 'OutExpo', 'InOutExpo',
'InCirc', 'OutCirc', 'InOutCirc',
'InElastic', 'OutElastic', 'InOutElastic',
'InBack', 'OutBack', 'InOutBack',
'InBounce', 'OutBounce', 'InOutBounce'
})
create_enum('ease_lower', {
'linear',
'inSine', 'outSine', 'inOutSine',
'inQuad', 'outQuad', 'inOutQuad',
'inCubic', 'outCubic', 'inOutCubic',
'inQuart', 'outQuart', 'inOutQuart',
'inQuint', 'outQuint', 'inOutQuint',
'inExpo', 'outExpo', 'inOutExpo',
'inCirc', 'outCirc', 'inOutCirc',
'inElastic', 'outElastic', 'inOutElastic',
'inBack', 'outBack', 'inOutBack',
'inBounce', 'outBounce', 'inOutBounce'
})
-- Load Libraries
json = require "lib/json" -- json parser
dpf = require "lib/dpf" -- functions for handling json files
logger = require "lib/log" -- logging
deeper = require "lib/deeper"
log = logger.log
rd = require "lib/rd" -- main doctor's notepad library
log("Doctor's Notepad")
inlevel = arg[1]
if not inlevel then
error("Usage: main.lua [level folder]")
end
level = rd.load(inlevel .. "/level.rdlevel")
level:init()
script = assert(loadfile(inlevel .. "/level.lua"))
script()
level:save(inlevel .. "/out.rdlevel")