-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessors.rb
More file actions
771 lines (703 loc) · 26 KB
/
processors.rb
File metadata and controls
771 lines (703 loc) · 26 KB
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
VERSION='2.30'
DEFAULT_STATE = [:default, :lineblob, :roomname_for_in_room, :exitname_for_on_exit]
class InputStateMachine
def first_stage_mode_default(lineblob)
case lineblob.last
when /^IN\s+(".*"(?:[^->\s]\S*)?)\s*$/
[:in_room, lineblob, $1, nil]
when /^ON\s+(".*")\s+FROM\s+(".*"(?:[^->\s]\S*)?)\s*$/
[:on_exit, lineblob, $1, $2]
when /^ENDIN\s*$/
[:error, lineblob, 'ENDIN outside of IN-block.', nil]
when /^ENDON\s*$/
[:error, lineblob, 'ENDON outside of ON-block.', nil]
else
false # Don't change mode
end
end
def first_stage_mode_on_exit(lineblob)
case lineblob.last
when /^ENDON/ then DEFAULT_STATE
when /^ENDIN/ then [:error, lineblob, 'ENDIN inside of ON-block.', nil]
else false
end
end
def first_stage_mode_in_room(lineblob)
case lineblob.last
when /^ENDIN/ then DEFAULT_STATE
when /^ENDON/ then [:error, lineblob, 'ENDON inside of IN-block.', nil]
else false
end
end
# Transform known mode-lineblob pairs into opcodes
def second_stage_mode_default(state, lineblob)
case lineblob.last
when /^\s*$/
[[:NOP]]
when /^#.*$/
[[:NOP]]
when /^ATTR BASE:\s*(.*)$/
[[:ATTR_BASE, $1]]
when /^ALIAS\s*:?\s*(".*")\s*"(.*)"\s*$/i
[[:ALIAS, $1, $2]]
when /^REVERSE\s*:?\s*(".*")\s*(".*")\s*$/i
[[:REVERSE, $1, $2]]
when /^ROOM PARENT:\s*$/
[[:ROOM_PARENT, nil, nil]]
when /^ROOM PARENT:\s*(#\d+)\s*$/
[[:ROOM_PARENT, $1, :raw]]
when /^ROOM PARENT:\s*(".*"(?:[^->\s]\S*)?)\s*$/
[[:ROOM_PARENT, $1, :id]]
when /^ROOM ZONE:\s*$/
[[:ROOM_ZONE, nil, nil]]
when /^ROOM ZONE:\s*(#\d+)\s*$/
[[:ROOM_ZONE, $1, :raw]]
when /^ROOM ZONE:\s*(".*"(?:[^->\s]\S*)?)\s*$/
[[:ROOM_ZONE, $1, :id]]
when /^ROOM FLAGS:\s*$/
[[:ROOM_FLAGS, nil]]
when /^ROOM FLAGS:\s*(.+)\s*$/
[[:ROOM_FLAGS, $1]]
when /^EXIT PARENT:\s*$/
[[:EXIT_PARENT, nil, nil]]
when /^EXIT PARENT:\s*(#\d+)\s*$/
[[:EXIT_PARENT, $1, :raw]]
when /^EXIT PARENT:\s*(".*"(?:[^->\s]\S*)?)\s*$/
[[:EXIT_PARENT, $1, :id]]
when /^EXIT ZONE:\s*$/
[[:EXIT_ZONE, nil, nil]]
when /^EXIT ZONE:\s*(#\d+)\s*$/
[[:EXIT_ZONE, $1, :raw]]
when /^EXIT ZONE:\s*(".*"(?:[^->\s]\S*)?)\s*$/
[[:EXIT_ZONE, $1, :id]]
when /^EXIT FLAGS:\s*$/
[[:EXIT_FLAGS, nil]]
when /^EXIT FLAGS:\s*(.+)\s*$/
[[:EXIT_FLAGS, $1]]
when /^PREBUILT[\s:]\s*(".*?"(?:[^->\s]\S*)?)\s*=\s*(#\d+)\s*$/
[[:PREBUILT_ROOM, $1, $2]]
when /^(".*?")\s*:\s*((".*?"(?:[^->\s]\S*)?)(\s*(<?->)\s*(".*?"(?:[^->\s]\S*)?))+)\s*$/
exitname, roomstring, lastroom = $1, $2, $3
commands = [[:CREATE_ROOM, lastroom]]
roomstring.scan(/\s*(<?->)\s*(".*?"(?:[^->\s]\S*)?)/).each {|match|
commands.push([:CREATE_ROOM, match[1]])
commands.push([:CREATE_EXIT, exitname, lastroom, match[1]])
commands.push([:CREATE_REVERSE_EXIT, exitname, lastroom, match[1]]) if match[0] == "<->"
lastroom = match[1]
}
commands
when /^DESC(?:RIBE)?\s+(".*?"(?:[^=->\s]\S*)?)\s*=\s*(.*)$/
[[:BUFFER_ROOM, $1, "\n@describe here=" + $2]]
when /^ENDIN/
[[:NOP]]
when /^ENDON/
[[:NOP]]
else
[[:ERROR, "Unrecognized command: #{lineblob.last}"]]
end
end
def warn_if_looks_like_directive(state, lineblob)
if second_stage_mode_default(state, lineblob)[0][0] == :ERROR
[]
else
[[:WARNING,
'Directive matched inside "' +
{:in_room => 'IN', :on_exit => 'ON'}[state.first] +
"\" state: '" + lineblob.last.rstrip + "'"
]]
end
end
def buffer_prefix(s)
return (/^\s+/.match(s) ? "" : "\n") + s.sub(/^\s+/,'').gsub(/\t/,' ')
end
def second_stage_mode_in_room(state, lineblob)
case lineblob.last
when /^\s*$/
[[:NOP]]
when /^@@/,
[[:NOP]]
when /^IN\s/
[[:NOP]]
when /^#.*/
[[:BUFFER_ROOM, state[2], buffer_prefix(Regexp.last_match(0))]]
when /^ENDON/
[[:ERROR, "ENDON inside of IN-block."]]
when /^ENDIN/
[[:IMPOSSIBLE]] # Handled in default mode
else
warn_if_looks_like_directive(state, lineblob) +
[[:BUFFER_ROOM, state[2], buffer_prefix(lineblob.last.rstrip)]]
end
end
def second_stage_mode_on_exit(state, lineblob)
case lineblob.last
when /^\s*$/
[[:NOP]]
when /^@@/,
[[:NOP]]
when /^ON\s/
[[:NOP]]
when /^#.*/
# TODO: Eliminate this order-switch
[[:BUFFER_EXIT, state[3], state[2], buffer_prefix(Regexp.last_match(0))]]
when /^ENDIN/
[[:ERROR, "ENDIN inside of ON-block."]]
when /^ENDON/
[[:IMPOSSIBLE]] # Handled in default mode
else
# TODO: Eliminate this order-switch
warn_if_looks_like_directive(state, lineblob) +
[[:BUFFER_EXIT, state[3], state[2], buffer_prefix(lineblob.last.rstrip)]]
end
end
def second_stage_mode_error(state, lineblob)
[[:ERROR, state[2]]]
end
end
def process_file(file)
parser = InputStateMachine.new()
filename = file.path
lineblobs = file.each_line.each_with_index.map {|line,index| [filename, index+1, line] }
state_per_line = lineblobs.reduce([DEFAULT_STATE]) {|state, lineblob|
current_state = state.last
state << (parser.send("first_stage_mode_#{current_state.first}", lineblob) || current_state)
}.drop(1)
opblobs = state_per_line.zip(lineblobs).flat_map {|state, lineblob|
parser.send("second_stage_mode_#{state.first}", state, lineblob).map {|opcode|
lineblob + [opcode]
}
}
commands = opblobs.map {|opblob|
{:location => {:file => opblob[0], :linenumber => opblob[1]}, :opcode => opblob[3]}
}
return commands
end
# Section: Opcodes -> Graph
#
# Here we read the opcodes from the last section.
# This state machine has states that only affect its output, not input
# processing. This machine is simple enough that a switch will do.
# think squish(iter(lnum(1,127),if(cand(not(valid(attrname,x[chr(##)]x)),t(chr(##))),chr(##))))
# Invalid ASCII characters in attribute names for:
# PennMUSH : % ( ) : [ \ ] ^ { }
# TinyMUX : " % * , : ; [ \ ] { | }
# RhostMUSH: " * , : ; [ \ ] { | }
# All combined: " % ( ) * , : ; [ \ ] ^ { | }
#
# Don't rely on these functions producing consistent
# output between versions of quickbuild!
BADATTR_ORDS = [34, 37, 40, 41, 42, 44, 58, 59, 91, 92, 93, 94, 123, 124, 125]
BADATTR_CHARS = (BADATTR_ORDS.map {|x| x.chr() }) + [' ']
BADATTR_REPLACE = (BADATTR_ORDS.map {|x| '$' + x.to_s(16) }) + ['_']
BADATTR_REGEXP = Regexp.union(BADATTR_CHARS)
BADATTR_HASH = Hash[BADATTR_CHARS.zip(BADATTR_REPLACE)]
def mush_attr_escape(s)
return s.gsub(BADATTR_REGEXP, BADATTR_HASH)
end
def mush_id_format(s)
return mush_attr_escape(s.sub(/^"/,'').sub(/"$/,''))
end
def id_to_name(id)
return id.match(/"(.*)"/)[1]
end
def aliasify(given_name, alias_list = [])
name, _semicolon, given_aliases = given_name.partition(';')
aliased_word, aliased_name = nil, name
autobracket = false
if /<\S+>/ !~ name && autobracket then # Not pre-bracketed
aliased_word = name.split(' ').reduce('') {|m,w| m.concat(w[0])}
aliased_name = (name.split(' ').map {|word| word.sub(/^./, '<\0>')}).join(' ')
elsif /<\S+>/ =~ name then # Pre-bracketed exit name
aliased_word = name.scan(/<(\S+)>/).join('')
aliased_name = name
end
fullname = ([aliased_name, aliased_word, given_aliases].select {|x| x && x != ''}).join(';')
return [fullname, [aliased_word] + given_aliases.split(';')]
end
class RoomNode
attr_accessor :id, :name
attr_accessor :type
attr_reader :edges
attr_accessor :attr_base
attr_accessor :parent, :parent_type
attr_accessor :zone, :zone_type
attr_accessor :flags
attr_accessor :prebuilt_dbref
def initialize(id)
@id = mush_id_format(id)
@name = id_to_name(id)
@prebuilt_dbref = nil
@type = :room
@edges = {}
@attr_base = nil
@parent = nil
@parent_type = nil
@zone = nil
@zone_type = nil
@flags = nil
@buffer = ''
@properties = {}
end
def add_exit(exitedge)
@edges.store(exitedge.id, exitedge)
end
def lookup_exit(id)
return @edges[mush_id_format(id)]
end
def to_s()
return @id
end
def set_buffer(s)
@buffer = s
end
def append_buffer(s)
@buffer.concat(s)
end
def buffer()
@buffer.lstrip
end
end
class ExitEdge
attr_accessor :id, :name, :from_room, :to_room
attr_accessor :parent, :parent_type
attr_accessor :zone, :zone_type
attr_accessor :flags
def initialize(id, name, from_room, to_room)
@id = mush_id_format(id)
@name = name
@parent = nil
@parent_type = nil
@zone = nil
@zone_type = nil
@flags = nil
@from_room = from_room
@to_room = to_room
@buffer = ''
@properties = {}
end
def to_s()
return [@from_room.to_s(), '-->', @to_room.to_s()].join(' ')
end
def append_buffer(s)
@buffer.concat(s)
end
def buffer()
@buffer.lstrip
end
end
class MuGraph
attr_reader :edgelist
attr_accessor :id_parents, :id_zones
def initialize()
@nodes = {}
@prebuilt_rooms = {}
@edgelist = []
@id_parents = {}
@id_zones = {}
end
def [](x)
return @nodes[x]
end
def new_room(id)
@nodes.store(id, RoomNode.new(id))
end
def new_exit(id, from_room, to_room, aliases = {}, autoalias = true)
name = aliases[id]
name ||= aliasify(id_to_name(id))[0] if autoalias
name ||= id_to_name(id)
exitedge = ExitEdge.new(id, name, from_room, to_room)
from_room.add_exit(exitedge)
@edgelist.push(exitedge)
return exitedge
end
def nodes()
if block_given? then
@nodes.values {|node| yield(node)}
end
return @nodes.values
end
def edges()
if block_given? then
@edgelist.each {|exitedge| yield(exitedge)}
end
return @edgelist
end
end
def mywarn(stateobj, message, prefix="WARNING:")
warn("#{prefix} File '#{stateobj[:location][:file]}' Line #{stateobj[:location][:linenumber]}: #{message.to_s()}")
end
def die(stateobj, message)
mywarn(stateobj, message, "ERROR:")
abort
end
def op_PREBUILT_ROOM(stateobj, operation, operand)
graph = stateobj[:graph]
if graph[operand[0]] then
die("There is already an existing room or prebuilt directive for #{operand[0]} (= #{operand[1]})")
end
op_CREATE_ROOM(stateobj, operation, operand)
graph[operand[0]].prebuilt_dbref = operand[1]
end
def op_CREATE_ROOM(stateobj, operation, operand)
graph = stateobj[:graph]
if graph[operand[0]] == nil then
room = graph.new_room(operand[0])
room.attr_base = stateobj[:attr_base]
if stateobj[:room_parent] && operand[0] != stateobj[:room_parent] then
room.parent = stateobj[:room_parent]
room.parent_type = stateobj[:room_parent_type]
end
if stateobj[:room_zone] && operand[0] != stateobj[:room_zone] then
room.zone = stateobj[:room_zone]
room.zone_type = stateobj[:room_zone_type]
end
room.flags = stateobj[:room_flags]
room.set_buffer(graph.id_zones[operand[0]][:buffer]) if graph.id_zones[operand[0]]
room.set_buffer(graph.id_parents[operand[0]][:buffer]) if graph.id_parents[operand[0]]
end
# If we just made a Room Parent/Zone into a real room, make the list reference the real object.
graph.id_parents.store(operand[0], room) if graph.id_parents.key?(operand[0])
graph.id_zones.store(operand[0], room) if graph.id_zones.key?(operand[0])
end
# Take an opcode array and output a graph.
def process_opcodes(opcode_array, options = {})
stateobj = {
:location => nil,
:attr_base => "ROOM.",
:reverse_exits => {},
:exit_aliases => {},
:room_parent => nil, :room_parent_type => nil,
:room_zone => nil, :room_zone_type => nil,
:room_flags => nil,
:exit_parent => nil, :exit_parent_type => nil,
:exit_zone => nil, :exit_zone_type => nil,
:exit_flags => nil,
:graph => MuGraph.new()
}
graph = stateobj[:graph]
opcode_array.each {|h|
stateobj[:location] = h[:location]
operation, *operand = h[:opcode]
case operation
when :NOP
# Do nothing
when :ERROR
die(stateobj, operand[0])
when :WARNING
mywarn(stateobj, operand[0])
when :ATTR_BASE
stateobj[:attr_base] = (operand[0].strip.length == 0 ? "ROOM." : operand[0].strip)
when :ALIAS
old = stateobj[:exit_aliases].delete(operand[0])
mywarn(stateobj, "Replacing alias definition #{operand[0]}->\"#{old}\" with #{operand[0]}->\"#{operand[1]}\".") if old
stateobj[:exit_aliases].store(operand[0], operand[1])
when :REVERSE
old = stateobj[:reverse_exits].delete(operand[0])
mywarn(stateobj, "Replacing reverse definition #{operand[0]}->#{old} with #{operand[0]}->#{operand[1]}") if old
stateobj[:reverse_exits].store(operand[0], operand[1])
if options[:bidirectional_reverse] then
old = stateobj[:reverse_exits].delete(operand[1])
mywarn(stateobj, "Replacing reverse definition #{operand[1]}->#{old} with #{operand[1]}->#{operand[0]}") if old
stateobj[:reverse_exits].store(operand[1], operand[0])
end
when :ROOM_PARENT
if operand[0] && operand[1] == :id then
room = graph[operand[0]] || # Can return nil
{:attr_base => stateobj[:attr_base],
:id => mush_id_format(operand[0]),
:name => id_to_name(operand[0]),
:buffer => ''}
graph.id_parents.store(operand[0], room)
end
stateobj[:room_parent_type] = operand[1]
stateobj[:room_parent] = operand[0]
when :ROOM_ZONE
if operand[0] && operand[1] == :id then
room = graph[operand[0]] || # Can return nil
{:attr_base => stateobj[:attr_base],
:id => mush_id_format(operand[0]),
:name => id_to_name(operand[0]),
:buffer => ''}
graph.id_zones.store(operand[0], room)
end
stateobj[:room_zone_type] = operand[1]
stateobj[:room_zone] = operand[0]
when :ROOM_FLAGS
stateobj[:room_flags] = operand[0]
when :EXIT_PARENT
if operand[0] && operand[1] == :id then
# Make a room (or thing) if one doesn't exist.
# Exits typically do not make good exit @parents!
room = graph[operand[0]] || # Can return nil
{:attr_base => stateobj[:attr_base],
:id => mush_id_format(operand[0]),
:name => id_to_name(operand[0]),
:buffer => ''}
graph.id_parents.store(operand[0], room)
end
stateobj[:exit_parent_type] = operand[1]
stateobj[:exit_parent] = operand[0]
when :EXIT_ZONE
if operand[0] && operand[1] == :id then
# Make a room (or thing) if one doesn't exist.
# Exits typically do not make good exit @parents!
room = graph[operand[0]] || # Can return nil
{:attr_base => stateobj[:attr_base],
:id => mush_id_format(operand[0]),
:name => id_to_name(operand[0]),
:buffer => ''}
graph.id_zones.store(operand[0], room)
end
stateobj[:exit_zone_type] = operand[1]
stateobj[:exit_zone] = operand[0]
when :EXIT_FLAGS
stateobj[:exit_flags] = operand[0]
when :PREBUILT_ROOM
op_PREBUILT_ROOM(stateobj, operation, operand)
when :CREATE_ROOM # Do not error/warn if it exists.
op_CREATE_ROOM(stateobj, operation, operand)
when :CREATE_EXIT
from_room, to_room = graph[operand[1]], graph[operand[2]]
die(stateobj, "Room #{operand[1]} doesn't exist") if ! from_room
die(stateobj, "Room #{operand[2]} doesn't exist") if ! to_room
die(stateobj, "There is already an exit #{operand[0]} in room #{operand[1]}") if from_room.lookup_exit(operand[0])
exitedge = graph.new_exit(operand[0], from_room, to_room, stateobj[:exit_aliases], options[:brackets])
if stateobj[:exit_parent] then
exitedge.parent = stateobj[:exit_parent]
exitedge.parent_type = stateobj[:exit_parent_type]
end
if stateobj[:exit_zone] then
exitedge.zone = stateobj[:exit_zone]
exitedge.zone_type = stateobj[:exit_zone_type]
end
exitedge.flags = stateobj[:exit_flags]
when :CREATE_REVERSE_EXIT
from_room, to_room = graph[operand[1]], graph[operand[2]]
reverse = stateobj[:reverse_exits][operand[0]]
die(stateobj, "No reverse exit for #{operand[0]}") if ! reverse
die(stateobj, "Room #{operand[1]} doesn't exist") if ! from_room
die(stateobj, "Room #{operand[2]} doesn't exist") if ! to_room
die(stateobj, "There is already an exit #{reverse} in room #{operand[2]} (while making reverse exit for #{operand[0]})") if to_room.lookup_exit(reverse)
exitedge = graph.new_exit(reverse, to_room, from_room, stateobj[:exit_aliases], options[:brackets])
if stateobj[:exit_parent] then
exitedge.parent = stateobj[:exit_parent]
exitedge.parent_type = stateobj[:exit_parent_type]
end
if stateobj[:exit_zone] then
exitedge.zone = stateobj[:exit_zone]
exitedge.zone_type = stateobj[:exit_zone_type]
end
exitedge.flags = stateobj[:exit_flags]
when :BUFFER_ROOM
# We can add a buffer to any room built or guaranteed to be built.
if graph[operand[0]] then
graph[operand[0]].append_buffer(operand[1])
elsif graph.id_parents[operand[0]] then
graph.id_parents[operand[0]][:buffer] += operand[1]
elsif graph.id_zones[operand[0]] then
graph.id_zones[operand[0]][:buffer] += operand[1]
else
die(stateobj, "Room #{operand[0]} doesn't exist")
end
when :BUFFER_EXIT
# Exits must be built in a room before they can have a buffer.
room = graph[operand[0]]
die(stateobj, "Room #{operand[0]} doesn't exist") if room == nil
exitedge = room.lookup_exit(operand[1])
die(stateobj, "Exit #{operand[1]} doesn't exist") if exitedge == nil
exitedge.append_buffer(operand[2])
end
}
return graph
end
# Section: Graph -> Softcode
#
# TODO: Warn on: Rooms with no entrances
#
# Print out MUSH code. We do it like this:
# 1. Dig all of the rooms and store their database reference numbers. (DBref)
# 2. Visit each room, and, while there:
# a. Open all of the exits leading from that room, applying exit code
# b. Apply any room code
# We use attributes on the player to store room dbrefs. We call them
# #{room.attr_base}#{room.id}.
def wrap_text(initial_tab, tab, text, width = 75)
return initial_tab + text.scan(/(?:.{1,#{width}})(?:\s+|$)|(?:.{#{width}})/m).join("\n" + tab)
end
def print_room_buffer(roomnode)
output = []
if roomnode.buffer != ''
if roomnode.type == :thing then
output << "@teleport [v(#{roomnode.attr_base}#{roomnode.id})]=here"
output << "@teleport [v(#{roomnode.attr_base}#{roomnode.id})]"
end
output << roomnode.buffer
if roomnode.type == :thing then
output << "@teleport [loc(v(#{roomnode.attr_base}#{roomnode.id}))]"
output << "@teleport [v(#{roomnode.attr_base}#{roomnode.id})]=me"
end
end
return output
end
def process_graph(graph, options = {})
abort("Cannot use managed mode with --nosidefx option") if ! options[:unmanaged] && options[:nosidefx]
abort("Destroying exits is meaningless outside managed mode") if options[:unmanaged] && options[:destructive]
output = []
rooms = graph.nodes()
rooms.sort! {|a,b|
next -1 if a.zone == nil && b.zone != nil
next 1 if a.zone != nil && b.zone == nil
next -1 if a.parent == nil && b.parent != nil
next 1 if a.parent != nil && b.parent == nil
next 0
}
now = Time.now
timestring = ([now.year, now.month, now.day].map {|i| i.to_s.rjust(2,'0')} ).join('-')
output << "@@ File generated on " + timestring + " with Quickbuild version " + VERSION
output << wrap_text("@@ ", "@@ ", (graph.edgelist.map {|exitedge| "#{exitedge.from_room.id}-->#{exitedge.to_room.id}" }).join(' '))
# TODO: Once ATTR_BASES is set on exits, do graph.edgelist.map here.
attr_bases = (rooms.map {|roomnode| roomnode.attr_base }).sort.uniq
attr_bases_made = {}
attr_bases.each {|attrname|
pieces = attrname.split('`')
if pieces.length > 1 then
(0...pieces.length).each {|i|
attr_base = pieces[0..i].join('`')
attr_bases_made.store("&" + attr_base + " me=Placeholder", :true)
}
end
}
if attr_bases_made.length > 0 then
output << "think Constructing attribute trees (legacy PennMUSH support)"
output.concat(attr_bases_made.keys)
end
unbuilt_parents = graph.id_parents.select {|k,v| v.class == Hash}
if unbuilt_parents.length > 0 then
output << "think Creating room & exit parents " + (options[:nosidefx] ? "as rooms" : "as things")
unbuilt_parents.each {|k,v|
room = graph.new_room(k)
room.attr_base = v[:attr_base]
room.type = :thing
room.set_buffer(v[:buffer])
graph.id_parents.store(k, room)
attrname = "#{room.attr_base}#{room.id}"
if options[:nosidefx] then
output << "@dig/teleport #{room.name}"
output << "@set me=#{attrname}:%l"
elsif options[:unmanaged] then
output << "think set(me,#{attrname}:[create(#{room.name},10)])"
else
output << "think set(me,#{attrname}:[default(me/#{attrname},create(#{room.name},10))])"
end
output << "@lock [v(#{room.attr_base}#{room.id})]= =me"
output << "@lock/zone [v(#{room.attr_base}#{room.id})]= =me"
output << "@link [v(#{room.attr_base}#{room.id})]=me"
output += print_room_buffer(room) if ! rooms.include?(room)
}
end
unbuilt_zones = graph.id_zones.select {|k,v| v.class == Hash}
if unbuilt_zones.length > 0 then
output << "think Creating room & exit zones " + (options[:nosidefx] ? "as rooms" : "as things")
unbuilt_zones.each {|k,v|
room = graph.new_room(k)
room.attr_base = v[:attr_base]
room.type = :thing
room.set_buffer(v[:buffer])
graph.id_zones.store(k, room)
attrname = "#{room.attr_base}#{room.id}"
if options[:nosidefx] then
output << "@dig/teleport #{room.name}"
output << "@set me=#{attrname}:%l"
elsif options[:unmanaged] then
output << "think set(me,#{attrname}:[create(#{room.name},10)])"
else
output << "think set(me,#{attrname}:[default(me/#{attrname},create(#{room.name},10))])"
end
output << "@lock [v(#{room.attr_base}#{room.id})]= =me"
output << "@lock/zone [v(#{room.attr_base}#{room.id})]= =me"
output << "@link [v(#{room.attr_base}#{room.id})]=me"
output += print_room_buffer(room) if ! rooms.include?(room)
}
end
rooms.select {|roomnode| roomnode.prebuilt_dbref }.each do |roomnode|
output << "&#{roomnode.attr_base}#{roomnode.id} me=#{roomnode.prebuilt_dbref}"
end
output << "think Digging Rooms"
dig_rooms = rooms.reject {|roomnode| roomnode.prebuilt_dbref }
dig_rooms.each do |roomnode|
attrname = "#{roomnode.attr_base}#{roomnode.id}"
if options[:nosidefx] then
output << "@dig/teleport #{roomnode.name}"
output << "@set me=#{attrname}:%l"
elsif options[:unmanaged] then
output << "@dig/teleport #{roomnode.name}"
output << "think set(me,#{attrname}:%l)"
else
output << "think set(me,#{attrname}:[default(me/#{attrname},switch(functions(),* DIG *,dig(#{roomnode.name}),create(#{roomnode.name},,r)))])"
output << "@teleport [v(#{attrname})]"
if options[:destructive]
output << "think null(iter(lexits(here),[tel(##,me)][destroy(##)]))"
output << "@dolist/inline [lexits(here)]={@recycle ##; @recycle ##}"
end
end
if roomnode.parent then
output << "@parent here=#{roomnode.parent}" if roomnode.parent_type == :raw
p = graph[roomnode.parent]
if roomnode.parent_type == :id then
if options[:nosidefx] then
output << "@parent here=[v(#{p.attr_base}#{p.id})]"
else
output << "think null(parent(here,[v(#{p.attr_base}#{p.id})]))"
end
end
end
if roomnode.zone then
output << "@chzone here=#{roomnode.zone}" if roomnode.zone_type == :raw
z = graph[roomnode.zone]
output << "@chzone here=[v(#{z.attr_base}#{z.id})]" if roomnode.zone_type == :id
end
output << "@set here=#{roomnode.flags}" if roomnode.flags
end
output << "think Linking Rooms"
rooms.each {|roomnode|
output << "think WARNING: Creating room with no exits: #{roomnode.name}" if roomnode.edges.length == 0 && ! roomnode.prebuilt_dbref
output << "@teleport [v(#{roomnode.attr_base}#{roomnode.id})]" if roomnode.edges.length > 0 || roomnode.buffer != ''
roomnode.edges.each {|exitedge_id, exitedge|
shortname = exitedge.name.partition(';')[0]
if options[:nosidefx] || options[:unmanaged] then
output << "@open #{exitedge.name}=[v(#{exitedge.to_room.attr_base}#{exitedge.to_room.id})]"
else
output << "@teleport [setr(0,ifelse(t(setr(0,locate(me,#{shortname},eE))),r(0),switch(functions(),* OPEN *,open(#{exitedge.name}),create(#{exitedge.name},,e))))][link(%q0,v(#{exitedge.to_room.attr_base}#{exitedge.to_room.id}))]=here"
end
if exitedge.parent then
output << "@parent #{shortname}=#{exitedge.parent}" if exitedge.parent_type == :raw
p = graph[exitedge.parent] # Exit parents are not exits
if exitedge.parent_type == :id then
if options[:nosidefx] then
output << "@parent #{shortname}=[v(#{p.attr_base}#{p.id})]"
else
output << "think parent(#{shortname},[v(#{p.attr_base}#{p.id})])"
end
end
end
if exitedge.zone then
output << "@chzone #{shortname}=#{exitedge.zone}" if exitedge.zone_type == :raw
p = graph[exitedge.zone] # Exit zones are not exits
output << "@chzone #{shortname}=[v(#{p.attr_base}#{p.id})]" if exitedge.zone_type == :id
end
output << "@set #{shortname}=#{exitedge.flags}" if exitedge.flags
output << exitedge.buffer if exitedge.buffer != ''
}
output += print_room_buffer(roomnode)
}
has_entrance = Hash[graph.edgelist.map {|exitedge| [exitedge.to_room, true] }]
(dig_rooms - has_entrance.keys).each do |roomnode|
output << "think WARNING: Created room with no entrances: #{roomnode.name}"
end
return output
end
def process_file_list_into_softcode(file_list, options={})
commandlist = file_list.flat_map{|file| process_file(file) }
graph = process_opcodes(commandlist, options)
process_graph(graph, options)
end