--!name Knockout
--!icon game
--!category Games
local x=snail.touchscreen==true local aj,S=14,15 local o,ae=8,30 local az={{5,4},{8,4}}local aF={{4,3},{9,3}}local aE={{5,10},{8,10}}local aA={{4,6},{9,6}}local aC={guard={{4,12},{9,12}},left={{1,12},{2,12}},right={{11,12},{12,12}},block={{6,11},{7,11}},}local aB={{6,8},{7,8}}local ak=1 local ai={{11,2,"up",true}}local aD={"..^!..","..++..","<++++>","=++++?","..++..","..~|.."}local j,R,f="menu",1,1 local g,l,D,Q=0,1,1,ae local w,v,M,J,Z,Y=o,o,0,0,0,0 local b,G,m,K="guard",1,0,false local k,X,r="guard",1,0 local i,t=nil,0 local C,L,A,_=0,nil,0,nil local B,ah,ax=false,false,""local q={0,0}local ad=1 local function P(N)ad=(ad*1664525+1013904223)&0xFFFFFFFF return(ad>>16)%N end local function af()if f==1 then return math.max(600,950-50*l)end return math.max(500,750-40*l)end local function aY()return math.min(f==1 and 60 or 75,(f==1 and 24 or 40)+6*l)end local function aR()snail.save(q[1]..","..q[2])end local function ac(aU,N,W)L,A,_=aU,N,W end local function ap()b,m,K="guard",0,false k,r,t,i="guard",0,0,nil Q=ae end local function at()w,v,M,J,Z,Y=o,o,0,0,0,0 D=1 ap()end local function av(aX)j,ax="over",aX snail.tick(0)ah=g>q[f]if ah then q[f]=g aR()end end local function aN()if os and os.time then ad=os.time()%100000+11 end g,l=0,1 at()B,L,A,_=false,nil,0,nil C=1 j="play"snail.tick(af())end local function au()l=l+1 at()ac("FIGHT "..l,2)snail.tick(af())end local function ar(ay)i,t=ay,0 if ay=="him"then J,g,b=J+1,g+50,"down"else M,k,r=M+1,"guard",0 end end local function aq(N)w=math.max(0,w-N)if w==0 then ar("you")end end local function aV()b,K="punch",false if k==(G==1 and"right"or"left")then K,g=true,g+5 elseif k=="block"then if f==2 then aq(1)end else Y=Y+1 aq(f==1 and 2 or 3)end end local function aO()if b=="guard"then if P(100)<aY()then b,G,m="wind",P(2)+1,2 end elseif b=="wind"then m=m-1 if m==0 then local aL=f==2 and 20 or(l>1 and 10 or 0)if P(100)<aL then b="guard"else aV()end end elseif b=="punch"then if K then b,m="open",f==1 and 2 or 1 else b="guard"end elseif b=="open"then m=m-1 if m<=0 then b="guard"end end end local function aK()t=t+2 if(i=="him"and J or M)>=3 then if t<10 then return end if i=="him"then g=g+200 ac("KNOCKOUT!",2,au)else av("KNOCKED OUT")end return end if t<6 then return end if i=="him"then v,b=o-2*J,"guard"else w=o-2*M end t,i=0,nil end local function aG()if D<3 then D=D+1 w,v=math.min(o,w+2),math.min(o,v+2)ap()ac("ROUND "..D,2)elseif Z>Y then g=g+100 ac("WIN ON POINTS",2,au)else av("LOST ON POINTS")end end function tick()if j~="play"or B then snail.tick(0)return end if C>0 then C=C-1 return end if A>0 then A=A-1 if A==0 then local W=_ L,_=nil,nil if W then W()end end return end if i then return aK()end Q=Q-1 aO()if r>0 then r=r-1 if r==0 then k="guard"end end if Q<=0 and not i then aG()end end local function aQ()X=3-X k,r="punch",1 if b=="open"or(b=="punch"and K)then Z=Z+1 g=g+(b=="open"and 20 or 10)v=v-1 if v<=0 then ar("him")end elseif b=="guard"and P(100)<(f==1 and 30 or 50)then b,G,m="wind",P(2)+1,2 end end local function H(h)if j~="play"or B or C>0 or A>0 or i then return end if h=="up"then aQ()elseif h=="down"then k,r="block",2 else k,r=h,2 end end function key(h)if h=="top"then return end if j=="menu"then if h=="up"or h=="down"then R=3-R elseif h=="ok"then f=R aN()end return end if j=="over"then if h=="ok"then j="menu"end return end if h=="ok"then B=not B snail.tick(B and 0 or af())return end H(h)end local function aI(p,s)local c,d=s-S-1,p-1 if c<0 then return nil end local V,ao=2*(d-ak)-5,2*c-5 if V>=-7 and V<=7 then local am,an=math.abs(V),math.abs(ao)if am>an then return V<0 and"left"or"right"end if an>am then return ao<0 and"up"or"down"end return nil end for al,z in ipairs(ai)do if d>=z[1]and d<=z[1]+1 then return z[3]end end end function touch(aM,p,s)if aM~="board"then return end if x then local h=aI(p,s)if h then return key(h)end else p=p-1 end if s>S or s<3 then return end if s<=10 then return H("up")end if p<=4 then H("left")elseif p>=11 then H("right")else H("down")end end local function aJ()local n={}for c=1,6 do n[c]={}for d=1,aj do n[c][d]="."end for a=1,6 do n[c][ak+a]=aD[c]:sub(a,a)end end for al,z in ipairs(ai)do local aa,c,d=z[4]and"()[]"or"$&;:",z[2]+1,z[1]+1 n[c][d],n[c][d+1]=aa:sub(1,1),aa:sub(2,2)n[c+1][d],n[c+1][d+1]=aa:sub(3,3),aa:sub(4,4)end for c=1,6 do n[c]=table.concat(n[c])end return n end local function aS(u)local O={}for c=0,S-1 do local E=table.concat(u[c])O[#O+1]=x and E or"."..E.."."end if x then for al,ab in ipairs(aJ())do O[#O+1]=ab end end return table.concat(O,"/")end local function aH()local y=C>0 local u={}for c=0,S-1 do local ab={}for d=1,aj do ab[d]="."end u[c]=ab end local function e(d,c,T)u[c][d+1]=T end local function F(aw,ag,T)e(aw[1],aw[2],(y or ag)and T or T:upper())end local E=y and"8888"or string.format("%4d",math.min(g,9999)):gsub(" ","_")for a=1,4 do e(a-1,0,E:sub(a,a))end for a=1,5 do e(3+2*a,0,(y or Q>(a-1)*ae//5)and"e"or"E")end for a=1,o do e(2+a,1,(y or v>=a)and"e"or"E")e(2+a,14,(y or w>=a)and"e"or"E")end e(6,3,"a");e(7,3,"b");e(6,4,"c");e(7,4,"d")e(4,5,"u");e(9,5,"v")for d=5,8 do e(d,5,"z");e(d,6,"z");e(d,7,"w")end local U=b=="open"or b=="down"e(6,2,(y or U)and"s"or"S");e(7,2,(y or U)and"s"or"S")for a=1,2 do local aT=(b=="wind"or b=="punch")and G==a F(az[a],not U and not aT,"k")F(aF[a],b=="wind"and G==a,"k")F(aE[a],b=="punch"and G==a,"k")F(aA[a],U,"k")end local aW=i~="you"for as,aP in pairs(aC)do for a=1,2 do local ag=aW and(k==as or(k=="punch"and as=="guard"and a~=X))F(aP[a],ag,"y")end end for a=1,2 do F(aB[a],k=="punch"and X==a,"y")end return aS(u)end function draw()local u=f==1 and"A"or"B"if x and j~="menu"then snail.title(string.format("Knockout %s  FIGHT %d RD %d  BEST %d",u,l,D,q[f]))else snail.title("Knockout")end if j=="menu"then snail.row("Game A",R==1)snail.row("Game B (harder punches, feints)",R==2)snail.gap()snail.text(string.format("Best: A %d   B %d",q[1],q[2]))snail.gap()snail.small("He winds up before he punches: duck away from the glove,")snail.small("or block. Hit him while he is open after a miss.")if x then snail.small("D-pad left and right duck, down blocks; the round button")snail.small("(or up) punches. Tap him to punch, too.")else snail.small("LEFT and RIGHT duck; the side keys punch (UP) and block")snail.small("(DOWN).")end snail.small("Three knockdowns is a knockout. Three rounds a fight; the")snail.small("bar beside the score is the round clock.")snail.hint("OK start BACK leave")return end if not x then snail.small(string.format("GAME %s   FIGHT %d  ROUND %d   BEST %d",u,l,D,q[f]))end local I=L if i and not L then I=(i=="him"and"HE'S DOWN  "or"YOU'RE DOWN  ")..t end if C>0 then I="GAME "..(f==1 and"A"or"B")end if B then I="PAUSED"end if j=="over"then I=string.format("%s  %d%s",ax,g,ah and"  BEST!"or"")end snail.board(aH(),nil,I)if j=="over"then snail.hint("OK menu BACK leave")elseif x then snail.hint("OK pause BACK leave")else snail.hint("L R duck OK pause BACK leave")end end function start()snail.ink("fast")local E=snail.load()if E then local p,s=E:match("(%d+),(%d+)")q={tonumber(p)or 0,tonumber(s)or 0}end end
