diff --git a/js/index.js b/js/index.js index 6b7ec6e..c2b4bc2 100644 --- a/js/index.js +++ b/js/index.js @@ -8,10 +8,10 @@ const BIASES = { 0x46aafa: 0.4, 0x0078ff: 0.35, 0x0000ff: 0.3, - 0xf5a500: 0.15, - 0xff0000: 0.1, - 0x666666: 0.01, - 0xb2b2b2: 0.02 + 0xf5a500: 0.25, + 0xff0000: 0.2, + 0x666666: 0.15, + 0xb2b2b2: 0.1 }; function validXY(x, y) { @@ -33,12 +33,26 @@ function setRGBA(data, i, input) { } } + function isLand(data) { 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; 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) { this.img = new Image(); this.baseMap = baseMap; @@ -66,42 +80,43 @@ Game.prototype.main = function () { if (isLand(rgba)) setRGBA(data, i, this.colors[colonized[i]]); - let weight = 11; - - let broken = false; - let i2 = i; - for (let dx = -1; dx <= 1; dx++) { - for (let dy = -1; dy <= 1; dy++) { - weight--; - let x2 = x + dx; - let y2 = y + dy; + let p = [[-1, -1], [-1, 1], [1, 1], [1, -1], [0, 1], [1, 0], [-1, 0], [0, -1]] - let i = x2 + y2 * width; + shuffle(p); - if (!validXY(x2, y2)) continue; - let rgba = getRGBA(biomes.data, i, out); - let rgbaH = rgba[0] * 0x10000 + rgba[1] * 0x100 + rgba[2]; + for (let ni in p) { + let n = p[ni]; + 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] != 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; + if (colonized[y2 * width + x2] == colonized[i2]) { 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 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; } 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.strength[count] = 1; } @@ -151,7 +174,7 @@ Game.prototype.place = function () { Game.prototype.loop = function () { let that = this; - setInterval(function () { that.main() }, 1000 / 60); + setInterval(async function () { that.main() }, 1000 / 10); } Game.prototype.startGame = function () {