Class: Lich::DragonRealms::CreatureInstance

Inherits:
Object
  • Object
show all
Includes:
Common::CreatureBase
Defined in:
documented/dragonrealms/creature.rb

Overview

A single DragonRealms creature, keyed by its server exist id.

Shares its registry, room roster and <crtrStatus> flag handling with GemStone through Common::CreatureBase; the DragonRealms-specific layer adds the assess-stream backfill (name, assess number, relation, range) and a DragonRealms valid_target? rule.

Constant Summary collapse

NAME_ARTICLE =

Leading article dropped before comparing a configured entry with #name.

/\A(?:an?|the|some)\s+/i.freeze

Constants included from Common::CreatureBase

Common::CreatureBase::ALL_CRTR_FLAGS, Common::CreatureBase::CRTR_CLASSIFICATION_FLAGS, Common::CreatureBase::CRTR_STATUS_FLAGS, Common::CreatureBase::KNOWN_FLAG_NAMES, Common::CreatureBase::STATUS_DURATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::CreatureBase

included

Constructor Details

#initialize(id, noun, name) ⇒ CreatureInstance

Returns a new instance of CreatureInstance.

Parameters:

  • id (Integer, String) —

    server creature id.

  • noun (String, nil) —

    noun, when available.

  • name (String, nil) —

    display name, when already known.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'documented/dragonrealms/creature.rb', line 95

def initialize(id, noun, name)
  @id = id.to_i
  @noun = noun
  @noun_from_server = server_noun?(noun)
  @name = name
  @created_at = Time.now
  @assess_number = nil
  @relation = nil
  @assess_status = nil
  @range = nil
  @target_id = nil
  @target = nil
  @target_number = nil
  @balance = nil
  @conditions = []
  @enriched_at = nil
  # The assess stream is the authoritative id<->name tie; once it names a
  # creature, the room-objs backfill must not overwrite it (see
  # #apply_room_name). Room-objs may still supply a name first, before any
  # assess has run.
  @name_from_assess = false
  initialize_status_tracking
end

Instance Attribute Details

#assess_number ⇒ Integer? (readonly)

Returns the assess list number (1-based), if seen.

Returns:

  • (Integer, nil) —

    the assess list number (1-based), if seen.



61
62
63
# File 'documented/dragonrealms/creature.rb', line 61

def assess_number
  @assess_number
end

#assess_status ⇒ String? (readonly)

Returns the assess parenthetical status, e.g. "cursed and solidly balanced".

Returns:

  • (String, nil) —

    the assess parenthetical status, e.g. "cursed and solidly balanced".



66
67
68
# File 'documented/dragonrealms/creature.rb', line 66

def assess_status
  @assess_status
end

#balance ⇒ String? (readonly)

Returns balance descriptor parsed from the assess parenthetical, one of DR_BALANCE_VALUES (e.g. "solidly", "off"). nil until an assess with a balance phrase arrives. See #off_balance?.

Returns:

  • (String, nil) —

    balance descriptor parsed from the assess parenthetical, one of DR_BALANCE_VALUES (e.g. "solidly", "off"). nil until an assess with a balance phrase arrives. See #off_balance?.



79
80
81
# File 'documented/dragonrealms/creature.rb', line 79

def balance
  @balance
end

#conditions ⇒ Array<String> (readonly)

Returns assess-only condition/classification words parsed from the assess parenthetical that crtrStatus does NOT carry (e.g. ["cursed"], ["friendly", "cursed"]); [] when none/unseen. crtrStatus states (prone/sleeping/stunned/immobile/hidden/...) live in crtr_flag?/ has_status?, not here. See #cursed? and #friendly?.

Returns:

  • (Array<String>) —

    assess-only condition/classification words parsed from the assess parenthetical that crtrStatus does NOT carry (e.g. ["cursed"], ["friendly", "cursed"]); [] when none/unseen. crtrStatus states (prone/sleeping/stunned/immobile/hidden/...) live in crtr_flag?/ has_status?, not here. See #cursed? and #friendly?.



85
86
87
# File 'documented/dragonrealms/creature.rb', line 85

def conditions
  @conditions
end

#created_at ⇒ Time (readonly)

Returns when this instance was first registered.

Returns:

  • (Time) —

    when this instance was first registered.



59
60
61
# File 'documented/dragonrealms/creature.rb', line 59

def created_at
  @created_at
end

#enriched_at ⇒ Time? (readonly)

Returns when assess enrichment last landed for this id; nil until first assess. See #enriched?. Assess fields (range/balance/ conditions/relation/target) are "pull" snapshots and can go stale - poll assess before relying on one.

Returns:

  • (Time, nil) —

    when assess enrichment last landed for this id; nil until first assess. See #enriched?. Assess fields (range/balance/ conditions/relation/target) are "pull" snapshots and can go stale - poll assess before relying on one.



90
91
92
# File 'documented/dragonrealms/creature.rb', line 90

def enriched_at
  @enriched_at
end

#id ⇒ Integer (readonly)

Returns server creature id.

Returns:

  • (Integer) —

    server creature id.



50
51
52
# File 'documented/dragonrealms/creature.rb', line 50

def id
  @id
end

#name ⇒ String?

Returns display name; nil until a room-objs backfill or an assess tie-in supplies one.

Returns:

  • (String, nil) —

    display name; nil until a room-objs backfill or an assess tie-in supplies one.



57
58
59
# File 'documented/dragonrealms/creature.rb', line 57

def name
  @name
end

#noun ⇒ String? (readonly)

Returns attack noun: the server's noun (a link's noun=) when the stream supplies one, otherwise the trailing word of the name; set whenever a name is set, nil until then.

Returns:

  • (String, nil) —

    attack noun: the server's noun (a link's noun=) when the stream supplies one, otherwise the trailing word of the name; set whenever a name is set, nil until then.



54
55
56
# File 'documented/dragonrealms/creature.rb', line 54

def noun
  @noun
end

#range ⇒ Symbol? (readonly)

Returns assess range bucket: :melee, :pole or :missile.

Returns:

  • (Symbol, nil) —

    assess range bucket: :melee, :pole or :missile.



68
69
70
# File 'documented/dragonrealms/creature.rb', line 68

def range
  @range
end

#relation ⇒ String? (readonly)

Returns the assess relation phrase, e.g. "behind you".

Returns:

  • (String, nil) —

    the assess relation phrase, e.g. "behind you".



63
64
65
# File 'documented/dragonrealms/creature.rb', line 63

def relation
  @relation
end

#target ⇒ String? (readonly)

Returns name of the creature/PC this one is engaging, from assess (e.g. "you", "Holdigor", "a plague spawn").

Returns:

  • (String, nil) —

    name of the creature/PC this one is engaging, from assess (e.g. "you", "Holdigor", "a plague spawn").



73
74
75
# File 'documented/dragonrealms/creature.rb', line 73

def target
  @target
end

#target_id ⇒ String? (readonly)

Returns id of the creature/PC this one is engaging, from assess.

Returns:

  • (String, nil) —

    id of the creature/PC this one is engaging, from assess.



70
71
72
# File 'documented/dragonrealms/creature.rb', line 70

def target_id
  @target_id
end

#target_number ⇒ Integer? (readonly)

Returns the engaged target's assess list number, if any.

Returns:

  • (Integer, nil) —

    the engaged target's assess list number, if any.



75
76
77
# File 'documented/dragonrealms/creature.rb', line 75

def target_number
  @target_number
end

Class Method Details

.balance_pattern ⇒ Regexp

Matches a DR_BALANCE_VALUES descriptor followed by the balance word in an assess parenthetical, e.g. "solidly balanced", "off balance", and the worst levels' "imbalanced" phrasing ("extremely imbalanced"). Built lazily so DR_BALANCE_VALUES (loaded with drvariables) is present by first use; union order (longest multi-word values first in the array) resolves "somewhat off" ahead of "off".

Returns:

  • (Regexp)


288
289
290
# File 'documented/dragonrealms/creature.rb', line 288

def self.balance_pattern
  @balance_pattern ||= /\b(#{Regexp.union(DR_BALANCE_VALUES)})\s+(?:im)?balanced?\b/i
end

Instance Method Details

#apply_room_name(name, noun = nil) ⇒ void

This method returns an undefined value.

Applies a name learned from the room-objs stream, with its attack noun.

Two producers call this, both via the XML parser: the stream-order backfill that pairs the Nth bold room-objs name with the Nth <crtrStatus> id (legacy dialect), and the linked room-objs creature (<a exist noun>) of the Saga-identity dialect, named with the article that precedes the link ("a jeol moradu"). The linked creature passes the link's noun=, which is the server's noun and wins: it need not be the last word of the name ("a sleek golden panther with enigmatic violet eyes" has noun "panther"). Without one (legacy) the noun is derived from the name's last word. The assess stream is the authoritative id<->name tie over the legacy positional pairing, so once #feed_assess has named the creature a name without a link noun is ignored. A linked name is tied to its id by the link itself, so it replaces an assess name: server ids are recycled, and an id reused for another creature must not keep the old creature's assessed name. A nil name (e.g. an out-of-range positional lookup, the count-guard) is ignored, leaving any existing name intact.

Parameters:

  • name (String, nil) —

    display name from the room-objs stream.

  • noun (String, nil) (defaults to: nil) —

    the server's noun (the link's noun=); nil or blank derives it from the name. Surrounding whitespace is trimmed.



179
180
181
182
183
184
185
186
# File 'documented/dragonrealms/creature.rb', line 179

def apply_room_name(name, noun = nil)
  return if name.nil?
  return if @name_from_assess && !server_noun?(noun)

  @name = name
  @noun = resolve_noun(noun, name)
  @noun_from_server = server_noun?(noun)
end

#condition?(name) ⇒ Boolean

Whether a given assess affliction is present (e.g. "cursed", "poisoned").

Parameters:

Returns:

  • (Boolean)


222
223
224
# File 'documented/dragonrealms/creature.rb', line 222

def condition?(name)
  @conditions.include?(name.to_s)
end

#cursed? ⇒ Boolean

Convenience for the "cursed" affliction; use #condition? for others.

Returns:

  • (Boolean)


229
230
231
# File 'documented/dragonrealms/creature.rb', line 229

def cursed?
  condition?('cursed')
end

#enriched? ⇒ Boolean

Whether any assess enrichment (range/balance/conditions/relation/target) has landed for this id. These are "pull" snapshots -- poll assess before relying on them, and see #enriched_at for staleness.

Returns:

  • (Boolean)


203
204
205
# File 'documented/dragonrealms/creature.rb', line 203

def enriched?
  !@enriched_at.nil?
end

#feed_assess(entry) ⇒ void

This method returns an undefined value.

Backfills name and positional data from a parsed assess entry.

<crtrStatus> registers a creature id-first with no name, so this is how DragonRealms learns which name/relation/range belongs to that id. The assess feed capitalizes the subject ("A jeol moradu"); the name is downcased to match the room-objs vocabulary DragonRealms scripts expect. The noun is the entry's :noun (the server noun from the subject's <a exist> link, Saga-identity stream) when present, so a long name such as "a sleek golden panther with enigmatic violet eyes" keeps noun "panther"; link-less (legacy) entries derive it from the name. A server noun keeps the server's case and is not downcased with the name, so it matches the noun the linked room-objs path stores (a link noun="Warden" stays "Warden"); a derived noun comes from the downcased name and is lowercase.

Parameters:

  • entry (Hash) —

    an entry from XMLData.parse_assess_line, with keys :name, :noun, :number, :status, :relation, :range, :target_id.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'documented/dragonrealms/creature.rb', line 137

def feed_assess(entry)
  if entry[:name]
    @name = entry[:name].to_s.downcase
    @noun = resolve_noun(entry[:noun], @name)
    @noun_from_server = server_noun?(entry[:noun])
    # Authoritative from here on: subsequent room-objs backfills no-op.
    @name_from_assess = true
  end
  @assess_number = entry[:number]
  @assess_status = entry[:status]
  @balance, @conditions = parse_assess_status(entry[:status])
  @relation = entry[:relation]
  @range = entry[:range]
  @target_id = entry[:target_id]
  @target = entry[:target]
  @target_number = entry[:target_number]
  @enriched_at = Time.now
end

#friendly? ⇒ Boolean

True while assess reports this creature as "friendly" - i.e. it has been empathically manipulated (an Empath ability: "manipulate ... to consider you friend, not foe") into treating you as a friend.

This is a TEMPORARY, manipulated state, NOT a permanent classification: crtrStatus keeps reporting hostile="1" throughout, and it wears off. Confirmed from logs - one jeol moradu (exist 105829093) went hostile/flanking-you -> manipulated "friendly" (and turned on its own kind) while crtrStatus stayed hostile the whole time. Assess-only, so it is a staleable pull field: re-assess before relying on it, and do not treat it as a durable friend/foe flag (a summon/pet is a different thing).

Returns:

  • (Boolean)


246
247
248
# File 'documented/dragonrealms/creature.rb', line 246

def friendly?
  condition?('friendly')
end

#matches?(entry) ⇒ Boolean

Whether a configured entry, such as an ignored_npcs item, names this creature. Case-insensitive, and a leading article on the entry is ignored. An entry matches when it is:

  • the game's noun ("leopard"), from the stream's creature link;
  • the whole name ("spectral white leopard with luminous blue eyes");
  • a phrase of two or more words in the name ("spectral white leopard").

A single word matches the game's noun only, never another word of the name, so "eyes" does not name every creature "with ... eyes". A noun Lich derived from the name (legacy stream, no link) is not used: it is the name's last word, which can be a word of a "with ..." clause. DRRoom.unignored_npcs still drops plain nouns by its own noun rule.

Parameters:

Returns:

  • (Boolean)


268
269
270
271
272
273
274
275
276
277
278
# File 'documented/dragonrealms/creature.rb', line 268

def matches?(entry)
  wanted = entry.to_s.strip.downcase.squeeze(' ').sub(NAME_ARTICLE, '')
  return false if wanted.empty?
  return true if @noun_from_server && @noun.downcase == wanted

  full = @name.to_s.strip.downcase.squeeze(' ').sub(NAME_ARTICLE, '')
  return false if full.empty?
  return true if full == wanted

  wanted.include?(' ') && " #{full} ".include?(" #{wanted} ")
end

#off_balance? ⇒ Boolean

Whether the creature is below "solidly balanced" per the last assess (a softer target). False when balance is unknown.

Returns:

  • (Boolean)


211
212
213
214
215
216
# File 'documented/dragonrealms/creature.rb', line 211

def off_balance?
  return false unless @balance

  idx = DR_BALANCE_VALUES.index(@balance)
  !idx.nil? && idx < DR_BALANCE_VALUES.index('solidly')
end

#valid_target? ⇒ Boolean

Whether this creature should be considered attackable.

DragonRealms has no UCS or appendage decoys to exclude, so the rule is simply "not confirmed dead by <crtrStatus>".

Returns:

  • (Boolean)


194
195
196
# File 'documented/dragonrealms/creature.rb', line 194

def valid_target?
  !crtr_flag?(:dead)
end