change colonization physics

This commit is contained in:
biglyderv 2025-05-07 20:15:48 -04:00
parent dbb92fc4b6
commit f740c348e9
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5

View file

@ -8,10 +8,10 @@ const BIASES = {
0x46aafa: 0.4, 0x46aafa: 0.4,
0x0078ff: 0.35, 0x0078ff: 0.35,
0x0000ff: 0.3, 0x0000ff: 0.3,
0xf5a500: 0.15, 0xf5a500: 0.25,
0xff0000: 0.1, 0xff0000: 0.2,
0x666666: 0.01, 0x666666: 0.15,
0xb2b2b2: 0.02 0xb2b2b2: 0.1
}; };
function validXY(x, y) { function validXY(x, y) {
@ -33,12 +33,26 @@ function setRGBA(data, i, input) {
} }
} }
function isLand(data) { function isLand(data) {
if (data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255) return false; if (data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255) return false;
if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 255) return false; if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 255) return false;
return true; return true;
} }
function shuffle(array) {
let currentIndex = array.length;
while (currentIndex != 0) {
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
}
function Game(baseMap) { function Game(baseMap) {
this.img = new Image(); this.img = new Image();
this.baseMap = baseMap; this.baseMap = baseMap;
@ -66,42 +80,43 @@ Game.prototype.main = function () {
if (isLand(rgba)) if (isLand(rgba))
setRGBA(data, i, this.colors[colonized[i]]); setRGBA(data, i, this.colors[colonized[i]]);
let weight = 11;
let broken = false;
let i2 = i; let i2 = i;
for (let dx = -1; dx <= 1; dx++) { let p = [[-1, -1], [-1, 1], [1, 1], [1, -1], [0, 1], [1, 0], [-1, 0], [0, -1]]
for (let dy = -1; dy <= 1; dy++) {
weight--;
let x2 = x + dx;
let y2 = y + dy;
let i = x2 + y2 * width; shuffle(p);
if (!validXY(x2, y2)) continue; for (let ni in p) {
let rgba = getRGBA(biomes.data, i, out); let n = p[ni];
let rgbaH = rgba[0] * 0x10000 + rgba[1] * 0x100 + rgba[2]; let dx = n[0];
let dy = n[1];
let x2 = x + dx;
let y2 = y + dy;
let bias = ((rgbaH in BIASES) ? BIASES[rgbaH] : 0.2) / weight; if (colonized[y2 * width + x2] == colonized[i2]) {
if (colonized[y2 * width + x2] != 0) {
bias *= this.aggression[colonized[i]];
}
if (Math.random() > bias) {
continue;
}
if (colonized[y2 * width + x2] == colonized[i2]) {
continue;
}
colonized[y2 * width + x2] = colonized[i2];
this.strength[colonized[i]]++;
broken = true;
break; break;
} }
if (broken) break;
let i = x2 + y2 * width;
if (!validXY(x2, y2)) continue;
let rgba = getRGBA(biomes.data, i, out);
let rgbaH = rgba[0] * 0x10000 + rgba[1] * 0x100 + rgba[2];
let bias = ((rgbaH in BIASES) ? BIASES[rgbaH] : 0.2);
if (colonized[y2 * width + x2] != 0) {
bias *= this.aggression[colonized[i]];
}
if (Math.random() > bias && ni != p.length - 1) {
continue;
}
colonized[y2 * width + x2] = colonized[i2];
this.strength[colonized[i]]++;
this.strength[colonized[y2 * width + x2]]--;
break;
} }
} }
@ -139,11 +154,19 @@ Game.prototype.place = function () {
let rgba = getRGBA(biomes.data, i, out); let rgba = getRGBA(biomes.data, i, out);
let rgbaH = rgba[0] * 0x10000 + rgba[1] * 0x100 + rgba[2];
let bias = ((rgbaH in BIASES) ? BIASES[rgbaH] : 0.2);
if (Math.random() > bias ** 4) {
continue;
}
if (isLand(rgba)) break; if (isLand(rgba)) break;
} }
colonized[i] = count; colonized[i] = count;
this.aggression[count] = Math.random(); this.aggression[count] = (Math.random() ** 6);
this.colors[count] = [Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), 255]; this.colors[count] = [Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), 255];
this.strength[count] = 1; this.strength[count] = 1;
} }
@ -151,7 +174,7 @@ Game.prototype.place = function () {
Game.prototype.loop = function () { Game.prototype.loop = function () {
let that = this; let that = this;
setInterval(function () { that.main() }, 1000 / 60); setInterval(async function () { that.main() }, 1000 / 10);
} }
Game.prototype.startGame = function () { Game.prototype.startGame = function () {