make context great again

This commit is contained in:
2019-11-14 22:28:58 +03:00
parent 3560be99a1
commit 6a37870bd2
12 changed files with 160 additions and 84 deletions

View File

@ -1,6 +1,7 @@
package gamemap
import (
"context"
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util/appctx"
@ -13,7 +14,7 @@ var mapHeight = 90
type Level struct {
types.Rect
ctx appctx.ClientCtx
ctx context.Context
Name string
Branch string
Depth int
@ -53,17 +54,26 @@ func (l *Level) SetTileByXY (x,y int, tile *Tile) {
l.Tiles[y*l.W+x] = tile
}
//only replaces tile if original is not passable
func (l *Level) MakePassByXY (x,y int, tile *Tile) {
t := l.Tiles[y*l.W+x]
if t.BlocksPass {
l.Tiles[y*l.W+x] = tile
}
}
func (l *Level) Put (x, y int, tileFunc interface{}) {
tile := tileFunc.(func() *Tile)()
if tile == nil {
l.ctx.Logger().Fatal().Msgf("Got non-tile type to put into level: %v", tile)
appctx.Logger(l.ctx).Fatal().Msgf("Got non-tile type to put into level: %v", tile)
}
if l.InBounds(types.Coords{x, y}) {
l.Tiles[y*l.W+x] = tile
}
}
func NewLevel(ctx appctx.ClientCtx, branch string, depth int) *Level {
func NewLevel(ctx context.Context, branch string, depth int) *Level {
l := &Level{
ctx: ctx,
Name: branch + string(depth),
@ -72,7 +82,7 @@ func NewLevel(ctx appctx.ClientCtx, branch string, depth int) *Level {
}
l.Tiles = make([]*Tile, l.W*l.H)
ctx.Logger().Debug().Msgf("Generating level of branch %s depth %d", branch, depth)
appctx.Logger(ctx).Debug().Msgf("Generating level of branch %s depth %d", branch, depth)
return l
}

View File

@ -1,6 +1,7 @@
package mapgens
import (
"context"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/util"
@ -9,7 +10,7 @@ import (
//fixme move to config
var minRoomSize = 5
var maxRoomSize = 25
var maxRoomSize = 15
var maxrooms = 200
var fges = map[int]types.RectFill{
@ -38,7 +39,7 @@ var fges = map[int]types.RectFill{
},
}
func GetRandomRoomList(ctx appctx.ClientCtx, rng *util.RNG, l *gamemap.Level, maxRooms, minRoomSize, maxRoomSize int, ) []gamemap.Room{
func GetRandomRoomList(ctx context.Context, rng *util.RNG, l *gamemap.Level, maxRooms, minRoomSize, maxRoomSize int, ) []gamemap.Room{
rooms := make([]gamemap.Room, 0)
pfLoader := gamemap.NewPrefabLoader(ctx)
pfRooms := pfLoader.PrefabRoomsList()
@ -102,6 +103,28 @@ func GetRandomRoomList(ctx appctx.ClientCtx, rng *util.RNG, l *gamemap.Level, ma
}
//delaunay helper funcs
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)
}
func FindNearestConnector(midpoint types.Coords, room gamemap.Room) types.Coords {
var nearest types.Coords
@ -118,8 +141,8 @@ func FindNearestConnector(midpoint types.Coords, room gamemap.Room) types.Coords
}
func ConnectStraight(rng *util.RNG, l *gamemap.Level, from, to, midpoint types.Coords) {
toss := rng.Range(0, 1)
if toss == 0 {
toss := rng.Range(1, 2)
if toss > 1 {
DigHTunnel(l, from.X, midpoint.X, from.Y)
DigVTunnel(l, from.Y, to.Y, midpoint.X)
DigHTunnel(l, midpoint.X, to.X, to.Y)
@ -141,7 +164,7 @@ func DigHTunnel(l *gamemap.Level, x1, x2, y int) {
}
for i := start; i <= finish; i++ {
if l.InBounds(types.Coords{i, y}) {
l.SetTileByXY(i, y, gamemap.NewFloor())
l.MakePassByXY(i, y, gamemap.NewFloor())
//l.Tiles[i][y] = gamemap.NewFloor()
}
}
@ -158,7 +181,7 @@ func DigVTunnel(l *gamemap.Level, y1, y2, x int) {
}
for i := start; i <= finish; i++ {
if l.InBounds(types.Coords{x, i}) {
l.SetTileByXY(x, i, gamemap.NewFloor())
l.MakePassByXY(x, i, gamemap.NewFloor())
}
}
}

View File

@ -1,15 +1,15 @@
package mapgens
import (
"context"
"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) {
func DelaunayMstGen(ctx context.Context, l *gamemap.Level) (*gamemap.Level, []gamemap.Room) {
rng := util.NewRNG()
@ -33,7 +33,7 @@ func DelaunayMstGen(ctx appctx.ClientCtx, l *gamemap.Level) (*gamemap.Level, []g
for _, room := range rooms {
centers = append(centers, room.Center)
}
edges := delaunay.GetMst(centers, l.W, l.H)
edges := delaunay.GetMst(centers, l.W, l.H, 0)
for _, edge := range edges {
MedianStraight(rng, l, rooms, centers, edge)
}
@ -41,27 +41,5 @@ func DelaunayMstGen(ctx appctx.ClientCtx, l *gamemap.Level) (*gamemap.Level, []g
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)
}

View File

@ -0,0 +1,55 @@
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 DelaunayMstExtGen(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, l.W) //get negative Weights
outlyingCorridors := make([]types.Edge, 0)
outlyingCorrCount := 0
for _, edge := range edges {
MedianStraight(rng, l, rooms, centers, edge)
outlyingCorridors = append(outlyingCorridors, edge)
outlyingCorrCount++
if outlyingCorrCount > 5 {
break
}
}
edges = delaunay.GetMst(centers, l.W, l.H, 0) //get negative Weights
for _, edge := range edges {
MedianStraight(rng, l, rooms, centers, edge)
}
return l, rooms
}

View File

@ -1,6 +1,7 @@
package gamemap
import (
"context"
"encoding/json"
"io/ioutil"
"lab.zaar.be/thefish/alchemyst-go/engine/items"
@ -42,10 +43,10 @@ func LoadPrefabFile(filename string) (*PrefabFile, error) {
}
type PrefabLoader struct {
ctx appctx.ClientCtx
ctx context.Context
}
func NewPrefabLoader(ctx appctx.ClientCtx) PrefabLoader {
func NewPrefabLoader(ctx context.Context) PrefabLoader {
return PrefabLoader{ctx: ctx}
}
@ -105,7 +106,7 @@ func (pfbl PrefabLoader) PrefabRoomsList() []Room {
} else {
f, ok = TileTypeMap[shortName]
if (!ok) {
pfbl.ctx.Logger().Warn().Msgf("Unknown tile: %s", shortName)
appctx.Logger(pfbl.ctx).Warn().Msgf("Unknown tile: %s", shortName)
}
}
room.Geometry[i+ j*room.W] = f

View File

@ -110,6 +110,33 @@ func NewWaterTile() *Tile {
BlocksSight: false,
Explored: false,
MustDraw: true, //fixme debug
Appearance: &Appearance{
Glyph: &PlainGlyphHolder{" "},
ColorSet: TileColorSet{
Fg: &PlainColorHolder{255, 220, 220, 250},
Bg: &DanceColorHolder{
255,
SingleColorRing(5),
FillColorRing(2, 2, 42, 4),
FillColorRing(154, 150, 229, 12),
},
DarkFg: &PlainColorHolder{255, 30, 20, 50},
DarkBg: &PlainColorHolder{255, 7, 7, 30},
},
},
}
}
func NewDeepWaterTile() *Tile {
//ch := &ColorHolder{5, 2, 154}
return &Tile{
Name: "Deep Water",
Description: "Deep water",
BlocksPass: false,
BlocksSight: false,
Explored: false,
MustDraw: true, //fixme debug
Appearance: &Appearance{
Glyph: &PlainGlyphHolder{" "},
@ -125,31 +152,6 @@ func NewWaterTile() *Tile {
DarkBg: &PlainColorHolder{255, 7, 7, 30},
},
},
}
}
func NewDeepWaterTile() *Tile {
//ch := &ColorHolder{5, 2, 154}
return &Tile{
Name: "Deep Water",
Description: "Deep water",
BlocksPass: false,
BlocksSight: false,
Explored: false,
MustDraw: true, //fixme debug
Appearance: &Appearance{
Glyph: &PlainGlyphHolder{" "},
ColorSet: TileColorSet{
Fg: &PlainColorHolder{255, 220, 220, 250},
Bg: &DanceColorHolder{
255,
SingleColorRing(5),
FillColorRing(2, 2, 42, 4),
FillColorRing(154, 150, 229, 12),
},
DarkFg: &PlainColorHolder{255, 30, 20, 50},
DarkBg: &PlainColorHolder{255, 7, 7, 30},
},
},
}
}