diff --git a/server/mapblockparser/mapblock.go b/server/mapblockparser/mapblock.go index fd7756b..5f4f65d 100644 --- a/server/mapblockparser/mapblock.go +++ b/server/mapblockparser/mapblock.go @@ -30,11 +30,16 @@ type Item struct { Name string `json:"name"` Count int `json:"count"` Wear int `json:"wear"` + //TODO: metadata +} + +func (this *Item) IsEmpty() bool { + return this.Name == "" && this.Count == 0 } type Inventory struct { Size int `json:"size"` - Items []Item `json:"items"` + Items []*Item `json:"items"` } func getNodePos(x, y, z int) int { diff --git a/server/mapblockparser/metadata.go b/server/mapblockparser/metadata.go index cd335f9..e16b73c 100644 --- a/server/mapblockparser/metadata.go +++ b/server/mapblockparser/metadata.go @@ -148,7 +148,7 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) { item.Count = int(val) } - currentInventory.Items = append(currentInventory.Items, item) + currentInventory.Items = append(currentInventory.Items, &item) } diff --git a/server/mapobject/atm.go b/server/mapobject/atm.go new file mode 100644 index 0000000..031a3e7 --- /dev/null +++ b/server/mapobject/atm.go @@ -0,0 +1,12 @@ +package mapobject + +import ( + "mapserver/mapblockparser" + "mapserver/mapobjectdb" +) + +type ATM struct{} + +func (this *ATM) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject { + return mapobjectdb.NewMapObject(block.Pos, x, y, z, "atm") +} diff --git a/server/mapobject/fancyvend.go b/server/mapobject/fancyvend.go new file mode 100644 index 0000000..5287134 --- /dev/null +++ b/server/mapobject/fancyvend.go @@ -0,0 +1,78 @@ +package mapobject + +import ( + "mapserver/mapblockparser" + "mapserver/mapobjectdb" + "mapserver/luaparser" + "strconv" +) + +type FancyVend struct{} + +func (this *FancyVend) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject { + md := block.Metadata.GetMetadata(x, y, z) + nodename := block.GetNodeName(x, y, z) + invMap := block.Metadata.GetInventoryMapAtPos(x, y, z) + parser := luaparser.New() + + isAdmin := false + + if nodename == "fancy_vend:admin_vendor" || nodename == "fancy_vend:admin_depo" { + isAdmin = true + } + + payInv := invMap["wanted_item"] + giveInv := invMap["given_item"] + mainInv := invMap["main"] + + if payInv.Items[0].IsEmpty() || giveInv.Items[0].IsEmpty() { + return nil + } + + settings, err := parser.ParseMap(md["settings"]) + if err != nil { + panic(err)//TODO + } + + if settings["input_item_qty"] == nil || settings["output_item_qty"] == nil { + return nil + } + + in_count := settings["input_item_qty"].(int) + if in_count < 1 { + in_count = 1 + } + out_count := settings["output_item_qty"].(int) + if out_count < 1 { + out_count = 1 + } + + in_item := payInv.Items[0].Name + out_item := giveInv.Items[0].Name + + stock := 0 + + if isAdmin { + stock = 999 + + } else { + for _, item := range mainInv.Items { + if item.Name == out_item { + stock += item.Count + } + } + } + + stock_factor := int(float64(stock) / float64(out_count)) + + o := mapobjectdb.NewMapObject(block.Pos, x, y, z, "shop") + o.Attributes["owner"] = md["owner"] + + o.Attributes["in_item"] = in_item + o.Attributes["in_count"] = strconv.Itoa(in_count) + o.Attributes["out_item"] = out_item + o.Attributes["out_count"] = strconv.Itoa(out_count) + o.Attributes["stock"] = strconv.Itoa(stock_factor) + + return o +} diff --git a/server/mapobject/setup.go b/server/mapobject/setup.go index f4fedda..00dcb65 100644 --- a/server/mapobject/setup.go +++ b/server/mapobject/setup.go @@ -101,11 +101,15 @@ func Setup(ctx *app.App) { } if ctx.Config.MapObjects.Fancyvend { - //TODO + v := &FancyVend{} + l.AddMapObject("fancy_vend:admin_vendor", v) + l.AddMapObject("fancy_vend:admin_depo", v) + l.AddMapObject("fancy_vend:player_vendor", v) + l.AddMapObject("fancy_vend:player_depo", v) } if ctx.Config.MapObjects.ATM { - //TODO + l.AddMapObject("atm:atm", &ATM{}) } ctx.BlockAccessor.Eventbus.AddListener(&l)