# # hq-prob-table.rb: generate a probability table for Issaries's HeroQuest # RPG, and interactively assess probabilities between # two Ability Ranks. # CRIT_ROLL = 1 FUMB_ROLL = 20 CRITICAL = 2 SUCCESS = 1 FAILURE = 0 FUMBLE = -1 DEGREES = [ :complete_victory, :major_victory, :minor_victory, :marginal_victory, :tie, :marginal_defeat, :minor_defeat, :major_defeat, :complete_defeat ] def outcome(rank, roll) if roll == CRIT_ROLL then CRITICAL elsif roll == FUMB_ROLL then FUMBLE elsif roll <= rank then SUCCESS else FAILURE end end def degree(mastery, a, r, rolla, rollr) diff = mastery + outcome(a, rolla) - outcome(r, rollr) case diff when 3 then :complete_victory when 2 then :major_victory when 1 then :minor_victory when 0 then if rolla < rollr then return :marginal_victory elsif rolla == rollr then return :tie else return :marginal_defeat end when -1 then :minor_defeat when -2 then :major_defeat when -3 then :complete_defeat else (diff > 0) ? :complete_victory : :complete_defeat end end def counts(mastery, a, r) result = Hash.new(0) 1.upto(20) do |rolla| 1.upto(20) do |rollr| result[degree(mastery, a, r, rolla, rollr)] += 1 end end return result end def total_success(c) return [ :complete_victory, :major_victory, :minor_victory, :marginal_victory,].inject(0.0) { |sum, d| sum + c[d].to_f/4 } end def parse_rank(s) match = /(\d+)([MmWw])?(\d+)?/.match(s) return nil unless match a = match[1].to_i if match[3] then m = match[3].to_i elsif match[2] then m = 1 else m = 0 end return a, m end def rank(a, m) astr = a.to_s astr << 'w' if m > 0 astr << m.to_s if m > 1 return astr end def print_header printf("higher ranklower rank" + ("%s" * DEGREES.size) + "\n", *(DEGREES.collect { |d| d.to_s.tr('_',' ') })) end def print_probabilities(m, a, r) c = counts(m, a, r) printf("%s%d" + ("%5.2f%%" * DEGREES.size) + "\n", rank(a, m), r, *(DEGREES.collect { |d| c[d].to_f/4 })) end def print_probability_page puts < HeroQuest Simple Contest Probabilities of Success

HeroQuest Simple Contest Probabilities of Success

EOT puts "

Quick Probability Guide

" print_difference_prob_table puts "

Detailed Probabilities

" print_probability_table puts "" end def print_difference_prob_table puts < In the absence of mastery levels, there is a relationship between probability of any victory and the difference between Ranks:

EOT print("") 0.upto(9) do |df| printf("", df) end print("\n") print("") 0.upto(9) do |df| c = counts(0, 19, 19 - df) printf("", total_success(c)) end print("\n") print("") 10.upto(18) do |df| printf("", df) end print("\n") print("") 10.upto(18) do |df| c = counts(0, 19, 19 - df) printf("", total_success(c)) end print("\n") puts "
%2d
%5.2f%%
%2d
%5.2f%%
" puts < Absent differences in mastery, the probability of ties is equal to (20 + lower-rank - higher-rank) x 0.25%. For example, between equal ranks, a tie will occur 5% of the time. If the difference is 4 (e.g. 17 and 13), the chance of a tie is 4%.

EOT end def print_probability_table puts < How to use this table:

  1. If both parties have levels of Mastery, reduce each by the lower of the two levels.
  2. Determine which party has the higher Rank. A Victory for the party with the higher rank is equivalent to a Defeat for the other party.
  3. Convert ranks of 20, 20w, etc. into 19, 19w, etc. Mathematically, they're equivalent, due to automatic Fumble at 20.
  4. Look up the probabilities on this chart. Note that "5w" is "five Mastery", and "1w2" is "one Mastery two". Representing the actual Mastery rune (|_|_| in ASCII art) using programmatically-generated HTML is just too annoying.

Note that this chart is meant only for informational purposes. To actually resolve conflicts, roll as instructed in the HeroQuest rules. (You'd need two d20 to use these percentages anyway ...)

EOT print_header 1.upto(61) do |t| m = t / 20 a = t % 20 maxr = (t > 19) ? 19 : t if a > 0 then 1.upto(maxr) do |r| print_probabilities(m, a, r) end end end print_probabilities(4, 1, 19) puts "
" end case ARGV.size when 0 then print_probability_page when 2 then a, ma = parse_rank(ARGV[0]) r, mr = parse_rank(ARGV[1]) c = counts(ma - mr, a, r) printf("%16s: %5.2f%%\n", 'SUCCESS', total_success(c)) DEGREES.each do |d| printf("%16s: %5.2f%%\n", d, c[d].to_f/4) end else puts "usage: #{$0} [ active-rank resist-rank ]" end