Jump to content

Worldgeneration with schematic files[1.7.10]


Androm

Recommended Posts

No help ? I think I have to explain my code:

 

If I want to generate something i call the GeneralGenerateMethod.

I need lots of parameters:

1. int chance = 1/chance = chance if it spawn in a chunk

2. String schemname : the name of the .schematic file

3. int where => if where = 0 it spawns on the ground ; if where = 1 it spawns in underground ; if where = 2 it spawns only over water ; if where = 3 it spawns only on the ground of a sea

4. int blockInUnderGround : there you tell the method if some blocks of a building have to be in the underground => that´s good for springs or buildings with cellars .

5.  int structureHigh : it is only used for generating buildings under the earth : you put the high of your building there

6. boolean structureWithAir : If you want some buildings to spawn with air (usefull for underground buildings) you can set this to true otherwise set this to false.

7. int ChunkX : the Chunk X coodinates.

8. Random rand = useable to get a random coordinate in a chunk

9. int chunkZ : the Chunk Z coordinates.

10. World world = in which world it spawns (default : overworld)

11. ArrayList<Block> allowedBlocks = if it generate a structure it checks if the Block where it spawns is on the ArrayList : if true it will spawn otherwise it wont spawn

12. boolean SpawnerActive = if there is a spawner set it to true

13. ArrayList<Block> spawnerBlocks = the blocks in the .schematic which will be repleaced with a spawner

14.boolean ChestWithRandomLoot = if there are chest which have to have random loot set it to true

15. ArrayList<Block> chestBlocks = the blocks in the .schematic which will be repleaced with a chest

16. boolean randomSpinnwebs = if there are webs which have to be generated randomly = in every building the webs are on an other place

17. ArrayList<Block> spinnwebsBlocks = the blocks in the .schematic which will be repleaced with a web

18. ArrayList<Item> LootItems = which Items you can find in a chest

19. ArrayList<Integer> LootMaxStackSize = the max amount of an Item in a chest.

 

I hope that can help you to explain me what I have done wrong

Link to comment
Share on other sites

Just found my problem, i forgot to add i++; at some times...

My code :

 

package de.MhytRPG.www.worldgen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.feature.WorldGenLiquids;
import cpw.mods.fml.common.IWorldGenerator;
import de.MhytRPG.www.MhytRPG;
import de.MhytRPG.www.structures.SchematicLoader;
import de.MhytRPG.www.structures.SchematicLoader.Schematic;

public class WorldGen implements IWorldGenerator{

private static SchematicLoader sl = new SchematicLoader();

@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {
        switch(world.provider.dimensionId){
        case -1:
            generateNether(world, random, chunkX * 16, chunkZ * 16);
            break;
        case 0:
            generateSurface(world, random, chunkX * 16, chunkZ * 16);
            break;
        case 1:
            generateEnd(world, random, chunkX * 16, chunkZ * 16);
            break;
        }
}

private void generateEnd(World world, Random rand, int chunkX, int chunkZ) {}

private void generateSurface(World world, Random rand, int chunkX, int chunkZ) {
	ArrayList<Block> normalList = new ArrayList<Block>();
	normalList.add(Blocks.sand);
	normalList.add(Blocks.dirt);
	normalList.add(Blocks.stone);
	normalList.add(Blocks.grass);

	ArrayList<Block> normalListSpawner = new ArrayList<Block>();
	normalListSpawner.add(Blocks.soul_sand);

	ArrayList<Block> normalListChest = new ArrayList<Block>();
	normalListChest.add(Blocks.bedrock);

	ArrayList<Block> normalListSpinnwebs = new ArrayList<Block>();
	normalListSpinnwebs.add(Blocks.end_portal_frame);

	ArrayList<Block> nothingBlocks = new ArrayList<Block>();

	ArrayList<Item> noLoot = new ArrayList<Item>();
	ArrayList<Integer> noStackSize = new ArrayList<Integer>();

	ArrayList<Item> towerLoot = new ArrayList<Item>();
	ArrayList<Integer> towerLootStackSize = new ArrayList<Integer>();
	towerLoot.add(Items.potato);
	towerLootStackSize.add(5);
	towerLoot.add(Items.apple);
	towerLootStackSize.add(7);
	towerLoot.add(Items.arrow);
	towerLootStackSize.add(18);
	towerLoot.add(Items.iron_ingot);
	towerLootStackSize.add(9);
	towerLoot.add(Items.gold_nugget);
	towerLootStackSize.add(10);
	towerLoot.add(Items.ender_pearl);
	towerLootStackSize.add(3);
	towerLoot.add(Items.blaze_powder);
	towerLootStackSize.add(2);
	towerLoot.add(Items.spider_eye);
	towerLootStackSize.add(7);
	towerLoot.add(Items.rotten_flesh);
	towerLootStackSize.add(30);

	GeneralGenerateMethod(10, "spring", 0, 1, 12, false, chunkX, rand, chunkZ, world, normalList, false, nothingBlocks, false, nothingBlocks, false, nothingBlocks, noLoot, noStackSize);
	GeneralGenerateMethod(10, "tower", 0, -1, 50, false, chunkX, rand, chunkZ, world, normalList, true, normalListSpawner, true, normalListChest, true, normalListSpinnwebs, towerLoot, towerLootStackSize);
}

private void generateNether(World world, Random rand, int chunkX, int chunkZ) {}


public static void GeneralGenerateMethod(int chance, String schemname, int where, int blockInUnderGround, int structureHigh, boolean structureWithAir, 
		int chunkX, Random rand, int chunkZ, World world, ArrayList<Block> allowedBlocks, boolean SpawnerAcivate, ArrayList<Block> spawnerBlocks,
		boolean ChestWithRandomLoot, ArrayList<Block> chestBlocks, boolean randomSpinnwebs, ArrayList<Block> spinnwebsBlocks, ArrayList<Item> LootItems, ArrayList<Integer> LootMaxStackSize) {

	 int chanceForSpawn = new Random().nextInt(chance);
     if(chanceForSpawn == 1) {
        int bcy = 0;
        Schematic schem = sl.get(schemname);
        int firstBlockXCoord = chunkX + rand.nextInt(16);
        int firstBlockZCoord = chunkZ + rand.nextInt(16);
        if(where == 0) {
        	//where = 0 : spawn over ground  ; where  = 1 : spawn underground ; where = 2 : spawn only over water ; where = 3 : spawn on sea ground
        	for(bcy = 256; bcy > 0; bcy--) {
	        	if(!world.getBlock(chunkX, bcy, chunkZ).equals(Blocks.air)) {
	        		if(allowedBlocks.contains(world.getBlock(chunkX, bcy, chunkZ))) {
	        			bcy = bcy - (blockInUnderGround + 1);
	        			break;
	        		}
	        		else {
	        			return;
	        		}
	       		}
        	}
        }
       	else if(where == 1) {
       		for(bcy = 0; bcy < 256; bcy++) {
       			if(world.getBlock(chunkX, bcy, chunkZ).equals(Blocks.air)) {
       				bcy = (bcy-2)-structureHigh;
       				break;
       			}
       			if(bcy == 240) {
       				return;
       			}
       		}
       	}
        
       	else if(where == 2) {
       		for(bcy = 256; bcy > 3; bcy--) {
       			if(world.getBlock(chunkX, bcy, chunkZ).equals(Blocks.water)) {
       				bcy = (bcy + 1) - blockInUnderGround;
       				break;
       			}
       			if(bcy == 4) {
       				return;
       			}
       		}
       	}
        
       	else if(where == 3) {
       		for(bcy = 1; bcy < 256; bcy++) {
       			if(world.getBlock(chunkX, bcy, chunkZ).equals(Blocks.water)) {
       				bcy = bcy - blockInUnderGround;
       				break;
       			}
       			if(bcy == 240) {
       				return;
       			}
       		}
       	}
        	
     	int i = 0;
        for(int cy = 0; cy < schem.height; cy++) {
        	for(int cz = 0; cz < schem.length; cz++) {
        		for(int cx = 0; cx < schem.width; cx++){

        			Block b = Block.getBlockById(schem.blocks[i]);
        			
        			if(b == Blocks.air) {
        				if(structureWithAir == true) {
        					world.setBlockToAir(cx + firstBlockXCoord , cy + bcy, cz + firstBlockZCoord);
		             	    world.setBlock(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord, b, schem.data[i] , 2);
		             	    i++;
		             	    continue;
        				}
        				else if(structureWithAir == false) {
        					i++;
        					continue;
        				}
        			}
        			
        			else if(spinnwebsBlocks.contains(b)) {
        				if(randomSpinnwebs == true) {
        					int ran = new Random().nextInt(4);
        					if(ran <= 1) {
        						world.setBlockToAir(cx + firstBlockXCoord , cy + bcy, cz + firstBlockZCoord);
			             	    world.setBlock(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord, Blocks.web, schem.data[i], 2);
			             	    i++;
			             	    continue;
        					}
        					else { 
        						i++;
        						continue;
        						}
        				}
        				else if(randomSpinnwebs == false) {
        					world.setBlockToAir(cx + firstBlockXCoord , cy + bcy, cz + firstBlockZCoord);
		             	    world.setBlock(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord, Blocks.web, schem.data[i], 2);
		             	    i++;
        					continue;
        				}
        			}
        			else if(spawnerBlocks.contains(b)) {
        				if(SpawnerAcivate == true) {
        					
        					world.setBlockToAir(cx + firstBlockXCoord , cy + bcy, cz + firstBlockZCoord);
		             	    world.setBlock(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord, Blocks.mob_spawner, schem.data[i] , 2);
		             	    TileEntityMobSpawner spawner = (TileEntityMobSpawner)world.getTileEntity(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord);
		             	    spawner.func_145881_a().setEntityName("Witch");
		             	    i++;
		             	    continue;
        				}
        				else if(SpawnerAcivate == false) {
        					i++;
        					continue;
        				}
        			}
        			else if(chestBlocks.contains(b)) {
        				if(ChestWithRandomLoot == true) {
        					world.setBlockToAir(cx + firstBlockXCoord , cy + bcy, cz + firstBlockZCoord);
		             	    world.setBlock(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord, Blocks.chest, schem.data[i], 2);
		             	    TileEntityChest tec = (TileEntityChest) world.getTileEntity(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord);
		             	    if(!tec.isInvalid()) {
		             	    		int loot = (int) new Random().nextInt(10);
		             	    		for(int t = 0; t <= loot; t++) {
		             	    			int place = new Random().nextInt(26) + 1;
		             	    			int whichItem = new Random().nextInt(LootItems.size());
		             	    			Item ranItem = LootItems.get(whichItem);
		             	    			int stackSize = new Random().nextInt(LootMaxStackSize.get(whichItem) + 1);
		             	    			ItemStack stack = new ItemStack(ranItem, stackSize);
		             	    			tec.setInventorySlotContents(place, stack);
		             	    			place = 0;
		             	    			continue;
		             	    		}
		             	    		i++;
		             	    		continue;
		             	    }
		             	    else if(tec.isInvalid()) {
		             	    	i++;
		             	    	continue;
		             	    }
        				}
        				else if(ChestWithRandomLoot == false) {
        					i++;
		             	    continue;
        				}
        			}
        			
        			else if(b != Blocks.air && !chestBlocks.contains(b) && !spawnerBlocks.contains(b) && !spinnwebsBlocks.contains(b)) {
        				world.setBlockToAir(cx + firstBlockXCoord , cy + bcy, cz + firstBlockZCoord);
	             	    world.setBlock(cx + firstBlockXCoord, cy + bcy, cz + firstBlockZCoord, b, schem.data[i] , 2);
	             	    i++;
	             	    continue;
        			}
        		}	
             }
         }
        
        if (schem.tileentities != null)
        {
            for (int i1 = 0; i1 < schem.tileentities.tagCount(); ++i1)
            {
                NBTTagCompound nbttagcompound4 = schem.tileentities.getCompoundTagAt(i1);
                TileEntity tileentity = TileEntity.createAndLoadEntity(nbttagcompound4);

                if (tileentity != null)
                {
                    tileentity.xCoord = tileentity.xCoord + firstBlockXCoord;
                    tileentity.yCoord = tileentity.yCoord + bcy;
                    tileentity.zCoord = tileentity.zCoord + firstBlockZCoord;
                    world.setTileEntity(tileentity.xCoord, tileentity.yCoord, tileentity.zCoord, tileentity);
                }
            }
        }
        System.out.println("Succesfull loaded schematic!");

     }
}
}

 

 

But now I have a problem :

1. Where is the start of a schematic  ? Sometimes buildings spawn in air or above some trees or over the earth also with BlocksInGround setted...

Thank you for help!

Link to comment
Share on other sites

  • 5 months later...

Note: I know this is technically a necro post, but I might as well add on to the previous post.

 

Start of schematic is one of bottom corners.

 

If you implement your Schematic input as MultiMote has, your schematics start in the bottom NW corner.

 

 

The following example assumes your for loops look something like this:

(((pretend you are loading a 5x5x1 schematic (because it is hard to make 3d objects in a text document on a 2d screen  :-[))))

for(y = 0; y < schem.height; y++)
{
    for(z = 0; z < schem.length; z++)
    {
        for(x = 0; x < schem.width; x++)
        {
            generate(...);
        }
    }
}

 

 

 

 

 

EXAMPLE:

All of these are done assuming:

  N

W  E

  S

1

.

 

2

. .

 

3

. . .

 

4

. . . .

 

5

. . . . .

 

6

. . . . .

.

 

7

. . . . .

. .

 

8

. . . . .

. . .

 

9

. . . . .

. . . .

 

(skip to the end)

25

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

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.