Class: Lich::Gemstone::Messaging

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

Overview

Creature combat and ambient message templates with placeholder substitution.

Stores creature-specific combat messages (attacks, spells, special abilities) and ambient messages, with placeholders (Pronoun, direction, etc.) that can be matched against actual game output or rendered with substitutions. Arrays of message variants (common for e.g. attack messages) are merged and matched against any variant; PlaceholderTemplate objects handle regex generation and matching for templated variants.

Constant Summary collapse

PLACEHOLDER_MAP =

Every form a placeholder can take in a real game line. The lists are alternatives in the generated regex, so a form that is missing here makes an otherwise-correct message unmatchable - "its" and "their" (possessives) and "out" (a flee direction) were absent.

{
  Pronoun: %w[He She It His Her Its Their Him Them Himself Herself Itself Themselves],
  pronoun: %w[he she it his her its their him them himself herself itself themselves],
  Reflexive: %w[Himself Herself Itself Themselves],
  reflexive: %w[himself herself itself themselves],
  direction: %w[north south east west up down out northeast northwest southeast southwest],
  weapon: %w[RAW:.+?]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Messaging

Returns a new instance of Messaging.



941
942
943
944
945
# File 'documented/gemstone/creature.rb', line 941

def initialize(data)
  data.each do |key, value|
    instance_variable_set("@#{key}", normalize(value))
  end
end

Instance Attribute Details

#ambient ⇒ Object

Returns the value of attribute ambient.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def ambient
  @ambient
end

#arrival ⇒ Object

Returns the value of attribute arrival.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def arrival
  @arrival
end

#attack ⇒ Object

Returns the value of attribute attack.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def attack
  @attack
end

#attacks ⇒ Object

Returns the value of attribute attacks.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def attacks
  @attacks
end

#bite ⇒ Object

Returns the value of attribute bite.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def bite
  @bite
end

#claw ⇒ Object

Returns the value of attribute claw.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def claw
  @claw
end

#death ⇒ Object

Returns the value of attribute death.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def death
  @death
end

#decay ⇒ Object

Returns the value of attribute decay.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def decay
  @decay
end

#description ⇒ Object

Returns the value of attribute description.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def description
  @description
end

#enrage ⇒ Object

Returns the value of attribute enrage.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def enrage
  @enrage
end

#flee ⇒ Object

Returns the value of attribute flee.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def flee
  @flee
end

#frenzy ⇒ Object

Returns the value of attribute frenzy.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def frenzy
  @frenzy
end

#mstrike ⇒ Object

Returns the value of attribute mstrike.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def mstrike
  @mstrike
end

#search ⇒ Object

Returns the value of attribute search.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def search
  @search
end

#spell_prep ⇒ Object

Returns the value of attribute spell_prep.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def spell_prep
  @spell_prep
end

#stand ⇒ Object

Returns the value of attribute stand.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def stand
  @stand
end

#stun_break ⇒ Object

Returns the value of attribute stun_break.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def stun_break
  @stun_break
end

#sympathy ⇒ Object

Returns the value of attribute sympathy.



922
923
924
# File 'documented/gemstone/creature.rb', line 922

def sympathy
  @sympathy
end

Instance Method Details

#display(field, subs = {}) ⇒ String

Renders a message field with optional placeholder substitutions.

Fetches a message by field name and renders it. PlaceholderTemplate values are rendered with substitutions; arrays join with newlines. Returns the rendered string (including nil if the field does not exist).

Examples:

messaging.display(:attack, {pronoun: "he"}) #=> "The orc swings his sword."

Parameters:

  • field (Symbol, String) —

    the message field to render (e.g. :attack, :flee)

  • subs (Hash) (defaults to: {}) —

    substitutions for placeholders ("he", ...)

Returns:

  • (String) —

    the rendered message, with placeholders replaced



979
980
981
982
983
984
985
986
987
988
# File 'documented/gemstone/creature.rb', line 979

def display(field, subs = {})
  msg = send(field)
  if msg.is_a?(Array)
    msg.map { |m| m.is_a?(PlaceholderTemplate) ? m.to_display(subs) : m }.join("\n")
  elsif msg.is_a?(PlaceholderTemplate)
    msg.to_display(subs)
  else
    msg
  end
end

#match(field, str) ⇒ Object

Returns the placeholder captures ({} for a literal hit) when str is one of the field's messages, else nil. Arrays are the common case - most creatures have several variants of a message - so a match against any variant counts.



994
995
996
997
998
999
1000
# File 'documented/gemstone/creature.rb', line 994

def match(field, str)
  Array(send(field)).each do |msg|
    hit = msg.is_a?(PlaceholderTemplate) ? msg.match(str) : (msg == str ? {} : nil)
    return hit if hit
  end
  nil
end

#normalize(value) ⇒ String, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a raw message value into a normalized form.

Recursively processes arrays. Strings containing ... placeholders are wrapped in PlaceholderTemplate for later regex matching and substitution; other values pass through unchanged.

Parameters:

  • value (String, Array, nil) —

    a raw message, array of messages, or nil

Returns:



956
957
958
959
960
961
962
963
964
965
966
# File 'documented/gemstone/creature.rb', line 956

def normalize(value)
  if value.is_a?(Array)
    value.map { |v| normalize(v) }
  elsif value.is_a?(String) && value.match?(/\{[a-zA-Z_]+\}/)
    phs = value.scan(/\{([a-zA-Z_]+)\}/).flatten.map(&:to_sym)
    placeholders = phs.map { |ph| [ph, PLACEHOLDER_MAP[ph] || []] }.to_h
    PlaceholderTemplate.new(value, placeholders)
  else
    value
  end
end