1
0
forked from MTSR/mapserver

smartshop impl

This commit is contained in:
NatureFreshMilk 2019-02-08 11:45:56 +01:00
parent edda1561b9
commit 132e087783
3 changed files with 108 additions and 8 deletions

View File

@ -41,6 +41,20 @@ func getNodePos(x, y, z int) int {
return x + (y * 16) + (z * 256) return x + (y * 16) + (z * 256)
} }
func (inv *Inventory) IsEmpty() bool {
if inv.Size == 0 || len(inv.Items) == 0 {
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 { func (mb *MapBlock) GetNodeId(x, y, z int) int {
pos := getNodePos(x, y, z) pos := getNodePos(x, y, z)
return mb.Mapdata.ContentId[pos] return mb.Mapdata.ContentId[pos]

View File

@ -13,15 +13,24 @@ type MapObjectListener interface {
onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject
} }
type MapMultiObjectListener interface {
onMapObject(x, y, z int, block *mapblockparser.MapBlock) []*mapobjectdb.MapObject
}
type Listener struct { type Listener struct {
ctx *app.App ctx *app.App
objectlisteners map[string]MapObjectListener objectlisteners map[string]MapObjectListener
multiobjectlisteners map[string]MapMultiObjectListener
} }
func (this *Listener) AddMapObject(blockname string, ol MapObjectListener) { func (this *Listener) AddMapObject(blockname string, ol MapObjectListener) {
this.objectlisteners[blockname] = ol this.objectlisteners[blockname] = ol
} }
func (this *Listener) AddMapMultiObject(blockname string, ol MapMultiObjectListener) {
this.multiobjectlisteners[blockname] = ol
}
func (this *Listener) OnEvent(eventtype string, o interface{}) { func (this *Listener) OnEvent(eventtype string, o interface{}) {
if eventtype != eventbus.MAPBLOCK_RENDERED { if eventtype != eventbus.MAPBLOCK_RENDERED {
return return
@ -37,6 +46,36 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
this.ctx.WebEventbus.Emit("mapobjects-cleared", block.Pos) this.ctx.WebEventbus.Emit("mapobjects-cleared", block.Pos)
for id, name := range block.BlockMapping { for id, name := range block.BlockMapping {
for k, v := range this.multiobjectlisteners {
if k == name {
//block matches
mapblockparser.IterateMapblock(func(x,y,z int){
nodeid := block.GetNodeId(x, y, z)
if nodeid == id {
fields := logrus.Fields{
"mbpos": block.Pos,
"x": x,
"y": y,
"z": z,
"type": name,
"nodeid": nodeid,
}
log.WithFields(fields).Debug("OnEvent()")
objs := v.onMapObject(x, y, z, block)
if len(objs) > 0 {
for _, obj := range objs {
this.ctx.Objectdb.AddMapData(obj)
this.ctx.WebEventbus.Emit("mapobject-created", obj)
}
}
}
})
} // k==name
} //for k,v
for k, v := range this.objectlisteners { for k, v := range this.objectlisteners {
if k == name { if k == name {
//block matches //block matches
@ -63,6 +102,7 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
}) })
} // k==name } // k==name
} //for k,v } //for k,v
} //for id, name } //for id, name
} }
@ -70,6 +110,7 @@ func Setup(ctx *app.App) {
l := Listener{ l := Listener{
ctx: ctx, ctx: ctx,
objectlisteners: make(map[string]MapObjectListener), objectlisteners: make(map[string]MapObjectListener),
multiobjectlisteners: make(map[string]MapMultiObjectListener),
} }
//mapserver stuff //mapserver stuff
@ -135,7 +176,10 @@ func Setup(ctx *app.App) {
//jumpdrive, TODO: fleet controller //jumpdrive, TODO: fleet controller
l.AddMapObject("jumpdrive:engine", &JumpdriveBlock{}) l.AddMapObject("jumpdrive:engine", &JumpdriveBlock{})
//TODO: atm, digiterms, signs/banners, spacecannons, shops (smart, fancy) //smartshop
l.AddMapMultiObject("smartshop:shop", &SmartShopBlock{})
//TODO: atm, shops (smart, fancy)
ctx.BlockAccessor.Eventbus.AddListener(&l) ctx.BlockAccessor.Eventbus.AddListener(&l)
} }

View File

@ -3,21 +3,63 @@ package mapobject
import ( import (
"mapserver/mapblockparser" "mapserver/mapblockparser"
"mapserver/mapobjectdb" "mapserver/mapobjectdb"
"strconv"
"math"
) )
type SmartShopBlock struct{} type SmartShopBlock struct{}
func (this *SmartShopBlock) onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject { func (this *SmartShopBlock) onMapObject(x, y, z int, block *mapblockparser.MapBlock) []*mapobjectdb.MapObject {
list := make([]*mapobjectdb.MapObject, 4)
md := block.Metadata.GetMetadata(x, y, z) md := block.Metadata.GetMetadata(x, y, z)
invMap := block.Metadata.GetInventoryMapAtPos(x, y, z)
mainInv := invMap["main"]
o := mapobjectdb.NewMapObject(block.Pos, x, y, z, "shop") if mainInv.IsEmpty() {
o.Attributes["type"] = "smartshop" return list
//TODO: 4 objects per coordinate }
//invMap := block.Metadata.GetInventoryMapAtPos(x, y, z) for i := 1; i <= 4; i++ {
payInvName := "pay" + strconv.Itoa(i)
giveInvName := "give" + strconv.Itoa(i)
pay := invMap[payInvName]
give := invMap[giveInvName]
o.Attributes["index"] = md["index"] if pay.IsEmpty() || give.IsEmpty() {
continue
}
return o o := mapobjectdb.NewMapObject(block.Pos, x, y, z, "shop")
o.Attributes["type"] = "smartshop"
o.Attributes["owner"] = md["owner"]
in_item := pay.Items[0].Name
in_count := math.Max(1, float64(pay.Items[0].Count))
out_item := give.Items[0].Name
out_count := math.Max(1, float64(give.Items[0].Count))
stock := 0
for _, item := range mainInv.Items {
if item.Name == out_item {
stock += item.Count
}
}
//multiples of out_count
stock_factor := math.Floor( float64(stock) / float64(out_count) )
o.Attributes["in_item"] = in_item
o.Attributes["in_count"] = strconv.Itoa(int(in_count))
o.Attributes["out_item"] = out_item
o.Attributes["out_count"] = strconv.Itoa(int(out_count))
o.Attributes["stock"] = strconv.Itoa(int(stock_factor))
list = append(list, o)
}
return list
} }