Class: FirstAidAction

Inherits:
Natural20::Action show all
Defined in:
lib/natural_20/actions/first_aid_action.rb

Instance Attribute Summary collapse

Attributes inherited from Natural20::Action

#action_type, #errors, #result, #session, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Natural20::Action

#initialize, #label, #name, #validate

Constructor Details

This class inherits a constructor from Natural20::Action

Instance Attribute Details

#targetObject

Returns the value of attribute target.



2
3
4
# File 'lib/natural_20/actions/first_aid_action.rb', line 2

def target
  @target
end

Class Method Details

.apply!(battle, item) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/natural_20/actions/first_aid_action.rb', line 91

def self.apply!(battle, item)
  case item[:type]
  when :first_aid
    if item[:success]
      item[:target].stable!
      Natural20::EventManager.received_event(event: :first_aid,
                                             target: item[:target], source: item[:source],
                                             roll: item[:roll])
    else
      Natural20::EventManager.received_event(event: :first_aid_failure,
                                             target: item[:target], source: item[:source],
                                             roll: item[:roll])
    end

    battle.consume(item[:source], :action)
  end
end

.build(session, source) ⇒ Object



53
54
55
56
# File 'lib/natural_20/actions/first_aid_action.rb', line 53

def self.build(session, source)
  action = FirstAidAction.new(session, source, :grapple)
  action.build_map
end

.can?(entity, battle, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/natural_20/actions/first_aid_action.rb', line 4

def self.can?(entity, battle, _options = {})
  (battle.nil? || entity.total_actions(battle).positive?) && unconscious_targets(entity, battle).size.positive?
end

.unconscious_targets(entity, battle) ⇒ Object

Parameters:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/natural_20/actions/first_aid_action.rb', line 10

def self.unconscious_targets(entity, battle)
  return [] unless battle
  return [] unless battle.map

  adjacent_squares = entity.melee_squares(battle.map, adjacent_only: true)
  entities = []
  adjacent_squares.select do |pos|
    entity_pos = battle.map.entity_at(*pos)
    next unless entity_pos
    next if entity_pos == entity
    next unless battle.map.can_see?(entity, entity_pos)

    entities << entity_pos if entity_pos.unconscious? && !entity_pos.stable? && !entity_pos.dead?
  end
  entities
end

Instance Method Details

#build_mapObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/natural_20/actions/first_aid_action.rb', line 31

def build_map
  OpenStruct.new({
                   action: self,
                   param: [
                     {
                       type: :select_target,
                       range: 5,
                       target_types: %i[enemies allies],
                       filter: 'state:unconscious & state:!stable & state:!dead',
                       num: 1
                     }
                   ],
                   next: lambda { |target|
                     self.target = target
                     OpenStruct.new({
                                      param: nil,
                                      next: -> { self }
                                    })
                   }
                 })
end

#resolve(_session, _map, opts = {}) ⇒ Object

Build the attack roll information

Parameters:

Options Hash (opts):



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/natural_20/actions/first_aid_action.rb', line 63

def resolve(_session, _map, opts = {})
  target = opts[:target] || @target
  battle = opts[:battle]
  raise 'target is a required option for :first_aid' if target.nil?

  medicine_check = @source.medicine_check!(battle)

  @result = if medicine_check.result >= 10
              [{
                source: @source,
                target: target,
                type: :first_aid,
                success: true,
                battle: battle,
                roll: medicine_check
              }]
            else
              [{
                source: @source,
                target: target,
                type: :first_aid,
                success: false,
                battle: battle,
                roll: medicine_check
              }]
            end
end

#to_sObject



27
28
29
# File 'lib/natural_20/actions/first_aid_action.rb', line 27

def to_s
  @action_type.to_s.humanize
end