-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.js
92 lines (77 loc) · 2.25 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Action, MessageType, MapUtility } from '../src/index.js';
export const BOT_NAME = 'Turner';
const directionActions = [Action.Up, Action.Right, Action.Down, Action.Left];
let currentDirection = 0;
function canMoveForward(mapUtils) {
return mapUtils.canIMoveInDirection(directionActions[currentDirection]);
}
function canMoveLeft(mapUtils) {
return mapUtils.canIMoveInDirection(directionActions[(currentDirection + 3) % 4]);
}
function canMoveRight(mapUtils) {
return mapUtils.canIMoveInDirection(directionActions[(currentDirection + 1) % 4]);
}
function canMoveBack(mapUtils) {
return mapUtils.canIMoveInDirection(directionActions[(currentDirection + 2) % 4]);
}
function turnLeft() {
currentDirection = (currentDirection + 3) % 4;
}
function turnRight() {
currentDirection = (currentDirection + 1) % 4;
}
function turnAround() {
currentDirection = (currentDirection + 2) % 4;
}
/**
* @param {import('../src/index.js').MapUpdateEvent} mapUpdateEvent
* @returns {Action | Promise<Action>}
*/
export function getNextAction(mapUpdateEvent) {
const mapUtils = new MapUtility(mapUpdateEvent.map, mapUpdateEvent.receivingPlayerId);
const myCharacter = mapUtils.getMyCharacterInfo();
if (myCharacter.carryingPowerUp) {
return Action.Explode;
}
if (!canMoveForward(mapUtils)) {
if (canMoveLeft(mapUtils)) {
turnLeft();
} else if (canMoveRight(mapUtils)) {
turnRight();
} else if (canMoveBack(mapUtils)) {
turnAround();
} else {
return Action.Stay;
}
}
return directionActions[currentDirection];
}
// This handler is optional
export function onMessage(message) {
switch (message.type) {
case MessageType.GameStarting:
// Reset bot state here
currentDirection = 0;
break;
}
}
// Set to null to user server default settings
export const GAME_SETTINGS = {
maxNoofPlayers: 5,
timeInMsPerTick: 250,
obstaclesEnabled: true,
powerUpsEnabled: true,
addPowerUpLikelihood: 15,
removePowerUpLikelihood: 5,
trainingGame: true,
pointsPerTileOwned: 1,
pointsPerCausedStun: 5,
noOfTicksInvulnerableAfterStun: 3,
minNoOfTicksStunned: 8,
maxNoOfTicksStunned: 10,
startObstacles: 30,
startPowerUps: 0,
gameDurationInSeconds: 60,
explosionRange: 4,
pointsPerTick: false,
};