summaryrefslogtreecommitdiff
path: root/tools.go
blob: 11b6cef706d518d570115e6b33e6e6d18400233f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main

import (
	"github.com/kch42/gomcmap/mcmap"
)

type BiomeGetSetter interface {
	GetBiomeAt(x, z int) (mcmap.Biome, bool)
	SetBiomeAt(x, z int, bio mcmap.Biome)
}

type XZPos struct {
	X, Z int
}

type Tool interface {
	SingleClick() bool // Whether only one click should be performed (true) or the action should be repeated, if the mouse is dragged
	Do(bio mcmap.Biome, biogs BiomeGetSetter, x, z int)
}

type drawTool struct {
	radGetter func() int
}

func (d *drawTool) SingleClick() bool { return false }

func (d *drawTool) Do(bio mcmap.Biome, biogs BiomeGetSetter, x, z int) {
	rad := d.radGetter()
	if rad <= 0 {
		return
	}

	for xp := x - (rad - 1); xp < x+rad; xp++ {
		for zp := z - (rad - 1); zp < z+rad; zp++ {
			biogs.SetBiomeAt(xp, zp, bio)
		}
	}
}

func NewDrawTool(radGetter func() int) *drawTool {
	return &drawTool{radGetter}
}

type fillTool struct{}

func (f *fillTool) SingleClick() bool { return true }

func (f *fillTool) Do(bio mcmap.Biome, biogs BiomeGetSetter, x, z int) {
	if oldbio, ok := biogs.GetBiomeAt(x, z); ok {
		floodfill(oldbio, bio, biogs, x, z)
	}
}

func floodfill(oldbio, newbio mcmap.Biome, biogs BiomeGetSetter, x, z int) {
	if bio, ok := biogs.GetBiomeAt(x, z); ok && (bio == oldbio) {
		biogs.SetBiomeAt(x, z, newbio)

		floodfill(oldbio, newbio, biogs, x-1, z)
		floodfill(oldbio, newbio, biogs, x+1, z)
		floodfill(oldbio, newbio, biogs, x, z-1)
		floodfill(oldbio, newbio, biogs, x, z+1)
	}
}

func NewFillTool() *fillTool {
	return new(fillTool)
}