Jump to content

Procedural Structure Generation


Reika

Recommended Posts

I have several questions related to procedural structure generation (piece-wise multi-chunk structures like Strongholds, Nether Fortresses, and many Twilight Forest structures, not single-pass isolated structures like dungeons, jungle temples, and anything generated in an IWorldGenerator).

 

How do I go about adding my own kind? I assume I extend MapGenStructure, but how do I register it? ChunkProvider classes seem to hardcode what generators they use, with no obvious Forge hooks aside from a PopulateChunkEvent.Pre, but that seems not to be the intended process, and some of the parameters, like the coordinates, seem off.

 

Also, how does the structure generator work? I see that structures have pieces and a StructureStart, but what are the requirements on those pieces, such as dimensions (for example, must each piece be a 16x16 (or other size) square?), and how do I get pieces to plug into each other properly?

Link to comment
Share on other sites

I remember looking at it once and going "how the fuck does this even work?"  I was never able to back-trace the parts that I understood (each segment) back to another piece I understood (the world generator).

 

I wish you luck.

  • Haha 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

When you say your structure generation, does this include the cave generation and stuff generated inside the chunk provider at world generation? I've been looking in to it a bit and have a little bit of an understanding how some of it works.

 

From what I've been doing, I would assume to add your own structure generation that you would have to make your own Chunk Provider and add them in there, I spent a few days perusing the internet and the best I could find was IWorldGenerators which will not work for things like nether fortresses.

 

The structure start for a Mineshaft, if I remember correctly, are the big dirt based rooms that you encounter, and they generate say 7 paths off of themselves. These paths keep generating until they collide with another path or itself. During my perusal of the internet I happened across a technique called "perlin worms", and these seem very close to what is going on inside here. The conceptual block I've been having with this is if I pick a start point for my perlin worm, how would I know if it ended up inside another chunk, and how would I manage to hollow out the chunk next door without generating the chunk it started in?

 

The villager center is the well structure, I think, I'm not sure if that ends up being a 16x16, but I would not be surprised if the start component was a full chunk, I believe that would make it easy to generate the structures than a smaller one, but once you figure out how to generate the structure, I doubt it matters as long as your method is designed to work in that manner.

 

One thing to note, when it calls the method to begin generating your structure, like caves, the MapGenBase FORCES the random to be initialized with the worlds seed. With this you MUST generate a new seed for the random you pass to the actual method to generate the structure. The joy of this is as long as your method generates a consistent seed (which should be super easy seeing as the MapGenBase does it for you) you would be able to generate extremely consistent structures. Once again, how does one figure out when it pierces your chunk? Do you set a limit of say 6 chunk range (or 8 maybe if that's what that means?) and chunk all chunks and generate EVERY structure that exists to fully build it's components that may hit your chunk?

 

Let me know if that was English. Sounded good while I typed it.

Link to comment
Share on other sites

Depending on why and how you want it to generate, if you're making totally new stuff I'd probably suggest not hooking into the existing generation but rather just make your own grounds-up generation code.  I've found that with structure generation, the procedural part is very dependent on what kind of structure you're talking about.  A regular structure can be done with loops and interchangeable parts while an irregular structure might need to basically just read from a set of schematics. 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Depending on why and how you want it to generate, if you're making totally new stuff I'd probably suggest not hooking into the existing generation but rather just make your own grounds-up generation code.  I've found that with structure generation, the procedural part is very dependent on what kind of structure you're talking about.  A regular structure can be done with loops and interchangeable parts while an irregular structure might need to basically just read from a set of schematics.

When you say your structure generation, does this include the cave generation and stuff generated inside the chunk provider at world generation? I've been looking in to it a bit and have a little bit of an understanding how some of it works.

 

From what I've been doing, I would assume to add your own structure generation that you would have to make your own Chunk Provider and add them in there, I spent a few days perusing the internet and the best I could find was IWorldGenerators which will not work for things like nether fortresses.

 

The structure start for a Mineshaft, if I remember correctly, are the big dirt based rooms that you encounter, and they generate say 7 paths off of themselves. These paths keep generating until they collide with another path or itself. During my perusal of the internet I happened across a technique called "perlin worms", and these seem very close to what is going on inside here. The conceptual block I've been having with this is if I pick a start point for my perlin worm, how would I know if it ended up inside another chunk, and how would I manage to hollow out the chunk next door without generating the chunk it started in?

 

The villager center is the well structure, I think, I'm not sure if that ends up being a 16x16, but I would not be surprised if the start component was a full chunk, I believe that would make it easy to generate the structures than a smaller one, but once you figure out how to generate the structure, I doubt it matters as long as your method is designed to work in that manner.

 

One thing to note, when it calls the method to begin generating your structure, like caves, the MapGenBase FORCES the random to be initialized with the worlds seed. With this you MUST generate a new seed for the random you pass to the actual method to generate the structure. The joy of this is as long as your method generates a consistent seed (which should be super easy seeing as the MapGenBase does it for you) you would be able to generate extremely consistent structures. Once again, how does one figure out when it pierces your chunk? Do you set a limit of say 6 chunk range (or 8 maybe if that's what that means?) and chunk all chunks and generate EVERY structure that exists to fully build it's components that may hit your chunk?

 

Let me know if that was English. Sounded good while I typed it.

 

As it happens, yes, these will be going in a custom dimension which allows me control over the ChunkProviders, but this does not help me actually figure out how the system works; there seems to be an internal automatic algorithm Minecraft uses to generate structures like these.

Link to comment
Share on other sites

I would forget any premade stuff and create my structure using IWorldGenerator.

Here is an example of what my code would look like. However, its 01 am here in Austria right now, don't expect that this actually works.

 

 

 



IWorldGenerator:

int chancesNeighbourRooms = 400; //400 % chance that there are rooms next to this one
GenerationPortalShrine.generate(random, x + random.nextInt(16), z + random.nextInt(16), chancesNeighbourRooms, null, 0){



Generation Class:

generate(Random rand, int x, int z, int chancesNeighbourRooms, int[][] usedLocations, int side):


/*
*
*side 0 not set
*1 north
*2 east
*3 south
*4 west
*/


if(Random.nextInt(100) > chancesNeighbourRooms){

CLOSE DOOR

} else {
     boolean canGenerate = true;

     
     Room room = Rooms.getRooms().get(RANDOMNUMBER);

     CHECK IF ROOM HAS DOORS AT THE REQUIRED SIDE, IF NOT LOOK FOR NEW ROOM
     
     for(LOOP THROUGH usedLocations AND CHECK IF THE POSITION AND SIZE OF THE NEW ROOMS ARE OVERLAPPING WITH AN OLD ONE){

          canGenerate=false

     }


    if(canGenerate){

     GENARTION STARTS AT THE SET DOOR, POSITION HAS TO BE RESET DEPENDING ON SIDE AND SIZE OF THE NEW ROOM (+ door offset?)
     x=
     y=

     room.generate(x,y)

     ADD ALL NOW ALREADY SET BLOCKS TO USED LOCATIONS
     
     if(room.getSides.getSize()>1){

     for(int i = 0; i< room.getSides.getSize(); i++){

      if(room.getSides.get(i) != side){

        GET SIDE OF NEW ROOM AND NEW X AND Z LOCATIONS
        GenerationPortalShrine.generate(random, x, z, chancesNeighbourRooms-20, usedLocations, newSide);
      }

     }
    }

}

 

 

 

Here could be your advertisement!

Link to comment
Share on other sites

I would forget any premade stuff and create my structure using IWorldGenerator.

Here is an example of what my code would look like. However, its 01 am here in Austria right now, don't expect that this actually works.

 

 

 



IWorldGenerator:

int chancesNeighbourRooms = 400; //400 % chance that there are rooms next to this one
GenerationPortalShrine.generate(random, x + random.nextInt(16), z + random.nextInt(16), chancesNeighbourRooms, null, 0){



Generation Class:

generate(Random rand, int x, int z, int chancesNeighbourRooms, int[][] usedLocations, int side):


/*
*
*side 0 not set
*1 north
*2 east
*3 south
*4 west
*/


if(Random.nextInt(100) > chancesNeighbourRooms){

CLOSE DOOR

} else {
     boolean canGenerate = true;

     
     Room room = Rooms.getRooms().get(RANDOMNUMBER);

     CHECK IF ROOM HAS DOORS AT THE REQUIRED SIDE, IF NOT LOOK FOR NEW ROOM
     
     for(LOOP THROUGH usedLocations AND CHECK IF THE POSITION AND SIZE OF THE NEW ROOMS ARE OVERLAPPING WITH AN OLD ONE){

          canGenerate=false

     }


    if(canGenerate){

     GENARTION STARTS AT THE SET DOOR, POSITION HAS TO BE RESET DEPENDING ON SIDE AND SIZE OF THE NEW ROOM (+ door offset?)
     x=
     y=

     room.generate(x,y)

     ADD ALL NOW ALREADY SET BLOCKS TO USED LOCATIONS
     
     if(room.getSides.getSize()>1){

     for(int i = 0; i< room.getSides.getSize(); i++){

      if(room.getSides.get(i) != side){

        GET SIDE OF NEW ROOM AND NEW X AND Z LOCATIONS
        GenerationPortalShrine.generate(random, x, z, chancesNeighbourRooms-20, usedLocations, newSide);
      }

     }
    }

}

 

 

Just generating things is really easy... see this: https://github.com/VapourDrive/ExpandedWorld

Trying to register things as structures seems to be such a massive pain in the ass that I abandoned all hope and moved on to a different project. I had a dungeon based off of spherical rooms that had a chance to recursively generate another room in a row with connecting hallways and it looked quite nice. The issue ran into the size of the structure, without actually registering it as a structure the game doesn't care about where it generates and things can get really messy.

I'll keep watching this thread for any updates....

Link to comment
Share on other sites

Yeah, thats why i use an int to decrease the chance for new rooms.

But i doubt that the user will really notice how your structure was generated if its underground and dark enough.

Btw, did you generate the rooms and the hallways at the same time or did you first generate the rooms and once they were all generated the hallways?

Here could be your advertisement!

Link to comment
Share on other sites

The problem lies in that if you don't register a structure properly, for all you know the game will end up throwing your generated "thing" in randomly and it can interfere with other legit structures, such as overlap a stronghold for example.

As for the rooms and hallways, a room generates initially, then there is a chance for another room to generate and if it does, a connecting hallway gets generated immediately after.

The process always goes: roomA -> roomB -> hallwayAB, if roomB calls another room then the process is repeated as roomB -> roomC -> hallwayBC

Generating the rooms first allowed me to make all of the sides solid and then the hallways force openings into the room. Getting the size of the rooms variable took a lot of fiddling around because they needed to be generated at different distances apart from each other. It was all pretty fun but "organizing" the world was hard with structures so big. Something like a vanilla dungeon doesn't matter because it never exceeds the bounds of its chunk (afaik).

Link to comment
Share on other sites

Yeah, but you could write your own function to check if there are bricks or planks nearby to determine if there are already dungeons there.

 

I recommended to have rooms with predefined doors because this way you have more control about the design of the room.

Here could be your advertisement!

Link to comment
Share on other sites

here is some code for adding village structures in 1.7.10

 

during FMLPreInitializationEvent call

 

VillagerRegistry.instance().registerVillageCreationHandler(new StoreHandler());

MapGenStructureIO.func_143031_a(ComponentShop.class, VillageIdiot.modid+":vibss");

 

 

StoreHandler  class

package me.el.VillageIdiot;

import java.util.List;
import java.util.Random;

import net.minecraft.world.gen.structure.StructureVillagePieces.PieceWeight;
import net.minecraft.world.gen.structure.StructureVillagePieces.Start;
import cpw.mods.fml.common.registry.VillagerRegistry.IVillageCreationHandler;

public class StoreHandler implements IVillageCreationHandler {

@Override
public PieceWeight getVillagePieceWeight(Random random, int i) {
	 return new PieceWeight(ComponentShop.class, 9, 1);
}

@Override
public Class<?> getComponentClass() {
	return ComponentShop.class;
}

@Override
public Object buildComponent(PieceWeight villagePiece, Start startPiece, List pieces, Random random, int p1, int p2, int p3, int p4, int p5) {
	return ComponentShop.buildComponent(startPiece, pieces, random, p1, p2, p3, p4, p5);
}
}

 

ComponentShop class

package me.el.VillageIdiot;

import java.util.List;
import java.util.Random;

import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraft.world.gen.structure.StructureBoundingBox;
import net.minecraft.world.gen.structure.StructureComponent;
import net.minecraft.world.gen.structure.StructureVillagePieces;
import net.minecraft.world.gen.structure.StructureVillagePieces.Start;

public class ComponentShop   extends StructureVillagePieces.House1{
private int averageGroundLevel=-1;
private static Random rng=new Random();
private static BlockSelector basment;

public ComponentShop()
{
}

/**
 * @param villagePiece  
 * @param par2 
 */
public ComponentShop(Start villagePiece, int par2, Random rnd, StructureBoundingBox sbb, int cbs){
	super();
	this.coordBaseMode = cbs;
	this.boundingBox = sbb;
}

public static ComponentShop buildComponent (Start villagePiece, List<?> pieces, Random random, int x, int y, int z, int cbs, int p5){
	//System.out.println("ComponentEtchStore buildComponent "+x+" "+y+" "+z);
	StructureBoundingBox nbb = StructureBoundingBox.getComponentToAddBoundingBox(x, y, z, 0, -2, 0, 10, 4, 7, cbs);
	//System.out.println("ComponentEtchStore buildComponent "+nbb.toString());
	ComponentShop r= canVillageGoDeeper(nbb) && StructureComponent.findIntersecting(pieces, nbb) == null ? new ComponentShop(villagePiece, p5, random, nbb, cbs) : null;
	//System.out.println("ComponentEtchStore buildComponent "+r+" "+nbb);
	return r;
}

@Override
public boolean addComponentParts (World world, Random random, StructureBoundingBox nsbb){
	//System.out.println("ComponentDJStore addComponentParts "+this+" "+nsbb+" "+averageGroundLevel);
	StructureBoundingBox sbb= new StructureBoundingBox(nsbb);
	if (this.averageGroundLevel < 0){
		this.averageGroundLevel = this.getAverageGroundLevel(world, nsbb);

		if (this.averageGroundLevel < 0){
			return true;
		}

		this.boundingBox.offset(0, this.averageGroundLevel - this.boundingBox.maxY + 2, 0);
	}

	int s2 = getMetadataWithOffset(Blocks.oak_stairs, 2);
	int s3 = getMetadataWithOffset(Blocks.oak_stairs, 3);

	int ld= getMetadataWithOffset(Blocks.ladder, 5);

	fillWithRandomizedBlocks(world, sbb, 0, -3, 0, 9, 1, 6, false, rng, basment);
	fillWithBlocks(world, sbb, 1, 1, 1, 8, 1, 5, Blocks.planks, Blocks.planks, false);
	fillWithBlocks(world, sbb, 0,2,0 , 9, 5, 6, Blocks.planks, Blocks.planks, false);

	fillWithBlocks(world, sbb, 0, 3, 3 , 9, 3, 3, Blocks.glass_pane, Blocks.glass_pane, false);
	fillWithBlocks(world, sbb, 2, 3, 0 , 3, 3, 6, Blocks.glass_pane, Blocks.glass_pane, false);
	fillWithBlocks(world, sbb, 6, 3, 6 , 7, 3, 6, Blocks.glass_pane, Blocks.glass_pane, false);

	fillWithAir(world, sbb, 1, 2, 1, 8, 4, 5);
	fillWithAir(world, sbb, 1, -2, 1, 8, -1, 5);
	fillWithMetadataBlocks(world, sbb, 1, -2, 3, 1, 2, 3, Blocks.ladder, ld, Blocks.ladder, ld, false);

	fillWithBlocks(world, sbb, 0, 6, 2, 9, 7, 4, Blocks.planks, Blocks.planks, false);
	fillWithMetadataBlocks(world, sbb, 0, 8, 3, 9, 8, 3, Blocks.wooden_slab, 5, Blocks.wooden_slab, 5, false);

	for (int t=0;t<3;t++){
		fillWithMetadataBlocks(world, sbb, 0, 5+t, 0+t, 9, 5+t, 0+t, Blocks.dark_oak_stairs, s3, Blocks.dark_oak_stairs, s3, false);
		fillWithMetadataBlocks(world, sbb, 0, 5+t, 6-t, 9, 5+t, 6-t, Blocks.dark_oak_stairs, s2, Blocks.dark_oak_stairs, s2, false);
	}
	placeBlockAtCurrentPosition(world, Blocks.glass_pane, 0, 5, 3, 0, sbb);
	placeDoorAtCurrentPosition(world, sbb, rng, 7, 2, 0, getMetadataWithOffset(Blocks.wooden_door, 1));
	placeBlockAtCurrentPosition(world, Blocks.stone_stairs, s3, 7, 1, -1, sbb);

	placeBlockAtCurrentPosition(world, Blocks.torch, 0, 3, 4, 1, sbb);
	placeBlockAtCurrentPosition(world, Blocks.torch, 0, 6, 4, 1, sbb);
	placeBlockAtCurrentPosition(world, Blocks.torch, 0, 3, 4, 5, sbb);
	placeBlockAtCurrentPosition(world, Blocks.torch, 0, 6, 4, 5, sbb);
	spawnVillagers(world, sbb, 3, 2, 4, 2);
	return true;
}

@Override
protected int getVillagerType(int n) {
	VillageIdiot.log("getVillagerType"+n);
	if ((n&1)==0){
		return VillageIdiot.proxy.idiot_b;
	}else{
		return VillageIdiot.proxy.idiot_s;
	}
}

static{
	basment=new BlockSelector(){
		@Override
		public void selectBlocks(Random rng, int x, int y, int z, boolean b) {
			field_151562_a=Blocks.stonebrick;
		}
		@Override
		public int getSelectedBlockMetaData() {
			int r = rng.nextInt(5);
			if (r>2)r=0;
			return r;
		}
	};
};

}

 

you might find some errors as it will try to spawn villagers with professions you don't have as i skipped there code, but it should be easily fixed

 

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.