--!name Life Counter
--!icon heart
--!category Games
local Q,N,W,av,au,R=-99,999,10,21,99,8 local M={20,30,40}local s=26 local aO={B="D",D="B",C="E",E="C",G="I",I="G",H="J",J="H",["3"]="e",["4"]="h",["7"]="L",P="d",["6"]="9",["9"]="6",K="k"}local aP={["0"]=63,["2"]=91,["3"]=79,["4"]=102,["5"]=109,["6"]=125,["7"]=7,["8"]=127,["9"]=111,["-"]=64}local aN={{3,5,1},{4,7,1},{6,9,1},{7,10,2}}local g,A,l=4,40,1 local o,y,u={},{},{}local p,c,U,ai="setup",1,nil,false local function v(B,P,aA)if B<P then return P end if B>aA then return aA end return B end local function D(a,k)return(a-1)*R+k end local function al()for a=1,g do o[a],y[a]=A,0 for k=1,R do u[D(a,k)]=0 end end l,U,ai=1,nil,true end local function aj(a)if o[a]<=0 or y[a]>=W then return true end for k=1,g do if u[D(a,k)]>=av then return true end end return false end local function I()local e={}for a=1,g do for k=1,g do if k~=a then e[#e+1]=u[D(a,k)]end end end snail.save(table.concat({"L2",g,A,l,table.concat(o,",",1,g),table.concat(y,",",1,g),table.concat(e,",")},";"))end local function am(q)local j={}for B in(q or""):gmatch("%-?%d+")do j[#j+1]=tonumber(B)end return j end local function aD()local q=snail.load()if type(q)~="string"or q:sub(1,3)~="L2;"then return false end local f={}for B in(q..";"):gmatch("([^;]*);")do f[#f+1]=B end local h,aK=tonumber(f[2]),tonumber(f[3])if not h or h<2 or h>R or not aK then return false end g,A=h,v(aK,1,N)al()local _,a0,e=am(f[5]),am(f[6]),am(f[7])local b=1 for a=1,g do o[a]=v(_[a]or A,Q,N)y[a]=v(a0[a]or 0,0,W)for k=1,g do if k~=a then u[D(a,k)]=v(e[b]or 0,0,au)b=b+1 end end end l=v(tonumber(f[4])or 1,1,g)return true end local function an()return(g+3)//4 end local function aa(n)local h=an()return n*(g//h)+math.min(n,g%h)end local function aH(a)local n=an()-1 while n>0 and a-1<aa(n)do n=n-1 end return n end local function ah(i,f)if i=="1"then return(f[3]>1 or f[1]<=4)and 3 or 4 end if i=="-"then return(f[1]*2+2)//3 end return f[1]end local function T(i,f,d,e)local t,Y,j=ah(i,f),f[2],f[3]if d<0 or e<0 or d>=Y or e>=t then return false end if i=="1"then if j>1 then return e>0 or d<2 end local q=t-2 return e==q or(d==0 and e==q-1)or(d==Y-1 and e>=q-1)end local L,aE=aP[i],(Y-j)//2 local ar,P=d<aE+j,d>=aE return(d<j and L&1>0)or(d>=Y-j and L&8>0)or(ar and P and L&64>0 and(e>=j or i~="3"))or(e<j and(ar and L&32>0 or P and L&16>0))or(e>=t-j and(ar and L&2>0 or P and L&4>0))end local function a1(i,f,d,e)local aL,G,_,aJ=T(i,f,d-1,e),T(i,f,d+1,e),T(i,f,d,e-1),T(i,f,d,e+1)local ap,aq,ag,af=not(aL or _),not(aL or aJ),not(G or aJ),not(G or _)if ap and aq then return"G"end if aq and ag then return"H"end if ag and af then return"I"end if af and ap then return"J"end return ap and"B"or aq and"C"or ag and"D"or af and"E"or"A"end local function aF(z,q,J,aU,aZ)local f,S,h for m=aU,1,-1 do f,h=aN[m],0 for b=1,#q do h=h+ah(q:sub(b,b),f)end S=f[3]if h+(#q-1)*S>J then S=1 end h=h+(#q-1)*S if h<=J then break end end local w,a4=1+(J-h)//2,aZ-f[2]//2 for b=1,#q do local i=q:sub(b,b)local ax=ah(i,f)for d=0,f[2]-1 do for e=0,ax-1 do if T(i,f,d,e)then z(a4+d,w+e,a1(i,f,d,e))end end end w=w+ax+S end end local function a_(C,as,at,t,a,ao)local function z(d,e,i)if ao then d,e,i=14-d,t-1-e,aO[i]or i end C[(at+d)*s+as+e+1]=i end local J,ae=t-2,t>13 if a==l then for e=0,t-1 do z(1,e,"*");z(14,e,"*")end for d=2,13 do z(d,0,"*");z(d,t-1,"*")end end z(2,1,"P");z(2,2,tostring(a))if aj(a)then z(2,3,"K")end aF(z,tostring(o[a]),J,ae and 4 or 2,7)local Z,ad={},{}if y[a]>0 then Z[1]="p"..y[a]end for k=1,g do if k~=a and u[D(a,k)]>0 then ad[#ad+1]=k end end table.sort(ad,function(r,H)local w,aM=u[D(a,r)],u[D(a,H)]if w~=aM then return w>aM end return r<H end)for X,k in ipairs(ad)do Z[#Z+1]="P"..k..":"..u[D(a,k)]end local O,K={},""for X,ak in ipairs(Z)do local j=K==""and ak or K.." "..ak if#j<=J then K=j else O[#O+1]=K;K=ak end end if K~=""then O[#O+1]=K end local ab=ae and 13 or 12 for m=1,math.min(#O,ae and 1 or 2)do local j=O[m]local w=1+(J-#j)//2 for b=1,#j do local i=j:sub(b,b)if i~=" "then z(ab+m-1,w+b-1,i)end end end end local function aI()local n,h=aH(l),an()local r=aa(n)local F=(n+1<h and aa(n+1)or g)-r if F==4 then return{{r+1,0,0,13,true},{r+2,13,0,13,true},{r+3,0,17,13,false},{r+4,13,17,13,false}},n,h,F end local j={{r+1,0,0,26,true}}if F==2 then j[2]={r+2,0,17,26,false}elseif F>=3 then j[2]={r+2,0,17,13,false};j[3]={r+3,13,17,13,false}end return j,n,h,F end local function a3()local C={}for m=1,32*s do C[m]="."end local V,n,h,F=aI()local function az(ab,aC)if l~=aC and l~=aC+1 then for d=ab+1,ab+12 do C[d*s+13]="V"end end end for X,x in ipairs(V)do a_(C,x[2],x[3],x[4],x[1],x[5])end if F==4 then az(0,V[1][1])end if F>2 then az(18,V[F-1][1])end local E={}for d=0,31 do E[d+1]=table.concat(C,"",d*s+1,d*s+s)end return table.concat(E,"/"),n,h end local function ay()return g+1 end local function aG(ac)local b=2 for k=1,g do if k~=l then b=b+1 if b==ac then return k end end end end local function aW(b)if b=="up"then c=c>1 and c-1 or 3 elseif b=="down"then c=c<3 and c+1 or 1 elseif b=="left"or b=="right"then local G=b=="right"and 1 or-1 if c==1 then g=v(g+G,2,R)elseif c==2 then local m=1 for w,B in ipairs(M)do if B==A then m=w end end A=M[(m-1+G)%#M+1]end elseif b=="ok"then if c==1 then g=g>=R and 2 or g+1 elseif c==2 then local m=1 for w,B in ipairs(M)do if B==A then m=w end end A=M[m%#M+1]else al()p="table"I()end elseif b=="top"and ai then aD()p,c="options",1 end end local function aV(b)if b=="up"then c=c>1 and c-1 or 4 elseif b=="down"then c=c<4 and c+1 or 1 elseif b=="ok"then if c==2 then al();I()elseif c==3 then p,c="setup",3;return elseif c==4 then l=math.random(g)U="P"..l.." goes first!"I()end p="table"end end local function aB(b)local a,E=l,ay()if b=="up"then c=c>1 and c-1 or E elseif b=="down"then c=c<E and c+1 or 1 elseif b=="left"or b=="right"then local G=b=="right"and 1 or-1 if c==1 then o[a]=v(o[a]+5*G,Q,N)elseif c==2 then y[a]=v(y[a]+G,0,W)else local m=D(a,aG(c))local aw=u[m]u[m]=v(aw+G,0,au)o[a]=v(o[a]-(u[m]-aw),Q,N)end I()elseif b=="top"then p="table"end end local function aX(b)if b=="up"or b=="down"then l=(l-1+(b=="down"and 1 or g-1))%g+1 I()elseif b=="left"or b=="right"then o[l]=v(o[l]+(b=="right"and 1 or-1),Q,N)I()elseif b=="ok"then p,c="detail",1 elseif b=="top"then p,c="options",1 end end function key(b)U=nil if p=="setup"then aW(b)elseif p=="options"then aV(b)elseif p=="detail"then aB(b)else aX(b)end end function touch(aY,r,H)if aY~="board"then return end if p=="detail"then aB(r*2<=s and"left"or"right")return end if p~="table"then return end U=nil local V,X,h=aI()if h>1 and H>=16 and H<=17 then local n=(aH(l)+1)%h l=aa(n)+1 I()return end for X,x in ipairs(V)do local a,as,at,t,ao=x[1],x[2],x[3],x[4],x[5]local e=r-1-as local d=H-1-at if e>=0 and e<t and d>=0 and d<15 then if ao then e=t-1-e end l=a o[a]=v(o[a]+(e*2<t and-1 or 1),Q,N)I()return end end end local function aT()local a2,n,h=a3()local a=l local H=U or("P"..a..":  "..o[a].." life"..(y[a]>0 and",  "..y[a].." poison"or"")..(aj(a)and"  (out)"or"")..(h>1 and"   page "..(n+1).."/"..h or""))snail.title("Life Counter")snail.board(a2,nil,H)snail.hint("tap L -1  R +1  OK counters  BACK options")end local function aQ()local a=l snail.title("P"..a..":  "..o[a].." life"..(aj(a)and"  -  out"or""))snail.row("Life  "..o[a].."    (5 per press)",c==1)snail.row("Poison  "..y[a].." of "..W,c==2)snail.gap()snail.small("Commander damage P"..a.." has taken:")for ac=3,ay()do local k=aG(ac)snail.row("From P"..k.."'s commander  "..u[D(a,k)].." of "..av,c==ac)end snail.gap()snail.small("Commander damage comes off life too. Out at 0 life, 10 poison or 21 from one commander.")local C={}for m=1,12*s do C[m]="."end aF(function(d,e,i)C[d*s+e+1]=i end,tostring(o[a]),s-2,4,6)local E={}for d=0,11 do E[d+1]=table.concat(C,"",d*s+1,d*s+s)end snail.board(table.concat(E,"/"))snail.hint(c==1 and"BACK table  tap or L-5 R+5  UP/DN choose"or"BACK table  tap or L-1 R+1  UP/DN choose")end local function aR()snail.title("Life Counter")local E={"Back to the table","Rematch: "..g.." players, "..A.." life","New game: change players or life","Pick who goes first"}for m,j in ipairs(E)do snail.row(j,m==c)end snail.gap()snail.small("The game is kept when you leave. Open Life Counter again to carry on.")snail.hint("BACK leave OK choose UP/DN move")end local function aS()snail.title("New game")snail.row("Players  "..g,c==1)snail.row("Starting life  "..A,c==2)snail.row("Start the game",c==3)snail.gap()snail.small("20 life for most formats, 30 for Brawl or Two-Headed Giant, 40 for Commander.")snail.small("Lay the reader flat between the players: the top row faces the far side.")snail.small("Up to four players share a screen; five to eight go on two pages.")snail.hint((ai and"BACK cancel"or"BACK leave").." OK start L/R change UP/DN move")end function draw()if p=="setup"then aS()elseif p=="options"then aR()elseif p=="detail"then aQ()else aT()end end function start()snail.ink("fast")if aD()then p="table"else g,A,p,c=4,40,"setup",3 end end
