sped up road generation via multithreading
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -122,13 +123,15 @@ func generatePOIs(width, height int, settings *Settings, allWaterPixels []image.
|
|||||||
|
|
||||||
return pois
|
return pois
|
||||||
}
|
}
|
||||||
|
|
||||||
func connectPOIs(pois []*PointOfInterest, width, height int, settings *Settings, randSrc *rand.Rand, allWaterPixels []image.Point) []*Road {
|
func connectPOIs(pois []*PointOfInterest, width, height int, settings *Settings, randSrc *rand.Rand, allWaterPixels []image.Point) []*Road {
|
||||||
if len(pois) < 2 {
|
if len(pois) < 2 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var roads []*Road
|
var roads []*Road
|
||||||
|
var roadChan = make(chan *Road)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
visited := make(map[*PointOfInterest]bool)
|
visited := make(map[*PointOfInterest]bool)
|
||||||
existingRoads := make(map[string]bool)
|
existingRoads := make(map[string]bool)
|
||||||
|
|
||||||
@@ -205,20 +208,32 @@ func connectPOIs(pois []*PointOfInterest, width, height int, settings *Settings,
|
|||||||
key = fmt.Sprintf("%p-%p", closest, fromNode)
|
key = fmt.Sprintf("%p-%p", closest, fromNode)
|
||||||
}
|
}
|
||||||
existingRoads[key] = true
|
existingRoads[key] = true
|
||||||
|
wg.Add(1)
|
||||||
path := calculateRoadPath(fromNode, closest, settings.RoadCurvyness/100.0, avgDim, randSrc, numControlPoints, allWaterPixels)
|
go func(fromNode, closest *PointOfInterest) {
|
||||||
|
defer wg.Done()
|
||||||
roads = append(roads, &Road{
|
localRand := rand.New(rand.NewSource(randSrc.Int63()))
|
||||||
Start: fromNode,
|
path := calculateRoadPath(fromNode, closest, settings.RoadCurvyness/100.0, avgDim, localRand, numControlPoints, allWaterPixels)
|
||||||
End: closest,
|
roadChan <- &Road{
|
||||||
Points: path,
|
Start: fromNode,
|
||||||
})
|
End: closest,
|
||||||
|
Points: path,
|
||||||
|
}
|
||||||
|
}(fromNode, closest)
|
||||||
} else {
|
} else {
|
||||||
// No more reachable POIs
|
// No more reachable POIs
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(roadChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
for road := range roadChan {
|
||||||
|
roads = append(roads, road)
|
||||||
|
}
|
||||||
|
|
||||||
for _, road := range roads {
|
for _, road := range roads {
|
||||||
road.Importance = road.Start.Connections + road.End.Connections
|
road.Importance = road.Start.Connections + road.End.Connections
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user