combatbud/combatbud.bqn

105 lines
5.6 KiB
BQN
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env -S rlwrap BQN
# String to Int list
#ToInts ← (+´(10⋆⌽∘↕∘≠)⊸×∘-⟜'0')¨ (+`׬)⊸-∘=⟜' '⊸⊔
ToInts (+´(10)×-'0')¨ (+`׬)-¬('0'+10)
# Is string a die roll
IsRoll´"d0123456789-+"
# Takes a die string and rolls it
Roll(´'d'=)(+´(10)×-'0'){ 𝕊str:
nums 0/ (+´(10)×-'0')¨(+`׬)-¬('0'+10) 𝕩
adm {a,d,m: 𝕩; a,d:{´str"+-"? 1𝕩; 𝕩0}𝕩; d: 1d0} nums
+´(1+)(a •rand.Range d)+ (´'-'=𝕩)+,- m}
# A string with die rolls to Int list
RollToInts Roll¨ (+`׬)-=' '
# Rolls input and splits input into pairs
ToPairs(¯1+´(10)×-'0')¨, Roll¨ {𝕎𝕩}¨ ((<˘ 2) (+`׬)-=' ')
# Roll strings
RollString •Out( ' '¨) •Fmt¨RollToInts
# Mark the firsts, removing overlaps.
MarkFirst{l-1𝕨l=0(-1l×<)`(𝕩)𝕨𝕩}
# old‿new ReplaceAll string
ReplaceAll{fr𝕊𝕩: g(+`(»(f)fMarkFirst))𝕩 r¨((gf)/)g}
macros
# Read file of tab sepperated pairs, add contents to macro list.
FileErr{•Out "Macro file: "𝕩" doesn't exist"}
ReadMacros{macros ¨/ (+`׬)-=(@+9)¨ •Flines 𝕩}FileErr
ReadMacros"No Macros" "macros"
MacroReplace{𝕩 ReplaceAll´ macros}
# Create graveyard and turnOrder ⟨initiative, name, heath⟩
turnOrdergraveyard
Clear{𝕊: turnOrdergraveyard}
# Sorts the turn order acording to a list of indicies
Sort<(¨){𝕩 graveyard 𝔽 turnOrder 𝔽}
# General Error
Err{•Out "Don't understand input: "•Fmt 𝕩}
# Parses input and adds each foe to the turn order.
_CreateFoe{𝔽{0=𝕩 ? 0;
IsRoll𝕩 ? 𝔽 Roll,𝔾,Roll{𝕎𝕩}¨ 3𝕩 𝕊3𝕩;
𝔽 (Roll "d20")𝔾,Roll{𝕎𝕩}¨ 2𝕩 𝕊2𝕩}(< ¯1( ' '¨)){
¨/ (+`+» IsRoll¨) (+`׬)-=' '𝕩}ErrMacroReplace}
NewFoe,DeadFoe{𝕩_CreateFoe}¨{turnOrder ¨112 /𝕩}{graveyard ¨112 /𝕩}
# Parse a list of numbers, move said indices from turn order to grave
KillFoe{m¬(1+ turnOrder)𝕩
graveyard ¨ (¬m)/¨ turnOrderturnOrder m/¨} ToInts
# Parse a list of numbers, move said indices to turn order from grave
ResurrectFoe{m¬(1+ graveyard)𝕩
turnOrder ¨ (¬m)/¨ graveyardgraveyard m/¨} ToInts
# Hurt foes, id hurt id hurt format
HurtFoe{𝕊iddam: turnOrder (-dam)(id)(2)} ToPairs
# Heal foes, id heal id heal format
HealFoe{𝕊iddam: turnOrder ((id3turnOrder) +dam)(id)(2)} ToPairs
# Move Foes, id initative, id initative
MoveFoe{𝕊idinit: turnOrder init(id)(0)} ToPairs
# Turns a turn order into a string
OrderToString{𝕨 ˜´( " "¨)¨<˘>{•Fmt¨,,•Fmt¨{𝕎𝕩}¨𝕩}𝕩}
# Writes graveyard and turnorder to a file
Dump{𝕩 •Flines "add""add dead" OrderToString¨turnOrdergraveyard}
# Reads file, prints message is it doesn't exist
File•Flines{•Show "File '"𝕩"' doesn't exist. Nothing to read"}
# Displays each active character in a turn order.
Display{idinhp:•Outid,":",i,@+9,n," has ",hp," hit points"@}{
(𝔽 •Fmt¨(1101/))(4)˘> (<1+) 𝕩}
help"Commands:
roll: Roll a specified die. “roll 3d20-7”
add: Add foes to the turn order. “add grendi d4 d20+3 globlsmark 2d6”
add dead: Add foes to the graveyard. “add 17 King John 5”
display: Show turn order, formatted foe id, initative, name and hit points.
grave: Same as display but for graveyard
hurt: Using a id, hurt a foe. “hurt 2 d8 4 2d8+3”
heal: Using a id, heal a foe. “heal 1 2d6 1 2d6”
move: Using a id and a new initiative, move a foe. “move 3 d20 4 10”
kill: Using a id, move a foe to the graveyard. “kill 3 4 2”
resurrect: Using a id, move a foe from the graveyard. “resurrect 3 4 2”
file: Read in a file as if it was typed right in right now.
dump: Write out a file in a mannor that it can be read back in.
clear: Clear the turn order and graveyard.
help: Displays this message.
exit: Ends the program.
"
Read{
"roll"4𝕩 ? RollString 5𝕩 ; "add dead"8𝕩 ? DeadFoe 9𝕩 ;
"add"3𝕩 ? NewFoe 4𝕩 ; "move"4𝕩 ? MoveFoe 5𝕩 ;
"hurt"4𝕩 ? HurtFoe 5𝕩 ; "heal"4𝕩 ? HealFoe 5𝕩 ;
"kill"4𝕩 ? KillFoe 5𝕩 ; "resurrect"9𝕩 ? ResurrectFoe 10𝕩 ;
"display"𝕩 ? Display turnOrder ; "grave"𝕩 ? Display graveyard ;
"file"4𝕩 ? Read¨ File 5𝕩 ; "dump"4𝕩 ? Dump 5𝕩 ;
"macros"𝕩 ? •Show macros ; "macros"6𝕩 ? ReadMacros 7𝕩 ;
"clear"𝕩 ? Clear @ ; ("?"𝕩)"help"𝕩 ? •Out help ;
# Debug Commands
"Display"𝕩 ? •Show turnOrder ; "Grave"𝕩 ? •Show graveyard ;
"exit"𝕩 ? run0 ; 0}
run 1
{Sort @ Read •Getline 𝕩} •_While_ {𝕤run0} @