tablescript 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/COPYING +674 -0
- data/README.md +127 -0
- data/Rakefile +4 -0
- data/lib/tablescript/dice_roller.rb +94 -0
- data/lib/tablescript/roll_descriptor.rb +55 -0
- data/lib/tablescript/table.rb +53 -0
- data/lib/tablescript/table_entry.rb +31 -0
- data/lib/tablescript/table_entry_environment.rb +124 -0
- data/lib/tablescript/version.rb +20 -0
- data/lib/tablescript.rb +81 -0
- data/tablescript.gemspec +38 -0
- metadata +57 -0
data/README.md
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
# Tablescript.rb
|
2
|
+
|
3
|
+
This is a Ruby Gem that helps RPG-playing nerds like me generate random things from tables.
|
4
|
+
|
5
|
+
For example, a Dungeons & Dragons wandering monster table like this:
|
6
|
+
|
7
|
+
Wandering Monsters (d10):
|
8
|
+
|
9
|
+
1-5: d6 orcs
|
10
|
+
6-9: 3d6 ancient red dragons
|
11
|
+
10: a cuddly bunny
|
12
|
+
|
13
|
+
can be automated in Ruby-ish ways like this:
|
14
|
+
|
15
|
+
table :wandering_monsters do
|
16
|
+
f( 1..5 ) { "#{roll_dice('d6')} orcs" }
|
17
|
+
f( 6..9 ) { "#{roll_dice('3d6')} ancient red dragons" }
|
18
|
+
f { "a cuddly bunny" }
|
19
|
+
end
|
20
|
+
|
21
|
+
puts roll_on( :wandering_monsters )
|
22
|
+
|
23
|
+
# Syntax
|
24
|
+
|
25
|
+
Tablescript.rb is a simple DSL built on Ruby that helps to define and roll on tables.
|
26
|
+
|
27
|
+
Define a table as follows:
|
28
|
+
|
29
|
+
table :table_name do
|
30
|
+
...
|
31
|
+
end
|
32
|
+
|
33
|
+
Roll on a table as follows:
|
34
|
+
|
35
|
+
roll_on( :table_name )
|
36
|
+
|
37
|
+
Table entries define blocks that are returned if the die roll matches the entry. Entries can be simple text:
|
38
|
+
|
39
|
+
f { "a cuddly bunny" }
|
40
|
+
|
41
|
+
complex/interpolated text:
|
42
|
+
|
43
|
+
f { "#{roll_dice('3d6')} cuddly bunnies" }
|
44
|
+
|
45
|
+
or arbitrary Ruby code:
|
46
|
+
|
47
|
+
f { { :effect => roll_on( :random_limb_loss ), :damage => roll_dice( '4d10' ) }
|
48
|
+
|
49
|
+
Table entries are either "f" or "d" for "fixed" and "dynamic" respectively.
|
50
|
+
|
51
|
+
Fixed entries are defined for specify die rolls. For example:
|
52
|
+
|
53
|
+
f( 1 ) { ... }
|
54
|
+
|
55
|
+
defines the result for the roll of 1.
|
56
|
+
|
57
|
+
f( 5..9 ) { ... }
|
58
|
+
|
59
|
+
defines the result for a roll of 5, 6, 7, 8, or 9.
|
60
|
+
|
61
|
+
f { ... }
|
62
|
+
|
63
|
+
defines the result for the _next_ roll. If it's the first entry, it defaults to 1. Otherwise, it's whatever the previous entry was + 1.
|
64
|
+
|
65
|
+
The :wandering_monsters example table above defines 3 entries: 1-5, 6-9, and 10.
|
66
|
+
|
67
|
+
Dynamic entries are defined for groups of rolls. For example:
|
68
|
+
|
69
|
+
d( 10 ) { ... }
|
70
|
+
d( 50 ) { ... }
|
71
|
+
d( 40 ) { ... }
|
72
|
+
|
73
|
+
defines 3 groups of results. The first is for rolls of 1-10 (i.e. the first 10). The second is for rolls of 11-60 (i.e. the next 50). And the third is for rolls of 51-100 (i.e. the next 40). In this case the total number of entries works out to 100, so the entries are effectively 10%, 50%, and 40%.
|
74
|
+
|
75
|
+
Entries do not have to total 100. For example:
|
76
|
+
|
77
|
+
d( 1 ) { ... }
|
78
|
+
d( 2 ) { ... }
|
79
|
+
|
80
|
+
defines 2 groups of results where the second has twice the chance of the first. Tablescript will effectively roll a d3.
|
81
|
+
|
82
|
+
# Reference
|
83
|
+
|
84
|
+
Tablescript includes the following global functions:
|
85
|
+
|
86
|
+
## table( name, &blk )
|
87
|
+
|
88
|
+
Defines a table, as in the above examples.
|
89
|
+
|
90
|
+
## roll_on( name )
|
91
|
+
|
92
|
+
Generates a random number from 1 to the highest defined entry, and returns the corresponding table entry from table named _name_.
|
93
|
+
|
94
|
+
## roll_on_and_ignore_duplicates( name, times, *args )
|
95
|
+
|
96
|
+
Rolls on the _name_ table _times_ times and ignores duplicate rolls.
|
97
|
+
|
98
|
+
## lookup( name, roll )
|
99
|
+
|
100
|
+
Returns the entry from table _name_ corresponding to the roll _roll_ as if that number had been randomly generated.
|
101
|
+
|
102
|
+
## roll_dice( dice )
|
103
|
+
|
104
|
+
Returns a random number generated by the dice described by _dice_.
|
105
|
+
|
106
|
+
The following formats are acceptable:
|
107
|
+
|
108
|
+
dX: rolls a single X-sided die
|
109
|
+
YdX: rolls Y X-sided dice and returns the sum of all rolls
|
110
|
+
YdXdl[Z]: rolls Y X-sided dice, drops the lowest Z rolls, and returns the sum of the remainder
|
111
|
+
YdXdh[Z]: rolls Y X-sided dice, drops the highest Z rolls, and returns the sum of the remainder
|
112
|
+
|
113
|
+
## choose( options )
|
114
|
+
|
115
|
+
Returns a random selection from the passed array of options. For example:
|
116
|
+
|
117
|
+
puts choose( [ "red", "green", "blue" ] )
|
118
|
+
|
119
|
+
is equivalent to:
|
120
|
+
|
121
|
+
table :temporary do
|
122
|
+
f { "red" }
|
123
|
+
f { "green" }
|
124
|
+
f { "blue" }
|
125
|
+
end
|
126
|
+
|
127
|
+
puts roll_on( :temporary )
|
data/Rakefile
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module TableScript
|
19
|
+
|
20
|
+
class DiceRoller
|
21
|
+
|
22
|
+
@@DICE_REGEXP = /(\d*)d(\d+)((dl)(\d*)|(dh)(\d*))?/
|
23
|
+
|
24
|
+
def random_value_in_range( range )
|
25
|
+
rand( range )
|
26
|
+
end
|
27
|
+
|
28
|
+
def roll_dice( roll_descriptor )
|
29
|
+
rolled_values = roll_all_dice_from_descriptor( roll_descriptor )
|
30
|
+
drop_lowest( rolled_values, roll_descriptor )
|
31
|
+
drop_highest( rolled_values, roll_descriptor )
|
32
|
+
total( rolled_values )
|
33
|
+
end
|
34
|
+
|
35
|
+
def roll( dice )
|
36
|
+
while m = dice.downcase.match( @@DICE_REGEXP ) do
|
37
|
+
rolled_value = roll_dice( RollDescriptor.new( m ) )
|
38
|
+
dice[ m.begin( 0 )...m.end( 0 ) ] = rolled_value.to_s
|
39
|
+
end
|
40
|
+
eval( dice )
|
41
|
+
end
|
42
|
+
|
43
|
+
def roll_and_ignore( dice, args )
|
44
|
+
ignored_values = collect_ignored_values( args )
|
45
|
+
rolled_value = nil
|
46
|
+
while rolled_value.nil? do
|
47
|
+
rolled_value = roll( dice )
|
48
|
+
if ignored_values.include? rolled_value
|
49
|
+
rolled_value = nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
rolled_value
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def roll_all_dice_from_descriptor( roll_descriptor )
|
58
|
+
rolled_values = []
|
59
|
+
1.upto roll_descriptor.count do
|
60
|
+
rolled_values << random_value_in_range( 1..roll_descriptor.die )
|
61
|
+
end
|
62
|
+
rolled_values.sort
|
63
|
+
end
|
64
|
+
|
65
|
+
def drop_lowest( rolled_values, roll_descriptor )
|
66
|
+
rolled_values.slice!( 0, roll_descriptor.drop_lowest )
|
67
|
+
end
|
68
|
+
|
69
|
+
def drop_highest( rolled_values, roll_descriptor )
|
70
|
+
rolled_values.slice!( rolled_values.size - roll_descriptor.drop_highest, roll_descriptor.drop_highest )
|
71
|
+
end
|
72
|
+
|
73
|
+
def total( rolled_values )
|
74
|
+
rolled_values.inject( :+ )
|
75
|
+
end
|
76
|
+
|
77
|
+
def collect_ignored_values( args )
|
78
|
+
ignored_values = []
|
79
|
+
until args.empty? do
|
80
|
+
value = args.shift
|
81
|
+
if value.is_a?(Integer)
|
82
|
+
ignored_values << value
|
83
|
+
elsif value.class == Range
|
84
|
+
value.each do |i|
|
85
|
+
ignored_values << i
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
ignored_values.uniq
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module TableScript
|
19
|
+
|
20
|
+
class RollDescriptor
|
21
|
+
|
22
|
+
attr_reader :die, :count, :drop_lowest, :drop_highest
|
23
|
+
|
24
|
+
def initialize( match )
|
25
|
+
initialize_die( match )
|
26
|
+
initialize_count( match )
|
27
|
+
initialize_dropped_dice( match )
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def initialize_die( match )
|
33
|
+
@die = match[ 2 ].to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize_count( match )
|
37
|
+
@count = 1
|
38
|
+
@count = match[ 1 ].to_i unless match[ 1 ].empty?
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize_dropped_dice( match )
|
42
|
+
@drop_lowest = 0
|
43
|
+
@drop_highest = 0
|
44
|
+
if match[ 4 ] == "dl"
|
45
|
+
@drop_lowest = 1
|
46
|
+
@drop_lowest = match[ 5 ].to_i unless match[ 5 ].empty?
|
47
|
+
elsif match[ 6 ] == "dh"
|
48
|
+
@drop_highest = 1
|
49
|
+
@drop_highest = match[ 7 ].to_i unless match[ 7 ].empty?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module TableScript
|
19
|
+
|
20
|
+
class Table
|
21
|
+
|
22
|
+
attr_reader :entries
|
23
|
+
|
24
|
+
def initialize( name, roller )
|
25
|
+
@name = name
|
26
|
+
@roller = roller
|
27
|
+
@entries = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def build( &blk )
|
31
|
+
@entries = TableEntryEnvironment.new( @name, @roller )
|
32
|
+
@entries.instance_eval( &blk )
|
33
|
+
end
|
34
|
+
|
35
|
+
def random_entry
|
36
|
+
@roller.rollD( @entries.die_to_roll )
|
37
|
+
end
|
38
|
+
|
39
|
+
def lookup( index )
|
40
|
+
@entries.lookup( index )
|
41
|
+
end
|
42
|
+
|
43
|
+
def roll
|
44
|
+
@entries.reroll
|
45
|
+
end
|
46
|
+
|
47
|
+
def roll_and_ignore_duplicates( times, args )
|
48
|
+
@entries.reroll_and_ignore_duplicates( times, args )
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module TableScript
|
19
|
+
|
20
|
+
class TableEntry
|
21
|
+
|
22
|
+
def initialize( blk )
|
23
|
+
@blk = blk
|
24
|
+
end
|
25
|
+
|
26
|
+
def evaluate( roll )
|
27
|
+
@blk.call( roll )
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module TableScript
|
19
|
+
|
20
|
+
class TableEntryEnvironment
|
21
|
+
|
22
|
+
def initialize( table_name, roller )
|
23
|
+
@table_name = table_name
|
24
|
+
@roller = roller
|
25
|
+
@entries = []
|
26
|
+
end
|
27
|
+
|
28
|
+
def fixed( *args, &blk )
|
29
|
+
if args.empty?
|
30
|
+
@entries << TableEntry.new( blk )
|
31
|
+
else
|
32
|
+
roll = args.shift
|
33
|
+
if roll.is_a?(Integer)
|
34
|
+
@entries[ roll - 1 ] = TableEntry.new( blk )
|
35
|
+
elsif roll.class == Range
|
36
|
+
entry = TableEntry.new( blk )
|
37
|
+
roll.each do |i|
|
38
|
+
@entries[ i - 1 ] = entry
|
39
|
+
end
|
40
|
+
end
|
41
|
+
raise "Too many parameters for f in table #{@table_name}" unless args.empty?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
alias :f :fixed
|
46
|
+
|
47
|
+
def dynamic( *args, &blk )
|
48
|
+
if args.empty?
|
49
|
+
@entries << TableEntry.new( blk )
|
50
|
+
else
|
51
|
+
count = args.shift
|
52
|
+
if count.is_a?(Integer)
|
53
|
+
entry = TableEntry.new( blk )
|
54
|
+
1.upto count do
|
55
|
+
@entries << entry
|
56
|
+
end
|
57
|
+
end
|
58
|
+
raise "Too many parameters for d in table #{@table_name}" unless args.empty?
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
alias :d :dynamic
|
63
|
+
|
64
|
+
def dice_to_roll
|
65
|
+
"d#{@entries.size}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def lookup( index )
|
69
|
+
raise "No table entry for a roll of #{index}." if ( index <= 0 or index > @entries.size )
|
70
|
+
@entries[ index - 1 ].evaluate( index )
|
71
|
+
end
|
72
|
+
|
73
|
+
def reroll
|
74
|
+
rolled_value = @roller.roll( dice_to_roll )
|
75
|
+
lookup( rolled_value )
|
76
|
+
end
|
77
|
+
|
78
|
+
def reroll_and_ignore( *args )
|
79
|
+
validate_ignored_values( args )
|
80
|
+
rolled_value = @roller.roll_and_ignore( dice_to_roll, args )
|
81
|
+
lookup( rolled_value )
|
82
|
+
end
|
83
|
+
|
84
|
+
def reroll_and_ignore_duplicates( times, *args )
|
85
|
+
ignored_entries = entries_from_ignored_values( args )
|
86
|
+
evaluated_rolled_entries = []
|
87
|
+
until evaluated_rolled_entries.size == times do
|
88
|
+
rolled_value = @roller.roll( dice_to_roll )
|
89
|
+
rolled_entry = @entries[ rolled_value - 1 ]
|
90
|
+
unless ignored_entries.include? rolled_entry
|
91
|
+
ignored_entries << rolled_entry
|
92
|
+
evaluated_rolled_entries << rolled_entry.evaluate( rolled_value )
|
93
|
+
end
|
94
|
+
end
|
95
|
+
evaluated_rolled_entries
|
96
|
+
end
|
97
|
+
|
98
|
+
def method_missing( method_id, *args )
|
99
|
+
raise "Undefined command '#{method_id}' in table #{@table_name}"
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def entries_from_ignored_values( args )
|
105
|
+
entries = []
|
106
|
+
args.each do |arg|
|
107
|
+
if arg.is_a?(Integer)
|
108
|
+
entries << lookup( arg )
|
109
|
+
elsif arg.class == Range
|
110
|
+
arg.each do |i|
|
111
|
+
entries << lookup( arg )
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
entries.uniq
|
116
|
+
end
|
117
|
+
|
118
|
+
def validate_ignored_values( args )
|
119
|
+
raise "No ignored values specified" if args.empty?
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module TableScript
|
19
|
+
VERSION = "0.0.4"
|
20
|
+
end
|
data/lib/tablescript.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'tablescript/dice_roller'
|
19
|
+
require 'tablescript/roll_descriptor'
|
20
|
+
require 'tablescript/table'
|
21
|
+
require 'tablescript/table_entry'
|
22
|
+
require 'tablescript/table_entry_environment'
|
23
|
+
|
24
|
+
$LOAD_PATH.push File.expand_path( ENV[ "TS_PATH" ] ) unless ENV[ "TS_PATH" ].nil?
|
25
|
+
|
26
|
+
$all_tables = {}
|
27
|
+
|
28
|
+
def table( name, &blk )
|
29
|
+
begin
|
30
|
+
new_table = TableScript::Table.new( name, TableScript::DiceRoller.new )
|
31
|
+
new_table.build( &blk )
|
32
|
+
$all_tables[ name ] = new_table
|
33
|
+
rescue Exception => e
|
34
|
+
puts e
|
35
|
+
exit
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def roll_on( name )
|
40
|
+
raise "No table named '#{name}'" if $all_tables[ name ].nil?
|
41
|
+
$all_tables[ name ].roll
|
42
|
+
end
|
43
|
+
|
44
|
+
def roll_on_and_ignore_duplicates( name, times, *args )
|
45
|
+
begin
|
46
|
+
raise "No table named '#{name}'" if $all_tables[ name ].nil?
|
47
|
+
$all_tables[ name ].roll_and_ignore_duplicates( times, args )
|
48
|
+
rescue Exception => e
|
49
|
+
puts e
|
50
|
+
exit
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def lookup( name, roll )
|
55
|
+
begin
|
56
|
+
raise "No table named '#{name}'" if $all_tables[ name ].nil?
|
57
|
+
$all_tables[ name ].lookup( roll )
|
58
|
+
rescue Exception => e
|
59
|
+
puts e
|
60
|
+
exit
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def roll_dice( dice )
|
65
|
+
begin
|
66
|
+
TableScript::DiceRoller.new.roll( dice.dup )
|
67
|
+
rescue Exception => e
|
68
|
+
puts e
|
69
|
+
exit
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def choose( options )
|
74
|
+
options[ TableScript::DiceRoller.new.random_value_in_range( 1..options.size ) - 1 ]
|
75
|
+
end
|
76
|
+
|
77
|
+
class String
|
78
|
+
def roll
|
79
|
+
roll_dice(self)
|
80
|
+
end
|
81
|
+
end
|
data/tablescript.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Copyright 2015 Jamie Hale
|
2
|
+
#
|
3
|
+
# This file is part of the Tablescript.rb gem.
|
4
|
+
#
|
5
|
+
# Tablescript.rb is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Tablescript.rb is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
# coding: utf-8
|
19
|
+
lib = File.expand_path('../lib', __FILE__)
|
20
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
21
|
+
|
22
|
+
require 'tablescript/version'
|
23
|
+
|
24
|
+
Gem::Specification.new do |s|
|
25
|
+
s.name = 'tablescript'
|
26
|
+
s.version = TableScript::VERSION
|
27
|
+
s.date = '2015-05-05'
|
28
|
+
s.summary = "TableScript RPG Tool"
|
29
|
+
s.description = "Library for creating role-playing game random tables."
|
30
|
+
s.authors = [ 'Jamie Hale' ]
|
31
|
+
s.email = [ 'jamie@smallarmyofnerds.com' ]
|
32
|
+
s.homepage = 'http://smallarmyofnerds.com/tablescript'
|
33
|
+
s.license = "GPL-3.0"
|
34
|
+
s.platform = Gem::Platform::RUBY
|
35
|
+
s.files = `git ls-files -z`.split( "\x0" )
|
36
|
+
s.require_paths = [ 'lib' ]
|
37
|
+
s.metadata['allowed_push_host'] = 'https://rubygems.org'
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tablescript
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jamie Hale
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Library for creating role-playing game random tables.
|
14
|
+
email:
|
15
|
+
- jamie@smallarmyofnerds.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- COPYING
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/tablescript.rb
|
25
|
+
- lib/tablescript/dice_roller.rb
|
26
|
+
- lib/tablescript/roll_descriptor.rb
|
27
|
+
- lib/tablescript/table.rb
|
28
|
+
- lib/tablescript/table_entry.rb
|
29
|
+
- lib/tablescript/table_entry_environment.rb
|
30
|
+
- lib/tablescript/version.rb
|
31
|
+
- tablescript.gemspec
|
32
|
+
homepage: http://smallarmyofnerds.com/tablescript
|
33
|
+
licenses:
|
34
|
+
- GPL-3.0
|
35
|
+
metadata:
|
36
|
+
allowed_push_host: https://rubygems.org
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 2.6.8
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: TableScript RPG Tool
|
57
|
+
test_files: []
|