Small library that can generate a world based on elevation and moisture, complete with terrain features and exports the SVG image and the JSON world data object.

Development

LanguageJavascript
Licensepublic domain
Version1.5.0
Release dateDecember 24, 2021
Downloadzip archive
SHA-2564e40138dc2b08dc4d2643fe5e4f5e87f69d3154b59f2cfd71bc03a32d226f44f

Usage

Create the main world object based on the provided settings and populate the internal array. All parameters are optional:

let new_world = new world({
	width: 64,				/* world width, in hexes */
	height: 32,				/* world height, in hexes */
	erosion: 0.54,			/* world erosion */
	seed_moisture: 6893		/* Load a specific random seed */
	seed_elevation: 736		/* Load a specific random seed */
});

Draw the internal JSON world data as an image:

<div class="worldmap"></div>
/* draw to a SVG image with 0.5 terrain opacity,  all other parameters are optional */
new_world.draw('.worldmap', {
	mode: 'svg',			/* map mode, SVG or HTML5 canvas */
	opacity: 0.5,			/* terrain opacity */
	hex_size: 24,			/* size of a single hex */
	show_grid: true,		/* whether to display the hex grid */
	show_terrain: true,		/* whether to display the advanced terrain features */
	assets_url: './'		/* Path to load the terrain assets PNGs from */
});

/* draw to a HTML5 canvas with 0.4 terrain opacity */
new_world.draw('.worldmap', {
	mode: 'canvas',
	opacity: 0.4
});

/* draw to a HTML5 canvas with default options */
new_world.draw('.worldmap', {
	mode: 'canvas'
});

Get the properties of the world:

let properties = new_world.props();

/* will result in */

{
	width: 64,
	height: 32,
	erosion: 0.54,
	seeds: {
		elevation: 736,
		moisture: 6893
	}
}

Export the internal JSON world data:

/* export JSON serialized data */
let exported_data = new_world.export_json();

/* export raw Array object */
let exported_data = new_world.export();

Manipulate the world data array:

let world_data = new_world.data();
let properties = new_world.props();
for (let y = 0; y < properties.height; y++) {
	for (let x = 0; x < properties.width; x++) {
		console.log(world_data[y][x].e);
	}
}

Get the default terrain colors:

let terrains = new_world.terrains();

/* will result in */

terrains['grass'].color = '#E6F59A';

/* and you can get the color of a hex too: */
let terrain = new_world.get_hex_terrain({
	x: 12,
	y: 26
});
let terrains = new_world.terrains();
let hex_color = terrains[terrain].color;

/* if terrain = 'mountains', hex_color will be: */

hex_color = '#B37D1A';

Check if a hex is water:

let is_water = new_world.hex_is_water({
	x: 12,
	y: 26
});

/* will result in */

is_water = false;

Get the terrain type of the specified hex, based on its elevation:

let terrain = new_world.get_hex_terrain({
	x: 12,
	y: 26
});

/* will result in */

terrain = 'mountains';

Get the current elevation of a hex:

let elevation = new_world.get_hex_elevation({
	x: 2,
	y: 4
});

/* will result in */

elevation = 0.7391983661686046;

Get the current moisture of a hex:

let moisture = new_world.get_hex_moisture({
	x: 10,
	y: 11
});

/* will result in */

moisture = 0.4925356581346529;

Get a random hex from the world data with the specified terrain (or false if there is no hex with that terrain type).

let hex = new_world.get_random_hex_with_terrain('grass');

/* will result in */

hex = {
	x: 10,
	y: 22
}

Get the world data for the specified hex.

let hex_data = new_world.get_hex({
	x: 4,
	y: 2
});

/* will result in */

hex_data = {
	e: 0.7391983661686046,
	m: 0.6185405664699859
}

Get terrain statistics (number of hexes for each terrain type).

let stats = new_world.stats();

/* will result in */

stats = { ocean: 0, grass: 2086, subtropical_desert: 11, temperate_desert: 12, taiga: 0, ... }

Raise the current moisture of a hex by 0.12:

let new_moisture = new_world.raise_hex_moisture({
	x: 10,
	y: 11
}, 0.12);

/* will result in */

new_moisture = 0.4925356581346529 + 0.12;

/* if you want to lower the moisture, use the lower_hex_moisture() method */

Lower the current elevation of a hex by 0.87:

let new_elevation = new_world.lower_hex_elevation({
	x: 10,
	y: 11
}, 0.87);

/* will result in */

new_elevation = 0.7391983661686046 - 0.87;

/* if you want to raise the elevation, use the raise_hex_elevation() method */

Set a hex data key to the specified value. You can use this method to store additional information into the world data array.

new_world.set_hex(hex, 'city', {
	name: 'Alexandria',
	population: 130000
});

Load raw world data into the generator:

let world_data = [
	[
		{
			"e": 0.7578606610586678,
			"m": 0.5642837814565862
		}, {
			"e": 0.7902203979081555,
			"m": 0.6573637811637490
		}
	]
];

new_world.data(world_data);

License

This library is written by sizeof(cat) and released into the public domain.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

Acknowledgements

Screenshots

More info


  1. Copyright (c) 2015 Jonas Wagner, licensed under the MIT license. ↩︎

  2. Copyright (c) 2009 Michael Baczynski, polygonal.de, ported by Alex Veenendaal, opendoorgonorth.com, licensed under the MIT license. ↩︎

  3. Copyright (c) 2010-2011 Gabe Lerner, code.google.com/p/canvg, licensed under the MIT license. ↩︎

  4. Released to the public domain by Inkwell Ideas, Inc. ↩︎