|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
"strings"
|
|
|
|
"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 = true
|
|
|
|
)
|
|
|
|
|
|
|
|
type Position struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Enemy struct {
|
|
|
|
hp int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
screen tcell.Screen
|
|
|
|
level [][]rune
|
|
|
|
player Position
|
|
|
|
status string
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
enemies map[Position]Enemy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) DrawStatus(msg string) {
|
|
|
|
for x, cell := range msg {
|
|
|
|
game.screen.SetContent(x, game.height, cell, nil, tcell.StyleDefault)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
for y, line := range game.level {
|
|
|
|
for x, cell := range line {
|
|
|
|
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, tcell.ColorYellow)
|
|
|
|
|
|
|
|
for pos, _ := range game.enemies {
|
|
|
|
game.DrawEntity('G', pos, tcell.ColorRed)
|
|
|
|
}
|
|
|
|
|
|
|
|
game.DrawStatus(game.status)
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
// Inbounds comes first to prevent accessing level with bad x,y
|
|
|
|
return !game.Inbounds(pos, 1) ||
|
|
|
|
game.level[y][x] == WALL ||
|
|
|
|
is_enemy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) {
|
|
|
|
if !game.Occupied(game.player.x + x_delta, game.player.y + y_delta) {
|
|
|
|
game.player.x += x_delta
|
|
|
|
game.player.y += y_delta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 MakeGame(width int, height int) (*Game) {
|
|
|
|
var game Game
|
|
|
|
var err error
|
|
|
|
|
|
|
|
encoding.Register()
|
|
|
|
|
|
|
|
game.width = width
|
|
|
|
game.height = height
|
|
|
|
game.enemies = make(map[Position]Enemy)
|
|
|
|
|
|
|
|
game.level = make([][]rune, height, height)
|
|
|
|
|
|
|
|
if RENDER {
|
|
|
|
game.screen, err = tcell.NewScreen()
|
|
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
|
|
|
|
err = game.screen.Init()
|
|
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
game.player = Position{1,1}
|
|
|
|
|
|
|
|
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) ClearMap() {
|
|
|
|
row := strings.Repeat("#", game.width)
|
|
|
|
|
|
|
|
for y := 0 ; y < game.height; y++ {
|
|
|
|
as_runes := []rune(strings.Clone(row))
|
|
|
|
game.level[y] = as_runes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) 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) MakeMap() []Position {
|
|
|
|
game.ClearMap()
|
|
|
|
dead_ends := game.HuntAndKill()
|
|
|
|
game.ClearMap()
|
|
|
|
game.AddRooms(dead_ends, 3)
|
|
|
|
dead_ends = game.HuntAndKill()
|
|
|
|
game.Status("FINISHED")
|
|
|
|
|
|
|
|
return dead_ends
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) PlaceEnemies(places []Position) {
|
|
|
|
for _, pos := range places {
|
|
|
|
if rand.Int() % 2 == 0 {
|
|
|
|
game.enemies[pos] = Enemy{10}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This program just prints "Hello, World!". Press ESC to exit.
|
|
|
|
func main() {
|
|
|
|
out, err := os.Create("debug.log")
|
|
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
dbg = log.New(out, "", log.LstdFlags)
|
|
|
|
|
|
|
|
game := MakeGame(43, 27)
|
|
|
|
dead_ends := game.MakeMap()
|
|
|
|
game.PlaceEnemies(dead_ends)
|
|
|
|
game.Render()
|
|
|
|
|
|
|
|
for game.HandleEvents() {
|
|
|
|
game.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
game.Exit()
|
|
|
|
}
|