Tutorial 10. World Generation

Before you read, make sure you have seen the previous tutorial

Code is on github

So, we got blocks and items, but now, lets spawn them naturally.
For that, we use a worldGenerator.
We can register our own, by telling GameRegistry about it.

So, we link to our worldGenration class, and we give it an int. That int is the weight. Higher weight tend to sink, so higher values will get called later in the generation.

So, in order to act as a worldGenerator, we need to implement IWorldGenerator.
IntelliJ will now throw errors, because we need to override a method, called generate.

So, what does this do: We get a few things from this method. World, Chunk coords, and a random.
With the world param, we can check what dimension we are in. This way, we can add blocks to specific worlds. (0 = overworld, 1 = end, -1 = nether).
To get Coordinates, instead of chunk numbers, we multiply by 16.
(Chunk 3, 5 will become coords 48, 80).

Lets create those methods then.

Cool, now. The math.
In forge, there are a few gens you can call. The most common one is WorldGenMinable, because this is what spawns the ores. This method replaces a block with another block, at given coords, in a vain.
So, with a little math, you get this.

From top to bottom:
We make a for loop, so it will happen more than once (if set) in a chunk.
We randomize the location of the ore within the chunk.
We call WorldGenMinable, and give it what block to replace, the vain size, and the block to replace. Then we call generate to tell where.


If you dont get this, read it again, or learn java.
As you can see, it takes a hell of a lot of params. Ill list them later.


To call this, in generateOverworld, call the addOreSpawn.

So, lets list the params:

* block: Block to generate
* world: world var (see params)
* random: a random (see params)
* chunkX: The x coordinate of a chunk (see params)
* chunkZ: The z coordinate of a chunk (see params)
* minVainSize: Minimal vain size
* maxVainSize: The max vain siz
* chanceToSpawn: The chance to spawn. Dont make this extreme like 1000, keep it around at most 50, or your world wont load.
* minY: Lowest point to spawn
* maxY: Highest point to spawn.

If you fill this in, your block will be added to the world.
Just keep diggin ;)

Back to List
Next tutorial


2 Responses to Tutorial 10. World Generation

  1. Roon says:

    The &lt for the orespawn method is a mistake? I seem to get an error when i put it in.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">