From ba15feaaa82b323d25a68cde4cab50ddbb27848b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 25 Mar 2026 23:42:09 -0400 Subject: [PATCH] Can now mostly make a maze map with rooms but the maps aren't fixed yet. --- 05_Bigger/game.go | 2 +- 05_Bigger/maze.go | 2 +- 06_MazeWorld/Makefile | 6 + 06_MazeWorld/combat.go | 45 ++++++ 06_MazeWorld/data.go | 53 +++++++ 06_MazeWorld/debug.go | 33 ++++ 06_MazeWorld/game.go | 100 ++++++++++++ 06_MazeWorld/go.mod | 15 ++ 06_MazeWorld/go.sum | 48 ++++++ 06_MazeWorld/main.go | 26 +++ 06_MazeWorld/map.go | 67 ++++++++ 06_MazeWorld/maze.go | 331 +++++++++++++++++++++++++++++++++++++++ 06_MazeWorld/movement.go | 22 +++ 06_MazeWorld/pathing.go | 94 +++++++++++ 06_MazeWorld/ui.go | 123 +++++++++++++++ 15 files changed, 965 insertions(+), 2 deletions(-) create mode 100644 06_MazeWorld/Makefile create mode 100644 06_MazeWorld/combat.go create mode 100644 06_MazeWorld/data.go create mode 100644 06_MazeWorld/debug.go create mode 100644 06_MazeWorld/game.go create mode 100644 06_MazeWorld/go.mod create mode 100644 06_MazeWorld/go.sum create mode 100644 06_MazeWorld/main.go create mode 100644 06_MazeWorld/map.go create mode 100644 06_MazeWorld/maze.go create mode 100644 06_MazeWorld/movement.go create mode 100644 06_MazeWorld/pathing.go create mode 100644 06_MazeWorld/ui.go diff --git a/05_Bigger/game.go b/05_Bigger/game.go index e519cc5..37dc1a1 100644 --- a/05_Bigger/game.go +++ b/05_Bigger/game.go @@ -75,7 +75,7 @@ func (game *Game) WorldGenerator() { game.NewMap() // then use the dead_ends to create rooms - room_pos := game.AddRooms(dead_ends) + room_pos := game.PlaceRooms(dead_ends) // re-run the maze gen, and it'll work around the rooms dead_ends = game.NewMaze() diff --git a/05_Bigger/maze.go b/05_Bigger/maze.go index b750017..f08ac1a 100644 --- a/05_Bigger/maze.go +++ b/05_Bigger/maze.go @@ -93,7 +93,7 @@ func (game *Game) PunchHole(pos Position, width int, height int) { } } -func (game *Game) AddRooms(dead_ends []Position) []Position { +func (game *Game) PlaceRooms(dead_ends []Position) []Position { for _, room := range dead_ends { size := rand.Int() % 4 diff --git a/06_MazeWorld/Makefile b/06_MazeWorld/Makefile new file mode 100644 index 0000000..255e0bb --- /dev/null +++ b/06_MazeWorld/Makefile @@ -0,0 +1,6 @@ + +build: + go build . + +run: build + ./gorogue diff --git a/06_MazeWorld/combat.go b/06_MazeWorld/combat.go new file mode 100644 index 0000000..dc4b1e4 --- /dev/null +++ b/06_MazeWorld/combat.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "math/rand" +) + +func (game *Game) EnemyDeath() { + is_dead := make([]Position, 0, len(game.Enemies)) + + for pos, enemy := range game.Enemies { + if enemy.HP < 0 { + is_dead = append(is_dead, pos) + } + } + + for _, pos := range is_dead { + delete(game.Enemies, pos) + } +} + +func (game *Game) ApplyDamage(attacker *Thing, defender *Thing) { + damage := rand.Int() % attacker.Damage + defender.HP -= damage + if damage == 0 { + game.SetStatus("MISSED!") + } else if defender.HP > 0 { + game.SetStatus(fmt.Sprintf("HIT %d damage", damage)) + } else { + game.SetStatus("DEAD!") + } +} + +func (game *Game) Attack(target Position) { + enemy, hit_enemy := game.Enemies[target] + loot, hit_loot := game.Loot[target] + + if hit_enemy { + game.ApplyDamage(&game.Player, enemy) + game.ApplyDamage(enemy, &game.Player) + } else if hit_loot { + game.Player.HP += loot.Healing + delete(game.Loot, target) + } +} diff --git a/06_MazeWorld/data.go b/06_MazeWorld/data.go new file mode 100644 index 0000000..5149e69 --- /dev/null +++ b/06_MazeWorld/data.go @@ -0,0 +1,53 @@ +package main + +import ( + "github.com/gdamore/tcell/v2" +) + +const ( + WALL = '▒' + SPACE = ' ' + PATH_LIMIT = 1000 + RENDER = true + SHOW_RENDER = false + SHOW_PATHS = true + HEARING_DISTANCE = 6 + ROOM_SIZE=4 +) + +type Map [][]rune +type Paths [][]int + +type Position struct { + X int + Y int +} + +type Thing struct { + HP int + Pos Position + Damage int + Healing int +} + +type Room struct { + X int + Y int + Width int + Height int +} + +type Game struct { + Screen tcell.Screen + Level Map + Paths Paths + Player Thing + Status string + Width int + Height int + DeadArea Room + Enemies map[Position]*Thing + Loot map[Position]*Thing + Rooms []Room + DeadEnds []Position +} diff --git a/06_MazeWorld/debug.go b/06_MazeWorld/debug.go new file mode 100644 index 0000000..1be0a2f --- /dev/null +++ b/06_MazeWorld/debug.go @@ -0,0 +1,33 @@ +package main + +import ( + "log" + "os" + "fmt" + "github.com/gdamore/tcell/v2" +) + +var dbg *log.Logger + +func DebugInit() { + out, err := os.Create("debug.log") + if err != nil { log.Fatal(err) } + dbg = log.New(out, "", log.LstdFlags) +} + +func (game *Game) DrawPaths() { + for y, row := range game.Paths { + for x, path_num := range row { + if path_num == PATH_LIMIT { continue } + + as_str := fmt.Sprintf("%x", path_num % 16) + style := tcell.StyleDefault.Foreground(tcell.ColorGray) + + if path_num >= 0 && path_num <= 16 { + style = style.Reverse(true) + } + + game.Screen.SetContent(x, y, rune(as_str[0]), nil, style) + } + } +} diff --git a/06_MazeWorld/game.go b/06_MazeWorld/game.go new file mode 100644 index 0000000..4912662 --- /dev/null +++ b/06_MazeWorld/game.go @@ -0,0 +1,100 @@ +package main + +import ( + "log" + "os" + "math/rand" + "fmt" +) + +func NewGame(width int, height int) (*Game) { + var game Game + + if width % 2 == 0 { log.Fatal("NewGame: width must be odd") } + if height % 2 == 0 { + log.Fatal("NewGame: height must be odd") + } + + game.Width = width + game.Height = height + game.Enemies = make(map[Position]*Thing) + game.Loot = make(map[Position]*Thing) + game.Level = make(Map, height, height) + game.Paths = make(Paths, height, height) + game.Player = Thing{20, Position{1,1}, 4, 0} + + return &game +} + +func (game *Game) Exit() { + if RENDER { + game.Screen.Fini() + } + + os.Exit(0) +} + +func (game *Game) PlaceEnemies() { + for _, pos := range game.DeadEnds { + if rand.Int() % 2 == 0 { + enemy := new(Thing) + enemy.HP = 10 + enemy.Pos = pos + enemy.Damage = 4 + game.Enemies[pos] = enemy + } + } +} + +func (game *Game) PlaceLoot() { + for _, pos := range game.DeadEnds { + if !game.Occupied(pos) { + loot := new(Thing) + loot.Healing = 10 + game.Loot[pos] = loot + } + } +} + +func (game *Game) Restart() { + game.SetStatus("YOU DIED! Try again.") + game.Player.HP = 20 + game.Player.Pos = Position{1,1} + clear(game.Enemies) + + game.FillPaths(game.Paths, PATH_LIMIT) + game.NewMap() + game.Render() +} + +func (game *Game) WorldGenerator() { + // clear the map to all '#' + game.NewMap() + + // first generate a maze to get dead ends + game.NewMaze() + + // this clears the previous map + game.NewMap() + + game.SetDeadArea(3) + + // create random rooms + game.RandomizeRooms(ROOM_SIZE) + + // then use the dead_ends to create rooms + game.PlaceRooms() + + // re-run the maze gen, and it'll work around the rooms + game.NewMaze() + + // validate the map + if !game.Validate() { + fmt.Println("BAD MAP") + os.Exit(1) + } + + // finally place enemies inside the rooms + game.PlaceEnemies() + game.PlaceLoot() +} diff --git a/06_MazeWorld/go.mod b/06_MazeWorld/go.mod new file mode 100644 index 0000000..b3700d6 --- /dev/null +++ b/06_MazeWorld/go.mod @@ -0,0 +1,15 @@ +module MY/gorogue + +go 1.25.1 + +require github.com/gdamore/tcell/v2 v2.9.0 + +require ( + github.com/gdamore/encoding v1.0.1 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + golang.org/x/sys v0.35.0 // indirect + golang.org/x/term v0.34.0 // indirect + golang.org/x/text v0.28.0 // indirect +) diff --git a/06_MazeWorld/go.sum b/06_MazeWorld/go.sum new file mode 100644 index 0000000..26d9b14 --- /dev/null +++ b/06_MazeWorld/go.sum @@ -0,0 +1,48 @@ +github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw= +github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo= +github.com/gdamore/tcell/v2 v2.9.0 h1:N6t+eqK7/xwtRPwxzs1PXeRWnm0H9l02CrgJ7DLn1ys= +github.com/gdamore/tcell/v2 v2.9.0/go.mod h1:8/ZoqM9rxzYphT9tH/9LnunhV9oPBqwS8WHGYm5nrmo= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= +golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.34.0 h1:O/2T7POpk0ZZ7MAzMeWFSg6S5IpWd/RXDlM9hgM3DR4= +golang.org/x/term v0.34.0/go.mod h1:5jC53AEywhIVebHgPVeg0mj8OD3VO9OzclacVrqpaAw= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= +golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/06_MazeWorld/main.go b/06_MazeWorld/main.go new file mode 100644 index 0000000..ac8cae2 --- /dev/null +++ b/06_MazeWorld/main.go @@ -0,0 +1,26 @@ +package main + +func main() { + DebugInit() + + game := NewGame(41, 41) + game.InitScreen() + + for { + game.WorldGenerator() + game.Render() + + for game.HandleEvents() && game.Player.HP > 0 { + game.EnemyDeath() + game.ComputePaths(game.Player.Pos.X, game.Player.Pos.Y) + game.EnemyPathing() + game.Render() + } + + if game.Player.HP <= 0 { + game.Restart() + } else { + game.Exit() + } + } +} diff --git a/06_MazeWorld/map.go b/06_MazeWorld/map.go new file mode 100644 index 0000000..1def989 --- /dev/null +++ b/06_MazeWorld/map.go @@ -0,0 +1,67 @@ +package main + +import ( + "slices" +) + +func Compass(near Position, offset int) []Position { + return []Position{ + Position{near.X, near.Y - offset}, + Position{near.X, near.Y + offset}, + Position{near.X + offset, near.Y}, + Position{near.X - offset, near.Y}, + } +} + +func (game *Game) CloneMap() Map { + // this is a shallow copy though + new_map := slices.Clone(game.Level) + + for i, row := range new_map { + // this makes sure the row is an actual copy + new_map[i] = slices.Clone(row) + } + + return new_map +} + +func (game *Game) Inbounds(pos Position, offset int) bool { + return pos.X >= offset && + pos.X < game.Width - offset && + pos.Y >= offset && + pos.Y < game.Height - offset +} + +func (game *Game) Occupied(pos Position) bool { + _, is_enemy := game.Enemies[pos] + _, is_loot := game.Loot[pos] + is_player := pos == game.Player.Pos + + // Inbounds comes first to prevent accessing level with bad x,y + return !game.Inbounds(pos, 1) || + game.Level[pos.Y][pos.X] == WALL || + is_enemy || is_loot || is_player +} + +func (game *Game) FillMap(target Map, setting rune) { + for y := 0 ; y < game.Height; y++ { + target[y] = slices.Repeat([]rune{setting}, game.Width) + } +} + +func (game *Game) Neighbors(near Position) []Position { + result := make([]Position, 0, 4) + points := Compass(near, 2) + + for _, pos := range points { + if game.Inbounds(pos, 0) { + result = append(result, pos) + } + } + + return result +} + +func (game *Game) NewMap() { + game.FillMap(game.Level, WALL) +} diff --git a/06_MazeWorld/maze.go b/06_MazeWorld/maze.go new file mode 100644 index 0000000..f4979f9 --- /dev/null +++ b/06_MazeWorld/maze.go @@ -0,0 +1,331 @@ +package main + +import ( + "math/rand" + "time" + "log" + "fmt" +) + +func (room *Room) Overlaps(other Room) bool { + // other left > this right == other too far right + return !( other.X > room.X + room.Width || + + // other right < this left == other too far left + other.X + other.Width < room.X || + + // other top > this bottom == too far below + other.Y > room.Y + room.Height || + + // other bottom < this top == too far above + other.Y + other.Height < room.Y) +} + +func (room *Room) Contains(at Position) bool { + return at.X >= room.X && + at.X <= room.X + room.Width -1 && + at.Y >= room.Y && + at.Y <= room.Y + room.Height - 1 +} + + +func (game *Game) NeighborWalls(pos Position) []Position { + neighbors := game.Neighbors(pos) + result := make([]Position, 0) + + for _, at := range neighbors { + cell := game.Level[at.Y][at.X] + + if cell == WALL { + result = append(result, at) + } + } + + return result +} + +func (game *Game) HuntNext(on *Position, found *Position) bool { + for y := 1; y < game.Height ; y += 2 { + for x := 1; x < game.Width ; x += 2 { + if game.Level[y][x] != WALL { + continue + } + + neighbors := game.Neighbors(Position{x, y}) + + for _, pos := range neighbors { + if game.Level[pos.Y][pos.X] == SPACE { + *on = Position{x, y} + *found = pos + return true + } + } + } + } + + return false +} + +func (game *Game) HAKStep(from Position, to Position) { + game.Level[from.Y][from.X] = SPACE + row := (from.Y + to.Y) / 2 + col := (from.X + to.X) / 2 + game.Level[row][col] = SPACE +} + +func (game *Game) NewMaze() []Position { + on := Position{1, 1} + found := Position{1,1} + + game.DeadEnds = make([]Position, 0) + game.Rooms = make([]Room, 0) + + for { + neighbors := game.NeighborWalls(on) + + if len(neighbors) == 0 { + game.DeadEnds = append(game.DeadEnds, on) + + if !game.HuntNext(&on, &found) { + break + } + + game.HAKStep(on, found) + } else { + rand_neighbor := rand.Int() % len(neighbors) + nb := neighbors[rand_neighbor] + game.HAKStep(nb, on) + on = nb + } + + if SHOW_RENDER { + game.Render() + time.Sleep(50 * time.Millisecond) + } + } + + return game.DeadEnds +} + +func (game *Game) RoomShouldExist(room Room) bool { + // 1 offset means it won't be on the perimeter + top_left := Position{room.X - 1, room.Y - 1} + bottom_right := Position{room.X + room.Width + 2, room.Y + room.Height + 2} + + // range of 2 so it's not at the perimeter + if !game.Inbounds(top_left, 2) || + !game.Inbounds(bottom_right, 2) { + return false + } + + // if it overlaps a game.NoRoomRegion then false + if(room.Overlaps(game.DeadArea)) { + return false + } + + // if this room overlaps any other room then false + for _, other := range game.Rooms { + if room == other { + continue + } + + if room.Overlaps(other) { + return false + } + } + + return true; +} + +func room_sweetspot(width int, room_size int) int { + if width < 20 { + return 4 + } else if width < 30 { + return width / room_size / 2 + } else if width < 50 { + return width / room_size + } else { + return width / 2 + } +} + +func (game *Game) RandomizeRooms(room_size int) { + // need to put dead ends in game or a map struct + rand.Shuffle(len(game.DeadEnds), func(i, j int) { + game.DeadEnds[i], game.DeadEnds[j] = game.DeadEnds[j], game.DeadEnds[i] + }) + + // shuffle the dead ends + max_rooms := room_sweetspot(game.Width, room_size) + fmt.Println("max_rooms", max_rooms) + + for _, at := range game.DeadEnds { + // avoid dead ends near edge + if !game.Inbounds(at, 2) { continue } + + // quit after max_rooms + if len(game.Rooms) > max_rooms { + break + } + + // get the room corners + corners := []Position{ + Position{at.X, at.Y}, // top left + Position{at.X - room_size + 1, at.Y}, // top right + Position{at.X - room_size + 1, at.Y - room_size + 1}, // bottom right + Position{at.X, at.Y - room_size + 1}, // bottom left + } + + // randomly select starting corner + rand.Shuffle(len(corners), func(i, j int) { + corners[i], corners[j] = corners[j], corners[i] + }) + + // for each possible starting corner + for _, corner := range corners { + room := Room{corner.X, corner.Y, room_size, room_size} + // if the room is good/should exist + + if game.RoomShouldExist(room) { + // add it to the room list + game.Rooms = append(game.Rooms, room) + } + } + } +} + +func (game *Game) Clear() { + game.FillMap(game.Level, WALL); +} + + +func (game *Game) PunchHole(pos Position, width int, height int) { + for y := pos.Y; y < pos.Y + height; y++ { + for x := pos.X; x < pos.X + width; x++ { + game.Level[y][x] = SPACE + } + } +} + +func (game *Game) PlaceRooms() { + // switch from using game.DeadEnds to using the rooms list + for _, room := range game.Rooms { + game.PunchHole(Position{room.X, room.Y}, room.Width, room.Height) + if SHOW_RENDER { game.Render() } + } +} + +func (game *Game) RemoveDeadEnds() { + // for each point at dead end + // compass to find open space + // remove the opposite wall from the space +} + +func (game *Game) Enclose() { + // might need the perimeter from shapes + // basically just go around the perimeter and fill in with walls +} + +func (game *Game) Repair() { + +} + +func (game *Game) ValidDoor(x int, y int) { + // door is valid if north/south xor east/west is open +} + +func (game *Game) PlaceDoors() { + for _, room := range game.Rooms { + fmt.Println(room) + // go through all the rooms, punch out doors, find longest path + + // computer longest path from room as a test + + // can't path out so make a hole + // walk the perimeter + // if it's a wall and it's a valid door location + // make it a door + // recomputer the longest path + // if it's greater than the best_longest + // record as best door and best_longest + // if best_longest is good then done + + // should now have a door with the longest path + } +} + +func (game *Game) Validate() bool { + // walk the perimeter and + // if a dead end is there then not valid + // if a door is there not valid + // if any part is not a wall, not valid + + // if we have multiple rooms then confirm none overlap + if len(game.Rooms) > 1 { + for _, room := range game.Rooms { + if !game.RoomShouldExist(room) { + return false + } + } + } + + var from Position + + // pick a random room + if len(game.Rooms) > 0 { + room := game.Rooms[0] + from = Position{room.X, room.Y} + } else if len(game.DeadEnds) > 0 { + from = game.DeadEnds[0] + } else { + log.Println("Invalid map, doesn't have rooms or dead ends.") + return false + } + + // compute paths to the room + game.ComputePaths(from.X, from.Y) + + // search the map for a space value that's also a path limit in Paths + for y := 0; y < game.Height; y++ { + for x := 0; x < game.Width; x++ { + if game.Level[y][x] == SPACE && + game.Paths[y][x] == PATH_LIMIT { + // that means the map is not pathable, not valid + return false + } + } + } + + // all good valid + return true +} + +func (game *Game) SetDeadArea(size int) { + x := game.Width / 2; + y := game.Height / 2; + + game.DeadArea = Room{ + x - size, + y - size, + size * 2 + 1, + size * 2 + 1, + } + + fmt.Println("room", game.DeadArea) + r := game.DeadArea + game.PunchHole(Position{r.X, r.Y}, r.Width, r.Height) +} + +////////// SCRIPT and SHAPES + +func (game *Game) OpenBox(outer_size int) { + // get the center x/y + // compensate for the box's border + outer_size++ + + // get the x,y/width,height for the outerbox + // walk the perimeter + // running compass around it + // if there's a dead end then punch that out and break +} + diff --git a/06_MazeWorld/movement.go b/06_MazeWorld/movement.go new file mode 100644 index 0000000..cd4ebd6 --- /dev/null +++ b/06_MazeWorld/movement.go @@ -0,0 +1,22 @@ +package main + +func (game *Game) MovePlayer(x_delta int, y_delta int) { + target := Position{ + game.Player.Pos.X + x_delta, + game.Player.Pos.Y + y_delta, + } + + if game.Occupied(target) { + game.Attack(target) + } else { + game.Player.Pos = target + } +} + +func (game *Game) MoveEnemy(from Position, to Position) { + enemy, ok := game.Enemies[from] + if !ok { dbg.Fatal("no enemy at", from, "wtf") } + + delete(game.Enemies, from) + game.Enemies[to] = enemy +} diff --git a/06_MazeWorld/pathing.go b/06_MazeWorld/pathing.go new file mode 100644 index 0000000..6095628 --- /dev/null +++ b/06_MazeWorld/pathing.go @@ -0,0 +1,94 @@ +package main + +import ( + "slices" +) + +func (game *Game) FillPaths(target Paths, setting int) { + for y := 0 ; y < game.Height; y++ { + target[y] = slices.Repeat([]int{setting}, game.Width) + } +} + + +func (game *Game) PathAddNeighbors(neighbors []Position, closed Map, near Position) []Position { + points := Compass(near, 1) + + for _, pos := range points { + if game.Inbounds(pos, 0) && closed[pos.Y][pos.X] == SPACE { + closed[pos.Y][pos.X] = WALL + neighbors = append(neighbors, pos) + } + } + + return neighbors +} + +func (game *Game) ComputePaths(x int, y int) { + in_grid := make([][]int, game.Height, game.Height) + game.FillPaths(in_grid, 1) + in_grid[y][x] = 0 + + game.FillPaths(game.Paths, PATH_LIMIT) + closed := game.CloneMap() + starting_pixels := make([]Position, 0, 10) + open_pixels := make([]Position, 0, 10) + + counter := 0 + + for counter < game.Height * game.Width { + x := counter % game.Width + y := counter / game.Width + + if in_grid[y][x] == 0 { + game.Paths[y][x] = 0 + closed[y][x] = WALL + starting_pixels = append(starting_pixels, Position{x, y}) + } + + counter += 1 + } + + for _, pos := range starting_pixels { + open_pixels = game.PathAddNeighbors(open_pixels, closed, pos) + } + + counter = 1 + for counter < PATH_LIMIT && len(open_pixels) > 0 { + next_open := make([]Position, 0, 10) + for _, pos := range open_pixels { + game.Paths[pos.Y][pos.X] = counter + next_open = game.PathAddNeighbors(next_open, closed, pos) + } + open_pixels = next_open + counter += 1 + } + + for _, pos := range open_pixels { + game.Paths[pos.Y][pos.X] = counter + } +} + +func (game *Game) EnemyPathing() { + for enemy_at, _ := range game.Enemies { + // get the four directions + dirs := Compass(enemy_at, 1) + + // sort by closest path number + slices.SortFunc(dirs, func(a Position, b Position) int { + return game.Paths[a.Y][a.X] - game.Paths[b.Y][b.X] + }) + + // 0 dir is now the best direction + move_to := dirs[0] + + // can we hear the player? occupied? + can_hear := game.Paths[move_to.Y][move_to.X] < HEARING_DISTANCE + occupied := game.Occupied(move_to) + + if can_hear && !occupied { + // move the enemy in the best direction + game.MoveEnemy(enemy_at, move_to) + } + } +} diff --git a/06_MazeWorld/ui.go b/06_MazeWorld/ui.go new file mode 100644 index 0000000..2015db3 --- /dev/null +++ b/06_MazeWorld/ui.go @@ -0,0 +1,123 @@ +package main + +import ( + "fmt" + "github.com/gdamore/tcell/v2" + "github.com/gdamore/tcell/v2/encoding" +) + +//// DRAWING + +func (game *Game) DrawText(x int, y int, text string) { + for i, cell := range text { + game.Screen.SetContent(x+i, y, cell, nil, tcell.StyleDefault) + } +} + +func (game *Game) DrawStatus() { + game.DrawText(0, game.Height, game.Status) + + hp := fmt.Sprintf("W: %d, HP: %d", game.Width, game.Player.HP) + + game.DrawText(game.Width - len(hp), game.Height, hp) +} + +func (game *Game) SetStatus(msg string) { + game.Status = msg +} + +func (game *Game) DrawEntity(symbol rune, pos Position, color tcell.Color) { + style := tcell.StyleDefault.Bold(true).Foreground(color) + game.Screen.SetContent(pos.X, pos.Y, symbol, nil, style) +} + +func (game *Game) DrawMap() { + gray := tcell.StyleDefault.Foreground(tcell.ColorGray) + + for y, line := range game.Level { + for x, cell := range line { + if cell == SPACE { + game.Screen.SetContent(x, y, cell, nil, gray) + } else { + game.Screen.SetContent(x, y, cell, nil, tcell.StyleDefault) + } + } + } +} + +///// RENDERING + +func (game *Game) InitScreen() { + if !RENDER {return } + + var err error + encoding.Register() + + game.Screen, err = tcell.NewScreen() + + if err != nil { dbg.Fatal(err) } + + err = game.Screen.Init() + if err != nil { dbg.Fatal(err) } +} + +func (game *Game) Render() { + if !RENDER { return } + + game.Screen.Clear() + game.DrawMap() + + if SHOW_PATHS { + game.DrawPaths() + } + + game.DrawEntity('@', game.Player.Pos, tcell.ColorYellow) + + for pos, _ := range game.Enemies { + game.DrawEntity('G', pos, tcell.ColorRed) + } + + for pos, _ := range game.Loot { + game.DrawEntity('!', pos, tcell.ColorGreen) + } + + game.DrawStatus() + game.Screen.Show() +} + +//// EVENTS + +func (game *Game) HandleKeys(ev *tcell.EventKey) bool { + switch ev.Key() { + case tcell.KeyEscape: + return false + case tcell.KeyUp: + game.MovePlayer(0, -1) + case tcell.KeyDown: + game.MovePlayer(0, 1) + case tcell.KeyRight: + game.MovePlayer(1, 0) + case tcell.KeyLeft: + game.MovePlayer(-1, 0) + } + + switch ev.Rune() { + case 'q': + return false + } + + return true +} + +func (game *Game) HandleEvents() bool { + if !RENDER { return false } + + switch ev := game.Screen.PollEvent().(type) { + case *tcell.EventResize: + game.Screen.Sync() + case *tcell.EventKey: + return game.HandleKeys(ev) + } + + return true +}