Class: GUEReader

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

Overview

Class that allows importing a GUE map file.

Defined Under Namespace

Classes: Complex, MapError, ParseError

Constant Summary collapse

GUE_MAGIC =

This is the GUEmap magic number/ID at the beginning of the file.

"\307U\305m\341p"
DIRS =
{ 
  0 => 0, # n
  1 => 4, # s
  2 => 2, # e
  3 => 6, # w
  4 => 1, # ne
  5 => 7, # nw
  6 => 3, # se
  7 => 5, # sw

  # GUE Map also supports connections to the sides of centers
  #
  #  -*--*-
  #  |    |
  #  -*--*-
  #
  # We translate these as corner connections.
  #
  8  => [0, 1], #   ----*-
  9  => [0, 7],
  10 => [4, 3], #   ----*-
  11 => [4, 5],

  # DOT directions
  # 12 => 4,
}
@@debug =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, map = Map.new('GUE Imported Map')) ⇒ GUEReader

Returns a new instance of GUEReader.



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/IFMapper/GUEReader.rb', line 419

def initialize(file, map = Map.new('GUE Imported Map'))
  @map     = map
  @complex = []

  @f = File.open(file, 'rb')
  parse
  
  reposition
  remove_dots

  @map.fit
  @map.section = 0
  if @map.kind_of?(FXMap)
    @map.filename   = file.sub(/\.gmp$/i, '.map')
    @map.navigation = true
    @map.window.show
  end
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



50
51
52
# File 'lib/IFMapper/GUEReader.rb', line 50

def map
  @map
end

Instance Method Details

#debug(x) ⇒ Object



54
55
56
57
58
# File 'lib/IFMapper/GUEReader.rb', line 54

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

#headerObject

Read GUEMap header from file stream



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

def header
  magic = @f.read(6)
  if magic != GUE_MAGIC
    raise ParseError, "Not a GUEMap"
  end
  @f.read(4)
  version = @f.read(2).unpack('s')[0]
  debug "VERSION: #{version}"
  case version
  when 4
    crap = @f.read(2)
    crap = @f.read(1)[0]
    if crap == 32
	len = @f.read(1)[0]
	@map.name = @f.read(len)
	crap = @f.read(1)[0]
    end
    if crap == 40
	len = @f.read(1)[0]
	if len == 255
 len = @f.read(2).unpack('s')[0]
	end
	@f.read(len)
	crap = @f.read(1)[0]
    end
    hdr = @f.read(15)
  when 3,2
    crap = @f.read(2)
    crap = @f.read(1)[0]
    if crap == 32
	len = @f.read(1)[0]
	@map.name = @f.read(len)
	crap = @f.read(1)[0]
    end
    if crap == 40
	len = @f.read(1)[0]
	if len == 255
 len = @f.read(2).unpack('s')[0]
	end
	@f.read(len)
	crap = @f.read(1)[0]
    end
    hdr = @f.read(8)
  else
    raise ParseError, "Unknown GUEMap version #{version}"
  end
end

#parseObject

Main parsing loop.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/IFMapper/GUEReader.rb', line 305

def parse
  header
  while not @f.eof?
    type = @f.read(1)[0]
    data = @f.read(1)[0]

    debug "type: #{type}"
    debug "data: #{data}"

    case type
    when 2
	read_room
    when 3
	read_connection
    when 4
	break
    else
	raise "Unknown type: #{type}"
    end
  end
end

#read_connectionObject

Read a connection from file stream



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

def read_connection
  type = Connection::FREE
  dir  = Connection::BOTH
  dA   = @f.read(1)[0]
  a    = @f.read(1)[0]
  stub = false

  opt = @f.read(1)[0]
  exitAtext = exitBtext = 0

  case opt
  when 26
    #normal connection
  when 34
    # up/down/in/out connection
    exitAtext = @f.read(1)[0]
    if exitAtext & 8 != 0
	exitAtext -= 8
	type = Connection::SPECIAL
    end
    if exitAtext & 16 != 0
	# no exit connection
	stub = true
	exitAtext -= 16
	b  = a
	dB = dA
    end
    if exitAtext & 32 != 0
	# stub connection
	stub = true
	exitAtext -= 32
    end
    if exitAtext & 64 != 0
	exitAtext -= 64
	dir = Connection::AtoB
    end
    exitBtext = @f.read(1)[0]
    data      = @f.read(1)[0]
  else
    raise ParseError, "option: #{opt} unknown"
  end

  unless stub
    dB    = @f.read(1)[0]
    b     = @f.read(1)[0]
    opt   = @f.read(1)[0]
  end

  data    = @f.read(1)[0]
  data    = @f.read(1)[0] unless @f.eof?

  section = @map.sections[@map.section]
  roomA = section.rooms[a]
  roomB = section.rooms[b] if b


  debug "Index  : #{a}->#{b}"
  debug "dA:   #{dA}   dB: #{dB}"
  if roomA.name == '*'
    if roomB.x != roomA.x or roomB.y != roomA.y
	dirA = roomA.exit_to(roomB)
    else
	dirA = 0
    end
  else
    dirA = DIRS[dA]
    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
    raise "Unknown direction #{dA}" unless dirA
  end

  if b
    if roomB.name == '*'
	dirB = (dirA + 4) % 8
    else
	dirB = DIRS[dB]
	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
	raise "Unknown direction #{dB}" unless dirB
    end
  end


  if roomA[dirA]
    dirA = roomA.exits.index(nil)
  end

  if roomB
    if roomB[dirB]
	dirB = roomB.exits.rindex(nil)
    end
  end


  debug "Connect: #{roomA} ->#{roomB} "
  debug "dirA: #{dirA} dirB: #{dirB}"
  debug "exitA: #{exitAtext} exitB: #{exitBtext}"

  begin
    c = map.new_connection( roomA, dirA, roomB, dirB )

    c.exitAtext = exitAtext
    c.exitBtext = exitBtext
    c.dir       = dir
    c.type      = type
    
    debug c
  rescue Section::ConnectionError
  end

end

#read_roomObject

Read a room from file stream



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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/IFMapper/GUEReader.rb', line 246

def read_room
  x       = @f.read(1)[0]
  data    = @f.read(1)
  y       = @f.read(1)[0]
  data    = @f.read(1)[0]

  type    = @f.read(1)[0]

  case type
  when 41
    r = @map.new_room(x, y)
    r.name = "*"
    data  = @f.read(4)
    if data[0] == 0
	r.name = ''
    end
    return
  when 0
    r = @map.new_room(x, y)
    r.name = ''
    @f.read(2)
    return
  end

  len  = @f.read(1)[0]
  name = ''
  comment = ''

  name = @f.read(len)
  test = @f.read(1)[0]

  case test
  when 41
    @f.read(4)
  when 0
    data = @f.read(2)
  else
    len     = @f.read(1)[0]
    if len == 255
	len = @f.read(2).unpack('s')[0]
    end
    comment = @f.read(len)
    data    = @f.read(3)
  end
  debug "Room:    #{name.inspect}  pos: #{x}, #{y}"
  debug "Comment: #{comment.inspect}"

  # r = @map.new_room(x, y)
  r = @map.new_room(x, y)
  name.gsub!(/[\r\n]+/, ' ')
  comment.gsub!(/\r/, '')

  r.name  = name
  r.tasks = comment
end

#remove_dotsObject



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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/IFMapper/GUEReader.rb', line 327

def remove_dots
  @map.sections.each do |sect|
    sect.connections.each do |c|
	a, b = c.room
	next unless b
	next if a.name == '*' or b.name != '*'
	# OK.  We have a ROOM->* connection.  Follow it to find other room.
	# And turn it into a complex connection.
	dirA = a.exits.index(c)
	dirB = b.exits.rindex(c)
	origB   = dirB
	dir     = Connection::BOTH
	dirB    = nil
	cc      = c
	while b.name == '*'
 found = false
 b.exits.each { |e|
   next if not e or e == cc
   found = true
   idx  = e.index(b) ^ 1
   b    = e.room[idx]
   dirB = b.exits.index(e)
   dir  = e.dir
   cc   = e
   break
 }
 if not found
   debug "not found complement for #{cc}"
   break
 end
	end

	next if b.name == '*'

	# remove the other connection
	sect.delete_connection(cc)

	c.roomB[origB] = nil

	# Make connection a complex connection and remove old
	# connection in b room
	c.roomB = b
	c.dir   = dir
	b[dirB] = c
    end
  end


  # Now, remove all DOT rooms and their connections
  @map.sections.each { |sect|
    del = sect.rooms.find_all { |r| r.name == '*' }
    del.each { |r|
	sect.delete_room(r)
    }
  } 
end

#repositionObject



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

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 /= 4.0
	r.y /= 4.0
    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