--!name 5E Character Sheet
--!icon sword
--!category Games
local ax=6 local aO=8000 local i,F={},{}local E=0 local function A(X)local f=i[X]if f==nil and F[X]then f=F[X].d end return f end local function s(X)return tonumber(A(X))or 0 end local function D(p)return p>=0 and"+"..p or tostring(p)end local function I(a)return tonumber(i[a[1].."_"])or a[4]or 0 end local function Q(a)local ao=I(a)local f=tonumber(i[a[1]])or ao return f>ao and ao or f end local function Y(a)local d=i[a[1]]if type(d)~="table"then d={};i[a[1]]=d end return d end local aN,ay="5E Characters","nm"local al={"5E Character Sheet works with the fifth edition rules in the System Reference Document 5.2.","It is an independent product.",'This work includes material from the System Reference Document 5.2 ("SRD 5.2") by',"Wizards of the Coast LLC, available at https://www.dndbeyond.com/srd.","The SRD 5.2 is licensed under the Creative Commons Attribution 4.0 International License,","available at https://creativecommons.org/licenses/by/4.0/","legalcode.",}local ad={"Strength","Dexterity","Constitution","Intelligence","Wisdom","Charisma"}local av={"STR","DEX","CON","INT","WIS","CHA"}local ae={"st","dx","co","it","wi","ch"}local aT={300,900,2700,6500,14000,23000,34000,48000,64000,85000,100000,120000,140000,165000,195000,225000,265000,305000,355000}local function aa()return math.max(1,s("lv"))end local function H()return 2+(aa()-1)//4 end local function P(b)return(s(ae[b])-10)//2 end local aB,aK,ah={},{},{}for b=1,6 do local a={ae[b],"n",ad[b],1,30,10}a.s=function(f)return ad[b].."  "..f.."   ("..D((f-10)//2)..")"end aB[b]=a a={"s"..ae[b],"k",ad[b],{"[ ]","[x]"}}a.s=function(f)return(f>0 and"[x]  "or"[  ]  ")..ad[b].." save   "..D(P(b)+f*H())end aK[b]=a end local aA={"[  ]","[x]","[E]"}local az local aS="Acrobatics2Animal Handling5Arcana4Athletics1Deception6History4Insight5Intimidation6".."Investigation4Medicine5Nature4Perception5Performance6Persuasion6Religion4Sleight of Hand2Stealth2Survival5"for aq,m in aS:gmatch("([%a ]+)(%d)")do m=tonumber(m)local a={"k"..(#ah+1),"k",aq,aA}if aq=="Perception"then az=a[1]end a.s=function(f)return aA[f%3+1].."  "..aq.." ("..av[m]..")   "..D(P(m)+f%3*H())end ah[#ah+1]=a end local function aH()return 10+P(5)+s(az)%3*H()end local K={{"sa","c","Spellcasting ability",{"none","Intelligence","Wisdom","Charisma"}},}for b=1,9 do K[#K+1]={"l"..b,"p","Level "..b.." slots",0}end K[#K+1]={"ca","l","Cantrips",20,"cantrip",50}K[#K+1]={"sl","l","Spells",40,"spell",60}local function a9()i.hp,i.th,i.ds,i.df=nil,0,0,0 i.hl=aa()for b=1,9 do i["l"..b]=nil end end local function aW()local aE=aT[aa()]return"Proficiency bonus "..D(H())..(aE and"   Next level at "..aE.." XP"or"")end local function aX()return(A("cl")~=""and A("cl").." "or"Level ")..aa()end local function aU()local d={}for b=1,6 do d[b]=av[b]..s(ae[b])end return table.concat(d," ")end local function bc()return"Passive Perception "..aH().."   [E] = expertise"end local function aY()return"Initiative "..D(P(2)).."   Proficiency "..D(H()).."   Passive "..aH()end local function aZ()local a=F.hp return"HP "..Q(a).."/"..I(a)..(s("th")>0 and" +"..s("th")or"").."  AC "..s("ac")end local function be()local m=s("sa")if m==0 then return"Choose a spellcasting ability for your save DC and attack bonus."end return"Spell save DC "..(8+H()+P(m+3)).."   Spell attack "..D(H()+P(m+3))end local function a2()return s("gp").." gp"end local z={{"Basics",{{"nm","t","Name",30},{"cl","t","Class",40},{"lv","n","Level",1,20,1},{"sc","t","Subclass",40},{"sp","t","Species",30},{"bg","t","Background",30},{"al","t","Alignment",20},{"xp","n","Experience points",0,999999,0},{"la","t","Languages",80},{"pr","t","Armor, weapon and tool training",80},},h=aW,s=aX},{"Abilities",aB,s=aU},{"Saving throws",aK},{"Skills",ah,h=bc},{"Combat",{{"hp","p","Hit points",10},{"th","n","Temporary hit points",0,999,0},{"ac","n","Armor class",0,40,10},{"sd","n","Speed (ft)",0,300,30},{"hd","t","Hit dice",20},{"hl","n","Hit dice left",0,40,1},{"ds","n","Death save successes",0,3,0},{"df","n","Death save failures",0,3,0},{"in","k","Heroic inspiration",{"[  ]","[x]"}},{"cn","t","Conditions",60},{"lr","a","Long rest",a9,"Rested: HP, hit dice and slots are back."},},h=aY,s=aZ},{"Attacks",{{"at","l","Attacks",20,"attack",70}}},{"Spells",K,h=be},{"Features and traits",{{"ft","l","Features and traits",40,"feature",80}}},{"Inventory",{{"iv","l","Items",40,"item",60},{"gp","n","Gold (gp)",0,9999999,0},{"sv","n","Silver (sp)",0,9999999,0},{"cp","n","Copper (cp)",0,9999999,0},{"ep","n","Electrum (ep)",0,9999999,0},{"pp","n","Platinum (pp)",0,9999999,0},},s=a2},{"Notes",{{"no","l","Notes",40,"note",90}}},}local function am()local k,h=A("nm"),A("cl")return(k~=""and k or"Unnamed")..(h~=""and" - "..h.." "..aa()or"")end for M,e in ipairs(z)do for M,a in ipairs(e[2])do assert(not F[a[1]],"two fields are called "..a[1])F[a[1]]=a local c=a[2]a.d=(c=="t"and"")or(c=="n"and(a[6]or a[4]))or((c=="k"or c=="c")and(a[5]or 0))or nil end end local o={}local l,R,u,N,t="list",1,nil,nil,nil local af=0 local g={list=1,sheet=1,items=1}local function bb()local q={}for c,f in pairs(i)do if type(f)=="table"then if#f>0 then q[#q+1]=c.."="for b,p in ipairs(f)do if b>1 then q[#q+1]="|"end q[#q+1]=p end q[#q+1]="\n"end elseif f~=""then q[#q+1]=c.."="..f.."\n"end end return table.concat(q)end local function a0(e)i={}for c,f in(e or""):gmatch("([%w_]+)=([^\n]*)")do local a=F[c]if a and a[2]=="l"then local d={}for a3 in f:gmatch("[^|]+")do d[#d+1]=a3 end i[c]=d elseif a and a[2]=="t"then i[c]=f else i[c]=tonumber(f)end end end local function as()local q={"S1"}for M,r in ipairs(o)do q[#q+1]=r.slot.."|"..r.sum end snail.save(table.concat(q,"\n"))end local function S()if E==0 then return end snail.cache("c"..E,bb())local ai=am():sub(1,60)for M,r in ipairs(o)do if r.slot==E and r.sum~=ai then r.sum=ai;as()end end end local function ab()local k=A(ay);return k~=""and k or"Unnamed"end local aP="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function bf(b)return aP:sub(b+1,b+1)end local aw={"1234567890<","QWERTYUIOP'","ASDFGHJKL-+","^ZXCVBNM,./","()[_____]?>"}local aR="'-+,./()?"local j local w,v,y=1,2,1 local function G(p,L)return aw[L]:sub(p,p)end local function aj()local k=0 for M,f in pairs(i)do if type(f)=="table"then for M,p in ipairs(f)do k=k+#p+1 end elseif type(f)=="string"then k=k+#f end end return k end local function an(a,b,aV)local ac=a[2]=="n"or a[2]=="p"local e=(a[2]=="t"and A(a[1]))or(b and Y(a)[b])or""local ap=ac and 9 or(a[2]=="t"and(a[4]or 40))or a[6]or 80 local aJ=ac and ap or math.max(#e,aO-aj()+#e)j={f=a,i=b,s=e,at=#e,back=aV,num=ac,max=math.min(ap,aJ),full=aJ<ap}y=(e==""and not ac)and 1 or 0 if ac then w,v=1,1 elseif v==1 then w,v=1,2 end l="kb"end local function aD()local a=j.f local e=(j.s:gsub("^%s+",""):gsub("%s+$",""))local c=a[2]if c=="t"then i[a[1]]=e elseif c=="l"then local d=Y(a)if j.i then if e==""then table.remove(d,j.i)else d[j.i]=e end elseif e~=""then d[#d+1]=e g.items=math.min(#d+1,a[4])end elseif e~=""then local m,T=e:match("^(%d*)/(%d+)$")local at,k=e:match("^([+-]?)(%d+)$")if c=="p"and T then i[a[1].."_"]=tonumber(T)if m~=""then i[a[1]]=tonumber(m)end i[a[1]]=Q(a)elseif k then k=tonumber(k)local U=c=="p"and Q(a)or s(a[1])if c=="n"and a[4]<0 then if at=="-"then k=-k end elseif at=="+"then k=U+k elseif at=="-"then k=U-k end if c=="p"then i[a[1]]=math.max(0,math.min(I(a),k))else i[a[1]]=math.max(a[4],math.min(a[5],k))end else t="That isn't a number: "..e end end l,j=j.back,nil S()end local function aI()local h=G(w,v)local e,x=j.s,j.at if h=="<"then if x>0 then j.s,j.at=e:sub(1,x-1)..e:sub(x+1),x-1 end elseif h=="^"then y=(y+1)%3 elseif h=="["then j.at=math.max(0,x-1)elseif h=="]"then j.at=math.min(#e,x+1)elseif h==">"then aD()elseif#e>=j.max then t=j.full and"Sheet full: shorten a list or note"or"That's as long as it can be"else if h=="_"then h=" "elseif y==0 then h=h:lower()end j.s,j.at=e:sub(1,x)..h..e:sub(x+1),x+1 if y==1 and h:match("%a")then y=0 end end end local function a4(c)if c=="top"then aD()elseif c=="ok"then aI()elseif c=="left"or c=="right"then local n,h=c=="left"and-1 or 1,G(w,v)repeat w=(w-1+n)%11+1 until h~="_"or G(w,v)~="_"elseif c=="up"or c=="down"then v=(v-1+(c=="up"and-1 or 1))%5+1 end end function touch(B,m,T)if B~="board"or l~="kb"or not aw[T]or m<1 or m>11 then return end t=nil w,v=m,T aI()end local function a5()local J,a7={},G(w,v)=="_"for L=1,5 do local d={}for p=1,11 do local h=G(p,L)local ag=L==v and(p==w or(a7 and h=="_"))local O,aG,b=ag and 1 or 0,aR:find(h,1,true)if h:match("%u")then b=ag and h:byte()-65 elseif h:match("%d")then b=ag and 26+h:byte()-48 elseif aG then b=(ag and 35+aG)or(h=="/"and 45)elseif h=="<"then b=46+O elseif h=="^"then b=48+y*2+O elseif h=="["then b=54+O elseif h=="]"then b=56+O elseif h=="_"then b=58+O*3+(G(p-1,L)~="_"and 0 or G(p+1,L)~="_"and 2 or 1)else b=64+O end d[p]=b and bf(b)or("{"..h.."}")end J[L]=table.concat(d)end return table.concat(J,"/")end local function ba(a)local c,f=a[2],A(a[1])if N==a then return"Press OK again: "..a[3]end if a.s then return a.s(f)end if c=="t"then return a[3]..": "..(f~=""and f or"-")end if c=="n"then return a[3]..": "..f end if c=="p"then return a[3]..": "..(I(a)>0 and Q(a).." / "..I(a)or"none")end if c=="k"then return a[4][f%#a[4]+1].."  "..a[3]end if c=="c"then return a[3]..": "..a[4][f%#a[4]+1]end if c=="l"then return a[3].." ("..#Y(a)..")"end return a[3]end local function aC(a,n)local c=a[2]if c=="n"then i[a[1]]=math.max(a[4],math.min(a[5],s(a[1])+n))elseif c=="p"then i[a[1]]=math.max(0,math.min(I(a),Q(a)+n))elseif c=="k"or c=="c"then i[a[1]]=(s(a[1])+n)%#a[4]else return end S()end local function J(C,U,ar)local aM=math.max(1,math.min(U-ar//2,#C-ar+1))for b=aM,math.min(#C,aM+ar-1)do snail.row(C[b]:sub(1,90),b==U)end end local function ak(b,n,k)return(b-1+n)%k+1 end local function aL(b)local V=z[b][2]return#V==1 and V[1][2]=="l"end local function aF(b)R=b if aL(b)then u,g.items,l=z[b][2][1],1,"items"else g["s"..b]=g["s"..b]or 1 l="sect"end end local function a8(e)E=e a0((snail.cache("c"..e)))g.sheet,l=1,"sheet"end local function a6()local aj={}for M,r in ipairs(o)do aj[r.slot]=true end local e=1 while aj[e]do e=e+1 end E,i=e,{}o[#o+1]={slot=e,sum=am()}g.list,g.sheet=#o,1 as()S()aF(1)g.s1=1 an(F[ay],nil,"sect")end local function a_()snail.cache("c"..E,nil)for b,r in ipairs(o)do if r.slot==E then table.remove(o,b);break end end as()t=ab().." was deleted."E,i,l=0,{},"list"g.list=math.max(1,math.min(g.list,#o))end function key(c)if l=="kb"then t=nil;a4(c);return end if l=="list"and c=="top"then return end t=nil local m=N N=nil local n=(c=="up"and-1)or(c=="down"and 1)local _=(c=="left"and-1)or(c=="right"and 1)if l=="list"then local k=#o+(#o<ax and 1 or 0)+1 if n then g.list=ak(g.list,n,k)elseif c=="ok"then local r=o[g.list]if r then a8(r.slot)elseif g.list==k then l,af="about",0 else a6()end end elseif l=="about"then if n then af=math.max(0,math.min(#al-1,af+n))elseif c=="top"or c=="ok"then l="list"end elseif l=="sheet"then if n then g.sheet=ak(g.sheet,n,#z+1)elseif c=="top"then S();l="list"elseif c=="ok"then if g.sheet<=#z then aF(g.sheet)elseif m=="del"then a_()else N="del"end end elseif l=="sect"then local V,au=z[R][2],"s"..R local a=V[g[au]]if n then g[au]=ak(g[au],n,#V)elseif c=="top"then l="sheet"elseif _ then aC(a,_)elseif c=="ok"then local B=a[2]if B=="t"or B=="n"or B=="p"then an(a,nil,"sect")elseif B=="k"or B=="c"then aC(a,1)elseif B=="l"then u,g.items,l=a,1,"items"elseif m==a then t=a[4]()or a[5]S()else N=a end end elseif l=="items"then local d=Y(u)if n then g.items=ak(g.items,n,#d+(#d<u[4]and 1 or 0))elseif c=="top"then l=aL(R)and"sheet"or"sect"elseif c=="ok"then an(u,d[g.items]and g.items,"items")elseif _ and d[g.items]and d[g.items+_]then local Z=g.items+_ d[Z],d[g.items]=d[g.items],d[Z]g.items=Z S()end end end local aQ={t="BACK back  OK edit  UP/DN move",n="OK type  L/R -1/+1  UP/DN move",p="OK type  L/R -1/+1  UP/DN move",k="OK or L/R change  UP/DN move",c="OK or L/R change  UP/DN move",l="BACK back  OK open  UP/DN move",a="BACK back  OK twice to do it",}local function a1()local a=j.f snail.title(j.i==nil and a[2]=="l"and"New "..a[5]or a[2]=="l"and"Edit "..a[5]or a[3])local W if a[2]=="p"then W="Now "..Q(a).." / "..I(a)..". Type -8 for damage, +5 to heal, 12 to set, or 12/30 to set the most too."elseif j.num and a[4]<0 then W="Now "..D(s(a[1]))..". Type the new value, like 3 or -1."elseif j.num then W="Now "..s(a[1])..". Type 12 to set it, or +2 / -2 to change it."else W=#j.s.." of "..j.max.." letters"end snail.status(t or(y==2 and"CAPS"or y==1 and"Shift"or""))snail.text(j.s:sub(1,j.at).."|"..j.s:sub(j.at+1))snail.small(W)snail.board(a5())snail.hint("arrows move  BACK done  OK type")end function draw()if l=="kb"then return a1()end if l=="list"then snail.title(aN)if t then snail.status(t)end local d={}for b,r in ipairs(o)do d[b]=r.sum end if#o<ax then d[#d+1]="+ New character"end d[#d+1]="About and credits"J(d,g.list,13)if#o==0 then snail.small("Make a character to start. Sheets are kept on this X3.")end snail.hint("BACK leave  OK open  UP/DN move")return end if l=="about"then snail.title("About")for b=af+1,#al do snail.small(al[b])end snail.hint("BACK back  UP/DN scroll")return end if l=="sheet"then snail.title(ab())snail.status(t or am())local d={}for b,e in ipairs(z)do d[b]=e[1]..(e.s and("  "..e.s())or"")end d[#d+1]=N=="del"and"Press OK again to delete "..ab()or"Delete this character"J(d,g.sheet,13)snail.hint("BACK characters  OK open  UP/DN move")return end if l=="sect"then local e=z[R]local b=g["s"..R]snail.title(e[1])snail.status(t or ab())if e.h then snail.small(e.h())end local d={}for Z,a in ipairs(e[2])do d[Z]=ba(a)end J(d,b,12)snail.hint(aQ[e[2][b][2]])return end local d,C=Y(u),{}snail.title(u[3])snail.status(t or(ab().." - "..#d.." of "..u[4]))if u.h then snail.small(u.h())end for b,f in ipairs(d)do C[b]=f end if#d<u[4]then C[#C+1]="+ Add "..u[5]end J(C,g.items,12)snail.hint(d[g.items]and"OK edit  L/R reorder  UP/DN move"or"BACK back  OK add  UP/DN move")end function start()local e=snail.load()if type(e)=="string"and e:sub(1,2)=="S1"then for bd,ai in e:gmatch("\n(%d)|([^\n]*)")do o[#o+1]={slot=tonumber(bd),sum=ai}end end end
