-
Notifications
You must be signed in to change notification settings - Fork 3
/
metamap.lic
698 lines (620 loc) · 24.2 KB
/
metamap.lic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
237
238
239
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
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
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
=begin
Metamap is a tool for setting and updating map metadata. This metadata is used by xnarost (and possibly other tools)
to assign friendly names and categories to individual maps.
For each map, there should be one (and preferably only one) room with the following tags:
meta:mapname:NAME
REQUIRED. Specifies the 'long' name of the map, as shown in XNarost's list of all maps.
meta:mapshortname:NAME
OPTIONAL. Specifies the 'short' name of the map, as shown in XNarost's list of maps within a single category.
If omitted, this is the same as the longer name above.
meta:mapcategory:CATEGORY or meta:mapcategory:CATEGORY:SHORTNAME
OPTIONAL. Specifies which category this map belongs under and (optionally) its name within that category.
Maps without this tag will not be categorized.
This tag may be specified multiple times to specify multiple categories.
author: LostRanger ([email protected])
game: any
tags: core, movement
version: 0.1 (2019-08-14)
changelog:
version 0.1
* Initial release
=end
module MetaMapScript
class Metadata
attr_reader :name, :shortname, :categories, :id
attr_reader :rooms, :metadata_rooms
@@cache = {}
def Metadata.image_to_id(image)
if image =~ /^(.+)\.[^.]*$/
image = $1
end
if image =~ /^(.+)-\d+$/
return $1
end
return image
end
def Metadata.unload
@@cache.clear
end
def Metadata.load_all
unload
Room.list.each do |room|
next unless room.image
id = image_to_id(room.image)
(@@cache[id] or self.new(id)).ingest_room(room)
end
end
def Metadata.all
Metadata.load_all
return @@cache.values
end
def Metadata.sorted
return all.sort_by{|x| x.id.downcase}
end
def Metadata.[](id)
id = image_to_id(id)
map = @@cache[id]
return map if map
Room.list.each do |room|
next unless room.image
next unless image_to_id(room.image) == id
map ||= self.new(id)
map.ingest_room(room)
end
return map
end
def initialize(id)
@id = id
@@cache[id] = self
@primary_room = nil
@rooms = []
@metadata_rooms = Set.new
@name = nil
@shortname = nil
@categories = {}
end
def ingest_room(room)
@rooms << room
return unless room.tags
room.tags.each do |tag|
if tag =~ /^meta:mapname:(.*)$/
@metadata_rooms << room
@name ||= $1
elsif tag =~ /^meta:mapshortname:(.*)$/
@metadata_rooms << room
@shortname ||= $1
elsif tag =~ /^meta:mapcategory:(.*?)(?::(.*))?$/
@metadata_rooms << room
unless @categories.include?($1)
@categories[$1] = $2
end
end
end
end
def primary_room
return @primary_room if @primary_room
tagged_rooms = {
'town' => [],
'voln' => [],
'supernode' => [],
'node' => [],
}
@rooms.each do |room|
next unless room.tags
room.tags.each do |tag|
t = tagged_rooms[tag]
next unless t
t << room
end
end
best_untagged_room = nil
tagged_rooms.each do |tag, rooms|
next if rooms.length == 0
room = rooms.find{|rm| rm.tags.find{|tag| tag =~ /^meta:map(?:name|shortname|category):/}}
return @primary_room = room if room
best_untagged_room ||= rooms[0]
end
@metadata_rooms.each do |room|
return @primary_room = room
end
# return @primary_room = @metadata_rooms[0] if @metadata_rooms.length > 0
return @primary_room = best_untagged_room if best_untagged_room
return @primary_room = @rooms[0]
end
def condense(target=nil)
if target
@primary_room = target
else
target = self.primary_room
end
return target if @metadata_rooms.length == 1 and @metadata_rooms.to_a[0] == target # Nothing to condense.
meta_tags = Set.new
@metadata_rooms.each do |room|
next unless room.tags
new_tags = []
room.tags.each do |tag|
if tag =~ /^meta:map(?:name|shortname|category):/
meta_tags << tag
else
new_tags << tag
end
end
if new_tags.length
room.tags = new_tags
else
room.tags = nil
end
end
target.tags ||= []
target.tags += meta_tags.to_a
@metadata_rooms = Set.new
@metadata_rooms << target
return target
end
def _set_name(prefix, value)
tag = "#{prefix}#{value}" if value
target = condense
if target.tags
target.tags.delete_if{|x| x.start_with?(prefix)}
target.tags << tag if value
else
target.tags = tag if value
end
return value
end
def name=(v); @name = _set_name("meta:mapname:", v); end
def shortname=(v); @shortname = _set_name("meta:mapshortname:", v); end
def add_category(category, shortname=nil)
target = condense
prefix = "meta:mapcategory:#{category}"
tag = prefix.dup
tag += ":#{shortname}" if shortname
if target.tags
target.tags.delete_if{|x| x == prefix or x.start_with?("#{prefix}:")}
target.tags << tag
else
target.tags = tag
end
return shortname
end
def del_category(category)
target = condense
prefix = "meta:mapcategory:#{category}"
if target.tags
target.tags.delete_if{|x| x == prefix or x.start_with?("#{prefix}:")}
end
end
end
VERSION = '0.1 (2019-08-14)'
VERSION_INT = 1_000
def self.send_formatted(msg, mono=true)
if @stormfront
msg = REXML::Text.new(msg, respect_whitespace: true).to_s
msg.gsub!(/\*\*(.*?)\*\*/, '<preset id="whisper">\1</preset>')
msg = "<output class=\"mono\" />\n#{msg}\n<output class=\"\" />" if mono
puts msg
else
msg.gsub!('**', '')
respond msg
end
end
def self.send_help_formatted(msg)
if @stormfront
msg = REXML::Text.new(msg, respect_whitespace: true).to_s
msg.gsub!(/\*\*(.*?)\*\*/, '<preset id="whisper">\1</preset>')
msg.gsub!(/`(.*?)`/, '<preset id="speech">\1</preset>')
msg.gsub!("#{name} help", "</preset><d cmd=\"#{name} help\">#{name} help</d><preset id=\"whisper\">")
puts "<output class=\"mono\" />\n#{msg}\n<output class=\"\" />"
else
msg.gsub!('**', '')
msg.gsub!('`', '')
respond msg
end
end
def self.show_changelog
prev = GameSettings[:current_version] || 0
changelog = {
# 1_000 => [
# "Major version 0.1 changes:",
# '',
# " * Initial release.",
# ],
}
if prev != VERSION_INT
msg = []
msg << "<preset id='speech'>--------------------</preset>"
msg << "<preset id='speech'>Welcome to #{$lich_char}#{@script.name}</preset> <preset id='whisper'>#{VERSION}</preset>"
msg << ''
changelog.each do |version, notes|
if version > prev or version == VERSION_INT
msg << "<preset id='whisper'>#{notes[0]}</preset>"
msg += notes[1..-1]
msg << ''
end
end
msg << '<output class="mono" />'
msg << "For a more detailed changelog: <d>#{$lich_char}repo info #{@script.name}</d>."
msg << "To enable automatic updates: <d>#{$lich_char}repo set-updatable #{@script.name}</d>`."
msg << "For details on new features: <d>#{$lich_char}#{@script.name} help</d>"
#msg << " or visit: #{HELP_URL}"
msg << "For more LostRanger scripts: https://bit.ly/lostranger-scripts"
msg << '<output class="" />'
msg << ''
msg << "This message will only be displayed once unless a new version is downloaded."
msg << "<preset id='speech'>--------------------</preset>"
send msg
GameSettings[:current_version] = VERSION_INT
sleep 1.5
end
end
def self.send(what)
if @stormfront
return _respond(what)
end
unless what.is_a?(Array)
what = [what]
end
return _respond what.map{|line|
if line =~ /^\s*<\/?output[^>]*>$/
nil
else
line = line.gsub(/<preset.*?>(.*?)<\/preset>/, "\\1")
line.gsub!('<pushBold/>', monsterbold_start)
line.gsub!('<popBold/>', monsterbold_end)
line.gsub!(/<\/?d.*?>/, '')
line
end
}.keep_if{|x| x}
end
def self.show_help
name = "#{$lich_char}#{@script.name}"
msg = []
msg << "**This is #{@script.name} version #{VERSION}**"
msg << "**Information:**"
# msg << " `#{name} reload`"
# msg << " Reload all map data to reflect any changes made."
msg << " `#{name} maps`"
msg << " List all known maps."
msg << " `#{name} cats`"
msg << " List all known categories."
msg << " `#{name} categories`"
msg << " List all known categories."
msg << ""
msg << "**Consistency Checks:**"
msg << " `#{name} check`"
msg << " Check metadata consistency for the current map."
msg << " `#{name} check` **MAPID**"
msg << " Check metadata consistency for the specified map ID."
msg << " `#{name} check` **ROOM#**"
msg << " Check metadata consistency for the map associated with the specified room#"
msg << " `#{name} check all`"
msg << " Check metadata consistency for ALL maps."
msg << ""
msg << "**Map Metadata Updates:**"
msg << " These commands all target the current room's map by default. You can override this by preceding the command with @<ROOM#> or @<MAPID>"
msg << " e.g. `#{name} @288 name Wehnimer's Landing - Town Map`"
msg << " or `#{name} @wl-wehnimers name Wehnimer's Landing - Town Map`"
msg << ""
msg << " **NOTE: **Metadata updates will condense all meta tags for the map into a single room if they are not already."
msg << ""
msg << " `#{name} name` **NAME**"
msg << " Set the name of the current map. This is shown in the list of all maps."
msg << " `#{name} short` **SHORTNAME**"
msg << " Set the 'short name' of the current map. This is shown within categorized lists. You may use 'none' to remove an existing shortname."
msg << " `#{name} add` **CATEGORY**"
msg << " Adds this map to the CATEGORY category, using its default short name."
msg << " `#{name} add` **CATEGORY**`:`**SHORTNAME**"
msg << " Adds this map to the CATEGORY category, using the specified short name."
msg << " `#{name} remove` **CATEGORY**"
msg << " Removes this map from the CATEGORY category. `remove` may be shortened to `rem` or `rm`"
msg << " `#{name} target` [**ROOM#**]"
msg << " Moves metadata for this map to the specified ROOM#, which must use this map. If ROOM# is omitted, the current room is used."
# msg << ""
# msg << "**Categories:**"
# msg << " `#{name} rename` **OLD**`;`**NEW**"
# msg << " Rename the category named OLD to NEW."
# msg << " `#{name} merge` **OLD**`;`**NEW**"
# msg << " Merge contents of category OLD with NEW."
# msg << " `#{name} delete` **CATEGORY**"
# msg << " Delete category CATEGORY."
return send_help_formatted(msg.join("\n"))
end
def self.get_map(what=nil)
if what
if what =~ /^\d+$/
rm = Room[what]
unless rm
echo "Room ##{what} does not exist."
exit
end
unless rm.image
echo "Room ##{what} has no map image."
exit
end
what = rm.image
end
else
rm = Room.current
unless rm.id
echo "Current room does not appear to be in the map database."
exit
end
unless rm.image
echo "Current room ##{what} has no map image."
exit
end
what = rm.image
end
result = Metadata[what]
unless result
echo "Map '#{what}' not found."
exit
end
return result
end
def self.display_table(table, nil_as='-')
widths = {}
rightalign = {}
table.each do |row|
row.each do |k, v|
widths[k] ||= k.length
if v.nil?
v = nil_as
elsif v.is_a?(Numeric)
row[k] = v = v.to_s
rightalign[k] = true
end
l = v.length
widths[k] = l if widths[k] < l
end
end
header = []
separator = []
widths.each do |k,v|
if rightalign[k]
header << k.rjust(v)
else
header << k.ljust(v)
end
separator << '-'*v
end
header = header.join(' | ')
separator = separator.join('-|-')
output = [header, separator]
table.each do |row|
output << (row.map{|k,v|
w = widths[k]
next v.rjust(w) if rightalign[k]
next v.ljust(w)
}.join(' | ').rstrip)
end
output << separator
output << header
respond output
end
def self.do_check(what)
if what
if what.downcase == 'all'
@target_map = nil
else
@target_map = get_map(what)
end
else
@target_map ||= get_map
end
if @target_map
maps = [@target_map]
else
maps = Metadata.sorted
end
msg = []
one = (maps.length == 1)
maps.each do |map|
prefix = "[#{map.id}]"
msg << "#{prefix} rooms: #{map.rooms.length}; primary: #{map.primary_room.id or 'NONE'}" if one
n = map.metadata_rooms.length
if n == 0
msg << "#{prefix} no metadata defined."
next
end
if map.name.nil?
msg << "#{prefix} map has no name."
end
if n == 2
msg << "#{prefix} metadata is defined on multiple rooms: #{map.metadata_rooms.map(&:id).join(', ')}"
msg << "#{prefix} target a single room using: #{$lich_char}#{@script.name} @#{map.id} target ROOM"
elsif one
msg << "#{prefix} metadata is defined on room: #{map.metadata_rooms.map(&:id).join(', ')}"
end
if one
msg << "#{prefix} name: #{map.name}"
msg << "#{prefix} shortname: #{map.shortname or '(undefined)'}"
if map.categories.length == 0
msg << "#{prefix} has no defined categories!"
else
categories = map.categories.map{|k, v|
next(k) unless v
next("#{k} (#{v})")
}.join('; ')
msg << "#{prefix} categories (and shortnames): #{categories}"
end
end
names = Set.new
name_count = 0
shortnames = Set.new
shortname_count = 0
category_names = {}
category_counts = {}
map.metadata_rooms.each{|room|
next unless room.tags
room.tags.each{|x|
if x =~ /^meta:mapname:(.*)$/
names << $1
name_count += 1
elsif x =~ /^meta:mapshortname:(.*)$/
shortnames << $1
shortname_count += 1
elsif x =~ /^meta:mapcategory:(.*?)(?::(.*))?$/
category_names[$1] ||= Set.new
category_names[$1] << $2
category_counts[$1] ||= 0
category_counts[$1] += 1
end
}
}
if name_count > 1
msg << "#{prefix} name is declared #{name_count} times instead of just once."
if names.length > 1
msg << "#{prefix} has conflicting name declarations: #{names.to_a.join('; ')}"
end
msg << "#{prefix} fix this using: #{$lich_char}#{@script.name} @#{map.id} name NEWNAME"
end
if shortname_count > 1
msg << "#{prefix} shortname is declared #{shortname_count} times instead of just once."
if shortnames.length > 1
msg << "#{prefix} has conflicting shortname declarations: #{shortnames.to_a.join('; ')}"
end
msg << "#{prefix} fix this using: #{$lich_char}#{@script.name} @#{map.id} shortname NEWNAME"
end
category_counts.each do |cat, count|
next unless count > 1
msg << "#{prefix} category '#{cat}' is mentioned #{count} times instead of just once."
n = category_names[cat]
if n.length > 1
msg << "#{prefix} has conflicting category '#{cat}' shortname declarations: #{n.to_a.map{|k| k or 'DEFAULT'}.join('; ')}"
end
msg << "#{prefix} fix this using: #{$lich_char}#{@script.name} @#{map.id} add CATEGORY"
msg << "#{prefix} or using: #{$lich_char}#{@script.name} @#{map.id} add CATEGORY:SHORTNAME"
end
end
msg << "Total maps: #{maps.length}" unless one
respond msg
end
def self.run(script)
before_dying { Metadata.unload }
@stormfront = !$fake_stormfront
@script = script
@script.want_downstream = false
@script.want_downstream_xml = false
@target_map = nil
cmdline = script.vars[0].strip || ''
unless cmdline =~ /^(?:@(?:(\d+)|([^\s]+))\s+?)?(?!@)(.+)/
echo "For help, see #{$lich_char}#{script.name} help"
exit
end
cmdline = $3.strip
if $1
@target_map = get_map($1)
elsif $2
@target_map = get_map($2)
else
@target_map = nil
end
if cmdline =~ /^help$/i
show_help
exit
elsif cmdline =~ /^reload$/i
Metadata.unload
echo "Metadata unloaded."
exit
elsif cmdline =~ /^maps$/i
table = []
Metadata.sorted.each do |map|
table << {
'KEY' => map.id,
'NAME' => map.name || '(none)',
'SHORTNAME' => map.shortname || '(none)',
'PRIMARY' => (map.primary_room.id) || '???',
'#RMS' => map.rooms.length,
'#META' => map.metadata_rooms.length,
}
end
display_table(table)
exit
elsif cmdline =~ /^cats$|^categories$/i
categories = {}
Metadata.all.each do |map|
map.categories.each{|cat, _| categories[cat] ||= 0; categories[cat] += 1}
end
table = categories.map{|k,v| {'#MAPS' => v, 'NAME' => k}}
table.sort_by!{|row| row['NAME']}
display_table(table)
exit
elsif cmdline =~ /^name(?:\s+(.*))?$/i
@target_map ||= get_map
if $1
@target_map.name = $1
echo "Map #{@target_map.id} name is now '#{$1}'"
elsif @target_map.name
echo "Map #{@target_map.id} name is #{@target_map.name}."
else
echo "Map #{@target_map.id} has no name."
end
elsif cmdline =~ /^short(?:name)?(?:\s+(.*))?$/i
@target_map ||= get_map
if $1
if $1.downcase == 'none'
@target_map.shortname = nil
echo "Map #{@target_map.id} shortname deleted."
else
@target_map.shortname = $1
echo "Map #{@target_map.id} shortname is now '#{$1}'"
end
elsif @target_map.shortname
echo "Map #{@target_map.id} shortname is #{@target_map.shortname}."
else
echo "Map #{@target_map.id} has no shortname."
end
elsif cmdline =~ /^add\s+(.+?)(?::(.*))?$/i
@target_map ||= get_map
category = $1
shortname = $2
shortname = nil if shortname == ''
# Ensure that the category exists.
unless Metadata.all.find{|map| map.categories.include?($1)}
echo "Category '#{$1}' does not yet exist -- i.e. it is not referenced by any other maps."
echo "To continue anyways and add this category, unpause this script. Otherwise, kill this script."
pause_script
end
@target_map.add_category(category, shortname)
if shortname
echo "Map #{@target_map.id} added to category '#{category}' using shortname '#{shortname}'."
else
echo "Map #{@target_map.id} added to category '#{category}' using its default shortname."
end
exit
elsif cmdline =~ /^(?:rm|rem|remove)\s+(.+?)(?::(.*))?$/i
@target_map ||= get_map
category = $1
if @target_map.categories.include?(category)
@target_map.del_category(category)
echo "Map #{@target_map.id} removed from category '#{category}'."
else
echo "Map #{@target_map.id} is not in category '#{category}'."
end
exit
elsif cmdline =~ /^target(?:\s+(\d+))?$/i
@target_map ||= get_map
if $1
roomid = $1.to_i
else
roomid = Room.current.id
unless roomid
echo "The current room is not in the map database."
exit
end
end
unless @target_map.rooms.find{|x| x.id == roomid}
echo "Room ##{roomid} is not on the #{@target_map.id} map."
exit
end
@target_map.condense(Room[roomid])
echo "Metadata for map #{@target_map.id} moved to room ##{roomid}."
do_check(@target_map.id)
elsif cmdline =~ /^check(?:\s+(.+))?$/i
do_check($1)
end
# echo @target_map.inspect
end
end
MetaMapScript.run(script)