-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_zoom_test.go
More file actions
126 lines (118 loc) · 3.71 KB
/
Copy pathpreview_zoom_test.go
File metadata and controls
126 lines (118 loc) · 3.71 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"context"
"image"
"image/color"
"math"
"testing"
)
func TestScalePreviewImageSupportsArbitraryScale(t *testing.T) {
source := image.NewRGBA(image.Rect(10, 20, 22, 32))
for y := source.Bounds().Min.Y; y < source.Bounds().Max.Y; y++ {
for x := source.Bounds().Min.X; x < source.Bounds().Max.X; x++ {
source.SetRGBA(x, y, color.RGBA{R: uint8(x), G: uint8(y), A: 255})
}
}
tests := []struct {
name string
scale float64
xs []uint8
ys []uint8
}{
{name: "zoom in", scale: 0.75, xs: []uint8{14, 15, 16, 16}, ys: []uint8{24, 25, 26, 26}},
{name: "zoom out", scale: 2.25, xs: []uint8{11, 13, 16, 18}, ys: []uint8{21, 23, 26, 28}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := scalePreviewImage(source, 4, test.scale).(*image.RGBA)
for y, wantY := range test.ys {
for x, wantX := range test.xs {
pixel := got.RGBAAt(x, y)
if pixel.R != wantX || pixel.G != wantY || pixel.A != 255 {
t.Fatalf("pixel (%d,%d) = %#v, want source (%d,%d)", x, y, pixel, wantX, wantY)
}
}
}
})
}
}
func TestFastPreviewArbitraryZoomRepeatsWholeTiles(t *testing.T) {
const (
size = 64
scale = 0.4
)
settings := defaultFactorioTerrainSettings(123456)
img, gotScale, err := renderFastMapPreview(
context.Background(),
settings,
size,
previewZoom{mode: "scale", tilesPerPixel: scale, renderSize: size},
)
if err != nil {
t.Fatalf("render arbitrary zoom: %v", err)
}
if gotScale != scale {
t.Fatalf("tiles per pixel = %g, want %g", gotScale, scale)
}
origin := -float64(size) * scale / 2
for y := 0; y < size; y++ {
worldY := math.Floor(origin + float64(y)*scale)
for x := 0; x < size; x++ {
if x > 0 && math.Floor(origin+float64(x-1)*scale) == math.Floor(origin+float64(x)*scale) {
if img.RGBAAt(x, y) != img.RGBAAt(x-1, y) {
t.Fatalf("same world tile changed between pixels (%d,%d) and (%d,%d)", x-1, y, x, y)
}
}
if y > 0 && math.Floor(origin+float64(y-1)*scale) == worldY {
if img.RGBAAt(x, y) != img.RGBAAt(x, y-1) {
t.Fatalf("same world tile changed between pixels (%d,%d) and (%d,%d)", x, y-1, x, y)
}
}
}
}
}
func TestFastNaturalLayersArbitraryZoomRepeatWholeTiles(t *testing.T) {
const (
size = 128
scale = 0.4
)
settings := defaultFactorioNaturalSettings(123456)
settings.startingPositions = []factorioPoint{{x: 10_000, y: 10_000}}
img, _, err := renderFastMapPreview(
context.Background(),
settings,
size,
previewZoom{mode: "scale", tilesPerPixel: scale, renderSize: size},
)
if err != nil {
t.Fatalf("render natural layers at arbitrary zoom: %v", err)
}
terrainSettings := settings
terrainSettings.trees.enabled = false
terrainSettings.cliffs.enabled = false
terrain, _, err := renderFastMapPreview(
context.Background(),
terrainSettings,
size,
previewZoom{mode: "scale", tilesPerPixel: scale, renderSize: size},
)
if err != nil {
t.Fatalf("render terrain at arbitrary zoom: %v", err)
}
if img.Rect == terrain.Rect && bytes.Equal(img.Pix, terrain.Pix) {
t.Fatal("natural-layer zoom fixture did not render any tree or cliff overlay")
}
origin := -float64(size) * scale / 2
for y := 0; y < size; y++ {
worldY := math.Floor(origin + float64(y)*scale)
for x := 0; x < size; x++ {
if x > 0 && math.Floor(origin+float64(x-1)*scale) == math.Floor(origin+float64(x)*scale) && img.RGBAAt(x, y) != img.RGBAAt(x-1, y) {
t.Fatalf("natural overlays changed within world tile between pixels (%d,%d) and (%d,%d)", x-1, y, x, y)
}
if y > 0 && math.Floor(origin+float64(y-1)*scale) == worldY && img.RGBAAt(x, y) != img.RGBAAt(x, y-1) {
t.Fatalf("natural overlays changed within world tile between pixels (%d,%d) and (%d,%d)", x, y-1, x, y)
}
}
}
}