diff --git a/src/domain/bot.ts b/src/domain/bot.ts index eabfbed..f87c261 100644 --- a/src/domain/bot.ts +++ b/src/domain/bot.ts @@ -48,12 +48,23 @@ export default class Bot { } handleMove(angle: number, map: Map): void { - this.angle = angle + this.angle = this.pummiFactor(angle) console.log(this.position.x) - this.position.updatePosition(angle, map.getSpeedAtPosition(this.position)) + this.position.updatePosition( + this.angle, + map.getSpeedAtPosition(this.position), + ) Log.moveMessage(this.name, this.position) } + pummiFactor(angle: number): number { + // Standard normal distribution with box-muller transform https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform + let u1 = Math.random() + let u2 = Math.random() + let z0 = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2) + return angle + z0 * (Math.PI / 3) + } + handleStamp(map: Map): void { const { x, y } = this.position const closeEnoughToCheckpoint = map.closeEnoughToCheckpoint(this.position) diff --git a/src/domain/map.ts b/src/domain/map.ts index aa6a677..795181b 100644 --- a/src/domain/map.ts +++ b/src/domain/map.ts @@ -27,7 +27,7 @@ export default class Map { } getSpeedAtPosition(position: Position): number { - return 1 + return 10 } closeEnoughToCheckpoint(position: Position): boolean { diff --git a/src/example/client.ts b/src/example/client.ts index 441cb19..ba424cc 100644 --- a/src/example/client.ts +++ b/src/example/client.ts @@ -94,15 +94,11 @@ const joinMessage = { } const createConnection = (): void => { - const socket = connect( - PORT, - 'localhost', - () => { - // Send the initial message once connected - console.log(`Joining: ${JSON.stringify(joinMessage)}`) - socket.write(JSON.stringify(joinMessage)) - }, - ) + const socket = connect(PORT, 'localhost', () => { + // Send the initial message once connected + console.log(`Joining: ${JSON.stringify(joinMessage)}`) + socket.write(JSON.stringify(joinMessage)) + }) const handleMapMessage = (socket: Socket, message: MapMessage): void => { Log.info(`Received map: ${JSON.stringify(message.data)}`) @@ -150,10 +146,10 @@ const createConnection = (): void => { return } if ( - // Have a discussion with Jarno - or someone who knows goddamn math + // Could also use norm in math.js: https://mathjs.org/docs/reference/functions/norm.html Math.sqrt( - (currentPosition.x - target.x) * (currentPosition.x - target.x) + - (currentPosition.y - target.y) * (currentPosition.y - target.y), + (currentPosition.x - target.x) ** 2 + + (currentPosition.y - target.y) ** 2, ) < 5.0 ) { if (target === map.goal) {