-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorio_cliffs_test.go
More file actions
357 lines (338 loc) · 11.3 KB
/
Copy pathfactorio_cliffs_test.go
File metadata and controls
357 lines (338 loc) · 11.3 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"context"
"image"
"image/color"
"math"
"testing"
)
type factorioCliffFieldOracle struct {
Positions []naturalOraclePoint `json:"positions"`
Cases []struct {
Seed uint32 `json:"seed"`
Values []float64 `json:"values"`
} `json:"cases"`
}
type factorioCliffEntityOracle struct {
Region struct {
X0 float64 `json:"x0"`
Y0 float64 `json:"y0"`
X1 float64 `json:"x1"`
Y1 float64 `json:"y1"`
} `json:"region"`
Cases []struct {
Seed uint32 `json:"seed"`
Cliffs []naturalOraclePoint `json:"cliffs"`
} `json:"cases"`
}
func TestFactorioCliffFieldsMatchReferenceOracles(t *testing.T) {
elevationFixture := readNaturalOracle[factorioCliffFieldOracle](t, "oracle-cliff-elevation.seed123456.json")
for _, testCase := range elevationFixture.Cases {
settings := defaultFactorioNaturalSettings(testCase.Seed)
evaluator := newFactorioCliffEvaluator(settings, newFactorioNauvisEvaluator(settings))
worst := 0.0
for index, position := range elevationFixture.Positions {
got := evaluator.cliffElevationAt(position.X, position.Y)
want := testCase.Values[index]
delta := math.Abs(got - want)
worst = math.Max(worst, delta)
if delta >= math.Max(1, 1e-2*math.Abs(want)) {
t.Errorf("cliff elevation seed %d at (%g,%g) = %g, want %g", testCase.Seed, position.X, position.Y, got, want)
}
}
if worst >= 10 {
t.Errorf("cliff elevation seed %d worst residual = %g", testCase.Seed, worst)
}
}
cliffinessFixture := readNaturalOracle[factorioCliffFieldOracle](t, "oracle-cliffiness.seed123456.json")
for _, testCase := range cliffinessFixture.Cases {
settings := defaultFactorioNaturalSettings(testCase.Seed)
evaluator := newFactorioCliffEvaluator(settings, newFactorioNauvisEvaluator(settings))
mismatches := 0
for index, position := range cliffinessFixture.Positions {
if got, want := evaluator.cliffinessAt(position.X, position.Y), testCase.Values[index]; got != want {
mismatches++
if mismatches <= 8 {
t.Logf("cliffiness seed %d at (%g,%g) = %g, want %g", testCase.Seed, position.X, position.Y, got, want)
}
}
}
if mismatches != 0 {
t.Errorf("cliffiness seed %d mismatches = %d/%d", testCase.Seed, mismatches, len(cliffinessFixture.Positions))
}
}
}
func TestFactorioCliffPlacementMatchesEntityOracle(t *testing.T) {
fixture := readNaturalOracle[factorioCliffEntityOracle](t, "oracle-cliff-entities.seed123456.json")
for _, testCase := range fixture.Cases {
settings := defaultFactorioNaturalSettings(testCase.Seed)
evaluator := newFactorioCliffEvaluator(settings, newFactorioNauvisEvaluator(settings))
placed, err := evaluator.placedCells(
context.Background(),
fixture.Region.X0,
fixture.Region.Y0,
fixture.Region.X1,
fixture.Region.Y1,
)
if err != nil {
t.Fatalf("place cliffs for seed %d: %v", testCase.Seed, err)
}
predicted := make(map[factorioPoint]bool, len(placed))
for _, cell := range placed {
point := cell.factorioPoint
predicted[point] = true
if positiveMod(point.x, factorioCliffGridSize) != factorioCliffCellCenterX ||
positiveMod(point.y, factorioCliffGridSize) != factorioCliffCellCenterY {
t.Errorf("predicted cliff is off lattice: (%g,%g)", point.x, point.y)
}
}
matched := 0
for _, point := range testCase.Cliffs {
if positiveMod(point.X, factorioCliffGridSize) != factorioCliffCellCenterX ||
positiveMod(point.Y, factorioCliffGridSize) != factorioCliffCellCenterY {
t.Errorf("oracle cliff is off lattice: (%g,%g)", point.X, point.Y)
}
if predicted[factorioPoint{x: point.X, y: point.Y}] {
matched++
}
}
matchFraction := float64(matched) / float64(len(testCase.Cliffs))
if matchFraction < 0.85 {
t.Errorf("cliff placement seed %d matched %d/%d (%.1f%%), want >= 85%%", testCase.Seed, matched, len(testCase.Cliffs), matchFraction*100)
}
t.Logf("cliff placement seed %d matched %d/%d (%.1f%%), predicted %d", testCase.Seed, matched, len(testCase.Cliffs), matchFraction*100, len(placed))
}
}
func TestFactorioCrossesCliff(t *testing.T) {
tests := []struct {
name string
a float64
b float64
cliffiness float64
want int
}{
{name: "same band", a: 12, b: 20, cliffiness: 10, want: 0},
{name: "negative elevation", a: -1, b: 60, cliffiness: 10, want: 0},
{name: "below first band", a: 5, b: 8, cliffiness: 10, want: 0},
{name: "gate off", a: 45, b: 55, cliffiness: 0.5, want: 0},
{name: "crossing up", a: 45, b: 55, cliffiness: 10, want: 1},
{name: "crossing down", a: 55, b: 45, cliffiness: 10, want: -1},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := factorioCrossesCliff(test.a, test.b, test.cliffiness, 10, 40); got != test.want {
t.Fatalf("crossing = %d, want %d", got, test.want)
}
})
}
}
func TestRenderFactorioCliffsPaintsChartColorAndSkipsWater(t *testing.T) {
settings := defaultFactorioNaturalSettings(123456)
evaluator := newFactorioCliffEvaluator(settings, newFactorioNauvisEvaluator(settings))
landColor := color.RGBA{R: 90, G: 90, B: 90, A: 255}
img := image.NewRGBA(image.Rect(0, 0, 64, 64))
for y := 0; y < 64; y++ {
for x := 0; x < 64; x++ {
img.SetRGBA(x, y, landColor)
}
}
if err := renderFactorioCliffs(context.Background(), img, settings, evaluator, 960, 512, 1); err != nil {
t.Fatalf("render cliffs: %v", err)
}
painted := 0
adjacent := false
for y := 0; y < 64; y++ {
for x := 0; x < 64; x++ {
if img.RGBAAt(x, y) != factorioCliffMapColor {
continue
}
painted++
if x+1 < 64 && img.RGBAAt(x+1, y) == factorioCliffMapColor ||
y+1 < 64 && img.RGBAAt(x, y+1) == factorioCliffMapColor {
adjacent = true
}
}
}
if painted == 0 || !adjacent {
t.Fatalf("painted cliff pixels = %d, adjacent = %v", painted, adjacent)
}
water := image.NewRGBA(image.Rect(0, 0, 64, 64))
for y := 0; y < 64; y++ {
for x := 0; x < 64; x++ {
water.SetRGBA(x, y, factorioTerrainTiles[0].color)
}
}
if err := renderFactorioCliffs(context.Background(), water, settings, evaluator, 960, 512, 1); err != nil {
t.Fatalf("render cliffs over water: %v", err)
}
for y := 0; y < 64; y++ {
for x := 0; x < 64; x++ {
if got := water.RGBAAt(x, y); got != factorioTerrainTiles[0].color {
t.Fatalf("water changed at (%d,%d): %#v", x, y, got)
}
}
}
}
func TestFactorioCliffStrokeFollowsCrossingOrientation(t *testing.T) {
cell := factorioCliffCell{
factorioPoint: factorioPoint{x: 10, y: 10.5},
left: 1,
right: -1,
}
painted := map[image.Point]bool{}
factorioCliffStrokeRects(cell, func(x0, y0, x1, y1 float64) {
for y := int(y0); y < int(y1); y++ {
for x := int(x0); x < int(x1); x++ {
painted[image.Pt(x, y)] = true
}
}
})
for _, point := range []image.Point{image.Pt(8, 10), image.Pt(10, 10), image.Pt(12, 10)} {
if !painted[point] {
t.Errorf("horizontal cliff stroke did not paint %v", point)
}
}
for _, point := range []image.Point{image.Pt(8, 8), image.Pt(12, 8), image.Pt(8, 12), image.Pt(12, 12)} {
if painted[point] {
t.Errorf("horizontal cliff stroke painted chunky outer corner %v", point)
}
}
}
func TestRenderFactorioCliffsStitchesExactlyAcrossSeam(t *testing.T) {
settings := defaultFactorioNaturalSettings(123456)
evaluator := newFactorioCliffEvaluator(settings, newFactorioNauvisEvaluator(settings))
cells, err := evaluator.placedCells(context.Background(), 512, 512, 1024, 1024)
if err != nil {
t.Fatalf("place cliffs: %v", err)
}
if len(cells) == 0 {
t.Fatal("expected at least one cliff cell")
}
centerX := math.Floor(cells[0].x)
centerY := math.Floor(cells[0].y)
originX := centerX - 16
originY := centerY - 16
const (
fullWidth = 32
fullHeight = 32
)
seamX := centerX + 1
leftWidth := int(seamX - originX)
rightWidth := fullWidth - leftWidth
landColor := color.RGBA{R: 90, G: 90, B: 90, A: 255}
whole := solidFactorioCliffTestImage(fullWidth, fullHeight, landColor)
if err := renderFactorioCliffs(
context.Background(),
whole,
settings,
evaluator,
originX,
originY,
1,
); err != nil {
t.Fatalf("render whole cliffs: %v", err)
}
left := solidFactorioCliffTestImage(leftWidth, fullHeight, landColor)
if err := renderFactorioCliffs(
context.Background(),
left,
settings,
evaluator,
originX,
originY,
1,
); err != nil {
t.Fatalf("render left cliffs: %v", err)
}
right := solidFactorioCliffTestImage(rightWidth, fullHeight, landColor)
if err := renderFactorioCliffs(
context.Background(),
right,
settings,
evaluator,
seamX,
originY,
1,
); err != nil {
t.Fatalf("render right cliffs: %v", err)
}
centerPixelY := int(centerY - originY)
if got := right.RGBAAt(0, centerPixelY); got != factorioCliffMapColor {
t.Fatalf("cliff centered just left of seam did not paint across it: %#v", got)
}
for y := 0; y < fullHeight; y++ {
for x := 0; x < fullWidth; x++ {
var got color.RGBA
if x < leftWidth {
got = left.RGBAAt(x, y)
} else {
got = right.RGBAAt(x-leftWidth, y)
}
if want := whole.RGBAAt(x, y); got != want {
t.Fatalf("stitched cliff pixel (%d,%d) = %#v, want %#v", x, y, got, want)
}
}
}
}
func TestFactorioCliffsDisableWithContinuityOrRichness(t *testing.T) {
for _, mutate := range []func(*fastPreviewSettings){
func(settings *fastPreviewSettings) {
settings.cliffs.size = 0
settings.cliffs.enabled = false
},
func(settings *fastPreviewSettings) { settings.cliffRichness = 0 },
} {
settings := defaultFactorioNaturalSettings(123456)
mutate(&settings)
evaluator := newFactorioCliffEvaluator(settings, newFactorioNauvisEvaluator(settings))
placed, err := evaluator.placedCells(context.Background(), 0, 0, 512, 512)
if err != nil {
t.Fatalf("place disabled cliffs: %v", err)
}
if len(placed) != 0 {
t.Fatalf("disabled cliffs placed %d cells", len(placed))
}
}
}
func TestFactorioCliffPixelSpanScalesInWorldTiles(t *testing.T) {
tests := []struct {
tilesPerPixel float64
wantMin int
wantMax int
}{
{tilesPerPixel: 0.4, wantMin: 23, wantMax: 27},
{tilesPerPixel: 1, wantMin: 9, wantMax: 10},
{tilesPerPixel: 2, wantMin: 5, wantMax: 5},
{tilesPerPixel: 2.5, wantMin: 4, wantMax: 4},
{tilesPerPixel: 4, wantMin: 2, wantMax: 2},
}
for _, test := range tests {
minPixel, maxPixel := factorioCliffPixelSpan(9, 11, 0, test.tilesPerPixel)
if minPixel != test.wantMin || maxPixel != test.wantMax {
t.Errorf("span at %g tiles/pixel = [%d,%d], want [%d,%d]", test.tilesPerPixel, minPixel, maxPixel, test.wantMin, test.wantMax)
}
}
}
func TestFactorioCliffsAllowSolidContinuity(t *testing.T) {
settings := defaultFactorioNaturalSettings(123456)
settings.cliffRichness = 10
evaluator := newFactorioCliffEvaluator(settings, newFactorioNauvisEvaluator(settings))
if math.IsNaN(evaluator.cutoff) || math.IsInf(evaluator.cutoff, 0) || evaluator.cutoff != 0 {
t.Fatalf("solid-continuity cutoff = %g, want 0", evaluator.cutoff)
}
if _, err := evaluator.placedCells(context.Background(), -64, -64, 64, 64); err != nil {
t.Fatalf("place solid-continuity cliffs: %v", err)
}
}
func positiveMod(value, divisor float64) float64 {
return math.Mod(math.Mod(value, divisor)+divisor, divisor)
}
func solidFactorioCliffTestImage(width, height int, fill color.RGBA) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.SetRGBA(x, y, fill)
}
}
return img
}