Skip to content

Commit

Permalink
use minetest-go types package
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jun 21, 2024
1 parent 644ac25 commit 5c226cc
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 142 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/minetest-go/maprenderer

go 1.21
go 1.22.4

require (
github.com/minetest-go/mapparser v0.1.3
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.9.0
)

require github.com/minetest-go/types v1.0.1

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.15.4 // indirect
Expand Down
13 changes: 4 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/compress v1.15.4 h1:1kn4/7MepF/CHmYub99/nNX8az0IJjfSOU/jbnTVfqQ=
Expand All @@ -7,17 +6,13 @@ github.com/minetest-go/colormapping v1.0.1 h1:RF6sFd59QuTkm0G3iMXlj3Leg+ZHRps+fY
github.com/minetest-go/colormapping v1.0.1/go.mod h1:arMBjO6+z8+yhLvrsX4tCnYq5fzGcPFUOj/4Xb78Ktc=
github.com/minetest-go/mapparser v0.1.3 h1:mjQ+dXiLd3PPwtsf9QOQY6ZN16dHmFwC/eHynr8NZlA=
github.com/minetest-go/mapparser v0.1.3/go.mod h1:TcjEkPY9n0rBM+beSstw+Jje8nGl+LqFhiC00J6ihNw=
github.com/minetest-go/types v1.0.1 h1:vGNyfmdgOEDAy2HVMCjQ2AU/FlmxfZ0IFre6FQKVvfY=
github.com/minetest-go/types v1.0.1/go.mod h1:QS2q8tKAiuBe2Rin880ARFt9VokYIJNZV4Z4kHAChU0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 6 additions & 4 deletions iso_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"image"
"image/color"

"github.com/minetest-go/types"
)

func GetIsometricImageSize(size *Pos, cube_len int) (int, int) {
func GetIsometricImageSize(size *types.Pos, cube_len int) (int, int) {
width := (size.X() * cube_len / 2) +
(size.Z() * cube_len / 2)

Expand All @@ -17,7 +19,7 @@ func GetIsometricImageSize(size *Pos, cube_len int) (int, int) {
return width, height
}

func GetIsoCenterCubeOffset(size *Pos, cube_len int) (int, int) {
func GetIsoCenterCubeOffset(size *types.Pos, cube_len int) (int, int) {
x := (size.Z() * cube_len / 2) -
(cube_len / 2)

Expand All @@ -29,7 +31,7 @@ func GetIsoCenterCubeOffset(size *Pos, cube_len int) (int, int) {
return x, y
}

func GetIsoCubePosition(center_x, center_y, cube_len int, pos *Pos) (int, int) {
func GetIsoCubePosition(center_x, center_y, cube_len int, pos *types.Pos) (int, int) {
x := center_x -
(pos.Z() * cube_len / 2) +
(pos.X() * cube_len / 2)
Expand Down Expand Up @@ -86,7 +88,7 @@ func DrawIsoCube(img *image.RGBA, cube_len, x_offset, y_offset int, c1, c2, c3 c
return nil
}

func GetIsoNodeOrder(rel_pos, rel_max *Pos) int {
func GetIsoNodeOrder(rel_pos, rel_max *types.Pos) int {
return (rel_pos.Y() * (rel_max.X() * rel_max.Z())) +
(rel_max.X() - rel_pos.X()) +
(rel_max.Z() - rel_pos.Z())
Expand Down
16 changes: 9 additions & 7 deletions isorenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"image"
"slices"

"github.com/minetest-go/types"
)

type IsoRenderOpts struct {
Expand All @@ -18,27 +20,27 @@ func NewDefaultIsoRenderOpts() *IsoRenderOpts {
}
}

func RenderIsometric(na NodeAccessor, cr ColorResolver, from, to *Pos, opts *IsoRenderOpts) (image.Image, error) {
func RenderIsometric(na NodeAccessor, cr ColorResolver, from, to *types.Pos, opts *IsoRenderOpts) (image.Image, error) {
if opts == nil {
opts = NewDefaultIsoRenderOpts()
}

min, max := SortPos(from, to)
size := to.Subtract(from).Add(NewPos(1, 1, 1))
min, max := types.SortPos(from, to)
size := to.Subtract(from).Add(types.NewPos(1, 1, 1))

width, height := GetIsometricImageSize(size, opts.CubeLen)
center_x, center_y := GetIsoCenterCubeOffset(size, opts.CubeLen)
img := image.NewRGBA(image.Rectangle{Min: image.Point{}, Max: image.Point{X: width, Y: height}})

skip_alpha := !opts.EnableTransparency
ipos := NewPos(1, -1, 1)
ipos := types.NewPos(1, -1, 1)

nodes := []*NodeWithColor{}

// top layer
for x := min.X(); x <= max.X(); x++ {
for z := min.Z(); z <= max.Z(); z++ {
pnodes, err := Probe(min, max, NewPos(x, max.Y(), z), ipos, na, cr, skip_alpha)
pnodes, err := Probe(min, max, types.NewPos(x, max.Y(), z), ipos, na, cr, skip_alpha)
if err != nil {
return nil, fmt.Errorf("probe error, top layer: %v", err)
}
Expand All @@ -49,7 +51,7 @@ func RenderIsometric(na NodeAccessor, cr ColorResolver, from, to *Pos, opts *Iso
// left layer (without top stride)
for x := min.X(); x <= max.X(); x++ {
for y := min.Y(); y < max.Y()-1; y++ {
pnodes, err := Probe(min, max, NewPos(x, y, min.Z()), ipos, na, cr, skip_alpha)
pnodes, err := Probe(min, max, types.NewPos(x, y, min.Z()), ipos, na, cr, skip_alpha)
if err != nil {
return nil, fmt.Errorf("probe error, left layer: %v", err)
}
Expand All @@ -60,7 +62,7 @@ func RenderIsometric(na NodeAccessor, cr ColorResolver, from, to *Pos, opts *Iso
// right layer (without top and left stride)
for z := min.Z() + 1; z < max.Z(); z++ {
for y := min.Y(); y <= max.Y()-1; y++ {
pnodes, err := Probe(min, max, NewPos(min.X(), y, z), ipos, na, cr, skip_alpha)
pnodes, err := Probe(min, max, types.NewPos(min.X(), y, z), ipos, na, cr, skip_alpha)
if err != nil {
return nil, fmt.Errorf("probe error, right layer: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions isorenderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/minetest-go/colormapping"
"github.com/minetest-go/maprenderer"
"github.com/minetest-go/types"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,8 +23,8 @@ func TestIsoRenderer(t *testing.T) {
err = cm.LoadDefaults()
assert.NoError(t, err)

from := maprenderer.NewPos(0, -10, 0)
to := maprenderer.NewPos(160, 60, 160)
from := types.NewPos(0, -10, 0)
to := types.NewPos(160, 60, 160)
opts := &maprenderer.IsoRenderOpts{
CubeLen: 8,
}
Expand Down
6 changes: 3 additions & 3 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/minetest-go/mapparser"
"github.com/minetest-go/maprenderer"
"github.com/minetest-go/types"
)

func NewMap() *Map {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (m Map) Load(csvfile string) error {
return nil
}

func (m *Map) GetNode(pos *maprenderer.Pos) (*maprenderer.Node, error) {
func (m *Map) GetNode(pos *types.Pos) (*types.Node, error) {
mb_pos := pos.Divide(16)
pos_plain := CoordToPlain(mb_pos[0], mb_pos[1], mb_pos[2])
if m.hex_data[pos_plain] == "" {
Expand All @@ -79,7 +79,7 @@ func (m *Map) GetNode(pos *maprenderer.Pos) (*maprenderer.Node, error) {

rel_pos := pos.Subtract(mb_pos.Multiply(16))

n := &maprenderer.Node{
n := &types.Node{
Name: md.GetNodeName(rel_pos.X(), rel_pos.Y(), rel_pos.Z()),
Param1: 0,
Param2: md.GetParam2(rel_pos.X(), rel_pos.Y(), rel_pos.Z()),
Expand Down
14 changes: 8 additions & 6 deletions maprenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package maprenderer

import (
"image"

"github.com/minetest-go/types"
)

type MapRenderOpts struct {
}

func RenderMap(na NodeAccessor, cr ColorResolver, from, to *Pos, opts *MapRenderOpts) (*image.NRGBA, error) {
func RenderMap(na NodeAccessor, cr ColorResolver, from, to *types.Pos, opts *MapRenderOpts) (*image.NRGBA, error) {
// from = lowest, to = highest
from, to = SortPos(from, to)
search_dir := &Pos{0, -1, 0}
from, to = types.SortPos(from, to)
search_dir := &types.Pos{0, -1, 0}

// prepare image
upLeft := image.Point{0, 0}
Expand All @@ -20,7 +22,7 @@ func RenderMap(na NodeAccessor, cr ColorResolver, from, to *Pos, opts *MapRender
for x := from.X(); x <= to.X(); x++ {
for z := from.Z(); z <= to.Z(); z++ {
// top-down search
nodes, err := Probe(from, to, NewPos(x, to.Y(), z), search_dir, na, cr, true)
nodes, err := Probe(from, to, types.NewPos(x, to.Y(), z), search_dir, na, cr, true)
if err != nil {
return nil, err
}
Expand All @@ -37,7 +39,7 @@ func RenderMap(na NodeAccessor, cr ColorResolver, from, to *Pos, opts *MapRender
}

// add shadows for view-blocking neighbors
for _, above_pos := range []*Pos{{-1, 1, 0}, {0, 1, 1}} {
for _, above_pos := range []*types.Pos{{-1, 1, 0}, {0, 1, 1}} {
nn, err := na(node.Pos.Add(above_pos))
if err != nil {
return nil, err
Expand All @@ -48,7 +50,7 @@ func RenderMap(na NodeAccessor, cr ColorResolver, from, to *Pos, opts *MapRender
}

// lighten up if no nodes directly nearby
for _, near_pos := range []*Pos{{-1, 0, 0}, {0, 0, 1}} {
for _, near_pos := range []*types.Pos{{-1, 0, 0}, {0, 0, 1}} {
nn, err := na(node.Pos.Add(near_pos))
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions maprenderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/minetest-go/colormapping"
"github.com/minetest-go/maprenderer"
"github.com/minetest-go/types"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,8 +23,8 @@ func TestRenderMap(t *testing.T) {
err = cm.LoadDefaults()
assert.NoError(t, err)

from := maprenderer.NewPos(-10, -20, -10)
to := maprenderer.NewPos(100, 50, 100)
from := types.NewPos(-10, -20, -10)
to := types.NewPos(100, 50, 100)
opts := &maprenderer.MapRenderOpts{}

img, err := maprenderer.RenderMap(m.GetNode, cm.GetColor, from, to, opts)
Expand Down
58 changes: 0 additions & 58 deletions pos.go

This file was deleted.

4 changes: 3 additions & 1 deletion probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package maprenderer

import (
"fmt"

"github.com/minetest-go/types"
)

func Probe(min, max, pos, ipos *Pos, na NodeAccessor, cr ColorResolver, skip_alpha bool) ([]*NodeWithColor, error) {
func Probe(min, max, pos, ipos *types.Pos, na NodeAccessor, cr ColorResolver, skip_alpha bool) ([]*NodeWithColor, error) {
nodes := []*NodeWithColor{}

cpos := pos
Expand Down
9 changes: 5 additions & 4 deletions probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/minetest-go/colormapping"
"github.com/minetest-go/maprenderer"
"github.com/minetest-go/types"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,10 +19,10 @@ func TestProbe(t *testing.T) {
err = cm.LoadDefaults()
assert.NoError(t, err)

min := maprenderer.NewPos(0, 0, 0)
max := maprenderer.NewPos(32, 32, 32)
pos := maprenderer.NewPos(0, 32, 0)
ipos := maprenderer.NewPos(0, -1, 0)
min := types.NewPos(0, 0, 0)
max := types.NewPos(32, 32, 32)
pos := types.NewPos(0, 32, 0)
ipos := types.NewPos(0, -1, 0)

nodes, err := maprenderer.Probe(min, max, pos, ipos, m.GetNode, cm.GetColor, false)
assert.NoError(t, err)
Expand Down
16 changes: 6 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package maprenderer

import "image/color"
import (
"image/color"

// simple node-definition
type Node struct {
Pos *Pos
Name string `json:"name"`
Param1 int `json:"param1"`
Param2 int `json:"param2"`
}
"github.com/minetest-go/types"
)

type NodeWithColor struct {
*Node
*types.Node
Color *color.RGBA
}

// returns the node at the given position, nil if no node found
type NodeAccessor func(pos *Pos) (*Node, error)
type NodeAccessor func(pos *types.Pos) (*types.Node, error)

// resolves the node-name and param2 to a color, nil if no color-mapping found
type ColorResolver func(name string, param2 int) *color.RGBA
13 changes: 0 additions & 13 deletions utils.go

This file was deleted.

Loading

0 comments on commit 5c226cc

Please sign in to comment.