mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-04-02 13:37:24 +02:00
refactor: migrate ice module
This commit is contained in:
parent
f48178a93c
commit
2018a9f01d
4 changed files with 37 additions and 48 deletions
|
|
@ -8494,7 +8494,6 @@
|
||||||
|
|
||||||
<script defer src="config/heightmap-templates.js"></script>
|
<script defer src="config/heightmap-templates.js"></script>
|
||||||
<script defer src="config/precreated-heightmaps.js"></script>
|
<script defer src="config/precreated-heightmaps.js"></script>
|
||||||
<script defer src="modules/ice.js?v=1.111.0"></script>
|
|
||||||
<script defer src="modules/military-generator.js?v=1.107.0"></script>
|
<script defer src="modules/military-generator.js?v=1.107.0"></script>
|
||||||
<script defer src="modules/markers-generator.js?v=1.107.0"></script>
|
<script defer src="modules/markers-generator.js?v=1.107.0"></script>
|
||||||
<script defer src="modules/coa-generator.js?v=1.99.00"></script>
|
<script defer src="modules/coa-generator.js?v=1.99.00"></script>
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,15 @@
|
||||||
"use strict";
|
import Alea from "alea";
|
||||||
|
import { min } from 'd3';
|
||||||
|
import { clipPoly, getGridPolygon, getIsolines, lerp, minmax, normalize, P, ra, rand, rn } from "../utils";
|
||||||
|
import { Point } from "./voronoi";
|
||||||
|
|
||||||
// Ice layer data model - separates ice data from SVG rendering
|
declare global {
|
||||||
window.Ice = (function () {
|
var Ice: IceModule;
|
||||||
|
}
|
||||||
|
|
||||||
|
class IceModule {
|
||||||
// Find next available id for new ice element idealy filling gaps
|
// Find next available id for new ice element idealy filling gaps
|
||||||
function getNextId() {
|
private getNextId() {
|
||||||
if (pack.ice.length === 0) return 0;
|
if (pack.ice.length === 0) return 0;
|
||||||
// find gaps in existing ids
|
// find gaps in existing ids
|
||||||
const existingIds = pack.ice.map(e => e.i).sort((a, b) => a - b);
|
const existingIds = pack.ice.map(e => e.i).sort((a, b) => a - b);
|
||||||
|
|
@ -14,29 +19,34 @@ window.Ice = (function () {
|
||||||
return existingIds[existingIds.length - 1] + 1;
|
return existingIds[existingIds.length - 1] + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear all ice
|
||||||
|
private clear() {
|
||||||
|
pack.ice = [];
|
||||||
|
}
|
||||||
|
|
||||||
// Generate glaciers and icebergs based on temperature and height
|
// Generate glaciers and icebergs based on temperature and height
|
||||||
function generate() {
|
public generate() {
|
||||||
clear();
|
this.clear();
|
||||||
const { cells, features } = grid;
|
const { cells, features } = grid;
|
||||||
const { temp, h } = cells;
|
const { temp, h } = cells;
|
||||||
Math.random = aleaPRNG(seed);
|
Math.random = Alea(seed);
|
||||||
|
|
||||||
const ICEBERG_MAX_TEMP = 0;
|
const ICEBERG_MAX_TEMP = 0;
|
||||||
const GLACIER_MAX_TEMP = -8;
|
const GLACIER_MAX_TEMP = -8;
|
||||||
const minMaxTemp = d3.min(temp);
|
const minMaxTemp = min<number>(temp)!;
|
||||||
|
|
||||||
// Generate glaciers on cold land
|
// Generate glaciers on cold land
|
||||||
{
|
{
|
||||||
const type = "iceShield";
|
const type = "iceShield";
|
||||||
const getType = cellId =>
|
const getType = (cellId: number) =>
|
||||||
h[cellId] >= 20 && temp[cellId] <= GLACIER_MAX_TEMP ? type : null;
|
h[cellId] >= 20 && temp[cellId] <= GLACIER_MAX_TEMP ? type : null;
|
||||||
const isolines = getIsolines(grid, getType, { polygons: true });
|
const isolines = getIsolines(grid, getType, { polygons: true });
|
||||||
|
|
||||||
if (isolines[type]?.polygons) {
|
if (isolines[type]?.polygons) {
|
||||||
isolines[type].polygons.forEach(points => {
|
isolines[type].polygons.forEach((points: Point[]) => {
|
||||||
const clipped = clipPoly(points);
|
const clipped = clipPoly(points, graphWidth, graphHeight);
|
||||||
pack.ice.push({
|
pack.ice.push({
|
||||||
i: getNextId(),
|
i: this.getNextId(),
|
||||||
points: clipped,
|
points: clipped,
|
||||||
type: "glacier"
|
type: "glacier"
|
||||||
});
|
});
|
||||||
|
|
@ -58,13 +68,13 @@ window.Ice = (function () {
|
||||||
const size = minmax(rn(baseSize * randomFactor, 2), 0.1, 1);
|
const size = minmax(rn(baseSize * randomFactor, 2), 0.1, 1);
|
||||||
|
|
||||||
const [cx, cy] = grid.points[cellId];
|
const [cx, cy] = grid.points[cellId];
|
||||||
const points = getGridPolygon(cellId).map(([x, y]) => [
|
const points = getGridPolygon(cellId, grid).map(([x, y]: Point) => [
|
||||||
rn(lerp(cx, x, size), 2),
|
rn(lerp(cx, x, size), 2),
|
||||||
rn(lerp(cy, y, size), 2)
|
rn(lerp(cy, y, size), 2)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
pack.ice.push({
|
pack.ice.push({
|
||||||
i: getNextId(),
|
i: this.getNextId(),
|
||||||
points,
|
points,
|
||||||
type: "iceberg",
|
type: "iceberg",
|
||||||
cellId,
|
cellId,
|
||||||
|
|
@ -73,13 +83,13 @@ window.Ice = (function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addIceberg(cellId, size) {
|
addIceberg(cellId: number, size: number) {
|
||||||
const [cx, cy] = grid.points[cellId];
|
const [cx, cy] = grid.points[cellId];
|
||||||
const points = getGridPolygon(cellId).map(([x, y]) => [
|
const points = getGridPolygon(cellId, grid).map(([x, y]: Point) => [
|
||||||
rn(lerp(cx, x, size), 2),
|
rn(lerp(cx, x, size), 2),
|
||||||
rn(lerp(cy, y, size), 2)
|
rn(lerp(cy, y, size), 2)
|
||||||
]);
|
]);
|
||||||
const id = getNextId();
|
const id = this.getNextId();
|
||||||
pack.ice.push({
|
pack.ice.push({
|
||||||
i: id,
|
i: id,
|
||||||
points,
|
points,
|
||||||
|
|
@ -90,7 +100,7 @@ window.Ice = (function () {
|
||||||
redrawIceberg(id);
|
redrawIceberg(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeIce(id) {
|
removeIce(id: number) {
|
||||||
const index = pack.ice.findIndex(element => element.i === id);
|
const index = pack.ice.findIndex(element => element.i === id);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
const type = pack.ice.find(element => element.i === id).type;
|
const type = pack.ice.find(element => element.i === id).type;
|
||||||
|
|
@ -104,15 +114,7 @@ window.Ice = (function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateIceberg(id, points, size) {
|
randomizeIcebergShape(id: number) {
|
||||||
const iceberg = pack.ice.find(element => element.i === id);
|
|
||||||
if (iceberg) {
|
|
||||||
iceberg.points = points;
|
|
||||||
iceberg.size = size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function randomizeIcebergShape(id) {
|
|
||||||
const iceberg = pack.ice.find(element => element.i === id);
|
const iceberg = pack.ice.find(element => element.i === id);
|
||||||
if (!iceberg) return;
|
if (!iceberg) return;
|
||||||
|
|
||||||
|
|
@ -123,8 +125,8 @@ window.Ice = (function () {
|
||||||
// Get a different random cell for the polygon template
|
// Get a different random cell for the polygon template
|
||||||
const i = ra(grid.cells.i);
|
const i = ra(grid.cells.i);
|
||||||
const cn = grid.points[i];
|
const cn = grid.points[i];
|
||||||
const poly = getGridPolygon(i).map(p => [p[0] - cn[0], p[1] - cn[1]]);
|
const poly = getGridPolygon(i, grid).map((p: Point) => [p[0] - cn[0], p[1] - cn[1]]);
|
||||||
const points = poly.map(p => [
|
const points = poly.map((p: Point) => [
|
||||||
rn(cx + p[0] * size, 2),
|
rn(cx + p[0] * size, 2),
|
||||||
rn(cy + p[1] * size, 2)
|
rn(cy + p[1] * size, 2)
|
||||||
]);
|
]);
|
||||||
|
|
@ -132,7 +134,7 @@ window.Ice = (function () {
|
||||||
iceberg.points = points;
|
iceberg.points = points;
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeIcebergSize(id, newSize) {
|
changeIcebergSize(id: number, newSize: number) {
|
||||||
const iceberg = pack.ice.find(element => element.i === id);
|
const iceberg = pack.ice.find(element => element.i === id);
|
||||||
if (!iceberg) return;
|
if (!iceberg) return;
|
||||||
|
|
||||||
|
|
@ -152,19 +154,6 @@ window.Ice = (function () {
|
||||||
iceberg.points = points;
|
iceberg.points = points;
|
||||||
iceberg.size = newSize;
|
iceberg.size = newSize;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Clear all ice
|
window.Ice = new IceModule();
|
||||||
function clear() {
|
|
||||||
pack.ice = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
generate,
|
|
||||||
addIceberg,
|
|
||||||
removeIce,
|
|
||||||
updateIceberg,
|
|
||||||
randomizeIcebergShape,
|
|
||||||
changeIcebergSize,
|
|
||||||
clear
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
@ -13,3 +13,4 @@ import "./states-generator";
|
||||||
import "./zones-generator";
|
import "./zones-generator";
|
||||||
import "./religions-generator";
|
import "./religions-generator";
|
||||||
import "./provinces-generator";
|
import "./provinces-generator";
|
||||||
|
import "./ice";
|
||||||
|
|
@ -7,13 +7,13 @@ import { minmax, rn } from "./numberUtils";
|
||||||
* @param {number} max - maximum value
|
* @param {number} max - maximum value
|
||||||
* @return {number} random integer between min and max
|
* @return {number} random integer between min and max
|
||||||
*/
|
*/
|
||||||
export const rand = (min: number, max?: number): number => {
|
export const rand = (min?: number, max?: number): number => {
|
||||||
if (min === undefined && max === undefined) return Math.random();
|
if (min === undefined && max === undefined) return Math.random();
|
||||||
if (max === undefined) {
|
if (max === undefined) {
|
||||||
max = min;
|
max = min;
|
||||||
min = 0;
|
min = 0;
|
||||||
}
|
}
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
return Math.floor(Math.random() * (max! - min! + 1)) + min!;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue