Class: Lich::Gemstone::Messaging

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

Overview

Combat and encounter messaging for a creature template.

Stores game lines for creature actions (attacks, spells, flight, death, etc.), with support for templated text and placeholders. Creature messages can be rendered with specific values (e.g., different pronouns or directions) or matched against game output to recognize actions.

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.



891
892
893
894
895
# File 'documented/gemstone/creature.rb', line 891

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.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def ambient
  @ambient
end

#arrival ⇒ Object

Returns the value of attribute arrival.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def arrival
  @arrival
end

#attack ⇒ Object

Returns the value of attribute attack.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def attack
  @attack
end

#attacks ⇒ Object

Returns the value of attribute attacks.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def attacks
  @attacks
end

#bite ⇒ Object

Returns the value of attribute bite.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def bite
  @bite
end

#claw ⇒ Object

Returns the value of attribute claw.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def claw
  @claw
end

#death ⇒ Object

Returns the value of attribute death.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def death
  @death
end

#decay ⇒ Object

Returns the value of attribute decay.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def decay
  @decay
end

#description ⇒ Object

Returns the value of attribute description.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def description
  @description
end

#enrage ⇒ Object

Returns the value of attribute enrage.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def enrage
  @enrage
end

#flee ⇒ Object

Returns the value of attribute flee.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def flee
  @flee
end

#frenzy ⇒ Object

Returns the value of attribute frenzy.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def frenzy
  @frenzy
end

#info ⇒ Object

Returns the value of attribute info.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def info
  @info
end

#mstrike ⇒ Object

Returns the value of attribute mstrike.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def mstrike
  @mstrike
end

#search ⇒ Object

Returns the value of attribute search.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def search
  @search
end

#spell_prep ⇒ Object

Returns the value of attribute spell_prep.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def spell_prep
  @spell_prep
end

#stand ⇒ Object

Returns the value of attribute stand.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def stand
  @stand
end

#stun_break ⇒ Object

Returns the value of attribute stun_break.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def stun_break
  @stun_break
end

#sympathy ⇒ Object

Returns the value of attribute sympathy.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def sympathy
  @sympathy
end

#triggers ⇒ Object

Returns the value of attribute triggers.



872
873
874
# File 'documented/gemstone/creature.rb', line 872

def triggers
  @triggers
end

Instance Method Details

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

Renders a message field with optional placeholder substitutions.

Looks up the named messaging field and renders any PlaceholderTemplates it contains with the provided substitutions (e.g., pronouns, directions). For array fields, renders each variant and joins with newlines. Falls back to sampling from PLACEHOLDER_MAP if a substitution is not provided.

Examples:

messaging.display(:arrival, {pronoun: 'she', direction: 'north'})

Parameters:

  • field (Symbol, String) —

    the messaging field name (e.g., :arrival, :death)

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

    optional placeholder values (e.g., 'he')

Returns:

  • (String) —

    the rendered message



938
939
940
941
942
943
944
945
946
947
# File 'documented/gemstone/creature.rb', line 938

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.



953
954
955
956
957
958
959
# File 'documented/gemstone/creature.rb', line 953

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) ⇒ Hash, ...

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 raw message data into displayable and matchable objects.

Recursively processes hashes (transforms values), arrays (maps over elements), and strings. Any string containing placeholders is wrapped in a PlaceholderTemplate with extracted placeholder names and their allowed values from PLACEHOLDER_MAP. Strings without placeholders and other types pass through unchanged.

Parameters:

  • value (Hash, Array, String, Object) —

    message data to normalize

Returns:



908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
# File 'documented/gemstone/creature.rb', line 908

def normalize(value)
  if value.is_a?(Hash)
    # attacks: and triggers: are keyed hashes of message lists. Without
    # this branch their strings stayed raw, so a {pronoun} in an attack
    # or trigger line never became a matchable PlaceholderTemplate the
    # way the flat fields' did.
    value.transform_values { |v| normalize(v) }
  elsif 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