Class: InformWriter
- Inherits:
-
Object
- Object
- InformWriter
- Defined in:
- lib/IFMapper/InformWriter.rb
Constant Summary collapse
- DIRECTIONS =
[ 'n_to', 'ne_to', 'e_to', 'se_to', 's_to', 'sw_to', 'w_to', 'nw_to', ]
- OTHERDIRS =
[ '', 'u_to', 'd_to', 'in_to', 'out_to', ]
Instance Method Summary collapse
- #door(e) ⇒ Object
- #get_door_tag(e) ⇒ Object
- #get_tag(elem, name = elem.name) ⇒ Object
-
#inform_quote(text) ⇒ Object
Take a text and quote it for inform’s double-quote text areas.
-
#initialize(map, fileroot) ⇒ InformWriter
constructor
A new instance of InformWriter.
- #new_tag(elem, str) ⇒ Object
- #objects(r) ⇒ Object
- #room(r) ⇒ Object
- #section(sect, idx) ⇒ Object
- #start ⇒ Object
- #wrap_text(text, width = 65, indent = 79 - width) ⇒ Object
Constructor Details
#initialize(map, fileroot) ⇒ InformWriter
Returns a new instance of InformWriter.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/IFMapper/InformWriter.rb', line 355 def initialize(map, fileroot) = {} @root = fileroot @base = File.basename(@root) @map = map keywords = [ 'doorway', 'class', 'include', 'room', 'has', 'with', 'description', ] + DIRECTIONS @keyword = /^(?:#{keywords.join('|')})$/i start end |
Instance Method Details
#door(e) ⇒ Object
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 |
# File 'lib/IFMapper/InformWriter.rb', line 166 def door(e) tag = get_door_tag(e) roomA = get_tag(e.roomA) roomB = get_tag(e.roomB) dirA = e.roomA.exits.index(e) dirB = e.roomB.exits.rindex(e) dirA = Room::DIRECTIONS[dirA] dirB = Room::DIRECTIONS[dirB] props = '' if e.type == Connection::LOCKED_DOOR props = "\n has lockable locked" elsif e.type == Connection::CLOSED_DOOR props = "\n has ~open" end if e.dir == Connection::BOTH found_in = "#{roomA} #{roomB}" elsif e.dir == Connection::AtoB found_in = roomA elsif e.dir == Connection::BtoA found_in = roomB end @f.puts "\n! Door connecting \#{e}\nDoorway \#{tag} \"door\"\n with name 'door',\n side1_to \#{roomA},\n side1_dir \#{dirA}_to,\n side2_to \#{roomB},\n side2_dir \#{dirB}_to,\n found_in \#{found_in}\#{props};\n" end |
#get_door_tag(e) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/IFMapper/InformWriter.rb', line 28 def get_door_tag(e) dirA = e.roomA.exits.index(e) dirB = e.roomB.exits.rindex(e) name = Room::DIRECTIONS[dirA] + "_" + Room::DIRECTIONS[dirB] name << "_door" get_tag(e, name) end |
#get_tag(elem, name = elem.name) ⇒ Object
78 79 80 81 |
# File 'lib/IFMapper/InformWriter.rb', line 78 def get_tag(elem, name = elem.name) return [elem] if [elem] return new_tag(elem, name) end |
#inform_quote(text) ⇒ Object
Take a text and quote it for inform’s double-quote text areas.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/IFMapper/InformWriter.rb', line 111 def inform_quote(text) str = text.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' ) str = utf.iconv( str ) else str = str.encode( 'utf-8', :invalid => :replace, :undef => :replace, :replace => '' ) end # Quote special characters str.gsub!(/@/, '@@64') str.gsub!(/"/, '@@34') str.gsub!(/~/, '@@126') str.gsub!(/\\/, '@@92') str.gsub!(/\^/, '@@94') return str end |
#new_tag(elem, str) ⇒ Object
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 |
# File 'lib/IFMapper/InformWriter.rb', line 36 def new_tag(elem, str) tag = str.dup 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 # 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 (inform limit) # tag cannot be keyword (Doorway, Room, Class, etc) if tag =~ @keyword tag << '1' end if .values.include?(tag) idx = 2 root = tag[0..29] # to leave room for number 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
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 |
# File 'lib/IFMapper/InformWriter.rb', line 133 def objects(r) room = get_tag(r) objs = r.objects.split("\n") objs.each { |o| tag = new_tag(o, o) names = o.dup names.gsub!(/"/, '') # remove any quotes names.gsub!(/'/, '^') # change ' to ^ (inform convention) names.gsub!(/\b\w\b/, '') # remove single letter words names = names.split(' ') names = "'" + names.join("' '") + "'" name = inform_quote(o) classname = 'Object' # If name begins with uppercase, assume it is an NPC if name =~ /[A-Z]/ classname = 'NPC' end @f.print "\n\#{classname} \#{tag} \"\#{name}\" \#{room}\n with name \#{names},\n description \"\#{name} is UNDER CONSTRUCTION.\";\n" } end |
#room(r) ⇒ Object
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 |
# File 'lib/IFMapper/InformWriter.rb', line 205 def room(r) tag = get_tag(r) name = inform_quote(r.name) @f.puts "\n!-------------------- \#{r.name}\nRoom \#{tag} \"\#{name}\"\n" @f.puts " with description" @f.print " \"#{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.puts ',' @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.print get_door_tag(e) else @f.print get_tag(b) end } if r.darkness @f.print " has ~light" end @f.puts ";" objects(r) end |
#section(sect, idx) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/IFMapper/InformWriter.rb', line 256 def section(sect, idx) @f = File.open("#@root-#{idx}.inf", "w") @f.puts '!' + '=' * 78 @f.puts "! Section: #{sect.name} " @f.puts '!' + '=' * 78 sect.rooms.each { |r| room(r) } @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
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 |
# File 'lib/IFMapper/InformWriter.rb', line 276 def start @f = File.open("#@root.inf", "w") @f.puts '!' + '=' * 78 story = @map.name story = 'Untitled' if story == '' today = Date.today serial = today.strftime("%y%m%d") @f.puts "Constant Story \"\#{story}\";\nConstant Headline\n \"^A game map done with IFMapper\n ^by \#{@map.creator}.^\";\nRelease 1; \nSerial \"\#{serial}\"; ! \#{Time.now}\n\n! These constants are to make the game also potentially Glulx compatible\n#ifndef WORDSIZE;\nConstant TARGET_ZCODE;\nConstant WORDSIZE 2;\n#endif;\n\n!Constant ZDEBUG;\n\nInclude \"Parser\";\nInclude \"VerbLib\";\n\n\n" @f.puts '!' + '=' * 78 @f.puts "! Object classes" @f.puts "\n! We use Andrew MacKinnon's EasyDoors for our doors.\nInclude \"easydoors\";\n\n! We define a class for NPC (non-player characters)\nclass NPC\nhas animate;\n\n" @f.puts '!' + '=' * 78 @f.puts "! Map sections" @map.sections.each_index { |idx| @f.puts "#include \"#@base-#{idx}.inf\";" } 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 "!============================================================================\n! Entry point routines\n\n[ Initialise;\n\#{start_location};\nlookmode = 2; ! verbose\ninventory_style = FULLINV_BIT + ENGLISH_BIT + RECURSE_BIT; ! wide\n];\n"; @f.puts "!============================================================================\n! Standard and extended grammar\n\nInclude \"Grammar\";\n" @f.close @map.sections.each_with_index { |sect, idx| section(sect, idx) } end |
#wrap_text(text, width = 65, indent = 79 - width) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/IFMapper/InformWriter.rb', line 84 def wrap_text(text, width = 65, indent = 79 - width) return 'UNDER CONSTRUCTION' unless text str = inform_quote( text ) 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 |