Class: IFMWriter
- Inherits:
-
Object
- Object
- IFMWriter
- Defined in:
- lib/IFMapper/IFMWriter.rb
Constant Summary collapse
- EXIT_TEXT =
[ '', 'up', 'down', 'in', 'out' ]
Instance Method Summary collapse
- #directions(c, a, b) ⇒ Object
- #export(file) ⇒ Object
- #get_tag(e, t) ⇒ Object
- #header ⇒ Object
-
#initialize(map, file) ⇒ IFMWriter
constructor
A new instance of IFMWriter.
- #link(c) ⇒ Object
-
#nolink(a, b) ⇒ Object
hook up two rooms that are not linked.
- #room(r) ⇒ Object
- #sections ⇒ Object
Constructor Details
#initialize(map, file) ⇒ IFMWriter
Returns a new instance of IFMWriter.
246 247 248 249 250 251 252 253 254 255 |
# File 'lib/IFMapper/IFMWriter.rb', line 246 def initialize(map, file) @link = nil @links = [] @rooms = [] @last_room = nil @tags = [] @elem = {} @map = map export(file) end |
Instance Method Details
#directions(c, a, b) ⇒ Object
30 31 32 33 34 35 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 |
# File 'lib/IFMapper/IFMWriter.rb', line 30 def directions(c, a, b) return if c.stub? @links << c if c.gpts.empty? # self connection if a == b idx = c.dirs[0] else # simple connection idx = a.next_to?(b) end dir = ' ' + Room::DIRECTIONS_ENGLISH[idx] else # complex path dir = '' pts = c.gpts.dup x = a.x y = a.y if c.roomA != a pts.reverse! end pts.each { |p| dx, dy = [ p[0] - x, p[1] - y ] idx = Room::vector_to_dir(dx, dy) dir += " #{Room::DIRECTIONS_ENGLISH[idx]}" x = p[0] y = p[1] } dx, dy = [ b.x - x, b.y - y ] idx = Room::vector_to_dir(dx, dy) dir += " #{Room::DIRECTIONS_ENGLISH[idx]}" end @f.print " dir#{dir}" end |
#export(file) ⇒ Object
239 240 241 242 243 244 |
# File 'lib/IFMapper/IFMWriter.rb', line 239 def export(file) @f = File.open(file, 'w') header sections @f.close end |
#get_tag(e, t) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/IFMapper/IFMWriter.rb', line 68 def get_tag(e, t) return @elem[e] if @elem.has_key?(e) tag = t.dup 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 tag.gsub!(/[\-\(\s,\.!'&"#$@\/\\\-\)]+/, '_') tag.gsub!(/__/, '') # remove reduntant __ repetitions tag.sub!(/^([\d]+)_?(.*)/, '\2\1') # No numbers allowed at start of tag tagname = tag idx = 2 while @tags.include?(tagname) or tagname.empty? or tagname.size == 1 tagname = tag + '_' + idx.to_s idx += 1 end @tags << tagname @elem[e] = tagname return tagname end |
#header ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/IFMapper/IFMWriter.rb', line 219 def header @f.puts <<-"HEADER" # # IFM map for #{@map.name} # Created by #{@map.creator} # #{Time.now} # # Map created using the Interactive Fiction Mapper # (C) 2005 - Gonzalo Garramuno # HEADER if @map.name.to_s != '' @f.puts "title \"#{@map.name}\";\n" end end |
#link(c) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/IFMapper/IFMWriter.rb', line 16 def link(c) return if @links.include?(c) # already spit out return if c.stub? a = c.roomA return if not a b = c.roomB roomA = @elem[a] roomB = @elem[b] @f.puts @f.print "link #{roomA} to #{roomB}" directions(c, a, b) @f.print ";\n" end |
#nolink(a, b) ⇒ Object
hook up two rooms that are not linked
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/IFMapper/IFMWriter.rb', line 96 def nolink(a, b) x = a.x y = a.y # find free room exit in a exit = 0 a.exits.each_with_index { |e, idx| next if e exit = idx break } dir = Room::DIRECTIONS_ENGLISH[exit] dx, dy = Room::DIR_TO_VECTOR[exit] x += dx y += dy while x != b.x or y != b.y dx = dy = 0 if b.x > x dx = 1 elsif b.x < x dx = -1 end if b.y > y dy = 1 elsif b.y < y dy = -1 end idx = Room::vector_to_dir(dx, dy) dir += " #{Room::DIRECTIONS[idx]}" x += dx y += dy end @f.print " dir #{dir} nolink" end |
#room(r) ⇒ Object
131 132 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 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 |
# File 'lib/IFMapper/IFMWriter.rb', line 131 def room(r) return if @rooms.include?(r) or r == nil @rooms << r tag = get_tag(r, r.name) @f.puts @f.print "room \"#{r.name}\" tag #{tag}" if not @link.empty? if @link[3] nolink(@last_room, r) else directions(@link[0], @link[1], @link[2]) if @last_room and @link[1] != @last_room tag = get_tag(@link[1], @link[1].name) @f.print " from #{tag}" end if @link[0].exitAtext != 0 dir = EXIT_TEXT[@link[0].exitAtext] @f.print " go #{dir}" end if @link[0].dir == Connection::AtoB @f.print " oneway" end if @link[0].type == Connection::SPECIAL @f.print " style special" end end end @f.print ";\n" @last_room = r objs = r.objects.split("\n") objs.each { |obj| tag = get_tag(obj, obj) @f.puts "\titem \"#{obj}\" tag #{tag};" } tasks = r.tasks.split("\n") tasks.each { |task| tag = get_tag(task, task) @f.puts "\ttask \"#{task}\" tag #{tag};" } r.exits.each { |c| next if not c or @links.include?(c) if c.roomA == c.roomB # self-link link(c) next end if r == c.roomA @link = [c, c.roomA, c.roomB] room(c.roomB) # Recurse along this branch... else @link = [c, c.roomB, c.roomA] room(c.roomA) # Recurse along this branch... end } end |
#sections ⇒ Object
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 |
# File 'lib/IFMapper/IFMWriter.rb', line 193 def sections old_section = @map.section @map.sections.each_with_index { |section, idx| @f.puts @f.puts '#' * 79 @f.puts if section.name.to_s == '' if @map.sections.size > 1 @f.puts "map \"Section #{idx+1}\";" end else @f.puts "map \"#{section.name}\";" end @map.section = idx @link = [] section.rooms.each { |r| room(r) @link << 'nolink' } section.connections.each { |c| link(c) } } @map.section = old_section end |