rpg-tools 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,12 @@
1
1
  h1. RPG Tools
2
2
 
3
- #TODO
3
+ RPG Tools is a compilation of helpful tools when developing a Role Playing Game (RPG). For now the gem has three classes:
4
+
5
+ Die: Models the use of a simple die (ie. D6, D10, D20, etc.)
6
+ Throw: Takes an array of Die to be rolled and the offset to be added (ie. 2D6, 3D4+5, 2D10,3D20+2, etc.)
7
+ CheckRoll: Using a Throw and a threshold models a check roll with one or several chances and some other settings (ie. 2D6 to get greater or equal to 10, 3D10 to get les than 15, etc.)
8
+
9
+ These tools try to be flexible, general and powerful. Most of Die representation in other gems do the job of a Throw (a pack of Dice with a offset) or just return the result of ten rolls as single Fixum giving no information of each of the single ten rolls (which may be important if you want to have detailed info or want to give feedback to the user). With RPG Tools each object acts as it should: a Die acts as a Die, with no more functionality than a normal Die and each. Also each roll has all the information needed just if you want it. By default rolling a Throw just returns the total amount, but if you ask for a detailed roll you will get the total amount and the result of each dice of the Throw.
4
10
 
5
11
  h2. Installation
6
12
 
@@ -15,7 +21,119 @@ Then run:
15
21
 
16
22
  h2. Using RPG Tools. API
17
23
 
18
- #TODO
24
+ h3. Using Tools::Die
25
+
26
+ Creating a new Die:
27
+ <pre><code>
28
+ die = Tools::Die.new #Creates a new Die with 20 sides (D20) by default
29
+ die = Tools::Die.new 6 #Creates a new Die with 6 sides (D6)
30
+ die = Tools::Die.new 100 #Creates a new Die with 100 sides (D100)
31
+ </code></pre>
32
+
33
+ Rolling a Die:
34
+ <pre><code>
35
+ die = Tools::Die.new
36
+ #Rolling the die once
37
+ die.roll # => 8
38
+ #Rolling the die five times
39
+ die.roll 5 # => [1, 20, 16, 11, 11]
40
+ </code></pre>
41
+
42
+ More methods of Die:
43
+ <pre><code>
44
+ #Converting Die into string
45
+ die1 = Tools::Die.new 20
46
+ die2 = Tools::Die.new 6
47
+ die3 = Tools::Die.new 100
48
+ die1.to_s # => "D20"
49
+ die2.to_s # => "D6"
50
+ die3.to_s # => "D100"
51
+ </code></pre>
52
+ <pre><code>
53
+ #Comparing Dice
54
+ die1 = Tools::Die.new 20
55
+ die2 = Tools::Die.new 6
56
+ die3 = Tools::Die.new 20
57
+ die1.eql? die1 # => true
58
+ die1.eql? die2 # => false
59
+ die1.eql? die3 # => true
60
+ </code></pre>
61
+
62
+ h3. Using Tools::Throw
63
+
64
+ Creating a new Throw:
65
+ <pre><code>
66
+ dice = [Tools::Die.new(6),Tools::Die.new(6),Tools::Die.new(20),Tools::Die.new(100),Tools::Die.new(100),Tools::Die.new(100)]
67
+ offset = 3
68
+ throw = Tools::Throw.new dice,offset #Creates a new throw with 2D6, 1D20, 3D100 and +3
69
+ </code></pre>
70
+
71
+ Rolling a Throw:
72
+ <pre><code>
73
+ dice = [Tools::Die.new(6),Tools::Die.new(6),Tools::Die.new(20)]
74
+ offset = 2
75
+ throw = Tools::Throw.new dice,offset #Creates a new throw with 2D6, 1D20 and +2
76
+ #Rolling the throw once (simple)
77
+ throw.roll # => 10
78
+ #Rolling the throw once (detailed)
79
+ throw.roll 1,true # => [25, [6, 6, 11]]
80
+ #Rolling the throw five times (simple)
81
+ throw.roll 5 # => [23, 22, 18, 18, 16]
82
+ #Rolling the throw five times (detailed)
83
+ throw.roll 5, true # => [[26, [1, 4, 19]], [28, [6, 3, 17]], [13, [1, 4, 6]], [23, [5, 6, 10]], [11, [3, 5, 1]]]
84
+ </code></pre>
85
+
86
+ More methods of Throw:
87
+ <pre><code>
88
+ #Converting Throw into string
89
+ dice = [Tools::Die.new(6),Tools::Die.new(6),Tools::Die.new(20),Tools::Die.new(100),Tools::Die.new(100),Tools::Die.new(100)]
90
+ offset = 3
91
+ throw = Tools::Throw.new dice,offset #Creates a new throw with 2D6, 1D20, 3D100 and +3
92
+ throw.to_s # => "2D6,1D20,3D100+3"
93
+ </code></pre>
94
+
95
+ h3. Using Tools::CheckRoll
96
+
97
+ Creating a new CheckRoll:
98
+ <pre><code>
99
+ dice = [Tools::Die.new(6),Tools::Die.new(6)]
100
+ offset = 2
101
+ throw = Tools::Throw.new dice,offset #Creates a new throw with 2D6+2
102
+ threshold = 9
103
+ check_roll = Tools::CheckRoll.new throw, threshold #Creates a CheckRoll with 2D6+2 throw and a threshold of 9
104
+ </code></pre>
105
+
106
+ Rolling a CheckRoll:
107
+ <pre><code>
108
+ dice = [Tools::Die.new(6),Tools::Die.new(6)]
109
+ offset = 2
110
+ throw = Tools::Throw.new dice,offset #Creates a new throw with 2D6+2
111
+ threshold = 9
112
+ check_roll = Tools::CheckRoll.new throw, threshold #Creates a CheckRoll with 2D6+2 throw and a threshold of 9
113
+ #Roll the CheckRoll
114
+ check_roll.roll # => [false, 6]
115
+ #CheckRoll results
116
+ check_roll.is_successful? # => false
117
+ check_roll.result # => [false, 6]
118
+ check_roll.detailed_result # => [false, [6, [3, 1]]]
119
+ #Reroll the CheckRoll
120
+ check_roll.reroll # => [true, 12]
121
+ #CheckRoll results
122
+ check_roll.is_successful? # => true
123
+ check_roll.result # => [true, 12]
124
+ check_roll.detailed_result # => [true, [12, [6, 4]]]
125
+ </code></pre>
126
+
127
+ More methods of CheckRoll:
128
+ <pre><code>
129
+ #Converting CheckRoll into string
130
+ dice = [Tools::Die.new(6),Tools::Die.new(6)]
131
+ offset = 2
132
+ throw = Tools::Throw.new dice,offset #Creates a new throw with 2D6+2
133
+ threshold = 9
134
+ check_roll = Tools::CheckRoll.new throw, threshold #Creates a CheckRoll with 2D6+2 throw and a threshold of 9
135
+ check_roll.to_s # => "2D6+2 must be greater or equal to 9."
136
+ </code></pre>
19
137
 
20
138
  h2. License
21
139
 
data/Rakefile CHANGED
@@ -17,7 +17,7 @@ task :default => :spec
17
17
 
18
18
  Rake::RDocTask.new do |rdoc|
19
19
  rdoc.rdoc_dir = 'rdoc'
20
- rdoc.title = "Fuzion RPG"
20
+ rdoc.title = "RPG Tools"
21
21
  rdoc.options << '--line-numbers' << '--inline-source'
22
22
  rdoc.rdoc_files.include('README.rdoc')
23
23
  rdoc.rdoc_files.include('lib/**/*.rb', 'app/**/*.rb')
@@ -1,82 +1,104 @@
1
- class Tools::CheckRoll
1
+ module Tools
2
+ class CheckRoll
2
3
 
3
- attr_accessor :throw, :threshold, :rolling_chances, :greater_than_threshold, :equal_than_threshold
4
- attr_reader :result
5
- def initialize throw, threshold, rolling_chances=1, greater_than_threshold=true, equal_than_threshold=true
6
- @throw = throw
7
- @threshold = threshold
8
- @rolling_chances = rolling_chances
9
- @greater_than_threshold = greater_than_threshold
10
- @equal_than_threshold = equal_than_threshold
11
- end
4
+ attr_accessor :throw, :threshold, :rolling_chances, :greater_than_threshold, :equal_than_threshold
5
+ attr_reader :result
6
+ def initialize throw, threshold, rolling_chances=1, greater_than_threshold=true, equal_than_threshold=true
7
+ @throw = throw
8
+ @threshold = threshold
9
+ @rolling_chances = rolling_chances
10
+ @greater_than_threshold = greater_than_threshold
11
+ @equal_than_threshold = equal_than_threshold
12
+ end
12
13
 
13
- #[true/false,best_rolled]
14
- def roll
15
- return @result if @result
16
- best_rolled = 0
17
- @rolling_chances.times do
18
- rolled_throw = @throw.roll 1,true #Detailed throw
19
- best_rolled = rolled_throw if rolled_throw[0] > best_rolled[0]
14
+ # Rolls the Throw as many times as rolling chances are set.If has already
15
+ # been rolled just returns the last result.
16
+ # Returns the result as an +Array+ with the format [true/false, simple_best_rolled].
17
+ # The first element of the array illustrates whether the check was successful or not.
18
+ # The second element is the best rolled throw as a simple Throw result, a +Fixnum+.
19
+ # For a more detailed result (with a detailed Throw result), use +detailed_result+.
20
+ def roll
21
+ return @result if @result
22
+ best_rolled = 0
23
+ @rolling_chances.times do
24
+ rolled_throw = @throw.roll 1,true #Detailed throw
25
+ best_rolled = rolled_throw if rolled_throw[0] > best_rolled[0]
26
+ end
27
+ @result = [compare(best_rolled[0], @threshold),best_rolled]
28
+ result
20
29
  end
21
- @result = [compare(best_rolled[0], @threshold),best_rolled]
22
- result
23
- end
24
30
 
25
- def reroll
26
- @result = nil
27
- roll
28
- end
31
+ # Forces a new roll by deleting the old result.
32
+ def reroll
33
+ @result = nil
34
+ roll
35
+ end
29
36
 
30
- def is_successful?
31
- roll if @result.blank?
32
- return @result[0]
33
- end
34
-
35
- def result
36
- [@result[0],@result[1][0]]
37
- end
38
-
39
- def detailed_result
40
- @result
41
- end
37
+ # Returns whether the check was successful or not. It rolls the check if not done before.
38
+ def is_successful?
39
+ roll if @result.blank?
40
+ return @result[0]
41
+ end
42
42
 
43
- def to_s
44
- string = "#{@throw.to_s} #{I18n.t('rpg_tools.check_roll.must_be')} #{comparatives_text} #{@threshold}."
45
- if @result
46
- string << " #{I18n.t('rpg_tools.check_roll.already_checked')}"
47
- string << " #{@result[0] ? I18n.t('rpg_tools.check_roll.success') : I18n.t('rpg_tools.check_roll.failure')}"
48
- string << " (#{@result[1][0]})."
43
+ # Returns the result as an +Array+ with the format [true/false, simple_best_rolled].
44
+ # The first element of the array illustrates whether the check was successful or not.
45
+ # The second element is the best rolled throw as a simple Throw result, a +Fixnum+.
46
+ # For a more detailed result (with a detailed Throw result), use +detailed_result+.
47
+ # It rolls the check if not done before.
48
+ def result
49
+ roll if @result.blank?
50
+ [@result[0],@result[1][0]]
49
51
  end
50
- string
51
- end
52
52
 
53
- private
53
+ # Returns the result as an +Array+ with the format [true/false, detailed_best_rolled].
54
+ # The first element of the array illustrates whether the check was successful or not.
55
+ # The second element is the best rolled throw as a detailed Throw result, please
56
+ # refer to +Tools::Throw.roll+ for more info on detailed Throw.
57
+ # It rolls the check if not done before.
58
+ def detailed_result
59
+ roll if @result.blank?
60
+ @result
61
+ end
54
62
 
55
- def compare rolled_throw, threshold
56
- gtt = @greater_than_threshold
57
- ett = @equal_than_threshold
58
- if gtt and ett
59
- return rolled_throw >= threshold
60
- elsif gtt and !ett
61
- return rolled_throw > threshold
62
- elsif !gtt and !ett
63
- return rolled_throw < threshold
64
- elsif !gtt and ett
65
- return rolled_throw <= threshold
63
+ # Returns the CheckRoll as a string. Example "2D4,1D6,1D20+3 must be greater or equal to 20."
64
+ def to_s
65
+ string = "#{@throw.to_s} #{I18n.t('rpg_tools.check_roll.must_be')} #{comparatives_text} #{@threshold}."
66
+ if @result
67
+ string << " #{I18n.t('rpg_tools.check_roll.already_checked')}"
68
+ string << " #{@result[0] ? I18n.t('rpg_tools.check_roll.success') : I18n.t('rpg_tools.check_roll.failure')}"
69
+ string << " (#{@result[1][0]})."
70
+ end
71
+ string
66
72
  end
67
- end
68
-
69
- def comparatives_text
70
- gtt = @greater_than_threshold
71
- ett = @equal_than_threshold
72
- if gtt and ett
73
- return I18n.t 'rpg_tools.check_roll.greater_and_equal'
74
- elsif gtt and !ett
75
- return I18n.t 'rpg_tools.check_roll.greater'
76
- elsif !gtt and !ett
77
- return I18n.t 'rpg_tools.check_roll.less'
78
- elsif !gtt and ett
79
- return I18n.t 'rpg_tools.check_roll.less_and_equal'
73
+
74
+ private
75
+
76
+ def compare rolled_throw, threshold
77
+ gtt = @greater_than_threshold
78
+ ett = @equal_than_threshold
79
+ if gtt and ett
80
+ return rolled_throw >= threshold
81
+ elsif gtt and !ett
82
+ return rolled_throw > threshold
83
+ elsif !gtt and !ett
84
+ return rolled_throw < threshold
85
+ elsif !gtt and ett
86
+ return rolled_throw <= threshold
87
+ end
88
+ end
89
+
90
+ def comparatives_text
91
+ gtt = @greater_than_threshold
92
+ ett = @equal_than_threshold
93
+ if gtt and ett
94
+ return I18n.t 'rpg_tools.check_roll.greater_and_equal'
95
+ elsif gtt and !ett
96
+ return I18n.t 'rpg_tools.check_roll.greater'
97
+ elsif !gtt and !ett
98
+ return I18n.t 'rpg_tools.check_roll.less'
99
+ elsif !gtt and ett
100
+ return I18n.t 'rpg_tools.check_roll.less_and_equal'
101
+ end
80
102
  end
81
103
  end
82
104
  end
@@ -1,35 +1,37 @@
1
- class Tools::Die
2
- attr_accessor :sides
3
-
4
- def initialize sides=20
5
- @sides = sides
6
- end
7
-
8
- # Rolls the die and returns result as +Fixnum+ if rolls==1 or as +Array+ of +Fixnum+
9
- # if rolls>1.
10
- def roll rolls=1
11
- return simple_roll if rolls==1
12
- results = Array.new
13
- rolls.times do
14
- results << simple_roll
1
+ module Tools
2
+ class Die
3
+ attr_accessor :sides
4
+ def initialize sides=20
5
+ @sides = sides
6
+ end
7
+
8
+ # Rolls the Die and returns the result as +Fixnum+ if rolls==1 or as +Array+ of +Fixnum+
9
+ # if rolls>1.
10
+ def roll rolls=1
11
+ return simple_roll if rolls==1
12
+ results = Array.new
13
+ rolls.times do
14
+ results << simple_roll
15
+ end
16
+ return results
17
+ end
18
+
19
+ # Returns the Die as a string with the format Dx, where x is the number of sides. Example "D6", "D20", "D100", etc.
20
+ def to_s
21
+ return "D#{@sides}"
22
+ end
23
+
24
+ # Return true if the dice are equal, i.e. they have the same number of sides.
25
+ def eql? die
26
+ return false if die==nil or die.sides!=@sides
27
+ return true
28
+ end
29
+
30
+ private
31
+
32
+ #Returns a simple die roll
33
+ def simple_roll
34
+ 1 + rand(sides)
15
35
  end
16
- return results
17
- end
18
-
19
- # Returns the die as a string with the format Dx, where x is the number of sides. Example "D6", "D20", "D100", etc.
20
- def to_s
21
- return "D#{@sides}"
22
- end
23
-
24
- # Return true if the dice are equal, i.e. they have the same number of sides.
25
- def eql? die
26
- return false if die==nil or die.sides!=@sides
27
- return true
28
- end
29
-
30
- private
31
-
32
- def simple_roll
33
- 1 + rand(sides)
34
36
  end
35
37
  end
@@ -1,71 +1,81 @@
1
- class Tools::Throw
2
- attr_accessor :dice, :offset
3
-
4
- def initialize dice, offset=0
5
- @dice = dice.sort_by{|die| die.sides}
6
- @offset = offset
7
- end
8
-
9
- # Rolls the dice, add the offset and returns result as +Fixnum+ if rolls==1 or as
10
- # +Array+ of +Fixnum+ if rolls>1.
11
- def roll rolls=1, detailed=false
12
- return simple_roll if rolls==1 and !detailed
13
- return detailed_roll if rolls==1 and detailed
14
- results = Array.new
15
- rolls.times do
16
- results << simple_roll if !detailed
17
- results << detailed_roll if detailed
1
+ module Tools
2
+ class Throw
3
+ attr_accessor :dice, :offset
4
+ def initialize dice, offset=0
5
+ @dice = dice.sort_by{|die| die.sides}
6
+ @offset = offset
18
7
  end
19
- return results
20
- end
21
-
22
- # Returns the dice as a string with the format aDx,bDy,cDz+n, where a,b,c are the number of dice,
23
- # x,y,z are the number of sides and n the offset. Example "D6,D20,D100+8", etc.
24
- def to_s
25
- throw_dice_number = Array.new
26
- throw_dice = Array.new
27
- temp_dice = dice.sort_by{|die| die.sides}
28
- last_die = nil
29
- temp_dice.each do |temp_die|
30
- if !temp_die.eql? last_die
31
- last_die = temp_die
32
- throw_dice << temp_die
33
- throw_dice_number << temp_dice.count{|die| die.eql? temp_die}
8
+
9
+ # Rolls the Throw and returns the result in different ways according to the params.
10
+ # If +detailed=false+ it will call +simple_roll+ and +detailed_roll+ in case of +true+.
11
+ # Refer to these methods for more info of the format.
12
+ # Returns the result as +simple_roll/detailed_roll+ if rolls==1 or as +Array+ of
13
+ # +simple_roll/detailed_roll+ if rolls>1.
14
+ def roll rolls=1, detailed=false
15
+ return simple_roll if rolls==1 and !detailed
16
+ return detailed_roll if rolls==1 and detailed
17
+ results = Array.new
18
+ rolls.times do
19
+ results << simple_roll if !detailed
20
+ results << detailed_roll if detailed
34
21
  end
22
+ return results
35
23
  end
36
- throw_string = ""
37
- throw_dice.each_index do |i|
38
- throw_string << "," if i>0
39
- throw_string << throw_dice_number[i].to_s + throw_dice[i].to_s
24
+
25
+ # Returns the Trhow as a string with the format aDx,bDy,cDz+n, where a,b,c are the number of dice,
26
+ # x,y,z are the number of sides and n the offset. Example "D6,D20,D100+8", etc.
27
+ def to_s
28
+ throw_dice_number = Array.new
29
+ throw_dice = Array.new
30
+ temp_dice = dice.sort_by{|die| die.sides}
31
+ last_die = nil
32
+ temp_dice.each do |temp_die|
33
+ if !temp_die.eql? last_die
34
+ last_die = temp_die
35
+ throw_dice << temp_die
36
+ throw_dice_number << temp_dice.count{|die| die.eql? temp_die}
37
+ end
38
+ end
39
+ throw_string = ""
40
+ throw_dice.each_index do |i|
41
+ throw_string << "," if i>0
42
+ throw_string << throw_dice_number[i].to_s + throw_dice[i].to_s
43
+ end
44
+ throw_string << "+" if @offset>0
45
+ throw_string << "#{@offset}" if @offset!=0
46
+ return throw_string
40
47
  end
41
- throw_string << "+" if @offset>0
42
- throw_string << "#{@offset}" if @offset!=0
43
- return throw_string
44
- end
45
-
46
- private
47
-
48
- def simple_roll
49
- total = 0
50
- dice.each do |die|
51
- total+= die.roll
48
+
49
+ private
50
+
51
+ # Rolls all the dice and adds the offset returning just the total sum.
52
+ def simple_roll
53
+ total = 0
54
+ dice.each do |die|
55
+ total+= die.roll
56
+ end
57
+ total += offset
58
+ return total
52
59
  end
53
- total += offset
54
- return total
55
- end
56
-
57
- def detailed_roll
58
- results = Array.new
59
- total = 0
60
- dice.each do |die|
61
- roll = die.roll
62
- total+= roll
63
- results << roll
60
+
61
+ # Rolls all the dice and adds the offset returning and +Array+ with detailed info.
62
+ # The first element of the +Array+ is the total sum of the rolled Throw and the second
63
+ # is another +Array+ containing the result of each one of therolled die of the Throw in
64
+ # the same order as returned by +dice+ (it may be different from the one used for
65
+ # initialize the Throw as it is ordered).
66
+ def detailed_roll
67
+ results = Array.new
68
+ total = 0
69
+ dice.each do |die|
70
+ roll = die.roll
71
+ total+= roll
72
+ results << roll
73
+ end
74
+ total += offset
75
+ detailed_throw = Array.new
76
+ detailed_throw[0] = total
77
+ detailed_throw[1] = results
78
+ return detailed_throw
64
79
  end
65
- total += offset
66
- detailed_throw = Array.new
67
- detailed_throw[0] = total
68
- detailed_throw[1] = results
69
- return detailed_throw
70
80
  end
71
- end
81
+ end
@@ -1,4 +1,4 @@
1
- module FuzionRPG
1
+ module RPG_Tools
2
2
  class << self
3
3
  def setup
4
4
  yield self
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rpg-tools"
3
- s.version = "0.1.0"
3
+ s.version = "0.2.0"
4
4
  s.authors = ["Eduardo Casanova Cuesta"]
5
5
  s.summary = "Some tools useful when creating a RPG: Die, Throw and CheckRoll."
6
6
  s.description = "Some tools useful when creating a RPG: Die, Throw and CheckRoll."
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpg-tools
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eduardo Casanova Cuesta
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-20 00:00:00 +02:00
18
+ date: 2011-08-21 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency