Posted October 10, 20159 yr Hey guys! I made a schematic reader like the coder of the mod "Instant Structures" made. Instead of hardcoding the gen, you can directly read the schematic file. So I wanted to ask how I can prevent that the structures spawn IN the floors and stuff. I tried this: package netcrafter.mods.aoto.world.gen.feature; import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import net.minecraft.world.biome.BiomeGenBase; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.gen.feature.WorldGenerator; import net.minecraftforge.fml.common.IWorldGenerator; import netcrafter.mods.aoto.util.Schematic; public class ForestGen implements IWorldGenerator { private Schematic schematic; public ForestGen(Schematic schematic) { this.schematic = schematic; } protected Block[] GetValidSpawnBlocks() { return new Block[] { Blocks.grass, Blocks.stone, Blocks.dirt, }; } public boolean LocationIsValidSpawn(World world, int x, int y, int z) { int distanceToAir = 0; Block checkBlock = world.getBlockState(new BlockPos(x, y, z)).getBlock(); while (checkBlock != Blocks.air) { distanceToAir++; checkBlock = world.getBlockState(new BlockPos(x, y + distanceToAir, z)).getBlock(); } if (distanceToAir > 1) { return false; } y += distanceToAir - 1; Block block = world.getBlockState(new BlockPos(x, y, z)).getBlock(); Block blockAbove = world.getBlockState(new BlockPos(x, y + 1, z)).getBlock(); Block blockBelow = world.getBlockState(new BlockPos(x, y - 1, z)).getBlock(); for (Block i : GetValidSpawnBlocks()) { if (blockAbove != Blocks.air) { return false; }if (block == i) { return true; }else if (block == Blocks.snow_layer && blockBelow == i) { return true; }else if (block.getMaterial() == Material.plants && blockBelow == i) { return true; } } return false; } @Override public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { if(random.nextInt(200) == 0) { int x = chunkX * 16 + random.nextInt(16); int z = chunkZ * 16 + random.nextInt(16); int y = getWorldHeightAt(world, x, z); if(LocationIsValidSpawn(world, x, y, z) || LocationIsValidSpawn(world, x + 20, y, z) || LocationIsValidSpawn(world, x + 20, y, z + 16) || LocationIsValidSpawn(world, x, y, z + 16)) { if(world.getBiomeGenForCoords(new BlockPos(x, y, z)) == BiomeGenBase.forest) { schematic.generate(world, x, y, z); System.out.println("FOREST DUNGEON GENERATED!"); } }else{ System.out.println("SPAWN LOCATION IS NOT VALID"); } } } private static int getWorldHeightAt(World world, int x, int z) { int height = 0; for(int i = 0; i < 255; i++) { if(world.getBlockState(new BlockPos(x, i, z)).getBlock().isSolidFullCube()) { height = i; } } return height; } } The structure still spawns in walls, in floors, in tree 'n stuff. So, it doesn't seem to work correctly.. Maybe I am just overseeing an error in the code? Do you have the solution for a "perfect" structure generation? Please help me - DaryBob
October 10, 20159 yr Author Any help? I also customized the schematic reader and not just copy pasted it. What I meant is, the structure spawns IN mountains, IN the ground etc. And I want it to spawn on a free location. What could I do?
October 10, 20159 yr Well, your code is kind of weird: if (distanceToAir > 1) { return false; } Why even check how long it takes to get to an air block if you are just going to return any time the block directly above the y coordinate isn't air? I suspect there are probably more logic issues like this in your code, but on to the main issue: if you want to only generate your structure in a clear space, there is only one way to do so - check if the space is clear before generating it. To do that, you need to know the length, width, and height of your structure and the location at which it will be generating, then check all* of the blocks within that area. * If you are clever, you will write your code in a way that fails as early as possible so you don't waste lots of time continuing to check when it has already failed, and you can also write it in a way that allows for some non-air blocks, depending on how strict you want to be. http://i.imgur.com/NdrFdld.png[/img]
October 15, 20159 yr Author So I could use this? public boolean isLocationValid() { private boolean flag0; private boolean flag1; private boolean flag2; for(int x1 = 0; x1 > blocksOfX; x1++) { if(!world.getBlockState(x1, y z) == Blocks.air) return false; }else{ flag2 = true; } for(int y1 = 0; y1 > blocksOfY; x1++) { if(!world.getBlockState(x, y1,z) == Blocks.air) return false; }else{ flag1 = true; } for(int y1 = 0; y1 > blocksOfX; y1++) { if(!world.getBlockState(x, y z1) == Blocks.air) return false; }else{ flag0 = true; } if(flag0 && flag1 && flag2) { return true; } return false; }
October 15, 20159 yr I'd be more sophisticated than simply checking for air. You don't want tall grass, bushes, flowers, tree-leaves etc stopping you. You might want to code a simple function for what kinds of blocks you're willing/unwilling to overwrite (and celebrate if you can find a common attribute like transparent/opaque that distinguishes them). As for your method, it does not even look to be syntactically correct, so I can't read it. However, it looks as if your loops are not nested, so you might only be checking edges of a space, not its entire volume. Please rewrite and format it in an IDE, and then paste between CODE tags to preserve formatting. The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.
October 17, 20159 yr Author Yeah with the code I posted up there, I would only check the edges.. A simple and better one I came up with would look like this: public boolean isLocationValid(World world, int x, int y, int z, int blocksLength, int blocksHeight, int blocksWidth) { for (int i = x; i <= blocksLength; i++) { for (int j = y; j <= blocksHeight; j++) { for (int k = z; k <= blocksWidth; k++) { if (!world.getBlockState(new BlockPos(i, j, k)).getBlock() == Blocks.air) { return false; } } } } return true; } The last code was kinda messed up.. I just added so many unneccesary things like flags and stuff.. and it doesn't even check all blocks.. But this one should do it right? And later I could add some more things to override plants.. Can I check the if the block is a plant? Some thing like: if(world.getBlockState(x, y, z) == Material.plant) The check didn't made sense I think, but I think you understand what I mean.. EDIT: Found out how to check the material: world.getBlockState(new BlockPos(x, y, z)).getMaterial() == Material.plant I can't test the codes because I am not home right now.
October 17, 20159 yr Assuming that x, y, and z are the coordinates for the floor-level northwestern corner of your structure then yes, that code should check all of the blocks. As jeffryfisher pointed out, however, it might be wiser to make a function to handle the check, e.g. instead of: if (!world.getBlockState(new BlockPos(i, j, k)).getBlock() == Blocks.air) { return false; } You would put: if (!myIsBlockValidMethod(world, new BlockPos(i, j, k))) { return false; } Then you can add as much complexity as you want to the block check within that method without a. having all the code indented 5 times and b. making a huge mess in the for-loop. Of course, it probably will not have any functional benefit for you, but it is cleaner stylistically. On another note, you have some weird / incorrect syntax: if (!world.getBlockState(new BlockPos(i, j, k)).getBlock() == Blocks.air) { // the not '!' should go with the '=', not in front of the method, as the method does not return true/false if (world.getBlockState(new BlockPos(i, j, k)).getBlock() != Blocks.air) { http://i.imgur.com/NdrFdld.png[/img]
October 17, 20159 yr Author public boolean isBlockValid(World world, BlockPos pos) { Block block = world.getBlockState(pos); if(block.getMaterial() == Material.plant) { return true; }if(block == Blocks.air){ return true; } return false; } Because I wanted to override plants too, I tryed this. Also, maybe I will add these methods to a structure helper class or something.. So I can use it for other Structures. This should work I think.
October 18, 20159 yr Also, rather than checking equal to Blocks air, it is probably better to call the isAir method on each block. That way, if there are any other blocks masquerading as air (especially from mods you never heard about), then those good-as-air blocks would be treated as air even though they're not actual air blocks. The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.
October 20, 20159 yr Author Now it's kinda better... But it still overrides block's I dont want.. I check the blocks under the floor block too. Maybe I messed up the code again.. public class ForestGen implements IWorldGenerator { static final int strWidth = 18; static final int strHeight = 7; static final int strDepth = 17; private Schematic schematic; public ForestGen(Schematic schematic) { this.schematic = schematic; } public static boolean isStructureLocationValid(World world, int x, int y, int z, int blocksLength, int blocksHeight, int blocksWidth) { for (int i = x; i <= blocksLength; i++) { for (int j = y; j <= blocksHeight; j++) { for (int k = z; k <= blocksWidth; k++) { if(!isBlockValid(world, x, y, z)) { return false; } } } } for(int a = x; a <= blocksLength; a++) { for(int c = z; c <= blocksWidth; c++) { if(!isGroundValid(world, a, y, c)) { return false; } } } return true; } public static boolean isBlockValid(World world, int x, int y, int z) { Block block = world.getBlockState(new BlockPos(x, y, z)).getBlock(); Block blockBelow = world.getBlockState(new BlockPos(x, y - 1, z)).getBlock(); if(block.getMaterial() == Material.plants && isValidBlock(blockBelow, world, x, y, z)) { return true; }if(block.isAir(world, new BlockPos(x, y, z))) { return true; }if (block == Blocks.snow_layer && isValidBlock(blockBelow, world, x, y, z)) { return true; } return false; } public static boolean isGroundValid(World world, int x, int y, int z) { Block groundBlock = world.getBlockState(new BlockPos(x, y - 1, z)).getBlock(); if(isValidBlock(groundBlock, world, x, y, z)) { return true; } return false; } public static boolean isValidBlock(Block block, World world, int x, int y, int z) { block = world.getBlockState(new BlockPos(x, y, z)).getBlock(); if(block == Blocks.dirt || block == Blocks.grass || block == Blocks.stone) { return true; } return false; } @Override public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { if(random.nextInt(200) == 0) { int x = chunkX * 16 + random.nextInt(16); int z = chunkZ * 16 + random.nextInt(16); int y = getWorldHeightAt(world, x, z); if(isStructureLocationValid(world, x, y, z, strWidth, strHeight, strDepth)) { if(world.getBiomeGenForCoords(new BlockPos(x, y, z)) == BiomeGenBase.forest) { schematic.generate(world, x, y, z); System.out.println("FOREST DUNGEON GENERATED!"); } System.out.println("FOREST DUNGEON SPOT WAS NOT VALID"); } } } private static int getWorldHeightAt(World world, int x, int z) { int height = 0; for(int i = 0; i < 255; i++) { if(world.getBlockState(new BlockPos(x, i, z)).getBlock().isSolidFullCube()) { height = i; } } return height; } }
October 21, 20159 yr If you want your structure to 'meld' better into the surrounding environment, one trick is to allow certain blocks to not be placed if there is already a block there. The major example is air blocks - if you can avoid overwriting huge sections of the land with air blocks, i.e. leave the land as it is, that's a big win as far as looks go. Another example is your stone steps, especially toward the lower levels - instead of forcing the stone steps to go all the way down, you could place them instead only if there isn't already a solid block there. This way they only go down as far as they need to. Other than that, it's looking pretty good. http://i.imgur.com/NdrFdld.png[/img]
October 25, 20159 yr Author I do not have an idea how to stop overriding blocks with airblocks lol.. Could you give me a tip or something? Or how you would do it?
October 25, 20159 yr Author I was thinking 'bout it. Does it even check my codes? Because it somehow still overrides trees, dirt even though I am checking first if the location is clear. It spawns on coal ore's, even though I am checking if the ground is dirt, stone or grass. Is it really checking all that stuff? It's strange.. GeneratorUtils.java package netcrafter.mods.aoto.util; import net.minecraft.block.Block; import net.minecraft.block.BlockChest; import net.minecraft.block.BlockMobSpawner; import net.minecraft.block.material.Material; import net.minecraft.block.state.BlockState; import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import netcrafter.mods.aoto.init.AOTOBlocks; public class GeneratorUtils { //STRUCTURES -------------------------------------------------------------------------------------------------------------------------- public static boolean isStructureLocationValid(World world, int x, int y, int z, int blocksLength, int blocksHeight, int blocksWidth) { for (int i = x; i <= blocksLength; i++) { for (int j = y; j <= blocksHeight; j++) { for (int k = z; k <= blocksWidth; k++) { if(!isBlockValid(world, i, j, k)) { return false; } } } } for(int a = x; a <= blocksLength; a++) { for(int c = z; c <= blocksWidth; c++) { if(!isGroundValid(world, a, y, c)) { return false; } } } return true; } public static boolean isBlockValid(World world, int x, int y, int z) { Block block = world.getBlockState(new BlockPos(x, y, z)).getBlock(); Block blockBelow = world.getBlockState(new BlockPos(x, y - 1, z)).getBlock(); if(block.getMaterial() == Material.plants && isValidBlock(blockBelow, world, x, y, z)) { return true; }if(block.isAir(world, new BlockPos(x, y, z))) { return true; }if (block == Blocks.snow_layer && isValidBlock(blockBelow, world, x, y, z)) { return true; }if (block == Blocks.water || blockBelow == Blocks.water) { return false; } return false; } public static boolean meldStructure(Block airBlock, World world, int x, int y, int z) { if(!airBlock.isAir(world, new BlockPos(x, y, z))) { return true; } return false; } public static boolean isGroundValid(World world, int x, int y, int z) { Block groundBlock = world.getBlockState(new BlockPos(x, y - 1, z)).getBlock(); if(isValidBlock(groundBlock, world, x, y, z)) { return true; } return false; } public static boolean isValidBlock(Block block, World world, int x, int y, int z) { block = world.getBlockState(new BlockPos(x, y, z)).getBlock(); if(block == Blocks.dirt || block == Blocks.grass || block == Blocks.stone) { return true; } return false; } public static boolean isBlockChestOrSpawner(World world, int x, int y, int z, int blocksLength, int blocksHeight, int blocksWidth) { Block block = world.getBlockState(new BlockPos(x, y, z)).getBlock(); for (int i = x; i <= blocksLength; i++) { for (int j = y; j <= blocksHeight; j++) { for (int k = z; k <= blocksWidth; k++) { if(block instanceof BlockMobSpawner) { setSpawner(world, i, j, k); return true; } if(block instanceof BlockChest) { return true; } } } } return false; } public static void setSpawner(World world, int x, int y, int z) { world.setBlockState(new BlockPos(x, y, z), AOTOBlocks.forest_guard_spawner.getDefaultState()); } public static void fillChest() { } } ForestGen.java package netcrafter.mods.aoto.world.gen.feature; import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import net.minecraft.world.biome.BiomeGenBase; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.gen.feature.WorldGenerator; import net.minecraftforge.fml.common.IWorldGenerator; import netcrafter.mods.aoto.util.GeneratorUtils; import netcrafter.mods.aoto.util.Schematic; public class ForestGen implements IWorldGenerator { static final int strWidth = 18; static final int strHeight = 8; static final int strDepth = 17; private Schematic schematic; public ForestGen(Schematic schematic) { this.schematic = schematic; } @Override public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { if(random.nextInt(200) == 0) { int x = chunkX * 16 + random.nextInt(16); int z = chunkZ * 16 + random.nextInt(16); int y = getWorldHeightAt(world, x, z); if(GeneratorUtils.isStructureLocationValid(world, x, y, z, strWidth, strHeight, strDepth)) { if(world.getBiomeGenForCoords(new BlockPos(x, y, z)) == BiomeGenBase.forest) { schematic.generate(world, x, y, z); System.out.println("FOREST DUNGEON GENERATED!"); if(GeneratorUtils.isBlockChestOrSpawner(world, x, y, z, strWidth + 4, strHeight + 4, strDepth + 4)) { } } } } } private static int getWorldHeightAt(World world, int x, int z) { int height = 0; for(int i = 0; i < 255; i++) { if(world.getBlockState(new BlockPos(x, i, z)).getBlock().isSolidFullCube()) { height = i; } } return height; } }
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.