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

@ -108,12 +108,9 @@ Canvas.prototype.render = function () {
let imgData = this.ctx.getImageData(0, 0, this.width, this.height); let imgData = this.ctx.getImageData(0, 0, this.width, this.height);
let pixels = imgData.data; let pixels = imgData.data;
let int = Math.ceil(this.width * this.height / 8); let int = (this.width * this.height)
for (let j = 0; j < 8; j++) {
let that = this; let that = this;
(async function () { for (let i = 0; i < int; i++) {
for (let i = j * int; i < (j + 1) * int; i++) {
if (i > that.width * that.height) break;
let x = Math.floor(i / that.height); let x = Math.floor(i / that.height);
let y = i % that.height; let y = i % that.height;
@ -128,9 +125,11 @@ Canvas.prototype.render = function () {
let val = (temp + 310) / 310; let val = (temp + 310) / 310;
if (val < -2.861) val = -2.861; if (val < -2.861) val = -2.861;
pixels[i2 * 4] = (block.color[0] - temp / 1e28) * val; let td = temp / 1e28;
pixels[i2 * 4 + 1] = (block.color[1] - temp / 1e28) * (val * 0.259 + 0.741);
pixels[i2 * 4 + 2] = (block.color[2] - temp / 1e28) * (val * 0.023 + 0.977); pixels[i2 * 4] = (block.color[0] - td) * val;
pixels[i2 * 4 + 1] = (block.color[1] - td) * (val * 0.259 + 0.741);
pixels[i2 * 4 + 2] = (block.color[2] - td) * (val * 0.023 + 0.977);
pixels[i2 * 4 + 3] = block.color[3] * 255 + Math.abs(val - 1) * 100 || 255; pixels[i2 * 4 + 3] = block.color[3] * 255 + Math.abs(val - 1) * 100 || 255;
} else { } else {
let lg = Math.log(temp); let lg = Math.log(temp);
@ -140,8 +139,6 @@ Canvas.prototype.render = function () {
pixels[i2 * 4 + 3] = 255; pixels[i2 * 4 + 3] = 255;
} }
} }
})()
}
/* TODO: clean up */ /* TODO: clean up */
@ -235,13 +232,8 @@ var canvas = new Canvas(Math.floor(window.innerWidth / 4.5), Math.floor(window.i
var handler = new TickHandler(canvas); var handler = new TickHandler(canvas);
setInterval(() => { setInterval(() => {
if (canvas.clicked) canvas.click()
canvas.render();
if (canvas.stopNow) return; if (canvas.stopNow) return;
handler.tick(); handler.tick();
}, 1000 / 60); }, 1000 / 60);
setInterval(() => {
if (canvas.stopNow) return;
if (canvas.clicked) canvas.click();
this.canvas.render();
}, 1000 / 60);

View file

@ -247,6 +247,9 @@ let air = mainTiles.resolveID('Vanilla/Air', 'Air');
setTimeout(function () { setTimeout(function () {
for (let tile in mainTiles.tiles) { for (let tile in mainTiles.tiles) {
mainTiles.tiles[tile].hook ? mainTiles.tiles[tile].hook() : false; let h = mainTiles.tiles[tile].hook;
for (let hh of h) {
hh();
}
} }
}, 400) }, 400)

View file

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

View file

@ -16,15 +16,15 @@ function temperature(event, conduct, transfer = 0) {
for (let x = -1; x < 2; x ++) { for (let x = -1; x < 2; x ++) {
for (let y = -1; y < 2; y++) { for (let y = -1; y < 2; y++) {
let blok = chunks.getBlock(cx + x, cy + y);
let temp = chunks.getBlock(cx, cy, true); let temp = chunks.getBlock(cx, cy, true);
let temp2 = chunks.getBlock(cx + x, cy + y, true); let temp2 = chunks.getBlock(cx + x, cy + y, true);
if (temp2 == undefined || temp == undefined || (x == 0 && y == 0)) { if (temp2 == undefined || temp == undefined || (x == 0 && y == 0) || Math.abs(temp2 - temp) < 0.03) {
if (temp != undefined) chunks.setBlock(cx, cy, Math.max(temp,-296.15), true);
continue; continue;
}; };
let blok = chunks.getBlock(cx + x, cy + y);
let conduct2 = (blok != -1) ? (mainTiles.tiles[blok].attributes.conduct || 0) : 0; let conduct2 = (blok != -1) ? (mainTiles.tiles[blok].attributes.conduct || 0) : 0;
let conductSum = Math.min(conduct * conduct2 * 10, 0.5); let conductSum = Math.min(conduct * conduct2 * 10, 0.5);

View file

@ -20,14 +20,15 @@ function getContrast(r, g, b) {
} }
function Tile(color, id, isAbstract = false, copyFrom = false, copyFromName = false) { function Tile(color, id, isAbstract = false, copyFrom = false, copyFromName = false) {
this.hook = this.hook || [];
if (copyFrom) { if (copyFrom) {
let that = this; let that = this;
this.hook = function () { this.hook.push(function () {
let oldie = mainTiles.resolve(copyFrom, copyFromName); let oldie = mainTiles.resolve(copyFrom, copyFromName);
Object.assign(that, Object.assign({}, oldie, that)); Object.assign(that, Object.assign({}, oldie, that));
that.attributes = Object.assign({}, that.attributes); that.attributes = Object.assign({}, that.attributes);
that.interactions = that.interactions.concat(oldie.interactions) that.interactions = that.interactions.concat(oldie.interactions)
} })
} }
this.color = color; this.color = color;