--!name Initiative Tracker
--!icon sword
--!category Games
local R,ax,az=16,16,16 local b={}local o local H,O=1,true local r,p,B,F,l="list",1,1,nil,nil local x,E local function u(a)for g,s in ipairs(b)do if s==a then return g end end end local function W(a)local g=u(a)if g then table.remove(b,g)end local t=#b+1 for f,s in ipairs(b)do if s.i<a.i then t=f;break end end table.insert(b,t,a)end local function K(af,aN,aG)return math.max(aN,math.min(aG,af))end local function I()local G={"I1|"..H.."|"..(u(o)or 0).."|"..(O and 1 or 0)}for S,a in ipairs(b)do G[#G+1]=table.concat({a.n,a.i,a.h,a.m,(a.p and"p"or"")..(a.d and"d"or""),a.t},"|")end snail.save(table.concat(G,"\n"))end local function aO()local c=snail.load()if type(c)~="string"or c:sub(1,3)~="I1|"then return end local i for aL in c:gmatch("[^\n]+")do local e={}for af in(aL.."|"):gmatch("([^|]*)|")do e[#e+1]=af end if e[1]=="I1"then H,i,O=math.max(1,tonumber(e[2])or 1),tonumber(e[3])or 0,e[4]~="0"elseif#b<R and e[1]and e[1]~=""then b[#b+1]={n=e[1],i=tonumber(e[2])or 0,h=tonumber(e[3])or 0,m=tonumber(e[4])or 0,p=(e[5]or""):find("p")~=nil,d=(e[5]or""):find("d")~=nil,t=e[6]or""}end end o=b[i]or b[1]end local function ab(a)return not(O and a.d)end local function ar(z)local j=#b if j==0 then return end local g,d,Y=u(o)or 1,H,false for S,a in ipairs(b)do Y=Y or ab(a)end repeat g=g+z if g>j then g,d=1,d+1 elseif g<1 then g,d=j,d-1 end until not Y or ab(b[g])if d<1 then l="That was the first turn";return end o,H,p=b[g],d,g I()end local function aU()local g=u(o)if not g then return end for f=1,#b-1 do local a=b[(g-1+f)%#b+1]if ab(a)then return a end end end local function as(ac,aR)local Z=ac:gsub(" %d+$","")local D,ak=0,false for S,a in ipairs(b)do if a~=aR then if a.n==ac then ak=true end local y,M=a.n:match("^(.-) (%d+)$")if a.n==Z then D=math.max(D,1)elseif y==Z then D=math.max(D,tonumber(M))end end end if not ak then return ac end return Z.." "..(D+1)end local function aq(a)local g=u(a)table.remove(b,g)if a==o then o=b[g]or b[1]end p=K(p,1,#b+1)end local function X(a,C)a.h=K(C,0,a.m)a.d=a.m>0 and a.h==0 end local au="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function aS(n)return au:sub(n+1,n+1)end local aw={"1234567890<","QWERTYUIOP'","ASDFGHJKL-+","^ZXCVBNM,./","()[_____]?>"}local ay={"1234567890<","-+/       >"}local aA="'-+,./()?"local k local m,q,A=1,2,1 local function U()return k.num and ay or aw end local function v(s,J)return U()[J]:sub(s,s)end local function L(e)local M=e=="i"or e=="h"local c=e=="n"and x.n or e=="t"and x.t or""k={f=e,s=c,at=#c,num=M,max=M and 7 or e=="n"and ax or az}A=(c==""and not M)and 1 or 0 if M then m,q=1,1 elseif q==1 or q>5 then m,q=1,2 end r="kb"end local function al()local e,a=k.f,x local c=(k.s:gsub("^%s+",""):gsub("%s+$",""))r,k="edit",nil if e=="n"then if c~=""then a.n=as(c,a)elseif E then aq(a);r="list"end elseif e=="t"then a.t=c elseif e=="i"then if c:match("^[+-]?%d+$")then a.i=K(tonumber(c),-9,99)W(a)elseif c~=""then l="That isn't a number: "..c end elseif c~=""then local i,y=c:match("^(%d*)/(%d+)$")local ae,j=c:match("^([+-]?)(%d+)$")if y then a.m=K(tonumber(y),0,999)X(a,i~=""and tonumber(i)or(a.h>0 and a.h or a.m))elseif j then j=tonumber(j)if a.m==0 then if ae==""then a.m=K(j,0,999);X(a,j)else l="Set HP first, like 12 or 12/30"end else if ae=="+"then j=a.h+j elseif ae=="-"then j=a.h-j end X(a,j)end else l="That isn't a number: "..c end end if E and r=="edit"then if e=="n"then L("i")E="i"I()return end l=l or(a.n.." joins on "..a.i)B=3 end E=nil I()end local function ap()local a=v(m,q)local c,t=k.s,k.at if a=="<"then if t>0 then k.s,k.at=c:sub(1,t-1)..c:sub(t+1),t-1 end elseif a=="^"then A=(A+1)%3 elseif a=="["then k.at=math.max(0,t-1)elseif a=="]"then k.at=math.min(#c,t+1)elseif a==">"then al()elseif#c>=k.max then l="That's as long as it can be"else if a=="_"then a=" "elseif A==0 then a=a:lower()end k.s,k.at=c:sub(1,t)..a..c:sub(t+1),t+1 if A==1 and a:match("%a")then A=0 end end end local function aH(f)if f=="top"then al()elseif f=="ok"then ap()elseif f=="left"or f=="right"then local z,a=f=="left"and-1 or 1,v(m,q)local j repeat m=(m-1+z)%11+1 j=v(m,q)until j~=" "and(a~="_"or j~="_")elseif f=="up"or f=="down"then q=(q-1+(f=="up"and-1 or 1))%#U()+1 if v(m,q)==" "then for N=1,10 do if m-N>=1 and v(m-N,q)~=" "then m=m-N;break end if m+N<=11 and v(m+N,q)~=" "then m=m+N;break end end end end end function touch(aJ,i,y)if aJ~="board"or r~="kb"or i<1 or i>11 or y<1 or y>#U()or v(i,y)==" "then return end l=nil m,q=i,y ap()end local function aI()local ah=U()local Q,aQ={},v(m,q)=="_"for J=1,#ah do local h={}for s=1,11 do local a=ah[J]:sub(s,s)local T=J==q and(s==m or(aQ and a=="_"))local C,ao,n=T and 1 or 0,aA:find(a,1,true)if a==" "then n=nil;a="."elseif a:match("%u")then n=T and a:byte()-65 elseif a:match("%d")then n=T and 26+a:byte()-48 elseif ao then n=(T and 35+ao)or(a=="/"and 45)elseif a=="<"then n=46+C elseif a=="^"then n=48+A*2+C elseif a=="["then n=54+C elseif a=="]"then n=56+C elseif a=="_"then n=58+C*3+(v(s-1,J)~="_"and 0 or v(s+1,J)~="_"and 2 or 1)else n=64+C end h[s]=n and aS(n)or a=="."and"."or("{"..a.."}")end Q[J]=table.concat(h)end return table.concat(Q,"/")end local function _(a)return a.m>0 and(a.h.." / "..a.m)or"not tracked"end local function aK(a)local c=(a==o and"* "or"")..a.i.."  "..a.n if a.p then c=c.." (PC)"end if a.m>0 then c=c.."  "..a.h.."/"..a.m end if a.d then c=c.."  DOWN"end if a.t~=""then c=c.."  "..a.t end return c end local function ai()local h={}if#b<R then h[#h+1]="add"end h[#h+1]="skip"if#b>0 then h[#h+1]="new";h[#h+1]="clear"end return h end local at={add="+ Add a fighter",new="New encounter (keeps players)",clear="Clear everyone",}local function aB(i)if F==i then return i=="new"and"Press OK again: clear the monsters"or"Press OK again: clear everyone"end if i=="skip"then return"Skip downed fighters: "..(O and"yes"or"no")end return at[i]end local function aC()x={n="",i=10,h=0,m=0,p=false,d=false,t=""}W(x)o=o or x E=true L("n")end local function aM(f,i)local aj=ai()local j=#b+#aj if f=="up"then p=(p-2)%j+1 elseif f=="down"then p=p%j+1 elseif f=="right"then ar(1)elseif f=="left"then ar(-1)elseif f=="ok"then if b[p]then x,r,F=b[p],"edit",nil;return end local w=aj[p-#b]if w=="add"then aC()elseif w=="skip"then O=not O;I()elseif w=="new"or w=="clear"then if i~=w then F=w;return end local aa={}if w=="new"then for S,a in ipairs(b)do if a.p then aa[#aa+1]=a end end end b,H=aa,1 o=b[1]p=1 l=w=="new"and"New encounter: round 1"or"Everyone is gone"I()end end end local P={"name","init","hp","note","side","down","order","turn","copy","remove"}local function aF(d,a)if d=="name"then return"Name: "..a.n end if d=="init"then return"Initiative: "..a.i end if d=="hp"then return"HP: ".._(a)end if d=="note"then return"Note: "..(a.t~=""and a.t or"-")end if d=="side"then return"Side: "..(a.p and"Player"or"Monster / NPC")end if d=="down"then return"Down or out: "..(a.d and"yes"or"no")end if d=="order"then return"Turn order: "..u(a).." of "..#b end if d=="turn"then return a==o and"It's their turn now"or"Give them the turn"end if d=="copy"then return"Add another like this"end return F=="remove"and"Press OK again to remove "..a.n or"Remove from the fight"end local function aP(a,z)local g=u(a)local G=b[g+z]if not G then return end a.i=G.i b[g],b[g+z]=G,a end local function aE(f,i)local a,d=x,P[B]local V=(f=="left"and-1)or(f=="right"and 1)if f=="top"then r,p="list",u(a)or p elseif f=="up"then B=(B-2)%#P+1 elseif f=="down"then B=B%#P+1 elseif V then if d=="init"then a.i=K(a.i+V,-9,99);W(a)elseif d=="hp"then if a.m==0 then l="OK sets HP, like 12"else X(a,a.h+V)end elseif d=="side"then a.p=not a.p elseif d=="down"then a.d=not a.d elseif d=="order"then aP(a,V)else return end I()elseif f=="ok"then if d=="name"then L("n")elseif d=="init"then L("i")elseif d=="hp"then L("h")elseif d=="note"then L("t")elseif d=="side"then a.p=not a.p elseif d=="down"then a.d=not a.d elseif d=="order"then l="LEFT moves them up, RIGHT down"elseif d=="turn"then o,r,p=a,"list",u(a)elseif d=="copy"then if#b>=R then l=R.." fighters is the most"else local z={n=as(a.n),i=a.i,h=a.m,m=a.m,p=a.p,d=false,t=""}W(z)l="Added "..z.n end elseif i~="remove"then F="remove"else l=a.n.." left the fight"aq(a)r="list"end I()end end function key(f)if r=="kb"then l=nil;aH(f);return end if r=="list"and f=="top"then return end l=nil local i=F F=nil if r=="list"then aM(f,i)else aE(f,i)end end local function Q(h,am,ad)local D=math.max(1,math.min(am-ad//2,#h-ad+1))for n=D,math.min(#h,D+ad-1)do snail.row(h[n]:sub(1,90),n==am)end end local ag={n="Up to 16 letters. A name already here gets a number: Goblin 2.",t="A condition or reminder, like Poisoned or Prone.",i="Type the roll, like 17.",}local function aD()local e,a=k.f,x local aT={n=E==true and"New fighter"or"Name",i="Initiative",h="HP",t="Note"}snail.title(aT[e]..((E==true or a.n=="")and""or" - "..a.n))snail.status(l or(k.num and""or A==2 and"CAPS"or A==1 and"Shift"or""))snail.text(k.s:sub(1,k.at).."|"..k.s:sub(k.at+1))if e=="h"then snail.small(a.m>0 and"Now ".._(a)..". Type -8 for damage, +5 to heal, 12 to set, 12/30 for both."or"No HP yet. Type 12, or 12/30 for 12 of 30.")elseif e=="i"then snail.small("Now "..a.i..". "..ag.i)else snail.small(ag[e])end snail.board(aI())snail.hint("arrows move  BACK done  OK press")end local av={init="BACK list  OK type  L/R -1/+1",hp="BACK list  OK type  L/R -1/+1",side="BACK list  OK or L/R change",down="BACK list  OK or L/R change",order="BACK list  L/R move up/down",remove="BACK list  OK twice to remove",}function draw()if r=="kb"then return aD()end if r=="edit"then local a=x snail.title(a.n)snail.status(l or(a==o and"Their turn - round "..H or"Initiative "..a.i..(a.m>0 and"  HP ".._(a)or"")))local h={}for g,d in ipairs(P)do h[g]=aF(d,a)end Q(h,B,13)snail.hint((av[P[B]]or"BACK list  OK choose").."  UP/DN move")return end if#b==0 then snail.title("Initiative Tracker")snail.status(l or"No one in the fight yet")else snail.title("Round "..H)snail.status(l or(o.n.."'s turn"..(o.d and" (down)"or"")))local an=aU()snail.small(an and"Next up: "..an.n or"No one else is up")end local h={}for g,a in ipairs(b)do h[g]=aK(a)end for S,w in ipairs(ai())do h[#h+1]=aB(w)end Q(h,p,13)if#b==0 then snail.small("Add each fighter with their initiative roll. HP and notes are optional.")end snail.hint(#b>0 and"BACK leave  OK open  L/R turn  UP/DN move"or"BACK leave  OK choose  UP/DN move")end function start()aO()p=u(o)or 1 end
