Class: Lich::Gemstone::Messaging

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

Overview

Creature messaging templates for contextual creature descriptions.

Stores and renders dynamic messaging for creatures: arrival/death text, spell casting, combat actions, and creature sounds. Supports placeholder substitution (pronouns, directions, weapon types) and random variation via arrays of alternatives.

Constant Summary collapse

PLACEHOLDER_MAP =

Placeholder substitution options for message templates.

Maps placeholder names (used in curly braces within messaging templates) to their allowed values. Capitalized variants (e.g. Pronoun) use title case; lowercase variants use lowercase. RAW: values are regex patterns matched without escaping.

Examples:

# Template: "The {Pronoun} swings a {weapon}"
# Placeholders:
#   Pronoun -> randomly one of ["He", "She", "It", ...]
#   weapon -> RAW regex pattern for any weapon name

See Also:

  • #normalize
{
  Pronoun: %w[He Her His It She],
  pronoun: %w[he her his it she],
  direction: %w[north south east west up down 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.



701
702
703
704
705
# File 'documented/gemstone/creature.rb', line 701

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

Instance Attribute Details

#arrivalObject

Returns the value of attribute arrival.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def arrival
  @arrival
end

#attackObject

Returns the value of attribute attack.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def attack
  @attack
end

#biteObject

Returns the value of attribute bite.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def bite
  @bite
end

#clawObject

Returns the value of attribute claw.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def claw
  @claw
end

#deathObject

Returns the value of attribute death.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def death
  @death
end

#descriptionObject

Returns the value of attribute description.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def description
  @description
end

#enrageObject

Returns the value of attribute enrage.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def enrage
  @enrage
end

#fleeObject

Returns the value of attribute flee.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def flee
  @flee
end

#frenzyObject

Returns the value of attribute frenzy.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def frenzy
  @frenzy
end

#mstrikeObject

Returns the value of attribute mstrike.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def mstrike
  @mstrike
end

#spell_prepObject

Returns the value of attribute spell_prep.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def spell_prep
  @spell_prep
end

#sympathyObject

Returns the value of attribute sympathy.



677
678
679
# File 'documented/gemstone/creature.rb', line 677

def sympathy
  @sympathy
end

Instance Method Details

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

Renders a messaging field with optional placeholder substitutions.

Retrieves the named field (e.g. :description, :arrival) and renders it: if it is a PlaceholderTemplate, substitutes placeholders (falling back to random options); if it is an array, joins each rendered element with newlines; otherwise returns the plain value.

Examples:

msg = Messaging.new(description: "{pronoun} has red skin")
msg.display(:description, pronoun: "She") #=> "She has red skin"

Parameters:

  • field (Symbol)

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

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

    optional placeholder -> value substitutions, e.g. { pronoun: "He", direction: "north" }

Returns:

  • (String, Object)

    rendered message or plain value



742
743
744
745
746
747
748
749
750
751
# File 'documented/gemstone/creature.rb', line 742

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) ⇒ Hash?

Matches a messaging field against a string and returns captured groups.

If the field is a PlaceholderTemplate, converts it to a regex and matches; returns captured placeholders as a hash, or nil if no match. For non-template fields, returns an empty hash if the field equals the string, nil otherwise.

Examples:

msg = Messaging.new(arrival: "A {noun} arrives from the {direction}")
msg.match(:arrival, "A goblin arrives from the north")
#=> { noun: "goblin", direction: "north" }

Parameters:

  • field (Symbol)

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

  • str (String)

    the string to match

Returns:

  • (Hash, nil)

    captured placeholder values, empty hash if exact match, nil if no match



767
768
769
770
771
772
773
774
# File 'documented/gemstone/creature.rb', line 767

def match(field, str)
  msg = send(field)
  if msg.is_a?(PlaceholderTemplate)
    msg.match(str)
  else
    msg == str ? {} : nil
  end
end

#normalize(value) ⇒ PlaceholderTemplate, ...

Recursively normalizes a messaging value into a renderable form.

Detects and converts placeholder templates (strings containing name patterns) into PlaceholderTemplate objects. Arrays are recursively normalized. Plain strings and other values pass through unchanged.

Parameters:

  • value (String, Array, Object)

    the raw value from template data

Returns:

See Also:



716
717
718
719
720
721
722
723
724
725
726
# File 'documented/gemstone/creature.rb', line 716

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