fifthed_sim 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68200fa0518a4975d872ed4583404f3a3e5a6a9f
4
+ data.tar.gz: a89f5017dcfeca2e354ee3173694139ed9f3c1a8
5
+ SHA512:
6
+ metadata.gz: ba637784bc60f87a409327852fc3468e901d96b7701d17ea9cc5fcb0e853be09da6f4fb79dd14d4c0b48381fc551d0f47a9b2f79eac317283c0a2678b1537c8f
7
+ data.tar.gz: f828486f33596455f97f861738aa8e0646f9de188f0073cb889afa8f8566e2b512b64a47b00617cb059aa62a6347c331d926bd88497b4e05d40f7326b8c3cae3
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ - 2.1.1
6
+ - 2.3.0
7
+ - 2.4.0
8
+
9
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fifthed_sim.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Anthony Super
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # FifthedSim
2
+
3
+ This is a gem to simulate a game that you play with dice on a d20 system.
4
+ It is unfinished, but intends to enable a user to run simulations, or to see the overall probability of things which happen.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'fifthed_sim'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install fifthed_sim
21
+
22
+ ## Usage
23
+
24
+ See the rdoc for now.
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/anthonysuper/fifthedsim.
35
+
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
40
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fifthed_sim"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fifthed_sim/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fifthed_sim"
8
+ spec.version = FifthedSim::VERSION
9
+ spec.authors = ["Anthony Super"]
10
+ spec.email = ["anthony@noided.media"]
11
+
12
+ spec.summary = %q{A 5th edition D&D encounter simulator}
13
+ spec.description = %q{This is a simulator to run a D&D encounter. It provides DSL-like tools to describe actions and abilities, and then it will (eventually) play with some degree of AI skill.}
14
+ spec.homepage = "https://github.com/AnthonySuper/FifthedSim"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,18 @@
1
+ require "fifthed_sim/version"
2
+ require "fifthed_sim/dice_result"
3
+ require "fifthed_sim/die_roll"
4
+ require "fifthed_sim/dice_calculation"
5
+ require "fifthed_sim/distribution"
6
+ require "securerandom"
7
+
8
+ module FifthedSim
9
+ def self.d(*args)
10
+ DiceResult.d(*args)
11
+ end
12
+ end
13
+
14
+ class Fixnum
15
+ def d(o)
16
+ FifthedSim.d(self, o)
17
+ end
18
+ end
@@ -0,0 +1,88 @@
1
+ require_relative './helpers/average_comparison'
2
+
3
+ module CalculatedFixnum
4
+ refine Fixnum do
5
+ include FifthedSim::AverageComparison
6
+
7
+ def value
8
+ self
9
+ end
10
+
11
+ def average
12
+ self
13
+ end
14
+
15
+ def has_critfail?
16
+ false
17
+ end
18
+
19
+ def has_crit?
20
+ false
21
+ end
22
+
23
+ def distance_from_average
24
+ 0
25
+ end
26
+
27
+ def distribution
28
+ FifthedSim::Distribution.for_number(self)
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+
35
+
36
+ module FifthedSim
37
+ class DiceCalculation
38
+ include AverageComparison
39
+ using CalculatedFixnum
40
+
41
+ def initialize(*values)
42
+ values.each{|v| check_type(v) }
43
+ @components = values
44
+ end
45
+
46
+ def +(other)
47
+ check_type(other)
48
+ @components << other
49
+ self
50
+ end
51
+
52
+ def value
53
+ # Symbol::& uses send, so refinements would break
54
+ # How sad
55
+ @components.map{|x| x.value}.inject(:+)
56
+ end
57
+
58
+ def average
59
+ @components.map{|x| x.average}.inject(:+)
60
+ end
61
+
62
+ def has_critfail?
63
+ @components.any?{|x| x.has_critfail?}
64
+ end
65
+
66
+ def has_crit?
67
+ @components.any?{|x| x.has_crit?}
68
+ end
69
+
70
+ def distribution
71
+ # TODO: Maybe figure out how to minimize convolution expense?
72
+ @components.map{|x| x.distribution}.inject do |memo, p|
73
+ memo.convolve(p)
74
+ end
75
+ end
76
+
77
+
78
+ private
79
+ ALLOWED_TYPES = [DiceResult,
80
+ Fixnum]
81
+
82
+ def check_type(obj)
83
+ unless ALLOWED_TYPES.any?{|t| obj.is_a?(t) }
84
+ raise TypeError, "not a proper member type"
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,108 @@
1
+ require_relative './helpers/average_comparison'
2
+ require_relative './distribution'
3
+
4
+ class Fixnum
5
+ def factorial
6
+ (1..self).inject(:*) || 1
7
+ end
8
+ end
9
+
10
+ def combination(n, r)
11
+ n.factorial / (r.factorial * (n - r).factorial)
12
+ end
13
+
14
+ module FifthedSim
15
+ ##
16
+ # This class models the result of a dice roll.
17
+ # It is filled with the actual result of randomly-rolled dice, but contains
18
+ # methods to enable the calculation of average values.
19
+ #
20
+ # When you add a modifier it becomes a DiceCalculation.
21
+ class DiceResult
22
+ def self.d(num, type)
23
+ self.new(num.times.map{DieRoll.roll(type)})
24
+ end
25
+
26
+ include AverageComparison
27
+
28
+ def initialize(array)
29
+ unless array.is_a?(Array) && ! array.empty?
30
+ raise ArgumentError, "Not a valid array"
31
+ end
32
+ unless array.all?{|elem| elem.is_a?(DieRoll) }
33
+ raise ArgumentError, "Not all die rolls"
34
+ end
35
+ @array = array
36
+ end
37
+
38
+ def has_crit?
39
+ @array.any?(&:crit?)
40
+ end
41
+
42
+ def average
43
+ @array.map(&:average).inject(:+)
44
+ end
45
+
46
+ def has_critfail?
47
+ @array.any?(&:critfail?)
48
+ end
49
+
50
+ def dice
51
+ @array.dup
52
+ end
53
+
54
+ def to_i
55
+ @array.map(&:to_i).inject(:+)
56
+ end
57
+
58
+ def to_f
59
+ to_i.to_f
60
+ end
61
+
62
+ def value
63
+ to_i
64
+ end
65
+
66
+ def +(other)
67
+ a = DiceCalculation.new(self)
68
+ (a + other)
69
+ end
70
+
71
+ def roll_count
72
+ @array.count
73
+ end
74
+
75
+ def dice_type
76
+ @array.first.type
77
+ end
78
+
79
+
80
+ def min_value
81
+ roll_count
82
+ end
83
+
84
+ def max_value
85
+ dice_type * roll_count
86
+ end
87
+
88
+ def distribution
89
+ total_possible = (dice_type ** roll_count)
90
+ mapped = min_value.upto(max_value).map do |k|
91
+ [k, occurences(k)]
92
+ end
93
+ Distribution.new(Hash[mapped], total_possible)
94
+ end
95
+
96
+ private
97
+
98
+ def occurences(num)
99
+ dice_type = @array.first.type
100
+ num_dice = @array.size
101
+ 0.upto((num - num_dice) / dice_type).map do |k|
102
+ ((-1) ** k) *
103
+ combination(num_dice, k) *
104
+ combination(num - dice_type*k - 1, num_dice- 1)
105
+ end.inject(:+)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,66 @@
1
+ require_relative './helpers/average_comparison'
2
+
3
+ module FifthedSim
4
+ class DieRoll
5
+ include AverageComparison
6
+
7
+ def self.roll(type)
8
+ raise ArgumentError, "Must be an Integer" unless type.is_a? Fixnum
9
+ self.new(SecureRandom.random_number(type - 1) + 1, type)
10
+ end
11
+
12
+ # Obtain a DieRoll filled with the average result of this die type
13
+ # This will round down.
14
+ def self.average(type)
15
+ self.new((type + 1) / 2, type)
16
+ end
17
+
18
+ # Obtain an average value for this die type, as a float
19
+ # We're extremely lazy here.
20
+ def self.average_value(type)
21
+ self.new(1, type).average
22
+ end
23
+
24
+ def initialize(val, type)
25
+ unless val.is_a?(Fixnum) && type.is_a?(Fixnum)
26
+ raise ArgumentError, "Type invald"
27
+ end
28
+ @value = val
29
+ @type = type
30
+ end
31
+
32
+ attr_reader :value, :type
33
+
34
+ def average
35
+ (@type + 1) / 2.0
36
+ end
37
+
38
+ def difference_from_average
39
+ @value - average
40
+ end
41
+
42
+ def critfail?
43
+ @value == 1
44
+ end
45
+
46
+ def crit?
47
+ @value == @type
48
+ end
49
+
50
+ def to_i
51
+ @value
52
+ end
53
+
54
+ def to_f
55
+ @value.to_f
56
+ end
57
+
58
+ def method_missing(method, *args, **kwards, &block)
59
+ if @value.respond_to? method
60
+ @value.public_send(method,*args,**kwards,&block)
61
+ else
62
+ super
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,73 @@
1
+ module FifthedSim
2
+ class Distribution
3
+ def self.for_number(num)
4
+ self.new({num => 1}, 1)
5
+ end
6
+
7
+ def initialize(map, total_possible)
8
+ keys = map.keys
9
+ @max = keys.max
10
+ @min = keys.min
11
+ @map = map
12
+ @total_possible = total_possible
13
+ end
14
+
15
+ attr_reader :total_possible,
16
+ :min,
17
+ :max
18
+
19
+ def range
20
+ (@max..@min)
21
+ end
22
+
23
+ def map
24
+ @map.dup
25
+ end
26
+
27
+ def probability_map
28
+ Hash[@map.map{|k, v| [k, v / @total_possible.to_f]}]
29
+ end
30
+
31
+ def percent_exactly(num)
32
+ return 0 if num < @min || num > @max
33
+ @map[num] / @total_possible.to_f
34
+ end
35
+
36
+ def percent_least(num)
37
+ return 0 if num < @min
38
+ return 1 if num >= @max
39
+
40
+ n = @min.upto(num).map(&map_proc).inject(:+)
41
+ n / @total_possible.to_f
42
+ end
43
+
44
+ def convolve(other)
45
+ h = {}
46
+ abs_min = [@min, other.min].min
47
+ abs_max = [@max, other.max].max
48
+ min_possible = @min + other.min
49
+ max_possible = @max + other.max
50
+ tp = @total_possible * other.total_possible
51
+ # TODO: there has to be a less stupid way to do this right?
52
+ v = min_possible.upto(max_possible).map do |val|
53
+ sum = abs_min.upto(abs_max).map do |m|
54
+ percent_exactly(m) * other.percent_exactly(val - m)
55
+ end.inject(:+)
56
+ [val, (sum * tp).to_i]
57
+ end
58
+ self.class.new(Hash[v], tp)
59
+ end
60
+
61
+ def ==(other)
62
+ @map == other.map &&
63
+ @total_possible == other.total_possible
64
+ end
65
+
66
+ private
67
+ def map_proc
68
+ return Proc.new do |arg|
69
+ res = @map[arg]
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,14 @@
1
+ module FifthedSim
2
+ module AverageComparison
3
+ # metaprogramming lolz
4
+ {"above_" => :>, "below_" => :<, "" => :==}.each do |k,v|
5
+ define_method "#{k}average?" do
6
+ value.public_send(v, average)
7
+ end
8
+ end
9
+
10
+ def difference_from_average
11
+ value - average
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module FifthedSim
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fifthed_sim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Super
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: This is a simulator to run a D&D encounter. It provides DSL-like tools
56
+ to describe actions and abilities, and then it will (eventually) play with some
57
+ degree of AI skill.
58
+ email:
59
+ - anthony@noided.media
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - fifthed_sim.gemspec
74
+ - lib/fifthed_sim.rb
75
+ - lib/fifthed_sim/dice_calculation.rb
76
+ - lib/fifthed_sim/dice_result.rb
77
+ - lib/fifthed_sim/die_roll.rb
78
+ - lib/fifthed_sim/distribution.rb
79
+ - lib/fifthed_sim/helpers/average_comparison.rb
80
+ - lib/fifthed_sim/version.rb
81
+ homepage: https://github.com/AnthonySuper/FifthedSim
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A 5th edition D&D encounter simulator
105
+ test_files: []