Class: MapReader
- Inherits:
-
Object
- Object
- MapReader
- Includes:
- MapUnquote
- Defined in:
- lib/IFMapper/MapReader.rb
Overview
A generic abstract class for reading a map from some file format.
Map readers are expected to parse their file formats and call:
new_room new_door new_obj
with @tag and @name defined.
These functions create arrays of temporary Map* classes (like MapRoom) that represent the room and their connections.
Later on, the create() function is invoked to actually create those rooms as actual FXRooms and similar. The reason this is done this way is because in order to position FXRooms around, we need to previously know the location of all rooms and their exits.
Defined Under Namespace
Classes: MapDoor, MapError, MapObject, MapOneWay, MapRoom, MapSpecialExit, ParseError
Constant Summary collapse
- SEP =
':'
- DIRLIST =
Direction list in order of positioning preference.
[ 0, 4, 2, 6, 1, 3, 5, 7 ]
- @@debug =
nil
Instance Attribute Summary collapse
-
#doors ⇒ Object
readonly
Returns the value of attribute doors.
-
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#include_dirs ⇒ Object
readonly
Returns the value of attribute include_dirs.
-
#map ⇒ Object
readonly
Returns the value of attribute map.
-
#rooms ⇒ Object
readonly
Returns the value of attribute rooms.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #best_dir(r, dirA) ⇒ Object
-
#choose_dir(a, b, go = nil, exitB = nil) ⇒ Object
Choose a direction to represent up/down/in/out.
-
#compact ⇒ Object
Try to move rooms around to make map smaller.
-
#create ⇒ Object
Create all the stuff we found.
- #create_map(file) ⇒ Object
- #create_room(r, x, y, dx = 2, dy = 0) ⇒ Object
- #debug(*x) ⇒ Object
- #get_exit(r, from, to, x, y, dx = 1, dy = 0) ⇒ Object
- #has_exit_to?(a, b) ⇒ Boolean
-
#initialize(file, map = Map.new('Read Map')) ⇒ MapReader
constructor
A new instance of MapReader.
- #make_room(to, x, y, dx = 1, dy = 0) ⇒ Object
- #new_door(loc = nil) ⇒ Object
- #new_obj(loc = nil, klass = MapObject) ⇒ Object
- #new_room(klass = MapRoom) ⇒ Object
- #oneway_link?(a, b) ⇒ Boolean
-
#properties ⇒ Object
Bring up the properties window, to allow user to change settings.
- #read_file(file) ⇒ Object
-
#resolve_exits ⇒ Object
Look for one way exits and add them to the proper room exit on the destination side.
- #set_include_dirs ⇒ Object
- #shift_link(room, dir) ⇒ Object
Methods included from MapUnquote
Constructor Details
#initialize(file, map = Map.new('Read Map')) ⇒ MapReader
Returns a new instance of MapReader.
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 |
# File 'lib/IFMapper/MapReader.rb', line 868 def initialize(file, map = Map.new('Read Map')) @map = map @doors = [] @functions = [] = {} @rooms = [] @objects = [] @include_dirs = [File.dirname(file)] set_include_dirs if @map.kind_of?(FXMap) return unless properties end create_map(file) debug "Done creating #{file}" = nil # save some memory by clearing the tag list @objects = nil @functions = nil @doors = nil @rooms = nil if @map.kind_of?(FXMap) @map.filename = file.sub(/\.t$/i, '.map') @map.['Location Description'] = true @map.window.show end end |
Instance Attribute Details
#doors ⇒ Object (readonly)
Returns the value of attribute doors.
220 221 222 |
# File 'lib/IFMapper/MapReader.rb', line 220 def doors @doors end |
#functions ⇒ Object (readonly)
Returns the value of attribute functions.
220 221 222 |
# File 'lib/IFMapper/MapReader.rb', line 220 def functions @functions end |
#include_dirs ⇒ Object (readonly)
Returns the value of attribute include_dirs.
221 222 223 |
# File 'lib/IFMapper/MapReader.rb', line 221 def include_dirs @include_dirs end |
#map ⇒ Object (readonly)
Returns the value of attribute map.
220 221 222 |
# File 'lib/IFMapper/MapReader.rb', line 220 def map @map end |
#rooms ⇒ Object (readonly)
Returns the value of attribute rooms.
220 221 222 |
# File 'lib/IFMapper/MapReader.rb', line 220 def rooms @rooms end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
220 221 222 |
# File 'lib/IFMapper/MapReader.rb', line 220 def end |
Instance Method Details
#best_dir(r, dirA) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/IFMapper/MapReader.rb', line 269 def best_dir(r, dirA) start = (dirA + 4) % 8 dirs = [ start, (start - 1) % 8, (start + 1) % 8, (start - 2) % 8, (start + 2) % 8, (start - 3) % 8, (start + 3) % 8, dirA ] dirs.each { |d| return d if not r.exits[d] } return nil end |
#choose_dir(a, b, go = nil, exitB = nil) ⇒ Object
Choose a direction to represent up/down/in/out.
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 |
# File 'lib/IFMapper/MapReader.rb', line 495 def choose_dir(a, b, go = nil, exitB = nil) if go rgo = go % 2 == 0? go - 1 : go + 1 # 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 return idx elsif roomB == a and roomA == b e.exitBtext = go 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 ? 100 : 140 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 |
#compact ⇒ Object
Try to move rooms around to make map smaller.
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 |
# File 'lib/IFMapper/MapReader.rb', line 778 def compact # First, verify all one-exit rooms lie next to its opposite room. sect = @map.sections[@map.section] rooms = sect.rooms.find_all { |r| r.num_exits == 1 } rooms.each { |a| c = a.exits.find { |e| e != nil } b = c.opposite(a) dx = (a.x - b.x).abs dy = (a.y - b.y).abs next if dx <= 1 and dy <= 1 x, y = [b.x, b.y] dirB = b.exits.index(c) dx, dy = Room::DIR_TO_VECTOR[dirB] x += dx y += dy dirA = a.exits.index(c) dx, dy = Room::DIR_TO_VECTOR[dirA] # Check if exits are complementary. If not, we need to move room # again. if x + dx != b.x or y + dy != b.y x -= dx y -= dy end next if x == a.x and y == a.y # Place room here. Check if filled. if sect.free?(x, y) a.x, a.y = x, y else if dx != 0 dy = 0 end sect.shift(x, y, -dx, -dy) a.x, a.y = x, y end } end |
#create ⇒ Object
Create all the stuff we found
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 |
# File 'lib/IFMapper/MapReader.rb', line 824 def create @rooms = @rooms.sort_by { |r| r.num_exits } @rooms.reverse! @rooms.each { |r| @tabs = 0 min, max = @map.sections[@map.section].min_max_rooms create_room(r, max[0] + 2, 0) } @rooms = [] @objects.delete_if { |obj| obj.scenery } # Add objects to rooms @objects.each { |obj| obj.location.each { |loc| r = [loc] while r and not r.kind_of?(Room) r = [ r.location[0] ] end next unless r r.objects << obj.name + "\n" } } end |
#create_map(file) ⇒ Object
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 |
# File 'lib/IFMapper/MapReader.rb', line 850 def create_map(file) read_file(file) puts "Rooms: #{@rooms.size}" puts "Doors: #{@doors.size}" puts "Objects: #{@objects.size}" begin resolve_exits create compact rescue => e puts e puts e.backtrace raise end end |
#create_room(r, x, y, dx = 2, dy = 0) ⇒ Object
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 771 772 773 |
# File 'lib/IFMapper/MapReader.rb', line 616 def create_room(r, x, y, dx = 2, dy = 0) return [r.tag] if [r.tag].kind_of?(Room) debug "+++ CREATE ROOM #{r.name} (#{x},#{y}) TAG:#{r.tag}" from, = make_room(r, x, y, dx, dy) r.exits[0,8].each_with_index { |to, exit| next unless to debug "#{r.name} EXIT:#{exit} points to #{to}" go = c = nil if to.kind_of?(MapOneWay) dx, dy = Room::DIR_TO_VECTOR[exit] x = from.x + dx y = from.y + dy @tabs += 1 create_room(to.room, x, y, dx, dy) @tabs -= 1 next elsif to.kind_of?(MapSpecialExit) go = to.go to = to.to end b = to dir = exit type = 0 # If exit leads to an enterable object, find out where does that # enterable object lead to. if to.kind_of?(MapObject) rooms = to.enterable rooms.each { |room| next if room == r to = b = [room] break } # Skip it if we are still an object. This means we are just # a container, like the phone booth in the Fate game demo. next if to.kind_of?(MapObject) end odir = nil if b.kind_of?(MapRoom) odir = b.exits.rindex(r.tag) if not odir b.exits[0,8].each_with_index { |e, idx| next if not e if (e.kind_of?(MapOneWay) and e.room == r) or (e.kind_of?(MapSpecialExit) and e.to == r) odir = idx break end } end elsif b.kind_of?(MapDoor) t = b.location.find { |t| t != r.tag } other = @rooms.find { |x| x.tag == t } # other = @tags[t] if other other.exits.each_with_index { |d, idx| next if not d if d == b or d == r.tag or (d.kind_of?(MapOneWay) and d.room == r) or (d.kind_of?(MapSpecialExit) and (d.to == b or d == r.tag)) odir = idx break end } end end odir = (dir + 4) % 8 if not odir if to.kind_of?(MapRoom) or to.kind_of?(MapDoor) dx, dy = Room::DIR_TO_VECTOR[dir] x = from.x + dx y = from.y + dy @tabs += 1 to, type = get_exit(r, from, to, x, y, dx, dy) @tabs -= 1 next if not to end # b = @rooms.find { |r2| r2.tag == tag } # odir = nil # odir = b.exits.rindex(r.tag) if b # odir = (dir + 4) % 8 if not odir if from[dir] c = from[dir] if to.exits.rindex(c) and c.roomB == from debug "LINK TRAVELLED BOTH" c.dir = Connection::BOTH c.exitBtext = go if go next else debug "#{exit} FROM #{from}->#{to} BLOCKED DIR: #{dir}" shift_link(from, dir) end end # Check we don't have a connection already if to[odir] c = to[odir] # We need to change odir to something else rgo = 0 if go rgo = go % 2 == 0? go - 1 : go + 1 end # First, check if we have a dangling one-way link going to->from # If we do, we use it. c = oneway_link?(from, to) if not c odir = choose_dir(to, from, rgo, dir) else debug "FOUND LINK #{c} -- filling it." idx = from.exits.index(c) from[idx] = nil from[dir] = c c.dir = Connection::BOTH c.exitBtext = go if go end else debug "to[odir] empty." # First, check if we have a dangling one-way link going to->from # If we do, we use it. c = oneway_link?(to, from) if c debug "FOUND LINK #{c} -- filling it." idx = from.exits.index(c) from[idx] = nil from[dir] = c c.dir = Connection::BOTH c.exitBtext = go if go end end if not c debug "NEW LINK #{from} #{dir} to #{to} #{odir}" begin c = @map.new_connection(from, dir, to, odir) c.exitAtext = go if go c.dir = Connection::AtoB c.type = type rescue Section::ConnectionError end end } return from end |
#debug(*x) ⇒ Object
53 54 55 56 57 |
# File 'lib/IFMapper/MapReader.rb', line 53 def debug(*x) return unless @@debug $stdout.puts x $stdout.flush end |
#get_exit(r, from, to, x, y, dx = 1, dy = 0) ⇒ Object
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 |
# File 'lib/IFMapper/MapReader.rb', line 565 def get_exit(r, from, to, x, y, dx = 1, dy = 0 ) elem = [to.tag] if elem.kind_of?(MapRoom) dirB = has_exit_to?(to, r) if dirB dbx, dby = Room::DIR_TO_VECTOR[dirB] if x + dbx != from.x or y + dby != from.y x -= dbx y -= dby end end room = create_room(to, x, y, dx, dy) return [room, Connection::FREE] elsif elem.kind_of?(MapDoor) if elem.connector type = Connection::FREE elsif elem.locked type = Connection::LOCKED_DOOR else type = Connection::CLOSED_DOOR end # Okay, connecting room is missing. Check door's locations property elem.location.each { |tag| next if [tag] == from @rooms.each { |o| next if o.tag != tag res = create_room( o, x, y, dx, dy ) return [ res, type ] } } # @rooms.each { |o| # next if @tags[o.tag] == from # o.exits.each { |e| # next unless e # if @tags[e] == elem # res = create_room( o, x, y, dx, dy ) # return [ res, type ] # end # } # } $stderr.puts "No room for door #{to.name}: #{to}" return [nil, nil] else return [elem, Connection::FREE] end end |
#has_exit_to?(a, b) ⇒ Boolean
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 |
# File 'lib/IFMapper/MapReader.rb', line 290 def has_exit_to?(a, b) idx = a.exits.rindex(b) return idx if idx a.exits.each_with_index { |e, idx| next unless e if e.kind_of?(MapSpecialExit) e = e.to elsif e.kind_of?(MapObject) e.enterable.each { |x| return idx if [x] == b } end case e when MapDoor tag = e.location.find { |t| t != a.tag } e = [tag] return idx if e == b when MapOneWay return idx if e.room == b else return idx if e == b end } return false end |
#make_room(to, x, y, dx = 1, dy = 0) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/IFMapper/MapReader.rb', line 194 def make_room(to, x, y, dx = 1, dy = 0) if not @map.free?(x, y) @map.shift(x, y, dx, dy) end room = @map.new_room(x, y) room.name = to.name desc = to.desc.to_s desc.gsub!(/[\t\n]/, ' ') desc.squeeze!(' ') room.desc = unquote(desc) room.darkness = !to.light [to.tag] = room return room end |
#new_door(loc = nil) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/IFMapper/MapReader.rb', line 173 def new_door(loc = nil) @obj = [@tag] || MapDoor.new @obj.tag = @tag @obj.name = @name || @tag @obj.location << loc if loc [@tag] = @obj @doors << @obj end |
#new_obj(loc = nil, klass = MapObject) ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/IFMapper/MapReader.rb', line 210 def new_obj(loc = nil, klass = MapObject) debug "+++ OBJECT #@name" @obj = klass.new(loc) @obj.tag = @tag @obj.name = @name # || @tag [@tag] = @obj @objects << @obj end |
#new_room(klass = MapRoom) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/IFMapper/MapReader.rb', line 184 def new_room(klass = MapRoom) # We assume we are a room (albeit we could be an obj) @room = klass.new @room.tag = @tag @room.name = @name || @tag @room.desc = '' [@tag] = @room @rooms << @room end |
#oneway_link?(a, b) ⇒ Boolean
480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/IFMapper/MapReader.rb', line 480 def oneway_link?(a, b) # First, check if room already has exit moving towards other room a.exits.each_with_index { |e, idx| next if not e or e.dir != Connection::AtoB roomA = e.roomA roomB = e.roomB if roomA == a and roomB == b return e end } return nil end |
#properties ⇒ Object
Bring up the properties window, to allow user to change settings
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 |
# File 'lib/IFMapper/MapReader.rb', line 227 def properties decor = DECOR_TITLE|DECOR_BORDER dlg = FXDialogBox.new( @map.window.parent, "Include Settings", decor ) mainFrame = FXVerticalFrame.new(dlg, FRAME_SUNKEN|FRAME_THICK| LAYOUT_FILL_X|LAYOUT_FILL_Y) frame = FXHorizontalFrame.new(mainFrame, LAYOUT_SIDE_TOP|LAYOUT_FILL_X) FXLabel.new(frame, "Include Dirs: ", nil, 0, LAYOUT_FILL_X) inc = FXTextField.new(frame, 80, nil, 0, LAYOUT_FILL_ROW) inc.text = @include_dirs.join(SEP) = FXHorizontalFrame.new(mainFrame, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X| PACK_UNIFORM_WIDTH) # Accept FXButton.new(, "&Accept", nil, dlg, FXDialogBox::ID_ACCEPT, FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT|LAYOUT_CENTER_Y) # Cancel FXButton.new(, "&Cancel", nil, dlg, FXDialogBox::ID_CANCEL, FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT|LAYOUT_CENTER_Y) if dlg.execute != 0 @include_dirs = inc.text.split(SEP) return true end return false end |
#read_file(file) ⇒ Object
261 262 263 264 265 266 267 |
# File 'lib/IFMapper/MapReader.rb', line 261 def read_file(file) debug "Start parsing #{file}" File.open(file) { |f| parse(f) } debug "Done parsing #{file}" end |
#resolve_exits ⇒ Object
Look for one way exits and add them to the proper room exit on the destination side. This is needed so that we properly count room exits and start mapping from rooms with more exits.
Also, resolve up/down/in/out exits into one of the proper 8 directions.
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 |
# File 'lib/IFMapper/MapReader.rb', line 323 def resolve_exits if [nil] raise "error" end # # First, we resolve tags to the corresponding Map* class. # If we deal with a MapDoor that is really a connector, we simplify and # just attach the destination room directly. # If we deal with a MapDoor with no matching other room, we remove. This # can happen in TADS games or if user just read a portion of the full map. # @rooms.each { |r| r.exits.each_with_index { |tag, idx| next unless tag to = [tag] if to.kind_of?(MapDoor) if to.location.size == 1 to = nil else if to.connector t = to.location.find { |t| t != r.tag } to = [t] end end end if idx > 7 # if Up/Down/In/Out exit and we have a similar exit # going in one direction, remove this exit if r.exits[0,8].index(to) r.exits[idx] = nil next end end if not to if not @functions.include?(tag) $stderr.puts "Exit to #{tag} not found, ignored." end end r.exits[idx] = to } } @rooms.each { |r| r.exits.each_with_index { |e, dirA| next if not e or e.kind_of?(MapOneWay) next if e == r ## # Do we have a special exit? if dirA > 7 # First, get dirB and see if it is not a proper directional # exit dirB = nil if e.kind_of?(MapRoom) dirB = has_exit_to?(e, r) elsif e.kind_of?(MapDoor) t = e.location.find { |t| t != r.tag } to = [t] if to to.exits.each_with_index { |d, idx| if d == e or d == r dirB = idx break end } end end dirB = 4 if not dirB or dirB > 7 go = dirA - 7 r.exits[dirA] = nil dirA = best_dir(r, dirB) next if not dirA r.exits[dirA] = MapSpecialExit.new(go, e) end ############### # we have a room or door exit if e.kind_of?(MapRoom) # check if destination room also has a connection towards us. next if has_exit_to?(e, r) dirB = best_dir(e, dirA) next unless dirB e.exits[dirB] = MapOneWay.new(r) elsif e.kind_of?(MapDoor) t = e.location.find { |t| t != r.tag } found = false to = [t] if to to.exits.each_with_index { |d, idx| next if not d if d == e or d == r found = true break end } end if to and not found dirB = best_dir(to, dirA) next unless dirB to.exits[dirB] = MapOneWay.new(r) end elsif e.kind_of?(MapObject) else raise "Unknown exit type #{e.class}" end } } end |
#set_include_dirs ⇒ Object
258 259 |
# File 'lib/IFMapper/MapReader.rb', line 258 def set_include_dirs end |
#shift_link(room, dir) ⇒ Object
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 |
# File 'lib/IFMapper/MapReader.rb', line 450 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 # raise "Warning. Cannot shift connection for #{room}." end end |