added parsing of map lumps
This commit is contained in:
@@ -1,3 +1,73 @@
|
||||
// Parsen der Lump aus wad.go zu go structs
|
||||
package mapfmt
|
||||
|
||||
//init
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Vertex struct {
|
||||
X int16
|
||||
Y int16
|
||||
}
|
||||
|
||||
type Linedef struct {
|
||||
V1, V2 uint16
|
||||
Flags uint16
|
||||
Special uint16
|
||||
Tag uint16
|
||||
RightSide int16
|
||||
LeftSide int16
|
||||
}
|
||||
|
||||
type MapData struct {
|
||||
Vertices []Vertex
|
||||
Linedefs []Linedef
|
||||
}
|
||||
|
||||
// Parsen der Vertices Lumps
|
||||
func ParseVertices(data []byte) ([]Vertex, error) {
|
||||
if len(data)%4 != 0 {
|
||||
return nil, fmt.Errorf("VERTEXES size %d not multiple of 4", len(data))
|
||||
}
|
||||
n := len(data) / 4
|
||||
verts := make([]Vertex, n)
|
||||
for i := 0; i < n; i++ {
|
||||
verts[i].X = int16(binary.LittleEndian.Uint16(data[i*4:]))
|
||||
verts[i].Y = int16(binary.LittleEndian.Uint16(data[i*4+2:]))
|
||||
}
|
||||
return verts, nil
|
||||
}
|
||||
|
||||
// Parsen der Linedef Lumps
|
||||
func ParseLinedefs(data []byte) ([]Linedef, error) {
|
||||
if len(data)%14 != 0 {
|
||||
return nil, fmt.Errorf("LINEDEFS size %d not multiple of 14", len(data))
|
||||
}
|
||||
n := len(data) / 14
|
||||
lines := make([]Linedef, n)
|
||||
for i := 0; i < n; i++ {
|
||||
base := i * 14
|
||||
lines[i].V1 = binary.LittleEndian.Uint16(data[base:])
|
||||
lines[i].V2 = binary.LittleEndian.Uint16(data[base+2:])
|
||||
lines[i].Flags = binary.LittleEndian.Uint16(data[base+4:])
|
||||
lines[i].Special = binary.LittleEndian.Uint16(data[base+6:])
|
||||
lines[i].Tag = binary.LittleEndian.Uint16(data[base+8:])
|
||||
lines[i].RightSide = int16(binary.LittleEndian.Uint16(data[base+10:]))
|
||||
lines[i].LeftSide = int16(binary.LittleEndian.Uint16(data[base+12:]))
|
||||
}
|
||||
return lines, nil
|
||||
}
|
||||
|
||||
// Generieren der MapData aus den von LoadMapLumps geliferten Daten und Parsen per funcs
|
||||
func LoadMap(raw map[string][]byte) (*MapData, error) {
|
||||
v, err := ParseVertices(raw["VERTEXES"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l, err := ParseLinedefs(raw["LINEDEFS"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MapData{Vertices: v, Linedefs: l}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user