--!name Ranks & DCs Character Sheet
--!icon sword
--!category Games
local az=6 local aO=8000 local k,C={},{}local I=0 local function y(A)local g=k[A]if g==nil and C[A]then g=C[A].d end return g end local function p(A)return tonumber(y(A))or 0 end local function T(r)return r>=0 and"+"..r or tostring(r)end local function H(a)return tonumber(k[a[1].."_"])or a[4]or 0 end local function K(a)local ar=H(a)local g=tonumber(k[a[1]])or ar return g>ar and ar or g end local function Y(a)local e=k[a[1]]if type(e)~="table"then e={};k[a[1]]=e end return e end local aN,aA="Ranks & DCs Characters","nm"local an={"Ranks & DCs Character Sheet works with the ORC-licensed second-edition fantasy rules.","It is an independent product.","This product is licensed under the ORC License, located at the Library of Congress at","TX 9-307-067 and available online at various locations including www.paizo.com/orclicense,","www.azoralaboratories.com/orclicense and others.","All warranties are disclaimed as set forth therein.","This product includes material from Pathfinder Player Core (c) Paizo Inc.,","licensed under the ORC License.",}local am={"Strength","Dexterity","Constitution","Intelligence","Wisdom","Charisma"}local ax={"STR","DEX","CON","INT","WIS","CHA"}local ao={"st","dx","co","it","wi","ch"}local ae={"U","T","E","M","L"}local function ah()return math.max(1,p("lv"))end local function ab(b)return p(ao[b])end local function ai(n)n=n%5;return n>0 and 2*n+ah()or 0 end local aB={}for b=1,6 do local a={ao[b],"n",am[b],-5,10,0}a.s=function(g)return am[b].."   "..T(g)end aB[b]=a end local function R(A,_,o,aF,m)local a={A,"k",_,ae,m}a.s=function(g)return ae[g%5+1].."   "..(aF and aF()or _).." ("..ax[o]..")   "..T(ab(o)+ai(g))end return a end local function aJ(A,_,m)local a={A,"k",_,ae,m}a.s=function(g)return ae[g%5+1].."   ".._ end return a end local bb={R("pe","Perception",5,nil,1),R("fo","Fortitude",3,nil,1),R("re","Reflex",2,nil,1),R("wl","Will",5,nil,1),}local x={}local aS="Acrobatics2Arcana4Athletics1Crafting4Deception6Diplomacy6Intimidation6Medicine5Nature5".."Occultism4Performance6Religion5Society4Stealth2Survival5Thievery2"for a6,o in aS:gmatch("(%a+)(%d)")do x[#x+1]=R("k"..(#x+1),a6,tonumber(o))end for b=1,2 do local e="t"..b x[#x+1]=R("o"..b,"Lore "..b,4,function()local d=y(e)return d~=""and d.." Lore"or"Lore "..b end)end x[#x+1]={"t1","t","Lore 1 topic",24}x[#x+1]={"t2","t","Lore 2 topic",24}local M={{"tr","t","Tradition",20},{"sa","c","Spellcasting attribute",{"none","Intelligence","Wisdom","Charisma"}},aJ("sr","Spellcasting proficiency",1),{"fp","p","Focus points",0},}for b=1,10 do M[#M+1]={"r"..b,"p","Rank "..b.." slots",0}end M[#M+1]={"ca","l","Cantrips",20,"cantrip",50}M[#M+1]={"sl","l","Spells",40,"spell",60}local function a9()local a=C.hp local aE=math.max(1,ab(3))*ah()k.hp=math.min(H(a),K(a)+aE)k.fp=nil for b=1,10 do k["r"..b]=nil end return"Rested: +"..aE.." HP, slots and focus are back."end local function aV()return"Level up at 1000 XP ("..math.max(0,1000-p("xp")).." to go)"end local function aW()return(y("cl")~=""and y("cl").." "or"Level ")..ah()end local function aT()local e={}for b=1,6 do e[b]=ax[b]..T(p(ao[b]))end return table.concat(e," ")end local function bc()return"Ranks U T E M L: +2 each, plus your level"end local function be()return"Name your Lore skills at the bottom of the list"end local function aX()local o=p("ka")%6+1 return"Class DC "..(10+ab(o)+ai(p("kr"))).."   Perception "..T(ab(5)+ai(p("pe")))end local function aY()local a=C.hp return"HP "..K(a).."/"..H(a)..(p("th")>0 and" +"..p("th")or"").."  AC "..p("ac")end local function bg()local o=p("sa")if o==0 then return"Choose a spellcasting attribute for your spell DC and attack."end local E=ab(o+3)+ai(p("sr"))return"Spell DC "..(10+E).."   Spell attack "..T(E)end local function a1()return p("gp").." gp"end local D={{"Basics",{{"nm","t","Name",30},{"an","t","Ancestry",30},{"he","t","Heritage",30},{"bg","t","Background",30},{"cl","t","Class",30},{"lv","n","Level",1,20,1},{"xp","n","Experience points",0,9999,0},{"hr","n","Hero points",0,3,1},{"de","t","Deity",30},{"sz","t","Size",12},{"la","t","Languages",80},{"ts","t","Traits",60},},h=aV,s=aW},{"Attributes",aB,s=aT},{"Perception and saves",bb,h=bc},{"Skills",x,h=be},{"Combat",{{"hp","p","Hit points",10},{"th","n","Temporary hit points",0,999,0},{"ac","n","Armor class",0,60,10},{"sh","p","Shield hit points",0},{"sd","n","Speed (ft)",0,300,25},{"ka","c","Key attribute",am},aJ("kr","Class DC proficiency",1),{"dy","n","Dying",0,4,0},{"wd","n","Wounded",0,3,0},{"dm","n","Doomed",0,3,0},{"cn","t","Conditions",60},{"rw","t","Resistances and weaknesses",60},{"lr","a","Rest for the night",a9},},h=aX,s=aY},{"Strikes",{{"at","l","Strikes",20,"strike",70}}},{"Spells",M,h=bg},{"Feats and features",{{"ft","l","Feats and features",40,"feat",80}}},{"Inventory",{{"iv","l","Items",40,"item",60},{"bk","t","Bulk carried",20},{"gp","n","Gold (gp)",0,9999999,0},{"sv","n","Silver (sp)",0,9999999,0},{"cp","n","Copper (cp)",0,9999999,0},{"pp","n","Platinum (pp)",0,9999999,0},},s=a1},{"Notes",{{"no","l","Notes",40,"note",90}}},}local function ap()local j,h=y("nm"),y("cl")return(j~=""and j or"Unnamed")..(h~=""and" - "..h.." "..ah()or"")end for O,d in ipairs(D)do for O,a in ipairs(d[2])do assert(not C[a[1]],"two fields are called "..a[1])C[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 q={}local l,S,u,P,t="list",1,nil,nil,nil local af=0 local f={list=1,sheet=1,items=1}local function bd()local s={}for c,g in pairs(k)do if type(g)=="table"then if#g>0 then s[#s+1]=c.."="for b,r in ipairs(g)do if b>1 then s[#s+1]="|"end s[#s+1]=r end s[#s+1]="\n"end elseif g~=""then s[#s+1]=c.."="..g.."\n"end end return table.concat(s)end local function a_(d)k={}for c,g in(d or""):gmatch("([%w_]+)=([^\n]*)")do local a=C[c]if a and a[2]=="l"then local e={}for a2 in g:gmatch("[^|]+")do e[#e+1]=a2 end k[c]=e elseif a and a[2]=="t"then k[c]=g else k[c]=tonumber(g)end end end local function au()local s={"S1"}for O,n in ipairs(q)do s[#s+1]=n.slot.."|"..n.sum end snail.save(table.concat(s,"\n"))end local function U()if I==0 then return end snail.cache("c"..I,bd())local aj=ap():sub(1,60)for O,n in ipairs(q)do if n.slot==I and n.sum~=aj then n.sum=aj;au()end end end local function ac()local j=y(aA);return j~=""and j or"Unnamed"end local aP="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function bh(b)return aP:sub(b+1,b+1)end local ay={"1234567890<","QWERTYUIOP'","ASDFGHJKL-+","^ZXCVBNM,./","()[_____]?>"}local aR="'-+,./()?"local i local w,v,B=1,2,1 local function J(r,N)return ay[N]:sub(r,r)end local function ak()local j=0 for O,g in pairs(k)do if type(g)=="table"then for O,r in ipairs(g)do j=j+#r+1 end elseif type(g)=="string"then j=j+#g end end return j end local function aq(a,b,aU)local ad=a[2]=="n"or a[2]=="p"local d=(a[2]=="t"and y(a[1]))or(b and Y(a)[b])or""local as=ad and 9 or(a[2]=="t"and(a[4]or 40))or a[6]or 80 local aK=ad and as or math.max(#d,aO-ak()+#d)i={f=a,i=b,s=d,at=#d,back=aU,num=ad,max=math.min(as,aK),full=aK<as}B=(d==""and not ad)and 1 or 0 if ad then w,v=1,1 elseif v==1 then w,v=1,2 end l="kb"end local function aD()local a=i.f local d=(i.s:gsub("^%s+",""):gsub("%s+$",""))local c=a[2]if c=="t"then k[a[1]]=d elseif c=="l"then local e=Y(a)if i.i then if d==""then table.remove(e,i.i)else e[i.i]=d end elseif d~=""then e[#e+1]=d f.items=math.min(#e+1,a[4])end elseif d~=""then local o,E=d:match("^(%d*)/(%d+)$")local av,j=d:match("^([+-]?)(%d+)$")if c=="p"and E then k[a[1].."_"]=tonumber(E)if o~=""then k[a[1]]=tonumber(o)end k[a[1]]=K(a)elseif j then j=tonumber(j)local V=c=="p"and K(a)or p(a[1])if c=="n"and a[4]<0 then if av=="-"then j=-j end elseif av=="+"then j=V+j elseif av=="-"then j=V-j end if c=="p"then k[a[1]]=math.max(0,math.min(H(a),j))else k[a[1]]=math.max(a[4],math.min(a[5],j))end else t="That isn't a number: "..d end end l,i=i.back,nil U()end local function aI()local h=J(w,v)local d,z=i.s,i.at if h=="<"then if z>0 then i.s,i.at=d:sub(1,z-1)..d:sub(z+1),z-1 end elseif h=="^"then B=(B+1)%3 elseif h=="["then i.at=math.max(0,z-1)elseif h=="]"then i.at=math.min(#d,z+1)elseif h==">"then aD()elseif#d>=i.max then t=i.full and"Sheet full: shorten a list or note"or"That's as long as it can be"else if h=="_"then h=" "elseif B==0 then h=h:lower()end i.s,i.at=d:sub(1,z)..h..d:sub(z+1),z+1 if B==1 and h:match("%a")then B=0 end end end local function a3(c)if c=="top"then aD()elseif c=="ok"then aI()elseif c=="left"or c=="right"then local m,h=c=="left"and-1 or 1,J(w,v)repeat w=(w-1+m)%11+1 until h~="_"or J(w,v)~="_"elseif c=="up"or c=="down"then v=(v-1+(c=="up"and-1 or 1))%5+1 end end function touch(F,o,E)if F~="board"or l~="kb"or not ay[E]or o<1 or o>11 then return end t=nil w,v=o,E aI()end local function a4()local L,a7={},J(w,v)=="_"for N=1,5 do local e={}for r=1,11 do local h=J(r,N)local ag=N==v and(r==w or(a7 and h=="_"))local Q,aH,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 aH then b=(ag and 35+aH)or(h=="/"and 45)elseif h=="<"then b=46+Q elseif h=="^"then b=48+B*2+Q elseif h=="["then b=54+Q elseif h=="]"then b=56+Q elseif h=="_"then b=58+Q*3+(J(r-1,N)~="_"and 0 or J(r+1,N)~="_"and 2 or 1)else b=64+Q end e[r]=b and bh(b)or("{"..h.."}")end L[N]=table.concat(e)end return table.concat(L,"/")end local function ba(a)local c,g=a[2],y(a[1])if P==a then return"Press OK again: "..a[3]end if a.s then return a.s(g)end if c=="t"then return a[3]..": "..(g~=""and g or"-")end if c=="n"then return a[3]..": "..g end if c=="p"then return a[3]..": "..(H(a)>0 and K(a).." / "..H(a)or"none")end if c=="k"then return a[4][g%#a[4]+1].."  "..a[3]end if c=="c"then return a[3]..": "..a[4][g%#a[4]+1]end if c=="l"then return a[3].." ("..#Y(a)..")"end return a[3]end local function aC(a,m)local c=a[2]if c=="n"then k[a[1]]=math.max(a[4],math.min(a[5],p(a[1])+m))elseif c=="p"then k[a[1]]=math.max(0,math.min(H(a),K(a)+m))elseif c=="k"or c=="c"then k[a[1]]=(p(a[1])+m)%#a[4]else return end U()end local function L(G,V,at)local aM=math.max(1,math.min(V-at//2,#G-at+1))for b=aM,math.min(#G,aM+at-1)do snail.row(G[b]:sub(1,90),b==V)end end local function al(b,m,j)return(b-1+m)%j+1 end local function aL(b)local W=D[b][2]return#W==1 and W[1][2]=="l"end local function aG(b)S=b if aL(b)then u,f.items,l=D[b][2][1],1,"items"else f["s"..b]=f["s"..b]or 1 l="sect"end end local function a8(d)I=d a_((snail.cache("c"..d)))f.sheet,l=1,"sheet"end local function a5()local ak={}for O,n in ipairs(q)do ak[n.slot]=true end local d=1 while ak[d]do d=d+1 end I,k=d,{}q[#q+1]={slot=d,sum=ap()}f.list,f.sheet=#q,1 au()U()aG(1)f.s1=1 aq(C[aA],nil,"sect")end local function aZ()snail.cache("c"..I,nil)for b,n in ipairs(q)do if n.slot==I then table.remove(q,b);break end end au()t=ac().." was deleted."I,k,l=0,{},"list"f.list=math.max(1,math.min(f.list,#q))end function key(c)if l=="kb"then t=nil;a3(c);return end if l=="list"and c=="top"then return end t=nil local o=P P=nil local m=(c=="up"and-1)or(c=="down"and 1)local aa=(c=="left"and-1)or(c=="right"and 1)if l=="list"then local j=#q+(#q<az and 1 or 0)+1 if m then f.list=al(f.list,m,j)elseif c=="ok"then local n=q[f.list]if n then a8(n.slot)elseif f.list==j then l,af="about",0 else a5()end end elseif l=="about"then if m then af=math.max(0,math.min(#an-1,af+m))elseif c=="top"or c=="ok"then l="list"end elseif l=="sheet"then if m then f.sheet=al(f.sheet,m,#D+1)elseif c=="top"then U();l="list"elseif c=="ok"then if f.sheet<=#D then aG(f.sheet)elseif o=="del"then aZ()else P="del"end end elseif l=="sect"then local W,aw=D[S][2],"s"..S local a=W[f[aw]]if m then f[aw]=al(f[aw],m,#W)elseif c=="top"then l="sheet"elseif aa then aC(a,aa)elseif c=="ok"then local F=a[2]if F=="t"or F=="n"or F=="p"then aq(a,nil,"sect")elseif F=="k"or F=="c"then aC(a,1)elseif F=="l"then u,f.items,l=a,1,"items"elseif o==a then t=a[4]()or a[5]U()else P=a end end elseif l=="items"then local e=Y(u)if m then f.items=al(f.items,m,#e+(#e<u[4]and 1 or 0))elseif c=="top"then l=aL(S)and"sheet"or"sect"elseif c=="ok"then aq(u,e[f.items]and f.items,"items")elseif aa and e[f.items]and e[f.items+aa]then local Z=f.items+aa e[Z],e[f.items]=e[f.items],e[Z]f.items=Z U()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 a0()local a=i.f snail.title(i.i==nil and a[2]=="l"and"New "..a[5]or a[2]=="l"and"Edit "..a[5]or a[3])local X if a[2]=="p"then X="Now "..K(a).." / "..H(a)..". Type -8 for damage, +5 to heal, 12 to set, or 12/30 to set the most too."elseif i.num and a[4]<0 then X="Now "..T(p(a[1]))..". Type the new value, like 3 or -1."elseif i.num then X="Now "..p(a[1])..". Type 12 to set it, or +2 / -2 to change it."else X=#i.s.." of "..i.max.." letters"end snail.status(t or(B==2 and"CAPS"or B==1 and"Shift"or""))snail.text(i.s:sub(1,i.at).."|"..i.s:sub(i.at+1))snail.small(X)snail.board(a4())snail.hint("arrows move  BACK done  OK type")end function draw()if l=="kb"then return a0()end if l=="list"then snail.title(aN)if t then snail.status(t)end local e={}for b,n in ipairs(q)do e[b]=n.sum end if#q<az then e[#e+1]="+ New character"end e[#e+1]="About and credits"L(e,f.list,13)if#q==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,#an do snail.small(an[b])end snail.hint("BACK back  UP/DN scroll")return end if l=="sheet"then snail.title(ac())snail.status(t or ap())local e={}for b,d in ipairs(D)do e[b]=d[1]..(d.s and("  "..d.s())or"")end e[#e+1]=P=="del"and"Press OK again to delete "..ac()or"Delete this character"L(e,f.sheet,13)snail.hint("BACK characters  OK open  UP/DN move")return end if l=="sect"then local d=D[S]local b=f["s"..S]snail.title(d[1])snail.status(t or ac())if d.h then snail.small(d.h())end local e={}for Z,a in ipairs(d[2])do e[Z]=ba(a)end L(e,b,12)snail.hint(aQ[d[2][b][2]])return end local e,G=Y(u),{}snail.title(u[3])snail.status(t or(ac().." - "..#e.." of "..u[4]))if u.h then snail.small(u.h())end for b,g in ipairs(e)do G[b]=g end if#e<u[4]then G[#G+1]="+ Add "..u[5]end L(G,f.items,12)snail.hint(e[f.items]and"OK edit  L/R reorder  UP/DN move"or"BACK back  OK add  UP/DN move")end function start()local d=snail.load()if type(d)=="string"and d:sub(1,2)=="S1"then for bf,aj in d:gmatch("\n(%d)|([^\n]*)")do q[#q+1]={slot=tonumber(bf),sum=aj}end end end
