Added bias towards picking tiles further away (hearts/stars) or closer (monsters) when difficulty increases.

Added minimum distance (from player) for spawning things.
This commit is contained in:
Sander Schobers 2021-08-09 12:29:32 +02:00
parent ad8a540af2
commit 45b64a46ec

View File

@ -2,6 +2,7 @@ package tins2021
import ( import (
"math/rand" "math/rand"
"sort"
"opslag.de/schobers/geom" "opslag.de/schobers/geom"
) )
@ -126,14 +127,30 @@ func (l *Level) Randomize(difficulty int, stars int) {
for pos := range l.Tiles { for pos := range l.Tiles {
positions = append(positions, pos) positions = append(positions, pos)
} }
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)]
}
flip := difficulty * len(l.Tiles) / 200 flip := difficulty * len(l.Tiles) / 200
if flip > len(l.Tiles)/2 { if flip > len(l.Tiles)/2 {
flip = len(l.Tiles) / 2 flip = len(l.Tiles) / 2
} }
for ; flip > 0; flip-- { for ; flip > 0; flip-- {
for { for {
i := rand.Intn(len(positions)) pos := pick(0)
pos := positions[i]
if l.Tiles[pos].Inversed { if l.Tiles[pos].Inversed {
continue continue
} }
@ -146,8 +163,7 @@ func (l *Level) Randomize(difficulty int, stars int) {
} }
l.Stars = stars l.Stars = stars
for stars > 0 { for stars > 0 {
i := rand.Intn(len(positions)) pos := pick(difficulty)
pos := positions[i]
if l.Tiles[pos].Occupied() { if l.Tiles[pos].Occupied() {
continue continue
} }
@ -156,8 +172,7 @@ func (l *Level) Randomize(difficulty int, stars int) {
} }
hearts := 1 + (100-difficulty)/50 // [3..1] (only difficulty has 3 hearts) hearts := 1 + (100-difficulty)/50 // [3..1] (only difficulty has 3 hearts)
for hearts > 0 { for hearts > 0 {
i := rand.Intn(len(positions)) pos := pick(difficulty)
pos := positions[i]
if l.Tiles[pos].Occupied() { if l.Tiles[pos].Occupied() {
continue continue
} }
@ -168,8 +183,7 @@ func (l *Level) Randomize(difficulty int, stars int) {
minRandomMonster := (100-difficulty)*80/100 + 10 // [90..10] minRandomMonster := (100-difficulty)*80/100 + 10 // [90..10]
minChaserMonster := (100-difficulty)*50/100 + 50 // [100..50] minChaserMonster := (100-difficulty)*50/100 + 50 // [100..50]
for monsters > 0 { for monsters > 0 {
i := rand.Intn(len(positions)) pos := pick(100 - difficulty)
pos := positions[i]
curr := l.Monsters[pos] curr := l.Monsters[pos]
if l.Tiles[pos].Occupied() || curr != nil { if l.Tiles[pos].Occupied() || curr != nil {
continue continue