-
Notifications
You must be signed in to change notification settings - Fork 0
/
biome.go
101 lines (82 loc) · 2.33 KB
/
biome.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package carto
import (
"fmt"
"image"
"image/color"
"log"
"math"
"math/bits"
"path/filepath"
"sort"
"strings"
"github.com/Tnze/go-mc/level"
"github.com/Tnze/go-mc/save"
"github.com/muesli/gamut"
)
type Biome struct {
Temperature float64 `json:"temperature"`
Downfall float64 `json:"downfall"`
}
func (b *Biome) ColorMapCoords() (int, int) {
r := clamp(b.Downfall, 0, 1) * clamp(b.Temperature, 0, 1)
x := int(math.Ceil(255 - (clamp(b.Temperature, 0, 1) * 255)))
y := int(math.Ceil(255 - (r * 255)))
return x, y
}
type BiomeRenderer struct {
biomes map[string]color.Color
}
func NewBiomeRenderer(loader *AssetLoader) *BiomeRenderer {
biomes := make(map[string]color.Color)
biomeNames := []string{}
for path := range loader.Files {
if strings.HasPrefix(path, "data/minecraft/worldgen/biome/") {
name := strings.TrimSuffix(filepath.Base(path), ".json")
biomeNames = append(biomeNames, fmt.Sprintf("minecraft:%s", name))
}
}
colors, err := gamut.Generate(len(biomeNames), gamut.PastelGenerator{})
if err != nil {
log.Panicf("Failed to generate color palette for biomes: %v", err)
}
sort.Strings(biomeNames)
for idx, biome := range biomeNames {
biomes[biome] = colors[idx]
}
return &BiomeRenderer{
biomes: biomes,
}
}
func (c *BiomeRenderer) Finalize(path string) error {
return nil
}
func (c *BiomeRenderer) ImageSize() (int, int) {
return 16, 16
}
func (c *BiomeRenderer) RenderChunk(chunk *save.Chunk) (image.Image, error) {
img := image.NewRGBA64(image.Rect(0, 0, 16, 16))
bitsForHeight := bits.Len(uint(len(chunk.Sections))*16 + 1)
motionBlocking := level.NewBitStorage(bitsForHeight, 16*16, chunk.Heightmaps["MOTION_BLOCKING"])
for x := 0; x < 16; x++ {
for z := 0; z < 16; z++ {
heightmapIndex := ((z) * 16) + x
yStart := motionBlocking.Get(heightmapIndex)
sectionY := yStart / 16
section := chunk.Sections[sectionY]
if len(section.Biomes.Palette) == 0 {
continue
}
v := calcBitsPerValue(16*16*16, len(section.Biomes.Data))
biomes := level.NewBitStorage(v, 16*16*16, section.Biomes.Data)
blockIndex := ((((sectionY) * 16) + z) * 16) + x
biomeState := section.Biomes.Palette[biomes.Get(blockIndex)]
color := c.biomes[string(biomeState)]
if color != nil {
img.Set(x, z, color)
} else {
log.Printf("unmapped biome %v", biomeState)
}
}
}
return img, nil
}