procedural
Library for defining procedural functions in a hierarchy to allow for complex procedurally generated content.
Introduction
This library aims to make it easy to create procedurally generated content, which usually means semi-random content that follows a complex set of rules. One great example of procedural generated content is the world in Minecraft. It's random for everyone who starts the game, but follows certain rules that together make a recognizable environment (hills, trees, caves, villages, ...)
You use this library by defining procedural functions. A procedural function may take any number of parameters, and can provide any number of values. Additionally, it may generate other procedural functions that depend on it, as well as its values.
Each instance of a procedural function (which you get by calling the function) has a unique hash which is used when calling any of the built-in pseudo-random number generators. This means you will always get the same sequence of random numbers for the same set of parameters, which is the most important aspect of procedurally generated content.
Here's how you define a procedural function that takes a single parameter:
var procedural = ;var world = ;
You may want a world to define if it's habitable:
world;
You can now create your world instance like this:
var earth = ;console;console;
Now it makes sense for a region in a world to be accessible...
var region = world ;
Let's find out what temperature we've got at (0, 0).
console;
Note that generated random numbers will be different for different parameters:
var kryptonTemp = temperature;console;
But wait, Krypton isn't habitable! Let's fix that by revisiting the habitable value of worlds.
world;
If you rerun all the code together now, you'll see that Krypton will be a bit hotter than before, while Earth is still comfortable.
I hope this example shows how useful it is to have a set of procedurally generated values that depend on each other when you want to create random content that still follows a set of rules.
For another example, see the bottom of this README, or go check out one of these demos:
- TODO: The space demo.
API
TODO: Define the API here.
Full example
How to define a procedurally generated avatar.
var procedural = ; var avatar = // The block size is just visual, so it shouldn't affect randomization. // The username is needed to create a unique avatar for every user. // Size, in blocks. Different sizes will create different avatars. // The pixel size of a single (square) block. // Calculate the colors that make up the avatar. // 75% of avatars have a mirrored effect, others don't. // A particular avatar has a unique set of blocks. // The validator will run independently for both parameters. // The color of this block. // Go back to defining the parent (avatar). // Renders to a canvas and returns a URL for <img>. ; var img = document;imgsrc = url;documentbody;