From bce4758dfdc93ec41dda067e9c1f382e9c2dd15c Mon Sep 17 00:00:00 2001 From: Luke Meyers Date: Wed, 5 Feb 2020 21:14:27 -0800 Subject: [PATCH] rudimentary brakes --- game/game.go | 46 ++++++++++++++++++++++++++++++++++++++-------- game/physics.go | 6 ------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/game/game.go b/game/game.go index f540001..02b04b4 100644 --- a/game/game.go +++ b/game/game.go @@ -2,6 +2,7 @@ package game import ( "log" + "math/rand" ) type State struct { @@ -70,13 +71,7 @@ func UpdateState(sOld State) State { s := sOld for i, t := range s.Teams { - switch chooseCommand(t, sOld) { - case speedUp: - accelerate(t.Baton.Holder) - case left: - t.Baton.Holder.Lane++ - } - + doCommand(chooseCommand(t, sOld), t.Baton.Holder) moveBot(t.Baton.Holder, sOld) maybePassBaton(&s.Teams[i]) } @@ -90,11 +85,45 @@ func UpdateState(sOld State) State { return s } +func doCommand(cmd command, b *Bot) { + da := 1 + da += rand.Intn(3) - 1 + + switch cmd { + case speedUp: + b.a += da + accelerate(b) + case slowDown: + b.a -= da + accelerate(b) + case left: + b.Lane++ + } +} + func chooseCommand(t Team, s State) command { h := t.Baton.Holder if collide(h.Pos+1, h.Lane, s) { return left } + + var nextBot *Bot + for i, b := range t.Bots { + if b.id == h.id+1 { + nextBot = &t.Bots[i] + break + } + } + + if nextBot != nil { + if h.Lane != nextBot.Lane { + if abs(nextBot.Pos-h.Pos) < h.v { + log.Println("WHOOOOOOA") + return slowDown + } + } + } + return speedUp } @@ -102,6 +131,7 @@ type command int const ( speedUp command = iota + slowDown left ) @@ -140,7 +170,7 @@ const ( numBots = 5 NumTeams = 2 maxA = 3 - maxV = 2 + maxV = 10 ) func abs(n int) int { diff --git a/game/physics.go b/game/physics.go index 868f314..2116845 100644 --- a/game/physics.go +++ b/game/physics.go @@ -1,12 +1,6 @@ package game -import "math/rand" - func accelerate(b *Bot) { - if b.a == 0 { - b.a = 1 - } - b.a += rand.Intn(3) - 1 if b.a < -maxA { b.a = -maxA }