2021-08-06 22:00:18 +00:00
|
|
|
package tins2021
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2021-08-09 10:29:32 +00:00
|
|
|
"sort"
|
2021-08-06 22:00:18 +00:00
|
|
|
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Level struct {
|
2021-08-08 23:25:51 +00:00
|
|
|
Player geom.Point
|
|
|
|
Lives int
|
2021-08-09 07:25:06 +00:00
|
|
|
Stars int
|
2021-08-08 23:25:51 +00:00
|
|
|
StarsCollected int
|
|
|
|
Tiles Tiles
|
|
|
|
Monsters Monsters
|
|
|
|
MonsterTargets map[geom.Point]geom.Point
|
2021-08-09 09:29:10 +00:00
|
|
|
GameOver bool
|
2021-08-09 10:08:28 +00:00
|
|
|
Difficulty int
|
|
|
|
Score int
|
2021-08-08 23:25:51 +00:00
|
|
|
Bounds geom.Rectangle
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 23:25:51 +00:00
|
|
|
func NewLevel() *Level {
|
|
|
|
const dims = 12
|
|
|
|
f := &Level{
|
|
|
|
Player: geom.Pt(1, 1),
|
|
|
|
Lives: 3,
|
2021-08-09 07:25:06 +00:00
|
|
|
Stars: 0,
|
2021-08-08 23:25:51 +00:00
|
|
|
StarsCollected: 0,
|
|
|
|
Tiles: Tiles{},
|
|
|
|
Monsters: Monsters{},
|
|
|
|
MonsterTargets: map[geom.Point]geom.Point{},
|
|
|
|
Bounds: geom.Rect(1, 1, dims+1, dims+1),
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
2021-08-08 23:25:51 +00:00
|
|
|
for y := 1; y <= dims; y++ {
|
|
|
|
endX := dims
|
|
|
|
if y%2 == 0 {
|
|
|
|
endX--
|
|
|
|
}
|
|
|
|
for x := 1; x <= endX; x++ {
|
|
|
|
f.Tiles[geom.Pt(x, y)] = &Tile{}
|
|
|
|
}
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
2021-08-08 23:25:51 +00:00
|
|
|
return f
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l Level) CanPlayerMove(dir Direction) (geom.Point, bool) {
|
2021-08-08 23:25:51 +00:00
|
|
|
return l.Tiles.CanMove(l.Player, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Level) CanMonsterMove(p geom.Point, dir Direction) (geom.Point, bool) {
|
|
|
|
q, ok := l.Tiles.CanMove(p, dir)
|
|
|
|
if !ok {
|
|
|
|
return geom.Point{}, false
|
|
|
|
}
|
|
|
|
if l.CanMonsterMoveTo(q) {
|
|
|
|
return q, true
|
|
|
|
}
|
|
|
|
return geom.Point{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Level) CanMonsterMoveTo(p geom.Point) bool {
|
|
|
|
if l.Tiles[p].Occupied() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, ok := l.Monsters[p]; ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, target := range l.MonsterTargets {
|
|
|
|
if p == target {
|
|
|
|
return false
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-08 23:25:51 +00:00
|
|
|
return true
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 09:29:10 +00:00
|
|
|
func (l *Level) DecrementLive() {
|
|
|
|
l.Lives--
|
|
|
|
if l.Lives == 0 {
|
|
|
|
l.GameOver = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Level) DestroyMonster(pos geom.Point) {
|
|
|
|
delete(l.MonsterTargets, pos)
|
|
|
|
delete(l.Monsters, pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Level) MoveMonster(target, source geom.Point) {
|
|
|
|
l.Monsters[target] = l.Monsters[source]
|
|
|
|
l.DestroyMonster(source)
|
|
|
|
}
|
|
|
|
|
2021-08-14 07:22:40 +00:00
|
|
|
type MonsterHit struct {
|
|
|
|
Position geom.Point
|
|
|
|
Type MonsterType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Level) MovePlayer(dir Direction) (bool, *MonsterHit) {
|
2021-08-06 22:00:18 +00:00
|
|
|
towards, allowed := l.CanPlayerMove(dir)
|
|
|
|
if !allowed {
|
2021-08-14 07:22:40 +00:00
|
|
|
return false, nil
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
|
|
|
l.Player = towards
|
2021-08-08 23:25:51 +00:00
|
|
|
tile := l.Tiles[towards]
|
|
|
|
if tile.Heart {
|
|
|
|
l.Lives++
|
|
|
|
tile.Heart = false
|
2021-08-09 10:08:28 +00:00
|
|
|
l.Score += 10
|
2021-08-08 23:25:51 +00:00
|
|
|
}
|
|
|
|
if tile.Star {
|
|
|
|
l.StarsCollected++
|
|
|
|
tile.Star = false
|
2021-08-09 10:08:28 +00:00
|
|
|
l.Score += 25
|
2021-08-08 23:25:51 +00:00
|
|
|
}
|
2021-08-14 07:22:40 +00:00
|
|
|
var hit *MonsterHit
|
2021-08-09 09:29:10 +00:00
|
|
|
if l.Monsters[towards] != nil {
|
2021-08-14 07:22:40 +00:00
|
|
|
hit = &MonsterHit{
|
|
|
|
Position: towards,
|
|
|
|
Type: l.Monsters[towards].Type(),
|
|
|
|
}
|
2021-08-09 09:29:10 +00:00
|
|
|
l.DecrementLive()
|
|
|
|
l.DestroyMonster(towards)
|
2021-08-09 10:08:28 +00:00
|
|
|
l.Score -= 5
|
2021-08-09 09:29:10 +00:00
|
|
|
}
|
2021-08-09 10:08:28 +00:00
|
|
|
l.Score -= 1 // for every move
|
2021-08-14 07:22:40 +00:00
|
|
|
return true, hit
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 23:25:51 +00:00
|
|
|
func (l *Level) Randomize(difficulty int, stars int) {
|
|
|
|
if difficulty < 0 {
|
|
|
|
difficulty = 0
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
2021-08-09 10:08:28 +00:00
|
|
|
l.Difficulty = difficulty
|
2021-08-08 23:25:51 +00:00
|
|
|
positions := make([]geom.Point, 0, len(l.Tiles))
|
|
|
|
for pos := range l.Tiles {
|
|
|
|
positions = append(positions, pos)
|
|
|
|
}
|
2021-08-09 10:29:32 +00:00
|
|
|
distances := l.Tiles.Distances(l.Player)
|
|
|
|
sort.Slice(positions, func(i, j int) bool {
|
|
|
|
return distances[positions[i]] < distances[positions[j]]
|
|
|
|
})
|
|
|
|
pick := func(bias int) geom.Point { // bias towards far away [0..100]
|
|
|
|
if bias > 100 {
|
|
|
|
bias = 100
|
|
|
|
}
|
|
|
|
if bias < 0 {
|
|
|
|
bias = 0
|
|
|
|
}
|
|
|
|
n := len(positions)
|
|
|
|
const min = 4
|
|
|
|
from := min + bias*(n-min)/(2*(n-min)) // never pick first
|
|
|
|
return positions[from+rand.Intn(n-from)]
|
|
|
|
}
|
|
|
|
|
2021-08-08 23:25:51 +00:00
|
|
|
flip := difficulty * len(l.Tiles) / 200
|
|
|
|
if flip > len(l.Tiles)/2 {
|
|
|
|
flip = len(l.Tiles) / 2
|
|
|
|
}
|
|
|
|
for ; flip > 0; flip-- {
|
|
|
|
for {
|
2021-08-09 10:29:32 +00:00
|
|
|
pos := pick(0)
|
2021-08-08 23:25:51 +00:00
|
|
|
if l.Tiles[pos].Inversed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.Tiles[pos].Invert()
|
|
|
|
if l.Tiles.AllReachable(l.Player) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
l.Tiles[pos].Invert()
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
2021-08-08 23:25:51 +00:00
|
|
|
}
|
2021-08-09 07:25:06 +00:00
|
|
|
l.Stars = stars
|
2021-08-08 23:25:51 +00:00
|
|
|
for stars > 0 {
|
2021-08-09 10:29:32 +00:00
|
|
|
pos := pick(difficulty)
|
2021-08-08 23:25:51 +00:00
|
|
|
if l.Tiles[pos].Occupied() {
|
|
|
|
continue
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
2021-08-08 23:25:51 +00:00
|
|
|
l.Tiles[pos].Star = true
|
|
|
|
stars--
|
|
|
|
}
|
2021-08-09 16:24:41 +00:00
|
|
|
hearts := rand.Intn(2 + (100-difficulty)/50) // [3..1] (only difficulty 0 has 3 hearts)
|
2021-08-08 23:25:51 +00:00
|
|
|
for hearts > 0 {
|
2021-08-09 10:29:32 +00:00
|
|
|
pos := pick(difficulty)
|
2021-08-08 23:25:51 +00:00
|
|
|
if l.Tiles[pos].Occupied() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.Tiles[pos].Heart = true
|
|
|
|
hearts--
|
|
|
|
}
|
2021-08-09 16:24:41 +00:00
|
|
|
monsters := 5 + (25 * difficulty / 100)
|
|
|
|
minRandomMonster := (100-difficulty)*60/100 + 10 // [70..10]
|
|
|
|
minChaserMonster := (100-difficulty)*70/100 + 25 // [95..25]
|
2021-08-08 23:25:51 +00:00
|
|
|
for monsters > 0 {
|
2021-08-09 10:29:32 +00:00
|
|
|
pos := pick(100 - difficulty)
|
2021-08-08 23:25:51 +00:00
|
|
|
curr := l.Monsters[pos]
|
|
|
|
if l.Tiles[pos].Occupied() || curr != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
monster := MonsterTypeStraight
|
|
|
|
m := rand.Intn(100)
|
|
|
|
if m >= minChaserMonster {
|
|
|
|
monster = MonsterTypeChaser
|
|
|
|
} else if m >= minRandomMonster {
|
|
|
|
monster = MonsterTypeRandom
|
|
|
|
}
|
|
|
|
switch monster {
|
|
|
|
case MonsterTypeStraight:
|
|
|
|
l.Monsters[pos] = &StraightWalkingMonster{Direction: RandomDirection()}
|
|
|
|
case MonsterTypeRandom:
|
|
|
|
l.Monsters[pos] = &RandomWalkingMonster{}
|
|
|
|
case MonsterTypeChaser:
|
|
|
|
l.Monsters[pos] = &ChasingMonster{}
|
|
|
|
default:
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
monsters--
|
2021-08-06 22:00:18 +00:00
|
|
|
}
|
|
|
|
}
|