Module: Lich::Common::MapBase::ClassMethods
- Defined in:
- documented/common/map/map_base.rb
Overview
Class methods shared across all Map implementations
Constant Summary collapse
- TAG_INDEX_BUILD_ATTEMPTS =
Tag name => Array of room ids. Private: callers must go through #rooms_by_tag or #tag_names so the memo cannot be mutated in place.
Rebuild attempts before giving up on caching the result.
3- TAG_INDEX_MUTEX =
Serialises publishing the memo against invalidating it. Only ever held across a couple of assignments, never across a build or a load, so it cannot invert the ordering against the load mutex.
Mutex.new
Instance Method Summary collapse
-
#[](val) ⇒ Object?
Look up a room by id, uid string, or fuzzy title/description text.
-
#apply_wayto_overrides ⇒ void
Applies personal map wayto overrides and custom targets from YAML settings.
-
#boundary_rooms_for(ids) ⇒ Array<Integer>
Rooms bordering a set of rooms: every room an exit of the set leads to that is not itself in the set (bigshot's perimeter list).
-
#dijkstra(source, destination = nil, static_only: false) ⇒ Array<Hash>?
(also: #dijkstra_hashes)
Class-level dijkstra dispatcher.
-
#estimate_time(array, from: ESTIMATE_NO_ORIGIN) ⇒ Float
Estimate total travel time along a path.
-
#findpath(source, destination) ⇒ Object
Find path between two rooms.
-
#get_free_id ⇒ Integer
Get the next available room ID.
-
#ids_from_uid(uid) ⇒ Object
Get room IDs from a UID.
-
#json_map_files ⇒ Array<String>
JSON map databases in the data directory, newest first.
-
#legacy_map_files ⇒ Array<String>
Legacy map files sitting in the data directory, basenames only.
-
#load(filename = nil) ⇒ Boolean
Load the newest JSON map database, or a specific file.
-
#load_json(filename = nil) ⇒ Boolean
Load the newest usable JSON map database, falling back to older candidates when one is unreadable.
-
#map_loaded_message(filename) ⇒ String
Announced once a database has loaded.
-
#match_multi_ids(ids) ⇒ Integer?
Narrow a set of candidate ids to the one reachable from the current room.
-
#match_no_uid ⇒ Object?
Resolve the current room when the game gave no usable uid.
-
#normalize_tag_lists(rooms = list) ⇒ nil
Re-wrap plain Array tags as TagList.
-
#parse_map_json(filename) ⇒ Boolean
False when the file was unusable.
-
#previous_uid ⇒ Integer?
The uid the game last navigated away from.
-
#reload ⇒ Object
Reload the map database.
-
#report_unsupported_map_files(files) ⇒ nil
Explain why an old map database no longer loads.
-
#reset_tag_index ⇒ nil
Drop the tag memo.
-
#rooms_by_tag(tag_name) ⇒ Array<Integer>
Room ids carrying a tag, nearest-agnostic and in room id order.
-
#save_json(filename = nil) ⇒ void
(also: #save)
Save the map as a JSON file.
-
#set_current(id) ⇒ Object?
Record the room the game moved to, remembering the one it left.
-
#set_fuzzy(id) ⇒ Object?
As #set_current, but a nil id leaves the previous room untouched.
-
#tag_names ⇒ Array<String>
Tag names present anywhere in the room list, in room id order.
-
#tags ⇒ Array<String>
Tag names present anywhere in the room list.
-
#to_json(*args) ⇒ String
Convert the map to a JSON array of its rooms, in id order.
-
#uids_add(uid, id) ⇒ Object
Add a UID mapping.
-
#validate_room_json!(room, filename) ⇒ nil
The id is the only field a room cannot do without: it indexes the room into the backing array, and a non-Integer would raise there with a message that says nothing about the database.
Instance Method Details
#[](val) ⇒ Object?
Look up a room by id, uid string, or fuzzy title/description text
617 618 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 |
# File 'documented/common/map/map_base.rb', line 617 def [](val) # One load attempt via the accessor, then work off that array; calling # #list again would retry the load on every lookup when it failed. rooms = list if val.is_a?(Integer) || val =~ /^[0-9]+$/ rooms[val.to_i] elsif val =~ /^u(-?\d+)$/i uid_request = ::Regexp.last_match(1).dup.to_i # nil.to_i is 0, so an unknown uid used to resolve to room 0. id = ids_from_uid(uid_request)[0] id.nil? ? nil : rooms[id.to_i] else chkre = /#{val.strip.sub(/\.$/, '').gsub(/\.(?:\.\.)?/, '|')}/i chk = /#{Regexp.escape(val.strip)}/i # Title and exact-description matches share one pass; the loose # regex pass only runs when neither found anything. Same precedence # as the three sequential scans this replaces. live = rooms.compact by_title = nil by_desc = nil live.each do |room| if room.title.find { |title| title =~ chk } by_title = room break end by_desc = room if by_desc.nil? && room.description.find { |desc| desc =~ chk } end by_title || by_desc || live.find { |room| room.description.find { |desc| desc =~ chkre } } end end |
#apply_wayto_overrides ⇒ void
This method returns an undefined value.
Applies personal map wayto overrides and custom targets from YAML settings. Reads base_wayto_overrides, personal_wayto_overrides, and personal_map_targets from the user's profile via get_settings. Ensures the map is loaded before accessing room data, consistent with other ClassMethods.
personal_map_targets are merged into go2's custom targets, so they are written to go2's settings row whichever script calls this (autostart, at login). See merge_personal_map_targets.
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 |
# File 'documented/common/map/map_base.rb', line 918 def apply_wayto_overrides self.load unless loaded? settings = get_settings base_overrides = settings.base_wayto_overrides || {} personal_overrides = settings.personal_wayto_overrides || {} wayto_overrides = base_overrides.merge(personal_overrides) wayto_overrides.each do |_key, values| next unless values.is_a?(Hash) && values['start_room'] && values['end_room'] start_room_id = values['start_room'].to_i end_room_id = values['end_room'].to_s start_room = list[start_room_id] next unless start_room if values['str_proc'] start_room.wayto[end_room_id] = StringProc.new(values['str_proc'].to_s) end if values['travel_time'] new_timeto = Float(values['travel_time'], exception: false) new_timeto ||= StringProc.new(values['travel_time'].to_s) start_room.timeto[end_room_id] = new_timeto end end personal_map_targets = settings.personal_map_targets merge_personal_map_targets(personal_map_targets) if personal_map_targets.is_a?(Hash) end |
#boundary_rooms_for(ids) ⇒ Array<Integer>
Rooms bordering a set of rooms: every room an exit of the set leads to that is not itself in the set (bigshot's perimeter list). Only the set's own wayto is read, so the cost is the size of the set, not of the map. A room with a one-way edge INTO the set and none back is not a boundary: wayto is player movement, and a room you cannot step out to is not an edge you can hold. Ids the map does not know contribute nothing.
715 716 717 718 719 |
# File 'documented/common/map/map_base.rb', line 715 def boundary_rooms_for(ids) inside = ids.to_a outside = inside.flat_map { |id| (self[id]&.wayto || {}).keys.map(&:to_i) } (outside - inside).uniq.sort end |
#dijkstra(source, destination = nil, static_only: false) ⇒ Array<Hash>? Also known as: dijkstra_hashes
Class-level dijkstra dispatcher
680 681 682 683 684 685 686 687 688 |
# File 'documented/common/map/map_base.rb', line 680 def dijkstra(source, destination = nil, static_only: false) room = source.is_a?(self) ? source : self[source] if room static_only ? room.dijkstra(destination, static_only: true) : room.dijkstra(destination) else echo 'Map.dijkstra: error: invalid source room' nil end end |
#estimate_time(array, from: ESTIMATE_NO_ORIGIN) ⇒ Float
Estimate total travel time along a path
Sums the timeto of each leg between consecutive rooms, treating the
first element as the room the trip starts in: [a, b, c] counts the
a->b and b->c legs. A leg with no timeto (including one from a room
that is gone) counts as 0.2 seconds, and a StringProc timeto is
evaluated.
Room#path_to and Map.findpath return paths that leave the starting
room out, so passing one of those alone drops the first leg (and a
one-step trip estimates 0.0). Name the starting room with from: to
count it:
start = Room.current
path = start.path_to(destination)
Map.estimate_time(path, from: start) if path
Read Room.current once, as above: it changes when the character moves, so a second read can name a different room than the path starts from.
When array already begins with the from: room it is not counted
twice, so a full path is safe to pass with from: as well. Otherwise
the from: room must have an exit (a wayto) to array.first.
That catches a start room off the path, but not one further along a
two-way path, which has an exit back; another reason to read
Room.current once.
The argument is never modified.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'documented/common/map/map_base.rb', line 304 def estimate_time(array, from: ESTIMATE_NO_ORIGIN) self.load unless loaded? unless array.is_a?(Array) raise ArgumentError, 'Map.estimate_time was given something not an array!' end rooms = from.equal?(ESTIMATE_NO_ORIGIN) ? array : estimate_time_path_from(array, from) time = 0.0 rooms.each_cons(2) do |room, next_room| # A path can name a room that is gone. Under the NilClass patch the # lookup yielded nil and the 0.2 default below applied; keep that # without relying on the patch. current = self[room] t = current.nil? ? nil : current.timeto[next_room.to_s] if t time += t.is_a?(StringProc) ? t.call.to_f : t.to_f else time += 0.2 end end time end |
#findpath(source, destination) ⇒ Object
Find path between two rooms
757 758 759 760 761 762 763 764 765 766 |
# File 'documented/common/map/map_base.rb', line 757 def findpath(source, destination) if source.is_a?(self) source.path_to(destination) elsif (room = self[source]) room.path_to(destination) else echo 'Map.findpath: error: invalid source room' nil end end |
#get_free_id ⇒ Integer
Get the next available room ID
182 183 184 185 186 187 188 189 |
# File 'documented/common/map/map_base.rb', line 182 def get_free_id rooms = list.compact # An empty map yields 1, which is what nil.id + 1 produced via Lich's # NilClass patch. Stating it means this no longer depends on that. return 1 if rooms.empty? rooms.max_by(&:id).id + 1 end |
#ids_from_uid(uid) ⇒ Object
Get room IDs from a UID
781 782 783 |
# File 'documented/common/map/map_base.rb', line 781 def ids_from_uid(uid) uids[uid] || [] end |
#json_map_files ⇒ Array<String>
JSON map databases in the data directory, newest first
580 581 582 583 584 585 586 587 588 589 |
# File 'documented/common/map/map_base.rb', line 580 def json_map_files directory = File.join(DATA_DIR, XMLData.game) return [] unless Dir.exist?(directory) Dir.entries(directory) .find_all { |fn| fn =~ /^map-[0-9]+\.json$/i } .collect { |fn| File.join(directory, fn) } .sort .reverse end |
#legacy_map_files ⇒ Array<String>
Legacy map files sitting in the data directory, basenames only
593 594 595 596 597 598 |
# File 'documented/common/map/map_base.rb', line 593 def legacy_map_files directory = File.join(DATA_DIR, XMLData.game) return [] unless Dir.exist?(directory) Dir.entries(directory).grep(/^map(?:-[0-9]+)?\.(?:dat|xml)$/i).sort end |
#load(filename = nil) ⇒ Boolean
Load the newest JSON map database, or a specific file
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'documented/common/map/map_base.rb', line 652 def load(filename = nil) file_list = filename.nil? ? json_map_files : [filename] # An explicitly named .dat or .xml would otherwise reach load_json and # raise a parse error rather than saying why it cannot be loaded. unsupported, file_list = file_list.partition { |fn| fn =~ /\.(?:dat|xml)\z/i } if file_list.empty? if unsupported.empty? respond '--- Lich: error: no map database found' report_unsupported_map_files(legacy_map_files) else report_unsupported_map_files(unsupported.map { |fn| File.basename(fn) }) end return false end while (filename = file_list.shift) return true if load_json(filename) end false end |
#load_json(filename = nil) ⇒ Boolean
Load the newest usable JSON map database, falling back to older candidates when one is unreadable. The two game classes differ only in how a parsed room is constructed and what they announce, so those are hooks: #room_from_json and #map_loaded_message.
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'documented/common/map/map_base.rb', line 489 def load_json(filename = nil) synchronize_load do return true if loaded? file_list = filename ? [filename] : json_map_files if file_list.empty? respond '--- Lich: error: no map database found' return false end while (filename = file_list.shift) next unless File.exist?(filename) next unless parse_map_json(filename) respond (filename) mark_loaded load_uids return true end false end end |
#map_loaded_message(filename) ⇒ String
Announced once a database has loaded. Names the script that triggered the load when there is one; a load can also happen with no script running, and nil.name only survived via Lich's NilClass patch.
556 557 558 559 |
# File 'documented/common/map/map_base.rb', line 556 def (filename) name = Script.current&.name name ? "--- #{name} Map loaded #{filename}" : "--- Map loaded #{filename}" end |
#match_multi_ids(ids) ⇒ Integer?
Narrow a set of candidate ids to the one reachable from the current room
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'documented/common/map/map_base.rb', line 218 def match_multi_ids(ids) # current_room_id can be nil, stale, or point at a hole. Under Lich's # NilClass patch a nil id made Array#[] hand back the whole room list # and the subsequent .wayto then yielded nil, so the result was no # matches either way. Check before indexing rather than relying on it. return nil if current_room_id.nil? current = list[current_room_id] return nil if current.nil? matches = ids.find_all { |s| current.wayto.keys.include?(s.to_s) } return matches[0] if matches.size == 1 nil end |
#match_no_uid ⇒ Object?
Resolve the current room when the game gave no usable uid. Delegates to the game-specific matchers.
206 207 208 209 210 211 212 |
# File 'documented/common/map/map_base.rb', line 206 def match_no_uid if (script = Script.current) set_current(match_current(script)) else set_fuzzy(match_fuzzy) end end |
#normalize_tag_lists(rooms = list) ⇒ nil
Re-wrap plain Array tags as TagList. Rooms that reach the list without going through the constructor, such as a caller assigning a list it built itself, otherwise hold tags that cannot invalidate the index.
Callers inside a load must pass the rooms explicitly, because the #list accessor triggers #load when the map is not yet loaded, and #load holds a non-reentrant mutex.
744 745 746 747 748 749 750 751 752 753 754 |
# File 'documented/common/map/map_base.rb', line 744 def normalize_tag_lists(rooms = list) rooms.compact.each do |room| existing = room. next if existing.is_a?(TagList) # Round trip through the writer, which is what rewraps the plain # Array as a TagList bound to this class. room. = existing end reset_tag_index end |
#parse_map_json(filename) ⇒ Boolean
Returns false when the file was unusable.
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'documented/common/map/map_base.rb', line 515 def parse_map_json(filename) File.open(filename) do |f| JSON.parse(f.read).each do |room| validate_room_json!(room, filename) # Defaulted before the loops below read .keys on them. # Every field except the id is optional in a real mapdb. room['title'] ||= [] room['description'] ||= [] room['paths'] ||= [] room['wayto'] ||= {} room['timeto'] ||= {} room['tags'] ||= [] room['uid'] ||= [] room['wayto'].keys.each do |k| room['wayto'][k] = StringProc.new(room['wayto'][k][3..]) if room['wayto'][k][0..2] == ';e ' end room['timeto'].keys.each do |k| if room['timeto'][k].is_a?(String) && room['timeto'][k][0..2] == ';e ' room['timeto'][k] = StringProc.new(room['timeto'][k][3..]) end end room_from_json(room) end end true rescue StandardError => e # A corrupt or unreadable database must not abort the load or leave a # half-built map behind. Report it, drop whatever was registered, and # let the caller try an older candidate. raw_list because the load # mutex is held and #list would re-enter it. respond "--- Lich: error: failed to load #{filename}: #{e.}" raw_list.clear false end |
#previous_uid ⇒ Integer?
The uid the game last navigated away from
199 200 201 |
# File 'documented/common/map/map_base.rb', line 199 def previous_uid XMLData.previous_nav_rm end |
#reload ⇒ Object
Reload the map database
769 770 771 772 |
# File 'documented/common/map/map_base.rb', line 769 def reload clear load end |
#report_unsupported_map_files(files) ⇒ nil
Explain why an old map database no longer loads. The Marshal (.dat) and XML formats were deprecated for years and support has been removed, so "no map database found" on its own would be misleading for anyone whose data directory still holds one.
606 607 608 609 610 611 612 |
# File 'documented/common/map/map_base.rb', line 606 def report_unsupported_map_files(files) return if files.empty? respond "--- Lich: found map data in a format that is no longer supported: #{files.sort.join(', ')}" respond '--- Lich: download the current JSON map database to continue.' nil end |
#reset_tag_index ⇒ nil
Drop the tag memo. Call after mutating any room's tags in place.
723 724 725 726 727 728 729 730 731 732 733 |
# File 'documented/common/map/map_base.rb', line 723 def reset_tag_index host = tag_cache_host TAG_INDEX_MUTEX.synchronize do # Under the same mutex as publication: bumping the generation outside # it could land between a publisher's validation and its assignment. host.instance_variable_set(:@tag_index_generation, (host.instance_variable_get(:@tag_index_generation) || 0) + 1) host.instance_variable_set(:@tag_index, nil) end nil end |
#rooms_by_tag(tag_name) ⇒ Array<Integer>
Room ids carrying a tag, nearest-agnostic and in room id order
702 703 704 |
# File 'documented/common/map/map_base.rb', line 702 def rooms_by_tag(tag_name) (tag_index[tag_name] || []).dup end |
#save_json(filename = nil) ⇒ void Also known as: save
This method returns an undefined value.
Save the map as a JSON file.
Nothing on disk is touched until the map has serialized, and every file
is published by renaming a fully written temp file over it, so a
failure part way through leaves the existing file as it was. Replacing
an existing file first copies it byte for byte to "
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
# File 'documented/common/map/map_base.rb', line 817 def save_json(filename = nil) filename ||= File.join(DATA_DIR, XMLData.game, "map-#{Time.now.to_i}.json") data = to_json existed = File.exist?(filename) # The rename below would replace a read-only file that the in-place # write refused to open, so keep that refusal explicitly. if existed && !File.writable?(filename) raise map_save_error(Errno::EACCES.new(filename), "could not write #{filename}; existing file left unchanged") end if existed respond 'File exists! Backing it up before proceeding...' begin backup = "#{filename}.bak" # Same refusal for a read-only backup, which the rename would replace. raise Errno::EACCES, backup if File.exist?(backup) && !File.writable?(backup) publish_map_file(backup, mode_of: filename) { |io| IO.copy_stream(filename, io) } rescue StandardError => e raise map_save_error(e, "could not back up #{filename}; map not saved") end end begin publish_map_file(filename, mode_of: filename) { |io| io.write(data) } rescue StandardError => e left = existed ? 'existing file left unchanged' : 'no file created' raise map_save_error(e, "could not write #{filename}; #{left}") end respond "#{filename} saved" end |
#set_current(id) ⇒ Object?
Record the room the game moved to, remembering the one it left
237 238 239 240 241 242 243 |
# File 'documented/common/map/map_base.rb', line 237 def set_current(id) self.previous_room_id = current_room_id if id != current_room_id self.current_room_id = id return nil if id.nil? list[id] end |
#set_fuzzy(id) ⇒ Object?
As #set_current, but a nil id leaves the previous room untouched
248 249 250 251 252 253 254 |
# File 'documented/common/map/map_base.rb', line 248 def set_fuzzy(id) self.previous_room_id = current_room_id if !id.nil? && id != current_room_id self.current_room_id = id return nil if id.nil? list[id] end |
#tag_names ⇒ Array<String>
Tag names present anywhere in the room list, in room id order
695 696 697 |
# File 'documented/common/map/map_base.rb', line 695 def tag_names tag_index.keys end |
#tags ⇒ Array<String>
Tag names present anywhere in the room list
193 194 195 |
# File 'documented/common/map/map_base.rb', line 193 def tag_names end |
#to_json(*args) ⇒ String
Convert the map to a JSON array of its rooms, in id order.
Serializes a compacted copy and leaves the list alone. The list is indexed by room id and a real mapdb has far more unused ids than rooms, so removing its nil gaps in place would move every room after the first gap to the wrong index for every script looking rooms up.
792 793 794 |
# File 'documented/common/map/map_base.rb', line 792 def to_json(*args) list.compact.sort_by(&:id).to_json(args) end |
#uids_add(uid, id) ⇒ Object
Add a UID mapping
775 776 777 778 |
# File 'documented/common/map/map_base.rb', line 775 def uids_add(uid, id) uids[uid] ||= [] uids[uid] << id unless uids[uid].include?(id) end |
#validate_room_json!(room, filename) ⇒ nil
The id is the only field a room cannot do without: it indexes the room into the backing array, and a non-Integer would raise there with a message that says nothing about the database. Everything else is optional and defaulted in #parse_map_json - the shipped mapdb has rooms with no description or paths, such as the fog transitions, so requiring those fields would reject a valid database outright.
571 572 573 574 575 576 |
# File 'documented/common/map/map_base.rb', line 571 def validate_room_json!(room, filename) id = room['id'] return nil if id.is_a?(Integer) raise "#{File.basename(filename)}: room id is not an Integer: #{id.inspect}" end |