Class: Lich::Gemstone::CreatureTemplate
- Inherits:
-
Object
- Object
- Lich::Gemstone::CreatureTemplate
- 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
-
#abilities ⇒ Object
readonly
Returns the value of attribute abilities.
-
#alchemy ⇒ Object
readonly
Returns the value of attribute alchemy.
-
#areas ⇒ Object
readonly
Returns the value of attribute areas.
-
#attack_attributes ⇒ Object
readonly
Returns the value of attribute attack_attributes.
-
#bcs ⇒ Object
readonly
Returns the value of attribute bcs.
-
#defense_attributes ⇒ Object
readonly
Returns the value of attribute defense_attributes.
-
#family ⇒ Object
readonly
Returns the value of attribute family.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#max_hp ⇒ Object
readonly
Returns the value of attribute max_hp.
-
#messaging ⇒ Object
readonly
Returns the value of attribute messaging.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#otherclass ⇒ Object
readonly
Returns the value of attribute otherclass.
-
#picture ⇒ Object
readonly
Returns the value of attribute picture.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#special_other ⇒ Object
readonly
Returns the value of attribute special_other.
-
#speed ⇒ Object
readonly
Returns the value of attribute speed.
-
#treasure ⇒ Object
readonly
Returns the value of attribute treasure.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#undead ⇒ Object
readonly
Returns the value of attribute undead.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .[](name) ⇒ Object
-
.all ⇒ Array<CreatureTemplate>
Returns all loaded creature templates.
-
.fix_template_name(template_name) ⇒ String
Cleans up the template name by removing boon adjectives.
-
.load_all ⇒ void
Loads all creature templates from the specified directory.
Instance Method Summary collapse
-
#initialize(data) ⇒ CreatureTemplate
constructor
A new instance of CreatureTemplate.
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
#abilities ⇒ Object (readonly)
Returns the value of attribute abilities.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def abilities @abilities end |
#alchemy ⇒ Object (readonly)
Returns the value of attribute alchemy.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def alchemy @alchemy end |
#areas ⇒ Object (readonly)
Returns the value of attribute areas.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def areas @areas end |
#attack_attributes ⇒ Object (readonly)
Returns the value of attribute attack_attributes.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def attack_attributes @attack_attributes end |
#bcs ⇒ Object (readonly)
Returns the value of attribute bcs.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def bcs @bcs end |
#defense_attributes ⇒ Object (readonly)
Returns the value of attribute defense_attributes.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def defense_attributes @defense_attributes end |
#family ⇒ Object (readonly)
Returns the value of attribute family.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def family @family end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def height @height end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def level @level end |
#max_hp ⇒ Object (readonly)
Returns the value of attribute max_hp.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def max_hp @max_hp end |
#messaging ⇒ Object (readonly)
Returns the value of attribute messaging.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def messaging @messaging end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def name @name end |
#otherclass ⇒ Object (readonly)
Returns the value of attribute otherclass.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def otherclass @otherclass end |
#picture ⇒ Object (readonly)
Returns the value of attribute picture.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def picture @picture end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def size @size end |
#special_other ⇒ Object (readonly)
Returns the value of attribute special_other.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def special_other @special_other end |
#speed ⇒ Object (readonly)
Returns the value of attribute speed.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def speed @speed end |
#treasure ⇒ Object (readonly)
Returns the value of attribute treasure.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def treasure @treasure end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def type @type end |
#undead ⇒ Object (readonly)
Returns the value of attribute undead.
15 16 17 |
# File 'documented/gemstone/creature.rb', line 15 def undead @undead end |
#url ⇒ Object (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 |
.all ⇒ Array<CreatureTemplate>
Returns all loaded creature templates.
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.
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_all ⇒ void
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.}" if $creature_debug end end @@loaded = true respond "--- loaded #{template_count} creature templates" if $creature_debug end |