|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"math/rand" |
|
|
|
"math/rand" |
|
|
|
|
|
|
|
"time" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"github.com/gdamore/tcell/v2" |
|
|
|
"github.com/gdamore/tcell/v2" |
|
|
|
"github.com/gdamore/tcell/v2/encoding" |
|
|
|
"github.com/gdamore/tcell/v2/encoding" |
|
|
@ -39,6 +40,10 @@ func (game *Game) Text(msg string) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Status(msg string) { |
|
|
|
|
|
|
|
game.status = msg |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Render() { |
|
|
|
func (game *Game) Render() { |
|
|
|
var comb []rune |
|
|
|
var comb []rune |
|
|
|
game.screen.Clear() |
|
|
|
game.screen.Clear() |
|
|
@ -60,7 +65,7 @@ func (game *Game) Exit() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Occupied(x int, y int) bool { |
|
|
|
func (game *Game) Occupied(x int, y int) bool { |
|
|
|
return game.level[y][x] != '.' |
|
|
|
return game.level[y][x] != '.' || !game.Inbounds(Position{x, y}, 1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
|
func (game *Game) MovePlayer(x_delta int, y_delta int) { |
|
|
@ -99,17 +104,23 @@ func (game *Game) HandleEvents() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func MakeGame() (*Game) { |
|
|
|
func MakeGame(width int, height int) (*Game) { |
|
|
|
var game Game |
|
|
|
var game Game |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
encoding.Register() |
|
|
|
encoding.Register() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.width = width |
|
|
|
|
|
|
|
game.height = height |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.level = make([][]rune, height, height) |
|
|
|
|
|
|
|
|
|
|
|
game.screen, err = tcell.NewScreen() |
|
|
|
game.screen, err = tcell.NewScreen() |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
|
|
|
|
err = game.screen.Init() |
|
|
|
err = game.screen.Init() |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
|
|
|
|
game.player = Position{1,1} |
|
|
|
game.player = Position{1,1} |
|
|
|
|
|
|
|
|
|
|
|
return &game |
|
|
|
return &game |
|
|
@ -124,8 +135,11 @@ func compass(x int, y int, offset int) []Position { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Inbounds(pos Position) bool { |
|
|
|
func (game *Game) Inbounds(pos Position, offset int) bool { |
|
|
|
return pos.x >= 0 && pos.x < game.width && pos.y >= 0 && pos.y < game.height |
|
|
|
return pos.x >= offset && |
|
|
|
|
|
|
|
pos.x < game.width - offset && |
|
|
|
|
|
|
|
pos.y >= offset && |
|
|
|
|
|
|
|
pos.y < game.height - offset |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) Neighbors(near Position) []Position { |
|
|
|
func (game *Game) Neighbors(near Position) []Position { |
|
|
@ -133,7 +147,7 @@ func (game *Game) Neighbors(near Position) []Position { |
|
|
|
points := compass(near.x, near.y, 2) |
|
|
|
points := compass(near.x, near.y, 2) |
|
|
|
|
|
|
|
|
|
|
|
for _, pos := range points { |
|
|
|
for _, pos := range points { |
|
|
|
if game.Inbounds(pos) { |
|
|
|
if game.Inbounds(pos, 0) { |
|
|
|
result = append(result, pos) |
|
|
|
result = append(result, pos) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -178,6 +192,13 @@ func (game *Game) FindCoord(on *Position, found *Position) bool { |
|
|
|
return false |
|
|
|
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 { |
|
|
|
func (game *Game) HuntAndKill() []Position { |
|
|
|
on := Position{1, 1} |
|
|
|
on := Position{1, 1} |
|
|
|
found := Position{1,1} |
|
|
|
found := Position{1,1} |
|
|
@ -194,43 +215,59 @@ func (game *Game) HuntAndKill() []Position { |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
game.level[on.y][on.x] = SPACE |
|
|
|
game.HAKStep(on, found) |
|
|
|
|
|
|
|
|
|
|
|
row := (on.y + found.y) / 2 |
|
|
|
|
|
|
|
col := (on.x + found.x) / 2 |
|
|
|
|
|
|
|
game.level[row][col] = SPACE |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
rand_neighbor := rand.Int() % len(neighbors) |
|
|
|
rand_neighbor := rand.Int() % len(neighbors) |
|
|
|
nb := neighbors[rand_neighbor] |
|
|
|
nb := neighbors[rand_neighbor] |
|
|
|
|
|
|
|
game.HAKStep(nb, on) |
|
|
|
game.level[nb.y][nb.x] = SPACE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
row := (nb.y + on.y) / 2 |
|
|
|
|
|
|
|
col := (nb.x + on.x) / 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.level[row][col] = SPACE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
on = nb |
|
|
|
on = nb |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.status = "FINISHED" |
|
|
|
game.Render() |
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return dead_ends |
|
|
|
return dead_ends |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (game *Game) MakeMap(width int, height int) { |
|
|
|
func (game *Game) ClearMap() { |
|
|
|
game.width = width |
|
|
|
row := strings.Repeat("#", game.width) |
|
|
|
game.height = height |
|
|
|
|
|
|
|
row := strings.Repeat("#", width) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0 ; i < height; i++ { |
|
|
|
for y := 0 ; y < game.height; y++ { |
|
|
|
as_runes := []rune(strings.Clone(row)) |
|
|
|
as_runes := []rune(strings.Clone(row)) |
|
|
|
game.level = append(game.level, as_runes) |
|
|
|
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) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// this returns dead_ends
|
|
|
|
func (game *Game) MakeMap() { |
|
|
|
|
|
|
|
game.ClearMap() |
|
|
|
|
|
|
|
dead_ends := game.HuntAndKill() |
|
|
|
|
|
|
|
game.ClearMap() |
|
|
|
|
|
|
|
game.AddRooms(dead_ends, 3) |
|
|
|
game.HuntAndKill() |
|
|
|
game.HuntAndKill() |
|
|
|
|
|
|
|
game.Status("FINISHED") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// This program just prints "Hello, World!". Press ESC to exit.
|
|
|
|
// This program just prints "Hello, World!". Press ESC to exit.
|
|
|
@ -239,8 +276,8 @@ func main() { |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
dbg = log.New(out, "", log.LstdFlags) |
|
|
|
dbg = log.New(out, "", log.LstdFlags) |
|
|
|
|
|
|
|
|
|
|
|
game := MakeGame() |
|
|
|
game := MakeGame(27, 17) |
|
|
|
game.MakeMap(27,17) |
|
|
|
game.MakeMap() |
|
|
|
game.Render() |
|
|
|
game.Render() |
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
for { |
|
|
|