-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcell.js
423 lines (352 loc) · 12.6 KB
/
cell.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
Note that the main run() loop will be executed once per frame as specified by WORLD_UPDATE_INTERVAL in worker.js.
Behind the scenes, the engine just keeps on building up a cellData tree of all different
state objects that are present within our current grid cell.
The tree is a simple JSON object and needs to be in the format:
{
// player is a state type.
player: {
// ...
},
// You can add other custom state types.
someType: {
// Use the id as the key (in the place of someId).
// It is recommended that you use a random uuid as the state id. See https://www.npmjs.com/package/uuid
someId: {
// All properties listed here are required.
// You can add additional ones.
id: someId,
type: someType,
x: someXCoordinate,
y: someYCoordinate,
},
anotherId: {
// ...
}
}
}
You can add new type subtrees, new states and new properties to the cellData
as you like. So long as you follow the structure above, the items will show
up on the front end in the relevant cell in our world (see the handleCellData function in index.html).
Adding new items to the cell is easy.
For example, to add a new coin to the cell, you just need to add a state object to the coin subtree (E.g):
cellData.coin[coin.id] = coin;
See how coinManager.addCoin is used below (and how it's implemented) for more details.
Note that states which are close to our current cell (based on WORLD_CELL_OVERLAP_DISTANCE)
but not exactly inside it will still be visible within this cell (they will have an additional
'external' property set to true).
External states should not be modified because they belong to a different cell and the change will be ignored.
*/
var _ = require('lodash');
var rbush = require('rbush');
var SAT = require('sat');
var config = require('./config');
var BotManager = require('./bot-manager').BotManager;
var CoinManager = require('./coin-manager').CoinManager;
// This controller will be instantiated once for each
// cell in our world grid.
var CellController = function (options, util) {
var self = this;
this.options = options;
this.cellIndex = options.cellIndex;
this.util = util;
// You can use the exchange object to publish data to global channels which you
// can watch on the front end (in index.html).
// The API for the exchange object is here: http://socketcluster.io/#!/docs/api-exchange
// To receive channel data on the front end, you can read about the subscribe and watch
// functions here: http://socketcluster.io/#!/docs/basic-usage
this.exchange = options.worker.exchange;
this.worldColCount = Math.ceil(config.WORLD_WIDTH / config.WORLD_CELL_WIDTH);
this.worldRowCount = Math.ceil(config.WORLD_HEIGHT / config.WORLD_CELL_HEIGHT);
this.worldCellCount = this.worldColCount * this.worldRowCount;
this.workerCount = options.worker.options.workers;
this.coinMaxCount = Math.round(config.COIN_MAX_COUNT / this.worldCellCount);
this.coinDropInterval = config.COIN_DROP_INTERVAL * this.worldCellCount;
this.botCount = Math.round(config.BOT_COUNT / this.worldCellCount);
var cellData = options.cellData;
this.botManager = new BotManager({
worldWidth: config.WORLD_WIDTH,
worldHeight: config.WORLD_HEIGHT,
botDefaultDiameter: config.BOT_DEFAULT_DIAMETER,
botMoveSpeed: config.BOT_MOVE_SPEED,
botMass: config.BOT_MASS,
botChangeDirectionProbability: config.BOT_CHANGE_DIRECTION_PROBABILITY
});
if (!cellData.player) {
cellData.player = {};
}
for (var b = 0; b < this.botCount; b++) {
var bot = this.botManager.addBot();
cellData.player[bot.id] = bot;
}
this.botMoves = [
{u: 1},
{d: 1},
{r: 1},
{l: 1}
];
this.coinManager = new CoinManager({
cellData: options.cellData,
cellBounds: options.cellBounds,
playerNoDropRadius: config.COIN_PLAYER_NO_DROP_RADIUS,
coinMaxCount: this.coinMaxCount,
coinDropInterval: this.coinDropInterval
});
this.lastCoinDrop = 0;
config.COIN_TYPES.sort(function (a, b) {
if (a.probability < b.probability) {
return -1;
}
if (a.probability > b.probability) {
return 1;
}
return 0;
});
this.coinTypes = [];
var probRangeStart = 0;
config.COIN_TYPES.forEach(function (coinType) {
var coinTypeClone = _.cloneDeep(coinType);
coinTypeClone.prob = probRangeStart;
self.coinTypes.push(coinTypeClone);
probRangeStart += coinType.probability;
});
this.playerCompareFn = function (a, b) {
if (a.id < b.id) {
return -1;
}
if (a.id > b.id) {
return 1;
}
return 0;
};
this.diagonalSpeedFactor = Math.sqrt(1 / 2);
};
/*
The main run loop for our cell controller.
*/
CellController.prototype.run = function (cellData) {
if (!cellData.player) {
cellData.player = {};
}
if (!cellData.coin) {
cellData.coin = {};
}
var players = cellData.player;
var coins = cellData.coin;
// Sorting is important to achieve consistency across cells.
var playerIds = Object.keys(players).sort(this.playerCompareFn);
this.findPlayerOverlaps(playerIds, players, coins);
this.dropCoins(coins);
this.generateBotOps(playerIds, players);
this.applyPlayerOps(playerIds, players, coins);
};
CellController.prototype.dropCoins = function (coins) {
var now = Date.now();
if (now - this.lastCoinDrop >= this.coinManager.coinDropInterval &&
this.coinManager.coinCount < this.coinManager.coinMaxCount) {
this.lastCoinDrop = now;
var rand = Math.random();
var chosenCoinType;
var numTypes = this.coinTypes.length;
for (var i = numTypes - 1; i >= 0; i--) {
var curCoinType = this.coinTypes[i];
if (rand >= curCoinType.prob) {
chosenCoinType = curCoinType;
break;
}
}
if (!chosenCoinType) {
throw new Error('There is something wrong with the coin probability distribution. ' +
'Check that probabilities add up to 1 in COIN_TYPES config option.');
}
var coin = this.coinManager.addCoin(chosenCoinType.value, chosenCoinType.type, chosenCoinType.radius);
if (coin) {
coins[coin.id] = coin;
}
}
};
CellController.prototype.generateBotOps = function (playerIds, players, coins) {
var self = this;
playerIds.forEach(function (playerId) {
var player = players[playerId];
// States which are external are managed by a different cell, therefore changes made to these
// states are not saved unless they are grouped with one or more internal states from the current cell.
// See util.groupStates() method near the bottom of this file for details.
if (player.subtype == 'bot' && !player.external) {
var radius = Math.round(player.diam / 2);
var isBotOnEdge = player.x <= radius || player.x >= config.WORLD_WIDTH - radius ||
player.y <= radius || player.y >= config.WORLD_HEIGHT - radius;
if (Math.random() <= player.changeDirProb || isBotOnEdge) {
var randIndex = Math.floor(Math.random() * self.botMoves.length);
player.repeatOp = self.botMoves[randIndex];
}
if (player.repeatOp) {
player.op = player.repeatOp;
}
}
});
};
CellController.prototype.keepPlayerOnGrid = function (player) {
var radius = Math.round(player.diam / 2);
var leftX = player.x - radius;
var rightX = player.x + radius;
var topY = player.y - radius;
var bottomY = player.y + radius;
if (leftX < 0) {
player.x = radius;
} else if (rightX > config.WORLD_WIDTH) {
player.x = config.WORLD_WIDTH - radius;
}
if (topY < 0) {
player.y = radius;
} else if (bottomY > config.WORLD_HEIGHT) {
player.y = config.WORLD_HEIGHT - radius;
}
};
CellController.prototype.applyPlayerOps = function (playerIds, players, coins) {
var self = this;
playerIds.forEach(function (playerId) {
var player = players[playerId];
var playerOp = player.op;
var moveSpeed;
if (player.subtype == 'bot') {
moveSpeed = player.speed;
} else {
moveSpeed = config.PLAYER_DEFAULT_MOVE_SPEED;
}
if (playerOp) {
var movementVector = {x: 0, y: 0};
var movedHorizontally = false;
var movedVertically = false;
if (playerOp.u) {
movementVector.y = -moveSpeed;
player.direction = 'up';
movedVertically = true;
}
if (playerOp.d) {
movementVector.y = moveSpeed;
player.direction = 'down';
movedVertically = true;
}
if (playerOp.r) {
movementVector.x = moveSpeed;
player.direction = 'right';
movedHorizontally = true;
}
if (playerOp.l) {
movementVector.x = -moveSpeed;
player.direction = 'left';
movedHorizontally = true;
}
if (movedHorizontally && movedVertically) {
movementVector.x *= self.diagonalSpeedFactor;
movementVector.y *= self.diagonalSpeedFactor;
}
player.x += movementVector.x;
player.y += movementVector.y;
}
if (player.playerOverlaps) {
player.playerOverlaps.forEach(function (otherPlayer) {
self.resolvePlayerCollision(player, otherPlayer);
self.keepPlayerOnGrid(otherPlayer);
});
delete player.playerOverlaps;
}
if (player.coinOverlaps) {
player.coinOverlaps.forEach(function (coin) {
if (self.testCircleCollision(player, coin).collided) {
player.score += coin.v;
self.coinManager.removeCoin(coin.id);
}
});
delete player.coinOverlaps;
}
self.keepPlayerOnGrid(player);
});
};
CellController.prototype.findPlayerOverlaps = function (playerIds, players, coins) {
var self = this;
var playerTree = new rbush();
var hitAreaList = [];
playerIds.forEach(function (playerId) {
var player = players[playerId];
player.hitArea = self.generateHitArea(player);
hitAreaList.push(player.hitArea);
});
playerTree.load(hitAreaList);
playerIds.forEach(function (playerId) {
var player = players[playerId];
playerTree.remove(player.hitArea);
var hitList = playerTree.search(player.hitArea);
playerTree.insert(player.hitArea);
hitList.forEach(function (hit) {
if (!player.playerOverlaps) {
player.playerOverlaps = [];
}
player.playerOverlaps.push(hit.target);
});
});
var coinIds = Object.keys(coins);
coinIds.forEach(function (coinId) {
var coin = coins[coinId];
var coinHitArea = self.generateHitArea(coin);
var hitList = playerTree.search(coinHitArea);
if (hitList.length) {
// If multiple players hit the coin, give it to a random one.
var randomIndex = Math.floor(Math.random() * hitList.length);
var coinWinner = hitList[randomIndex].target;
if (!coinWinner.coinOverlaps) {
coinWinner.coinOverlaps = [];
}
coinWinner.coinOverlaps.push(coin);
}
});
playerIds.forEach(function (playerId) {
delete players[playerId].hitArea;
});
};
CellController.prototype.generateHitArea = function (target) {
var targetRadius = target.r || Math.round(target.diam / 2);
return {
target: target,
minX: target.x - targetRadius,
minY: target.y - targetRadius,
maxX: target.x + targetRadius,
maxY: target.y + targetRadius
};
};
CellController.prototype.testCircleCollision = function (a, b) {
var radiusA = a.r || Math.round(a.diam / 2);
var radiusB = b.r || Math.round(b.diam / 2);
var circleA = new SAT.Circle(new SAT.Vector(a.x, a.y), radiusA);
var circleB = new SAT.Circle(new SAT.Vector(b.x, b.y), radiusB);
var response = new SAT.Response();
var collided = SAT.testCircleCircle(circleA, circleB, response);
return {
collided: collided,
overlapV: response.overlapV
};
};
CellController.prototype.resolvePlayerCollision = function (player, otherPlayer) {
var result = this.testCircleCollision(player, otherPlayer);
if (result.collided) {
var olv = result.overlapV;
var totalMass = player.mass + otherPlayer.mass;
var playerBuff = player.mass / totalMass;
var otherPlayerBuff = otherPlayer.mass / totalMass;
player.x -= olv.x * otherPlayerBuff;
player.y -= olv.y * otherPlayerBuff;
otherPlayer.x += olv.x * playerBuff;
otherPlayer.y += olv.y * playerBuff;
/*
Whenever we have one state affecting the (x, y) coordinates of
another state, we should group them together using the util.groupStates() function.
Otherwise we will may get flicker when the two states interact across
a cell boundary.
In this case, if we don't use groupStates(), there will be flickering when you
try to push another player across to a different cell.
*/
this.util.groupStates([player, otherPlayer]);
}
};
module.exports = CellController;