1
0
forked from MTSR/mapserver

149 lines
3.0 KiB
Go
Raw Normal View History

2019-01-06 21:00:24 +01:00
package mapblockparser
2019-01-21 11:31:50 +01:00
import (
"mapserver/coords"
)
2019-01-06 21:00:24 +01:00
type MapBlock struct {
2019-02-06 08:38:08 +01:00
Pos *coords.MapBlockCoords `json:"pos"`
2019-02-07 08:02:13 +01:00
Size int `json:"size"`
Version byte `json:"version"`
Underground bool `json:"underground"`
Mapdata *MapData `json:"mapdata"`
Metadata *Metadata `json:"metadata"`
BlockMapping map[int]string `json:"blockmapping"`
Mtime int64 `json:"mtime"`
2019-01-18 11:04:37 +01:00
}
2019-01-28 11:45:23 +01:00
type MapData struct {
ContentId []int `json:"contentid"`
Param1 []int `json:"param1"`
Param2 []int `json:"param2"`
}
2019-01-18 11:04:37 +01:00
type Metadata struct {
Inventories map[int]map[string]*Inventory `json:"inventories"`
Pairs map[int]map[string]string `json:"pairs"`
}
type Item struct {
Name string `json:"name"`
Count int `json:"count"`
Wear int `json:"wear"`
2019-02-15 11:25:49 +01:00
//TODO: metadata
}
2019-04-17 08:25:01 +02:00
func (this *MapBlock) IsEmpty() bool {
if len(this.BlockMapping) == 0 {
// only air
return true
}
if len(this.BlockMapping) == 1 {
for _, name := range this.BlockMapping {
if name == "vacuum:vacuum" {
// only vacuum
return true
}
}
}
// other stuff
return false
}
2019-02-15 11:25:49 +01:00
func (this *Item) IsEmpty() bool {
return this.Name == "" && this.Count == 0
2019-01-18 11:04:37 +01:00
}
type Inventory struct {
2019-02-15 14:30:51 +01:00
Size int `json:"size"`
2019-02-15 11:25:49 +01:00
Items []*Item `json:"items"`
2019-01-07 08:59:06 +01:00
}
2019-01-13 16:37:03 +01:00
func getNodePos(x, y, z int) int {
2019-01-10 16:04:20 +01:00
return x + (y * 16) + (z * 256)
}
2019-02-08 11:45:56 +01:00
func (inv *Inventory) IsEmpty() bool {
if len(inv.Items) == 0 {
2019-02-08 11:45:56 +01:00
return true
}
for _, item := range inv.Items {
if item.Name != "" && item.Count > 0 {
return false
}
}
return true
}
func (mb *MapBlock) GetNodeId(x, y, z int) int {
2019-01-13 16:37:03 +01:00
pos := getNodePos(x, y, z)
return mb.Mapdata.ContentId[pos]
}
2019-06-19 21:12:04 +02:00
func (mb *MapBlock) GetParam2(x, y, z int) int {
pos := getNodePos(x, y, z)
return mb.Mapdata.Param2[pos]
}
func (mb *MapBlock) GetNodeName(x, y, z int) string {
2019-01-28 14:33:32 +01:00
id := mb.GetNodeId(x, y, z)
2019-01-10 16:04:20 +01:00
return mb.BlockMapping[id]
}
2019-01-22 13:17:30 +01:00
func NewMapblock() *MapBlock {
2019-01-07 19:38:53 +01:00
mb := MapBlock{}
mb.Metadata = NewMetadata()
2019-01-07 19:53:05 +01:00
mb.BlockMapping = make(map[int]string)
2019-01-22 13:17:30 +01:00
return &mb
2019-01-07 19:38:53 +01:00
}
2019-01-07 08:59:06 +01:00
2019-01-21 14:47:57 +01:00
func NewMetadata() *Metadata {
2019-01-07 19:38:53 +01:00
md := Metadata{}
md.Inventories = make(map[int]map[string]*Inventory)
md.Pairs = make(map[int]map[string]string)
2019-01-21 14:47:57 +01:00
return &md
2019-01-07 19:38:53 +01:00
}
2019-01-07 16:06:03 +01:00
2019-01-24 13:55:29 +01:00
func (md *Metadata) GetMetadata(x, y, z int) map[string]string {
return md.GetPairsMap(getNodePos(x, y, z))
}
2019-01-07 19:38:53 +01:00
func (md *Metadata) GetPairsMap(pos int) map[string]string {
2019-01-07 16:06:03 +01:00
pairsMap := md.Pairs[pos]
if pairsMap == nil {
2019-01-07 16:35:26 +01:00
pairsMap = make(map[string]string)
2019-01-07 16:06:03 +01:00
md.Pairs[pos] = pairsMap
}
return pairsMap
}
func (md *Metadata) GetInventoryMap(pos int) map[string]*Inventory {
invMap := md.Inventories[pos]
if invMap == nil {
invMap = make(map[string]*Inventory)
md.Inventories[pos] = invMap
}
return invMap
}
2019-02-08 11:23:04 +01:00
func (md *Metadata) GetInventoryMapAtPos(x, y, z int) map[string]*Inventory {
return md.GetInventoryMap(getNodePos(x, y, z))
}
2019-01-07 16:06:03 +01:00
func (md *Metadata) GetInventory(pos int, name string) *Inventory {
m := md.GetInventoryMap(pos)
inv := m[name]
if inv == nil {
inv = &Inventory{}
m[name] = inv
}
return inv
2019-01-07 08:59:06 +01:00
}