Class: Tabletop::Roll

Inherits:
Object
  • Object
show all
Defined in:
lib/tabletop/roll.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool = nil, &block) ⇒ Roll

The block contains methods that further detail the roll, as described below.

pool must be (surprise!) a Pool. It’s optional, because if #sides is passed in the block, then #roll can be called with a number of dice to roll, and a new Pool object will be instantiated every time this is done.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tabletop/roll.rb', line 25

def initialize(pool=nil, &block)
  if pool
    raise ArgumentError if pool.class != Tabletop::Pool
  end
  @pool = pool
  @possibilities = []
  @die_sides = nil
  @static_modifier = 0
  @roll_modifier = 0
  @result_set = false
  instance_eval(&block)
  unless @result_set
    set_result(:sum)
  end
end

Instance Attribute Details

#poolObject

Rolls of necessity have a Pool object against which they check possible results.



18
19
20
# File 'lib/tabletop/roll.rb', line 18

def pool
  @pool
end

Instance Method Details

#add(mod) ⇒ Object

Sets a modifier that’s added to #sum.

Meant to be used in the initialize block.



185
186
187
# File 'lib/tabletop/roll.rb', line 185

def add(mod)
  @static_modifier = mod
end

#at_least(value, *outcomes) ⇒ Object

Attaches an object to work with #meets?.

value

An integer.

outcomes

An array of values to contribute to #effects if #meets? is true.



132
133
134
# File 'lib/tabletop/roll.rb', line 132

def at_least(value, *outcomes)
  @possibilities << Possibility.new(outcomes, :>= => value)
end

#check(p) ⇒ Object

Takes a Possibility, returns an Array containing nil if any of it’s conditions aren’t met. Otherwise, returns an Array containing all the Possibility’s outcomes. If any of those outcomes are Roll objects, they are rolled and their #effects are returned as an outcome. – TODO: checks #result, not #sum



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/tabletop/roll.rb', line 96

def check(p) #:nodoc:
  conditions_met = true
  
  if p.conditions[:>=] and sum < p.conditions[:>=]
    conditions_met = false
  end
  
  if p.conditions[:==] and p.conditions[:==] != sum
    conditions_met = false
  end
  
  if conditions_met
    results = []
    p.outcomes.each do |outcome|
      if outcome.instance_of?(Roll)
        results << outcome.roll.effects
      else
        results << outcome
      end
    end
    results
  else
    [nil]
  end
end

#effectsObject

Returns either an array or nil. + If a “difficulty” was set in the most recent call of #roll, and #result meets or exceeds it, then the first element will be “Success”. + If the conditions of any of the roll’s @possibilities are met (see #meets?), then their outcomes will be all following elements. + If none of these conditions are met, returns nil



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tabletop/roll.rb', line 46

def effects
  results = []
  
  if @difficulty
    results << "Success" if result >= @difficulty
  end
  
  @possibilities.each do |poss|
    results.concat(check(poss))
  end
  
  results.compact!

  if results.empty?
    results = nil
  end
  results
end

#equals(values, *outcomes) ⇒ Object

Attaches an object to work with #meets?.

values

Can be either an integer, or a Range. If it’s a range, then #equals creates an object for each number in the range. If it’s an integer, it creates just one.

outcomes

An array of values to contribute to #effects if #meets? is true.

– TODO: values can be an array



142
143
144
145
146
147
148
149
150
# File 'lib/tabletop/roll.rb', line 142

def equals(values, *outcomes)
  if values.instance_of?(Range)
    values.each do |val|
      @possibilities << Possibility.new(outcomes, :== => val)
    end
  else
    @possibilities << Possibility.new(outcomes, :== => values)
  end
end

#roll(opts = {}) ⇒ Object

Without any options passed, calls Pool#roll on the roll’s pool. Returns the Roll.

opts can have a few different values:

:modifier

adds to all subsequent calls to #sum, until #roll is called again

:pool

if #sides was called in the initialize block, and this is set, then a Pool of appropriate sides and number is created and assigned to @pool.

:difficulty

see #effects

– TODO: @difficulty changes back to nil if it is not called in a given roll. TODO: abstract out ternary?



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tabletop/roll.rb', line 74

def roll(opts={})
  @roll_modifier = opts[:modifier] ? opts[:modifier] : 0
  if @die_sides
    if opts[:pool] 
      @pool = opts[:pool].dX(@die_sides)
    else
      raise ArgumentError
    end
  end
  if opts[:difficulty]
    @difficulty = opts[:difficulty]
  end
  @pool.roll
  self 
end

#set_result(symbol, args = {}) ⇒ Object

Defines a #result method, used by #effects.

If symbol is ‘:count’, then args must include a :at_least option, and #result will be equal to the number of dice in @pool of value equal or greater than args.

Optionally, args can also include a :doubles option, for values that add 2 to #result

In all other cases, #result is aliased to #sum

Meant to be used in the initialize block. – TODO: raise an error symbol is :count but :at_least is not set



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/tabletop/roll.rb', line 165

def set_result(symbol, args={})
  if symbol == :count
    @count_at_least = args[:at_least]
    @count_doubles = args[:doubles]
    def result
      normal = @pool.count {|die| die.value >= @count_at_least}
      extra = @count_doubles ? @pool.count {|die| die.value == @count_doubles} : 0
      normal + extra
    end
  else
    def result
      sum
    end
  end
  @result_set = true
end

#sides(num_sides) ⇒ Object

Sets a default die size. If set, roll can be called with a :pool argument to create and roll a new Pool of the indicated number and type of dice.

Can be an integer other than zero, or :fudge for fudgedice.

Meant to be used in the initialize block.



195
196
197
# File 'lib/tabletop/roll.rb', line 195

def sides(num_sides)
  @die_sides = num_sides
end

#sumObject

The sum of the values of dice in the pool, and any modifier set in instantiation (see #add), or the most recent call to #roll.



124
125
126
# File 'lib/tabletop/roll.rb', line 124

def sum
  @pool.sum + @static_modifier + @roll_modifier
end