fixed buildings not generating deterministically

This commit is contained in:
Grimsace
2026-02-26 13:46:32 -06:00
parent c45aacbc46
commit 4925aab8bf
+30 -4
View File
@@ -456,14 +456,40 @@ func getComponentPixels(center image.Point, size float64, shape string, randSrc
// chooseShape selects a building shape based on the provided ratios. // chooseShape selects a building shape based on the provided ratios.
func chooseShape(randSrc *rand.Rand, ratios map[string]float64) string { func chooseShape(randSrc *rand.Rand, ratios map[string]float64) string {
var shapes []string if len(ratios) == 0 {
var weights []float64 return "squares"
}
// Deterministic iteration order so seeded runs remain reproducible.
baseOrder := []string{"squares", "circles", "rectangles"}
shapes := make([]string, 0, len(ratios))
seen := make(map[string]bool, len(ratios))
for _, shape := range baseOrder {
if _, ok := ratios[shape]; ok {
shapes = append(shapes, shape)
seen[shape] = true
}
}
// Include any extra shapes in sorted order to keep behavior stable.
extras := make([]string, 0, len(ratios))
for shape := range ratios {
if !seen[shape] {
extras = append(extras, shape)
}
}
sort.Strings(extras)
shapes = append(shapes, extras...)
weights := make([]float64, 0, len(shapes))
var cumulativeWeight float64 var cumulativeWeight float64
for shape, weight := range ratios { for _, shape := range shapes {
shapes = append(shapes, shape) weight := ratios[shape]
cumulativeWeight += weight cumulativeWeight += weight
weights = append(weights, cumulativeWeight) weights = append(weights, cumulativeWeight)
} }
if cumulativeWeight <= 0 {
return shapes[0]
}
// Generate a random number between 0 and the total weight // Generate a random number between 0 and the total weight
randNum := randSrc.Float64() * cumulativeWeight randNum := randSrc.Float64() * cumulativeWeight