--!name Safety Net
--!icon game
--!category Games
local o=snail.touchscreen==true local N,D=14,16 local O={{2,2},{2,5},{2,8},{3,10},{3,13},{4,9},{4,6},{5,6},{5,9},{6,13},{7,9},{7,6},{8,6},{8,9},{9,13},{10,9},{10,6},{11,6},{11,9},{12,12},}local ad={[1]=true,[6]=true,[7]=true,[11]=true,[12]=true,[16]=true,[17]=true}local T=4 local S={}local ac={"..^!..","..++..","<++++>","=++++?","..++..","..~|.."}local i,C="menu",1 local g={}local x,f,r=1,0,0 local q,I,t=0,0,0 local s,l,J,v,R=false,1,nil,0,false local m={0,0}local M=1 local function an(w)M=(M*1664525+1013904223)&0xFFFFFFFF return(M>>16)%w end local function P()if l==1 then return math.max(550,1000-f*3)end return math.max(450,800-f*3)end local function ao()snail.save(m[1]..","..m[2])end local function al()if os and os.time then M=os.time()%100000+11 end g={}x,f,r=1,0,0 q,I,t=0,0,1 s,J,v=false,nil,0 i="play"snail.tick(P())end local function am()i="over"snail.tick(0)R=f>m[l]if R then m[l]=f ao()end end local function U(w)local Y=f f=math.min(9999,f+w)if((Y<200 and f>=200)or(Y<500 and f>=500))and r>0 then r=0 J,v="BONUS  misses cleared",2 end snail.tick(P())end local function ai()for E,b in ipairs(g)do for h=1,3 do local _=5*h-b if _>=0 then for w=1,3 do if math.abs(w-h)>math.abs(5*w-1-_)then return false end end end end end return true end local function ap()local ak=l==1 and math.min(4,2+f//60)or math.min(4,3+f//80)if#g>=ak or not ai()then return end local af=l==1 and math.min(45,20+f//5)or math.min(60,35+f//5)if#g==0 or an(100)<af then g[#g+1]=1 end end function tick()if i~="play"or s then snail.tick(0)return end if t>0 then t=t-1 return end if v>0 then v=v-1 if v==0 then J=nil end end if q>0 then q=q-1 if q==0 then I=0 if r>=3 then am()end end return end for c=#g,1,-1 do local b=g[c]if b%5==0 and b<#O then if x==b//5 then U(1)else table.remove(g,c)r=r+1 I,q=b//5,3 end end end if q>0 then return end for c=#g,1,-1 do g[c]=g[c]+1 if g[c]>#O then table.remove(g,c)U(2)end end ap()end local function Q(n)if i~="play"or s or t>0 then return end x=math.max(1,math.min(3,n))end function key(h)if i=="menu"then if h=="up"or h=="down"then C=3-C elseif h=="ok"then l=C al()end return end if i=="over"then if h=="ok"then i="menu"end return end if h=="ok"then s=not s snail.tick(s and 0 or P())return end if h=="left"then Q(x-1)elseif h=="right"then Q(x+1)end end local function ag(k,u)local a,d=u-D-1,k-1 if a<0 then return nil end local H,aa=2*(d-T)-5,2*a-5 if H>=-7 and H<=7 then local W,X=math.abs(H),math.abs(aa)if W>X then return H<0 and"left"or"right"end if X>W then return aa<0 and"up"or"down"end return nil end for E,p in ipairs(S)do if d>=p[1]and d<=p[1]+1 then return p[3]end end end function touch(aj,k,u)if aj~="board"then return end if o then local h=ag(k,u)if h then return key(h)end else k=k-1 end if u<=D then Q(k<=4 and 1 or k<=7 and 2 or 3)end end local function ah()local j={}for a=1,6 do j[a]={}for d=1,N do j[a][d]="."end for c=1,6 do j[a][T+c]=ac[a]:sub(c,c)end end for E,p in ipairs(S)do local K,a,d=p[4]and"()[]"or"$&;:",p[2]+1,p[1]+1 j[a][d],j[a][d+1]=K:sub(1,1),K:sub(2,2)j[a+1][d],j[a+1][d+1]=K:sub(3,3),K:sub(4,4)end for a=1,6 do j[a]=table.concat(j[a])end return j end local function aq(z)local B={}for a=0,D-1 do local b=table.concat(z[a])B[#B+1]=o and b or"."..b.."."end if o then for E,L in ipairs(ah())do B[#B+1]=L end end return table.concat(B,"/")end local function ae()local y=t>0 local z={}for a=0,D-1 do local L={}for d=1,N do L[d]="."end z[a]=L end local function e(d,a,G)z[a][d+1]=G end local b=y and"8888"or string.format("%4d",f):gsub(" ","_")for c=1,4 do e(c-1,0,b:sub(c,c))end for c=1,3 do e(10+c,0,(y or r>=c)and"d"or"D")end e(0,1,"x");e(1,1,"x")for a=2,14 do e(0,a,"b");e(1,a,"b")end for d=0,N-1 do e(d,15,"g")end e(12,13,"q");e(13,13,"r");e(12,14,"s");e(13,14,"t")local V={}for E,b in ipairs(g)do V[b]=true end for b,Z in ipairs(O)do local A=y or V[b]local G=ad[b]and"u"or"j"e(Z[1],Z[2],A and G or G:upper())end for n=1,3 do local A=y or x==n e(3*n-1,14,A and"f"or"F")e(3*n,14,A and"n"or"N")e(3*n+1,14,A and"h"or"H")e(3*n,15,(y or I==n)and"d"or"D")end return aq(z)end function draw()local ab=string.format("GAME %s  BEST %d",l==1 and"A"or"B",m[l])snail.title((o and i~="menu")and"Safety Net  "..ab or"Safety Net")if i=="menu"then snail.row("Game A",C==1)snail.row("Game B (faster)",C==2)snail.gap()snail.text(string.format("Best: A %d   B %d",m[1],m[2]))snail.gap()snail.small("Bounce the jumpers across the street and into the ambulance.")snail.small("Each one lands on the net three times on the way.")if o then snail.small("Move the net with the d-pad, or tap the street.")else snail.small("LEFT and RIGHT move the net.")end snail.small("Three falls and it's over. 200 and 500 points clear them.")snail.hint("OK start BACK leave")return end if not o then snail.small(ab)end local F=J if t>0 then F="GAME "..(l==1 and"A"or"B")end if s then F="PAUSED"end if i=="over"then F=string.format("GAME OVER  %d%s",f,R and"  BEST!"or"")end snail.board(ae(),nil,F)if i=="over"then snail.hint("OK menu BACK leave")elseif o 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 b=snail.load()if b then local k,u=b:match("(%d+),(%d+)")m={tonumber(k)or 0,tonumber(u)or 0}end end
