--!name Deep Diver
--!icon game
--!category Games
local t=snail.touchscreen==true local E,F=14,15 local H={{2,1},{2,5},{2,9},{3,13},{5,13},{7,13},{9,13},{11,13}}local T={{{6,8},{5,9},{5,10},{5,11},{5,12}},{{7,8},{7,9},{7,10},{7,11},{7,12}},{{8,8},{9,9},{9,10},{9,11},{9,12}},{{9,7},{10,8},{11,9},{11,10},{11,11},{11,12}},}local G=5 local Y=4 local X={}local al={"..^!..","..++..","<++++>","=++++?","..++..","..~|.."}local k,D="menu",1 local h,l,B={0,0,0,0},{0,0,0,0},{0,0,0,0}local s,i,q,g=1,0,3,0 local L,p,x=false,0,0 local w,m,M,z,W=false,1,nil,0,false local r={0,0}local R=1 local function P(N)R=(R*1664525+1013904223)&0xFFFFFFFF return(R>>16)%N end local function V()if m==1 then return math.max(500,900-g*4)end return math.max(450,700-g*3)end local function aw()snail.save(r[1]..","..r[2])end local function au()if os and os.time then R=os.time()%100000+11 end for a=1,4 do h[a],l[a],B[a]=0,0,0 end s,i,q,g=1,0,3,0 L,p,x=false,0,1 w,M,z=false,nil,0 k="play"snail.tick(V())end local function av()k="over"snail.tick(0)W=g>r[m]if W then r[m]=g aw()end end local function am()local ad=g g=math.min(9999,g+i+(i==G and G or 0))i=0 if((ad<100 and g>=100)or(ad<300 and g>=300))and q<3 then q=q+1 M,z="BONUS  a diver back",2 end snail.tick(V())end local function ag(f)local a=f-4 return a>=1 and h[a]==#T[a]end local function ae()q=q-1 i=0 p=3 end local function ay()local U=0 for a=1,4 do local N=#T[a]if h[a]>0 or l[a]~=0 then U=U+1 end if l[a]==1 then h[a]=h[a]+1 if h[a]==N then l[a],B[a]=0,1+P(m==1 and 3 or 2)elseif h[a]>=2 and P(100)<15 then l[a]=-1 end elseif l[a]==-1 then h[a]=h[a]-1 if h[a]==0 then l[a]=0 end elseif h[a]==N then B[a]=B[a]-1 if B[a]<=0 then l[a]=-1 end end end local at=m==1 and(g<40 and 2 or 3)or(g<60 and 3 or 4)if U<at and P(100)<(m==1 and 45 or 60)then local a=P(4)+1 if h[a]==0 and l[a]==0 then l[a]=1 end end end function tick()if k~="play"or w then snail.tick(0)return end L=false if x>0 then x=x-1 return end if z>0 then z=z-1 if z==0 then M=nil end end if p>0 then p=p-1 if p==0 then if q==0 then av()else s=1 end end return end ay()if ag(s)then ae()end end local function _()return k=="play"and not w and x==0 and p==0 end local function ai()if not _()or s~=#H or L or i>=G then return end i=i+1 L=true end local function S(aq)if not _()then return end local f=s+aq if f>#H then return ai()end if f<1 then return end s=f if f==1 and i>0 then am()end if ag(f)then ae()end end function key(j)if k=="menu"then if j=="up"or j=="down"then D=3-D elseif j=="ok"then m=D au()end return end if k=="over"then if j=="ok"then k="menu"end return end if j=="ok"then w=not w snail.tick(w and 0 or V())return end if j=="left"then S(-1)elseif j=="right"then S(1)elseif j=="up"then ai()end end local function ao(o,y)local c,b=y-F-1,o-1 if c<0 then return nil end local K,ah=2*(b-Y)-5,2*c-5 if K>=-7 and K<=7 then local ab,ac=math.abs(K),math.abs(ah)if ab>ac then return K<0 and"left"or"right"end if ac>ab then return ah<0 and"up"or"down"end return nil end for Z,v in ipairs(X)do if b>=v[1]and b<=v[1]+1 then return v[3]end end end function touch(ar,o,y)if ar~="board"then return end if t then local j=ao(o,y)if j then return key(j)end else o=o-1 end if y<=F then local b=H[s][1]if o>b then S(1)elseif o<b then S(-1)end end end local function ap()local n={}for c=1,6 do n[c]={}for b=1,E do n[c][b]="."end for e=1,6 do n[c][Y+e]=al[c]:sub(e,e)end end for Z,v in ipairs(X)do local O,c,b=v[4]and"()[]"or"$&;:",v[2]+1,v[1]+1 n[c][b],n[c][b+1]=O:sub(1,1),O:sub(2,2)n[c+1][b],n[c+1][b+1]=O:sub(3,3),O:sub(4,4)end for c=1,6 do n[c]=table.concat(n[c])end return n end local function ax(A)local C={}for c=0,F-1 do local f=table.concat(A[c])C[#C+1]=t and f or"."..f.."."end if t then for Z,Q in ipairs(ap())do C[#C+1]=Q end end return table.concat(C,"/")end local function an()local u=x>0 local A={}for c=0,F-1 do local Q={}for b=1,E do Q[b]="."end A[c]=Q end local function d(b,c,J)A[c][b+1]=J end local f=u and"8888"or string.format("%4d",g):gsub(" ","_")for e=1,4 do d(e-1,0,f:sub(e,e))end for e=1,G do d(8+e,0,(u or i>=e)and"c"or"C")end d(1,1,(u or q>=2)and"d"or"D")d(0,1,(u or q>=3)and"d"or"D")d(0,2,"v");d(1,2,"m");d(2,2,"r")for b=3,E-1 do d(b,2,"w")end d(7,6,"p");d(8,6,"q");d(7,7,"y");d(8,7,"z")for a,aa in ipairs(T)do for e,af in ipairs(aa)do local J=e==#aa and"t"or"s"d(af[1],af[2],(u or h[a]>=e)and J or J:upper())end end for e,ak in ipairs(H)do local as=u or(s==e and(p==0 or p%2==1))d(ak[1],ak[2],as and(i>0 and not u and"e"or"d")or"D")end d(12,13,"k")for b=0,E-1 do d(b,14,"g")end return ax(A)end function draw()local aj=string.format("GAME %s  BEST %d",m==1 and"A"or"B",r[m])snail.title((t and k~="menu")and"Deep Diver  "..aj or"Deep Diver")if k=="menu"then snail.row("Game A",D==1)snail.row("Game B (faster)",D==2)snail.gap()snail.text(string.format("Best: A %d   B %d",r[1],r[2]))snail.gap()snail.small("Walk the sea floor to the chest, grab gold and bring it back")snail.small("to the boat. Keep clear of the octopus's tentacle tips.")if t then snail.small("Walk with the d-pad or tap either side of the diver.")snail.small("At the chest, the d-pad's top arm (or RIGHT) grabs a sack,")snail.small("one a beat, up to five.")else snail.small("LEFT and RIGHT walk. At the chest, RIGHT or the upper")snail.small("side button grabs a sack, one a beat, up to five.")end snail.small("Three divers lost and it's over. 100 and 300 bring one back.")snail.hint("OK start BACK leave")return end if not t then snail.small(aj)end local I=M if x>0 then I="GAME "..(m==1 and"A"or"B")end if w then I="PAUSED"end if k=="over"then I=string.format("GAME OVER  %d%s",g,W and"  BEST!"or"")end snail.board(an(),nil,I)if k=="over"then snail.hint("OK menu BACK leave")elseif t 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 f=snail.load()if f then local o,y=f:match("(%d+),(%d+)")r={tonumber(o)or 0,tonumber(y)or 0}end end
