Skip to content

Commit

Permalink
Generalize block selector
Browse files Browse the repository at this point in the history
  • Loading branch information
weqqr committed Oct 30, 2024
1 parent e00cb3b commit d8a59c3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
7 changes: 6 additions & 1 deletion internal/generator/flat/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ func (r *FlatRenderer) RenderTile(
rect := image.Rect(0, 0, 256, 256)
target := rasterizer.NewRenderBuffer(rect)

err := wd.GetBlocksAlongY(tilePos.X, tilePos.Y, func(pos geom.BlockPosition, block *world.MapBlock) error {
selector := world.BlocksAlongY{
X: tilePos.X,
Z: tilePos.Y,
}

err := wd.GetBlocks(selector, func(pos geom.BlockPosition, block *world.MapBlock) error {
return nil
})
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions internal/generator/tile/next.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tile

import (
"github.com/lord-server/panorama/internal/game"
"github.com/lord-server/panorama/internal/world"
"github.com/lord-server/panorama/pkg/geom"
)

type NextTiler struct {
}

func (t *NextTiler) FullRender(game *game.Game, world *world.World, workers int, region geom.Region, createRenderer CreateRendererFunc) {

}
15 changes: 15 additions & 0 deletions internal/world/selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package world

type BlockSelector interface {
Query() (string, []any)
}

type BlocksAlongY struct {
X, Z int
}

func (s BlocksAlongY) Query() (string, []any) {
return "SELECT posx, posy, posz, data FROM blocks WHERE posx=$1 and posz=$2 ORDER BY posy", []any{
s.X, s.Z,
}
}
12 changes: 7 additions & 5 deletions internal/world/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type Backend interface {
GetBlockData(pos geom.BlockPosition) ([]byte, error)
GetBlocksAlongY(x, z int, callback func(geom.BlockPosition, []byte) error) error
GetBlocks(selector BlockSelector, callback func(geom.BlockPosition, []byte) error) error
Close()
}

Expand Down Expand Up @@ -52,8 +52,10 @@ func (p *PostgresBackend) GetBlockData(pos geom.BlockPosition) ([]byte, error) {
return data, nil
}

func (p *PostgresBackend) GetBlocksAlongY(x, z int, callback func(geom.BlockPosition, []byte) error) error {
rows, err := p.conn.Query(context.Background(), "SELECT posx, posy, posz, data FROM blocks WHERE posx=$1 and posz=$2 ORDER BY posy", x, z)
func (p *PostgresBackend) GetBlocks(selector BlockSelector, callback func(geom.BlockPosition, []byte) error) error {
sql, args := selector.Query()

rows, err := p.conn.Query(context.Background(), sql, args...)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
Expand Down Expand Up @@ -169,8 +171,8 @@ func (w *World) GetBlock(pos geom.BlockPosition) (*MapBlock, error) {
return block, nil
}

func (w *World) GetBlocksAlongY(x, z int, callback func(geom.BlockPosition, *MapBlock) error) error {
return w.backend.GetBlocksAlongY(x, z, func(pos geom.BlockPosition, data []byte) error {
func (w *World) GetBlocks(selector BlockSelector, callback func(geom.BlockPosition, *MapBlock) error) error {
return w.backend.GetBlocks(selector, func(pos geom.BlockPosition, data []byte) error {
cachedBlock, ok := w.decodedBlockCache.Get(pos)

if ok {
Expand Down

0 comments on commit d8a59c3

Please sign in to comment.