Class: TrizbortReader

Inherits:
Object
  • Object
show all
Defined in:
lib/IFMapper/TrizbortReader.rb

Overview

Class that allows importing a Trizbort map file.

Defined Under Namespace

Classes: MapError, ParseError

Constant Summary collapse

W =
128
H =
64
DIRS =
{ 
  'n' => 0, # n
  's' => 4, # s
  'e' => 2, # e
  'w' => 6, # w
  'ne' => 1, # ne
  'nw' => 7, # nw
  'se' => 3, # se
  'sw' => 5, # sw

  # Trizbort also support connections to the sides of centers
  #
  #  --*--*--
  #  *      *
  #  *      *
  #  --*--*--
  #
  # We translate these as corner connections.
  #
  'ene' => [2, 1], #   ----*-
  'ese' => [2, 3], #   ----*-
  'nne' => [0, 1],
  'wnw' => [6, 7],
  'wsw' => [6, 5],
  'nnw' => [0, 7],
  'ssw' => [4, 5],
}
@@debug =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, map = Map.new('Trizbort Imported Map')) ⇒ TrizbortReader

Returns a new instance of TrizbortReader.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/IFMapper/TrizbortReader.rb', line 248

def initialize(file, map = Map.new('Trizbort Imported Map'))
  @map = map
  
  f = File.open( file )
  @doc = REXML::Document.new f

  parse

  reposition

  @map.fit
  @map.section = 0

  if @map.kind_of?(FXMap)
    @map.filename   = file.sub(/\.trizbort$/i, '.map')
    @map.navigation = false
    @map.window.show
  end
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



51
52
53
# File 'lib/IFMapper/TrizbortReader.rb', line 51

def doc
  @doc
end

Instance Method Details

#debug(x) ⇒ Object



55
56
57
58
59
# File 'lib/IFMapper/TrizbortReader.rb', line 55

def debug(x)
  if @@debug.to_i > 0
    $stderr.puts x
  end
end

#parseObject

Read Trimap rexml from file stream



64
65
66
67
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
94
95
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
130
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/IFMapper/TrizbortReader.rb', line 64

def parse
  
  rooms = {}


  @doc.elements.each('trizbort/info/title') { |e|
    @map.name = e.text
  }

  @doc.elements.each('trizbort/info/author') { |e|
    @map.creator = e.text
  }

  @doc.elements.each('trizbort/map/room') { |e|
    id = e.attributes['id'].to_i
    name = e.attributes['name'].to_s
    x = e.attributes['x'].to_i
    y = e.attributes['y'].to_i
    w = e.attributes['w'].to_i
    h = e.attributes['h'].to_i
    r = @map.new_room(x, y)
    r.name = name
    darkness = e.attributes['isDark']
    if darkness == 'yes'
      r.darkness = true
    end
    desc = e.attributes['description']
    r.desc = desc.to_s
    r.desc.gsub!("\r\n", "\n")
    rooms[id] = r
    e.elements.each('objects') { |o|
      r.objects = o.text.to_s.gsub('|',"\n")
    }
  }

  @doc.elements.each('trizbort/map/line') { |e|
    style = e.attributes['style'].to_s

    if style == 'dashed'
      style = Connection::SPECIAL
    else
      style = Connection::FREE
    end

    id = e.attributes['id'].to_i
    flow = e.attributes['flow']

    if flow == 'oneWay'
      flow = Connection::AtoB
    else
      flow = Connection::BOTH
    end

    startText = e.attributes['startText'].to_s
    endText = e.attributes['endText'].to_s

    line = {}

    e.elements.each { |x|
      name = x.local_name
      if name == 'dock'
        port = x.attributes['port']
        room = x.attributes['id'].to_i
        index = x.attributes['index'].to_i
        line[index] = [port, rooms[room]]
      elsif name == 'point'
        index = x.attributes['index'].to_i
        line[index] = [nil, nil]
      end
    }

    if line.size > 0
      roomA = line[0][1]
      dirA  = DIRS[line[0][0]]

      if dirA.kind_of?(Array)
        dirA.each { |d|
          next if roomA[d]
          dirA = d
          break
        }
        if dirA.kind_of? Array
          dirA = dirA[0]
        end
      end

      if line.size > 1
        roomB = line[1][1]
        dirB  = DIRS[line[1][0]]

        if dirB.kind_of? Array
          dirB.each { |d|
            next if roomB[d]
            dirB = d
            break
          }
          if dirB.kind_of?(Array)
            dirB = dirB[0]
          end
        end
      end

      debug "Connect: #{roomA} ->#{roomB} "
      debug "dirA: #{dirA} dirB: #{dirB}"

      if startText =~ /^up?/i
        startText = 1
      elsif startText =~ /^d(?:own)?/i
        startText = 2
      elsif startText =~ /^in?/i
        startText = 3
      elsif startText =~ /^o(?:ut)?/i
        startText = 4
      else
        startText = 0
      end

      if endText =~ /^up?/i
        endText = 1
      elsif endText =~ /^d(?:own)?/i
        endText = 2
      elsif endText =~ /^in?/i
        endText = 3
      elsif endText =~ /^o(?:ut)?/i
        endText = 4
      else
        endText = 0
      end

      debug "exitA: #{startText} exitB: #{endText}"

      begin
        c = @map.new_connection( roomA, dirA, roomB, dirB )
        c.exitAtext = startText
        if roomB
          c.exitBtext = endText
        end
        c.type = style
        c.dir = flow
        debug c
      rescue Section::ConnectionError
      end
    end
    
  }

end

#repositionObject



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
# File 'lib/IFMapper/TrizbortReader.rb', line 214

def reposition
  @map.sections.each do |sect|
    minXY, = sect.min_max_rooms
    sect.rooms.each do |r|
      r.x -= minXY[0]
	r.y -= minXY[1]
	r.x /= (W+32)
	r.y /= (H+32)
    end
  end

  @map.sections.each do |sect|
    sect.rooms.each_with_index do |r, idx|
      x = r.x
      y = r.y
      xf = x.floor
      yf = y.floor
      if x != xf or y != yf
        sect.rooms.each do |r2|
          next if r == r2
          if xf == r2.x.floor and yf == r2.y.floor
            dx = (x - xf).ceil
            dy = (y - yf).ceil
            sect.shift(x, y, dx, dy)
            break
          end
        end
      end
      r.x = r.x.floor.to_i
      r.y = r.y.floor.to_i
    end
  end
end