Module: Lich::Common::MapBase::ClassMethods
- Defined in:
- documented/common/map/map_base.rb
Instance Method Summary collapse
-
#apply_wayto_overrides ⇒ void
Applies overrides for wayto settings based on user preferences.
-
#dijkstra(source, destination = nil) ⇒ Array
Computes the shortest path from a source room to a destination room using Dijkstra's algorithm.
-
#estimate_time(array) ⇒ Float
Estimates the time required to traverse a given array of rooms.
-
#findpath(source, destination) ⇒ Array<Integer>?
Finds the path from a source room to a destination room.
-
#get_free_id ⇒ Integer
Retrieves the next available ID for a new map entry.
- #ids_from_uid(uid) ⇒ Object
-
#load_dat(filename = nil) ⇒ Boolean
Loads map data from a .dat file.
-
#reload ⇒ void
Reloads the map data from the source.
-
#save_json(filename = nil) ⇒ void
(also: #save)
Saves the current map data to a JSON file.
- #to_json(*args) ⇒ Object
- #uids_add(uid, id) ⇒ Object
Instance Method Details
#apply_wayto_overrides ⇒ void
This method returns an undefined value.
Applies overrides for wayto settings based on user preferences.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'documented/common/map/map_base.rb', line 240 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 if personal_map_targets.is_a?(Hash) custom_targets = (GameSettings['custom targets'] || {}) custom_targets.merge!(personal_map_targets) GameSettings['custom targets'] = custom_targets end end |
#dijkstra(source, destination = nil) ⇒ Array
Computes the shortest path from a source room to a destination room using Dijkstra's algorithm.
122 123 124 125 126 127 128 129 130 131 |
# File 'documented/common/map/map_base.rb', line 122 def dijkstra(source, destination = nil) if source.is_a?(self) source.dijkstra(destination) elsif (room = self[source]) room.dijkstra(destination) else echo 'Map.dijkstra: error: invalid source room' nil end end |
#estimate_time(array) ⇒ Float
Estimates the time required to traverse a given array of rooms.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'documented/common/map/map_base.rb', line 98 def estimate_time(array) self.load unless loaded? unless array.is_a?(Array) raise Exception.exception('MapError'), 'Map.estimate_time was given something not an array!' end time = 0.0 until array.length < 2 room = array.shift t = self[room].timeto[array.first.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) ⇒ Array<Integer>?
Finds the path from a source room to a destination room.
137 138 139 140 141 142 143 144 145 146 |
# File 'documented/common/map/map_base.rb', line 137 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
Retrieves the next available ID for a new map entry.
89 90 91 92 |
# File 'documented/common/map/map_base.rb', line 89 def get_free_id self.load unless loaded? list.compact.max_by(&:id).id + 1 end |
#ids_from_uid(uid) ⇒ Object
160 161 162 |
# File 'documented/common/map/map_base.rb', line 160 def ids_from_uid(uid) uids[uid] || [] end |
#load_dat(filename = nil) ⇒ Boolean
Loads map data from a .dat file.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 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 |
# File 'documented/common/map/map_base.rb', line 198 def load_dat(filename = nil) respond '--- WARNING: Map.load_dat (Marshal .dat format) is deprecated. Use Map.load_json instead.' synchronize_load do return true if loaded? file_list = if filename.nil? Dir.entries(File.join(DATA_DIR, XMLData.game)) .find_all { |fn| fn =~ /^map-[0-9]+\.dat$/ } .collect { |fn| File.join(DATA_DIR, XMLData.game, fn) } .sort .reverse else respond "--- file_list = #{filename.inspect}" [filename] end if file_list.empty? respond '--- Lich: error: no map database found' return false end while (filename = file_list.shift) begin self.list = File.open(filename, 'rb') { |f| Marshal.load(f.read) } respond "--- Map loaded #{filename}" mark_loaded load_uids return true rescue StandardError => e if file_list.empty? respond "--- Lich: error: failed to load #{filename}: #{e}" else respond "--- warning: failed to load #{filename}: #{e}" end end end false end end |
#reload ⇒ void
This method returns an undefined value.
Reloads the map data from the source.
150 151 152 153 |
# File 'documented/common/map/map_base.rb', line 150 def reload clear load end |
#save_json(filename = nil) ⇒ void Also known as: save
This method returns an undefined value.
Saves the current map data to a JSON file.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'documented/common/map/map_base.rb', line 172 def save_json(filename = nil) filename ||= File.join(DATA_DIR, XMLData.game, "map-#{Time.now.to_i}.json") if File.exist?(filename) respond 'File exists! Backing it up before proceeding...' begin File.open(filename, 'rb') do |infile| File.open("#{filename}.bak", 'wb:UTF-8') do |outfile| outfile.write(infile.read) end end rescue StandardError => e respond "--- Lich: error: #{e}\n\t#{e.backtrace[0..1].join("\n\t")}" Lich.log "error: #{e}\n\t#{e.backtrace.join("\n\t")}" end end File.open(filename, 'wb:UTF-8') { |file| file.write(to_json) } respond "#{filename} saved" # Reload if map index appears corrupted reload if self[-1].id != self[self[-1].id].id end |
#to_json(*args) ⇒ Object
164 165 166 167 |
# File 'documented/common/map/map_base.rb', line 164 def to_json(*args) list.delete_if(&:nil?) list.sort_by(&:id).to_json(args) end |
#uids_add(uid, id) ⇒ Object
155 156 157 158 |
# File 'documented/common/map/map_base.rb', line 155 def uids_add(uid, id) uids[uid] ||= [] uids[uid] << id unless uids[uid].include?(id) end |