This commit is contained in:
biglyderv 2024-09-02 01:58:52 -04:00
parent 46f7964d07
commit ba526c16a5
No known key found for this signature in database
GPG key ID: 33AC87E9ACE66954
3 changed files with 55 additions and 31 deletions

BIN
base.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 210 KiB

After

Width:  |  Height:  |  Size: 441 KiB

View file

@ -8,7 +8,7 @@
<body> <body>
<section id='main'> <section id='main'>
<h1>MapTest</h1> <h1>MapTest</h1>
<canvas width='3200' height='1600' id='canvas' style='width: 800px; height: 400px;'> <canvas width='2160' height='1080' id='canvas' style='width: 800px; height: 400px;'>
</section> </section>
<script src='js/index.js'></script> <script src='js/index.js'></script>
</body> </body>

View file

@ -1,13 +1,22 @@
const width = 2160;
const height = 1080;
const BIASES = { const BIASES = {
0xc8ff50: 1, 0x000000: 1,
0x46aafa: 0.5, 0x64ff50: 0.5,
0xff0000: 0.75, 0xc8ff50: 0.45,
0x0000ff: 0.4, 0x46aafa: 0.4,
0x0078ff: 0.35,
0x0000ff: 0.3,
0xf5a500: 0.15,
0xff0000: 0.1,
0x666666: 0.01,
0xb2b2b2: 0.02
}; };
function validXY(x, y) { function validXY(x, y) {
if (x < 0 || x >= 3200) return false; if (x < 0 || x >= width) return false;
if (y < 0 || y >= 1600) return false; if (y < 0 || y >= height) return false;
return true; return true;
} }
@ -26,56 +35,64 @@ 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;
return true; return true;
} }
function Game(baseMap) { function Game(baseMap) {
this.img = new Image(); this.img = new Image();
this.baseMap = baseMap; this.baseMap = baseMap;
this.colors = {};
this.aggression = {};
} }
Game.prototype.main = function() { Game.prototype.main = function() {
var {biomes, ctx, colonized} = this; var {biomes, ctx, colonized} = this;
var dat = ctx.getImageData(0,0,3200,1600); var dat = ctx.getImageData(0,0,width,height);
var data = dat.data; var data = dat.data;
var out = new Uint8Array(4); var out = new Uint8Array(4);
for (let k = 0; k < 3200*1600/10; k++) { for (let k = 0; k < width*height; k++) {
let i = Math.floor(Math.random() * biomes.data.length / 4); let i = k;
if (!colonized[i]) continue; if (!colonized[i]) continue;
var x = i % 3200; var x = i % width;
var y = Math.floor(i / 3200); var y = Math.floor(i / width);
let rgba = getRGBA(biomes.data,i,out); let rgba = getRGBA(biomes.data,i,out);
if (isLand(rgba)) if (isLand(rgba))
setRGBA(data,i,[255,128,128,255]); setRGBA(data,i,this.colors[colonized[i]]);
let weight = 10; let weight = 10;
let broken = false; let broken = false;
let i2 = i;
for (let dx = -1; dx <= 1; dx++) { for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++ ) { for (let dy = -1; dy <= 1; dy++ ) {
weight--; weight--;
let x2 = x + dx; let x2 = x + dx;
let y2 = y + dy; let y2 = y + dy;
let i = x2 + y2 * 3200; let i = x2 + y2 * width;
if (!validXY(x2,y2)) continue; if (!validXY(x2,y2)) continue;
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 rgbaH = rgba[0] * 0x10000 + rgba[1] * 0x100 + rgba[2];
if (Math.random() > ((rgbaH in BIASES) ? BIASES[rgbaH] : 0.2) / weight) { 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; continue;
} }
if (colonized[y2 * 3200 + x2] == 1) { if (colonized[y2 * width + x2] == colonized[i2]) {
continue; continue;
} }
colonized[y2 * 3200 + x2] = 1; colonized[y2 * width + x2] = colonized[i2];
broken = true; broken = true;
break; break;
} }
@ -83,12 +100,12 @@ Game.prototype.main = function() {
} }
} }
ctx.putImageData(dat,0,0,0,0,3200,1600); ctx.putImageData(dat,0,0,0,0,width,height);
} }
Game.prototype.map = function() { Game.prototype.map = function() {
var {biomes, ctx} = this; var {biomes, ctx} = this;
var dat = ctx.getImageData(0,0,3200,1600); var dat = ctx.getImageData(0,0,width,height);
var data = dat.data; var data = dat.data;
var out = new Uint8Array(4); var out = new Uint8Array(4);
@ -101,8 +118,8 @@ Game.prototype.map = function() {
setRGBA(data,i,[0,0,0,255]); setRGBA(data,i,[0,0,0,255]);
} }
} }
ctx.putImageData(dat,0,0,0,0,3200,1600); ctx.putImageData(dat,0,0,0,0,width,height);
} }
Game.prototype.place = function() { Game.prototype.place = function() {
@ -111,14 +128,19 @@ Game.prototype.place = function() {
let i = 0; let i = 0;
while (true) { for (let count = 1; count < 101; count++) {
i = Math.floor(Math.random() * biomes.data.length / 4); while (true) {
i = Math.floor(Math.random() * biomes.data.length / 4);
let rgba = getRGBA(biomes.data,i,out); let rgba = getRGBA(biomes.data,i,out);
if (isLand(rgba)) break; if (isLand(rgba)) break;
} }
colonized[i] = 1;
colonized[i] = count;
this.aggression[count] = Math.random();
this.colors[count] = [Math.floor(Math.random() * 255),Math.floor(Math.random() * 255),Math.floor(Math.random() * 255),255];
}
} }
Game.prototype.loop = function() { Game.prototype.loop = function() {
@ -132,12 +154,14 @@ Game.prototype.startGame = function() {
this.ctx = ctx; this.ctx = ctx;
ctx.drawImage(this.img,0, 0,3200,1600); ctx.imageSmoothingEnabled = false;
ctx.drawImage(this.img,0, 0,width,height);
var dat = ctx.getImageData(0,0,3200,1600);
var dat = ctx.getImageData(0,0,width,height);
this.biomes = dat; this.biomes = dat;
this.colonized = new Uint8Array(3200*1600); this.colonized = new Uint8Array(width*height);
this.map(); this.map();
this.place(); this.place();