-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdata.lua
418 lines (384 loc) · 10 KB
/
data.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
local floor=math.floor
local char,byte=string.char,string.byte
local ins=table.insert
local BAG,FIELD,MISSION,CUSTOMENV,GAME=BAG,FIELD,MISSION,CUSTOMENV,GAME
local DATA={}
-- Sep symbol: 33 (!)
-- Safe char: 34~126
--[[
Count: 34~96
Block: 97~125
Encode: A[B] sequence, A = block ID, B = repeat times, no B means do not repeat.
Example: "abcdefg" is [SZJLTOI], "a^aDb)" is [Z*63,Z*37,S*10]
]]
function DATA.copySequence()
local str=""
local count=1
for i=1,#BAG+1 do
if BAG[i+1]~=BAG[i] or count==64 then
str=str..char(96+BAG[i])
if count>1 then
str=str..char(32+count)
count=1
end
else
count=count+1
end
end
return str
end
function DATA.pasteSequence(str)
TABLE.cut(BAG)
local b,reg
for i=1,#str do
b=byte(str,i)
if not reg then
if b>=97 and b<=125 then
reg=b-96
else
return
end
else
if b>=97 and b<=125 then
ins(BAG,reg)
reg=b-96
elseif b>=34 and b<=96 then
for _=1,b-32 do
ins(BAG,reg)
end
reg=false
end
end
end
if reg then
ins(BAG,reg)
end
return true
end
local fieldMeta={__index=function(self,h)
for i=#self+1,h do
self[i]={0,0,0,0,0,0,0,0,0,0}
end
return self[h]
end}
function DATA.newBoard(f)-- Generate a new board
return setmetatable(f and TABLE.shift(f) or{},fieldMeta)
end
function DATA.copyBoard(page)-- Copy the [page] board
local F=FIELD[page or 1]
local str=""
-- Encode field
for y=1,#F do
local S=""
local L=F[y]
for x=1,10 do
S=S..char(L[x]+1)
end
str=str..S
end
return STRING.packBin(str)
end
function DATA.copyBoards()
local out={}
for i=1,#FIELD do
out[i]=DATA.copyBoard(i)
end
return table.concat(out,"!")
end
function DATA.pasteBoard(str,page)-- Paste [str] data to [page] board
if not page then
page=1
end
if not FIELD[page] then
FIELD[page]=DATA.newBoard()
end
local F=FIELD[page]
-- Decode
str=STRING.unpackBin(str)
if not str then return end
local fX,fY=1,1-- *ptr for Field(r*10+(c-1))
local p=1
while true do
local b=byte(str,p)-- 1byte
-- Str end
if not b then
if fX~=1 then
return
else
break
end
end
local id=b%32-1-- Block id
if id>26 then return end-- Illegal blockid
b=floor(b/32)-- Mode id
F[fY][fX]=id
if fX<10 then
fX=fX+1
else
fY=fY+1
if fY>60 then break end
fX=1
end
p=p+1
end
return true
end
--[[
Mission: 34~114
Count: 115~126
Encode: [A] or [AB] sequence, A = mission ID, B = repeat times, no B means do not repeat.
_1=01,_2=02,_3=03,_4=04,
A1=05,A2=06,A3=07,A4=08,
PC=09,
Z1=11,Z2=12,Z3=13,
S1=21,S2=22,S3=23,
J1=31,J2=32,J3=33,
L1=41,L2=42,L3=43,
T1=51,T2=52,T3=53,
O1=61,O2=62,O3=63,O4=64,
I1=71,I2=72,I3=73,I4=74,
]]
function DATA.copyMission()
local _
local str=""
local count=1
for i=1,#MISSION+1 do
if MISSION[i+1]~=MISSION[i] or count==13 then
_=33+MISSION[i]
str=str..char(_)
if count>1 then
str=str..char(113+count)
count=1
end
else
count=count+1
end
end
return str
end
function DATA.pasteMission(str)
local b
TABLE.cut(MISSION)
local reg
for i=1,#str do
b=byte(str,i)
if not reg then
if b>=34 and b<=114 then
reg=b-33
else
return
end
else
if b>=34 and b<=114 then
if ENUM_MISSION[reg] then
ins(MISSION,reg)
reg=b-33
else
TABLE.cut(MISSION)
return
end
elseif b>=115 and b<=126 then
for _=1,b-113 do
ins(MISSION,reg)
end
reg=false
end
end
end
if reg then
ins(MISSION,reg)
end
return true
end
function DATA.copyQuestArgs()
local ENV=CUSTOMENV
local str=""..
ENV.holdCount..
(ENV.ospin and "O" or "Z")..
(ENV.missionKill and "M" or "Z")..
ENV.sequence
return str
end
function DATA.pasteQuestArgs(str)
if #str<4 then return end
local ENV=CUSTOMENV
ENV.holdCount= str:byte(1)-48
ENV.ospin= str:byte(2)~=90
ENV.missionKill=str:byte(3)~=90
ENV.sequence= str:sub(4)
return true
end
--[[
Replay file:
a zlib-compressed json table
Replay data format (table):
{frame,event, frame,event, ...}
Replay data format (byte): (1 byte each period)
dt, event, dt, event, ...
all data range from 0 to 127
large value will be encoded as 1xxxxxxx(high)-1xxxxxxx-...-0xxxxxxx(low)
Example (decoded):
6,1, 20,-1, 0,2, 26,-2, 872,4, ...
This means:
Press key1 at 6f
Release key1 at 26f (6+20)
Press key2 at the same time (26+0)
Release key 2 after 26 frame (26+26)
Press key 4 after 872 frame (52+872)
...
]]
function DATA.dumpRecording(list,ptr)
local out=""
local buffer,buffer2=""
if not ptr then ptr=1 end
local prevFrm=list[ptr-2] or 0
while list[ptr] do
-- Flush buffer
if #buffer>10 then
out=out..buffer
buffer=""
end
-- Encode time
local t=list[ptr]-prevFrm
prevFrm=list[ptr]
if t>=128 then
buffer2=char(t%128)
t=floor(t/128)
while t>=128 do
buffer2=char(128+t%128)..buffer2
t=floor(t/128)
end
buffer=buffer..char(128+t)..buffer2
else
buffer=buffer..char(t)
end
-- Encode event
t=list[ptr+1]
if t>=128 then
buffer2=char(t%128)
t=floor(t/128)
while t>=128 do
buffer2=char(128+t%128)..buffer2
t=floor(t/128)
end
buffer=buffer..char(128+t)..buffer2
else
buffer=buffer..char(t)
end
-- Step
ptr=ptr+2
end
return out..buffer,ptr
end
function DATA.pumpRecording(str,L)
local len=#str
local p=1
local curFrm=L[#L-1] or 0
local code
while p<=len do
-- Read delta time
code=0
local b=byte(str,p)
while b>=128 do
code=code*128+b-128
p=p+1
b=byte(str,p)
end
curFrm=curFrm+code*128+b
L[#L+1]=curFrm
p=p+1
local event=0
b=byte(str,p)
while b>=128 do
event=event*128+b-128
p=p+1
b=byte(str,p)
end
L[#L+1]=event*128+b
p=p+1
end
end
do-- function DATA.saveReplay()
local noRecList={"custom","solo","round","techmino"}
local function _getModList()
local res={}
for _,v in next,GAME.mod do
if v.sel>0 then
ins(res,{v.no,v.sel})
end
end
return res
end
function DATA.saveReplay()
-- Filtering modes that cannot be saved
for _,v in next,noRecList do
if GAME.curModeName:find(v) then
MES.new('error',"Cannot save recording of this mode now!")
return
end
end
-- Write file
local fileName=os.date("replay/%Y_%m_%d_%H%M%S.rep")
if not love.filesystem.getInfo(fileName) then
love.filesystem.write(fileName,
love.data.compress('string','zlib',
JSON.encode{
date=os.date("%Y/%m/%d %H:%M:%S"),
mode=GAME.curModeName,
version=VERSION.string,
player=USERS.getUsername(USER.uid),
seed=GAME.seed,
setting=GAME.setting,
mod=_getModList(),
tasUsed=GAME.tasUsed,
}.."\n"..
DATA.dumpRecording(GAME.rep)
)
)
ins(REPLAY,1,DATA.parseReplay(fileName))
return true
else
MES.new('error',"Save failed: File already exists")
end
end
end
function DATA.parseReplay(fileName,ifFull)
local fileData
-- Read file
fileData=love.filesystem.read(fileName)
return DATA.parseReplayData(fileName,fileData,ifFull)
end
function DATA.parseReplayData(fileName,fileData,ifFull)
local success,metaData
local rep={-- unavailable replay object
fileName=fileName,
available=false,
}
if not (fileData and #fileData>0) then return rep end-- goto BREAK_cannotParse
-- Decompress file
success,fileData=pcall(love.data.decompress,'string','zlib',fileData)
if not success then return rep end-- goto BREAK_cannotParse
-- Load metadata
metaData,fileData=STRING.readLine(fileData)
metaData=JSON.decode(metaData)
if not metaData then return rep end-- goto BREAK_cannotParse
-- Convert ancient replays
metaData.mode=MODE_UPDATE_MAP[metaData.mode] or metaData.mode
if not MODES[metaData.mode] then return rep end-- goto BREAK_cannotParse
-- Create replay object
rep={
fileName=fileName,
available=true,
date=metaData.date,
mode=metaData.mode,
version=metaData.version,
player=metaData.player,
seed=metaData.seed,
setting=metaData.setting,
mod=metaData.mod,
tasUsed=metaData.tasUsed,
}
if ifFull then rep.data=fileData end
return rep
end
return DATA