slight optimizations

This commit is contained in:
biglyderv 2025-05-16 21:44:56 -04:00
parent 0fb19d45ad
commit 66d0642006
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
5 changed files with 77 additions and 71 deletions

View file

@ -5,81 +5,87 @@
fluid: Spread of substance
saturation: Maximum mass for gravity reactions to occur
*/
let gravityTable = [];
function gravity(event, mass, fluid, saturation) {
if (event.type != 'tick') return;
let cx = event.data[0];
let cy = event.data[1];
let chunks = event.canvas;
let dir = [0, 0];
let force = [0,0];
let dirX = 0;
let dirY = 0;
let forceX = 0;
let forceY = 0;
let density = 0;
let currBlock = chunks.getBlock(cx, cy);
for (let x = -1; x < 2; x ++) {
for (let x = -1; x < 2; x++) {
let x2 = x / fluid;
let x3 = x2 * x2;
for (let y = -1; y < 2; y++) {
let blok = chunks.getBlock(cx + x, cy + y);
if (blok == -1) continue;
let mass2 = mainTiles.tiles[blok].attributes.mass;
let mass2 = gravityTable[blok];
density += mass2;
if (density > saturation) break;
if (blok == currBlock) continue;
if (blok == currBlock || mass == mass2) continue;
let massDiff = (mass / mass2) - (mass2 / mass);
let x2 = x / fluid;
let dirDiff = (y - 1 + fluid) / (1/8 * (x2 * x2 - y * y + 8) * (x2 * x2 + y * y));
if (Math.abs(massDiff) < 0.001) continue;
let y3 = y * y;
let dirDiff = (y - 1 + fluid);
if (y == 0 && x == 0) dirDiff = 0;
if (isNaN(dirDiff)) dirDiff = 0;
if (dirDiff != 0)
dirDiff /= (1 / 8 * (x3 - y3 + 8) * (x3 + y3));
force[0] += massDiff * x2;
force[1] += massDiff * dirDiff * y;
if (isNaN(dirDiff)) dirDiff = 0;
forceX += massDiff * x2;
forceY += massDiff * dirDiff * y;
}
if (density > saturation) break;
}
dir[0] = (Math.abs(force[0]) < .5) ? 0 : Math.sign(force[0]);
dir[1] = (Math.abs(force[1]) < .5) ? 0 : Math.sign(force[1]);
dirX = (Math.abs(forceX) < .5) ? 0 : Math.sign(forceX);
dirY = (Math.abs(forceY) < .5) ? 0 : Math.sign(forceY);
if (density > saturation ) {
if (density > saturation) {
return;
}
let offBlock = chunks.getBlock(cx + dir[0], cy + dir[1]);
let offBlock = chunks.getBlock(cx + dirX, cy + dirY);
if (currBlock == offBlock && density <= saturation) {
dir[0] = Math.sign(force[0]);
offBlock = chunks.getBlock(cx + dir[0], cy + dir[1]);
dirX = Math.sign(forceX);
offBlock = chunks.getBlock(cx + dirX, cy + dirY);
}
if (currBlock == -1 || offBlock == -1 || currBlock == offBlock ||offBlock == undefined || mainTiles.tiles[offBlock].attributes.saturation / 9 < mass || chunks.noTick[(cx+dir[0])*chunks.height + (cy+dir[1])]) return;
if (currBlock == -1 || offBlock == -1 || currBlock == offBlock || offBlock == undefined || mainTiles.tiles[offBlock].attributes.saturation / 9 < mass || chunks.noTick[(cx + dirX) * chunks.height + (cy + dirY)]) return;
if (!canGravity[offBlock]) return;
chunks.noTick[cx*chunks.height + cy] = true;
chunks.noTick[(cx+dir[0])*chunks.height + (cy+dir[1])] = true;
chunks.noTick[cx * chunks.height + cy] = true;
chunks.noTick[(cx + dirX) * chunks.height + (cy + dirY)] = true;
chunks.setBlock(cx, cy, offBlock);
chunks.setBlock(cx + dir[0], cy + dir[1], currBlock);
chunks.setBlock(cx + dirX, cy + dirY, currBlock);
let t = chunks.getBlock(cx, cy,true);
let t2 = chunks.getBlock(cx + dir[0], cy + dir[1],true);
if (t != undefined && t2 != undefined) {
chunks.setBlock(cx, cy,t2, true);
chunks.setBlock(cx + dir[0], cy + dir[1], t, true);
}
let t = chunks.getBlock(cx, cy, true);
let t2 = chunks.getBlock(cx + dirX, cy + dirY, true);
chunks.setBlock(cx, cy, t2, true);
chunks.setBlock(cx + dirX, cy + dirY, t, true);
return true;
}
@ -90,6 +96,10 @@ Tile.prototype.gravity = function (mass, fluid, saturation) {
});
this.attributes.mass = mass;
this.attributes.saturation = saturation;
let that = this;
this.hook.push(function () {
gravityTable[mainTiles.tiles.findIndex((x, i) => x.id == that.id)] = that.attributes.mass;
})
return this;
}