-
-
Notifications
You must be signed in to change notification settings - Fork 882
alternative grid generators #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3827a50
aaacd2d
b640f97
db49f1c
4291777
f4b3662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| .vscode | ||
| .idea | ||
| .idea/Fantasy-Map-Generator.iml | ||
| node_modules | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -631,6 +631,7 @@ void (function addDragToUpload() { | |
| }); | ||
| })(); | ||
|
|
||
| const gridOptimizationRequired = () => globalThis[byId('gridAlgorithm').value] == jitteredGridPoints; | ||
| async function generate(options) { | ||
| try { | ||
| const timeStart = performance.now(); | ||
|
|
@@ -642,8 +643,10 @@ async function generate(options) { | |
|
|
||
| applyMapSize(); | ||
| randomizeOptions(); | ||
| const method = globalThis[byId('gridAlgorithm').value]; | ||
|
|
||
| if (shouldRegenerateGrid(grid)) grid = precreatedGraph || generateGrid(); | ||
| const cellsDesired = +byId("pointsInput").dataset.cells; | ||
| if (shouldRegenerateGrid(grid, method)) grid = precreatedGraph || generateGrid(cellsDesired, method); | ||
| else delete grid.cells.h; | ||
| grid.cells.h = await HeightmapGenerator.generate(grid); | ||
|
|
||
|
|
@@ -1164,23 +1167,27 @@ function generatePrecipitation() { | |
| } | ||
|
|
||
| // recalculate Voronoi Graph to pack cells | ||
| // pars: optimize -> optimize cells structure, copy original graph otherwise. | ||
| function reGraph() { | ||
| TIME && console.time("reGraph"); | ||
| const {cells: gridCells, points, features} = grid; | ||
| const newCells = {p: [], g: [], h: []}; // store new data | ||
| const spacing2 = grid.spacing ** 2; | ||
| const optimize = gridOptimizationRequired() | ||
|
|
||
| for (const i of gridCells.i) { | ||
| const height = gridCells.h[i]; | ||
| const type = gridCells.t[i]; | ||
| if (height < 20 && type !== -1 && type !== -2) continue; // exclude all deep ocean points | ||
| if (type === -2 && (i % 4 === 0 || features[gridCells.f[i]].type === "lake")) continue; // exclude non-coastal lake points | ||
| if (optimize) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If optimization is not required, probably the whole rePack is not required as well
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure. you are absolutely right. But that pack object is super coupled with er... everything. I didn't dare to eliminate it. This is the safe bet. When we rewrite this part, this can be changed. If you are absolutely sure there will be no problem (you know the code much better than me) let's change it.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, but we already can do a minor refactoring, leaving the resulting pack object as is. We can extract the logic for points repacking into a separate function and call it conditionally. If optimization is not required, then just return existing
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that was my first intention, but I was lazy to do it. It would certainly save some bytes but memory is cheap even in case of 100k maps (SVG eats several orders of magnitude more ram).
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more of a refactoring issue. Adding more options we make the code harder to understand, so if anything new is added, we need to make the old part simpler to not increase the entropy too much |
||
| if (height < 20 && type !== -1 && type !== -2) continue; // exclude all deep ocean points | ||
| if (type === -2 && (i % 4 === 0 || features[gridCells.f[i]].type === "lake")) continue; // exclude non-coastal lake points | ||
| } | ||
| const [x, y] = points[i]; | ||
|
|
||
| addNewPoint(i, x, y, height); | ||
|
|
||
| // add additional points for cells along coast | ||
| if (type === 1 || type === -1) { | ||
| if (optimize && (type === 1 || type === -1)) { | ||
| if (gridCells.b[i]) continue; // not for near-border cells | ||
| gridCells.c[i].forEach(function (e) { | ||
| if (i > e) return; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it can be a simple check:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's not exactly the same, and not a good design. String constants of the module (which is non-existent here of course :) shouldn't be used outside the module. One always should export an accessor (like gridOptimizationRequired) or define constants. Using functions instead of those "magic strings" has an additional advantage: your IDE can identify misspelling.
Of course there is no module, therefore the whole question is academic, but this accessor is a reminder to do the proper thing when the stuff will finally become a module.
We can remove it if you don't like it, but that one has a purpose.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I agree, but in this case
gridOptimizationRequiredshould not be in main.js. We can have a separate module for points generation and put this function there. Something like 'points-generator'There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course it shouldn't.There are quite a lot of stuff what shouldn't be there. :) It started as a small patch not a full blown refactoring project. That's the other branch. ^-^