Class: Lich::Gemstone::CreatureTemplate

Inherits:
Object
  • Object
show all
Defined in:
documented/gemstone/creature.rb

Overview

Represents a template for creatures in the game.

This class is responsible for loading and managing creature templates.

Constant Summary collapse

BOON_ADJECTIVES =
%w[
  adroit afflicted apt barbed belligerent blurry canny combative dazzling deft diseased drab
  dreary ethereal flashy flexile flickering flinty frenzied ghastly ghostly gleaming glittering
  glorious glowing grotesque hardy illustrious indistinct keen lanky luminous lustrous muculent
  nebulous oozing pestilent radiant raging ready resolute robust rune-covered shadowy shifting
  shimmering shining sickly green sinuous slimy sparkling spindly spiny stalwart steadfast stout
  tattoed tenebrous tough twinkling unflinching unyielding wavering wispy
]
BOON_REGEX =

Clean creature name by removing boon adjectives Optimized to use single compiled regex instead of 50+ sequential matches

/^(#{BOON_ADJECTIVES.join('|')})\s+/i.freeze
@@templates =
{}
@@loaded =
false
@@max_templates =

Prevent unbounded template cache growth

500

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CreatureTemplate

Returns a new instance of CreatureTemplate.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'documented/gemstone/creature.rb', line 31

def initialize(data)
  @name = data[:name]
  @url = data[:url]
  @picture = data[:picture]
  @level = data[:level].to_i
  @family = data[:family]
  @type = data[:type]
  @undead = data[:undead]
  @otherclass = data[:otherclass] || []
  @areas = data[:areas] || []
  @bcs = data[:bcs]
  @max_hp = data[:max_hp]&.to_i || data[:hitpoints]&.to_i
  @speed = data[:speed]
  @height = data[:height].to_i
  @size = data[:size]

  atk = data[:attack_attributes] || {}
  @attack_attributes = OpenStruct.new(
    physical_attacks: atk[:physical_attacks] || [],
    bolt_spells: atk[:bolt_spells] || [],
    warding_spells: normalize_spells(atk[:warding_spells]),
    offensive_spells: normalize_spells(atk[:offensive_spells]),
    maneuvers: atk[:maneuvers] || [],
    special_abilities: (atk[:special_abilities] || []).map { |s| SpecialAbility.new(s) }
  )

  @defense_attributes = DefenseAttributes.new(data[:defense_attributes] || {})
  @treasure = Treasure.new(data[:treasure] || {})
  @messaging = Messaging.new(data[:messaging] || {})
  @special_other = data[:special_other]
  @abilities = data[:abilities] || []
  @alchemy = data[:alchemy] || []
end

Instance Attribute Details

#abilitiesObject (readonly)

Returns the value of attribute abilities.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def abilities
  @abilities
end

#alchemyObject (readonly)

Returns the value of attribute alchemy.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def alchemy
  @alchemy
end

#areasObject (readonly)

Returns the value of attribute areas.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def areas
  @areas
end

#attack_attributesObject (readonly)

Returns the value of attribute attack_attributes.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def attack_attributes
  @attack_attributes
end

#bcsObject (readonly)

Returns the value of attribute bcs.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def bcs
  @bcs
end

#defense_attributesObject (readonly)

Returns the value of attribute defense_attributes.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def defense_attributes
  @defense_attributes
end

#familyObject (readonly)

Returns the value of attribute family.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def family
  @family
end

#heightObject (readonly)

Returns the value of attribute height.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def height
  @height
end

#levelObject (readonly)

Returns the value of attribute level.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def level
  @level
end

#max_hpObject (readonly)

Returns the value of attribute max_hp.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def max_hp
  @max_hp
end

#messagingObject (readonly)

Returns the value of attribute messaging.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def messaging
  @messaging
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def name
  @name
end

#otherclassObject (readonly)

Returns the value of attribute otherclass.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def otherclass
  @otherclass
end

#pictureObject (readonly)

Returns the value of attribute picture.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def picture
  @picture
end

#sizeObject (readonly)

Returns the value of attribute size.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def size
  @size
end

#special_otherObject (readonly)

Returns the value of attribute special_other.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def special_other
  @special_other
end

#speedObject (readonly)

Returns the value of attribute speed.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def speed
  @speed
end

#treasureObject (readonly)

Returns the value of attribute treasure.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def treasure
  @treasure
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def type
  @type
end

#undeadObject (readonly)

Returns the value of attribute undead.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def undead
  @undead
end

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'documented/gemstone/creature.rb', line 16

def url
  @url
end

Class Method Details

.[](name) ⇒ CreatureTemplate?

Retrieves a creature template by name.

Parameters:

  • name (String)

    the name of the template to retrieve

Returns:

  • (CreatureTemplate, nil)

    the corresponding creature template or nil if not found



140
141
142
143
144
145
146
147
148
149
150
151
# File 'documented/gemstone/creature.rb', line 140

def self.[](name)
  load_all unless @@loaded
  return nil unless name

  # Try exact match first
  template = @@templates[name.downcase]
  return template if template

  # Try with boon adjectives removed
  normalized_name = fix_template_name(name)
  @@templates[normalized_name]
end

.allArray<CreatureTemplate>

Returns all loaded creature templates.

Returns:



155
156
157
158
# File 'documented/gemstone/creature.rb', line 155

def self.all
  load_all unless @@loaded
  @@templates.values.uniq
end

.fix_template_name(template_name) ⇒ String

Cleans up the template name by removing boon adjectives.

Parameters:

  • template_name (String)

    the name of the template to clean

Returns:

  • (String)

    the cleaned template name



113
114
115
116
117
# File 'documented/gemstone/creature.rb', line 113

def self.fix_template_name(template_name)
  name = template_name.dup.downcase
  name.sub!(BOON_REGEX, '')
  name.strip
end

.load_allvoid

Note:

This method will only load templates if they have not been loaded already.

This method returns an undefined value.

Loads all creature templates from the specified directory.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'documented/gemstone/creature.rb', line 68

def self.load_all
  return if @@loaded

  templates_dir = File.join(File.dirname(__FILE__), 'creatures')
  return unless File.directory?(templates_dir)

  template_count = 0
  Dir[File.join(templates_dir, '*.rb')].each do |path|
    next if File.basename(path) == '_creature_template.rb'

    # Check template limit
    if template_count >= @@max_templates
      respond "--- warning: Template cache limit (#{@@max_templates}) reached, skipping remaining templates" if $creature_debug
      break
    end

    template_name = File.basename(path, '.rb').tr('_', ' ')
    normalized_name = fix_template_name(template_name)

    begin
      # Safer loading with validation
      file_content = File.read(path)
      data = load_template_data(file_content, path)
      next unless data.is_a?(Hash)

      data[:name] = template_name
      template = new(data)
      @@templates[normalized_name] = template
      template_count += 1
    rescue => e
      respond "--- error loading template #{template_name}: #{e.message}" if $creature_debug
    end
  end

  @@loaded = true
  respond "--- loaded #{template_count} creature templates" if $creature_debug
end