Module: Lich::Common::CreatureBase::ClassMethods Private

Defined in:
documented/common/creature/creature_base.rb

Overview

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

Class-level behaviour: the id-keyed instance registry, the current-room roster and the target/room queries shared by both games' Creature facades.

State lives in per-class instance variables (@instances, @current_room_ids, ...), so each extending class keeps an independent registry - GemStone and DragonRealms never share a roster even though they share this code.

Constant Summary collapse

HOUSEKEEPING_INTERVAL =

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

Seconds between wall-clock housekeeping passes from #register.

60

Instance Method Summary collapse

Instance Method Details

#[](id) ⇒ Object?

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.

Looks up a registered instance by id.

Parameters:

  • id (Integer, String) —

    server creature id.

Returns:

  • (Object, nil)


296
297
298
# File 'documented/common/creature/creature_base.rb', line 296

def [](id)
  instances[id.to_i]
end

#all ⇒ Array<Object>

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.

Returns every registered instance.

Returns:

  • (Array<Object>) —

    every registered instance.



309
310
311
# File 'documented/common/creature/creature_base.rb', line 309

def all
  instances.values
end

#auto_register? ⇒ Boolean

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.

Returns whether auto-registration is enabled (default true).

Returns:

  • (Boolean) —

    whether auto-registration is enabled (default true).



436
437
438
439
# File 'documented/common/creature/creature_base.rb', line 436

def auto_register?
  @auto_register = true if @auto_register.nil?
  @auto_register
end

#cleanup_max_age ⇒ Integer

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.

Returns seconds unseen before housekeeping removes a creature (default 600).

Returns:

  • (Integer) —

    seconds unseen before housekeeping removes a creature (default 600).



431
432
433
# File 'documented/common/creature/creature_base.rb', line 431

def cleanup_max_age
  @cleanup_max_age ||= 600
end

#cleanup_old(max_age_seconds = cleanup_max_age) ⇒ Integer

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.

Removes instances not seen within the given age.

Ages against last_seen_at, not creation time, so a creature that has been in a long fight is not discarded mid-fight and re-registered as a blank stranger. Creatures in the current or immediately previous room roster are never removed (#sheltered_ids).

Parameters:

  • max_age_seconds (Integer) (defaults to: cleanup_max_age) —

    age cutoff in seconds.

Returns:

  • (Integer) —

    number of instances removed.



331
332
333
334
335
336
337
# File 'documented/common/creature/creature_base.rb', line 331

def cleanup_old(max_age_seconds = cleanup_max_age)
  cutoff = Time.now - max_age_seconds
  shelter = sheltered_ids
  before = instances.size
  instances.reject! { |id, instance| instance.last_seen_at < cutoff && !shelter.include?(id) }
  before - instances.size
end

#clear ⇒ void

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.

This method returns an undefined value.

Clears all instances and the room roster (session reset).



316
317
318
319
320
# File 'documented/common/creature/creature_base.rb', line 316

def clear
  instances.clear
  clear_room
  @previous_room_ids = []
end

#clear_room ⇒ void

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.

This method returns an undefined value.

Empties the current-room roster (the persistent registry is untouched).

Called from the XML parser's nav and room-object refresh hooks; the roster is then rebuilt from fresh room XML.



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

def clear_room
  count = room_roster.size
  # Keep the outgoing roster as a shield: the parser rebuilds the new
  # one a creature at a time, so a creature late in the refresh is
  # briefly absent from the live roster. Housekeeping must not sweep
  # it in that window (see {#sheltered_ids}).
  @previous_room_ids = room_roster
  @current_room_ids = []
  respond "--- room: roster cleared (#{count} creature#{'s' unless count == 1})" if $creature_debug && count > 0
end

#configure(max_size: 1000, auto_register: true, cleanup_max_age: 600) ⇒ void

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.

This method returns an undefined value.

Configures registry limits.

Every call resets omitted options to their defaults - max_size to 1000, auto_register to true, cleanup_max_age to 600 - so calling this with no arguments restores all defaults. Facade callers (e.g. Creature.configure(**options)) should pass every option they mean to keep.

Parameters:

  • max_size (Integer) (defaults to: 1000) —

    maximum number of retained instances.

  • auto_register (Boolean) (defaults to: true) —

    whether #register creates instances.

  • cleanup_max_age (Integer) (defaults to: 600) —

    seconds a creature may go unseen before housekeeping removes it.



424
425
426
427
428
# File 'documented/common/creature/creature_base.rb', line 424

def configure(max_size: 1000, auto_register: true, cleanup_max_age: 600)
  @max_size = max_size
  @auto_register = auto_register
  @cleanup_max_age = cleanup_max_age
end

#current_room_ids ⇒ Array<Integer>

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.

Returns the creature ids currently present in the room.

Hands back a copy, not the live roster. The internal roster is mutated in place by #mark_in_room / #clear_room, so a caller that mutated the returned array - the GemStone facade relied on this defensive copy before the extraction - would otherwise corrupt shared targeting state.

Returns:

  • (Array<Integer>)


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

def current_room_ids
  room_roster.dup
end

#debug_on(level = :changes) ⇒ Boolean, Symbol

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.

Toggles live echo of status, flag, and registration changes.

Parameters:

  • level (Boolean, Symbol) (defaults to: :changes) —

    false disables debug output; true or :changes reports changes only; :all reports every <crtrStatus> flag; :active reports only active <crtrStatus> flags.

Returns:

  • (Boolean, Symbol) —

    the configured debug value.



509
510
511
# File 'documented/common/creature/creature_base.rb', line 509

def debug_on(level = :changes)
  $creature_debug = level
end

#evict_stalest ⇒ Object?

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.

Evicts the least-recently-seen instance that is not in the room.

The previous room's roster is a soft shelter here: it exists only to cover the mid-refresh window (see #sheltered_ids), so when the registry is full and nothing else is evictable, a previous-room creature goes before the newcomer is refused. Without that fallback a registry filled at the moment of a room change would refuse every creature in the new room until the next refresh. Only the live roster is untouchable.

Returns:

  • (Object, nil) —

    the evicted instance, or nil when every instance is in the current room.



382
383
384
385
386
387
388
389
390
391
392
393
# File 'documented/common/creature/creature_base.rb', line 382

def evict_stalest
  previous = @previous_room_ids || []
  candidates = instances.reject { |id, _| room_roster.include?(id) }
  preferred = candidates.reject { |id, _| previous.include?(id) }
  id, instance = (preferred.empty? ? candidates : preferred)
                 .min_by { |_, candidate| candidate.last_seen_at }
  return nil unless id

  instances.delete(id)
  respond "--- Registry full: evicted #{instance.name} (#{id})" if $creature_debug
  instance
end

#forget(id) ⇒ Object?

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.

Drops one instance from the registry (room roster untouched).

Parameters:

  • id (Integer, String) —

    server creature id.

Returns:

  • (Object, nil) —

    the removed instance.



304
305
306
# File 'documented/common/creature/creature_base.rb', line 304

def forget(id)
  instances.delete(id.to_i)
end

#full? ⇒ Boolean

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.

Returns whether the registry is at capacity.

Returns:

  • (Boolean) —

    whether the registry is at capacity.



452
453
454
# File 'documented/common/creature/creature_base.rb', line 452

def full?
  size >= max_size
end

#full_warning ⇒ 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.

Returns the #warn_full_once message.

Returns:



407
408
409
# File 'documented/common/creature/creature_base.rb', line 407

def full_warning
  "#{name}: creature registry full (#{max_size}) with every entry in the current room; new creatures are not being tracked"
end

#housekeep ⇒ Integer

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.

Runs #cleanup_old at most once per HOUSEKEEPING_INTERVAL.

Independent of Combat::Tracker, whose own periodic cleanup only runs when tracking is enabled (it is off by default) and only advances on combat text - which left the registry to fill up during travel and long non-tracked sessions.

Returns:

  • (Integer) —

    number of instances removed (0 when throttled).



360
361
362
363
364
365
366
367
368
# File 'documented/common/creature/creature_base.rb', line 360

def housekeep
  now = Time.now
  return 0 if @last_housekeeping && now - @last_housekeeping < HOUSEKEEPING_INTERVAL

  @last_housekeeping = now
  removed = cleanup_old
  respond "--- Housekeeping: removed #{removed} stale creatures (unseen > #{cleanup_max_age}s)" if removed > 0 && $creature_debug
  removed
end

#in_room(*filters) ⇒ Array<Object>

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.

Returns all tracked creatures currently in the room.

Unlike #targets this requires neither hostility nor target validity, so it also serves dead creatures, looting and wound inspection. Filters are ANDed and may name a status, a classification flag, or a not_ negation such as :not_prone; unknown filters match nothing.

Parameters:

  • filters (Array<String, Symbol>) —

    optional ANDed status/classification filters.

Returns:

  • (Array<Object>)


498
499
500
501
# File 'documented/common/creature/creature_base.rb', line 498

def in_room(*filters)
  candidates = room_roster.filter_map { |id| self[id] }
  apply_filters(candidates, filters)
end

#mark_in_room(id) ⇒ Boolean

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.

Marks an id present in the current-room roster.

Parameters:

  • id (Integer, String) —

    server creature id.

Returns:

  • (Boolean) —

    true when the id was newly added.



255
256
257
258
259
260
261
# File 'documented/common/creature/creature_base.rb', line 255

def mark_in_room(id)
  id = id.to_i
  return false if room_roster.include?(id)

  room_roster << id
  true
end

#max_size ⇒ Integer

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.

Returns configured maximum registry size (default 1000).

Returns:

  • (Integer) —

    configured maximum registry size (default 1000).



442
443
444
# File 'documented/common/creature/creature_base.rb', line 442

def max_size
  @max_size ||= 1000
end

#register(name, id, noun = nil) ⇒ Object?

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.

Registers or looks up a creature instance, marking it present in the current room.

Room-marking happens on every call, new instance or not, because the feed event that triggers registration (a bolded room-object name, or a <crtrStatus> tag) is exactly the signal that the creature is present.

Housekeeping also runs from here on a wall-clock throttle (#housekeep), so the registry stays bounded whether or not Combat::Tracker (off by default) ever calls #cleanup_old.

Parameters:

  • name (String, nil) —

    display name; may be nil when the feed supplies an id before a name (DragonRealms <crtrStatus>).

  • id (Integer, String) —

    server creature id.

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

    noun from room XML, when available.

Returns:

  • (Object, nil) —

    the registered instance, or nil when auto-registration is disabled or the registry is full of creatures in the current room.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'documented/common/creature/creature_base.rb', line 212

def register(name, id, noun = nil)
  # Record room presence first: the feed event that triggers this call
  # (a bolded room-object name or a <crtrStatus> tag) is itself proof the
  # creature is in the room, and that stays true even when
  # auto-registration is disabled and no instance will be created. Mark
  # before the guard so a known creature's room presence is still tracked
  # while auto-registration is off.
  entered_room = mark_in_room(id)
  return nil unless auto_register?

  # Before the known-creature return: a session that only refreshes
  # creatures it already knows must still sweep stale out-of-room ones.
  housekeep

  existing = instances[id.to_i]
  if existing
    existing.touch_seen
    respond "--- #{name} (#{id}): in room" if entered_room && $creature_debug
    return existing
  end

  if full?
    # Housekeeping should keep this from ever happening; if it does,
    # drop the single least-recently-seen creature that is not in the
    # room. Only a room holding max_size creatures leaves nothing to
    # evict.
    evict_stalest
    if full?
      warn_full_once
      return nil
    end
  end

  instance = new(id, noun, name)
  instances[id.to_i] = instance
  respond "--- #{name} (#{id}): registered" if $creature_debug
  instance
end

#sheltered_ids ⇒ Array<Integer>

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.

Ids housekeeping must never evict: the live roster plus the roster that #clear_room just replaced. The previous roster covers the window in which the parser is still re-marking creatures from a fresh room refresh; it is superseded on the next #clear_room.

Returns:

  • (Array<Integer>)


345
346
347
# File 'documented/common/creature/creature_base.rb', line 345

def sheltered_ids
  room_roster | (@previous_room_ids || [])
end

#size ⇒ Integer

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.

Returns current number of retained instances.

Returns:

  • (Integer) —

    current number of retained instances.



447
448
449
# File 'documented/common/creature/creature_base.rb', line 447

def size
  instances.size
end

#targets(*filters) ⇒ Array<Object>

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.

Returns attackable hostile creatures currently in the room.

Room membership comes from the creature roster (fed by XML room-object and <crtrStatus> events), not from any client last-selected-target control, which can go stale after movement or death. valid_target? (supplied by the game class) removes decoys/dead appendages and crtr_flag?(:hostile) supplies structured hostility. A creature the feed has sent no <crtrStatus> for at all (see #crtr_flags?, e.g. a fresh mount) counts when GameObj.targets lists it. A creature that was hostile and is now sympathetic (Sympathy 1120 swaps one for the other mid-fight) is still a target; see InstanceMethods#ever_hostile?. Pass :not_sympathetic to leave such creatures alone (e.g. to respect a group empath's Sympathy).

Server ids are recycled: a reused id gets the existing instance, ever_hostile? included, until housekeeping evicts it - unless the game class starts a fresh one (GemStone does when a known id arrives under a different name; see Lich::Gemstone::Creature.register).

Parameters:

  • filters (Array<String, Symbol>) —

    optional ANDed status/classification filters.

Returns:

  • (Array<Object>)


477
478
479
480
481
482
483
484
485
486
487
# File 'documented/common/creature/creature_base.rb', line 477

def targets(*filters)
  fallback_ids = nil # only built if an untagged creature is in the room
  candidates = room_roster.filter_map { |id| self[id] }.select do |c|
    next false unless c.valid_target?
    next true if c.crtr_flag?(:hostile) || (c.crtr_flag?(:sympathetic) && c.ever_hostile?)
    next false if c.crtr_flags?

    (fallback_ids ||= unreported_target_ids).include?(c.id.to_s)
  end
  apply_filters(candidates, filters)
end

#warn_full_once ⇒ void

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.

This method returns an undefined value.

Logs once per process that the registry is full of creatures in the current room.



399
400
401
402
403
404
# File 'documented/common/creature/creature_base.rb', line 399

def warn_full_once
  return if @warned_full

  @warned_full = true
  Lich.log full_warning if defined?(Lich) && Lich.respond_to?(:log)
end