-
Notifications
You must be signed in to change notification settings - Fork 1
/
aiplayer.ts
215 lines (186 loc) · 6.87 KB
/
aiplayer.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// server/AIPlayer.ts
import { Game } from "../game/game.ts";
import { GameGrid } from "../game/grid.ts";
import { Vector } from "../game/tetromino.ts";
import { BasePlayer } from "./baseplayer.ts";
class AIPlayer extends BasePlayer {
private aiInterval: number | undefined;
private BOT_NICKNAME = "AI-BOT";
private BASE_DECISION_DELAY_MS = 200;
private RANDOM_DECISION_DELAY_MAX_MS = 500;
// deno-lint-ignore no-explicit-any
constructor(games: Game[], config: any) {
super(null, games); // null socket
this.setNickname(this.BOT_NICKNAME);
// Custom config
if (config) {
if (config.BOT_NICKNAME) this.BOT_NICKNAME = config.BOT_NICKNAME;
if (config.BASE_DECISION_DELAY_MS) {
this.BASE_DECISION_DELAY_MS = config.BASE_DECISION_DELAY_MS;
}
if (config.RANDOM_DECISION_DELAY_MAX_MS) {
this.RANDOM_DECISION_DELAY_MAX_MS = config.RANDOM_DECISION_DELAY_MAX_MS;
}
}
// Cap custom config
if (this.BASE_DECISION_DELAY_MS < 50) this.BASE_DECISION_DELAY_MS = 50;
if (this.RANDOM_DECISION_DELAY_MAX_MS < 50) {
this.RANDOM_DECISION_DELAY_MAX_MS = 50;
}
}
public connect(game: Game) {
if (!(game.getMode() === "battle" || game.getMode() === "coop")) {
throw new Error("Bot only supports battle or coop games");
}
this.setGame(game);
this.g?.addPlayer(this);
this.startAI();
}
public setKeyState(key: string, value: boolean) {
this.controls[key] = value;
if (this.g) this.g.act(this, key, value);
}
private startAI() {
this.aiInterval = setInterval(
() => {
const controls = this.calculateAIControls();
for (const key in controls) {
this.setKeyState(key, controls[key]);
}
if (this.g?.getStatus() === "gameover") {
clearInterval(this.aiInterval);
}
},
this.BASE_DECISION_DELAY_MS +
(Math.random() * this.RANDOM_DECISION_DELAY_MAX_MS),
); // AI decision every xms
}
private calculateAIControls(): { [key: string]: boolean } {
const controls: { [key: string]: boolean } = {
"up": false,
"down": false,
"left": false,
"right": false,
"rotCW": false,
"rotCCW": false,
"drop": false,
};
if (!this.g) {
return controls;
}
const playerIndex = this.g.getPlayerIndex(this);
// deno-lint-ignore no-explicit-any
const gameData: any = this.g.getDataNew();
const currentTetromino = gameData.Tetrominoes[playerIndex][0];
const _nextTetromino = gameData.Tetrominoes[playerIndex][1];
const currentPosition = gameData.Position[playerIndex];
const currentRotation = gameData.Rotation[playerIndex];
const grid: GameGrid = this.g.getMode() === "battle"
? gameData.Grid[playerIndex]
: gameData.Grid;
let leastSpaceBelow = Infinity;
let lowestLowPoint = Infinity;
let lowestHighPoint = Infinity;
let bestMove = null;
// Try different rotations
for (let rotation = 0; rotation < 4; rotation++) {
// Extract rotated tetromino sprite
const bogusSprite = currentTetromino.Sprites[rotation].Data;
// Try different positions, start at -2 to ensure that tetrominoes beginning with empty columns can be placed
for (let x = -2; x < grid.width; x++) {
// Drop tetromino
let y = currentPosition.Y;
let bogusY = y + 1;
while (this.validMove(grid, bogusSprite, { X: x, Y: bogusY })) {
y = bogusY;
bogusY += 1;
}
// Check for least space below
if (this.validMove(grid, bogusSprite, { X: x, Y: y })) {
const lowPoint = y + Math.max(...bogusSprite.map((v: Vector) => v.Y)); // Higher is better
const highPoint = y +
Math.min(...bogusSprite.map((v: Vector) => v.Y)); // Higher is better
const spaceBelow = this.countEmptySpaces(grid, bogusSprite, {
X: x,
Y: y,
});
if (
(
spaceBelow < leastSpaceBelow ||
(spaceBelow - 1 < leastSpaceBelow &&
lowPoint > lowestLowPoint + 2) || // Allow placing a tetromino with 1 empty slot below, if this will make it place 2 rows lower in the grid
(spaceBelow - 2 < leastSpaceBelow &&
lowPoint > lowestLowPoint + 4) // Allow placing a tetromino with 2 empty slots below, if this will make it place 4 rows lower in the grid
) ||
(spaceBelow === leastSpaceBelow && (lowPoint > lowestLowPoint) ||
(spaceBelow === leastSpaceBelow &&
(lowPoint === lowestLowPoint) && highPoint > lowestHighPoint))
) {
leastSpaceBelow = spaceBelow;
lowestLowPoint = lowPoint;
lowestHighPoint = highPoint;
bestMove = { x, rotation };
}
}
}
}
if (bestMove) {
// Try to get to the best move
controls.rotCW = bestMove.rotation > currentRotation;
controls.rotCCW = bestMove.rotation < currentRotation;
controls.left = bestMove.x < currentPosition.X;
controls.right = bestMove.x > currentPosition.X;
// Already there?
controls.drop = bestMove.x === currentPosition.X &&
bestMove.rotation === currentRotation;
}
return controls;
}
validMove(grid: GameGrid, s: Vector[], p: Vector): boolean {
for (const v of s) {
// Check that there is space below
const targetX = v.X + p.X;
const targetY = v.Y + p.Y; // Increment Y position by 1
const outOfRange = targetX < 0 ||
targetX > grid.width - 1 ||
targetY < 0 ||
targetY > grid.height - 1 ||
(grid.Data[targetX + targetY * grid.width] !==
undefined);
if (outOfRange) { // Check for undefined instead of null
return false;
}
}
return true;
}
private countEmptySpaces(grid: GameGrid, s: Vector[], p: Vector): number {
let emptySpaces = 0;
for (const spriteVector of s) {
// First, check that the sprite itself does not cover the vector below
const vectorBelow = { X: spriteVector.X, Y: spriteVector.Y + 1 };
if (!s.some((v) => v.X === vectorBelow.X && v.Y === vectorBelow.Y)) {
// The sprite doesn't cover the vector below, now check the grid
// Then check that the grid doesnt cover the vector below
const targetX = spriteVector.X + p.X;
const targetY = spriteVector.Y + p.Y + 1;
const targetArrayIndex = grid.width * targetY + targetX;
if (targetArrayIndex < grid.Data.length) {
if (grid.Data[targetArrayIndex] === undefined) {
// There is empty space below!
emptySpaces++;
}
}
}
}
return emptySpaces;
}
public disconnect() {
if (this.aiInterval) {
clearInterval(this.aiInterval);
this.aiInterval = undefined;
}
this.g?.removePlayer(this);
this.setGame(null);
}
}
export { AIPlayer };