Module: Lich::Gemstone::Combat::Parser

Defined in:
documented/gemstone/combat/parser.rb

Overview

Combat event parser

Parses game lines to extract combat events including attacks, damage, status effects, and UCS (Unarmed Combat System) data. Uses lazy-loaded pattern matching for performance.

Examples:

Parse an attack

attack = Parser.parse_attack(line)
# => { name: :swing, target: { id: 1234, noun: 'troll', name: 'troll' }, damaging: true }

Parse damage

damage = Parser.parse_damage(line)  # => 50

Constant Summary collapse

/<a exist="(?<id>[^"]+)" noun="(?<noun>[^"]+)">(?<name>[^<]+)<\/a>/i.freeze
BOLD_WRAPPER_PATTERN =

Bold tag pattern - creatures are wrapped in bold tags Non-greedy match to avoid spanning multiple creatures

/<pushBold\/>([^<]+<a exist="[^"]+"[^>]+>[^<]+<\/a>)<popBold\/>/i.freeze

Class Method Summary collapse

Class Method Details

.extract_creature_target(line) ⇒ Hash?

Extract creature target from game line

Creatures must be wrapped in bold tags (<pushBold/> … <popBold/>) and contain a valid creature link with ID.

Parameters:

  • line (String)

    Game line to parse

Returns:

  • (Hash, nil)

    Target data with :id, :noun, :name or nil



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'documented/gemstone/combat/parser.rb', line 111

def extract_creature_target(line)
  # Check if line contains a bolded link
  bold_match = BOLD_WRAPPER_PATTERN.match(line)
  return nil unless bold_match

  # Extract the link from within the bold tags
  link_text = bold_match[1]
  link_match = TARGET_LINK_PATTERN.match(link_text)
  return nil unless link_match

  id = link_match[:id].to_i
  return nil if id <= 0 # Skip invalid IDs

  {
    id: id,
    noun: link_match[:noun],
    name: link_match[:name]
  }
end

.extract_target_from_line(line) ⇒ Object



152
153
154
155
156
# File 'documented/gemstone/combat/parser.rb', line 152

def extract_target_from_line(line)
  # ONLY accept bolded creatures as targets
  # Non-bolded links are equipment, objects, or other non-combatants
  extract_creature_target(line)
end

.extract_target_from_match(match) ⇒ Object

Try to extract target from regex match first, then from line



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'documented/gemstone/combat/parser.rb', line 132

def extract_target_from_match(match)
  return nil unless match.names.include?('target')
  target_text = match[:target]
  return nil if target_text.nil? || target_text.strip.empty?

  # Look for creature in target text
  if (target_match = TARGET_LINK_PATTERN.match(target_text))
    id = target_match[:id].to_i
    return nil if id < 0

    return {
      id: id,
      noun: target_match[:noun],
      name: target_match[:name]
    }
  end

  nil
end

.parse_attack(line) ⇒ Hash?

Parse attack initiation from a game line

Identifies the type of attack and extracts target information.

Parameters:

  • line (String)

    Game line to parse

  • return (Hash)

    a customizable set of options

Returns:

  • (Hash, nil)

    Attack data with :name, :target, :damaging or nil if no attack



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'documented/gemstone/combat/parser.rb', line 47

def parse_attack(line)
  return nil unless attack_detector.match?(line)

  attack_lookup.each do |pattern, name|
    if (match = pattern.match(line))
      target_info = extract_target_from_match(match) || extract_target_from_line(line)
      return {
        name: name,
        target: target_info || {},
        damaging: true
      }
    end
  end
  nil
end

.parse_damage(line) ⇒ Integer?

Parse damage amount from a game line

Parameters:

  • line (String)

    Game line to parse

Returns:

  • (Integer, nil)

    Damage points or nil if no damage found



67
68
69
70
# File 'documented/gemstone/combat/parser.rb', line 67

def parse_damage(line)
  result = Definitions::Damage.parse(line)
  result ? result[:damage] : nil
end

.parse_status(line) ⇒ Hash?

Parse status effects from a game line

Only active if track_statuses setting is enabled.

Parameters:

  • line (String)

    Game line to parse

  • return (Hash)

    a customizable set of options

Returns:

  • (Hash, nil)

    Status data with :status, :target, :action or nil



81
82
83
84
85
86
# File 'documented/gemstone/combat/parser.rb', line 81

def parse_status(line)
  return nil unless Tracker.settings[:track_statuses]

  # Return the full result including action field
  Definitions::Statuses.parse(line)
end

.parse_ucs(line) ⇒ Hash?

Parse UCS (Unarmed Combat System) events

Detects position changes, tierup vulnerabilities, and smite effects. Only active if track_ucs setting is enabled.

Parameters:

  • line (String)

    Game line to parse

  • return (Hash)

    a customizable set of options

Returns:

  • (Hash, nil)

    UCS event data or nil



98
99
100
101
102
# File 'documented/gemstone/combat/parser.rb', line 98

def parse_ucs(line)
  return nil unless Tracker.settings[:track_ucs]

  Definitions::UCS.parse(line)
end