alchemyst/engine/gamemap/mapgens/delaunay_mst.go
2019-11-14 21:14:52 +03:00

68 lines
1.6 KiB
Go

package mapgens
import (
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util"
"lab.zaar.be/thefish/alchemyst-go/util/appctx"
"lab.zaar.be/thefish/alchemyst-go/util/delaunay"
)
func DelaunayMstGen(ctx appctx.ClientCtx, l *gamemap.Level) (*gamemap.Level, []gamemap.Room) {
rng := util.NewRNG()
//fill with walls
for i := 0; i < l.W; i ++ {
for j := 0; j < l.H; j++ {
l.SetTileByXY(i, j, gamemap.NewWall())
}
}
rooms := GetRandomRoomList(ctx, rng, l, maxrooms, minRoomSize, maxRoomSize)
for _, room := range rooms {
err := room.BlitToLevel(l)
if err != nil {
fmt.Printf("err: %v", err)
}
}
centers := make([]types.Coords, 0)
for _, room := range rooms {
centers = append(centers, room.Center)
}
edges := delaunay.GetMst(centers, l.W, l.H)
for _, edge := range edges {
MedianStraight(rng, l, rooms, centers, edge)
}
return l, rooms
}
func MedianStraight(rng *util.RNG, l *gamemap.Level, rooms []gamemap.Room, centers []types.Coords, edge types.Edge) {
//find connected rooms
var fromRoom, toRoom gamemap.Room
for _, room := range rooms {
if room.Center == edge.From {
fromRoom = room
continue
}
if room.Center == edge.To {
toRoom = room
continue
}
if len(fromRoom.Connectors) > 0 && len(toRoom.Connectors) > 0 {
break
}
}
midpoint := edge.Midpoint()
fromConnector := FindNearestConnector(midpoint, fromRoom)
toConnector := FindNearestConnector(midpoint, toRoom)
ConnectStraight(rng, l, fromConnector, toConnector, midpoint)
}