Class: Lich::Common::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable, MapBase
Defined in:
documented/common/map/map_dr.rb,
documented/common/map/map_gs.rb

Overview

GemStone-specific Map implementation Inherits shared functionality from MapBase Includes GS-specific features: get_location, peer tags, meta:map tags, player shops

Direct Known Subclasses

Room

Constant Summary collapse

FOG_EXITS_PATTERN =

Pattern for the live exits line when fog hides the exits. The exits cannot be compared in that state, so text matching uses the title and description alone.

/^Obvious (?:exits|paths): obscured by a thick fog$/
PEER_TAG_PATTERN =

Pattern identifying a room whose disambiguation depends on a manual peer action (for example peering through a doorway to read an adjacent room before committing to a match). Such rooms cannot be told apart without a running script to perform the peer, so scriptless fuzzy matching declines to resolve them. Kept verbatim from the historical inline checks in match_fuzzy.

/^(set desc on; )?peer [a-z]+ =~ \/.+\/$/
TOLERANT_SENTENCE_MIN_WORDS =

Fewest words a live description sentence needs to identify a room in tolerant_text_candidates. Shorter fragments (a closing quote mark, an abbreviation cut at its period, a two-word aside) occur in many stored descriptions and say nothing about which room this is.

4
@@loaded =
false
@@load_mutex =
Mutex.new
@@current_room_mutex =
Mutex.new
@@fuzzy_room_mutex =
Mutex.new
@@resolution_mutex =

The last two room resolutions, each [room_count, room id, live UID, nav_count]. See note_resolution and anchor_room.

Mutex.new
@@uids =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MapBase

included

Constructor Details

#initialize(id, title, description, paths, uid = [], location = nil, climate = nil, terrain = nil, wayto = {}, timeto = {}, image = nil, image_coords = nil, tags = [], check_location = nil, unique_loot = nil) ⇒ Map

Initializes a Map room with all metadata.

Rooms are automatically registered in the class-level @@list and made available via .list, .[id], and iteration. If the map is already loaded, the tag index is reset to reflect the new room; during bulk load (when @@loaded is false), tag index resets are deferred until load_json finishes.

Parameters:

  • id (Integer) —

    unique room identifier

  • title (Array<String>) —

    room title(s); newest first

  • description (Array<String>) —

    room description(s); newest first

  • paths (Array<String>) —

    exit/path strings; newest first

  • uid (Array<Integer>) (defaults to: []) —

    GemStone unique IDs; default []

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

    in-game location from 'location' command; default nil

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

    climate tag; default nil

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

    terrain tag; default nil

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

    navigation hints (not currently used); default {}

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

    travel time estimates (not currently used); default {}

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

    map image file reference; default nil

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

    coordinates within the image; default nil

  • tags (Array<String>) (defaults to: []) —

    searchable tags including special meta:map tags; default []

  • check_location (Boolean, nil) (defaults to: nil) —

    when true, require location match during current room lookup; default nil

  • unique_loot (Array<String>, nil) (defaults to: nil) —

    specific items that appear only in this room; default nil



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'documented/common/map/map_dr.rb', line 63

def initialize(id, title, description, paths, uid = [], location = nil,
               climate = nil, terrain = nil, wayto = {}, timeto = {},
               image = nil, image_coords = nil, tags = [], check_location = nil,
               unique_loot = nil, _room_objects = nil,
               genie_id = nil, genie_zone = nil, genie_pos = nil)
  @id = id
  @title = title
  @description = description
  @paths = paths
  @uid = uid
  @location = location
  @climate = climate
  @terrain = terrain
  @wayto = wayto
  @timeto = timeto
  @image = image
  @image_coords = image_coords
  @tags = TagList.new(tags, self.class)
  @check_location = check_location
  @unique_loot = unique_loot
  @genie_id = genie_id
  @genie_zone = genie_zone
  @genie_pos = genie_pos
  @@list[@id] = self
  # Skipped during a bulk load: @@loaded is false throughout, load_json
  # clears the cache when it finishes, and any tag query while unloaded
  # goes through #list, which loads first. Saves one mutex per room.
  self.class.reset_tag_index if @@loaded
end

Instance Attribute Details

#check_location ⇒ Object

Returns the value of attribute check_location.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def check_location
  @check_location
end

#climate ⇒ Object

Returns the value of attribute climate.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def climate
  @climate
end

#description ⇒ Object

Returns the value of attribute description.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def description
  @description
end

#genie_id ⇒ Object

Returns the value of attribute genie_id.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def genie_id
  @genie_id
end

#genie_pos ⇒ Object

Returns the value of attribute genie_pos.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def genie_pos
  @genie_pos
end

#genie_zone ⇒ Object

Returns the value of attribute genie_zone.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def genie_zone
  @genie_zone
end

#id ⇒ Object (readonly)

Returns the value of attribute id.



32
33
34
# File 'documented/common/map/map_dr.rb', line 32

def id
  @id
end

#image ⇒ Object

Returns the value of attribute image.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def image
  @image
end

#image_coords ⇒ Object

Returns the value of attribute image_coords.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def image_coords
  @image_coords
end

#location ⇒ Object

Returns the value of attribute location.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def location
  @location
end

#paths ⇒ Object

Returns the value of attribute paths.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def paths
  @paths
end

#room_objects ⇒ Object

Returns the value of attribute room_objects.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def room_objects
  @room_objects
end

#tags ⇒ TagList (readonly)

Returns mutation-aware list of this room's tags.

Returns:

  • (TagList) —

    mutation-aware list of this room's tags



39
40
41
# File 'documented/common/map/map_dr.rb', line 39

def tags
  @tags
end

#terrain ⇒ Object

Returns the value of attribute terrain.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def terrain
  @terrain
end

#timeto ⇒ Object

Returns the value of attribute timeto.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def timeto
  @timeto
end

#title ⇒ Object

Returns the value of attribute title.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def title
  @title
end

#uid ⇒ Object

Returns the value of attribute uid.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def uid
  @uid
end

#unique_loot ⇒ Object

Returns the value of attribute unique_loot.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def unique_loot
  @unique_loot
end

#wayto ⇒ Object

Returns the value of attribute wayto.



33
34
35
# File 'documented/common/map/map_dr.rb', line 33

def wayto
  @wayto
end

Class Method Details

.adjacent_candidate(candidates) ⇒ Lich::Common::Map?

Of candidates, the single one the character can be standing in after leaving the anchor room (see anchor_room): a room it has a wayto to, or the anchor itself. The anchor counts because a fresh room display does not always mean a move, so an identical neighbour must not win over it; but not when the game's UIDs show the character has left it (see left_room?), so an identical room just left is never taken for the live one.

Parameters:

Returns:

  • (Lich::Common::Map, nil) —

    the one reachable candidate; nil with no anchor room, or when none or several are reachable



516
517
518
519
520
521
522
523
524
# File 'documented/common/map/map_dr.rb', line 516

def self.adjacent_candidate(candidates)
  from = anchor_room
  return nil if from.nil?

  stayed = !left_room?(from)
  wayto = from.wayto || {}
  reachable = candidates.select { |r| (stayed && r.id == from.id) || wayto.key?(r.id.to_s) }
  reachable.size == 1 ? reachable.first : nil
end

.anchor_room ⇒ Lich::Common::Map?

The room the character was in before the live display, to judge which of several identical rooms it can have reached.

DR resolves a room only on demand, so the last resolved room can be several moves old (go2 walks its whole path without asking) or can be the live room itself (resolved a moment ago for the same display). The game's own record of the room it left comes first: the UID of the previous

Returns:



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'documented/common/map/map_dr.rb', line 473

def self.anchor_room
  previous_uid = XMLData.previous_nav_rm
  ids = previous_uid.to_i.positive? ? ids_from_uid(previous_uid.to_i) : []
  return @@list[ids.first] if ids.size == 1

  return nil if previous_uid.nil?

  _count, id, uid, nav = earlier_resolution
  return nil unless id.is_a?(Integer) && id >= 0 && nav.is_a?(Integer)

  arrivals = XMLData.nav_count.to_i - nav
  return @@list[id] if arrivals == 1 && uid == previous_uid.to_i
  return @@list[id] if arrivals.zero? && uid == XMLData.room_id.to_i

  nil
end

.by_genie_ref(zone_id, node_id) ⇒ Map?

Finds a room by its GenieSys zone and node identifiers.

Parameters:

  • zone_id (String, Integer) —

    the GenieSys zone id

  • node_id (String, Integer) —

    the GenieSys node id

Returns:

  • (Map, nil) —

    the matching room, or nil if not found



215
216
217
218
# File 'documented/common/map/map_dr.rb', line 215

def self.by_genie_ref(zone_id, node_id)
  self.load unless @@loaded
  @@list.find { |r| r&.genie_zone == zone_id.to_s && r&.genie_id == node_id.to_s }
end

.clear ⇒ 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.

Clears the entire map from memory and resets the loaded state.

Acquires the load mutex before clearing to ensure thread safety. Also clears the tag cache, location cache, image cache, and triggers garbage collection.

Returns:

  • (Boolean) —

    true



715
716
717
718
719
720
721
722
723
724
# File 'documented/common/map/map_dr.rb', line 715

def self.clear
  @@load_mutex.synchronize do
    @@list.clear
    clear_tags_cache
    reset_resolutions
    @@loaded = false
    GC.start
  end
  true
end

.clear_tags_cache ⇒ 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 the cached tag index, forcing rebuild on next tag query.



186
187
188
# File 'documented/common/map/map_dr.rb', line 186

def clear_tags_cache
  reset_tag_index
end

.current ⇒ Map?

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 Map room object for the current location, matching by UID, description, title, exits, and optional peer tags and location requirements.

Returns nil if the room cannot be matched. Caches the result per room_count to avoid redundant matching on the same server message. Uses UIDs as primary lookup, then falls back to multi-UID rooms and description matching.

This method is called automatically by the game loop and is not typically invoked directly from scripts.

Returns:

  • (Map, nil) —

    the current room object, or nil if not identified

See Also:



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'documented/common/map/map_dr.rb', line 279

def self.current
  self.load unless @@loaded
  if Script.current
    return @@list[@@current_room_id] if XMLData.room_count == @@current_room_count && !@@current_room_id.nil?
  elsif XMLData.room_count == @@fuzzy_room_count && !@@current_room_id.nil?
    return @@list[@@current_room_id]
  end
  ids = XMLData.room_id.zero? ? [] : ids_from_uid(XMLData.room_id)
  return set_current(ids[0]) if ids.size == 1

  if ids.size > 1 && !@@current_room_id.nil? && (id = match_multi_ids(ids))
    return set_current(id)
  end
  match_no_uid
end

.current_or_new ⇒ Object

GS-specific: Extended current_or_new with meta:map tag handling



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'documented/common/map/map_dr.rb', line 619

def self.current_or_new
  return nil unless Script.current

  @@current_room_count = -1
  @@fuzzy_room_count = -1

  self.load unless @@loaded

  id = current&.id

  echo("Map: current room id is #{id.inspect}")
  unless id.nil?
    room = self[id]
    unless XMLData.room_id.zero? || room.uid.include?(XMLData.room_id)
      room.uid << XMLData.room_id
      uids_add(XMLData.room_id, room.id)
      echo "Map: Adding new uid for #{room.id}: #{XMLData.room_id}"
    end
    return set_current(room.id)
  end

  # Guard against a blank/incomplete arrival frame. DR occasionally streams a room whose
  # <nav> UID is delayed or absent (room_id 0) before the room text populates: the
  # description is only the "pitch dark" placeholder and there are no exits. Minting a
  # room here creates a junk stub (empty title, no UID) that orphans or duplicates the
  # real room. Keep the current room instead; the real room resolves by UID once the
  # delayed nav (or a re-look) provides it.
  if XMLData.room_id.zero? &&
     XMLData.room_exits_string.to_s.strip.empty? &&
     XMLData.room_description.to_s.strip == "It's pitch dark and you can't see a thing!"
    echo 'Map: skipped blank/incomplete room frame (no uid, pitch-dark, no exits)'
    # Keep the current room - but only if one has actually resolved.
    # @@current_room_id is the -1 sentinel before the first match; passing
    # that to set_current would index @@list[-1] (the last room) and make an
    # unrelated room current, so fall back to nil in that case instead.
    return set_current(@@current_room_id) if @@current_room_id.is_a?(Integer) && @@current_room_id >= 0

    return nil
  end

  # Several rooms fit the live text but none was confirmed as this room:
  # the live UID vetoed them, or none could be told apart from the room
  # before it. The room may be one of them or a new one; mapping it as
  # new keeps identical rooms with no UID mappable, so say so.
  twins = live_text_candidates.size
  if twins > 1
    echo "Map: #{twins} mapped rooms match this room's text but none could be confirmed as this room; mapping it as a new room"
  end

  id = get_free_id
  title = [XMLData.room_title]
  description = [XMLData.room_description.strip]
  paths = [XMLData.room_exits_string.strip]
  uid = XMLData.room_id.zero? ? [] : [XMLData.room_id]
  room = new(id, title, description, paths, uid)
  uids_add(XMLData.room_id, room.id) unless XMLData.room_id.zero?
  echo "mapped new room, set current room to #{room.id}"
  set_current(id)
end

.current_room_id ⇒ 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 room ID the current script is in, or nil if not yet set.

Returns:

  • (Integer, nil) —

    the current room ID



114
115
116
# File 'documented/common/map/map_dr.rb', line 114

def current_room_id
  @@current_room_id
end

.current_room_id=(id) ⇒ 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.

Sets the room ID for the current script.

Parameters:

  • id (Integer) —

    room ID

Returns:

  • (Integer) —

    the new room ID



120
121
122
123
# File 'documented/common/map/map_dr.rb', line 120

def current_room_id=(id)
  note_resolution(id)
  @@current_room_id = id
end

.description_sentences(description) ⇒ Array<String>

The sentences of a live description that tolerant_text_candidates looks for: split at each run of sentence-ending marks (+.+, !, ?), stripped, keeping only those of at least TOLERANT_SENTENCE_MIN_WORDS words. An empty description has none.

Parameters:

Returns:



396
397
398
399
400
# File 'documented/common/map/map_dr.rb', line 396

def self.description_sentences(description)
  description.to_s.split(/[.!?]+/).map(&:strip).select do |sentence|
    sentence.split.size >= TOLERANT_SENTENCE_MIN_WORDS
  end
end

.earlier_resolution ⇒ Array?

The newest noted resolution made for a room count before the live one. A resolution of the live display itself is never returned: it would make the room its own anchor.

Returns:

  • (Array, nil) —

    [room_count, room id, live UID, nav_count]



263
264
265
266
267
268
269
# File 'documented/common/map/map_dr.rb', line 263

def self.earlier_resolution
  count = XMLData.room_count
  records = @@resolution_mutex.synchronize { [@@last_resolution, @@prior_resolution] }
  records.find do |record|
    record && record[0].is_a?(Integer) && count.is_a?(Integer) && record[0] < count
  end
end

.fuzzy_room_id ⇒ 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 room ID of the last fuzzy match attempt, for scripts without current script context (used in background thread mode).

Returns:

  • (Integer, nil) —

    the fuzzy-matched room ID



226
227
228
# File 'documented/common/map/map_gs.rb', line 226

def self.fuzzy_room_id
  @@fuzzy_room_id
end

.get_location ⇒ Object

GS-specific: Get location using in-game 'location' command



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'documented/common/map/map_gs.rb', line 231

def self.get_location
  unless XMLData.room_count == @@current_location_count
    if (script = Script.current)
      save_want_downstream = script.want_downstream
      script.want_downstream = true
      waitrt?
      location_result = dothistimeout(
        'location', 15,
        /^You carefully survey your surroundings and guess that your current location is .*? or somewhere close to it\.$|^You can't do that while submerged under water\.$|^You can't do that\.$|^It would be rude not to give your full attention to the performance\.$|^You can't do that while hanging around up here!$|^You are too distracted by the difficulty of staying alive in these treacherous waters to do that\.$|^You carefully survey your surroundings but are unable to guess your current location\.$|^Not in pitch darkness you don't\.$|^That is too difficult to consider here\.$/
      )
      script.want_downstream = save_want_downstream
      @@current_location_count = XMLData.room_count
      if location_result =~ /^You can't do that while submerged under water\.$|^You can't do that\.$|^It would be rude not to give your full attention to the performance\.$|^You can't do that while hanging around up here!$|^You are too distracted by the difficulty of staying alive in these treacherous waters to do that\.$|^You carefully survey your surroundings but are unable to guess your current location\.$|^Not in pitch darkness you don't\.$|^That is too difficult to consider here\.$/
        @@current_location = false
      else
        @@current_location = /^You carefully survey your surroundings and guess that your current location is (.*?) or somewhere close to it\.$/.match(location_result).captures.first
      end
    else
      return nil
    end
  end
  @@current_location
end

.ids_from_uid(n) ⇒ 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 room IDs associated with a GemStone UID, or empty array if not found.

Parameters:

  • n (Integer) —

    GemStone UID from XMLData.room_id

Returns:

  • (Array<Integer>) —

    matching room IDs; empty if UID not known



706
707
708
# File 'documented/common/map/map_dr.rb', line 706

def self.ids_from_uid(n)
  @@uids[n].nil? || n.zero? ? [] : @@uids[n]
end

.images ⇒ Object

GS-specific: Get all unique map images



593
594
595
596
597
# File 'documented/common/map/map_gs.rb', line 593

def self.images
  self.load unless @@loaded
  @@images = @@list.compact.each_with_object({}) { |r, h| h[r.image] = nil unless h.key?(r.image) }.keys if @@images.empty?
  @@images.dup
end

.left_room?(room) ⇒ Boolean

Whether the game's own UIDs show the character has left room: the previous

Parameters:

Returns:

  • (Boolean)


496
497
498
499
500
501
502
503
# File 'documented/common/map/map_dr.rb', line 496

def self.left_room?(room)
  previous_uid = XMLData.previous_nav_rm.to_i
  live_uid = XMLData.room_id.to_i
  return false unless previous_uid.positive? && previous_uid != live_uid

  uids = stored_uids(room)
  uids.include?(previous_uid) && !uids.include?(live_uid)
end

.list ⇒ Array<Map>

Returns the complete array of all loaded rooms, loading the map if needed.

Triggers a load on first call. Thread-safe. Safe to iterate while the map is being modified.

Examples:

Map.list.size #=> 12345
Map.list.first #=> #<Map id=1>

Returns:

  • (Array<Map>) —

    all room objects



152
153
154
155
# File 'documented/common/map/map_dr.rb', line 152

def list
  self.load unless @@loaded
  @@list
end

.list=(value) ⇒ Array<Map>

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.

Replaces the entire room list and normalizes tag lists for efficient searching.

Parameters:

  • value (Array<Map>) —

    new list of rooms

Returns:

  • (Array<Map>) —

    the new list



168
169
170
171
# File 'documented/common/map/map_dr.rb', line 168

def list=(value)
  @@list = value
  normalize_tag_lists(value)
end

.live_text_candidates ⇒ Array<Lich::Common::Map>

The rooms that can be the live room by their text: those matching it exactly (see text_candidates), or, only when no room does, those whose description merely shares a sentence with it (see tolerant_text_candidates). The tolerant rooms are never mixed with an exact match, even an ambiguous or vetoed one.

Returns:



431
432
433
434
435
# File 'documented/common/map/map_dr.rb', line 431

def self.live_text_candidates
  rooms = rooms_matching_live_title_and_exits
  candidates = text_candidates(rooms)
  candidates.empty? ? tolerant_text_candidates(rooms) : candidates
end

.load_uids ⇒ 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.

Rebuilds the UID lookup table from the current room list.

Clears @@uids and re-indexes all UIDs from every room in @@list. Called after loading the map file to make UID lookups available.



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'documented/common/map/map_dr.rb', line 686

def self.load_uids
  self.load unless @@loaded
  @@uids.clear
  # compact rather than relying on Lich's NilClass patch to make r.uid on a
  # hole return nil; a sparse map should not need that to load.
  @@list.compact.each do |r|
    r.uid.each do |u|
      if @@uids[u].nil?
        @@uids[u] = [r.id]
      elsif !@@uids[u].include?(r.id)
        @@uids[u] << r.id
      end
    end
  end
end

.loaded ⇒ 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 map has finished loading from the map file.

Returns:

  • (Boolean) —

    true if the map is loaded



110
111
112
# File 'documented/common/map/map_gs.rb', line 110

def loaded
  @@loaded
end

.loaded? ⇒ 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 map has finished loading from the map file.

Returns:

  • (Boolean) —

    true if the map is loaded



143
144
145
# File 'documented/common/map/map_dr.rb', line 143

def loaded?
  @@loaded
end

.locations ⇒ Object

GS-specific: Get all unique locations



586
587
588
589
590
# File 'documented/common/map/map_gs.rb', line 586

def self.locations
  self.load unless @@loaded
  @@locations = @@list.compact.each_with_object({}) { |r, h| h[r.location] = nil unless h.key?(r.location) }.keys if @@locations.empty?
  @@locations.dup
end

.mark_loaded ⇒ 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 the map as fully loaded.

Returns:

  • (Boolean) —

    true



194
195
196
# File 'documented/common/map/map_dr.rb', line 194

def mark_loaded
  @@loaded = true
end

.match_current(script) ⇒ Object

GS-specific: match_current with peer tag checking



577
578
579
580
581
582
583
584
585
# File 'documented/common/map/map_dr.rb', line 577

def self.match_current(_script)
  @@current_room_mutex.synchronize do
    loop do
      @@current_room_count = XMLData.room_count
      id = match_live_text(honor_peer_tags: false)
      return id if @@current_room_count == XMLData.room_count
    end
  end
end

.match_fuzzy ⇒ 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.

Matches the current location to a room, used in non-script contexts where peer tags cannot be checked.

Used by background threads and front-end clients that do not have script context. Skips peer tag matching because those tags require executing in-game commands that only a script can do safely. Returns nil if the room has unresolved peer tag requirements.

Returns:

  • (Integer, nil) —

    the matched room ID, or nil if no match or match has peer tags



597
598
599
600
601
602
603
604
605
# File 'documented/common/map/map_dr.rb', line 597

def self.match_fuzzy
  @@fuzzy_room_mutex.synchronize do
    loop do
      @@fuzzy_room_count = XMLData.room_count
      id = match_live_text(honor_peer_tags: true)
      return id if @@fuzzy_room_count == XMLData.room_count
    end
  end
end

.match_live_text(honor_peer_tags:) ⇒ Integer?

Resolve the live room from its title, description and exits (see live_text_candidates).

Parameters:

Returns:

  • (Integer, nil) —

    the resolved room id, or nil when no room, or more than one, can be the live room



560
561
562
563
564
565
# File 'documented/common/map/map_dr.rb', line 560

def self.match_live_text(honor_peer_tags:)
  room = pick_text_candidate(live_text_candidates)
  return nil if room.nil?

  resolve_matched_room(room, honor_peer_tags: honor_peer_tags)
end

.note_resolution(id) ⇒ void

This method returns an undefined value.

Remember which room the live display at the current room count resolved to, the UID the game showed with it, and how many arrivals the game had reported by then. A later resolution of the same display replaces the record only with a room, never with nil: scriptless matching declines a peer-tagged room that a script resolves.

Parameters:

  • id (Integer, nil) —

    the resolved room id



235
236
237
238
239
240
241
242
243
244
245
246
# File 'documented/common/map/map_dr.rb', line 235

def self.note_resolution(id)
  count = XMLData.room_count
  record = [count, id, XMLData.room_id.to_i, XMLData.nav_count]
  @@resolution_mutex.synchronize do
    if @@last_resolution && @@last_resolution[0] == count
      @@last_resolution = record unless id.nil?
    else
      @@prior_resolution = @@last_resolution
      @@last_resolution = record
    end
  end
end

.peer_disambiguation_tag?(room) ⇒ Boolean

Whether room carries a peer-disambiguation tag (see PEER_TAG_PATTERN).

Parameters:

Returns:

  • (Boolean) —

    true when the room requires a manual peer to disambiguate, false otherwise



314
315
316
# File 'documented/common/map/map_dr.rb', line 314

def self.peer_disambiguation_tag?(room)
  room.tags.any? { |tag| tag =~ PEER_TAG_PATTERN }
end

.pick_text_candidate(candidates) ⇒ Lich::Common::Map?

Pick the live room from the rooms matching its text.

Every candidate the live UID vetoes is dropped. A lone survivor is the room. Otherwise the room resolves only when exactly one candidate is reachable from the anchor room (see adjacent_candidate) and that candidate survived the veto; in every other case it is unresolved rather than the first of them. No candidates is unresolved without looking up an anchor room.

When the veto dropped any candidate, reachability is judged over all the candidates, vetoed ones included, and even a lone survivor must be the one reachable. The game's UID then belongs to no candidate, so the live room may be the vetoed room under a UID the map lacks: a look-alike copy elsewhere in the map, or one sharing the vetoed room's neighbours, is not taken for it.

Parameters:

Returns:



544
545
546
547
548
549
550
551
552
# File 'documented/common/map/map_dr.rb', line 544

def self.pick_text_candidate(candidates)
  return nil if candidates.empty?

  survivors = candidates.reject { |r| uid_vetoes?(r) }
  return survivors.first if survivors.size == 1 && survivors.size == candidates.size

  room = adjacent_candidate(candidates)
  survivors.include?(room) ? room : nil
end

.previous ⇒ Map?

Returns the Map room object for the previous location, or nil if not yet set.

Examples:

Map.previous #=> #<Map id=1234>
Map.previous #=> nil  # if no previous room recorded

Returns:

  • (Map, nil) —

    the previous room



223
224
225
# File 'documented/common/map/map_dr.rb', line 223

def self.previous
  @@list[@@previous_room_id]
end

.previous_room_id ⇒ 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 room ID the current script was previously in, or nil if not yet set.

Returns:

  • (Integer, nil) —

    the previous room ID



128
129
130
# File 'documented/common/map/map_dr.rb', line 128

def previous_room_id
  @@previous_room_id
end

.previous_room_id=(id) ⇒ 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.

Sets the room ID for the previous location.

Parameters:

  • id (Integer) —

    room ID

Returns:

  • (Integer) —

    the new room ID



136
137
138
# File 'documented/common/map/map_dr.rb', line 136

def previous_room_id=(id)
  @@previous_room_id = id
end

.raw_list ⇒ Object

The backing array without triggering a load. Only for use inside the load path, where #list would re-enter the load mutex and deadlock.



159
160
161
# File 'documented/common/map/map_dr.rb', line 159

def raw_list
  @@list
end

.reset_resolutions ⇒ void

This method returns an undefined value.

Forget the noted resolutions, so no room carries over as an anchor.



251
252
253
254
255
256
# File 'documented/common/map/map_dr.rb', line 251

def self.reset_resolutions
  @@resolution_mutex.synchronize do
    @@last_resolution = nil
    @@prior_resolution = nil
  end
end

.resolve_matched_room(room, honor_peer_tags:) ⇒ Integer?

Resolve a room that already matched on title/description/paths down to a final room id, applying UID disambiguation.

The governing invariant is that a stored UID must never make a room less resolvable than an otherwise identical room with no UID.

  • When the game exposes a live UID (+XMLData.room_id+ is non-zero) and the matched room carries one or more UIDs, the match only stands if the live UID is among them (see uid_vetoes?). A stored UID of 0 counts as no UID (see stored_uids).
  • When the game exposes no UID (+XMLData.room_id+ is zero), UID disambiguation is skipped and the title/description/paths match is trusted regardless of any UID stored on the room, so a UID stamped onto a room the game shows without one cannot make it unresolvable.

Parameters:

  • room (Lich::Common::Map) —

    the room matched on title/description/paths

  • honor_peer_tags (Boolean) —

    when true, a room requiring a manual peer to disambiguate (see peer_disambiguation_tag?) resolves to nil unless the live UID confirms it; used by scriptless fuzzy matching, which cannot perform the peer. Exact (match_current) matching passes false and never consults peer tags.

Returns:

  • (Integer, nil) —

    the resolved room id; nil when the live UID vetoes the room, or when a peer-tagged room cannot be disambiguated



363
364
365
366
367
368
369
# File 'documented/common/map/map_dr.rb', line 363

def self.resolve_matched_room(room, honor_peer_tags:)
  return nil if uid_vetoes?(room)
  return room.id if !XMLData.room_id.zero? && stored_uids(room).any?
  return nil if honor_peer_tags && peer_disambiguation_tag?(room)

  room.id
end

.room_from_json(room) ⇒ Map

Construct a room from a parsed JSON hash

Parameters:

Returns:

  • (Map) —

    the registered room



729
730
731
732
733
734
735
736
737
738
# File 'documented/common/map/map_dr.rb', line 729

def self.room_from_json(room)
  new(
    room['id'], room['title'], room['description'], room['paths'],
    room['uid'], room['location'], room['climate'], room['terrain'],
    room['wayto'], room['timeto'], room['image'], room['image_coords'],
    room['tags'], room['check_location'], room['unique_loot'],
    nil, # _room_objects
    room['genie_id'], room['genie_zone'], room['genie_pos']
  )
end

.rooms_matching_live_title_and_exits ⇒ Array<Lich::Common::Map>

Every room whose title and exits match the live room, ignoring the exits under fog. The map is scanned once for both description passes. A room missing its title or exits (the map has a few) is skipped without relying on Lich's NilClass patch.

Returns:



443
444
445
446
447
448
449
450
451
452
453
454
# File 'documented/common/map/map_dr.rb', line 443

def self.rooms_matching_live_title_and_exits
  title = XMLData.room_title
  exits = XMLData.room_exits_string.to_s
  foggy = exits.match?(FOG_EXITS_PATTERN)
  exits = exits.strip
  @@list.select do |r|
    # Skip nil holes without relying on Lich's NilClass patch.
    r &&
      Array(r.title).include?(title) &&
      (foggy || Array(r.paths).include?(exits))
  end
end

.stored_uids(room) ⇒ Array<Integer>

The UIDs stored on room that can identify it. A stored 0 is not a UID: the parser reads 0 for a room the game shows without one (a bare

Parameters:

Returns:

  • (Array<Integer>)


324
325
326
# File 'documented/common/map/map_dr.rb', line 324

def self.stored_uids(room)
  Array(room.uid) - [0]
end

.synchronize_load { ... } ⇒ 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.

Executes a block under the map load mutex to ensure thread-safe loading.

Yields:

  • runs the given block with the load lock held

Returns:

  • (Object) —

    the result of the block



205
206
207
# File 'documented/common/map/map_dr.rb', line 205

def synchronize_load(&block)
  @@load_mutex.synchronize(&block)
end

.text_candidates(rooms = rooms_matching_live_title_and_exits) ⇒ Array<Lich::Common::Map>

Every room whose title, description and exits all match the live room. Under fog the live exits line carries no exits, so title and description alone must match. The description is compared whole.

Parameters:

Returns:



378
379
380
381
# File 'documented/common/map/map_dr.rb', line 378

def self.text_candidates(rooms = rooms_matching_live_title_and_exits)
  description = XMLData.room_description.to_s.strip
  rooms.select { |r| Array(r.description).include?(description) }
end

.tolerant_text_candidates(rooms = rooms_matching_live_title_and_exits) ⇒ Array<Lich::Common::Map>

Every room whose title and exits match the live room exactly (title alone under fog) and one of whose stored descriptions contains, as a run of text, at least one sentence of the live description (see description_sentences). The run need not start or end at a word edge of the stored text ("over the road" is found in "over the roadway"). This tolerates a description that has drifted from the map: a typo fixed in one sentence, a sentence rewritten, a day/night variant the map lacks. A live description with no usable sentence, an empty one included, matches nothing.

Parameters:

Returns:



415
416
417
418
419
420
421
422
# File 'documented/common/map/map_dr.rb', line 415

def self.tolerant_text_candidates(rooms = rooms_matching_live_title_and_exits)
  sentences = description_sentences(XMLData.room_description)
  return [] if sentences.empty?

  rooms.select do |r|
    Array(r.description).any? { |stored| sentences.any? { |sentence| stored.to_s.include?(sentence) } }
  end
end

.uid_vetoes?(room) ⇒ Boolean

Whether the live game UID rules room out: the game reports a UID and the room stores UIDs, none of which is the live one.

Parameters:

Returns:

  • (Boolean)


333
334
335
336
337
338
# File 'documented/common/map/map_dr.rb', line 333

def self.uid_vetoes?(room)
  return false if XMLData.room_id.zero?

  uids = stored_uids(room)
  uids.any? && !uids.include?(XMLData.room_id)
end

.uids ⇒ Hash{Integer => 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 GemStone UID-to-room-IDs lookup table.

Returns:

  • (Hash{Integer => Array<Integer>}) —

    maps UID to array of room IDs



178
179
180
# File 'documented/common/map/map_dr.rb', line 178

def uids
  @@uids
end

.uids_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 the UID-to-room-ID lookup table.



603
604
605
# File 'documented/common/map/map_gs.rb', line 603

def self.uids_clear
  @@uids.clear
end

Instance Method Details

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

Note:

Prefer .current over this method.

Returns the current room ID (instance method alias for class variable).

Returns:

  • (Integer, nil) —

    the current room ID



208
209
210
# File 'documented/common/map/map_gs.rb', line 208

def fuzzy_room_id
  @@current_room_id
end

#json_extra_fields ⇒ Hash

Exports GenieSys fields for JSON serialization.

Returns:

  • (Hash) —

    hash with :genie_id, :genie_zone, :genie_pos keys



105
106
107
# File 'documented/common/map/map_dr.rb', line 105

def json_extra_fields
  { genie_id: @genie_id, genie_zone: @genie_zone, genie_pos: @genie_pos }
end

#to_s ⇒ String

Returns a human-readable string representation of this room.

Examples:

room.to_s #=> "#1234 (u4294967295):\nRoad (Wehnimer's Landing)\nA dusty road...\nObvious exits: north, south"

Returns:

  • (String) —

    room ID, newest UID, title, location, description, and exits



98
99
100
# File 'documented/common/map/map_dr.rb', line 98

def to_s
  "##{@id} (#{@uid[-1]}):\n#{@title[-1]}\n#{@description[-1]}\n#{@paths[-1]}"
end