Class: Natural20::Npc

Inherits:
Object
  • Object
show all
Includes:
Multiattack, Entity, HealthFlavor, Lootable, Notable
Defined in:
lib/natural_20/npc.rb

Constant Summary

Constants included from Entity

Entity::ALL_SKILLS, Entity::ATTRIBUTE_TYPES, Entity::ATTRIBUTE_TYPES_ABBV, Entity::SKILL_AND_ABILITY_MAP

Instance Attribute Summary collapse

Attributes included from Entity

#casted_effects, #color, #current_hit_die, #death_fails, #death_saves, #effects, #entity_event_hooks, #entity_uid, #max_hit_die, #session, #statuses

Instance Method Summary collapse

Methods included from Multiattack

#clear_multiattack!, #multi_attack_actions, #multiattack?, #reset_turn!

Methods included from HealthFlavor

#describe_health

Methods included from Notable

#list_notes

Methods included from Entity

#ability_mod, #acrobatics_proficient?, #action?, #active_effects, #add_casted_effect, #add_item, #all_ability_mods, #all_ability_scores, #any_class_feature?, #athletics_proficient?, #attach_handler, #attack_ability_mod, #attack_roll_mod, #available_movement, #available_spells, #break_stealth!, #can_see?, #carry_capacity, #cha_mod, #check_equip, #con_mod, #conscious!, #conscious?, #darkvision?, #dead!, #dead?, #death_saving_throw!, #deduct_item, #description, #dex_mod, #dexterity_check!, #disengage!, #disengage?, #dismiss_effect!, #dodge?, #dodging!, #drop_grapple!, #drop_items!, #entered_melee?, #equip, #equipped?, #equipped_items, #equipped_weapons, #escape_grapple_from!, #expertise, #expertise?, #free_object_interaction?, #grappled?, #grappled_by!, #grapples, #grappling, #grappling?, #grappling_targets, #hand_slots_required, #has_reaction?, #has_spell_effect?, #has_spells?, #heal!, #help!, #help?, #hiding!, #hiding?, #hit_die, #incapacitated?, #initiative!, #insight_proficient?, #int_mod, #inventory, #inventory_count, #inventory_weight, #investigation_proficient?, #item_count, #items_label, #label, #languages, #light_properties, #locate_melee_positions, #lockpick!, #long_jump_distance, #max_spell_slots, #medicine_check!, #medicine_proficient?, #melee_squares, #object?, #opened?, #passive_perception, #pc?, #perception_proficient?, #proficiency_bonus, #proficient?, #proficient_with_armor?, #proficient_with_weapon?, #prone!, #prone?, #push_from, #race, #ranged_spell_attack!, #register_effect, #register_event_hook, #reset_turn!, #resistant_to?, #saving_throw!, #sentient?, #shield_equipped?, #short_rest!, #size_identifier, #spell_slots, #squeezed!, #squeezed?, #stable!, #stable?, #stand!, #standing_jump_distance, #stealth_proficient?, #str_mod, #strength_check!, #take_damage!, #to_item, #token_size, #total_actions, #total_bonus_actions, #total_reactions, #trigger_event, #unconscious!, #unconscious?, #unequip, #unequip_all, #ungrapple, #unsqueeze, #usable_items, #usable_objects, #use_hit_die!, #used_hand_slots, #wearing_armor?, #wis_mod, #wisdom_check!

Methods included from EntityStateEvaluator

#apply_effect, #eval_if

Methods included from Lootable

#build_map, #interactable?, #resolve, #use!

Methods included from Container

#retrieve, #store

Constructor Details

#initialize(session, type, opt = {}) ⇒ Npc

Returns a new instance of Npc.

Parameters:

  • session (Session)
  • type (String, Symbol)
  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • rand_life (Boolean)

    Determines if will use die for npc HP instead of fixed value



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/natural_20/npc.rb', line 17

def initialize(session, type, opt = {})
  @properties = YAML.load_file(File.join("npcs", "#{type}.yml")).deep_symbolize_keys!
  @properties.merge!(opt[:overrides].presence || {})
  @ability_scores = @properties[:ability]
  @color = @properties[:color]
  @session = session
  @npc_type = type
  @inventory = @properties[:default_inventory]&.map do |inventory|
    [inventory[:type].to_sym, OpenStruct.new({ qty: inventory[:qty] })]
  end.to_h || {}

  @properties[:inventory]&.each do |inventory|
    @inventory[inventory[:type].to_sym] = OpenStruct.new({ qty: inventory[:qty] })
  end

  @npc_actions = @properties[:actions]
  @battle_defaults = @properties[:battle_defaults]
  @opt = opt
  @resistances = []
  @statuses = Set.new

  @properties[:statuses]&.each do |stat|
    @statuses.add(stat.to_sym)
  end

  name = case type
    when "goblin"
      RandomNameGenerator.new(RandomNameGenerator::GOBLIN).compose(1)
    when "ogre"
      %w[Guzar Irth Grukurg Zoduk].sample(1).first
    else
      type.to_s.humanize
    end
  @name = opt.fetch(:name, name)
  @entity_uid = SecureRandom.uuid
  setup_attributes
end

Instance Attribute Details

#battle_defaultsObject

Returns the value of attribute battle_defaults.



12
13
14
# File 'lib/natural_20/npc.rb', line 12

def battle_defaults
  @battle_defaults
end

#hpObject

Returns the value of attribute hp.



12
13
14
# File 'lib/natural_20/npc.rb', line 12

def hp
  @hp
end

#max_hpObject (readonly)

Returns the value of attribute max_hp.



71
72
73
# File 'lib/natural_20/npc.rb', line 71

def max_hp
  @max_hp
end

#nameObject (readonly)

Returns the value of attribute name.



55
56
57
# File 'lib/natural_20/npc.rb', line 55

def name
  @name
end

#npc_actionsObject

Returns the value of attribute npc_actions.



12
13
14
# File 'lib/natural_20/npc.rb', line 12

def npc_actions
  @npc_actions
end

#npc_typeObject

Returns the value of attribute npc_type.



12
13
14
# File 'lib/natural_20/npc.rb', line 12

def npc_type
  @npc_type
end

#resistancesObject

Returns the value of attribute resistances.



12
13
14
# File 'lib/natural_20/npc.rb', line 12

def resistances
  @resistances
end

Instance Method Details

#armor_classObject



77
78
79
# File 'lib/natural_20/npc.rb', line 77

def armor_class
  @properties[:default_ac]
end

#available_actions(session, battle, opportunity_attack: false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/natural_20/npc.rb', line 85

def available_actions(session, battle, opportunity_attack: false)
  return i[end] if unconscious?

  if opportunity_attack
    return generate_npc_attack_actions(battle, opportunity_attack: true).select do |s|
      s.action_type == :attack && s.npc_action[:type] == 'melee_attack'
    end
  end
  
  [ generate_npc_attack_actions(battle) +

  i[hide dodge look stand move dash grapple escape_grapple].map do |type|
    next unless "#{type.to_s.camelize}Action".constantize.can?(self, battle)
    case type
    when :dodge
      DodgeAction.new(session, self, :dodge)
    when :hide
      HideAction.new(session, self, :hide)
    when :disengage
      action = DisengageAction.new(session, self, :disengage)
      action
    when :move
      MoveAction.new(session, self, type)
    when :stand
      StandAction.new(session, self, type)
    when :dash
      action = DashAction.new(session, self, type)
      action
    when :help
      action = HelpAction.new(session, self, :help)
      action
    else
      Natural20::Action.new(session, self, type)
    end
  end.compact].flatten
end

#available_interactions(entity, battle) ⇒ Object



132
133
134
# File 'lib/natural_20/npc.rb', line 132

def available_interactions(entity, battle)
  []
end

#class_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/natural_20/npc.rb', line 128

def class_feature?(feature)
  @properties[:attributes]&.include?(feature)
end

#generate_npc_attack_actions(battle, opportunity_attack: false) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/natural_20/npc.rb', line 144

def generate_npc_attack_actions(battle, opportunity_attack: false)
  actions = []

  actions += npc_actions.map do |npc_action|
    next if npc_action[:ammo] && item_count(npc_action[:ammo]) <= 0
    next if npc_action[:if] && !eval_if(npc_action[:if])
    next unless AttackAction.can?(self, battle, npc_action: npc_action, opportunity_attack: opportunity_attack)

    action = AttackAction.new(session, self, :attack)

    action.npc_action = npc_action
    action
  end.compact

  actions
end

#kindObject



57
58
59
# File 'lib/natural_20/npc.rb', line 57

def kind
  @properties[:kind]
end

#melee_distanceObject



122
123
124
125
126
# File 'lib/natural_20/npc.rb', line 122

def melee_distance
  @properties[:actions].select { |a| a[:type] == "melee_attack" }.map do |action|
    action[:range]
  end&.max
end

#npc?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/natural_20/npc.rb', line 73

def npc?
  true
end

#prepared_spellsObject



140
141
142
# File 'lib/natural_20/npc.rb', line 140

def prepared_spells
  @properties.fetch(:prepared_spells, [])
end

#proficient_with_equipped_armor?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/natural_20/npc.rb', line 136

def proficient_with_equipped_armor?
  true
end

#sizeObject



61
62
63
# File 'lib/natural_20/npc.rb', line 61

def size
  @properties[:size]
end

#speedObject



81
82
83
# File 'lib/natural_20/npc.rb', line 81

def speed
  @properties[:speed]
end

#tokenObject



65
66
67
# File 'lib/natural_20/npc.rb', line 65

def token
  @properties[:token]
end