Module: Lich::Common::MapBase::InstanceMethods

Defined in:
documented/common/map/map_base.rb

Overview

Instance methods for Room/Map objects

Instance Method Summary collapse

Instance Method Details

#desc ⇒ Object

Deprecated methods for backward compatibility



1204
1205
1206
# File 'documented/common/map/map_base.rb', line 1204

def desc
  @description
end

#dijkstra(destination = nil, static_only: false) ⇒ Array<Hash>? Also known as: dijkstra_hashes

Run Dijkstra's algorithm from this room.

Returns Hashes keyed by room id. These were Arrays indexed by room id until 5.20.0: the conversion allocated two Arrays sized by the highest room id reached, which dominated the cost on a sparse map and gained nothing, since every caller only ever indexes by room id. Access is unchanged - previous and distances read the same either way, and an unreached room still yields nil. #size and #length now count the rooms actually reached rather than the highest id plus one, and iteration yields pairs rather than slots.

Parameters:

  • destination (Integer, Array, nil) (defaults to: nil) —

    Target room(s) or nil for full graph

  • static_only (Boolean) (defaults to: false) —

    use only plain String wayto edges with finite, nonnegative real Numeric weights; never evaluate StringProc weights in this mode. This is route selection, not command validation.

Returns:

  • (Array<Hash>, nil) —

    [previous, distances] keyed by room id, or nil when the search failed



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'documented/common/map/map_base.rb', line 1059

def dijkstra(destination = nil, static_only: false)
  self.class.load unless self.class.loaded?
  source = @id
  visited = {}
  shortest_distances_hash = {}
  previous_hash = {}

  pq = MinHeap.new
  pq.push(0, source)
  shortest_distances_hash[source] = 0

  check_destination = proc do |v, dist|
    case destination
    when Integer
      v == destination
    when Array
      destination.include?(v) && dist < 20
    else
      false
    end
  end

  until pq.empty?
    current_dist, v = pq.pop

    next if visited[v]
    break if check_destination.call(v, current_dist)

    visited[v] = true

    # A wayto edge can name a room that is gone. Under the NilClass
    # patch the enumeration was simply skipped; without it the raise
    # would be rescued below and lose the whole search.
    room = self.class.list[v]
    next if room.nil?

    room.wayto.keys.each do |adj_room|
      adj_room_i = adj_room.to_i
      next if visited[adj_room_i]

      edge_weight = if static_only
                      weight = room.timeto[adj_room]
                      next unless room.wayto[adj_room].instance_of?(String) && weight.is_a?(Numeric) &&
                                  weight.real? && weight.finite? && weight >= 0

                      weight
                    elsif room.timeto[adj_room].is_a?(StringProc)
                      room.timeto[adj_room].call
                    else
                      room.timeto[adj_room]
                    end

      next unless edge_weight

      new_distance = current_dist + edge_weight

      if !shortest_distances_hash[adj_room_i] || shortest_distances_hash[adj_room_i] > new_distance
        shortest_distances_hash[adj_room_i] = new_distance
        previous_hash[adj_room_i] = v
        pq.push(new_distance, adj_room_i)
      end
    end
  end

  [previous_hash, shortest_distances_hash]
rescue StandardError => e
  echo "Map.dijkstra: error: #{e}"
  respond e.backtrace
  nil
end

#find_all_nearest_by_tag(tag_name) ⇒ Array<Integer>

Find all rooms with a specific tag, sorted by distance

Parameters:

  • tag_name (String) —

    Tag to search for

Returns:

  • (Array<Integer>) —

    Room IDs sorted by distance



1178
1179
1180
1181
1182
1183
1184
1185
# File 'documented/common/map/map_base.rb', line 1178

def find_all_nearest_by_tag(tag_name)
  target_list = self.class.rooms_by_tag(tag_name)
  _, shortest_distances = dijkstra
  return [] if shortest_distances.nil?

  target_list.delete_if { |room_num| shortest_distances[room_num].nil? }
  target_list.sort { |a, b| shortest_distances[a] <=> shortest_distances[b] }
end

#find_nearest(target_list) ⇒ Integer?

Find nearest room from a list

Parameters:

  • target_list (Array<Integer>) —

    List of room IDs to search

Returns:

  • (Integer, nil) —

    Nearest room ID



1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
# File 'documented/common/map/map_base.rb', line 1190

def find_nearest(target_list)
  target_list = target_list.collect(&:to_i)
  if target_list.include?(@id)
    @id
  else
    _, shortest_distances = dijkstra(target_list)
    return nil if shortest_distances.nil?

    valid_rooms = target_list.select { |room_num| shortest_distances[room_num].is_a?(Numeric) }
    valid_rooms.min_by { |room_num| shortest_distances[room_num] }
  end
end

#find_nearest_by_tag(tag_name) ⇒ Integer?

Find nearest room with a specific tag

Parameters:

  • tag_name (String) —

    Tag to search for

Returns:

  • (Integer, nil) —

    Room ID of nearest tagged room



1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'documented/common/map/map_base.rb', line 1164

def find_nearest_by_tag(tag_name)
  target_list = self.class.rooms_by_tag(tag_name)
  return @id if target_list.include?(@id)

  _, shortest_distances = dijkstra(target_list)
  return nil if shortest_distances.nil?

  target_list.delete_if { |room_num| shortest_distances[room_num].nil? }
  target_list.sort { |a, b| shortest_distances[a] <=> shortest_distances[b] }.first
end

#geo ⇒ nil

Returns geographic/zone data for this room.

This method is a placeholder for game-specific geographic information and always returns nil in the base implementation. Subclasses may override to provide game-specific geographic data.

Returns:

  • (nil)


1262
1263
1264
# File 'documented/common/map/map_base.rb', line 1262

def geo
  nil
end

#inside? ⇒ Boolean

Check if room is indoors

Returns:

  • (Boolean) —

    true if room is indoors



1002
1003
1004
# File 'documented/common/map/map_base.rb', line 1002

def inside?
  !outside?
end

#inspect ⇒ Object

Inspect room details



1007
1008
1009
1010
1011
# File 'documented/common/map/map_base.rb', line 1007

def inspect
  instance_variables.collect do |var|
    "#{var}=#{instance_variable_get(var).inspect}"
  end.join("\n")
end

#json_extra_fields ⇒ Object

Override in subclasses to add game-specific fields to JSON output. Must return a Hash. Nil/empty values are filtered automatically.



1015
1016
1017
# File 'documented/common/map/map_base.rb', line 1015

def json_extra_fields
  {}
end

#map_name ⇒ String?

Deprecated.

Use @image instead

Returns the name of the map image associated with this room.

Deprecated: use the @image field directly.

Returns:

  • (String, nil) —

    the image name, or nil if not set



1214
1215
1216
# File 'documented/common/map/map_base.rb', line 1214

def map_name
  @image
end

#map_roomsize ⇒ Integer?

Deprecated.

Use @image_coords instead

Returns the width of this room's bounding box on the map image.

Calculated as the difference between right and left edges. Deprecated: use @image_coords directly.

Returns:

  • (Integer, nil) —

    the room width in pixels, or nil if @image_coords is not set



1251
1252
1253
1254
1255
# File 'documented/common/map/map_base.rb', line 1251

def map_roomsize
  return nil if @image_coords.nil?

  image_coords[2] - image_coords[0]
end

#map_x ⇒ Integer?

Deprecated.

Use @image_coords instead

Returns the x-coordinate (horizontal center) of this room on its map image.

Calculated as the midpoint between the left and right edges of the room's bounding box. Deprecated: use @image_coords directly.

Returns:

  • (Integer, nil) —

    the center x-coordinate, or nil if @image_coords is not set



1225
1226
1227
1228
1229
# File 'documented/common/map/map_base.rb', line 1225

def map_x
  return nil if @image_coords.nil?

  ((image_coords[0] + image_coords[2]) / 2.0).round
end

#map_y ⇒ Integer?

Deprecated.

Use @image_coords instead

Returns the y-coordinate (vertical center) of this room on its map image.

Calculated as the midpoint between the top and bottom edges of the room's bounding box. Deprecated: use @image_coords directly.

Returns:

  • (Integer, nil) —

    the center y-coordinate, or nil if @image_coords is not set



1238
1239
1240
1241
1242
# File 'documented/common/map/map_base.rb', line 1238

def map_y
  return nil if @image_coords.nil?

  ((image_coords[1] + image_coords[3]) / 2.0).round
end

#outside? ⇒ Boolean

Check if room is outdoors Works for both GemStone and DragonRealms:

  • "Obvious paths:" indicates outdoor
  • "Obvious exits:" indicates indoor

Returns:

  • (Boolean) —

    true if room is outdoors



994
995
996
997
998
# File 'documented/common/map/map_base.rb', line 994

def outside?
  return false if @paths.nil? || @paths.empty?

  @paths.last =~ /^Obvious paths:/ ? true : false
end

#path_to(destination) ⇒ Array<Integer>?

Find path from this room to destination

Parameters:

  • destination (Integer) —

    Target room ID

Returns:

  • (Array<Integer>, nil) —

    Array of room IDs representing rooms to traverse (excluding source, including destination)



1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'documented/common/map/map_base.rb', line 1136

def path_to(destination)
  self.class.load unless self.class.loaded?
  destination = destination.to_i
  previous, = dijkstra(destination)
  # dijkstra returns nil when the search itself failed.
  return nil if previous.nil?
  return nil unless previous[destination]

  path = [destination]
  seen = { destination => true }
  until previous[path[-1]] == @id
    step = previous[path[-1]]
    # A chain that never reaches this room is not a usable path. The
    # hash yields nil for a missing predecessor, and a chain that
    # revisits a room is a cycle; either would loop forever. Dijkstra
    # should not produce either, but path_to is a public entry point and
    # dijkstra is overridable.
    return nil if step.nil? || seen[step]

    seen[step] = true
    path.push(step)
  end
  path.reverse
end

#tags=(value) ⇒ nil

Replace this room's tags and drop the tag index

Parameters:

  • value (Array, nil) —

    new tag names

Returns:

  • (nil) —

    Ruby ignores a writer's return value, so room.tags = x evaluates to x regardless of what this returns



979
980
981
982
# File 'documented/common/map/map_base.rb', line 979

def tags=(value)
  @tags = TagList.new(value, self.class)
  self.class.reset_tag_index
end

#to_i ⇒ Object

Convert room to integer (room ID)



985
986
987
# File 'documented/common/map/map_base.rb', line 985

def to_i
  @id
end

#to_json(*_args) ⇒ Object

Convert room to JSON



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'documented/common/map/map_base.rb', line 1020

def to_json(*_args)
  mapjson = {
    id: @id,
    title: @title,
    description: @description,
    paths: @paths,
    location: @location,
    climate: @climate,
    terrain: @terrain,
    wayto: @wayto&.sort_by { |k, _v| k.to_i }&.to_h,
    timeto: @timeto&.sort_by { |k, _v| k.to_i }&.to_h,
    image: @image,
    image_coords: @image_coords,
    tags: @tags&.sort_by { |tag| [tag.downcase, tag] },
    check_location: @check_location,
    unique_loot: @unique_loot,
    uid: @uid
  }
  mapjson.merge!(json_extra_fields)
  mapjson.delete_if { |_a, b| b.nil? || (b.is_a?(Array) && b.empty?) }
  JSON.pretty_generate(mapjson)
end