3 echo "input: $boss_hit $boss_damage"
6 [[ ! ${DEBUG+set} ]] && return
9 spells
=(missile drain shield poison recharge
)
10 cost
=(53 73 113 173 229)
12 # battle spell hit mana spent shield poison recharge boss_hit
13 # return 0 (and sets $spent) for win, 1 for loss/not possible, 2 + output
14 # to continue, 3 to prune path as non-maximal
16 debug
" battle $@" >&3
17 if [[ $# != 8 ]]; then
18 echo " invalid input" >&3
22 if [[ $spent -gt $minspent ]]; then
23 debug
" not a minimal path, pruning rather than following"
26 local spell
=$1 hit
=$2 mana
=$3 shield
=$5 poison
=$6 recharge
=$7 boss
=$8
28 debug
" player has $hit hit points, $((7*!!shield)) armor, $mana mana"
29 debug
" boss has $boss hit points"
30 if [[ ! $part1 && $
((--hit)) == 0 ]]; then
31 debug
" hard mode, hit points ran out"
34 if [[ $shield -gt 0 ]]; then
36 debug
" shield effect down to $shield"
38 if [[ $shield -gt 0 && $spell == 2 ]]; then
39 debug
" shield effect still active"
42 if [[ $poison -gt 0 ]]; then
45 debug
" poison effect down to $poison"
47 if [[ $poison -gt 0 && $spell = 3 ]]; then
48 debug
" poison effect still active"
51 if [[ $recharge -gt 0 ]]; then
54 debug
" recharge effect down to $recharge, mana up to $mana"
56 if [[ $recharge -gt 0 && $spell = 4 ]]; then
57 debug
" recharge effect still active"
60 if [[ ${cost[$spell]} -gt $mana ]]; then
61 debug
" not enough mana left"
64 debug
" casting ${spells[$spell]}"
65 mana
=$
((mana
- cost
[spell
]))
66 spent
=$
((spent
+ cost
[spell
]))
68 0) boss
=$
((boss-4
)) ;;
69 1) boss
=$
((boss-2
)) hit
=$
((hit
+2)) ;;
74 if [[ $boss -le 0 ]]; then
75 debug
" boss defeated"
80 debug
" player has $hit hit points, $((7*!!shield)) armor, $mana mana"
81 debug
" boss has $boss hit points"
82 if [[ $shield -gt 0 ]]; then
84 debug
" shield effect down to $shield"
86 if [[ $poison -gt 0 ]]; then
89 debug
" poison effect down to $poison"
91 if [[ $recharge -gt 0 ]]; then
94 debug
" recharge effect down to $recharge, mana up to $mana"
96 if [[ $boss -le 0 ]]; then
97 debug
" boss defeated"
100 hit
=$
((hit
- boss_damage
+ 7*!!shield
))
101 if [[ $hit -le 0 ]]; then
102 debug
" player defeated"
105 echo $hit $mana $spent $shield $poison $recharge $boss
108 hit
=${1-50} mana
=${2-500}
109 echo $hit $mana 0 0 0 0 $boss_hit > tmp
113 while [[ -s tmp
]]; do
114 echo "computing generation $((++gen)) from $(wc -l < tmp) scenarios"
119 if [[ $?
== 0 ]]; then
121 (( spent
< minspent
)) && minspent
=$spent
125 sort -k4,4n
-k8,8rn
< tmp2 |
uniq > tmp
128 echo "traced $scenarios possible sequences over $gen generations"
129 echo "computed $wins different win paths; cheapest cost $minspent mana"