--!name Stackfall
--!icon game
--!category Games
local aw="!$%&()+,-0123456789:;<=>"local ad,ax,az,ay=0,1,11,23 local function D(b)return aw:sub(b+1,b+1)end local f,q=12,18 local aN="IOTSZJL"local Y={{4,0,1,1,1,2,1,3,1},{2,0,0,1,0,0,1,1,1},{3,1,0,0,1,1,1,2,1},{3,1,0,2,0,0,1,1,1},{3,0,0,1,0,1,1,2,1},{3,0,0,0,1,1,1,2,1},{3,2,0,0,1,1,1,2,1},}local X={0,0,-1,0,1,0,0,-1,-2,0,2,0}local aA={10,25,45,70}local u={}local i,m,l,j=1,0,3,0 local t,r={},{}local n,A,y,G=0,0,1,1 local L,J,v=0,0,{}local z,k,s,B=false,false,nil,false local U=false local o,p="menu",1 local aE=1000 local function Z(P,K,c)local h=Y[P]local g,e,a=h[1],h[2*c],h[2*c+1]for ae=1,K%4 do e,a=g-1-a,e end return e,a end local function E(P,K,aL,aM)for c=1,4 do local e,a=Z(P,K,c)e,a=aL+e,aM+a if e<0 or e>=f or a>=q then return false end if a>=0 and u[a*f+e+1]~=0 then return false end end return true end local function at()return math.max(450,1000-55*(y-1))end local function I()if o=="play"and B and not k then aE=snail.tick(at())or at()else snail.tick(0)end end local function aa()if#r==0 then for b=1,7 do r[b]=b end for b=7,2,-1 do local al=math.random(b)r[b],r[al]=r[al],r[b]end end return table.remove(r)end local function F()local av={}for b=1,f*q do av[b]=u[b]end snail.save(table.concat({"F2",n,A,y,G,L,J,table.concat(v,","),B and 1 or 0,k and 1 or 0,i,m,l,j,table.concat(t),table.concat(r),table.concat(av)},";"))end local function aG()local h=snail.load()if type(h)~="string"then return end local w={}for Q in(h..";"):gmatch("([^;]*);")do w[#w+1]=Q end local g=function(b)return tonumber(w[b])or 0 end if w[1]=="F1"then J=g(7);return end if w[1]~="F2"then return end n,A,y,G,L,J=g(2),g(3),math.max(1,g(4)),math.max(1,g(5)),g(6),g(7)for Q in(w[8]or""):gmatch("%d+")do v[#v+1]=tonumber(Q)end local R=w[17]or""if g(9)~=1 or#R~=f*q then return end for b=1,f*q do u[b]=R:byte(b)-48 end i,m,l,j=math.max(0,math.min(7,g(11))),g(12),g(13),g(14)t[1]=tonumber((w[15]or""):match("%d"))for M in(w[16]or""):gmatch("%d")do r[#r+1]=tonumber(M)end t[1]=t[1]or aa()B,k=true,g(10)==1 end local function ar()U=n>L if U then L=n end v[#v+1]=n table.sort(v,function(H,R)return H>R end)while#v>5 do table.remove(v)end end local function V()i=table.remove(t,1)t[#t+1]=aa()m,z=0,false l=(f-Y[i][1])//2 j=i==1 and-1 or 0 if not E(i,m,l,j)then k=true ar()I()end end local function ap()for b=1,f*q do u[b]=0 end t,r={},{}t[1]=aa()n,A,y=0,0,G k,s,B,U=false,nil,true,false J=J+1 o="play"V()I()F()end local function ak()local d={}for a=0,q-1 do local aj=true for e=1,f do if u[a*f+e]==0 then aj=false;break end end if aj then d[#d+1]=a end end return d end local function ai()local O={}for ae,a in ipairs(s)do O[a]=true end local N=q-1 for a=q-1,0,-1 do if not O[a]then if N~=a then for e=1,f do u[N*f+e]=u[a*f+e]end end N=N-1 end end for a=N,0,-1 do for e=1,f do u[a*f+e]=0 end end s=nil V()F()end local function am()for c=1,4 do local e,a=Z(i,m,c)e,a=l+e,j+a if a<0 then k=true ar()I()F()return end u[a*f+e+1]=i end local C=ak()if#C>0 then n=n+aA[#C]*y A=A+#C local an=math.max(G,A//10+1)if an~=y then y=an;I()end s=C i=0 F()else V()F()end end local function aD()local a=j while E(i,m,l,a+1)do a=a+1 end return a end local function as(ah)if E(i,m,l+ah,j)then l=l+ah if z and E(i,m,l,j+1)then z=false end end end local function au(aC)local K=(m+aC)%4 for b=1,#X,2 do local e,a=l+X[b],j+X[b+1]if E(i,K,e,a)then m,l,j=K,e,a if z and E(i,m,l,j+1)then z=false end return end end end function tick()if o~="play"or k or not B then return end if s then ai();return end if E(i,m,l,j+1)then j=j+1 z=false elseif z then am()else z=true end end local function ao()local d={}if B and not k then d[#d+1]={"Resume","resume"}end d[#d+1]={"New game","new"}d[#d+1]={"Start at level "..G.."  (L/R)","level"}d[#d+1]={"High scores","scores"}d[#d+1]={"How to play","help"}return d end local function aI()o,p="menu",1 I()F()end function key(c)if o=="menu"then local x=ao()if p>#x then p=#x end local H=x[p][2]if c=="up"then p=(p-2)%#x+1 elseif c=="down"then p=p%#x+1 elseif H=="level"and(c=="left"or c=="right"or c=="ok")then G=(G-1+(c=="left"and-1 or 1))%10+1 F()elseif c=="ok"then if H=="resume"then o="play";I()elseif H=="new"then ap()else o=H end end return end if o~="play"then if c=="top"or c=="ok"then o="menu"end return end if c=="top"then aI();return end if k then if c=="ok"then ap()end return end if s then ai()end if k then return end if c=="left"then as(-1)elseif c=="right"then as(1)elseif c=="up"then au(1)elseif c=="ok"then au(-1)elseif c=="down"then local a=aD()n=n+a-j j=a am()end end local W="....."local function _(g)local h=tostring(g)local c=false if#h>5 then h,c=tostring(g//1000),true end local d={}for b=1,#h do d[b]=D(ax+h:byte(b)-48)end if c then d[#d+1]=D(ay)end local aq=(5-#d)//2 return("."):rep(aq)..table.concat(d)..("."):rep(5-aq-#d)end local function T(b)local ac=az+3*b return"."..D(ac)..D(ac+1)..D(ac+2).."."end local function aJ(P,aK)local h=Y[P]local ab=9 for c=1,4 do ab=math.min(ab,h[2*c+1])end local d={".",".",".",".","."}for c=1,4 do if h[2*c+1]-ab==aK then d[h[2*c]+2]=D(ad)end end return table.concat(d)end local function aH(a)if a==0 then return T(0)end if a==1 or a==2 then return t[1]and aJ(t[1],a-1)or W end if a==4 then return T(1)end if a==5 then return _(n)end if a==7 then return T(2)end if a==8 then return _(y)end if a==10 then return T(3)end if a==11 then return _(A)end return W end local function aB()local af={}if i>0 and not k then for c=1,4 do local e,a=Z(i,m,c)af[(j+a)*f+l+e+1]=true end end local O={}if s then for ae,a in ipairs(s)do O[a]=true end end local C={}for a=0,q-1 do local d={"#"}for e=1,f do local b=a*f+e local M="."if O[a]then M="*"elseif af[b]or u[b]~=0 then M=D(ad)end d[e+1]=M end d[f+2]="#"d[f+3]=aH(a)C[a+1]=table.concat(d)end C[q+1]=("#"):rep(f+2)..W return table.concat(C,"/")end local function S(g)local h=tostring(g)while true do local d,c=h:gsub("^(%d+)(%d%d%d)","%1,%2")h=d if c==0 then return h end end end function draw()if o=="menu"then snail.title("Stackfall")local x=ao()if p>#x then p=#x end for b,aF in ipairs(x)do snail.row(aF[1],b==p)end snail.gap()if B and not k then snail.small("Paused: score "..S(n)..", level "..y..", "..A.." lines.")end snail.small("Best score "..S(L)..".")snail.hint("BACK leave OK choose UP/DN move")return end if o=="scores"then snail.title("High scores")if#v==0 then snail.text("No games finished yet.")end for b,Q in ipairs(v)do snail.text(b..".  "..S(Q))end snail.gap()snail.small("Games played: "..J..".")snail.hint("BACK back OK back")return end if o=="help"then snail.title("How to play")snail.text("Pieces fall one row at a time. Fill a whole row to clear it.")snail.text("LEFT and RIGHT move. UP turns clockwise, OK spins the other way.")snail.text("DOWN drops the piece straight to the bottom.")snail.text("A piece that lands by itself waits a tick: slide it under a ledge.")snail.text("1-4 rows at once: 10, 25, 45, 70 points times the level.")snail.text("Every 10 rows the level goes up and pieces fall faster.")snail.text("BACK pauses the game. The panel shows the next piece.")snail.hint("BACK back OK back")return end snail.title("Stackfall")local ag if k then ag=(U and"New best! "or"Game over - ")..S(n)end snail.board(aB(),nil,ag)if k then snail.hint("BACK options OK play again")else snail.hint("BACK pause OK spin L/R move UP turn DN drop")end end function start()math.randomseed(os.time())snail.ink("fast")for b=1,f*q do u[b]=0 end aG()if B and not k and i==0 then s=ak()if#s==0 then s=nil;V()end end o,p="menu",1 snail.tick(0)end
