class Oakdex::Battle::Action

Represents one Action. One turn has many actions.

Constants

ITEM_PRIORITY
RECALL_PRIORITY

Attributes

damage[R]
trainer[R]
turn[RW]

Public Class Methods

new(trainer, attributes) click to toggle source
# File lib/oakdex/battle/action.rb, line 17
def initialize(trainer, attributes)
  @trainer = trainer
  @attributes = attributes
end

Public Instance Methods

execute() click to toggle source
# File lib/oakdex/battle/action.rb, line 66
def execute
  return execute_growth if growth?
  return execute_recall if recall?
  return execute_use_item if item?
  targets.each { |t| MoveExecution.new(self, t).execute }
end
hitting?() click to toggle source
# File lib/oakdex/battle/action.rb, line 61
def hitting?
  @hitting = rand(1..1000) <= hitting_probability ? 1 : 0
  @hitting == 1
end
hitting_probability() click to toggle source
# File lib/oakdex/battle/action.rb, line 57
def hitting_probability
  ((move.accuracy / 100.0) * (pokemon.accuracy / target.evasion)) * 1000
end
item_id() click to toggle source
# File lib/oakdex/battle/action.rb, line 73
def item_id
  @attributes['item_id']
end
move() click to toggle source
# File lib/oakdex/battle/action.rb, line 51
def move
  return unless @attributes['move']
  @move ||= pokemon.moves.find { |m| m.name == @attributes['move'] }
  @move ||= Oakdex::Pokemon::Move.create(@attributes['move'])
end
pokemon() click to toggle source
# File lib/oakdex/battle/action.rb, line 26
def pokemon
  return pokemon_by_team_position if item?
  recall? ? pokemon_by_position : battle.pokemon_by_id(pokemon_id)
end
pokemon_id() click to toggle source
# File lib/oakdex/battle/action.rb, line 31
def pokemon_id
  move? ? @attributes['pokemon'] : nil
end
pokemon_position() click to toggle source
# File lib/oakdex/battle/action.rb, line 35
def pokemon_position
  recall? ? @attributes['pokemon'] : nil
end
priority() click to toggle source
# File lib/oakdex/battle/action.rb, line 22
def priority
  move&.priority || (recall? ? RECALL_PRIORITY : ITEM_PRIORITY)
end
target() click to toggle source
# File lib/oakdex/battle/action.rb, line 39
def target
  recall? ? battle.pokemon_by_id(@attributes['target']) : targets
end
target_id() click to toggle source
# File lib/oakdex/battle/action.rb, line 43
def target_id
  recall? ? @attributes['target'] : nil
end
type() click to toggle source
# File lib/oakdex/battle/action.rb, line 47
def type
  @attributes['action']
end

Private Instance Methods

add_log(*args) click to toggle source
# File lib/oakdex/battle/action.rb, line 168
def add_log(*args)
  battle.add_to_log(*args)
end
add_recalls_log() click to toggle source
# File lib/oakdex/battle/action.rb, line 172
def add_recalls_log
  if pokemon
    add_log 'recalls', trainer.name, pokemon.name, target.name
  else
    add_log 'recalls_for_fainted', trainer.name, target.name
  end
end
execute_growth() click to toggle source
# File lib/oakdex/battle/action.rb, line 141
def execute_growth
  trainer.growth_event.execute(@attributes['option'])
  while trainer.growth_event? && trainer.growth_event.read_only?
    e = trainer.growth_event
    add_log e.message
    e.execute
  end
end
execute_recall() click to toggle source
# File lib/oakdex/battle/action.rb, line 127
def execute_recall
  add_recalls_log
  if pokemon
    trainer.remove_from_battle(pokemon, side)
    trainer.send_to_battle(target, side)
  else
    trainer.send_to_battle(target, side)
  end
end
execute_use_item() click to toggle source
# File lib/oakdex/battle/action.rb, line 150
def execute_use_item
  add_log 'uses_item_on_pokemon', trainer.name, pokemon.name, item_id
  consumed = pokemon.use_item(item_id, in_battle: true)
  trainer.consume_item(item_id) if consumed
  action_id = 0
  while pokemon.growth_event? do
    event = pokemon.growth_event
    if event.read_only?
      add_log trainer.name, pokemon.name, event.message
      event.execute
    else
      raise 'Invalid Item Usage' unless item_actions[action_id]
      event.execute(item_actions[action_id])
      action_id += 1
    end
  end
end
growth?() click to toggle source
# File lib/oakdex/battle/action.rb, line 105
def growth?
  type == 'growth_event'
end
item?() click to toggle source
# File lib/oakdex/battle/action.rb, line 101
def item?
  type == 'use_item_on_pokemon'
end
item_actions() click to toggle source
# File lib/oakdex/battle/action.rb, line 137
def item_actions
  @attributes['item_actions']
end
move?() click to toggle source
# File lib/oakdex/battle/action.rb, line 97
def move?
  type == 'move'
end
pokemon_by_position() click to toggle source
# File lib/oakdex/battle/action.rb, line 109
def pokemon_by_position
  trainer.active_in_battle_pokemon
    .find { |ibp| ibp.position == @attributes['pokemon'] }&.pokemon
end
pokemon_by_team_position() click to toggle source
# File lib/oakdex/battle/action.rb, line 114
def pokemon_by_team_position
  trainer.team[@attributes['pokemon_team_pos']]
end
recall?() click to toggle source
# File lib/oakdex/battle/action.rb, line 93
def recall?
  type == 'recall'
end
side() click to toggle source
# File lib/oakdex/battle/action.rb, line 123
def side
  battle.sides.find { |s| s.trainer_on_side?(trainer) }
end
target_by_position(side, position) click to toggle source
# File lib/oakdex/battle/action.rb, line 118
def target_by_position(side, position)
  side.active_in_battle_pokemon
    .find { |ibp| ibp.position == position }&.pokemon
end
target_list() click to toggle source
# File lib/oakdex/battle/action.rb, line 86
def target_list
  list = @attributes['target']
  return [] if (list || []).empty?
  list = [list] unless list[0].is_a?(Array)
  list
end
targets() click to toggle source
# File lib/oakdex/battle/action.rb, line 79
def targets
  target_list.map do |target|
    side = battle.side_by_id(target[0])
    target_by_position(side, target[1])
  end.compact
end