Files
RPG_City_Maker_Reborn/buildings.go
T

198 lines
6.1 KiB
Go
Raw Normal View History

2026-02-05 15:11:55 -06:00
package main
import (
"image"
"image/color"
"math"
"math/rand"
"sort"
)
2026-02-05 17:50:55 -06:00
// GenerateBuildings creates and places buildings on the map.
2026-02-05 15:11:55 -06:00
func GenerateBuildings(img *image.RGBA, width, height int, settings *Settings, roadPixels, allWaterPixels []image.Point, seed int64) []image.Point {
2026-02-05 17:50:55 -06:00
// Early exit if no buildings are to be generated
2026-02-05 15:11:55 -06:00
if settings.NumBuildings == 0 {
return nil
}
2026-02-05 17:50:55 -06:00
// Initialize random number generator
2026-02-05 15:11:55 -06:00
randSrc := rand.New(rand.NewSource(seed))
buildingColor := color.RGBA{R: 128, G: 128, B: 128, A: 255} // Gray color for buildings
2026-02-05 17:50:55 -06:00
// Create lookup maps for water and road pixels for efficient collision detection
2026-02-05 15:11:55 -06:00
isWater := make(map[image.Point]bool)
for _, p := range allWaterPixels {
isWater[p] = true
}
isRoad := make(map[image.Point]bool)
for _, p := range roadPixels {
isRoad[p] = true
}
2026-02-05 17:50:55 -06:00
// Initialize building data structures
2026-02-05 15:11:55 -06:00
isBuilding := make(map[image.Point]bool)
var buildingPixels []image.Point
var anchorPoints []image.Point
2026-02-05 17:50:55 -06:00
// Determine anchor points for building placement
2026-02-05 15:11:55 -06:00
if len(roadPixels) > 0 {
anchorPoints = roadPixels
} else {
// If no roads, use all land pixels as anchors
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
p := image.Point{X: x, Y: y}
if !isWater[p] {
anchorPoints = append(anchorPoints, p)
}
}
}
}
2026-02-05 17:50:55 -06:00
// Early exit if no anchor points are available
2026-02-05 15:11:55 -06:00
if len(anchorPoints) == 0 {
return nil
}
2026-02-05 17:50:55 -06:00
// Sort anchor points for deterministic placement
2026-02-05 15:11:55 -06:00
sort.Slice(anchorPoints, func(i, j int) bool {
if anchorPoints[i].Y != anchorPoints[j].Y {
return anchorPoints[i].Y < anchorPoints[j].Y
}
return anchorPoints[i].X < anchorPoints[j].X
})
2026-02-05 17:50:55 -06:00
// Collect all land points for random placement
2026-02-05 15:11:55 -06:00
landPoints := make([]image.Point, 0, width*height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
p := image.Point{X: x, Y: y}
if !isWater[p] && !isRoad[p] {
landPoints = append(landPoints, p)
}
}
}
2026-02-05 17:50:55 -06:00
// Main loop for placing buildings
2026-02-05 15:11:55 -06:00
buildingsPlaced := 0
2026-02-05 17:50:55 -06:00
searchTries := 100 // Number of attempts to find a spot for a building around an anchor
2026-02-05 15:11:55 -06:00
maxPlacementAttempts := settings.NumBuildings * 5 // To prevent infinite loops
for buildingsPlaced < settings.NumBuildings && maxPlacementAttempts > 0 {
maxPlacementAttempts--
2026-02-05 17:50:55 -06:00
// Select an anchor point for the new building
2026-02-05 15:11:55 -06:00
var anchor image.Point
if randSrc.Float64() > settings.BuildingDistribution/100.0 {
2026-02-05 17:50:55 -06:00
// Place near roads or other existing features
2026-02-05 15:11:55 -06:00
anchor = anchorPoints[randSrc.Intn(len(anchorPoints))]
} else {
2026-02-05 17:50:55 -06:00
// Place randomly on any available land
2026-02-05 15:11:55 -06:00
if len(landPoints) == 0 {
continue // No land to place buildings on
}
anchor = landPoints[randSrc.Intn(len(landPoints))]
}
2026-02-05 17:50:55 -06:00
// Search for a valid building location around the anchor
2026-02-05 15:11:55 -06:00
for i := 0; i < searchTries; i++ {
searchRadius := float64(i) * 2.0 // Search in expanding circles
angle := randSrc.Float64() * 2 * math.Pi
dist := searchRadius * randSrc.Float64()
center := image.Point{
X: anchor.X + int(dist*math.Cos(angle)),
Y: anchor.Y + int(dist*math.Sin(angle)),
}
2026-02-05 17:50:55 -06:00
// For fully random distribution, pick any point on the map
2026-02-05 15:11:55 -06:00
if settings.BuildingDistribution == 100 {
center = image.Point{
X: randSrc.Intn(width),
Y: randSrc.Intn(height),
}
}
2026-02-05 17:50:55 -06:00
// Ensure the center point is within the map boundaries
2026-02-05 15:11:55 -06:00
if center.X < 0 || center.Y < 0 || center.X >= width || center.Y >= height {
continue
}
2026-02-05 17:50:55 -06:00
// Attempt to create a building at the selected center
2026-02-05 15:11:55 -06:00
size := settings.MinBuildingSize + randSrc.Float64()*(settings.MaxBuildingSize-settings.MinBuildingSize)
pixels, ok := getBuildingPixels(center, size, settings.BuildingShape, isWater, isRoad, isBuilding, width, height, randSrc)
if ok {
2026-02-05 17:50:55 -06:00
// If successful, draw the building and update data structures
2026-02-05 15:11:55 -06:00
for _, p := range pixels {
img.Set(p.X, p.Y, buildingColor)
isBuilding[p] = true
buildingPixels = append(buildingPixels, p)
}
buildingsPlaced++
2026-02-05 17:50:55 -06:00
break // Move to the next building
2026-02-05 15:11:55 -06:00
}
}
}
return buildingPixels
}
2026-02-05 17:50:55 -06:00
// getBuildingPixels determines the pixels for a single building based on its shape and checks for collisions.
2026-02-05 15:11:55 -06:00
func getBuildingPixels(center image.Point, size float64, shape string, isWater, isRoad, isBuilding map[image.Point]bool, width, height int, randSrc *rand.Rand) ([]image.Point, bool) {
var pixels []image.Point
var halfSize = int(size / 2)
2026-02-05 17:50:55 -06:00
// Generate pixels based on the selected building shape
2026-02-05 15:11:55 -06:00
switch shape {
case "squares":
for y := center.Y - halfSize; y <= center.Y+halfSize; y++ {
for x := center.X - halfSize; x <= center.X+halfSize; x++ {
p := image.Point{X: x, Y: y}
if p.X < 0 || p.Y < 0 || p.X >= width || p.Y >= height || isWater[p] || isRoad[p] || isBuilding[p] {
2026-02-05 17:50:55 -06:00
return nil, false // Collision detected
2026-02-05 15:11:55 -06:00
}
pixels = append(pixels, p)
}
}
case "circles":
r2 := (size / 2) * (size / 2)
for y := center.Y - halfSize; y <= center.Y+halfSize; y++ {
for x := center.X - halfSize; x <= center.X+halfSize; x++ {
dx, dy := float64(x-center.X), float64(y-center.Y)
if dx*dx+dy*dy <= r2 {
p := image.Point{X: x, Y: y}
if p.X < 0 || p.Y < 0 || p.X >= width || p.Y >= height || isWater[p] || isRoad[p] || isBuilding[p] {
2026-02-05 17:50:55 -06:00
return nil, false // Collision detected
2026-02-05 15:11:55 -06:00
}
pixels = append(pixels, p)
}
}
}
case "rectangles":
2026-02-05 17:50:55 -06:00
// Create rectangles with varied aspect ratios
2026-02-05 15:11:55 -06:00
longSide := size
shortSide := randSrc.Float64()*(size-float64(halfSize)) + float64(halfSize)
var w, h int
if randSrc.Intn(2) == 0 {
w, h = int(longSide), int(shortSide)
} else {
w, h = int(shortSide), int(longSide)
}
halfW, halfH := w/2, h/2
2026-02-05 17:50:55 -06:00
// Check for collisions and gather pixels
2026-02-05 15:11:55 -06:00
for y := center.Y - halfH; y <= center.Y+halfH; y++ {
for x := center.X - halfW; x <= center.X+halfW; x++ {
p := image.Point{X: x, Y: y}
if p.X < 0 || p.Y < 0 || p.X >= width || p.Y >= height || isWater[p] || isRoad[p] || isBuilding[p] {
2026-02-05 17:50:55 -06:00
return nil, false // Collision detected
2026-02-05 15:11:55 -06:00
}
pixels = append(pixels, p)
}
}
}
2026-02-05 17:50:55 -06:00
// Final check to ensure pixels were generated
2026-02-05 15:11:55 -06:00
if len(pixels) == 0 {
return nil, false
}
return pixels, true
}