requested changes

This commit is contained in:
kruschen 2024-09-08 17:09:24 +00:00
parent 8f78e460fe
commit 6b5d3fd67e
3 changed files with 39 additions and 32 deletions

View file

@ -1,22 +1,16 @@
import { getGridPolygon } from "utils/graphUtils";
import { P, normalize, rn, last } from "utils";
import { clipPoly } from "utils/lineUtils";
import { ERROR } from "config/logging";
import * as d3 from "d3";
import { byId } from "utils/nodeUtils";
export function drawIce() {
const ice = d3.select("#ice");
const ice = byId("ice");
const { ice: icePack } = pack;
let innerHTML = "";
for (const shield of icePack.iceShields) {
ice
.append("polygon")
.attr("points", shield.points.toString())
innerHTML += `<polygon points="${shield.points.toString()}" />`;
}
for (const iceberg of icePack.icebergs) {
ice
.append("polygon")
.attr("points", iceberg.points.toString())
innerHTML += `<polygon points="${iceberg.points.toString()}" />`;
}
ice.innerHTML = innerHTML;
}

View file

@ -1,6 +1,5 @@
import { ERROR } from "config/logging";
import { aleaPRNG } from "scripts/aleaPRNG";
import { IIce, Iiceberg } from "types/pack/ice";
import { clipPoly, last, normalize, P, rn } from "utils";
export function generateIce(
@ -8,7 +7,7 @@ export function generateIce(
vertices: IGraphVertices,
temp: Int8Array,
features: TGridFeatures,
gridCells: Pick<IGrid["cells"], "f" | "t">,
gridCells: Pick<IGrid["cells"], "f" | "t">
): IIce {
const shieldMin = -8; // max temp to form ice shield (glacier)
const icebergMax = 1; // max temp to form an iceberg
@ -34,12 +33,16 @@ export function generateIce(
const chain = connectVertices(vertex);
if (chain.length < 3) continue;
const points = clipPoly(chain.map((v) => vertices.p[v]));
icePack.iceShields.push({ points, type: "iceShield" });
icePack.iceShields.push({ points, transform: { x: 0, y: 0 } });
continue;
}
// mildly cold: iceberd
if (P(normalize(temperature, -7, 2.5))) continue; // t[-5; 2] cold: skip some cells
if (gridCells.f[i] !== 0 && (features[gridCells.f[i]] as IGridFeature).type === "lake") continue; // lake: no icebers // MARKER as IGridFeature
if (
gridCells.f[i] !== 0 &&
(features[gridCells.f[i]] as IGridFeature).type === "lake"
)
continue; // lake: no icebers // MARKER as IGridFeature
let size = (6.5 + temperature) / 10; // iceberg size: 0 = full size, 1 = zero size
if (gridCells.t[i] === -1) size *= 1.3; // coasline: smaller icebers
size = Math.min(size * (0.4 + Math.random() * 1.2), 0.95); // randomize iceberg size
@ -48,13 +51,20 @@ export function generateIce(
return icePack;
// Helper functions
function generateIceberg(i: number, size: number): Iiceberg {
function generateIceberg(i: number, size: number): IiceBerg {
const cellMidPoint = cells.p[i];
const points = cells.v[i].map(v => vertices.p[v]).map((point) => [
(point[0] + (cellMidPoint[0] - point[0]) * size) | 0,
(point[1] + (cellMidPoint[1] - point[1]) * size) | 0,
]);
return { points, cell: i, size: rn(1 - size, 2) };
const points: TPoints = cells.v[i]
.map((v) => vertices.p[v])
.map((point) => [
(point[0] + (cellMidPoint[0] - point[0]) * size) | 0,
(point[1] + (cellMidPoint[1] - point[1]) * size) | 0,
]);
return {
points,
transform: { x: 0, y: 0 },
cell: i,
size: rn(1 - size, 2),
};
}
// connect vertices to chain
@ -71,9 +81,12 @@ export function generateIce(
currentVertex
.filter((cellIndicie) => temp[cellIndicie] <= shieldMin)
.forEach((cellIndice) => (used[cellIndice] = 1));
const c0 = currentVertex[0] >= nOfCells || temp[currentVertex[0]] > shieldMin;
const c1 = currentVertex[1] >= nOfCells || temp[currentVertex[1]] > shieldMin;
const c2 = currentVertex[2] >= nOfCells || temp[currentVertex[2]] > shieldMin;
const c0 =
currentVertex[0] >= nOfCells || temp[currentVertex[0]] > shieldMin;
const c1 =
currentVertex[1] >= nOfCells || temp[currentVertex[1]] > shieldMin;
const c2 =
currentVertex[2] >= nOfCells || temp[currentVertex[2]] > shieldMin;
const vertexNeighbors = vertices.v[current]; // neighboring vertices
if (vertexNeighbors[0] !== prev && c0 !== c1)
current = vertexNeighbors[0];

View file

@ -1,18 +1,18 @@
export interface IIceBase {
points: number[][];
interface IIceBase {
points: TPoints;
transform: {x: number, y: number};
}
export interface Iiceberg extends IIceBase {
interface IiceBerg extends IIceBase {
cell: number;
size: number;
}
export interface IiceShield extends IIceBase {
type: string;
interface IiceShield extends IIceBase {
}
export interface IIce{
icebergs: Iiceberg[];
interface IIce{
icebergs: IiceBerg[];
iceShields: IiceShield[];
}