Class: Inform7Writer
- Inherits:
-
Object
- Object
- Inform7Writer
- Defined in:
- lib/IFMapper/Inform7Writer.rb
Constant Summary collapse
- DIRECTIONS =
[ 'North', 'Northeast', 'East', 'Southeast', 'South', 'Southwest', 'West', 'Northwest', ]
- OTHERDIRS =
[ '', 'Above', 'Below', 'Inside', 'Outside', ]
- IGNORE_WORDS =
[ 'a', 'the', 'and', 'of', 'your', 'to', 'out', 'in' ]
- IGNORED_ARTICLES =
/^(?:#{IGNORE_WORDS.join('|')})$/
- KEYWORDS =
- INVALID_KEYWORD =
/\b(?:#{KEYWORDS.join('|')})\b/i
- LOCATION_NAMES =
[ 'door', 'include', 'room', 'has', 'with', 'is', 'container', ] + DIRECTIONS
- INVALID_LOCATION_NAME =
/^(?:#{LOCATION_NAMES.join('|')})$/i
- ANIMALS =
Some common animals in adventure games
[ 'horse', 'donkey', 'mule', 'goat', # lizards 'lizard', 'snake', 'turtle', # fishes 'fish', 'dolphin', 'whale', 'tortoise', # dogs 'dog', 'wolf', # cats 'cat', 'leopard', 'tiger', 'lion', # fantastic 'grue', # birds 'bird', 'pigeon', 'peacock', 'eagle', ]
- IS_ANIMAL =
/\b(?:#{ANIMALS.join('|')})\b/i
- MALE_PEOPLE =
[ 'boy', 'man', 'sir', ]
- IS_MALE_PERSON =
/\b(?:#{MALE_PEOPLE.join('|')})\b/i
- FEMALE_PEOPLE =
[ 'girl', 'woman', 'lady', 'actress', ]
- IS_FEMALE_PERSON =
/\b(?:#{FEMALE_PEOPLE.join('|')})\b/i
- PEOPLE =
MALE_PEOPLE + FEMALE_PEOPLE + [ 'child', 'attendant', 'doctor', 'actor', 'character', 'engineer', 'bum', 'nerd', 'ghost', ]
- IS_PERSON =
/\b(?:#{PEOPLE.join('|')})\b/i
- CONTAINERS =
Some common container types
[ 'box', 'cup', 'crate', 'tin', 'can', 'chest', 'wardrobe', 'jacket', 'suit', 'trophy case', 'coffin', 'briefcase', 'suitcase', 'bag', 'flask', ]
- IS_CONTAINER =
/\b(?:#{CONTAINERS.join('|')})\b/i
- UNLIT_TYPES =
Some common lit types
[ 'lantern', 'torch', ]
- IS_LIGHT =
/\b(?:#{UNLIT_TYPES.join('|')})\b/i
- CLOTHES =
Some common wearable types
[ 'cape', 'glove', 'jeans', 'trousers', 'hat', 'gown', 'backpack', 'shirt', 'jacket', 'suit', 'ring', 'bracelet', 'amulet', 'locket', ]
- IS_WEARABLE =
/\b(?:#{CLOTHES.join('|')})\b/i
- SUPPORTERS =
Some common supporter types
[ 'table', 'shelf', ]
- IS_SUPPORTER =
/\b(?:#{SUPPORTERS.join('|')})\b/i
- FOOD =
Some common edible types
[ 'bread', 'cake', # drinks 'water', 'soda', 'beer', 'beverage', 'potion', # fruits 'fruit', 'apple', 'orange', 'banana', 'almond', 'nut', ]
- IS_EDIBLE =
/\b(?:#{FOOD.join('|')})\b/i
Instance Method Summary collapse
- #door(e) ⇒ Object
- #get_door_name(e) ⇒ Object
- #get_door_tag(e) ⇒ Object
- #get_room_tag(elem) ⇒ Object
- #get_tag(elem, name) ⇒ Object
-
#inform_quote(text) ⇒ Object
Take a text and quote it for inform’s double-quote text areas.
-
#initialize(map, fileroot) ⇒ Inform7Writer
constructor
A new instance of Inform7Writer.
- #new_tag(elem, str) ⇒ Object
- #objects(r) ⇒ Object
- #room(r) ⇒ Object
- #section(sect, idx) ⇒ Object
- #start ⇒ Object
- #wrap_text(text, width = 75, indent = 78 - width) ⇒ Object
Constructor Details
#initialize(map, fileroot) ⇒ Inform7Writer
Returns a new instance of Inform7Writer.
581 582 583 584 585 586 587 588 |
# File 'lib/IFMapper/Inform7Writer.rb', line 581 def initialize(map, fileroot) = {} @root = fileroot @base = File.basename(@root) @map = map start end |
Instance Method Details
#door(e) ⇒ Object
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 |
# File 'lib/IFMapper/Inform7Writer.rb', line 437 def door(e) name = get_door_name(e) tag = get_tag(e, name) roomA = get_room_tag(e.roomA) roomB = get_room_tag(e.roomB) dirA = e.roomA.exits.index(e) dirB = e.roomB.exits.rindex(e) dirA = DIRECTIONS[dirA].downcase dirB = DIRECTIONS[dirB].downcase props = '' if e.type == Connection::LOCKED_DOOR props = "It is locked." elsif e.type == Connection::CLOSED_DOOR props = "It is closed." end found_in = 'It is ' if e.dir == Connection::BOTH found_in << "#{dirA} of #{roomA} and #{dirB} of #{roomB}" elsif e.dir == Connection::AtoB found_in << "#{dirA} of #{roomA}. Through it is #{roomB}" elsif e.dir == Connection::BtoA found_in << "#{dirB} of #{roomB}. Through it is #{roomA}" end @f.puts "\nThe \#{tag} is a door. \#{props} \nThe printed name of \#{tag} is \"a \#{name}\".\nUnderstand \"\#{name}\" as \#{tag}.\nUnderstand \"door\" as \#{tag}.\n\#{found_in}.\n" end |
#get_door_name(e) ⇒ Object
281 282 283 284 285 286 |
# File 'lib/IFMapper/Inform7Writer.rb', line 281 def get_door_name(e) dirA = e.roomA.exits.index(e) dirB = e.roomB.exits.rindex(e) name = DIRECTIONS[dirA].downcase + "-" + DIRECTIONS[dirB].downcase name << " door" end |
#get_door_tag(e) ⇒ Object
288 289 290 291 292 |
# File 'lib/IFMapper/Inform7Writer.rb', line 288 def get_door_tag(e) name = get_door_name(e) name.upcase get_tag(e, name) end |
#get_room_tag(elem) ⇒ Object
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/IFMapper/Inform7Writer.rb', line 270 def get_room_tag(elem) return [elem] if [elem] tag = elem.name if tag =~ INVALID_KEYWORD or .values.include?(tag) tag = new_tag(elem, elem.name) else [elem] = tag end return tag end |
#get_tag(elem, name) ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/IFMapper/Inform7Writer.rb', line 259 def get_tag(elem, name) return [elem] if [elem] tag = name if tag =~ INVALID_KEYWORD or .values.include?(tag) return new_tag(elem, name) else [elem] = tag end return tag end |
#inform_quote(text) ⇒ Object
Take a text and quote it for inform’s double-quote text areas.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/IFMapper/Inform7Writer.rb', line 321 def inform_quote(text) str = text.dup # Take text from Unicode utf-8 to iso-8859-1859-1 if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('1.9.0') utf = Iconv.new( 'iso-8859-1', 'utf-8' ) str = utf.iconv( str ) else str = str.encode( 'utf-8', :invalid => :replace, :undef => :replace, :replace => '' ) end # Quote special characters # str.gsub!(/@/, '@@64') str.gsub!(/"/, '\'') # str.gsub!(/~/, '@@126') # str.gsub!(/\\/, '@@92') # str.gsub!(/\^/, '@@94') return str end |
#new_tag(elem, str) ⇒ Object
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 |
# File 'lib/IFMapper/Inform7Writer.rb', line 210 def new_tag(elem, str) tag = str.dup # Take text from Unicode utf-8 to iso-8859-1 if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('1.9.0') utf = Iconv.new( 'iso-8859-1', 'utf-8' ) tag = utf.iconv( tag ) else tag = tag.encode( 'utf-8', :invalid => :replace, :undef => :replace, :replace => '' ) end # Remove redundant spaces tag.sub!(/^\s/, '') tag.sub!(/\s$/, '') # Invalid tag characters, replaced with _ tag.gsub!(/[\s"\\\#\&,\.:;!\?\n\(\)]+/,'_') tag.sub!(/^([\d]+)_?(.*)/, '\2\1') # No numbers allowed at start of tag tag.gsub!(/__/, '_') # tag cannot be repeated and cannot be keyword (Doorway, Room, etc) # In those cases, we add a number to the tag name. idx = 0 if .values.include?(tag) or tag =~ INVALID_LOCATION_NAME idx = 1 end if idx > 0 root = tag.dup tag = "#{root}#{idx}" while .values.include?(tag) tag = "#{root}#{idx}" idx += 1 end end if elem.kind_of?(String) [tag] = tag else [elem] = tag end return tag end |
#objects(r) ⇒ Object
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 |
# File 'lib/IFMapper/Inform7Writer.rb', line 343 def objects(r) room = get_room_tag(r) objs = r.objects.split("\n") objs.each { |o| tag = get_tag(o, o) names = o.dup names.gsub!(/"/, '') # remove any quotes names.gsub!(/\b\w\b/, '') # remove single letter words name = names names = name.split(' ') article = 'a ' if name =~ /ves$/ or name =~ /s$/ article = 'some ' elsif name =~ /^[aeiou]/ article = 'an ' end # If name begins with uppercase, assume it is an NPC type = 'a thing' if name =~ IS_ANIMAL type = 'an animal' elsif name =~ IS_PERSON article = 'a ' type = 'a person' if name =~ IS_MALE_PERSON type = 'a male person' elsif name =~ IS_FEMALE_PERSON type = 'a female person' end elsif name =~ /[A-Z]/ if name !~ /'/ # possesive, like Michael's wallet # if too many words, probably a book's title if names.size <= 3 article = '' if name =~ /^(?:Miss|Mrs)/ type = 'a woman' else type = 'a person' end end end end props = '' if tag != name props << "\n The printed name of #{tag} is \"#{article}#{name}\"." else tag = article + tag end if name =~ IS_LIGHT props << "\n It is not lit." end if name =~ IS_CONTAINER props << "\n It is a container." end if name =~ IS_SUPPORTER props << "\n It is a supporter." end if name =~ IS_WEARABLE props << "\n It is wearable." end if name =~ IS_EDIBLE props << "\n It is edible." end @f.print "\nIn \#{room}, there is \#{type} called \#{tag}. \#{props}\n The description of \#{tag} is \\\"UNDER CONSTRUCTION.\\\"\n" if names.size > 1 names.each { |n| next if n =~ IGNORED_ARTICLES @f.puts " Understand \"#{n}\" as #{tag}." } end } end |
#room(r) ⇒ Object
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 |
# File 'lib/IFMapper/Inform7Writer.rb', line 475 def room(r) tag = get_room_tag(r) name = r.name prop = '' if r.darkness prop << 'dark ' end @f.print "\n#{tag} is a #{prop}room." if name != tag name = inform_quote(name) @f.print " The printed name of #{tag} is \"#{name}\"." end @f.puts @f.puts " \"#{wrap_text(r.desc)}\"" # Now, handle exits... r.exits.each_with_index { |e, dir| next if (not e) or e.stub? or e.type == Connection::SPECIAL if e.loop? text = e.exitAtext b = e.roomB else if e.roomB == r next if e.dir == Connection::AtoB text = e.exitBtext b = e.roomA else next if e.dir == Connection::BtoA text = e.exitAtext b = e.roomB end end @f.print ' ' if text == 0 @f.print "#{DIRECTIONS[dir]} is " else @f.print "#{OTHERDIRS[text]} is " end if e.type == Connection::CLOSED_DOOR or e.type == Connection::LOCKED_DOOR @f.print get_door_tag(e) else @f.print get_room_tag(b) end @f.puts '.' } objects(r) end |
#section(sect, idx) ⇒ Object
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/IFMapper/Inform7Writer.rb', line 529 def section(sect, idx) name = sect.name name = 'Unnamed' if name.to_s == '' @f.puts @f.puts "Section #{idx+1} - #{name}" @f.puts @f.puts @f.puts "Part 1 - Room Descriptions" @f.puts sect.rooms.each { |r| room(r) } @f.puts @f.puts @f.puts @f.puts "Part 2 - Doors" @f.puts sect.connections.each { |e| next if (e.type != Connection::LOCKED_DOOR and e.type != Connection::CLOSED_DOOR) door(e) } end |
#start ⇒ Object
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 |
# File 'lib/IFMapper/Inform7Writer.rb', line 554 def start @f = File.open("#@root.inform", "w") story = @map.name story = 'Untitled' if story == '' today = Date.today serial = today.strftime("%y%m%d") @f.puts @f.puts "\"#{story}\" by \"#{@map.creator}\"" @f.puts @f.puts "The story genre is \"Unknown\". The release number is 1.\nThe story headline is \"An Interactive Fiction\".\nThe story description is \"\".\nThe story creation year is \#{today.year}.\n\n\nUse full-length room descriptions. \n\n" @map.sections.each_with_index { |sect, idx| section(sect, idx) } @f.close end |
#wrap_text(text, width = 75, indent = 78 - width) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/IFMapper/Inform7Writer.rb', line 294 def wrap_text(text, width = 75, indent = 78 - width) return 'UNDER CONSTRUCTION.' if not text or text == '' str = inform_quote( text ) if str.size > width r = '' while str if str.size >= width idx = str.rindex(/[ -]/, width) idx = str.size unless idx else idx = str.size end r << str[0..idx] str = str[idx+1..-1] r << "\n" << ' ' * indent if str end return r else return str end end |