diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dd91bb7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# ---> Vim +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +backup +*.exe +*.dll +*.world +coverage +coverage/* +.venv +*.gz +config.toml +public +*.idx +*.sqlite3 +gorogue +*.log diff --git a/01_the_screen/Makefile b/01_the_screen/Makefile new file mode 100644 index 0000000..faf6c36 --- /dev/null +++ b/01_the_screen/Makefile @@ -0,0 +1,6 @@ + +build: + go build . + +run: + ./gorogue diff --git a/01_the_screen/go.mod b/01_the_screen/go.mod new file mode 100644 index 0000000..add6d2e --- /dev/null +++ b/01_the_screen/go.mod @@ -0,0 +1,17 @@ +module MY/gorogue + +go 1.25.1 + +require ( + github.com/gdamore/tcell/v2 v2.9.0 + github.com/mattn/go-runewidth v0.0.16 +) + +require ( + github.com/gdamore/encoding v1.0.1 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // 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/01_the_screen/go.sum b/01_the_screen/go.sum new file mode 100644 index 0000000..26d9b14 --- /dev/null +++ b/01_the_screen/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/01_the_screen/main.go b/01_the_screen/main.go new file mode 100644 index 0000000..c823cdd --- /dev/null +++ b/01_the_screen/main.go @@ -0,0 +1,206 @@ + +package main + +import ( + "os" + "log" + "fmt" + "github.com/gdamore/tcell/v2" + "github.com/gdamore/tcell/v2/encoding" +) + +var dbg *log.Logger + +const ( + WALL = '#' + SPACE = '.' + RENDER = true +) + +type Map [][]rune +type Paths [][]int + +type Position struct { + x int + y int +} + +type Enemy struct { + hp int + pos Position + damage int +} + +type Game struct { + screen tcell.Screen + level Map + player Enemy + status string + width int + height int +} + +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("HP: %d", game.player.hp) + + game.DrawText(game.width - len(hp), game.height, hp) +} + +func (game *Game) Status(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) + } + } + } +} + +func (game *Game) Render() { + if !RENDER { return } + + game.screen.Clear() + + game.DrawMap() + + game.DrawEntity('@', game.player.pos, tcell.ColorYellow) + + game.DrawStatus() + + game.screen.Show() +} + +func (game *Game) Exit() { + if RENDER { + game.screen.Fini() + } + + os.Exit(0) +} + +func (game *Game) Occupied(x int, y int) bool { + return game.level[y][x] != SPACE +} + +func (game *Game) NewMap() { + game.level = Map{ + []rune("#################"), + []rune("#.#...#.........#"), + []rune("#.#.###.#.###.#.#"), + []rune("#.#.....#...#.#.#"), + []rune("#.#.#######.#.###"), + []rune("#.#...#...#.#...#"), + []rune("#.###.###...###.#"), + []rune("#...#.......#...#"), + []rune("#.#.#########...#"), + []rune("#.#.............#"), + []rune("#################"), + } +} + +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.x, target.y) { + game.player.pos = target + } +} + +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 +} + +func (game *Game) InitScreen() { + encoding.Register() + + var err error + game.screen, err = tcell.NewScreen() + if err != nil { log.Fatal(err) } + + err = game.screen.Init() + if err != nil { log.Fatal(err) } +} + +func NewGame(width int, height int) (*Game) { + var game Game + + game.width = width + game.height = height + game.level = make(Map, height, height) + game.player = Enemy{20, Position{1,1}, 4} + + return &game +} + +func main() { + out, err := os.Create("debug.log") + if err != nil { log.Fatal(err) } + dbg = log.New(out, "", log.LstdFlags) + + game := NewGame(17, 11) + err := game.InitScreen() + + game.NewMap() + game.Render() + + for game.HandleEvents() { + game.Render() + } + + game.Exit() +} diff --git a/02_mazes_and_enemies/Makefile b/02_mazes_and_enemies/Makefile new file mode 100644 index 0000000..faf6c36 --- /dev/null +++ b/02_mazes_and_enemies/Makefile @@ -0,0 +1,6 @@ + +build: + go build . + +run: + ./gorogue diff --git a/02_mazes_and_enemies/go.mod b/02_mazes_and_enemies/go.mod new file mode 100644 index 0000000..add6d2e --- /dev/null +++ b/02_mazes_and_enemies/go.mod @@ -0,0 +1,17 @@ +module MY/gorogue + +go 1.25.1 + +require ( + github.com/gdamore/tcell/v2 v2.9.0 + github.com/mattn/go-runewidth v0.0.16 +) + +require ( + github.com/gdamore/encoding v1.0.1 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // 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/02_mazes_and_enemies/go.sum b/02_mazes_and_enemies/go.sum new file mode 100644 index 0000000..26d9b14 --- /dev/null +++ b/02_mazes_and_enemies/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/02_mazes_and_enemies/main.go b/02_mazes_and_enemies/main.go new file mode 100644 index 0000000..95477b9 --- /dev/null +++ b/02_mazes_and_enemies/main.go @@ -0,0 +1,378 @@ + +package main + +import ( + "os" + "slices" + "log" + "fmt" + "math/rand" + "time" + "github.com/gdamore/tcell/v2" + "github.com/gdamore/tcell/v2/encoding" +) + +var dbg *log.Logger + +const ( + WALL = '#' + SPACE = '.' + PATH_LIMIT = 1000 + RENDER = true + SHOW_RENDER = false +) + +type Map [][]rune +type Paths [][]int + +type Position struct { + x int + y int +} + +type Enemy struct { + hp int + pos Position + damage int +} + +type Game struct { + screen tcell.Screen + level Map + player Enemy + status string + width int + height int + enemies map[Position]*Enemy +} + +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("HP: %d", game.player.hp) + + game.DrawText(game.width - len(hp), game.height, hp) +} + +func (game *Game) Status(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) + } + } + } +} + +func (game *Game) Render() { + if !RENDER { return } + + game.screen.Clear() + + game.DrawMap() + + game.DrawEntity('@', game.player.pos, tcell.ColorYellow) + + for pos, _ := range game.enemies { + game.DrawEntity('G', pos, tcell.ColorRed) + } + + game.DrawStatus() + + game.screen.Show() +} + +func (game *Game) Exit() { + if RENDER { + game.screen.Fini() + } + + os.Exit(0) +} + +func (game *Game) Occupied(x int, y int) bool { + pos := Position{x, y} + _, is_enemy := game.enemies[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[y][x] == WALL || + is_enemy || + is_player +} + +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.x, target.y) { + game.player.pos = target + } +} + +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 +} + +func (game *Game) InitScreen() { + encoding.Register() + + var err error + game.screen, err = tcell.NewScreen() + if err != nil { log.Fatal(err) } + + err = game.screen.Init() + if err != nil { log.Fatal(err) } +} + +func NewGame(width int, height int) (*Game) { + var game Game + + game.width = width + game.height = height + game.enemies = make(map[Position]*Enemy) + + game.level = make(Map, height, height) + + game.player = Enemy{20, Position{1,1}, 4} + + return &game +} + +func compass(x int, y int, offset int) []Position { + return []Position{ + Position{x, y - offset}, + Position{x, y + offset}, + Position{x + offset, y}, + Position{ x - offset, y}, + } +} + +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) Neighbors(near Position) []Position { + result := make([]Position, 0, 4) + points := compass(near.x, near.y, 2) + + for _, pos := range points { + if game.Inbounds(pos, 0) { + result = append(result, pos) + } + } + + return result +} + +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) FindCoord(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) HuntAndKill() []Position { + on := Position{1, 1} + found := Position{1,1} + + dead_ends := make([]Position, 0) + + for { + neighbors := game.NeighborWalls(on) + + if len(neighbors) == 0 { + dead_ends = append(dead_ends, on) + + if !game.FindCoord(&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 dead_ends +} + +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) CarveRoom(pos Position, size int) { + // only use ones far enough inside + for y := pos.y - size; y < pos.y + size; y++ { + for x := pos.x - size; x < pos.x + size; x++ { + if game.Inbounds(Position{x, y}, 1) { + game.level[y][x] = SPACE + } + } + } +} + +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) AddRooms(dead_ends []Position, size int) { + rand.Shuffle(len(dead_ends), func(i, j int) { + dead_ends[i], dead_ends[j] = dead_ends[j], dead_ends[i] + }) + + for _, pos := range dead_ends[0:4] { + rs := rand.Int() % size + 1 + game.CarveRoom(pos, rs) + } +} + +func (game *Game) NewMap() []Position { + game.FillMap(game.level, '#') + dead_ends := game.HuntAndKill() + + game.FillMap(game.level, '#') + game.AddRooms(dead_ends, game.height / 8) + + dead_ends = game.HuntAndKill() + + return dead_ends +} + +func (game *Game) PlaceEnemies(places []Position) { + for _, pos := range places { + if rand.Int() % 2 == 0 { + game.enemies[pos] = &Enemy{10, pos, 4} + } + } +} + +func main() { + out, err := os.Create("debug.log") + if err != nil { log.Fatal(err) } + dbg = log.New(out, "", log.LstdFlags) + + game := NewGame(27, 17) + game.InitScreen() + + dead_ends := game.NewMap() + game.PlaceEnemies(dead_ends) + game.Render() + + for game.HandleEvents() && game.player.hp > 0 { + game.Render() + } + + game.Exit() +} diff --git a/03_pathing_enemies/Makefile b/03_pathing_enemies/Makefile new file mode 100644 index 0000000..faf6c36 --- /dev/null +++ b/03_pathing_enemies/Makefile @@ -0,0 +1,6 @@ + +build: + go build . + +run: + ./gorogue diff --git a/03_pathing_enemies/go.mod b/03_pathing_enemies/go.mod new file mode 100644 index 0000000..add6d2e --- /dev/null +++ b/03_pathing_enemies/go.mod @@ -0,0 +1,17 @@ +module MY/gorogue + +go 1.25.1 + +require ( + github.com/gdamore/tcell/v2 v2.9.0 + github.com/mattn/go-runewidth v0.0.16 +) + +require ( + github.com/gdamore/encoding v1.0.1 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // 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/03_pathing_enemies/go.sum b/03_pathing_enemies/go.sum new file mode 100644 index 0000000..26d9b14 --- /dev/null +++ b/03_pathing_enemies/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/03_pathing_enemies/main.go b/03_pathing_enemies/main.go new file mode 100644 index 0000000..a5d1477 --- /dev/null +++ b/03_pathing_enemies/main.go @@ -0,0 +1,518 @@ + +package main + +import ( + "os" + "slices" + "log" + "fmt" + "math/rand" + "time" + "github.com/gdamore/tcell/v2" + "github.com/gdamore/tcell/v2/encoding" +) + +var dbg *log.Logger + +const ( + WALL = '#' + SPACE = '.' + PATH_LIMIT = 1000 + RENDER = true + SHOW_RENDER = false + SHOW_PATHS = true + HEARING_DISTANCE = 6 +) + +type Map [][]rune +type Paths [][]int + +type Position struct { + x int + y int +} + +type Enemy struct { + hp int + pos Position + damage int +} + +type Game struct { + screen tcell.Screen + level Map + paths Paths + player Enemy + status string + width int + height int + enemies map[Position]*Enemy +} + +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("HP: %d", game.player.hp) + + game.DrawText(game.width - len(hp), game.height, hp) +} + +func (game *Game) Status(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) + } + } + } +} + +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) + } + } +} + +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) + } + + game.DrawStatus() + + game.screen.Show() +} + +func (game *Game) Exit() { + if RENDER { + game.screen.Fini() + } + + os.Exit(0) +} + +func (game *Game) Occupied(x int, y int) bool { + pos := Position{x, y} + _, is_enemy := game.enemies[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[y][x] == WALL || + is_enemy || + is_player +} + +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) 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.x, target.y) { + game.player.pos = target + } +} + +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 +} + +func (game *Game) InitScreen() { + encoding.Register() + + var err error + game.screen, err = tcell.NewScreen() + if err != nil { log.Fatal(err) } + + err = game.screen.Init() + if err != nil { log.Fatal(err) } +} + +func NewGame(width int, height int) (*Game) { + var game Game + + game.width = width + game.height = height + game.enemies = make(map[Position]*Enemy) + + game.level = make(Map, height, height) + game.paths = make(Paths, height, height) + + game.player = Enemy{20, Position{1,1}, 4} + + return &game +} + +func compass(x int, y int, offset int) []Position { + return []Position{ + Position{x, y - offset}, + Position{x, y + offset}, + Position{x + offset, y}, + Position{ x - offset, y}, + } +} + +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) Neighbors(near Position) []Position { + result := make([]Position, 0, 4) + points := compass(near.x, near.y, 2) + + for _, pos := range points { + if game.Inbounds(pos, 0) { + result = append(result, pos) + } + } + + return result +} + +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) FindCoord(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) HuntAndKill() []Position { + on := Position{1, 1} + found := Position{1,1} + + dead_ends := make([]Position, 0) + + for { + neighbors := game.NeighborWalls(on) + + if len(neighbors) == 0 { + dead_ends = append(dead_ends, on) + + if !game.FindCoord(&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 dead_ends +} + +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) FillPaths(target Paths, setting int) { + for y := 0 ; y < game.height; y++ { + target[y] = slices.Repeat([]int{setting}, game.width) + } +} + +func (game *Game) CarveRoom(pos Position, size int) { + // only use ones far enough inside + for y := pos.y - size; y < pos.y + size; y++ { + for x := pos.x - size; x < pos.x + size; x++ { + if game.Inbounds(Position{x, y}, 1) { + game.level[y][x] = SPACE + } + } + } +} + +func (game *Game) MoveEnemy(from Position, to Position) { + enemy, ok := game.enemies[from] + if !ok { log.Fatal("no enemy at", from, "wtf") } + + delete(game.enemies, from) + game.enemies[to] = enemy +} + +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) PathAddNeighbors(neighbors []Position, closed Map, near Position) []Position { + points := compass(near.x, near.y, 1) + + for _, pos := range points { + // NOTE: if you also add !game.Occupied(pos.x, pos.y) it ???? + if closed[pos.y][pos.x] == SPACE { + closed[pos.y][pos.x] = WALL + neighbors = append(neighbors, pos) + } + } + + return neighbors +} + +func (game *Game) PathEnemies() { + in_grid := make([][]int, game.height, game.height) + game.FillPaths(in_grid, 1) + in_grid[game.player.pos.y][game.player.pos.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) EnemyMovement() { + for enemy_at, _ := range game.enemies { + // get the four directions + dirs := compass(enemy_at.x, enemy_at.y, 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.x, move_to.y) + + if can_hear && !occupied { + // move the enemy in the best direction + game.MoveEnemy(enemy_at, move_to) + } + } +} + +func (game *Game) AddRooms(dead_ends []Position, size int) { + rand.Shuffle(len(dead_ends), func(i, j int) { + dead_ends[i], dead_ends[j] = dead_ends[j], dead_ends[i] + }) + + for _, pos := range dead_ends[0:4] { + rs := rand.Int() % size + 1 + game.CarveRoom(pos, rs) + } +} + +func (game *Game) NewMap() []Position { + game.FillMap(game.level, '#') + dead_ends := game.HuntAndKill() + + game.FillMap(game.level, '#') + game.AddRooms(dead_ends, game.height / 8) + + dead_ends = game.HuntAndKill() + + return dead_ends +} + +func (game *Game) PlaceEnemies(places []Position) { + for _, pos := range places { + if rand.Int() % 2 == 0 { + game.enemies[pos] = &Enemy{10, pos, 4} + } + } +} + +func main() { + out, err := os.Create("debug.log") + if err != nil { log.Fatal(err) } + dbg = log.New(out, "", log.LstdFlags) + + game := NewGame(27, 17) + game.InitScreen() + + for { + dead_ends := game.NewMap() + game.PlaceEnemies(dead_ends) + game.Render() + + for game.HandleEvents() && game.player.hp > 0 { + game.PathEnemies() + game.EnemyMovement() + game.Render() + } + + game.Exit() + } +} diff --git a/04_combat/Makefile b/04_combat/Makefile new file mode 100644 index 0000000..faf6c36 --- /dev/null +++ b/04_combat/Makefile @@ -0,0 +1,6 @@ + +build: + go build . + +run: + ./gorogue diff --git a/04_combat/go.mod b/04_combat/go.mod new file mode 100644 index 0000000..add6d2e --- /dev/null +++ b/04_combat/go.mod @@ -0,0 +1,17 @@ +module MY/gorogue + +go 1.25.1 + +require ( + github.com/gdamore/tcell/v2 v2.9.0 + github.com/mattn/go-runewidth v0.0.16 +) + +require ( + github.com/gdamore/encoding v1.0.1 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // 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/04_combat/go.sum b/04_combat/go.sum new file mode 100644 index 0000000..26d9b14 --- /dev/null +++ b/04_combat/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/04_combat/main.go b/04_combat/main.go new file mode 100644 index 0000000..4a24dfe --- /dev/null +++ b/04_combat/main.go @@ -0,0 +1,554 @@ + +package main + +import ( + "os" + "slices" + "log" + "fmt" + "math/rand" + "time" + "github.com/gdamore/tcell/v2" + "github.com/gdamore/tcell/v2/encoding" +) + +var dbg *log.Logger + +const ( + WALL = '#' + SPACE = '.' + PATH_LIMIT = 1000 + RENDER = true + SHOW_RENDER = false + SHOW_PATHS = false + HEARING_DISTANCE = 6 +) + +type Map [][]rune +type Paths [][]int + +type Position struct { + x int + y int +} + +type Enemy struct { + hp int + pos Position + damage int +} + +type Game struct { + screen tcell.Screen + level Map + paths Paths + player Enemy + status string + width int + height int + enemies map[Position]*Enemy +} + +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("HP: %d", game.player.hp) + + game.DrawText(game.width - len(hp), game.height, hp) +} + +func (game *Game) Status(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) + } + } + } +} + +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) + } + } +} + +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) + } + + game.DrawStatus() + + game.screen.Show() +} + +func (game *Game) Exit() { + if RENDER { + game.screen.Fini() + } + + os.Exit(0) +} + +func (game *Game) Occupied(x int, y int) bool { + pos := Position{x, y} + _, is_enemy := game.enemies[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[y][x] == WALL || + is_enemy || + is_player +} + +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 *Enemy, defender *Enemy) { + damage := rand.Int() % attacker.damage + defender.hp -= damage + if damage == 0 { + game.Status("MISSED!") + } else if defender.hp > 0 { + game.Status(fmt.Sprintf("HIT %d damage", damage)) + } else { + game.Status("DEAD!") + } +} + +func (game *Game) Attack(target Position) { + enemy, hit_enemy := game.enemies[target] + + if hit_enemy { + game.ApplyDamage(&game.player, enemy) + game.ApplyDamage(enemy, &game.player) + } +} + +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.x, target.y) { + game.Attack(target) + } else { + game.player.pos = target + } +} + +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 +} + +func (game *Game) InitScreen() { + encoding.Register() + + var err error + game.screen, err = tcell.NewScreen() + if err != nil { log.Fatal(err) } + + err = game.screen.Init() + if err != nil { log.Fatal(err) } +} + +func NewGame(width int, height int) (*Game) { + var game Game + + game.width = width + game.height = height + game.enemies = make(map[Position]*Enemy) + + game.level = make(Map, height, height) + game.paths = make(Paths, height, height) + + game.player = Enemy{20, Position{1,1}, 4} + + return &game +} + +func compass(x int, y int, offset int) []Position { + return []Position{ + Position{x, y - offset}, + Position{x, y + offset}, + Position{x + offset, y}, + Position{ x - offset, y}, + } +} + +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) Neighbors(near Position) []Position { + result := make([]Position, 0, 4) + points := compass(near.x, near.y, 2) + + for _, pos := range points { + if game.Inbounds(pos, 0) { + result = append(result, pos) + } + } + + return result +} + +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) FindCoord(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) HuntAndKill() []Position { + on := Position{1, 1} + found := Position{1,1} + + dead_ends := make([]Position, 0) + + for { + neighbors := game.NeighborWalls(on) + + if len(neighbors) == 0 { + dead_ends = append(dead_ends, on) + + if !game.FindCoord(&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 dead_ends +} + +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) FillPaths(target Paths, setting int) { + for y := 0 ; y < game.height; y++ { + target[y] = slices.Repeat([]int{setting}, game.width) + } +} + +func (game *Game) CarveRoom(pos Position, size int) { + // only use ones far enough inside + for y := pos.y - size; y < pos.y + size; y++ { + for x := pos.x - size; x < pos.x + size; x++ { + if game.Inbounds(Position{x, y}, 1) { + game.level[y][x] = SPACE + } + } + } +} + +func (game *Game) MoveEnemy(from Position, to Position) { + enemy, ok := game.enemies[from] + if !ok { log.Fatal("no enemy at", from, "wtf") } + + delete(game.enemies, from) + game.enemies[to] = enemy +} + +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) PathAddNeighbors(neighbors []Position, closed Map, near Position) []Position { + points := compass(near.x, near.y, 1) + + for _, pos := range points { + // NOTE: if you also add !game.Occupied(pos.x, pos.y) it ???? + if closed[pos.y][pos.x] == SPACE { + closed[pos.y][pos.x] = WALL + neighbors = append(neighbors, pos) + } + } + + return neighbors +} + +func (game *Game) PathEnemies() { + in_grid := make([][]int, game.height, game.height) + game.FillPaths(in_grid, 1) + in_grid[game.player.pos.y][game.player.pos.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) EnemyMovement() { + for enemy_at, _ := range game.enemies { + // get the four directions + dirs := compass(enemy_at.x, enemy_at.y, 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.x, move_to.y) + + if can_hear && !occupied { + // move the enemy in the best direction + game.MoveEnemy(enemy_at, move_to) + } + } +} + +func (game *Game) AddRooms(dead_ends []Position, size int) { + rand.Shuffle(len(dead_ends), func(i, j int) { + dead_ends[i], dead_ends[j] = dead_ends[j], dead_ends[i] + }) + + for _, pos := range dead_ends[0:4] { + rs := rand.Int() % size + 1 + game.CarveRoom(pos, rs) + } +} + +func (game *Game) NewMap() []Position { + game.FillMap(game.level, '#') + dead_ends := game.HuntAndKill() + + game.FillMap(game.level, '#') + game.AddRooms(dead_ends, game.height / 8) + + dead_ends = game.HuntAndKill() + + return dead_ends +} + +func (game *Game) PlaceEnemies(places []Position) { + for _, pos := range places { + if rand.Int() % 2 == 0 { + game.enemies[pos] = &Enemy{10, pos, 4} + } + } +} + +func (game *Game) Restart() { + game.Status("YOU DIED! Try again.") + game.player.hp = 20 + game.player.pos = Position{1,1} + game.screen.Clear() + clear(game.enemies) +} + +func main() { + out, err := os.Create("debug.log") + if err != nil { log.Fatal(err) } + dbg = log.New(out, "", log.LstdFlags) + + game := NewGame(27, 17) + game.InitScreen() + + for { + dead_ends := game.NewMap() + game.PlaceEnemies(dead_ends) + game.Render() + + for game.HandleEvents() && game.player.hp > 0 { + game.EnemyDeath() + game.PathEnemies() + game.EnemyMovement() + game.Render() + } + + if game.player.hp <= 0 { + game.Restart() + } else { + game.Exit() + } + } +}