oakdex-battle 0.0.2 → 0.1.0

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.
@@ -1,130 +0,0 @@
1
- module Oakdex
2
- class Battle
3
- # Creates Pokemon instance and prefills attributes
4
- class PokemonFactory
5
- ATTRIBUTES = %i[exp gender ability nature hp iv ev moves]
6
-
7
- class << self
8
- def create(species, options = {})
9
- factory = new(species, options)
10
- attributes = Hash[ATTRIBUTES.map do |attr|
11
- [attr, factory.send(attr)]
12
- end]
13
- Pokemon.new(species, attributes)
14
- end
15
- end
16
-
17
- def initialize(species, options = {})
18
- @species = species
19
- @options = options
20
- end
21
-
22
- private
23
-
24
- def moves
25
- if @options[:moves]
26
- @options[:moves].map do |move_data|
27
- Move.new(
28
- Oakdex::Pokedex::Move.find!(move_data[0]),
29
- move_data[1],
30
- move_data[2]
31
- )
32
- end
33
- else
34
- (generate_available_moves + additional_moves).take(4)
35
- end
36
- end
37
-
38
- def generate_available_moves
39
- available_moves.sample(4).map do |move_name|
40
- move_type = Oakdex::Pokedex::Move.find!(move_name)
41
- Move.new(move_type, move_type.pp, move_type.pp)
42
- end
43
- end
44
-
45
- def additional_moves
46
- return [] unless @options[:additional_moves]
47
- @options[:additional_moves].map do |move_name|
48
- move_type = Oakdex::Pokedex::Move.find!(move_name)
49
- Move.new(move_type, move_type.pp, move_type.pp)
50
- end
51
- end
52
-
53
- def available_moves
54
- @species.learnset.map do |m|
55
- m['move'] if m['level'] && m['level'] <= level
56
- end.compact
57
- end
58
-
59
- def ability
60
- if @options['ability']
61
- Oakdex::Pokedex::Ability.find!(@options['ability'])
62
- else
63
- Oakdex::Pokedex::Ability.find!(abilities.sample['name'])
64
- end
65
- end
66
-
67
- def abilities
68
- @species.abilities.select { |a| !a['hidden'] && !a['mega'] }
69
- end
70
-
71
- def exp
72
- @options[:exp] || PokemonStat.exp_by_level(
73
- @species.leveling_rate,
74
- @options[:level]
75
- )
76
- end
77
-
78
- def level
79
- PokemonStat.level_by_exp(@species.leveling_rate, exp)
80
- end
81
-
82
- def hp
83
- return @options[:hp] if @options[:hp]
84
- PokemonStat.initial_stat(:hp,
85
- level: level,
86
- iv: iv,
87
- ev: ev,
88
- base_stats: @species.base_stats,
89
- nature: nature
90
- )
91
- end
92
-
93
- def iv
94
- return @options[:iv] if @options[:iv]
95
- @iv ||= Hash[Pokemon::BATTLE_STATS.map do |stat|
96
- [stat, rand(0..31)]
97
- end]
98
- end
99
-
100
- def ev
101
- return @options[:ev] if @options[:ev]
102
- @ev ||= Hash[Pokemon::BATTLE_STATS.map do |stat|
103
- [stat, 0]
104
- end]
105
- end
106
-
107
- def gender
108
- return @options[:gender] if @options[:gender]
109
- return 'neuter' unless @species.gender_ratios
110
- calculate_gender
111
- end
112
-
113
- def calculate_gender
114
- if rand(1..1000) <= @species.gender_ratios['male'] * 10
115
- 'male'
116
- else
117
- 'female'
118
- end
119
- end
120
-
121
- def nature(options = {})
122
- @nature ||= if options[:nature]
123
- Oakdex::Pokedex::Nature.find!(options[:nature])
124
- else
125
- Oakdex::Pokedex::Nature.all.values.sample
126
- end
127
- end
128
- end
129
- end
130
- end
@@ -1,109 +0,0 @@
1
- module Oakdex
2
- class Battle
3
- # Calculates Pokemon Stats
4
- class PokemonStat
5
- STAGE_MULTIPLIERS = {
6
- -6 => Rational(2, 8),
7
- -5 => Rational(2, 7),
8
- -4 => Rational(2, 6),
9
- -3 => Rational(2, 5),
10
- -2 => Rational(2, 4),
11
- -1 => Rational(2, 3),
12
- 0 => Rational(2, 2),
13
- 1 => Rational(3, 2),
14
- 2 => Rational(4, 2),
15
- 3 => Rational(5, 2),
16
- 4 => Rational(6, 2),
17
- 5 => Rational(7, 2),
18
- 6 => Rational(8, 2)
19
- }
20
-
21
- STAGE_MULTIPLIERS_CRITICAL_HIT = {
22
- 0 => Rational(1, 24),
23
- 1 => Rational(1, 8),
24
- 2 => Rational(1, 2),
25
- 3 => Rational(1, 1)
26
- }
27
-
28
- STAGE_MULTIPLIERS_ACC_EVA = {
29
- -6 => Rational(3, 9),
30
- -5 => Rational(3, 8),
31
- -4 => Rational(3, 7),
32
- -3 => Rational(3, 6),
33
- -2 => Rational(3, 5),
34
- -1 => Rational(3, 4),
35
- 0 => Rational(3, 3),
36
- 1 => Rational(4, 3),
37
- 2 => Rational(5, 3),
38
- 3 => Rational(6, 3),
39
- 4 => Rational(7, 3),
40
- 5 => Rational(8, 3),
41
- 6 => Rational(9, 3)
42
- }
43
-
44
- class << self
45
- def initial_stat(stat, options = {})
46
- first_part = initial_stat_first_part(stat, options)
47
- (
48
- if stat == :hp
49
- first_part + options[:level] + 10
50
- elsif stat.to_s == options[:nature].increased_stat
51
- (first_part + 5) * 1.1
52
- elsif stat.to_s == options[:nature].decreased_stat
53
- (first_part + 5) * 0.9
54
- else
55
- first_part + 5
56
- end
57
- ).to_i
58
- end
59
-
60
- def exp_by_level(leveling_rate, level)
61
- case leveling_rate
62
- when 'Fast' then ((4.0 * level**3) / 5).to_i
63
- when 'Slow' then ((5.0 * level**3) / 4).to_i
64
- when 'Medium Slow' then medium_slow_exp(level)
65
- when 'Fluctuating' then fluctuating_exp(level)
66
- else level**3
67
- end
68
- end
69
-
70
- def level_by_exp(leveling_rate, exp)
71
- level = 2
72
- level += 1 while exp_by_level(leveling_rate, level) <= exp
73
- level - 1
74
- end
75
-
76
- private
77
-
78
- def medium_slow_exp(level)
79
- (
80
- ((6.0 / 5) * level**3) - 15 * level**2 + (100 * level) - 140
81
- ).to_i
82
- end
83
-
84
- def fluctuating_exp(level)
85
- (
86
- if level <= 15
87
- level**3 * ((((level + 1) / 3.0) + 24) / 50)
88
- elsif level <= 36
89
- level**3 * ((level + 14) / 50.0)
90
- else
91
- level**3 * (((level / 2.0) + 32) / 50)
92
- end
93
- ).to_i
94
- end
95
-
96
- def initial_stat_first_part(stat, options = {})
97
- (
98
- (
99
- 2.0 *
100
- options[:base_stats][stat.to_s] +
101
- options[:iv][stat] +
102
- (options[:ev][stat] / 4)
103
- ) * options[:level]
104
- ) / 100
105
- end
106
- end
107
- end
108
- end
109
- end