Class: Doom::Map::MapData
- Inherits:
-
Object
- Object
- Doom::Map::MapData
- Defined in:
- lib/doom/map/data.rb
Constant Summary collapse
- COOP_START_TYPES =
Thing types 1-4 are the co-op starts for players 1-4; type 11 is a deathmatch spawn point. Every stock WAD carries all of them -- until now only type 1 was ever read.
(1..4).to_a.freeze
- DEATHMATCH_START_TYPE =
11- BLOCKMAP_BLOCK_SIZE =
BLOCKMAP: 128-unit grid index into linedefs. Each block lists the linedefs that touch it. Used for fast collision lookup.
128
Instance Attribute Summary collapse
-
#linedefs ⇒ Object
readonly
Returns the value of attribute linedefs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#sectors ⇒ Object
readonly
Returns the value of attribute sectors.
-
#segs ⇒ Object
readonly
Returns the value of attribute segs.
-
#sidedefs ⇒ Object
readonly
Returns the value of attribute sidedefs.
-
#subsectors ⇒ Object
readonly
Returns the value of attribute subsectors.
-
#things ⇒ Object
readonly
Returns the value of attribute things.
-
#vertices ⇒ Object
readonly
Returns the value of attribute vertices.
Class Method Summary collapse
Instance Method Summary collapse
- #blockmap_loaded? ⇒ Boolean
- #deathmatch_starts ⇒ Object
-
#each_linedef_near(min_x, min_y, max_x, max_y) ⇒ Object
Yield each linedef whose block overlaps the bounding box (min_x, min_y, max_x, max_y).
-
#initialize(name) ⇒ MapData
constructor
A new instance of MapData.
- #load_blockmap(data) ⇒ Object
- #load_linedefs(data) ⇒ Object
- #load_nodes(data) ⇒ Object
- #load_sectors(data) ⇒ Object
- #load_segs(data) ⇒ Object
- #load_sidedefs(data) ⇒ Object
- #load_subsectors(data) ⇒ Object
- #load_things(data) ⇒ Object
- #load_vertices(data) ⇒ Object
- #player_start ⇒ Object
-
#player_start_for(id) ⇒ Object
Start for player
id(0-based), falling back to player 1's start so a map without enough co-op starts is still playable. -
#player_starts ⇒ Object
Co-op starts indexed by player number, so player N spawns at start N.
-
#sector_at(x, y) ⇒ Object
Find the sector at a given position by traversing the BSP tree.
-
#subsector_at(x, y) ⇒ Object
Find the subsector containing a point.
Constructor Details
#initialize(name) ⇒ MapData
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/doom/map/data.rb', line 81 def initialize(name) @name = name @things = [] @vertices = [] @linedefs = [] @sidedefs = [] @sectors = [] @segs = [] @subsectors = [] @nodes = [] end |
Instance Attribute Details
#linedefs ⇒ Object (readonly)
Returns the value of attribute linedefs.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def linedefs @linedefs end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def name @name end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def nodes @nodes end |
#sectors ⇒ Object (readonly)
Returns the value of attribute sectors.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def sectors @sectors end |
#segs ⇒ Object (readonly)
Returns the value of attribute segs.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def segs @segs end |
#sidedefs ⇒ Object (readonly)
Returns the value of attribute sidedefs.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def sidedefs @sidedefs end |
#subsectors ⇒ Object (readonly)
Returns the value of attribute subsectors.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def subsectors @subsectors end |
#things ⇒ Object (readonly)
Returns the value of attribute things.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def things @things end |
#vertices ⇒ Object (readonly)
Returns the value of attribute vertices.
79 80 81 |
# File 'lib/doom/map/data.rb', line 79 def vertices @vertices end |
Class Method Details
.load(wad, map_name) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/doom/map/data.rb', line 93 def self.load(wad, map_name) map = new(map_name) lump_idx = wad.directory.index { |e| e.name == map_name.upcase } raise Error, "Map #{map_name} not found" unless lump_idx map.load_things(wad.read_lump_at(wad.directory[lump_idx + 1])) map.load_linedefs(wad.read_lump_at(wad.directory[lump_idx + 2])) map.load_sidedefs(wad.read_lump_at(wad.directory[lump_idx + 3])) map.load_vertices(wad.read_lump_at(wad.directory[lump_idx + 4])) map.load_segs(wad.read_lump_at(wad.directory[lump_idx + 5])) map.load_subsectors(wad.read_lump_at(wad.directory[lump_idx + 6])) map.load_nodes(wad.read_lump_at(wad.directory[lump_idx + 7])) map.load_sectors(wad.read_lump_at(wad.directory[lump_idx + 8])) # BLOCKMAP is at lump +10 (REJECT is +9). Optional -- parse defensively. blockmap_entry = wad.directory[lump_idx + 10] map.load_blockmap(wad.read_lump_at(blockmap_entry)) if blockmap_entry && blockmap_entry.name == 'BLOCKMAP' map end |
Instance Method Details
#blockmap_loaded? ⇒ Boolean
333 334 335 |
# File 'lib/doom/map/data.rb', line 333 def blockmap_loaded? !@blockmap_blocks.nil? end |
#deathmatch_starts ⇒ Object
264 265 266 |
# File 'lib/doom/map/data.rb', line 264 def deathmatch_starts @things.select { |t| t.type == DEATHMATCH_START_TYPE } end |
#each_linedef_near(min_x, min_y, max_x, max_y) ⇒ Object
Yield each linedef whose block overlaps the bounding box (min_x, min_y, max_x, max_y). Yields each linedef at most once per call. Falls back to iterating all linedefs if no blockmap is loaded.
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 |
# File 'lib/doom/map/data.rb', line 305 def each_linedef_near(min_x, min_y, max_x, max_y, &) unless @blockmap_blocks @linedefs.each(&) return end bx0 = ((min_x - @blockmap_origin_x) / BLOCKMAP_BLOCK_SIZE).floor.clamp(0, @blockmap_cols - 1) bx1 = ((max_x - @blockmap_origin_x) / BLOCKMAP_BLOCK_SIZE).floor.clamp(0, @blockmap_cols - 1) by0 = ((min_y - @blockmap_origin_y) / BLOCKMAP_BLOCK_SIZE).floor.clamp(0, @blockmap_rows - 1) by1 = ((max_y - @blockmap_origin_y) / BLOCKMAP_BLOCK_SIZE).floor.clamp(0, @blockmap_rows - 1) seen = {} by0.upto(by1) do |by| row_base = by * @blockmap_cols bx0.upto(bx1) do |bx| indices = @blockmap_blocks[row_base + bx] next unless indices indices.each do |idx| next if seen[idx] seen[idx] = true yield @linedefs[idx] end end end end |
#load_blockmap(data) ⇒ Object
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/doom/map/data.rb', line 272 def load_blockmap(data) return if data.nil? || data.size < 8 @blockmap_origin_x = data[0, 2].unpack1('s<') @blockmap_origin_y = data[2, 2].unpack1('s<') @blockmap_cols = data[4, 2].unpack1('s<') @blockmap_rows = data[6, 2].unpack1('s<') return if @blockmap_cols <= 0 || @blockmap_rows <= 0 block_count = @blockmap_cols * @blockmap_rows @blockmap_blocks = Array.new(block_count) block_count.times do |i| offset_words = data[8 + (i * 2), 2].unpack1('v') byte_offset = offset_words * 2 linedefs_in_block = [] ptr = byte_offset # Skip the leading 0x0000 sentinel that some blockmaps include. ptr += 2 if ptr + 2 <= data.size && data[ptr, 2].unpack1('s<') == 0 while ptr + 2 <= data.size idx = data[ptr, 2].unpack1('s<') break if idx == -1 # 0xFFFF terminator linedefs_in_block << idx ptr += 2 end @blockmap_blocks[i] = linedefs_in_block end end |
#load_linedefs(data) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/doom/map/data.rb', line 140 def load_linedefs(data) count = data.size / 14 count.times do |i| offset = i * 14 @linedefs << Linedef.new( data[offset, 2].unpack1('v'), data[offset + 2, 2].unpack1('v'), data[offset + 4, 2].unpack1('v'), data[offset + 6, 2].unpack1('v'), data[offset + 8, 2].unpack1('v'), data[offset + 10, 2].unpack1('s<'), data[offset + 12, 2].unpack1('s<') ) end end |
#load_nodes(data) ⇒ Object
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/doom/map/data.rb', line 213 def load_nodes(data) count = data.size / 28 count.times do |i| offset = i * 28 bbox_right = Node::BBox.new( data[offset + 8, 2].unpack1('s<'), data[offset + 10, 2].unpack1('s<'), data[offset + 12, 2].unpack1('s<'), data[offset + 14, 2].unpack1('s<') ) bbox_left = Node::BBox.new( data[offset + 16, 2].unpack1('s<'), data[offset + 18, 2].unpack1('s<'), data[offset + 20, 2].unpack1('s<'), data[offset + 22, 2].unpack1('s<') ) @nodes << Node.new( data[offset, 2].unpack1('s<'), data[offset + 2, 2].unpack1('s<'), data[offset + 4, 2].unpack1('s<'), data[offset + 6, 2].unpack1('s<'), bbox_right, bbox_left, data[offset + 24, 2].unpack1('v'), data[offset + 26, 2].unpack1('v') ) end end |
#load_sectors(data) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/doom/map/data.rb', line 171 def load_sectors(data) count = data.size / 26 count.times do |i| offset = i * 26 @sectors << Sector.new( data[offset, 2].unpack1('s<'), data[offset + 2, 2].unpack1('s<'), data[offset + 4, 8].delete("\x00").strip, data[offset + 12, 8].delete("\x00").strip, data[offset + 20, 2].unpack1('v'), data[offset + 22, 2].unpack1('v'), data[offset + 24, 2].unpack1('v') ) end end |
#load_segs(data) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/doom/map/data.rb', line 187 def load_segs(data) count = data.size / 12 count.times do |i| offset = i * 12 @segs << Seg.new( data[offset, 2].unpack1('v'), data[offset + 2, 2].unpack1('v'), data[offset + 4, 2].unpack1('s<'), data[offset + 6, 2].unpack1('v'), data[offset + 8, 2].unpack1('v'), data[offset + 10, 2].unpack1('s<') ) end end |
#load_sidedefs(data) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/doom/map/data.rb', line 156 def load_sidedefs(data) count = data.size / 30 count.times do |i| offset = i * 30 @sidedefs << Sidedef.new( data[offset, 2].unpack1('s<'), data[offset + 2, 2].unpack1('s<'), data[offset + 4, 8].delete("\x00").strip, data[offset + 12, 8].delete("\x00").strip, data[offset + 20, 8].delete("\x00").strip, data[offset + 28, 2].unpack1('v') ) end end |
#load_subsectors(data) ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/doom/map/data.rb', line 202 def load_subsectors(data) count = data.size / 4 count.times do |i| offset = i * 4 @subsectors << Subsector.new( data[offset, 2].unpack1('v'), data[offset + 2, 2].unpack1('v') ) end end |
#load_things(data) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/doom/map/data.rb', line 115 def load_things(data) count = data.size / 10 count.times do |i| offset = i * 10 @things << Thing.new( data[offset, 2].unpack1('s<'), data[offset + 2, 2].unpack1('s<'), data[offset + 4, 2].unpack1('v'), data[offset + 6, 2].unpack1('v'), data[offset + 8, 2].unpack1('v') ) end end |
#load_vertices(data) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/doom/map/data.rb', line 129 def load_vertices(data) count = data.size / 4 count.times do |i| offset = i * 4 @vertices << Vertex.new( data[offset, 2].unpack1('s<'), data[offset + 2, 2].unpack1('s<') ) end end |
#player_start ⇒ Object
248 249 250 |
# File 'lib/doom/map/data.rb', line 248 def player_start @things.find { |t| t.type == 1 } end |
#player_start_for(id) ⇒ Object
Start for player id (0-based), falling back to player 1's start so a
map without enough co-op starts is still playable.
260 261 262 |
# File 'lib/doom/map/data.rb', line 260 def player_start_for(id) player_starts[id] || player_start end |
#player_starts ⇒ Object
Co-op starts indexed by player number, so player N spawns at start N. Sparse on maps that only define some: index 2 may be nil while 0 is not.
254 255 256 |
# File 'lib/doom/map/data.rb', line 254 def player_starts COOP_START_TYPES.map { |type| @things.find { |t| t.type == type } } end |
#sector_at(x, y) ⇒ Object
Find the sector at a given position by traversing the BSP tree
338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/doom/map/data.rb', line 338 def sector_at(x, y) subsector = subsector_at(x, y) return nil unless subsector # Get sector from first seg of subsector seg = @segs[subsector.first_seg] return nil unless seg linedef = @linedefs[seg.linedef] sidedef_idx = seg.direction == 0 ? linedef.sidedef_right : linedef.sidedef_left return nil if sidedef_idx < 0 @sectors[@sidedefs[sidedef_idx].sector] end |
#subsector_at(x, y) ⇒ Object
Find the subsector containing a point
354 355 356 357 358 359 360 361 362 |
# File 'lib/doom/map/data.rb', line 354 def subsector_at(x, y) node_idx = @nodes.size - 1 while (node_idx & Node::SUBSECTOR_FLAG) == 0 node = @nodes[node_idx] side = point_on_side(x, y, node) node_idx = side == 0 ? node.child_right : node.child_left end @subsectors[node_idx & ~Node::SUBSECTOR_FLAG] end |