1
0
forked from MTSR/mapserver

add new mapblock+pos type / refactor listeners

This commit is contained in:
BuckarooBanzay 2022-01-30 13:29:47 +01:00
parent 73c93af328
commit 571825ef88
7 changed files with 62 additions and 28 deletions

View File

@ -0,0 +1,13 @@
package coords
type MapblockIterator func(x, y, z int)
func IterateMapblock(it MapblockIterator) {
for x := 0; x < 16; x++ {
for y := 0; y < 16; y++ {
for z := 0; z < 16; z++ {
it(x, y, z)
}
}
}
}

View File

@ -3,6 +3,7 @@ package mapblockaccessor
import (
"mapserver/coords"
"mapserver/eventbus"
"mapserver/types"
"sync"
"github.com/minetest-go/mapparser"
@ -84,7 +85,7 @@ func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.M
return nil, err
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, pos))
cacheBlockCount.Inc()
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)

View File

@ -4,6 +4,7 @@ import (
"mapserver/eventbus"
"mapserver/layer"
"mapserver/settings"
"mapserver/types"
"github.com/minetest-go/mapparser"
cache "github.com/patrickmn/go-cache"
@ -12,7 +13,7 @@ import (
type FindNextLegacyBlocksResult struct {
HasMore bool
List []*mapparser.MapBlock
List []*types.ParsedMapblock
UnfilteredCount int
Progress float64
LastMtime int64
@ -29,7 +30,7 @@ func (a *MapBlockAccessor) FindNextLegacyBlocks(s settings.Settings, layers []*l
blocks := nextResult.List
result := FindNextLegacyBlocksResult{}
mblist := make([]*mapparser.MapBlock, 0)
mblist := make([]*types.ParsedMapblock, 0)
result.HasMore = nextResult.HasMore
result.UnfilteredCount = nextResult.UnfilteredCount
result.Progress = nextResult.Progress
@ -59,11 +60,11 @@ func (a *MapBlockAccessor) FindNextLegacyBlocks(s settings.Settings, layers []*l
return nil, err
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, block.Pos))
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
cacheBlockCount.Inc()
mblist = append(mblist, mapblock)
mblist = append(mblist, types.NewParsedMapblock(mapblock, block.Pos))
}

View File

@ -4,6 +4,7 @@ import (
"mapserver/coords"
"mapserver/eventbus"
"mapserver/layer"
"mapserver/types"
"github.com/minetest-go/mapparser"
cache "github.com/patrickmn/go-cache"
@ -15,7 +16,7 @@ type FindMapBlocksByMtimeResult struct {
HasMore bool
LastPos *coords.MapBlockCoords
LastMtime int64
List []*mapparser.MapBlock
List []*types.ParsedMapblock
UnfilteredCount int
}
@ -39,7 +40,7 @@ func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, laye
result := FindMapBlocksByMtimeResult{}
mblist := make([]*mapparser.MapBlock, 0)
mblist := make([]*types.ParsedMapblock, 0)
var newlastpos *coords.MapBlockCoords
result.HasMore = len(blocks) == limit
result.UnfilteredCount = len(blocks)
@ -77,11 +78,11 @@ func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, laye
continue
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, block.Pos))
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
cacheBlockCount.Inc()
mblist = append(mblist, mapblock)
mblist = append(mblist, types.NewParsedMapblock(mapblock, block.Pos))
}

View File

@ -2,19 +2,21 @@ package mapobject
import (
"mapserver/app"
"mapserver/coords"
"mapserver/eventbus"
"mapserver/mapblockparser"
"mapserver/mapobjectdb"
"mapserver/types"
"github.com/minetest-go/mapparser"
"github.com/sirupsen/logrus"
)
type MapObjectListener interface {
onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject
onMapObject(mbpos *coords.MapBlockCoords, x, y, z int, block *mapparser.MapBlock) *mapobjectdb.MapObject
}
type MapMultiObjectListener interface {
onMapObject(x, y, z int, block *mapblockparser.MapBlock) []*mapobjectdb.MapObject
onMapObject(mbpos *coords.MapBlockCoords, x, y, z int, block *mapparser.MapBlock) []*mapobjectdb.MapObject
}
type Listener struct {
@ -36,26 +38,26 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
return
}
block := o.(*mapblockparser.MapBlock)
pmb := o.(*types.ParsedMapblock)
err := this.ctx.Objectdb.RemoveMapData(block.Pos)
err := this.ctx.Objectdb.RemoveMapData(pmb.Pos)
if err != nil {
panic(err)
}
this.ctx.WebEventbus.Emit("mapobjects-cleared", block.Pos)
this.ctx.WebEventbus.Emit("mapobjects-cleared", pmb.Pos)
//TODO: refactor into single loop
for id, name := range block.BlockMapping {
for id, name := range pmb.Mapblock.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)
coords.IterateMapblock(func(x, y, z int) {
nodeid := pmb.Mapblock.GetNodeId(x, y, z)
if nodeid == id {
fields := logrus.Fields{
"mbpos": block.Pos,
"mbpos": pmb.Pos,
"x": x,
"y": y,
"z": z,
@ -64,14 +66,14 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
}
log.WithFields(fields).Debug("OnEvent()")
objs := v.onMapObject(x, y, z, block)
objs := v.onMapObject(pmb.Pos, x, y, z, pmb.Mapblock)
if len(objs) > 0 {
for _, obj := range objs {
err := this.ctx.Objectdb.AddMapData(obj)
if err != nil {
fields = logrus.Fields{
"mbpos": block.Pos,
"mbpos": pmb.Pos,
"x": x,
"y": y,
"z": z,
@ -95,11 +97,11 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
for k, v := range this.objectlisteners {
if k == name {
//block matches
mapblockparser.IterateMapblock(func(x, y, z int) {
nodeid := block.GetNodeId(x, y, z)
coords.IterateMapblock(func(x, y, z int) {
nodeid := pmb.Mapblock.GetNodeId(x, y, z)
if nodeid == id {
fields := logrus.Fields{
"mbpos": block.Pos,
"mbpos": pmb.Pos,
"x": x,
"y": y,
"z": z,
@ -108,13 +110,13 @@ func (this *Listener) OnEvent(eventtype string, o interface{}) {
}
log.WithFields(fields).Debug("OnEvent()")
obj := v.onMapObject(x, y, z, block)
obj := v.onMapObject(pmb.Pos, x, y, z, pmb.Mapblock)
if obj != nil {
err := this.ctx.Objectdb.AddMapData(obj)
if err != nil {
fields = logrus.Fields{
"mbpos": block.Pos,
"mbpos": pmb.Pos,
"x": x,
"y": y,
"z": z,

View File

@ -3,9 +3,9 @@ package tilerendererjob
import (
"mapserver/app"
"mapserver/coords"
"mapserver/types"
"strconv"
"github.com/minetest-go/mapparser"
"github.com/sirupsen/logrus"
)
@ -14,7 +14,7 @@ func getTileKey(tc *coords.TileCoords) string {
strconv.Itoa(tc.Zoom) + "/" + strconv.Itoa(tc.LayerId)
}
func renderMapblocks(ctx *app.App, mblist []*mapparser.MapBlock) int {
func renderMapblocks(ctx *app.App, mblist []*types.ParsedMapblock) int {
tileRenderedMap := make(map[string]bool)
tilecount := 0
totalRenderedMapblocks.Add(float64(len(mblist)))

16
types/parsedmapblock.go Normal file
View File

@ -0,0 +1,16 @@
package types
import (
"mapserver/coords"
"github.com/minetest-go/mapparser"
)
type ParsedMapblock struct {
Mapblock *mapparser.MapBlock
Pos *coords.MapBlockCoords
}
func NewParsedMapblock(mb *mapparser.MapBlock, pos *coords.MapBlockCoords) *ParsedMapblock {
return &ParsedMapblock{Mapblock: mb, Pos: pos}
}