Class: TranscriptReader
- Inherits:
-
Object
- Object
- TranscriptReader
- Defined in:
- lib/IFMapper/TranscriptReader.rb
Overview
Class used to parse an Infocom-style transcript and automatically generate a map. This can be used to automatically map a game as you play it.
This code is based on Perl code by Dave Chapeskie, used in IFM, albeit it has been enhanced to handle multi-line commands, room stub exits and oneway exits, objects parsing and the ability to parse transcripts as they are being spit out.
Constant Summary collapse
- TRANSCRIPT =
/^(?:Start of a transcript of|Here begins a transcript of interaction with (.*)|Here begins a transcript of interaction with)/
- LOOK =
PROMPT = /^>s*/
/^l(ook)?/i
- UNDO =
/^undo$/i
- RESTART =
/^restart$/i
- RESTORE =
/^restore$/i
- IGNORE =
/transcript/i
- OTHERS =
/^(read|inventory$|i$)/i
- UNSCRIPT =
/^unscript$/i
- BLANK =
/^\s*$/
- TAKE =
/^(take|get)\s+(a\s+|the\s+)?(.*)/i
- DROP =
/^(drop|leave)\s+()/i
- STARTUP =
/(^[A-Z]+$|Copyright|\([cC]\)\s*\d|Trademark|Release|Version|[Ss]erial [Nn]umber|Written by)/
- DARKNESS =
/^dark(ness)?$|^in the dark$|^a dark place$|^It is pitch black|^It is too dark to see anything|^You stumble around in the dark/i
- DEAD =
/(You die|You have died|You are dead)/i
- YES =
/^y(es)/i
- THEN =
/\bthen\b/i
- DIRMAP =
Compass direction command -> direction mapping.
{ "n" => 0, "north" => 0, "ne" => 1, "northeast" => 1, "e" => 2, "east" => 2, "southeast" => 3, "se" => 3, "south" => 4, "s" => 4, "southwest" => 5, "sw" => 5, "west" => 6, "w" => 6, "northwest" => 7, "nw" => 7 }
- ODIRMAP =
{"up" => 1, "u" => 1, "down" => 2, "d" => 2, "in" => 3, "out" => 4, 'enter' => 3, 'exit' => 4 }
- DIR_REX =
'(' + DIRMAP.keys.join('|') + '|' + ODIRMAP.keys.join('|') + ')'
- GO =
/^((walk|run|go|drive|jump)\s+)?#{DIR_REX}[.,\s]*\b/i
- DIRLIST =
Direction list in order of positioning preference.
[ 0, 4, 2, 6, 1, 3, 5, 7 ]
- NO_EXIT =
No exit replies
[ /\byou\scan't\sgo\sthat\sway\b/i, /\byou\scan't\sgo\sin\sthat\sdirection\b/i, /\bdoorways\slead\s/i, /\bthat's\sa\swall\b/i, /\bno\sexit\b/i, /\bthere's\s+nowhere\s+to\s+go\s+in\sthat\s+direction\b/i, /\bthere's\s+nothing\s+in\s+that\s+direction\b/i, /\bblocks?\sthe\sway/i, /\byou\scan\sonly\sgo\s/i, /\byou\scan\sgo\sonly\s/i, /\bimpenetrable\b/i, /\bbars\syour\sway/i, /\bthere's\s.*in\s(?:your|the)\sway/i ]
- CLOSED_EXIT =
Closed exit replies
[ /door\s+is\s+closed/i, ]
- PREPOSITION =
remove things like (on the bed) from short room name
"(?:in|on|under|behind|inside|along|\w+ing)"
- NAME_REMOVE =
/(\s+\(#{PREPOSITION}\s+.+\)|,\s+#{PREPOSITION}\s+[^\,\.]+)/
- NAME_INVALID =
not a room name if it has any of this.
/--|[:;\*\[\]\|\+\=!\.\?\000<>]/
- SALUT =
Salutations and other words with periods that can appear as name of room
'(Mr|Mr?s|Miss|Jr|Sr|St|Dr|Ave|Inc)'
- SALUTATIONS =
/\b#{SALUT}\./
- NAME_MAXWORDS =
Maximum length of room name. Otherwise, a sentence we ignore.
20
- NAME_UNCAP =
word list that may be uncapitalized in room name
/^(?:of|on|to|with|by|a|in|the|at|under|through|near)$/i
- DESC_MINWORDS =
Default room description recognition parameters.
20
- DESC_IGNORE =
Ignore these words when matching words in description To avoid cases, like: An open gate vs. A closed gate
/(?:an|a|open(ed)?|closed?)/i
- RESTORE_OK =
/\b(Okay|Ok|completed?|Restored)\b/
- THE =
'(?:the|an|a|your|some of the|some)\s+'
- ARTICLE =
Regex to eliminate articles from get/take replies.
/^#{THE}/i
- TAKE_OBJ =
Possible nessages indicating get/take succeeded
'([\w\d\-\'\s]+)'
- TAKE_FROM =
'(?:\s+(?:up|from|out)\s+.*)$'
- TAKE_ALIAS =
'(?:grab|pick\s+up|pick|pilfer|take(?:\s+up)?)'
- TAKE_OK =
[ /\btaken\b/i, /you\s+have\s+now\s+(?:got\s+)?(?:#{THE})?#{TAKE_OBJ}#{TAKE_FROM}/i, /you\s+have\s+now\s+(?:got\s+)?(?:#{THE})?#{TAKE_OBJ}/i, /you\s+#{TAKE_ALIAS}\s+(?:#{THE})?#{TAKE_OBJ}#{TAKE_FROM}/i, /you\s+#{TAKE_ALIAS}\s+(?:#{THE})?#{TAKE_OBJ}/i, /you\s+now\s+have\s+(?:got\s+)?(?:#{THE})?#{TAKE_OBJ}/i, /you\s+pick\s+up\s+(?:#{THE})?#{TAKE_OBJ}/i, /you\s+are\s+now\s+holding\s+(?:#{THE})?#{TAKE_OBJ}/i, ]
- IT =
/^(it|them)$/i
- IDENTIFY_BY_DESCRIPTION =
0
- IDENTIFY_BY_SHORTNAME =
1
- SHORTNAME_CLASSIC =
0
- SHORTNAME_CAPITALIZED =
1
- SHORTNAME_MOONMIST =
2
- SHORTNAME_WITNESS =
3
- SHORTNAME_ADRIFT =
4
- SHORTNAME_ALL_CAPS =
5
- DIRS =
Given a room description, parse it to try to find out all exits from room.
{ # SWD added variants used in e.g. YAGWAD 'north-east' => 1, 'south-east' => 3, 'south-west' => 5, 'north-west' => 7, # Usual directions 'north' => 0, 'northeast' => 1, 'east' => 2, 'southeast' => 3, 'south' => 4, 'southwest' => 5, 'west' => 6, 'northwest' => 7, }
- DIR =
SWD: hack: the variants with dashes must come first (so that regexes matches ‘north-east’ in preference to ‘north’), so we manually prepend their names. The duplication is harmless, it just makes the regexes slightly slower in the non-match case.
'(' + DIRS.keys.join('\b|') + ')'
- OR =
'(?:and|or)'
- EXITS_REGEX =
[ # paths lead west and north /(run|bears|lead|wander|winds|go|continues)\s+#{DIR}\s+#{OR}\s+#{DIR}\b/i, # to the east or west /to\s+the#{DIR}\s+#{OR}\s+(to\s+the\s+)?#{DIR}\b/i, # You can go south /you\s+can\s+go#{DIR}\b/i, # to the east /to\s+the\s+#{DIR}\b/i, # east-west corridor /[\.\s+]#{DIR}[\/-]#{DIR}\s+(passage|corridor)/i, # East is the postoffice /\b#{DIR}\s+is/i, # Directly north /\bDirectly\s+#{DIR}/i, # continues|lies|etc... east /(runs|bears|leads|heads|opens|winds|continues\s+on|continues|branches|lies|wanders|bends|curves)\s+#{DIR}\b/i, /(running|leading|heading|opening|branching|lying|wandering|looking|bending)\s+#{DIR}\b/i, ]
- EXITS_SPECIAL =
{ /four\s+directions/i => [0, 2, 4, 6], /four\s+compass\s+points/i => [0, 2, 4, 6], /all\s+directions/i => [0, 1, 2, 3, 4, 5, 6, 7], }
- @@win =
nil
- @@debug =
Change this to non-nil to print out debugging info
nil
Instance Attribute Summary collapse
-
#identify ⇒ Object
Returns the value of attribute identify.
-
#map ⇒ Object
Returns the value of attribute map.
-
#shortName ⇒ Object
Returns the value of attribute shortName.
Instance Method Summary collapse
- #_parse_line(line) ⇒ Object
-
#add(room) ⇒ Object
Add a new room to the automapper.
- #capitalize_room(line) ⇒ Object
-
#choose_dir(a, b, go = nil, exitB = nil) ⇒ Object
Choose a direction to represent up/down/in/out.
- #debug(*msg) ⇒ Object
- #destroy ⇒ Object
- #find_room(name, desc) ⇒ Object
- #find_room_by_desc(name, desc) ⇒ Object
- #find_room_by_name(name, desc) ⇒ Object
-
#initialize(map, file) ⇒ TranscriptReader
constructor
A new instance of TranscriptReader.
- #new_link(move, from, to, dir, go) ⇒ Object
-
#new_room(move, name, desc, dir = nil, from = nil, go = nil) ⇒ Object
Create a new room.
- #parse_exits(r, desc) ⇒ Object
- #parse_line(line) ⇒ Object
- #properties(modal = false) ⇒ Object
-
#remove(rooms) ⇒ Object
Remove some rooms from the automap knowledge (user removed these manually).
- #room_name(line) ⇒ Object
- #room_name_alan(line) ⇒ Object
-
#room_name_all_caps(line) ⇒ Object
Determine if line corresponds to a room name.
-
#room_name_classic(line, all_capitals = true) ⇒ Object
Determine if line corresponds to a room name.
- #room_name_moonmist(line) ⇒ Object
- #room_name_witness(line) ⇒ Object
- #shift_link(room, dir) ⇒ Object
- #start ⇒ Object
-
#step ⇒ Object
Step one user command at a time.
- #stop ⇒ Object
-
#take(move, objs) ⇒ Object
Handle a take action.
Constructor Details
#initialize(map, file) ⇒ TranscriptReader
Returns a new instance of TranscriptReader.
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1372 def initialize(map, file) @shortName = 0 @prompt = /^>\s*/ @identify = 0 @f = nil @file = file @map = map @objects = {} @moves = [] @here = nil @section = -1 @last_obj = nil end |
Instance Attribute Details
#identify ⇒ Object
Returns the value of attribute identify.
146 147 148 |
# File 'lib/IFMapper/TranscriptReader.rb', line 146 def identify @identify end |
#map ⇒ Object
Returns the value of attribute map.
146 147 148 |
# File 'lib/IFMapper/TranscriptReader.rb', line 146 def map @map end |
#shortName ⇒ Object
Returns the value of attribute shortName.
145 146 147 |
# File 'lib/IFMapper/TranscriptReader.rb', line 145 def shortName @shortName end |
Instance Method Details
#_parse_line(line) ⇒ Object
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 |
# File 'lib/IFMapper/TranscriptReader.rb', line 356 def _parse_line(line) @moves.clear # # Read all commands # loop do line.sub!(@prompt, '') if @prompt.to_s =~ />/ line.chop! line.sub!(/\s+$/, '') cmd = line # Read reply reply = [] while line = @f.gets break if line =~ @prompt line.chop! line.sub!(/\s+$/,'') reply << line end if cmd =~ UNDO if @moves.size < 2 @here = nil # out of moves, no clue where we are else @moves.pop end else move = { } # Replace all 'THEN' in command for periods cmd.sub!(THEN, '.') # and all ANDs for commas cmd.sub!(/\band\b/i,',') # and multiple periods for just one cmd.sub!(/\s*\.+\s*/, '.') move[:cmd] = cmd move[:reply] = reply @moves << move end break if not line end # Step 2 @restart = false tele = nil @moves.each { |move| cmd = move[:cmd] next if cmd =~ IGNORE or cmd =~ OTHERS if cmd =~ UNSCRIPT tele = 1 @here = nil next end if @restart and cmd =~ YES @here = nil else @restart = false tele = nil end if cmd =~ RESTART @restart = true tele = 1 end if cmd =~ RESTORE if move[:reply].join() =~ RESTORE_OK @here = nil tele = 1 else next end end name = nil desc = '' roomflag = false desc_gap = false startup = false rooms = [] move[:reply].each { |r| tele = 1 if r =~ DEAD if r =~ STARTUP # Dealing with a startup message, such as: # MY GAME # Copyright (C) 1984 ComputerQuest # We skip the whole thing until the next blank line debug "#{r} skipped due to startup" startup = true desc = '' name = nil roomflag = false desc_gap = false @here = nil tele = 1 next end next if startup and r !~ BLANK startup = false if not roomflag and r !~ BLANK and n = room_name(r) debug "Found room #{n}" roomflag = true desc_gap = false name = n desc = '' next end if not desc_gap and roomflag and r =~ BLANK and desc == '' desc_gap = true next end if roomflag and r !~ BLANK desc << r << "\n" next end if r =~ BLANK and roomflag if desc.count("\n") == 1 and desc =~ /\?$/ # A "What next?" type of prompt, not a room description desc = '' end if desc == '' desc = nil else desc.gsub!(/\n/, ' ') desc.strip! end rooms << { :name => name, :desc => desc, :tele => tele } roomflag = false desc_gap = false name = nil desc = '' tele = nil end } # Oops, there was no newline between room description and prompt and # we missed the last room. # This happens for example with Magnetic Scrolls transcripts. if name rooms << { :name => name, :desc => desc, :tele => tele } end if not rooms.empty? move[:rooms] = rooms move[:look] = true if cmd =~ LOOK move[:reply] = nil end } @moves.each { |move| cmd = move[:cmd] if objs = cmd.scan(TAKE) take(move, objs) end if not move[:rooms] # Check if a moving command failed if cmd =~ GO dir = $3 # See if a stub direction exits in that direction if @here and DIRMAP[dir] then dir = DIRMAP[dir] next unless @here[dir] if @here[dir].stub? # Check if reply was that there's no exit there. NO_EXIT.each { |re| if move[:reply][0] =~ re # If so, remove it... automapper got it wrong. Not an exit. @map.delete_connection(@here[dir]) break end } end # Check if there is a closed door if @here[dir] and @here[dir].type == Connection::FREE CLOSED_EXIT.each { |re| if move[:reply][0] =~ re # If so, flag it @here[dir].type = Connection::CLOSED_DOOR break end } end end end next end move[:rooms].each { |room| name = room[:name] debug "SECTION: #{@map.section}" debug "HERE IS: #{@here}" debug "CMD: #{cmd}" debug "ENDS AT: #{name}" desc = room[:desc].to_s desc.gsub!(/(\w)\s*\n/, '\1 ') line = move[:line] # If we teleported, try to find room if room[:tele] debug "\t ****TELEPORT TO #{name}****" @here = find_room(name, desc) debug "\t TO: #{@here}" end # Make sure the user has not deleted the current room from map # and that we are in the proper section. if @here found = false @map.sections.each_with_index { |sect, idx| if sect.rooms.include?(@here) found = true if idx != @map.section @map.fit @map.section = idx end break end } @here = nil if not found end # If it is a look command or we don't know where we are yet, # set current room. if move[:look] or not @here if move[:look] and name =~ DARKNESS @here = find_room(name, desc) if @here and not @here.darkness else @here = find_room(name, desc) end @here = new_room(move, name, desc) unless @here @here.selected = true next end cmd.sub!(GO, '') dir = $3 # Swallow everything until next command (separated by .) cmd.sub!(/.*\./, '') debug "MOVED IN DIRECTION: #{dir} CMD LEFT:#{cmd}" if not cmd debug "Oops... run out of commands." break end sect = @map.section # Otherwise, assume we moved in some way. Try to find the new room. if name =~ DARKNESS idx = DIRMAP[dir] || ODIRMAP[dir] # Moving to a dark room is special. We verify the exit to see # if it was already moving to a dark room (in which case it # becomes "there") or if it was moving to some other room (in # which case we flag it as dark and make it there) if idx and @here[idx] c = @here[idx] if c.roomA == @here b = c.roomB else b = c.roomA end if not b there = nil else if b.name !~ DARKNESS b.darkness = true end there = b end else # No connection yet, create dark room there = nil end else there = find_room(name, desc) end next if there == @here go = nil if DIRMAP[dir] dir = DIRMAP[dir] elsif ODIRMAP[dir] go = ODIRMAP[dir] debug "#{dir} move #{go}" dir = choose_dir(@here, there, go) else # special move --- teleport/death/etc. go = 0 dir = choose_dir(@here, there, go) end debug "MOVED IN DIR INDEX: #{dir}" @here.selected = false if not there # Unvisited -- new room @here = new_room(move, name, desc, dir, @here, go) else # Visited before -- new link if sect == @map.section # don't conn new_link(move, @here, there, dir, go) end @here = there end } } @moves.clear @map.fit if @map.kind_of?(FXMap) end end |
#add(room) ⇒ Object
Add a new room to the automapper
234 235 236 |
# File 'lib/IFMapper/TranscriptReader.rb', line 234 def add(room) @rooms << room end |
#capitalize_room(line) ⇒ Object
860 861 862 863 864 865 866 867 868 869 870 |
# File 'lib/IFMapper/TranscriptReader.rb', line 860 def capitalize_room(line) words = line.split(' ') words.each_with_index { |w, idx| if idx > 0 and w =~ NAME_UNCAP w.downcase! else w.capitalize! end } return words.join(' ') end |
#choose_dir(a, b, go = nil, exitB = nil) ⇒ Object
Choose a direction to represent up/down/in/out.
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1277 def choose_dir(a, b, go = nil, exitB = nil) # Don't add a new connection if we already have a normal connection # to the room and we are moving up/down/etc. if go rgo = go % 2 == 0? go - 1 : go + 1 debug "#{Connection::EXIT_TEXT[go]} <=> #{Connection::EXIT_TEXT[rgo]}" # First, check if room already has exit moving towards other room a.exits.each_with_index { |e, idx| next if not e roomA = e.roomA roomB = e.roomB if roomA == a and roomB == b e.exitAtext = go if e.exitBtext == rgo return idx elsif roomB == a and roomA == b e.exitBtext = go if e.exitAtext == rgo return idx end } end # We prefer directions that travel less... so we need to figure # out where we start from... if b x = b.x y = b.y else x = a.x y = a.y end if exitB dx, dy = Room::DIR_TO_VECTOR[exitB] x += dx y += dy end # No such luck... Pick a direction. best = nil bestscore = nil DIRLIST.each { |dir| # We prefer straight directions to diagonal ones inc = dir % 2 == 1 ? 10 : 14 score = 1000 # We prefer directions where both that dir and the opposite side # are empty. if (not a[dir]) or a[dir].stub? score += inc score += 4 if a[dir] #attaching to stubs is better end # rdir = (dir + 4) % 8 # score += 1 unless a[rdir] # Measure distance for that exit, we prefer shorter # paths dx, dy = Room::DIR_TO_VECTOR[dir] dx = (a.x + dx) - x dy = (a.y + dy) - y d = dx * dx + dy * dy score -= d next if bestscore and score <= bestscore bestscore = score best = dir } if not bestscore raise "No free exit for choose_dir" end return best end |
#debug(*msg) ⇒ Object
129 130 131 132 133 |
# File 'lib/IFMapper/TranscriptReader.rb', line 129 def debug(*msg) if @@debug $stdout.puts msg end end |
#destroy ⇒ Object
1353 1354 1355 1356 1357 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1353 def destroy @t.kill if @t @f.close if @f GC.start end |
#find_room(name, desc) ⇒ Object
700 701 702 703 704 705 706 707 |
# File 'lib/IFMapper/TranscriptReader.rb', line 700 def find_room(name, desc) case @identify when IDENTIFY_BY_DESCRIPTION return find_room_by_desc(name, desc) when IDENTIFY_BY_SHORTNAME return find_room_by_name(name, desc) end end |
#find_room_by_desc(name, desc) ⇒ Object
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 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 |
# File 'lib/IFMapper/TranscriptReader.rb', line 776 def find_room_by_desc(name, desc) bestscore = 0 best = nil @map.sections.each_with_index { |sect, idx| sect.rooms.each { |room| score = 0 if desc and room.desc # We have a description... # Try exact description match first score += 100 if room.desc == desc # Try substring match score += 50 if room.desc.index(desc) # If we have a room where both name and desc match, # we get a better score than just description only. # This is to help, for example, Trinity, where two rooms have # the exact description but different name. score += 1 if room.name == name and score > 0 # If still no luck, try first N words if score == 0 dwords = room.desc.split(' ') words = desc.split(' ') match = true count = 0 0.upto(desc.size) { |i| # Ignore some words (like open/close, which may just mean # some doors changed state) next if words[i] =~ DESC_IGNORE if words[i] != dwords[i] if count < DESC_MINWORDS match = false break else next end end count += 1 } score += count if match and room.name == name end else # Just the name, not so good score += 1 if room.name == name end next if score <= bestscore bestscore = score best = [room, idx] } } return nil if not best # Make sure we are in the right section if best[1] != @map.section @map.fit @map.section = best[1] end if desc and (not best[0].desc or best[0].desc == '') best[0].desc = desc end return best[0] end |
#find_room_by_name(name, desc) ⇒ Object
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 771 772 773 774 |
# File 'lib/IFMapper/TranscriptReader.rb', line 709 def find_room_by_name(name, desc) bestscore = 0 best = nil @map.sections.each_with_index { |sect, idx| sect.rooms.each { |room| score = 0 score += 1 if room.name == name if score == 1 and desc and room.desc # We have a description... # Try exact description match first score += 100 if room.desc == desc # Try substring match score += 50 if room.desc.index(desc) # If we have a room where both name and desc match, # we get a better score than just description only. # This is to help, for example, Trinity, where two rooms have # the exact description but different name. score += 1 if room.name == name and score > 0 # If still no luck, try first N words if score == 1 dwords = room.desc.split(' ') words = desc.split(' ') match = true count = 0 0.upto(desc.size) { |i| # Ignore some words (like open/close, which may just mean # some doors changed state) next if words[i] =~ DESC_IGNORE if words[i] != dwords[i] if count < DESC_MINWORDS match = false break else next end end count += 1 } score += count if match end end next if score <= bestscore bestscore = score best = [room, idx] } } return nil if not best # Make sure we are in the right section if best[1] != @map.section @map.fit @map.section = best[1] end if desc and (not best[0].desc or best[0].desc == '') best[0].desc = desc end return best[0] end |
#new_link(move, from, to, dir, go) ⇒ Object
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1144 def new_link(move, from, to, dir, go) odir = (dir + 4) % 8 c = nil # If we have something in the from direction if from[dir] c = from[dir] debug "\tMOVE #{c} DIR: #{dir}" if c.stub? # Stub connection, fill it c.roomB = to c.exitAtext = go if go c.type = Connection::SPECIAL if go == 0 # we still need to check the destination to[odir]... if not to[odir] # nothing to do elsif to[odir].stub? @map.delete_connection(to[odir]) debug "\tREMOVE #{to[odir]}" else # this end cannot be deleted. we need to shift odir debug "\tCHOOSE NEW DIR for #{odir}" rgo = nil if go and go > 0 rgo = rgo % 2 == 0? go - 1 : go + 1 end odir = choose_dir(to, from, rgo, dir) @map.delete_connection(to[odir]) if to[odir] debug "\tSHIFTED DESTINATION TO EXIT #{odir}" end to[odir] = c elsif c.roomB == to # We already went this way. Nothing to do. debug "\tWE ALREADY PASSED THRU HERE" elsif c.roomA == to # We verified we can travel thru this connection in both # directions. Change its status to both. c.dir = Connection::BOTH debug "\tSECTION: #{@map.section}" debug "\tVERIFIED EXIT BOTH WAYS" else debug "\tOTHER" if c.roomA == from b = c.roomB if b.name =~ DARKNESS debug "*** REPLACING DARK ROOM ***" @map.delete_connection(c) to.darkness = true @map.delete_room(b) c = nil else if c.exitAtext != 0 # if it was an up/down/in/out dir, we shift it shift_link(from, dir) c = nil else # else, we really have a problem in the map debug "*** CANNOT AUTOMAP --- MAZE ***" dir = Room::DIRECTIONS[dir] @map.cannot_automap "Maze detected.\n'#{from}' #{dir} leads to '#{c.roomB}',\nnot to this '#{to}'." self.stop return nil end end else debug "SHIFT LINK #{from} #{dir}" # We have a connection that turns. Move the link around shift_link(from, dir) c = nil end end end if not c and to[odir] c = to[odir] if c.stub? debug "\tREMOVE #{to[odir]} and REPLACE with #{c}" # Stub connection, fill it c.roomB = from # @map.delete_connection(from[dir]) if from[dir].stub? from[dir] = c c.dir = Connection::BtoA c.exitBtext = go if go c.type = Connection::SPECIAL if go == 0 elsif c.roomB == from c.exitBtext = go if go c.type = Connection::SPECIAL if go == 0 else # We need to change odir to something else rgo = nil if go and go > 0 rgo = go % 2 == 0? go - 1 : go + 1 end odir = choose_dir(to, from, rgo, dir) @map.delete_connection(to[odir]) if to[odir] and to[odir].stub? c = nil end end if not c # First, check all from exits that are AtoB to see if we have one # that goes to the room we want. from.exits.each_with_index { |e, idx| next unless e if e.roomA == to and e.dir == Connection::AtoB c = e from[idx] = nil end } if c # If so, make that connection go both ways and attach it to # current direction. from[dir] = c c.dir = Connection::BOTH c.exitAtext = go if go c.type = Connection::SPECIAL if go == 0 end end if not c # No link exists -- create new one. begin c = @map.new_connection( from, dir, to, odir ) c.exitAtext = go if go c.dir = Connection::AtoB rescue Section::ConnectionError end end return c end |
#new_room(move, name, desc, dir = nil, from = nil, go = nil) ⇒ Object
Create a new room
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 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 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1019 def new_room( move, name, desc, dir = nil, from = nil, go = nil ) b = nil dark = false if not from debug "FROM undefined. Increase section #{name}" @section += 1 @map.new_section if @section >= @map.sections.size r = @map.new_room(0, 0) r.name = name else darkexits = [] if name =~ DARKNESS dark = true else dark = false if from[dir] # oops, we had a connection there. c = from[dir] b = c.opposite(from) if b and b.name =~ DARKNESS # Was it a dark room that is now lit? # if so, set dark flag and delete dark room @map.delete_connection(c) @map.delete_room_only(b) dark = true c = nil end end end x = from.x y = from.y dx, dy = Room::DIR_TO_VECTOR[dir] x += dx y += dy @map.shift(x, y, dx, dy) if not @map.free?(x, y) debug "+++ New Room #{name} from #{from}" r = @map.new_room(x, y) r.name = name r.darkness = dark if b and dark b.exits.each_with_index { |de, didx| next unless de @map.sections[@map.section].connections << de r[didx] = de if de.roomA == b de.roomA = r else de.roomB = r end } end c = nil if from[dir] # oops, we had a connection there. c = from[dir] b = c.roomB if c.stub? # Stub connection. Update it. debug "\tUPDATE #{c}" odir = (dir + 4) % 8 c.exitAtext = go if go c.type = Connection::SPECIAL if go == 0 c.roomB = r r[odir] = c debug "\tNOW IT IS #{c}" else # Probably a link that is complex # or oneway. Shift it to some other location shift_link(from, dir) c = nil end end if c == nil begin c = @map.new_connection( from, dir, r ) c.exitAtext = go if go c.type = Connection::SPECIAL if go == 0 c.dir = Connection::AtoB rescue Section::ConnectionError end end end parse_exits(r, desc) # Update room description r.desc = desc return r end |
#parse_exits(r, desc) ⇒ Object
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 |
# File 'lib/IFMapper/TranscriptReader.rb', line 297 def parse_exits(r, desc) return if not desc exits = [] # Now, start searching for stuff # First try the special directions... EXITS_SPECIAL.each { |re, dirs| if desc =~ re exits += dirs break end } # If that failed, start searching for exits if exits.empty? EXITS_REGEX.each { |re| matches = desc.scan(re) next if matches.empty? matches.each { |arr| arr.each { |m| next unless DIRS[m] exits << DIRS[m] } } } end exits.uniq! # Add a 'stub' for the new connection exits.each { |exit| next if r[exit] begin c = @map.new_connection( r, exit, nil ) c.dir = Connection::AtoB debug "\tADDED STUB #{c}" rescue ConnectionError end } end |
#parse_line(line) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/IFMapper/TranscriptReader.rb', line 340 def parse_line(line) return unless line @map.mutex.synchronize do _parse_line(line) end if @map.kind_of?(FXMap) @map.clear_selection @here.selected = true if @here @map.create_pathmap @map.zoom = @map.zoom @map.center_view_on_room(@here) if @here @map.draw end end |
#properties(modal = false) ⇒ Object
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1360 def properties(modal = false) require 'IFMapper/TranscriptDialogBox' if not @@win @@win = TranscriptDialogBox.new(self) else @@win.copy_from(self) end if modal @@win.execute end end |
#remove(rooms) ⇒ Object
Remove some rooms from the automap knowledge (user removed these manually)
241 242 243 |
# File 'lib/IFMapper/TranscriptReader.rb', line 241 def remove(rooms) rooms.each { |r| @rooms.delete(r) } end |
#room_name(line) ⇒ Object
845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
# File 'lib/IFMapper/TranscriptReader.rb', line 845 def room_name(line) case @shortName when SHORTNAME_ALL_CAPS return room_name_all_caps(line) when SHORTNAME_CAPITALIZED return room_name_classic(line, false) when SHORTNAME_MOONMIST return room_name_moonmist(line) when SHORTNAME_WITNESS return room_name_witness(line) else return room_name_classic(line, true) end end |
#room_name_alan(line) ⇒ Object
872 873 874 875 876 |
# File 'lib/IFMapper/TranscriptReader.rb', line 872 def room_name_alan(line) return false if line !~ /^[^.]+\.$/ line.sub!(/\.$/, '') return room_name_classic(line, true) end |
#room_name_all_caps(line) ⇒ Object
Determine if line corresponds to a room name
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 |
# File 'lib/IFMapper/TranscriptReader.rb', line 961 def room_name_all_caps(line) # Check if user/game has created a room with that name already return line if find_room(line, nil) # We have a room if we match darkness return line if line =~ DARKNESS # Remove unwanted stuff line (on the bed) line.sub!(NAME_REMOVE, '') # Check if user/game has created a room with that name already return line if find_room(line, nil) # Remove periods from salutations line.sub!(SALUTATIONS, '\1') # quick check for invalid format return false if line =~ NAME_INVALID # Qucik check for word characters return false unless line =~ /\w/ # Check if we start line with uncapitalized words or symbols return false if line =~ /^[ a-z\/\\\-\(\)']/ # Check if line holds only capitalized words or several spaces together # or a quote or a 1) line. If so, not a room. return false if line =~ /^[a-z\d,\.\/\-"'\s]+$/ or line =~ /\s\s/ or line =~ /^".*"$/ or line =~ /^"[^"]+$/ or line =~ /^\d+\)/ # Check word count (if too many, not a room) words = line.split(' ') return false if words.size > NAME_MAXWORDS # If not, check all words of 4 chars or more are capitalized # and that there are no 3 or more short letter words together # (which means a diagram) num = 0 words.each { |w| if w.size <= 2 num += 1 return false if num > 2 else num = 0 end } # Restore period to salutations line.sub!(/\b#{SALUT}\b/, '\1.') # Okay, it is a room. return line end |
#room_name_classic(line, all_capitals = true) ⇒ Object
Determine if line corresponds to a room name
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 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 946 947 948 949 950 951 952 953 954 955 956 |
# File 'lib/IFMapper/TranscriptReader.rb', line 901 def room_name_classic(line, all_capitals = true) # Check if user/game has created a room with that name already return line if find_room(line, nil) # We have a room if we match darkness return line if line =~ DARKNESS # Remove unwanted stuff line (on the bed) line.sub!(NAME_REMOVE, '') # Check if user/game has created a room with that name already return line if find_room(line, nil) # Remove periods from salutations line.sub!(SALUTATIONS, '\1') # quick check for invalid format return false if line =~ NAME_INVALID # Qucik check for word characters return false unless line =~ /\w/ # Check if we start line with uncapitalized words or symbols return false if line =~ /^[ a-z\/\\\-\(\)']/ # Check if line holds only capitalized words or several spaces together # or a quote or a 1) line. If so, not a room. return false if line =~ /^[A-Z\d,\.\/\-"'\s]+$/ or line =~ /\s\s/ or line =~ /^".*"$/ or line =~ /^"[^"]+$/ or line =~ /^\d+\)/ # Check word count (if too many, not a room) words = line.split(' ') return false if words.size > NAME_MAXWORDS return false if not all_capitals and words.size > 6 # If not, check all words of 4 chars or more are capitalized # and that there are no 3 or more short letter words together # (which means a diagram) num = 0 words.each { |w| return false if all_capitals and w =~ /^[a-z]/ and w !~ NAME_UNCAP if w.size <= 2 num += 1 return false if num > 2 else num = 0 end } # Restore period to salutations line.sub!(/\b#{SALUT}\b/, '\1.') # Okay, it is a room. return line end |
#room_name_moonmist(line) ⇒ Object
889 890 891 892 893 894 895 896 |
# File 'lib/IFMapper/TranscriptReader.rb', line 889 def room_name_moonmist(line) return false if line =~ /^\(You are not holding it.\)$/ if line =~ /^\(You\sare\s(?:now\s)?(?:[io]n\s)?(?:#{THE})?([\w'\d\s\-_]+)\.\)$/ return capitalize_room( $1 ) else return false end end |
#room_name_witness(line) ⇒ Object
878 879 880 881 882 883 884 885 886 887 |
# File 'lib/IFMapper/TranscriptReader.rb', line 878 def room_name_witness(line) if line =~ /^You\sare\s(?:now\s)?(?:[io]n\s)?(?:#{THE})?([\w'\d\s\-_]+)\.$/ return false if $1 =~ /own feet/ return $1 elsif line =~ /^\(([\w\d\s_]+)\)$/ return $1 else return false end end |
#shift_link(room, dir) ⇒ Object
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1114 def shift_link(room, dir) idx = dir + 1 idx = 0 if idx > 7 while idx != dir break if not room[idx] idx += 1 idx = 0 if idx > 7 end if idx != dir room[idx] = room[dir] room[dir] = nil # get position of other room ox, oy = Room::DIR_TO_VECTOR[dir] c = room[idx] if c.roomA == room b = c.roomB else b = c.roomA end x, y = [b.x, b.y] x -= ox y -= oy dx, dy = Room::DIR_TO_VECTOR[idx] @map.shift(x, y, -dx, -dy) else debug "Warning. Cannot shift connection." end end |
#start ⇒ Object
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1400 def start if not @f @f = File.open(@file, 'r') while line = @f.gets if @map.name =~ /^Empty Map/ and line =~ TRANSCRIPT if $1 @map.name = $1 else @map.name = @f.gets.strip end @map.name = capitalize_room(@map.name) end break if @prompt =~ line end parse_line(line) end @t = Thread.new { loop do self.step Thread.pass sleep 3 end } @t.run end |
#step ⇒ Object
Step one user command at a time
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1388 def step begin if @f.eof @map.status AUTOMAP_IS_WAITING_FOR_MORE_TEXT end parse_line(@f.gets) rescue => e $stderr.puts e $stderr.puts e.backtrace end end |
#stop ⇒ Object
1349 1350 1351 |
# File 'lib/IFMapper/TranscriptReader.rb', line 1349 def stop @t.kill if @t end |
#take(move, objs) ⇒ Object
Handle a take action
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 |
# File 'lib/IFMapper/TranscriptReader.rb', line 161 def take(move, objs) return unless @here if @here.objects != '' and @here.objects[-1,1] != "\n" @here.objects << "\n" end objs.each { |cmd, dummy, obj| next if not obj or not move[:reply] objlist = obj.split(',') o = objlist[0] if objlist.size == 1 and o != 'all' # ignore 'get up' next if cmd == 'get' and (o == 'up' or o == 'off') o = @last_obj if o =~ IT next if not o if move[:reply][0] =~ /^\((.*)\)$/ nobj = $1 words = nobj.split(/\s+/) if words.size < 7 # Acclaration, like: # > get mask # (the brown mask) # Taken. o = nobj else # too big of an aclaration... probably something like # (putting the xxx in your bag to make room) end end o.sub!(ARTICLE, '') status = move[:reply].to_s TAKE_OK.each { |re| if status =~ re then obj = $1 || o obj = o if obj =~ /\b(it|them)\b/ @last_obj = obj # Take succeeded, change object o if not @objects.has_key?(obj) and not @here.objects =~ /\b#{obj}\b/ @here.objects << obj << "\n" @objects[obj] = 1 end break end } else # Handle multiple objects move[:reply].each { |reply| o, status = reply.split(':') next if not status next if o.split(' ').size > 6 # ignore if too many words o.sub!(ARTICLE, '') TAKE_OK.each { |re| if status =~ re then if o and not @objects.has_key?(o) and not @here.objects =~ /\b#{o}\b/ @here.objects << o << "\n" @objects[o] = 1 end @last_obj = o break end } } end } @here.objects.squeeze("\n") end |