From 0f367eaf96f2da38e85a2fbe155c0aed934ecab5 Mon Sep 17 00:00:00 2001 From: "anton.gurov" Date: Thu, 14 Nov 2019 18:53:41 +0300 Subject: [PATCH] debugged delaunay, problem was in referencing slice of connectors instead of copying --- .../mapgens/delaunaymst/delaunay_mst.go | 45 ++++++++++++++++--- engine/gamemap/prefab.go | 3 ++ engine/gamemap/room.go | 31 ++++++++++--- 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/engine/gamemap/mapgens/delaunaymst/delaunay_mst.go b/engine/gamemap/mapgens/delaunaymst/delaunay_mst.go index 721ad8e..530d72e 100644 --- a/engine/gamemap/mapgens/delaunaymst/delaunay_mst.go +++ b/engine/gamemap/mapgens/delaunaymst/delaunay_mst.go @@ -11,8 +11,8 @@ import ( //fixme move to config var minRoomSize = 5 -var maxRoomSize = 15 -var maxrooms = 59 +var maxRoomSize = 25 +var maxrooms = 200 var fges = map[int]types.RectFill{ 1: types.RectFill{ @@ -58,12 +58,24 @@ func DelaunayMstGen(ctx appctx.ClientCtx, l *gamemap.Level) (*gamemap.Level, []g failed := false var fillage types.RectFill fillage = fges[rng.GetWeightedEntity(map[int]int{1: 10, 2: 1})] - var newRoom gamemap.Room + var newRoom = gamemap.Room{} if !prefabUsed || rng.Range(0, 5) > 3 { + //if prefabUsed { //prefab prefabUsed = true + fmt.Printf("\n\n------USING PREFAB-----\n") r := pfRooms[rng.Range(0, len(pfRooms))] //copy to local scope - newRoom = r + newRoom = gamemap.Room{ + Rect: r.Rect, + Center: r.Center, + Geometry: r.Geometry, + Items: r.Items, + Mobs: r.Mobs, + Connectors: make([]types.Coords,0), + } + for _, coord := range r.Connectors { + newRoom.Connectors = append(newRoom.Connectors, coord) + } } else { newRoom = gamemap.NewRandomRectRoom( rng, @@ -104,8 +116,9 @@ func DelaunayMstGen(ctx appctx.ClientCtx, l *gamemap.Level) (*gamemap.Level, []g for _, room := range rooms { centers = append(centers, room.Center) } - - for _, edge := range delaunay.GetMst(centers, l.W, l.H) { + edges := delaunay.GetMst(centers, l.W, l.H) + fmt.Printf("edges: ", edges) + for _, edge := range edges { MedianStraight(rng, l, rooms, centers, edge) } @@ -132,6 +145,26 @@ func MedianStraight(rng *util.RNG, l *gamemap.Level, rooms []gamemap.Room, cente midpoint := edge.Midpoint() fromConnector := findNearestCoonector(midpoint, fromRoom) toConnector := findNearestCoonector(midpoint, toRoom) + if (!l.InBounds(midpoint)) { + //fmt.Printf("rooms: ", rooms) + fmt.Printf("\nedges: ", edge) + fmt.Printf("\nmidpoint: ", midpoint) + panic(fmt.Errorf("midpoint out of level bounds!")) + } + if (!l.InBounds(fromConnector)) { + fmt.Printf("\nfrom room: ", fromRoom.String()) + fmt.Printf("\nedges: ", edge) + fmt.Printf("\nfromConnector: ", fromConnector) + panic(fmt.Errorf("fromConnector out of level bounds!")) + } + + if (!l.InBounds(toConnector)) { + fmt.Printf("\nto room: ", toRoom.String()) + fmt.Printf("\nedges: ", edge) + fmt.Printf("\ntoConnector: ", toConnector) + panic(fmt.Errorf("toConnector out of level bounds!")) + } + connectStraight(rng, l, fromConnector, toConnector, midpoint) } diff --git a/engine/gamemap/prefab.go b/engine/gamemap/prefab.go index 87224cb..834ebe3 100644 --- a/engine/gamemap/prefab.go +++ b/engine/gamemap/prefab.go @@ -2,6 +2,7 @@ package gamemap import ( "encoding/json" + "fmt" "io/ioutil" "lab.zaar.be/thefish/alchemyst-go/engine/items" "lab.zaar.be/thefish/alchemyst-go/engine/mob" @@ -112,6 +113,8 @@ func (pfbl PrefabLoader) PrefabRoomsList() []Room { } } //add room to list + fmt.Printf("PREFAB room append: ", room.String()) + rooms = append(rooms, room) } return rooms diff --git a/engine/gamemap/room.go b/engine/gamemap/room.go index fd92045..e1ab245 100644 --- a/engine/gamemap/room.go +++ b/engine/gamemap/room.go @@ -7,6 +7,7 @@ import ( "lab.zaar.be/thefish/alchemyst-go/engine/mob" "lab.zaar.be/thefish/alchemyst-go/engine/types" "lab.zaar.be/thefish/alchemyst-go/util" + "strings" ) var invalidBlit = errors.New("trying to blit on existing good tile") @@ -59,17 +60,24 @@ func (room *Room) BlitToLevel(l *Level) error { } func (room *Room) MoveToCoords(where types.Coords) *Room { + fmt.Printf("\n MOVING room :", room.String()) + fmt.Printf("\nmovin room to %v", where) //update room coords? room.X = where.X room.Y = where.Y //update centers! - room.Center.X += where.X - room.Center.Y += where.Y + room.Center.X = room.Center.X + where.X + room.Center.Y = room.Center.Y + where.Y //update connector? - for i, _ := range room.Connectors { - room.Connectors[i].X += where.X - room.Connectors[i].Y += where.Y + for i, coords := range room.Connectors { + fmt.Printf("\nupdating coords ", coords) + coords.X = coords.X + where.X + coords.Y = coords.Y + where.Y + fmt.Printf("\nupdated coords ", coords) + room.Connectors[i] = coords } + + fmt.Printf("\nROOM MOVED to %v \n\n", room) return room } @@ -90,8 +98,17 @@ func NewRandomRectRoom(rng *util.RNG, w, h int, fillage types.RectFill) Room { newRoom.Connectors, types.Coords{rng.Range(1, w - 2), 0}, types.Coords{rng.Range(1, w - 2), h -1}, - types.Coords{0, rng.Range(1, h - 2)}, - types.Coords{w - 1, rng.Range(1, h - 2)}, + types.Coords{0, rng.Range(1, h - 1)}, + types.Coords{w - 1, rng.Range(1, h - 1)}, ) return newRoom } + +func (r *Room) String() string { + return strings.Join([]string{ + "room: ", + "\t" + fmt.Sprintf(" rect: X: %d, Y: %d, maxX: %d, maxY: %d", r.Rect.X, r.Rect.Y, r.Rect.W + r.X - 1, r.Rect.H + r.Y - 1), + "\t" + fmt.Sprintf(" center:", r.Center.X, r.Center.Y), + "\t" + fmt.Sprintf(" Connectors: %v", r.Connectors), + },"\n") + "\n" +} \ No newline at end of file