diff --git a/server/mapblockparser/mapblock.go b/server/mapblockparser/mapblock.go index ddb1ee2..b46c1bd 100644 --- a/server/mapblockparser/mapblock.go +++ b/server/mapblockparser/mapblock.go @@ -41,9 +41,13 @@ func getNodePos(x, y, z int) int { return x + (y * 16) + (z * 256) } -func (mb *MapBlock) GetNodeName(x, y, z int) string { +func (mb *MapBlock) GetNodeId(x, y, z int) int { pos := getNodePos(x, y, z) - id := mb.Mapdata.ContentId[pos] + return mb.Mapdata.ContentId[pos] +} + +func (mb *MapBlock) GetNodeName(x, y, z int) string { + id := mb.GetNodeId(x,y,z) return mb.BlockMapping[id] } diff --git a/server/mapobject/poi.go b/server/mapobject/poi.go index 74f5fc4..4addfab 100644 --- a/server/mapobject/poi.go +++ b/server/mapobject/poi.go @@ -5,25 +5,17 @@ import ( "mapserver/mapobjectdb" ) -func onPoiBlock(id int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) { +type PoiBlock struct {} - for x := 0; x < 16; x++ { - for y := 0; y < 16; y++ { - for z := 0; z < 16; z++ { - name := block.GetNodeName(x, y, z) - if name == "mapserver:poi" { - md := block.Metadata.GetMetadata(x, y, z) +func (this *PoiBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) { + md := block.Metadata.GetMetadata(x, y, z) - o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "poi") - o.Attributes["name"] = md["name"] - o.Attributes["category"] = md["category"] - o.Attributes["url"] = md["url"] - o.Attributes["active"] = md["active"] - o.Attributes["owner"] = md["owner"] + o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "poi") + o.Attributes["name"] = md["name"] + o.Attributes["category"] = md["category"] + o.Attributes["url"] = md["url"] + o.Attributes["active"] = md["active"] + o.Attributes["owner"] = md["owner"] - odb.AddMapData(o) - } - } - } - } + odb.AddMapData(o) } diff --git a/server/mapobject/protector.go b/server/mapobject/protector.go new file mode 100644 index 0000000..f687cf4 --- /dev/null +++ b/server/mapobject/protector.go @@ -0,0 +1,17 @@ +package mapobject + +import ( + "mapserver/mapblockparser" + "mapserver/mapobjectdb" +) + +type ProtectorBlock struct {} + +func (this *ProtectorBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) { + md := block.Metadata.GetMetadata(x, y, z) + + o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "protector") + o.Attributes["owner"] = md["owner"] + + odb.AddMapData(o) +} diff --git a/server/mapobject/setup.go b/server/mapobject/setup.go index 3b32c7a..9706068 100644 --- a/server/mapobject/setup.go +++ b/server/mapobject/setup.go @@ -4,10 +4,20 @@ import ( "mapserver/app" "mapserver/eventbus" "mapserver/mapblockparser" + "mapserver/mapobjectdb" ) +type MapObjectListener interface { + onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) +} + type Listener struct { ctx *app.App + objectlisteners map[string]MapObjectListener +} + +func (this *Listener) AddMapObject(blockname string, ol MapObjectListener){ + this.objectlisteners[blockname] = ol } func (this *Listener) OnEvent(eventtype string, o interface{}) { @@ -23,12 +33,37 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) { } for id, name := range block.BlockMapping { - if name == "mapserver:poi" { - onPoiBlock(id, block, this.ctx.Objectdb) - } - } + for k, v := range this.objectlisteners { + if k == name { + //block matches + + for x := 0; x < 16; x++ { + for y := 0; y < 16; y++ { + for z := 0; z < 16; z++ { + nodeid := block.GetNodeId(x, y, z) + if nodeid == id { + v.onMapObject(x, y, z, block, this.ctx.Objectdb) + } + }//z + }//y + }//x + + } + }//for k,v + }//for id, name } func Setup(ctx *app.App) { - ctx.BlockAccessor.Eventbus.AddListener(&Listener{ctx: ctx}) + l := Listener{ + ctx: ctx, + objectlisteners: make(map[string]MapObjectListener), + } + + l.AddMapObject("mapserver:poi", &PoiBlock{}) + l.AddMapObject("mapserver:train", &TrainBlock{}) + l.AddMapObject("travelnet:travelnet", &TravelnetBlock{}) + l.AddMapObject("protector:protect", &ProtectorBlock{}) + l.AddMapObject("protector:protect2", &ProtectorBlock{}) + + ctx.BlockAccessor.Eventbus.AddListener(&l) } diff --git a/server/mapobject/train.go b/server/mapobject/train.go new file mode 100644 index 0000000..00f67e3 --- /dev/null +++ b/server/mapobject/train.go @@ -0,0 +1,20 @@ +package mapobject + +import ( + "mapserver/mapblockparser" + "mapserver/mapobjectdb" +) + +type TrainBlock struct {} + +func (this *TrainBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) { + md := block.Metadata.GetMetadata(x, y, z) + + o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "train") + o.Attributes["station"] = md["station"] + o.Attributes["line"] = md["line"] + o.Attributes["index"] = md["index"] + o.Attributes["owner"] = md["owner"] + + odb.AddMapData(o) +} diff --git a/server/mapobject/travelnet.go b/server/mapobject/travelnet.go new file mode 100644 index 0000000..eb3a37b --- /dev/null +++ b/server/mapobject/travelnet.go @@ -0,0 +1,24 @@ +package mapobject + +import ( + "mapserver/mapblockparser" + "mapserver/mapobjectdb" +) + +type TravelnetBlock struct {} + +func (this *TravelnetBlock) onMapObject(x,y,z int, block *mapblockparser.MapBlock, odb mapobjectdb.DBAccessor) { + md := block.Metadata.GetMetadata(x, y, z) + + if md["station_name"] == "" || md["owner"] == "" { + //station not set up + return + } + + o := mapobjectdb.NewMapObject(&block.Pos, x, y, z, "travelnet") + o.Attributes["owner"] = md["owner"] + o.Attributes["station_name"] = md["station_name"] + o.Attributes["station_network"] = md["station_network"] + + odb.AddMapData(o) +}