2007-08-15 10:37:35 -04:00
|
|
|
; a helper function which prints each argument seperated by a space
|
|
|
|
(define output
|
2007-08-16 10:21:37 -04:00
|
|
|
(lambda (. things)
|
|
|
|
(cond
|
|
|
|
((null? things) (display #\newline))
|
|
|
|
(else
|
|
|
|
(display (car things))
|
|
|
|
(display #\space)
|
|
|
|
(apply output (cdr things))))))
|
2007-08-15 10:37:35 -04:00
|
|
|
|
|
|
|
; a helper function which shows arguments before calling them
|
|
|
|
(define explicit-apply
|
2007-08-16 10:21:37 -04:00
|
|
|
(lambda (f . args)
|
|
|
|
(let ((result (apply f args)))
|
|
|
|
(output f args result)
|
|
|
|
result)))
|
2007-08-15 10:37:35 -04:00
|
|
|
|
|
|
|
; determine whether or not a given attack roll will hit
|
|
|
|
(define is-hit?
|
2007-08-16 10:21:37 -04:00
|
|
|
(lambda (roll attack ac)
|
|
|
|
(or (= roll 20) (and (< 1 roll) (>= (+ roll attack) ac)))))
|
2007-08-15 10:37:35 -04:00
|
|
|
|
|
|
|
; determine whether or not a given attack roll will crit
|
|
|
|
(define is-crit?
|
2007-08-16 10:21:37 -04:00
|
|
|
(lambda (roll attack ac threat)
|
|
|
|
(or (= roll 20) (and (is-hit? roll attack ac) (>= roll threat)))))
|
2007-08-15 10:37:35 -04:00
|
|
|
|
|
|
|
; determine the expected damage of a particular attack roll
|
|
|
|
(define roll-dmg
|
2007-08-16 10:21:37 -04:00
|
|
|
(lambda (roll attack ac dmg threat mult)
|
|
|
|
(cond
|
|
|
|
((is-crit? roll attack ac threat) (* dmg mult))
|
|
|
|
((is-hit? roll attack ac) dmg)
|
|
|
|
(else 0))))
|
2007-08-15 10:37:35 -04:00
|
|
|
|
|
|
|
; determine the expected damage across all attack rolls
|
|
|
|
(define expected-dmg
|
2007-08-16 10:21:37 -04:00
|
|
|
(lambda (. args)
|
|
|
|
(define adder
|
|
|
|
(lambda (total roll)
|
|
|
|
(if (> roll 20)
|
|
|
|
total
|
|
|
|
(adder (+ total (apply roll-dmg roll args))
|
|
|
|
(+ roll 1)))))
|
|
|
|
(/ (adder 0 1) 20)))
|
2007-08-15 10:37:35 -04:00
|
|
|
|
|
|
|
; find the best power attack score (and expected damage) versus an AC
|
|
|
|
(define find-best-power
|
2007-08-16 10:21:37 -04:00
|
|
|
(lambda (power-max attack ac dmg threat mult)
|
|
|
|
(define checker
|
|
|
|
(lambda (power best-power best-dmg)
|
|
|
|
(if (> power power-max)
|
|
|
|
(list best-power best-dmg)
|
|
|
|
(let* ((a (- attack power))
|
|
|
|
(d (+ dmg power))
|
|
|
|
(ed (expected-dmg a ac d threat mult)))
|
|
|
|
(if (> ed best-dmg)
|
|
|
|
(checker (+ power 1) power ed)
|
|
|
|
(checker (+ power 1) best-power best-dmg))))))
|
|
|
|
(checker 0 0 0)))
|
2007-08-15 10:37:35 -04:00
|
|
|
|
|
|
|
; iterate across a range of armor classes
|
|
|
|
(define iter
|
2007-08-16 10:21:37 -04:00
|
|
|
(let ((max-power 6)
|
|
|
|
(attack 6)
|
|
|
|
(dmg 5.5)
|
|
|
|
(threat 20)
|
|
|
|
(mult 3))
|
|
|
|
(lambda (ac max-ac)
|
|
|
|
(explicit-apply find-best-power max-power attack ac dmg threat mult)
|
|
|
|
(if (> ac max-ac) #f (iter (+ ac 1) max-ac)))))
|
2007-08-15 10:37:35 -04:00
|
|
|
(iter 10 30)
|