papercats/game-core.js
theKidOfArcrania 8352433fa5 Optimize flood-fill and fix OBOB + player bug
The flood-fill had allocated a good amount of memory ~1MB each time we visit a square. This is reduced to the grid size squared, only allocated if the square we are on now is valid.

The OBOB error occured when server was updating one time too few when a player is initialized.

The player bug occurs when a new player joins the game. Normally each new player gets about 60ish frames of lag to start up. However, the new player client incorrectly assumes all other players are new as well.
2017-03-01 00:56:56 +00:00

135 lines
3.3 KiB
JavaScript

var ANIMATE_FRAMES = 24;
var CELL_WIDTH = 40;
//TODO: remove constants.
exports.initPlayer = function(grid, player)
{
for (var dr = -1; dr <= 1; dr++)
for (var dc = -1; dc <= 1; dc++)
if (!grid.isOutOfBounds(dr + player.row, dc + player.col))
grid.set(dr + player.row, dc + player.col, player);
};
exports.updateFrame = function(grid, players, dead, notifyKill)
{
var adead = [];
if (dead instanceof Array)
adead = dead;
var kill;
if (!notifyKill)
kill = function() {};
else
kill = function(killer, other) {if (!removing[other]) notifyKill(killer, other);};
//Move players.
var tmp = players.filter(function(val) {
val.move();
if (val.dead)
adead.push(val);
return !val.dead;
});
//Remove players with collisions.
var removing = new Array(players.length);
for (var i = 0; i < players.length; i++)
{
for (var j = i; j < players.length; j++)
{
//Remove those players when other players have hit their tail.
if (!removing[j] && players[j].tail.hitsTail(players[i]))
{
kill(i, j);
removing[j] = true;
//console.log("TAIL");
}
if (!removing[i] && players[i].tail.hitsTail(players[j]))
{
kill(j, i);
removing[i] = true;
//console.log("TAIL");
}
//Remove players with collisons...
if (i !== j && squaresIntersect(players[i].posX, players[j].posX) &&
squaresIntersect(players[i].posY, players[j].posY))
{
//...if one player is own his own territory, the other is out.
if (grid.get(players[i].row, players[i].col) === players[i])
{
kill(i, j);
removing[j] = true;
}
else if (grid.get(players[j].row, players[j].col) === players[j])
{
kill(j, i);
removing[i] = true;
}
else
{
//...otherwise, the one that sustains most of the collision will be removed.
var areaI = area(players[i]);
var areaJ = area(players[j]);
if (areaI === areaJ)
{
kill(i, j);
kill(j, i);
removing[i] = removing[j] = true;
}
else if (areaI > areaJ)
{
kill(j, i);
removing[i] = true;
}
else
{
kill(i, j);
removing[j] = true;
}
}
}
}
}
tmp = tmp.filter(function(val, i) {
if (removing[i])
{
adead.push(val);
val.die();
}
return !removing[i];
});
players.length = tmp.length;
for (var i = 0; i < tmp.length; i++)
players[i] = tmp[i];
//Remove dead squares.
for (var r = 0; r < grid.size; r++)
{
for (var c = 0; c < grid.size; c++)
{
if (adead.indexOf(grid.get(r, c)) !== -1)
grid.set(r, c, null);
}
}
};
function squaresIntersect(a, b)
{
if (a < b)
return b < a + CELL_WIDTH;
else
return a < b + CELL_WIDTH;
}
function area(player)
{
var xDest = player.col * CELL_WIDTH;
var yDest = player.row * CELL_WIDTH;
if (player.posX === xDest)
return Math.abs(player.posY - yDest);
else
return Math.abs(player.posX - xDest);
}