-
Notifications
You must be signed in to change notification settings - Fork 2
/
px9_decomp.lua
76 lines (63 loc) · 1.1 KB
/
px9_decomp.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
function px9_decomp(x0,y0,src)
local function vlist_val(l, val)
for i=1,#l do
if l[i]==val then
for j=i,2,-1 do
l[j]=l[j-1]
end
l[1] = val
return i
end
end
end
local cache,cache_bits=0,0
function getval(bits)
if cache_bits<16 then
cache+=lshr(peek2(src),16-cache_bits)
cache_bits+=16
src+=2
end
local val=lshr(shl(cache,32-bits),16-bits)
cache=lshr(cache,bits)
cache_bits-=bits
return val
end
function gn1()
local bits,tot=1,1
while 1 do
local mx,vv=2^bits-1,getval(bits)
tot+=vv
bits+=1
if (vv<mx) return tot
end
end
local w,h,b,el,pr,x,y,splen,mode = gn1(),gn1(),gn1(),{},{},0,0,0
for i=1,gn1() do
add(el,getval(b))
end
for y=y0,y0+h-1 do
for x=x0,x0+w-1 do
splen-=1
if splen<1 then
splen,mode=gn1(),not mode
end
local a= y>y0 and sget(x,y-1) or 0
local l=pr[a]
if not l then
l={}
for e in all(el) do
add(l,e)
end
pr[a]=l
end
local idx=mode and 1 or gn1()+1
local v=l[idx]
vlist_val(l, v)
vlist_val(el, v)
sset(x,y,v)
x+=1
y+=flr(x/w)
x%=w
end
end
end