--!name Depth Charge
--!icon game
--!category Games
local v=snail.touchscreen==true local V,L=14,14 local F=7 local ad={3,4,6,7,9,10,12}local ac,W={5,8,11},{1,-1,1}local at={{2,3,3},{1,2,2}}local as,au,aq=1,2,13 local ab=1 local aa={{10,2,"up",true}}local ar={"..^!..","..++..","<++++>","=++++?","..++..","..~|.."}local m,K="menu",1 local o,U,E={},{},{}local f,h,H={},{},{}local k,j,t,Q=3,0,3,500 local n,z=0,0 local y,l,P,D,Z=false,1,nil,0,false local r={0,0}local T=1 local function ao(Y)T=(T*1664525+1013904223)&0xFFFFFFFF return(T>>16)%Y end local function X()if l==1 then return math.max(500,800-j//5)end return math.max(450,600-j//5)end local function aI()snail.save(r[1]..","..r[2])end local function s(aC,c,b)for g,ap in ipairs(aC)do if ap[1]==c and ap[2]==b then return g end end end local function aG()if os and os.time then T=os.time()%100000+11 end for a=1,3 do o[a],U[a],E[a]=false,false,0 end f,h,H={},{},{}k,j,t,Q=3,0,3,500 n,z=0,1 y,P,D=false,nil,0 m="play"snail.tick(X())end local function aH()m="over"snail.tick(0)Z=j>r[l]if Z then r[l]=j aI()end end local function av(Y)j=math.min(9999,j+Y)if j>=Q then Q=Q+500 if t<3 then t=t+1 P,D="BONUS SHIP",2 end end snail.tick(X())end local function aJ()t=t-1 n,f,h=3,{},{}end local function aF()for a=1,3 do U[a]=o[a]if o[a]then E[a]=E[a]+1 if E[a]>=at[l][a]then local c=o[a]+W[a]o[a],E[a]=c>=0 and c<F and c,0 end end end end local function aD()local x={}for G,N in ipairs(f)do local c,b=N[1],N[2]local a=b%2==0 and b//2 or 0 if a>0 and(o[a]==c or U[a]==c)then o[a],U[a]=false,false H[#H+1]={c,ac[a]}av(a*10)elseif b<#ad then local g=s(h,c,b+1)if g then table.remove(h,g)else x[#x+1]={c,b+1}end end end f=x end local function aE()local x={}for G,am in ipairs(h)do local c,b=am[1],am[2]-1 if b<1 then if c==k and n==0 then aJ()end else local g=s(f,c,b)if g then table.remove(f,g)else x[#x+1]={c,b}end end end if n==0 then h=x end end local function aB()local aK=math.min(35,12+(l-1)*10+j//50)local az=math.min(20,7+(l-1)*4+j//100)for a=1,3 do if not o[a]then if ao(100)<aK then o[a],E[a]=W[a]>0 and 0 or F-1,0 end elseif#h<l+1 and ao(100)<az then local c,b=o[a],2*a if not s(h,c,b)then local g=s(f,c,b)if g then table.remove(f,g)else h[#h+1]={c,b}end end end end end function tick()if m~="play"or y then snail.tick(0)return end H={}if z>0 then z=z-1 return end if D>0 then D=D-1 if D==0 then P=nil end end if n>0 then n=n-1 if n==0 and t==0 then aH()end return end aF()aD()aE()if n==0 then aB()end end local function ah()return m=="play"and not y and z==0 and n==0 end local function ai()if#f>=2 or s(f,k,1)then return end local g=s(h,k,1)if g then table.remove(h,g)return end f[#f+1]={k,1}end function key(i)if m=="menu"then if i=="up"or i=="down"then K=3-K elseif i=="ok"then l=K aG()end return end if m=="over"then if i=="ok"then m="menu"end return end if i=="ok"then y=not y snail.tick(y and 0 or X())return end if not ah()then return end if i=="left"then k=math.max(0,k-1)return true elseif i=="right"then k=math.min(F-1,k+1)return true elseif i=="up"or i=="down"then ai()end end local function ax(q,C)local d,e=C-L-1,q-1 if d<0 then return nil end local O,aj=2*(e-ab)-5,2*d-5 if O>=-7 and O<=7 then local ae,af=math.abs(O),math.abs(aj)if ae>af then return O<0 and"left"or"right"end if af>ae then return aj<0 and"up"or"down"end return nil end for G,w in ipairs(aa)do if e>=w[1]and e<=w[1]+1 then return w[3]end end end function touch(aA,q,C)if aA~="board"then return end if v then local i=ax(q,C)if i then return key(i)end else q=q-1 end if C<=L and ah()then local c=math.min(F-1,(q-1)//2)if c==k then ai()else k=c end end end local function ay()local p={}for d=1,6 do p[d]={}for e=1,V do p[d][e]="."end for b=1,6 do p[d][ab+b]=ar[d]:sub(b,b)end end for G,w in ipairs(aa)do local R,d,e=w[4]and"()[]"or"$&;:",w[2]+1,w[1]+1 p[d][e],p[d][e+1]=R:sub(1,1),R:sub(2,2)p[d+1][e],p[d+1][e+1]=R:sub(3,3),R:sub(4,4)end for d=1,6 do p[d]=table.concat(p[d])end return p end local function aL(I)local J={}for d=0,L-1 do local A=table.concat(I[d])J[#J+1]=v and A or"."..A.."."end if v then for G,S in ipairs(ay())do J[#J+1]=S end end return table.concat(J,"/")end local function aw()local B=z>0 local I={}for d=0,L-1 do local S={}for e=1,V do S[e]="."end I[d]=S end local function u(e,d,N)I[d][e+1]=N end local A=B and"8888"or string.format("%4d",j):gsub(" ","_")for b=1,4 do u(b-1,0,A:sub(b,b))end for b=1,3 do u(10+b,0,(B or t>=b)and"p"or"P")end for e=0,V-1 do u(e,au,"s")u(e,aq,"b")end for c=0,F-1 do local _=2*c u(_,as,(B or k==c)and(n>0 and"w"or"p")or"P")for b,aM in ipairs(ad)do local e,al=B or s(f,c,b),B or s(h,c,b)u(_,aM,e and(al and"n"or"c")or(al and"m"or"k"))end for a=1,3 do local an=W[a]>0 u(_,ac[a],(B or o[a]==c)and(an and"q"or"r")or(an and"Q"or"R"))end end for G,ag in ipairs(H)do u(2*ag[1],ag[2],"x")end return aL(I)end function draw()local ak=string.format("GAME %s  BEST %d",l==1 and"A"or"B",r[l])snail.title((v and m~="menu")and"Depth Charge  "..ak or"Depth Charge")if m=="menu"then snail.row("Game A",K==1)snail.row("Game B (faster)",K==2)snail.gap()snail.text(string.format("Best: A %d   B %d",r[1],r[2]))snail.gap()snail.small("Sink the submarines with depth charges: 10, 20 and")snail.small("30 points, the deepest worth the most.")if v then snail.small("Steer with the d-pad or tap a lane; the round button,")snail.small("or a tap on your own lane, drops a charge.")else snail.small("LEFT and RIGHT steer, UP or DOWN drops a charge.")end snail.small("Mines rising under your ship sink it; a charge stops")snail.small("a mine. Two charges at a time, three ships.")snail.hint("OK start BACK leave")return end if not v then snail.small(ak)end local M=P if z>0 then M="GAME "..(l==1 and"A"or"B")end if y then M="PAUSED"end if m=="over"then M=string.format("GAME OVER  %d%s",j,Z and"  BEST!"or"")end snail.board(aw(),nil,M)if m=="over"then snail.hint("OK menu BACK leave")elseif v then snail.hint("OK pause BACK leave")else snail.hint("L left R right OK pause BACK leave")end end function start()snail.ink("fast")local A=snail.load()if A then local q,C=A:match("(%d+),(%d+)")r={tonumber(q)or 0,tonumber(C)or 0}end end
