From 45b64a46ec51dbeb92c14366ad0bba013f4ec9b8 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 9 Aug 2021 12:29:32 +0200 Subject: [PATCH] Added bias towards picking tiles further away (hearts/stars) or closer (monsters) when difficulty increases. Added minimum distance (from player) for spawning things. --- level.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/level.go b/level.go index 6f816b3..017a602 100644 --- a/level.go +++ b/level.go @@ -2,6 +2,7 @@ package tins2021 import ( "math/rand" + "sort" "opslag.de/schobers/geom" ) @@ -126,14 +127,30 @@ func (l *Level) Randomize(difficulty int, stars int) { for pos := range l.Tiles { 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 if flip > len(l.Tiles)/2 { flip = len(l.Tiles) / 2 } for ; flip > 0; flip-- { for { - i := rand.Intn(len(positions)) - pos := positions[i] + pos := pick(0) if l.Tiles[pos].Inversed { continue } @@ -146,8 +163,7 @@ func (l *Level) Randomize(difficulty int, stars int) { } l.Stars = stars for stars > 0 { - i := rand.Intn(len(positions)) - pos := positions[i] + pos := pick(difficulty) if l.Tiles[pos].Occupied() { 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) for hearts > 0 { - i := rand.Intn(len(positions)) - pos := positions[i] + pos := pick(difficulty) if l.Tiles[pos].Occupied() { continue } @@ -168,8 +183,7 @@ func (l *Level) Randomize(difficulty int, stars int) { minRandomMonster := (100-difficulty)*80/100 + 10 // [90..10] minChaserMonster := (100-difficulty)*50/100 + 50 // [100..50] for monsters > 0 { - i := rand.Intn(len(positions)) - pos := positions[i] + pos := pick(100 - difficulty) curr := l.Monsters[pos] if l.Tiles[pos].Occupied() || curr != nil { continue