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.

Examples:

Loading all creature templates

Lich::Gemstone::CreatureTemplate.load_all

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.



30
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
# File 'documented/gemstone/creature.rb', line 30

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.



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

def abilities
  @abilities
end

#alchemyObject (readonly)

Returns the value of attribute alchemy.



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

def alchemy
  @alchemy
end

#areasObject (readonly)

Returns the value of attribute areas.



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

def areas
  @areas
end

#attack_attributesObject (readonly)

Returns the value of attribute attack_attributes.



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

def attack_attributes
  @attack_attributes
end

#bcsObject (readonly)

Returns the value of attribute bcs.



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

def bcs
  @bcs
end

#defense_attributesObject (readonly)

Returns the value of attribute defense_attributes.



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

def defense_attributes
  @defense_attributes
end

#familyObject (readonly)

Returns the value of attribute family.



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

def family
  @family
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#max_hpObject (readonly)

Returns the value of attribute max_hp.



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

def max_hp
  @max_hp
end

#messagingObject (readonly)

Returns the value of attribute messaging.



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

def messaging
  @messaging
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#otherclassObject (readonly)

Returns the value of attribute otherclass.



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

def otherclass
  @otherclass
end

#pictureObject (readonly)

Returns the value of attribute picture.



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

def picture
  @picture
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#special_otherObject (readonly)

Returns the value of attribute special_other.



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

def special_other
  @special_other
end

#speedObject (readonly)

Returns the value of attribute speed.



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

def speed
  @speed
end

#treasureObject (readonly)

Returns the value of attribute treasure.



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

def treasure
  @treasure
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#undeadObject (readonly)

Returns the value of attribute undead.



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

def undead
  @undead
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.[](name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'documented/gemstone/creature.rb', line 136

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:



151
152
153
154
# File 'documented/gemstone/creature.rb', line 151

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.



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

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

.load_allvoid

This method returns an undefined value.

Loads all creature templates from the specified directory. This method will only load templates if they havenโ€™t been loaded already.



67
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
# File 'documented/gemstone/creature.rb', line 67

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