Module: Pathfinder::Tools
- Included in:
- CharacterSheet
- Defined in:
- lib/pathfinder_dnd/tools.rb
Overview
This is where all the in-game functions are defined, like rolling dice. Also includes some analytics, like an ‘average` function.
Instance Method Summary collapse
-
#average(runs = 100, fn_name = nil, &block) ⇒ Integer
Average the many runs of a function.
-
#check(skill = 0) ⇒ Integer
Roll a 20-sided dice and add an optional skill bonus Alerts the user on 19-20 rolls.
-
#roll(dice = 1, sides = 6, crit_level = 0, failure_level = 1) ⇒ Array<Integer>
Roll a number of dice.
-
#single_roll(sides, crit_level = 0, failure_level = 1) ⇒ Integer
Roll one dice with N sides.
-
#sum(array) ⇒ Integer
Deep sum arrays of integers and arrays.
-
#verbose(&block) ⇒ Object
roll verbosely.
Instance Method Details
#average(runs = 100, fn_name = nil, &block) ⇒ Integer
Average the many runs of a function. Intended to benchmark your damage.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pathfinder_dnd/tools.rb', line 75 def average(runs = 100, fn_name = nil, &block) res = 0 if fn_name b = method(fn_name.to_sym) else b = block end runs.times { res += sum(b.call()) } res / runs end |
#check(skill = 0) ⇒ Integer
Roll a 20-sided dice and add an optional skill bonus Alerts the user on 19-20 rolls
62 63 64 65 66 |
# File 'lib/pathfinder_dnd/tools.rb', line 62 def check(skill = 0) verbose do single_roll(20, -1) + skill end end |
#roll(dice = 1, sides = 6, crit_level = 0, failure_level = 1) ⇒ Array<Integer>
Roll a number of dice
54 55 56 |
# File 'lib/pathfinder_dnd/tools.rb', line 54 def roll(dice = 1, sides = 6, crit_level = 0, failure_level = 1) (1..dice).to_a.map{ |_| single_roll(sides, crit_level, failure_level) } end |
#single_roll(sides, crit_level = 0, failure_level = 1) ⇒ Integer
Roll one dice with N sides. When rolling 20-sided dice, alerts on very high or low rolls.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pathfinder_dnd/tools.rb', line 35 def single_roll(sides, crit_level = 0, failure_level = 1) res = 1 + rand(sides) if res >= crit_level + sides and @verbose puts "High roll: rolled #{res} on a d#{sides}" end if res <= failure_level and @verbose puts "Low roll: rolled #{res} on a d#{sides}" end res end |
#sum(array) ⇒ Integer
Deep sum arrays of integers and arrays.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pathfinder_dnd/tools.rb', line 10 def sum(array) res = 0 # numbers sum to themselves return array if array.is_a? Fixnum array.each do |i| if i.respond_to? :each res += sum(i) else res += i end end res end |
#verbose(&block) ⇒ Object
roll verbosely
89 90 91 92 93 94 95 96 |
# File 'lib/pathfinder_dnd/tools.rb', line 89 def verbose(&block) old_v = @verbose @verbose = true res = block.call() @verbose = old_v res end |