--!name Solo Oracle
--!icon dice
--!category Games
local ah,W,ap,ag=8,12,28,40 local X={"Impossible","Very unlikely","Unlikely","Somewhat unlikely","50/50","Somewhat likely","Likely","Very likely","Certain"}local an={3,12,25,38,50,62,75,88,97}local af={"Yes, and","Yes","Yes, but","No, but","No","No, and"}local aI={"Yes, and something extra goes your way.","A clear yes.","Yes, but it comes with a cost or a catch.","No, but you get something to work with.","A clear no.","No, and things get worse.",}local aF="A new face shows up|Someone you know makes a move|An enemy gains ground|An ally asks for help|".."*A thread moves forward|*A thread hits a snag|*A thread takes a sharp turn|The past comes back|".."The place itself changes|Good fortune comes your way|Trouble lands on you|A secret comes to light|".."A strange sign or omen|News arrives from far away|Something you carry matters|A hard choice appears|".."Time is running out|A moment of calm|Someone wants a deal|A rival steps in"local aC="Abandon|Accuse|Aid|Ambush|Arrive|Bargain|Betray|Break|Build|Burn|Capture|Change|Chase|Claim|".."Conceal|Corrupt|Curse|Deceive|Defend|Delay|Deliver|Demand|Destroy|Discover|Divide|Escape|Expose|".."Fail|Follow|Forgive|Gather|Guard|Guide|Harm|Heal|Hunt|Imprison|Inherit|Inspire|Invade|Lose|Lure|".."Mourn|Negotiate|Oppose|Overthrow|Persuade|Plot|Protect|Provoke|Pursue|Rebuild|Refuse|Release|".."Rescue|Return|Reveal|Ruin|Sacrifice|Search|Seize|Separate|Spy|Steal|Summon|Test|Threaten|Trade|".."Transform|Trap|Trust|Uncover|Unite|Vanish|Wake|Warn"local aK="Alliance|Ambition|Animal|Army|Relic|Beast|Blood|Bond|Border|Bridge|Burden|Child|Coin|Crown|".."Debt|Dream|Enemy|Faith|Family|Fear|Fire|Food|Fortress|Friend|Gate|Gift|Gold|Grave|Guild|Home|Honour|".."Hope|Illness|Journey|Key|Knowledge|Land|Law|Leader|Letter|Light|Love|Machine|Magic|Map|Memory|".."Message|Monster|Mystery|Oath|Path|Plan|Power|Prisoner|Promise|Rival|Ruin|Rumour|Secret|Shelter|".."Ship|Shadow|Spirit|Storm|Stranger|Temple|Threat|Treasure|Truth|Vengeance|War|Water|Weapon|Wealth|Wound"local aD="It happens later than you planned|Someone unexpected is already there|A key person is missing|".."The place is not as you remember|It's quieter than you feared|It's more dangerous than you thought|".."You arrive in the middle of something|The weather turns against you|Something you need is gone|".."Someone got there first|What you want has moved|A friendly face is cold now|You were expected|".."Something is on fire|There's a crowd|It looks like a trap|A rival offers a deal|Your way out is blocked"local ao={"Expected","Altered","Interrupted"}local V={"d4","d6","d8","d10","d12","d20","d100","2d6","3d6","d66","4dF"}local o,q,H,O=5,0,5,6 local f,l={},{}local j,v,n,t local R,aj={},{}local b,m,i,Y="menu",1,nil,nil local J local function aZ(d,k)return d:match(("[^|]*|"):rep(k-1).."([^|]*)")end local function ad(d)return aZ(d,math.random(select(2,d:gsub("|",""))+1))end local function N(I,aV,aP)return math.max(aV,math.min(aP,I))end local function z()local function av()local S={"O1|"..o.."|"..q.."|"..H.."|"..O}for aq,g in ipairs(f)do S[#S+1]="t"..g end for aq,aQ in ipairs(l)do S[#S+1]="j"..aQ end return table.concat(S,"\n")end local d=av()while#d>1023 and#l>0 do l[#l]=nil d=av()end snail.save(d)end local function aW()local d=snail.load()if type(d)~="string"or d:sub(1,3)~="O1|"then return end for ak in d:gmatch("[^\n]+")do local e,I=ak:sub(1,1),ak:sub(2)if e=="O"then local p,F,G,u=ak:match("^O1|(%d+)|(%d+)|(%d+)|(%d+)")o,q=N(tonumber(p)or 5,1,9),tonumber(F)or 0 H,O=N(tonumber(G)or 5,1,9),N(tonumber(u)or 6,1,#V)elseif e=="t"and#f<ah then f[#f+1]=I:sub(1,ap)elseif e=="j"and#l<W then l[#l+1]=I:sub(1,ag)end end end local function aa(d)table.insert(l,1,((q>0 and"S"..q.." "or"")..d):sub(1,ag))l[W+1]=nil i="Written in the journal"z()end local function Z()local B=ad(aF)local u={f=B:gsub("^%*",""),a=ad(aC),s=ad(aK)}if B:sub(1,1)=="*"then u.th=#f>0 and f[math.random(#f)]or""end return u end local function aM()local D,x=an[H],math.random(100)local a=(5+2*o)local aB,au=math.max(1,D*a//100),math.max(1,(100-D)*a//100)local p if x<=D then p=x<=aB and 1 or x>D-aB and 3 or 2 else p=x>100-au and 6 or x<=D+au and 4 or 5 end if j then table.insert(aj,1,X[j.o]..": "..af[j.a]..(j.e and", twist"or"").." ("..j.r..")")aj[5]=nil end j={r=x,p=D,a=p,o=H}if math.random(100)<=2*o then j.e=Z()end end local function aN()q=q+1 local x,c=math.random(100),4*o n={r=x,n=q,k=x<=c and 3 or x<=8+7*o and 2 or 1}if n.k==3 then n.e=Z()elseif n.k==2 then n.alt=ad(aD)end z()end local function a0()local G=V[O]local ac,d=G:match("^(%d*)d(%w+)$")ac=tonumber(ac)or 1 local T,s,r={},0 if d=="F"then for c=1,4 do local I=math.random(3)-2 s=s+I T[c]=I>0 and"+"or I<0 and"-"or"0"end s=(s>0 and"+"or"")..s r="4dF: "..table.concat(T," ").." = "..s elseif d=="66"then local p,F=math.random(6),math.random(6)s=p*10+F r="d66: "..p.." then "..F.." = "..s else for c=1,ac do T[c]=math.random(tonumber(d))s=s+T[c]end r=G..": "..(ac>1 and table.concat(T," + ").." = "or"")..s end if t then table.insert(R,1,t.x);R[5]=nil end t={t=s,x=r}end local aE="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function a2(c)return aE:sub(c+1,c+1)end local Q={"1234567890<","QWERTYUIOP'","ASDFGHJKL-+","^ZXCVBNM,./","()[_____]?>"}local aJ="'-+,./()?"local h local C,w,E=1,2,1 local function K(r,M)return Q[M]:sub(r,r)end local function at(B,c)local d=c and(B=="t"and f or l)[c]or""h={f=B,i=c,s=d,at=#d,max=B=="t"and ap or ag-4}E=d==""and 1 or 0 if w==1 then C,w=1,2 end b="kb"end local function as()local d=(h.s:gsub("^%s+",""):gsub("%s+$",""))local B,c=h.f,h.i b,h,m=B=="t"and"threads"or"journal",nil,c or m if d==""then return end if B=="t"then if c then f[c]=d else f[#f+1]=d;m=#f end i=c and"Thread changed"or"Thread added"z()elseif c then l[c]=d;z()else aa(d);m=1 end end local function aw()local e=K(C,w)local d,A=h.s,h.at if e=="<"then if A>0 then h.s,h.at=d:sub(1,A-1)..d:sub(A+1),A-1 end elseif e=="^"then E=(E+1)%3 elseif e=="["then h.at=math.max(0,A-1)elseif e=="]"then h.at=math.min(#d,A+1)elseif e==">"then as()elseif#d>=h.max then i="That's as long as it can be"else if e=="_"then e=" "elseif E==0 then e=e:lower()end h.s,h.at=d:sub(1,A)..e..d:sub(A+1),A+1 if E==1 and e:match("%a")then E=0 end end end local function aR(k)if k=="top"then as()elseif k=="ok"then aw()elseif k=="left"or k=="right"then local G,e=k=="left"and-1 or 1,K(C,w)repeat C=(C-1+G)%11+1 until e~="_"or K(C,w)~="_"elseif k=="up"or k=="down"then w=(w-1+(k=="up"and-1 or 1))%#Q+1 end end function touch(aT,p,F)if aT~="board"or b~="kb"or p<1 or p>11 or not Q[F]or K(p,F)==" "then return end i=nil C,w=p,F aw()end local function aS()local y,a_={},K(C,w)=="_"for M=1,#Q do local g={}for r=1,11 do local e=Q[M]:sub(r,r)local _=M==w and(r==C or(a_ and e=="_"))local P,D,c=_ and 1 or 0,aJ:find(e,1,true)if e:match("%u")then c=_ and e:byte()-65 elseif e:match("%d")then c=_ and 26+e:byte()-48 elseif D then c=(_ and 35+D)or(e=="/"and 45)elseif e=="<"then c=46+P elseif e=="^"then c=48+E*2+P elseif e=="["then c=54+P elseif e=="]"then c=56+P elseif e=="_"then c=58+P*3+(K(r-1,M)~="_"and 0 or K(r+1,M)~="_"and 2 or 1)else c=64+P end g[r]=c and a2(c)or("{"..e.."}")end y[M]=table.concat(g)end return table.concat(y,"/")end local am={"ask","event","scene","dice","threads","journal","tension","help"}local function ae()if b=="menu"then return am end if b=="ask"then return j and{"odds","tension","log"}or{"odds","tension"}end if b=="event"then return{"again","log"}end if b=="scene"then return n and{"next","calm","wild","log"}or{"next","calm","wild"}end if b=="dice"then return t and{"die","log"}or{"die"}end if b=="threads"or b=="journal"then local g={}for c=1,#(b=="threads"and f or l)do g[c]=c end if b=="threads"and#f<ah then g[#g+1]="add"end if b=="journal"then g[#g+1]="add"end if b=="threads"and#f>1 then g[#g+1]="random"end return g end if b=="thread"then return{"edit","top","close"}end if b=="entry"then return{"edit","delete"}end return{}end local function a1(a)if type(a)=="number"then return b=="threads"and f[a]or l[a]end if a=="ask"then return"Ask a yes/no question"end if a=="event"then return"Random event"end if a=="scene"then return q>0 and"Scene check (scene "..q..")"or"Scene check"end if a=="dice"then return"Roll dice"end if a=="threads"then return"Threads ("..#f..")"end if a=="journal"then return"Journal ("..#l..")"end if a=="tension"then return"Tension: "..o.." of 9"end if a=="help"then return"How it works"end if a=="odds"then return"Odds: "..X[H].." ("..an[H].."% yes)"end if a=="log"then return"Write it in the journal"end if a=="again"then return"Roll another event"end if a=="next"then return"Start scene "..q+1 end if a=="calm"then return"Scene went my way: tension -1"end if a=="wild"then return"Scene got away from me: tension +1"end if a=="die"then return"Dice: "..V[O]end if a=="add"then return b=="threads"and"+ Add a thread"or"+ Write an entry"end if a=="random"then return"Pick a thread at random"end if a=="edit"then return"Change the words"end if a=="top"then return"Move to the top"end if Y==a then return"Press OK again to "..a end return a=="close"and"Close this thread"or"Delete this entry"end local function ax(G)o=N(o+G,1,9)i="Tension "..o z()end local function aX()if b=="ask"then aa(X[j.o].."? "..af[j.a]..(j.e and", twist"or""))elseif b=="event"then aa(v.a.." / "..v.s)elseif b=="scene"then local d="Scene "..n.n..": "..ao[n.k]table.insert(l,1,d)l[W+1]=nil i="Written in the journal"z()else aa(t.x)end end local function aO(d)b,m=d,1 if d=="event"then v=Z()end end local function aL(a,k,p)local ab=(k=="left"and-1)or(k=="right"and 1)if ab then if a=="tension"then ax(ab)elseif a=="odds"then H=N(H+ab,1,9);z()elseif a=="die"then O=(O-1+ab)%#V+1 if t then table.insert(R,1,t.x);R[5]=nil end t=nil z()end return end if k~="ok"then return end if b=="menu"then if a=="tension"then i="LEFT and RIGHT change the tension"else aO(a)end elseif a=="odds"or a=="tension"then aM()elseif a=="log"then aX()elseif a=="again"then v=Z()elseif a=="next"then aN()elseif a=="calm"or a=="wild"then ax(a=="calm"and-1 or 1)elseif a=="die"then a0()elseif type(a)=="number"then J,m=a,1 b=b=="threads"and"thread"or"entry"elseif a=="add"then at(b=="threads"and"t"or"j")elseif a=="random"then i="Thread: "..f[math.random(#f)]elseif a=="edit"then at(b=="thread"and"t"or"j",J)elseif a=="top"then table.insert(f,1,table.remove(f,J))b,m="threads",1 z()elseif p~=a then Y=a else table.remove(a=="close"and f or l,J)i=a=="close"and"Thread closed"or"Entry deleted"b,m=a=="close"and"threads"or"journal",1 z()end end local ai={ask="menu",event="menu",scene="menu",dice="menu",threads="menu",journal="menu",help="menu",thread="threads",entry="journal"}function key(k)if b=="kb"then i=nil;aR(k);return end if b=="menu"and k=="top"then return end i=nil local p=Y Y=nil local g=ae()if k=="top"then if ai[b]=="menu"then for c,a in ipairs(am)do if a==b then m=c end end else m=J end b=ai[b]return end if#g==0 then return end if k=="up"then m=(m-2)%#g+1 elseif k=="down"then m=m%#g+1 else aL(g[m],k,p)end m=N(m,1,#ae())end local U=("...................."):rep(3,"/")local function y(al)local g=ae()local aA=math.max(1,math.min(m-al//2,#g-al+1))for c=aA,math.min(#g,aA+al-1)do snail.row(a1(g[c]):sub(1,90),c==m)end end local function aY()snail.board(("#"):rep(o)..("o"):rep(9-o),"small")end local function ar(g)if#g>0 then snail.rule()end for c=1,#g do snail.small("Before: "..g[c])end end local function az()if#f==0 then return end snail.rule()for c=1,math.min(4,#f)do snail.small("Thread: "..f[c])end end local function ay(u,aU)snail.text(aU..u.f)if u.th then snail.text(u.th~=""and"Thread: "..u.th or"(No threads yet: use your current goal)")end snail.text("Think: "..u.a.." / "..u.s)end local aH={odds="BACK menu  OK ask  L/R odds  UP/DN move",tension="BACK menu  OK ask  L/R tension  UP/DN move",die="BACK menu  OK roll  L/R dice  UP/DN move",}local aG={"Ask: pick the odds and press OK. An 'and' makes the answer stronger, a 'but' softens it.","Tension, 1 to 9, is how wild the story is. Higher: more twists, extreme answers and odd scenes.","Scene check: one as each scene starts. Altered: change one thing. Interrupted: play the event.","Random event: read the focus and the two words and let them suggest what happens.","Threads are your open goals and mysteries; events can pick one. The journal keeps short notes.",}function draw()local a=ae()[m]if b=="kb"then snail.title(h.i and"Change the words"or h.f=="t"and"New thread"or"Journal entry")snail.status(i or(E==2 and"CAPS"or E==1 and"Shift"or""))snail.text(h.s:sub(1,h.at).."|"..h.s:sub(h.at+1))snail.small(h.f=="t"and"A goal, a mystery or a loose end, like Find the lost heir."or"A short note. Up to "..h.max.." letters; the scene number is added.")snail.board(aS())snail.hint("arrows move  BACK done  OK press")return end if b=="menu"then snail.title("Solo Oracle")snail.status(i or(q>0 and"Scene "..q.."  -  tension "..o or"Tension "..o))y(12)aY()snail.hint(a=="tension"and"BACK leave  L/R change  UP/DN move"or"BACK leave  OK choose  UP/DN move")return end if b=="ask"then snail.title("Ask the oracle")snail.status(i or"Tension "..o)if j then snail.board(U,nil,af[j.a]:upper())snail.text("Rolled "..j.r..", yes on "..j.p.." or less ("..X[j.o]..")")if j.a~=2 and j.a~=5 then snail.small(aI[j.a])end if j.e then ay(j.e,"Twist! ")end else snail.small("Think of a yes/no question, set the odds, then press OK.")end y(6)ar(aj)elseif b=="event"then snail.title("Random event")if i then snail.status(i)end snail.board(U,nil,v.f)if v.th then snail.text(v.th~=""and"Thread: "..v.th or"(No threads yet: use your current goal)")end snail.text("Think: "..v.a.." / "..v.s)y(6)if not v.th then az()end elseif b=="scene"then snail.title(q>0 and"Scene "..q or"Scene check")snail.status(i or"Tension "..o)if n then snail.board(U,nil,ao[n.k]:upper())snail.text("Rolled "..n.r.." at tension "..o)if n.k==1 then snail.small("Play the scene as you pictured it.")elseif n.k==2 then snail.text("Change one thing: "..n.alt)else ay(n.e,"Instead: ")end else snail.small("As each scene starts, say what you expect to happen, then check it.")end y(6)if not(n and n.e and n.e.th)then az()end elseif b=="dice"then snail.title("Dice")if i then snail.status(i)end if t then snail.board(U,nil,t.t)snail.text(t.x)end y(4)ar(R)elseif b=="threads"or b=="journal"then local L=b=="threads"snail.title(L and"Threads"or"Journal")snail.status(i or(L and#f.." of "..ah or#l.." of "..W..", newest first"))y(14)if L and#f==0 then snail.small("Threads are open goals and mysteries. Random events can pick one.")end if not L and#l==0 then snail.small("Short notes as you play. Results can be written here too.")end elseif b=="thread"or b=="entry"then local L=b=="thread"snail.title(L and"Thread"or"Journal entry")if i then snail.status(i)end snail.text(L and f[J]or l[J])y(4)else snail.title("How it works")for aq,d in ipairs(aG)do snail.text(d)end snail.hint("BACK menu")return end snail.hint(aH[a]or((a=="close"or a=="delete")and"BACK back  OK twice  UP/DN move")or"BACK "..(ai[b]=="menu"and"menu"or"back").."  OK choose  UP/DN move")end function start()math.randomseed(os.time())aW()end
