Class: TADSWriter
- Inherits:
-
Object
- Object
- TADSWriter
- Defined in:
- lib/IFMapper/TADSWriter.rb
Constant Summary collapse
- DIRECTIONS =
[ 'north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest', ]
- OTHERDIRS =
[ '', 'up', 'down', 'in', 'out', ]
Class Method Summary collapse
-
.quote(text) ⇒ Object
Take a text and quote it for TADS’s double/single-quote text areas.
Instance Method Summary collapse
- #door(e) ⇒ Object
- #get_door_tag(e, r = nil) ⇒ Object
- #get_tag(elem, name = elem.name) ⇒ Object
-
#initialize(map, fileroot) ⇒ TADSWriter
constructor
A new instance of TADSWriter.
- #new_tag(elem, str) ⇒ Object
- #objects(r) ⇒ Object
- #room(r) ⇒ Object
- #section(sect, idx) ⇒ Object
- #start ⇒ Object
- #wrap_text(text, width = 74, indent = 79 - width) ⇒ Object
Constructor Details
#initialize(map, fileroot) ⇒ TADSWriter
Returns a new instance of TADSWriter.
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/IFMapper/TADSWriter.rb', line 362 def initialize(map, fileroot) @tags = {} @root = fileroot @base = File.basename(@root) @map = map keywords = [ 'Door', 'class', 'include', 'DarkRoom', 'Room', 'Thing', 'case', 'match', 'Platform', ] + DIRECTIONS @keyword = /^(?:#{keywords.join('|')})$/i start end |
Class Method Details
.quote(text) ⇒ Object
Take a text and quote it for TADS’s double/single-quote text areas.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/IFMapper/TADSWriter.rb', line 119 def self.quote(text) str = text.dup # Take text from Unicode utf-8 to iso-8859-1 if RUBY_VERSION < 1.9 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!(/"/, '\"') str.gsub!(/'/, "\\\\'") return str end |
Instance Method Details
#door(e) ⇒ Object
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 |
# File 'lib/IFMapper/TADSWriter.rb', line 167 def door(e) tag = get_door_tag(e) b = nil destination = '' if e.dir == Connection::BOTH a = e.roomA t = e.exitAtext b = e.roomB elsif e.dir == Connection::AtoB a = e.roomA destination = "\n destination = #{get_tag(e.roomB)}" t = e.exitAtext elsif e.dir == Connection::BtoA a = e.roomB destination = "\n destination = #{get_tag(e.roomA)}" t = e.exitBtext end roomA = get_tag(a) dirA = a.exits.index(e) if t > 0 dirA = dirAern = '' else d = DIRECTIONS[dirA] dirA = "#{d} " dirAern = "#{d}ern " end prop = '' if e.type == Connection::LOCKED_DOOR prop = 'LockableWithKey, ' elsif e.type == Connection::CLOSED_DOOR end @f.puts <<"EOF" // Door connecting #{e} #{tag} : #{prop}Door '#{dirA}#{dirAern}door' '#{dirAern}door' location = #{roomA}#{destination} ; EOF if b roomB = get_tag(b) dirB = b.exits.rindex(e) if e.exitBtext > 0 dirB = dirBern = '' else d = DIRECTIONS[dirB] dirB = "#{d} " dirBern = "#{d}ern " end @f.puts <<"EOF" #{tag}B : #{prop}Door -> #{tag} '#{dirB}#{dirBern}door' '#{dirBern}door' location = #{roomB} ; EOF end end |
#get_door_tag(e, r = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/IFMapper/TADSWriter.rb', line 27 def get_door_tag(e, r = nil) dirA = e.roomA.exits.index(e) dirB = e.roomB.exits.rindex(e) name = Room::DIRECTIONS[dirA] + "_" + Room::DIRECTIONS[dirB] name << "_door" tag = get_tag(e, name) tag += 'B' if r and r == e.roomB and e.dir == Connection::BOTH return tag end |
#get_tag(elem, name = elem.name) ⇒ Object
85 86 87 88 |
# File 'lib/IFMapper/TADSWriter.rb', line 85 def get_tag(elem, name = elem.name) return @tags[elem] if @tags[elem] return new_tag(elem, name) end |
#new_tag(elem, str) ⇒ Object
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 |
# File 'lib/IFMapper/TADSWriter.rb', line 37 def new_tag(elem, str) tag = str.dup # Take text from Unicode utf-8 to iso-8859-1 if RUBY_VERSION < 1.9 utf = Iconv.new( 'iso-8859-1', 'utf-8' ) tag = utf.iconv( tag ) else tag = tag.encode( 'utf-8', :invalid => :replace, :undef => :replace, :replace => '' ) end # Invalid tag characters, replaced with _ tag.gsub!(/[\s"'\/\\\-&#\,.:;!\?\n\(\)]/,'_') tag.gsub!(/__/, '') # remove reduntant __ repetitions tag.sub!(/^([\d]+)_?(.*)/, '\2\1') # No numbers allowed at start of tag # tag.downcase! # All tags are lowercase tag = tag[0..31] # Max. 32 chars. in tag # tag cannot be keyword (Doorway, Room, Class, etc) if tag =~ @keyword tag << '1' end if @tags.values.include?(tag) idx = 2 root = tag[0..29] # to leave room for number while @tags.values.include?(tag) tag = "#{root}#{idx}" idx += 1 end end if elem.kind_of?(String) @tags[tag] = tag else @tags[elem] = tag end return tag end |
#objects(r) ⇒ Object
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 |
# File 'lib/IFMapper/TADSWriter.rb', line 138 def objects(r) room = get_tag(r) objs = r.objects.split("\n") objs.each { |o| tag = new_tag(o, o) name = TADSWriter::quote(o) classname = 'Thing' # If name begins with uppercase, assume it is an NPC if name =~ /[A-Z]/ classname = 'Person' end @f.print <<"EOF" #{tag} : #{classname} '#{name}' '#{name}' @#{room} "#{name} is UNDER CONSTRUCTION." ; EOF } end |
#room(r) ⇒ Object
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 |
# File 'lib/IFMapper/TADSWriter.rb', line 235 def room(r) tag = get_tag(r) name = TADSWriter::quote(r.name) dark = r.darkness ? "Dark" : "" @f.puts <<-"EOF" //-------------------- #{r.name} #{tag} : #{dark}Room '#{name}' \"#{wrap_text(r.desc)}\" EOF # 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 a = e.roomB b = e.roomA else next if e.dir == Connection::BtoA text = e.exitAtext a = e.roomA b = e.roomB end end @f.print ' ' if text == 0 @f.print "#{DIRECTIONS[dir]} = " else @f.print "#{OTHERDIRS[text]} = " end if e.type == Connection::CLOSED_DOOR or e.type == Connection::LOCKED_DOOR @f.puts get_door_tag(e, r ) else @f.puts get_tag(b) end } @f.puts ";" objects(r) end |
#section(sect, idx) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/IFMapper/TADSWriter.rb', line 285 def section(sect, idx) @f = File.open("#@root-#{idx}.t", "w") @f.puts '//' + '=' * 78 @f.puts "// Section: #{sect.name} " @f.puts '//' + '=' * 78 sect.rooms.each { |r| room(r) } @f.puts @f.puts '//' + '-' * 78 @f.puts '// Doorways' @f.puts '//' + '-' * 78 sect.connections.each { |e| next if (e.type != Connection::LOCKED_DOOR and e.type != Connection::CLOSED_DOOR) door(e) } @f.close end |
#start ⇒ Object
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 |
# File 'lib/IFMapper/TADSWriter.rb', line 308 def start @f = File.open("#@root.t", "w") @f.puts '#charset "us-ascii"' @f.puts @f.puts '//' + '=' * 78 story = TADSWriter::quote(@map.name) story = 'Untitled' if story == '' today = Date.today start_location = '' if @map.sections.size > 0 and @map.sections[0].rooms[0] r = @map.sections[0].rooms[0] tag = get_tag(r) start_location = "location = #{tag}" end @f.puts <<"EOF" #include <adv3.h> #include <en_us.h> versionInfo: GameID name = '#{story}' byline = '#{@map.creator}' version = '1.0' release = '#{today}' ; me: Actor #{start_location} ; gameMain: GameMainDef initialPlayerChar = me ; EOF @f.puts '//' + '=' * 78 @f.puts "// Object classes" @f.puts '//' + '=' * 78 @f.puts "// Map sections" @map.sections.each_index { |idx| @f.puts "#include \"#@base-#{idx}.t\"" } @f.close @map.sections.each_with_index { |sect, idx| section(sect, idx) } end |
#wrap_text(text, width = 74, indent = 79 - width) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/IFMapper/TADSWriter.rb', line 92 def wrap_text(text, width = 74, indent = 79 - width) return 'UNDER CONSTRUCTION' if not text or text == '' str = TADSWriter::quote( text.dup ) if text.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 |