-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_enemies_test.go
More file actions
303 lines (288 loc) · 9.36 KB
/
Copy pathpreview_enemies_test.go
File metadata and controls
303 lines (288 loc) · 9.36 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
package main
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"image"
"math"
"os"
"path/filepath"
"testing"
)
const enemyPreviewParityEnv = "FACTMAPGEN_ENEMY_PREVIEW_PARITY"
type enemyPreviewStats struct {
Seed string `json:"seed"`
TilesPerPixel float64 `json:"tilesPerPixel"`
RegionCorrelation float64 `json:"regionCorrelation"`
FastToFactorioPixels float64 `json:"fastToFactorioPixels"`
FactorioPixels int `json:"factorioPixels"`
FastPixels int `json:"fastPixels"`
FactorioNearest float64 `json:"factorioNearest"`
FastNearest float64 `json:"fastNearest"`
RadialEdges []float64 `json:"radialEdges"`
FactorioRadialPixels []int `json:"factorioRadialPixels"`
FastRadialPixels []int `json:"fastRadialPixels"`
}
func TestFastEnemyLayersMatchFactorioPreviewRegions(t *testing.T) {
if os.Getenv(enemyPreviewParityEnv) != "1" {
t.Skipf("set %s=1 to compare enemy-base regions against Factorio", enemyPreviewParityEnv)
}
if testing.Short() {
t.Skip("skipping enemy-layer Factorio integration test in short mode")
}
const (
outputSize = 1024
tilesPerPixel = 1.0
)
factorioBin := ""
store := newTestStore(t)
_, sourcePath := previewParityProfile(t, store, "default:Default")
mapGenPath := enemyLayerMapGenPath(t, sourcePath)
for _, seed := range []string{"123456"} {
t.Run("seed-"+seed, func(t *testing.T) {
factorioImg, _ := cachedFactorioEnemyPreview(
t, &factorioBin, mapGenPath, seed, outputSize, tilesPerPixel,
)
fastImg := renderFastEnemyLayer(t, mapGenPath, seed, outputSize, tilesPerPixel)
stats := measureEnemyPreview(seed, tilesPerPixel, factorioImg, fastImg)
writeEnemyPreviewArtifacts(t, seed, factorioImg, fastImg, stats)
if stats.FactorioPixels == 0 || stats.FastPixels == 0 {
t.Fatalf("enemy mask is empty: Factorio=%d fast=%d", stats.FactorioPixels, stats.FastPixels)
}
if stats.RegionCorrelation < 0.85 ||
stats.FastToFactorioPixels < 0.65 || stats.FastToFactorioPixels > 1.6 {
t.Errorf(
"enemy regions diverged: correlation=%.3f fast/game pixels=%.3f",
stats.RegionCorrelation,
stats.FastToFactorioPixels,
)
}
if math.Abs(stats.FastNearest-stats.FactorioNearest) > 25 {
t.Errorf(
"enemy starting-area edge diverged: Factorio=%.1f fast=%.1f",
stats.FactorioNearest,
stats.FastNearest,
)
}
for ring := range stats.FactorioRadialPixels {
if stats.FactorioRadialPixels[ring] < 25 {
continue
}
ratio := previewCountRatio(stats.FastRadialPixels[ring], stats.FactorioRadialPixels[ring])
if ratio < 0.65 || ratio > 1.6 {
t.Errorf(
"enemy radial population ring %d diverged: fast/game=%.3f pixels=%d/%d",
ring, ratio, stats.FastRadialPixels[ring], stats.FactorioRadialPixels[ring],
)
}
}
t.Logf(
"enemies correlation=%.3f fast/game pixels=%.3f nearest Factorio=%.1f fast=%.1f radial Factorio=%v fast=%v",
stats.RegionCorrelation,
stats.FastToFactorioPixels,
stats.FactorioNearest,
stats.FastNearest,
stats.FactorioRadialPixels,
stats.FastRadialPixels,
)
})
}
}
func enemyLayerMapGenPath(t *testing.T, sourcePath string) string {
t.Helper()
body, err := os.ReadFile(sourcePath)
if err != nil {
t.Fatalf("read enemy-layer map settings: %v", err)
}
body, err = enemyLayersMapGenJSON(body)
if err != nil {
t.Fatalf("build enemy-layer map settings: %v", err)
}
path := filepath.Join(t.TempDir(), "enemies-map-gen-settings.json")
if err := os.WriteFile(path, append(body, '\n'), 0o644); err != nil {
t.Fatalf("write enemy-layer map settings: %v", err)
}
return path
}
func enemyLayersMapGenJSON(body []byte) ([]byte, error) {
body, err := naturalLayersMapGenJSON(body, false, false)
if err != nil {
return nil, err
}
var root map[string]any
if err := json.Unmarshal(body, &root); err != nil {
return nil, err
}
controls := fastMap(root["autoplace_controls"])
controls["enemy-base"] = map[string]any{"frequency": 1, "size": 1, "richness": 1}
root["autoplace_controls"] = controls
root["no_enemies_mode"] = false
root["peaceful_mode"] = false
autoplace := fastMap(root["autoplace_settings"])
autoplace["entity"] = map[string]any{
"treat_missing_as_default": true,
"settings": map[string]any{
"fish": map[string]any{"frequency": 0, "size": 0, "richness": 0},
},
}
root["autoplace_settings"] = autoplace
return json.MarshalIndent(root, "", " ")
}
func cachedFactorioEnemyPreview(
t *testing.T,
factorioBin *string,
mapGenPath, seed string,
outputSize int,
tilesPerPixel float64,
) (image.Image, bool) {
t.Helper()
mapGenBody, err := os.ReadFile(mapGenPath)
if err != nil {
t.Fatalf("read enemy preview cache settings: %v", err)
}
settingsHash := sha256.Sum256(mapGenBody)
cachePath := filepath.Join(
"test-output",
"preview-cache",
"enemy-layers",
fmt.Sprintf(
"seed-%s-enemies-%dpx-%gtpp-%x-factorio.png",
seed, outputSize, tilesPerPixel, settingsHash[:6],
),
)
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
t.Fatalf("create enemy preview cache: %v", err)
}
return galleryReferenceImage(t, cachePath, outputSize, func() image.Image {
if *factorioBin == "" {
*factorioBin = requirePreviewParityFactorio(t)
}
sourceSize := int(math.Ceil(float64(outputSize) * tilesPerPixel))
body := renderFactorioPreviewPNG(t, *factorioBin, mapGenPath, sourceSize, seed)
source := decodePNG(t, body)
if tilesPerPixel == 1 {
return source
}
return scalePreviewImage(source, outputSize, tilesPerPixel)
})
}
func renderFastEnemyLayer(
t *testing.T,
mapGenPath, seed string,
size int,
tilesPerPixel float64,
) image.Image {
t.Helper()
body, err := readNormalizedMapGenJSON(mapGenPath)
if err != nil {
t.Fatalf("read fast enemy-layer settings: %v", err)
}
settings, err := parseFastPreviewSettings(body, seed)
if err != nil {
t.Fatalf("parse fast enemy-layer settings: %v", err)
}
img, _, err := renderFastMapPreview(
context.Background(),
settings,
size,
previewZoom{mode: "scale", tilesPerPixel: tilesPerPixel, renderSize: size},
)
if err != nil {
t.Fatalf("render fast enemy layer: %v", err)
}
return img
}
func measureEnemyPreview(
seed string,
tilesPerPixel float64,
factorioImg, fastImg image.Image,
) enemyPreviewStats {
factorioMask := previewColorMask(factorioImg, factorioEnemyMapColor)
fastMask := previewColorMask(fastImg, factorioEnemyMapColor)
factorioPixels := previewMaskCount(factorioMask)
fastPixels := previewMaskCount(fastMask)
bounds := factorioImg.Bounds()
radialEdges := []float64{256, 384, 512, 1024}
factorioRadial := enemyMaskRadialPixels(factorioMask, bounds.Dx(), bounds.Dy(), radialEdges)
fastRadial := enemyMaskRadialPixels(fastMask, bounds.Dx(), bounds.Dy(), radialEdges)
return enemyPreviewStats{
Seed: seed,
TilesPerPixel: tilesPerPixel,
RegionCorrelation: previewPearsonCorrelation(enemyMaskBlocks(factorioMask, bounds.Dx(), bounds.Dy(), 32), enemyMaskBlocks(fastMask, bounds.Dx(), bounds.Dy(), 32)),
FastToFactorioPixels: previewCountRatio(fastPixels, factorioPixels),
FactorioPixels: factorioPixels,
FastPixels: fastPixels,
FactorioNearest: enemyMaskNearest(factorioMask, bounds.Dx(), bounds.Dy()),
FastNearest: enemyMaskNearest(fastMask, bounds.Dx(), bounds.Dy()),
RadialEdges: radialEdges,
FactorioRadialPixels: factorioRadial,
FastRadialPixels: fastRadial,
}
}
func enemyMaskRadialPixels(mask []bool, width, height int, edges []float64) []int {
counts := make([]int, len(edges))
centerX := float64(width) / 2
centerY := float64(height) / 2
for index, marked := range mask {
if !marked {
continue
}
distance := math.Hypot(float64(index%width)-centerX, float64(index/width)-centerY)
for ring, edge := range edges {
if distance < edge {
counts[ring]++
break
}
}
}
return counts
}
func enemyMaskBlocks(mask []bool, width, height, blockSize int) []float64 {
blocksX := (width + blockSize - 1) / blockSize
blocksY := (height + blockSize - 1) / blockSize
blocks := make([]float64, blocksX*blocksY)
for index, marked := range mask {
if marked {
blocks[(index/width/blockSize)*blocksX+(index%width)/blockSize]++
}
}
return blocks
}
func enemyMaskNearest(mask []bool, width, height int) float64 {
centerX := float64(width) / 2
centerY := float64(height) / 2
nearest := math.Inf(1)
for index, marked := range mask {
if !marked {
continue
}
x := float64(index%width) - centerX
y := float64(index/width) - centerY
nearest = min(nearest, math.Hypot(x, y))
}
return nearest
}
func writeEnemyPreviewArtifacts(
t *testing.T,
seed string,
factorioImg, fastImg image.Image,
stats enemyPreviewStats,
) {
t.Helper()
dir := filepath.Join("test-output", "preview-diffs", "enemies-seed-"+seed)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("create enemy preview artifacts: %v", err)
}
writePNGArtifact(t, filepath.Join(dir, "factorio.png"), factorioImg)
writePNGArtifact(t, filepath.Join(dir, "fast.png"), fastImg)
_, diff, _ := previewImageDiff(factorioImg, fastImg)
writePNGArtifact(t, filepath.Join(dir, "diff.png"), diff)
body, err := json.MarshalIndent(stats, "", " ")
if err != nil {
t.Fatalf("marshal enemy preview stats: %v", err)
}
if err := os.WriteFile(filepath.Join(dir, "stats.json"), append(body, '\n'), 0o644); err != nil {
t.Fatalf("write enemy preview stats: %v", err)
}
}