Add the view-map command and some exploration of results
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"compress/gzip"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
@@ -18,6 +19,17 @@ var (
|
||||
notImplemented = fmt.Errorf("Not implemented")
|
||||
)
|
||||
|
||||
const (
|
||||
MaxHeight = 7 // Z coordinate
|
||||
MaxLength = 100 // Y coordinate
|
||||
MaxWidth = 130 // X coordinate
|
||||
|
||||
CellSize = 16 // seems to be
|
||||
|
||||
cellDataOffset = 0x100 // tentatively
|
||||
cellDataSize = MaxHeight * MaxLength * MaxWidth * CellSize
|
||||
)
|
||||
|
||||
type Header struct {
|
||||
IsCampaignMap uint32 // Tentatively: 0 = no, 1 = yes
|
||||
MinWidth uint32
|
||||
@@ -31,7 +43,37 @@ type Header struct {
|
||||
Magic [8]byte // "\x08\x00WHMAP\x00"
|
||||
Unknown5 uint32
|
||||
Unknown6 uint32
|
||||
SetName [8]byte // Or is it a null-terminated string?
|
||||
SetName [8]byte // Links to a filename in `/Sets/*.set`
|
||||
// Need to investigate the rest of the header too
|
||||
}
|
||||
|
||||
func (h Header) Width() int {
|
||||
return int(h.MaxWidth - h.MinWidth)
|
||||
}
|
||||
|
||||
func (h Header) Length() int {
|
||||
return int(h.MaxLength - h.MinLength)
|
||||
}
|
||||
|
||||
func (h Header) Height() int {
|
||||
return MaxHeight
|
||||
}
|
||||
|
||||
type Cell []byte // FIXME: need to deconstruct this into the various fields
|
||||
|
||||
// Cells is always a fixed size; use At to get a cell according to x,y,z
|
||||
type Cells []byte
|
||||
|
||||
// FIXME: Ordering may be incorrect? I assume z,y,x for now...
|
||||
func (c Cells) At(x, y, z int) Cell {
|
||||
start :=
|
||||
(z * MaxLength * MaxWidth * CellSize) +
|
||||
(y * MaxWidth * CellSize) +
|
||||
(x * CellSize)
|
||||
|
||||
end := start + CellSize
|
||||
|
||||
return Cell(c[start:end:end])
|
||||
}
|
||||
|
||||
func (h Header) Check() []error {
|
||||
@@ -49,7 +91,7 @@ func (h Header) Check() []error {
|
||||
|
||||
type GameMap struct {
|
||||
Header
|
||||
|
||||
Cells
|
||||
// TODO: parse this into sections
|
||||
Text string
|
||||
}
|
||||
@@ -57,8 +99,8 @@ type GameMap struct {
|
||||
// A game map contains a .txt and a .map. If they're in the same directory,
|
||||
// just pass the directory + basename to load both
|
||||
func LoadGameMap(prefix string) (*GameMap, error) {
|
||||
for _, mapExt := range []string{".MAP", ".map"} {
|
||||
for _, txtExt := range []string{".TXT", ".txt"} {
|
||||
for _, txtExt := range []string{".TXT", ".txt"} {
|
||||
for _, mapExt := range []string{".MAP", ".map"} {
|
||||
out, err := LoadGameMapByFiles(prefix+mapExt, prefix+txtExt)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
@@ -146,5 +188,16 @@ func loadMapFile(filename string) (*GameMap, error) {
|
||||
return nil, fmt.Errorf("Error parsing %s: %v", filename, err)
|
||||
}
|
||||
|
||||
// no gzip.SeekReader, so discard unread header bytes for now
|
||||
discardSize := int64(cellDataOffset - binary.Size(&out.Header))
|
||||
if _, err := io.CopyN(ioutil.Discard, zr, discardSize); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out.Cells = make(Cells, cellDataSize)
|
||||
if err := binary.Read(zr, binary.LittleEndian, &out.Cells); err != nil {
|
||||
return nil, fmt.Errorf("Error parsing %s: %v", filename, err)
|
||||
}
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user