Skip to content

Commit

Permalink
fix navMesh and more lag testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LegendaryLHL committed Sep 18, 2024
1 parent 84ab0c1 commit 7a3188d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
68 changes: 60 additions & 8 deletions sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ let polluteRate = 10;
let lastPollute;

// Game tick
const maxDelta = 4;
let maxDelta = 4;
const maxDeltaTime = 200;
let delta = 0;
const tickSpeed = 120;
const msBetweenTicks = 1000 / tickSpeed;
let tps = 0;
let ticks = 0;
let timeSinceLastSecond = 0;
let lagRecord = false;
let stepAmmount = 1;
let lastSecond = Date.now();
let lastTick = 0;

function windowResized() {
let changeInWidth = window.innerWidth / winWidth;
Expand Down Expand Up @@ -133,12 +136,28 @@ function draw() {
}
}

timeSinceLastSecond += deltaTime;
if (timeSinceLastSecond >= 1000) {
tps = ticks;
ticks = 0;
timeSinceLastSecond = 0;
console.log("TPS:", tps, "Lumberjack count:", lumberjackCount, "lag", tickSpeed - tps, "lag/lum:", (tickSpeed - tps) / lumberjackCount);
const timePassed = Date.now() - lastSecond;
if (timePassed >= 1000) {
const tickPassed = ticks - lastTick;
const tps = (tickPassed / timePassed * 1000).toFixed(2);
const lag = (tickSpeed - tps).toFixed(2);
const lagPerLumberjack = (lag / lumberjackCount).toFixed(2);
if (lagRecord) {
console.log("TPS:", tps, "Lumberjack count:", lumberjackCount, "lag", lag, "lag/lum:", lagPerLumberjack);
const lagPercent = lag / tickSpeed * 100;
if (lagPercent > 80) {
console.log("Lag rached 80% of tick speed, stepAmmount:", stepAmmount, "maxDelta:", maxDelta, "range:", Lumberjack.pathFindRange);
console.log("Reached number of lumberjack: ", lumberjackCount);
lagRecord = false;
}
let center = { x: sprout.x, y: sprout.y };
for (let i = 0; i < stepAmmount; i++) {
// attempt to append new lumberjack
while (!appendSprite(new Lumberjack(center.x + randint(-Lumberjack.pathFindRange * tileSize, Lumberjack.pathFindRange * tileSize), center.y + randint(-Lumberjack.pathFindRange * tileSize, Lumberjack.pathFindRange * tileSize))));
}
}
lastSecond = Date.now();
lastTick = ticks;
}

drawGridLine();
Expand Down Expand Up @@ -294,6 +313,21 @@ function unappendSprite(sprite) {
sprite.checkMapChange(true);
}

// empty every sprite except sprout
function resetSprites() {
for (const sprite of sprites) {
if (sprite.onTile) {
sprite.tile.remove();
}
}
sprites.length = 0;
mapChangedWatch.length = 0;
navMesh.clear();
initNavMesh();
appendSprite(sprout);
sprout.checkMapChange(true);
}

function mapChanged() {
for (const sprite of mapChangedWatch) {
sprite.mapChanged = true;
Expand Down Expand Up @@ -396,6 +430,24 @@ function closeFullscreen() {
}
}

function lagProfileTest(steps = null, maxDeltaAsign = null, range = null) {
resetSprites();
lagRecord = true;
stepAmmount = steps || stepAmmount;
maxDelta = maxDeltaAsign || maxDelta;
Lumberjack.pathFindRange = range || Lumberjack.pathFindRange;

let centerTileCoord = { x: gridWidth / 2, y: gridHeight / 2 };
let center = centerFromCoord(centerTileCoord.x, centerTileCoord.y);
sprout.x = center.x;
sprout.y = center.y;
let neighbors = findNeighbour(centerTileCoord);
for (const neighbor of neighbors) {
appendSprite(new Rock(0, 0), tileGrid[neighbor.y][neighbor.x]);
}
return "Test started until reached 80% lag";
}

/**
* @typedef {Object} Class - Just the class
* @param {BaseSprite} sprite - The instance of the sprite to start from
Expand Down
5 changes: 2 additions & 3 deletions sprites/baseSprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BaseSprite {

static ref() {
if (!BaseSprite._ref) {
BaseSprite._ref = new BaseSprite(0, 0);
BaseSprite._ref = new this(0, 0);
}
return BaseSprite._ref;
}
Expand Down Expand Up @@ -100,14 +100,13 @@ class BaseSprite {

// hard coded for lumberjack
let tileToCheck = findNeighbour(this.tile);
let center = this.tile.center();
navMesh.set(this.tile, Lumberjack.ref().isCollidingInRange(center.x, center.y, Lumberjack.ref().collide_range + tileSize));

const processTile = (tile) => {
let center = centerFromCoord(tile.x, tile.y);
navMesh.set(tileGrid[tile.y][tile.x], Lumberjack.ref().isCollidingInRange(center.x, center.y, Lumberjack.ref().collide_range + tileSize));
};

processTile(this.tile);
oldTileToCheck.forEach(tile => processTile(tile));
tileToCheck.forEach(tile => processTile(tile));

Expand Down
3 changes: 2 additions & 1 deletion sprites/lumberjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Lumberjack extends BaseSprite {
this.maxIdleTime = 5000;
}

static pathFindRange = 20;

_tick() {
this.pathfindCooldown -= this.deltaTime;
Expand Down Expand Up @@ -52,7 +53,7 @@ class Lumberjack extends BaseSprite {
}
else {
this.mapChanged = false;
this.path = pathFind(tileSize * 20, this, Tree, Sprout, Police);
this.path = pathFind(tileSize * Lumberjack.PathFindRange, this, Tree, Sprout, Police);
}
}

Expand Down

0 comments on commit 7a3188d

Please sign in to comment.