diff --git a/inventory.go b/inventory.go new file mode 100644 index 0000000..c0a6258 --- /dev/null +++ b/inventory.go @@ -0,0 +1,20 @@ +package types + +type Inventory struct { + Size int `json:"size"` + Items []*Item `json:"items"` +} + +func (inv *Inventory) IsEmpty() bool { + if len(inv.Items) == 0 { + return true + } + + for _, item := range inv.Items { + if item.Name != "" && item.Count > 0 { + return false + } + } + + return true +} diff --git a/item.go b/item.go new file mode 100644 index 0000000..2c92f59 --- /dev/null +++ b/item.go @@ -0,0 +1,12 @@ +package types + +type Item struct { + Name string `json:"name"` + Count int `json:"count"` + Wear int `json:"wear"` + //TODO: metadata +} + +func (i *Item) IsEmpty() bool { + return i.Name == "" && i.Count == 0 +} diff --git a/mapblock.go b/mapblock.go new file mode 100644 index 0000000..cf06cb8 --- /dev/null +++ b/mapblock.go @@ -0,0 +1,36 @@ +package types + +type MapBlock struct { + Size int `json:"size"` + Version byte `json:"version"` + Underground bool `json:"underground"` + Timestamp uint32 `json:"timestamp"` + Mapdata *MapData `json:"mapdata"` + Metadata *Metadata `json:"metadata"` + BlockMapping map[int]string `json:"blockmapping"` +} + +func NewMapblock() *MapBlock { + mb := MapBlock{} + mb.Metadata = NewMetadata() + mb.BlockMapping = make(map[int]string) + return &mb +} + +// returns true if the mapblock is empty (air-only) +func (mb *MapBlock) IsEmpty() bool { + return len(mb.BlockMapping) == 0 +} + +func (mb *MapBlock) GetNodeId(p *Pos) int { + return mb.Mapdata.ContentId[p.Index()] +} + +func (mb *MapBlock) GetParam2(p *Pos) int { + return mb.Mapdata.Param2[p.Index()] +} + +func (mb *MapBlock) GetNodeName(p *Pos) string { + id := mb.GetNodeId(p) + return mb.BlockMapping[id] +} diff --git a/mapdata.go b/mapdata.go new file mode 100644 index 0000000..c0e902a --- /dev/null +++ b/mapdata.go @@ -0,0 +1,7 @@ +package types + +type MapData struct { + ContentId []int `json:"contentid"` + Param1 []int `json:"param1"` + Param2 []int `json:"param2"` +} diff --git a/metadata.go b/metadata.go new file mode 100644 index 0000000..cc88120 --- /dev/null +++ b/metadata.go @@ -0,0 +1,52 @@ +package types + +type Metadata struct { + Inventories map[int]map[string]*Inventory `json:"inventories"` + Pairs map[int]map[string]string `json:"pairs"` +} + +func NewMetadata() *Metadata { + md := Metadata{} + md.Inventories = make(map[int]map[string]*Inventory) + md.Pairs = make(map[int]map[string]string) + return &md +} + +func (md *Metadata) GetMetadata(p *Pos) map[string]string { + return md.GetPairsMap(p.Index()) +} + +func (md *Metadata) GetPairsMap(pos int) map[string]string { + pairsMap := md.Pairs[pos] + if pairsMap == nil { + pairsMap = make(map[string]string) + md.Pairs[pos] = pairsMap + } + + return pairsMap +} + +func (md *Metadata) GetInventoryMap(index int) map[string]*Inventory { + invMap := md.Inventories[index] + if invMap == nil { + invMap = make(map[string]*Inventory) + md.Inventories[index] = invMap + } + + return invMap +} + +func (md *Metadata) GetInventoryMapAtPos(p *Pos) map[string]*Inventory { + return md.GetInventoryMap(p.Index()) +} + +func (md *Metadata) GetInventory(index int, name string) *Inventory { + m := md.GetInventoryMap(index) + inv := m[name] + if inv == nil { + inv = &Inventory{} + m[name] = inv + } + + return inv +} diff --git a/pos.go b/pos.go index 1ce291c..6498c50 100644 --- a/pos.go +++ b/pos.go @@ -11,6 +11,15 @@ func NewPos(x, y, z int) *Pos { return &Pos{x, y, z} } +func NewPosFromIndex(i int) *Pos { + x := i % 16 + i /= 16 + y := i % 16 + i /= 16 + z := i % 16 + return NewPos(x, y, z) +} + func SortPos(p1, p2 *Pos) (*Pos, *Pos) { return &Pos{ min(p1[0], p2[0]), @@ -68,3 +77,7 @@ func (p *Pos) IsWithin(min, max *Pos) bool { p[1] >= min[1] && p[1] <= max[1] && p[2] >= min[2] && p[2] <= max[2] } + +func (p *Pos) Index() int { + return p[0] + (p[1] * 16) + (p[2] * 256) +} diff --git a/pos_test.go b/pos_test.go index 86d2b9b..b106806 100644 --- a/pos_test.go +++ b/pos_test.go @@ -18,6 +18,17 @@ func TestPos(t *testing.T) { assert.Equal(t, 30, p2.Z()) } +func TestPosIndex(t *testing.T) { + pos := types.NewPos(5, 8, 12) + + i := pos.Index() + pos = types.NewPosFromIndex(i) + + assert.Equal(t, pos.X(), 5) + assert.Equal(t, pos.Y(), 8) + assert.Equal(t, pos.Z(), 12) +} + func TestSortPos(t *testing.T) { p1 := &types.Pos{1, 2, 3} p2 := &types.Pos{3, 2, 1}