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

Instance Method Details

#average(runs = 100, fn_name = nil, &block) ⇒ Integer

Average the many runs of a function. Intended to benchmark your damage.

Parameters:

  • runs (Integer) (defaults to: 100)

    how many samples to take

  • fn_name (String, Symbol) (defaults to: nil)

    name of the method to call

  • block (Proc)

    a block to use instead of calling a defined method

Returns:

  • (Integer)

    the average



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

Parameters:

  • skill (Integer) (defaults to: 0)

    your skill-check or saving-throw bonus. Default 0.

Returns:

  • (Integer)


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

Parameters:

  • dice (Integer) (defaults to: 1)

    number of dice to roll. Default 1.

  • sides (Integer) (defaults to: 6)

    number of sides on each die. Default 6.

Returns:

  • (Array<Integer>)

    list of dice roll results

See Also:



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.

Parameters:

  • sides (Integer)

    number of sides on the dice.

  • crit_level (Integer) (defaults to: 0)

    alert the user to dice rolls at or above this level when rolling 20-sided dice. Default 19.

  • failure_level (integer) (defaults to: 1)

    alert the user to dice rolls at or below this level when rolling 20-sided dice. Default 1.

Returns:

  • (Integer)

    result of the dice roll



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.

Parameters:

  • array (Array)

    the list to sum up.

Returns:

  • (Integer)

    the total



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