From 172e3e3af140eeb2b7d3c3735e9d1845c678eaac Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Sat, 15 Jun 2013 08:07:33 -0400 Subject: [PATCH] Cleaned up and tweaked DungeonHelper Cleaned up the indentation and empty lines in DungeonHelper. Set metadataFlipList and metadataNextList to have generic type Integer, since they're used to store block IDs. Whenever possible, we should always be using parameterized collections instead of leaving their types unspecified. I'd like to fix up the schematic HashMap in the future - unless it's necessary, we shouldn't be storing values of various types in that collection. Strongly typed variables or a class with the appropriate fields would be much cleaner. --- .../mod_pocketDim/helpers/DungeonHelper.java | 857 ++++++++---------- 1 file changed, 374 insertions(+), 483 deletions(-) diff --git a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java index 1c915e94..edbdc834 100644 --- a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java +++ b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java @@ -4,14 +4,10 @@ import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.DungeonGenerator; @@ -19,190 +15,134 @@ import StevenDimDoors.mod_pocketDim.LinkData; import StevenDimDoors.mod_pocketDim.mod_pocketDim; import StevenDimDoors.mod_pocketDim.helpers.jnbt.ByteArrayTag; import StevenDimDoors.mod_pocketDim.helpers.jnbt.CompoundTag; -import StevenDimDoors.mod_pocketDim.helpers.jnbt.IntTag; import StevenDimDoors.mod_pocketDim.helpers.jnbt.ListTag; import StevenDimDoors.mod_pocketDim.helpers.jnbt.NBTOutputStream; import StevenDimDoors.mod_pocketDim.helpers.jnbt.ShortTag; -import StevenDimDoors.mod_pocketDim.helpers.jnbt.StringTag; import StevenDimDoors.mod_pocketDim.helpers.jnbt.Tag; -/** - - * @Return - */ - - - public class DungeonHelper { - - public DungeonHelper() - { - if (properties == null) - properties = DDProperties.instance(); - } - private static DDProperties properties = null; - + private Random rand = new Random(); - - public HashMap customDungeonStatus = new HashMap(); - public ArrayList customDungeons = new ArrayList(); - - public ArrayList registeredDungeons = new ArrayList(); - - public ArrayList weightedDungeonGenList = new ArrayList(); + public HashMap customDungeonStatus = new HashMap(); - - public ArrayList simpleHalls = new ArrayList(); + public ArrayList customDungeons = new ArrayList(); + public ArrayList registeredDungeons = new ArrayList(); + public ArrayList weightedDungeonGenList = new ArrayList(); + public ArrayList simpleHalls = new ArrayList(); + public ArrayList complexHalls = new ArrayList(); + public ArrayList deadEnds = new ArrayList(); + public ArrayList hubs = new ArrayList(); + public ArrayList mazes = new ArrayList(); + public ArrayList pistonTraps = new ArrayList(); + public ArrayList exits = new ArrayList(); - - public ArrayList complexHalls = new ArrayList(); - - - public ArrayList deadEnds = new ArrayList(); - - - public ArrayList hubs = new ArrayList(); - - - public ArrayList mazes = new ArrayList(); - - - public ArrayList pistonTraps = new ArrayList(); - - - public ArrayList exits = new ArrayList(); - public ArrayList tagList = new ArrayList(); + public ArrayList metadataFlipList = new ArrayList(); + public ArrayList metadataNextList = new ArrayList(); + + public DungeonGenerator defaultUp = new DungeonGenerator(0, "/schematic/simpleStairsUp.schematic", true); - - public ArrayList metadataFlipList = new ArrayList(); - - public ArrayList metadataNextList = new ArrayList(); - - public DungeonGenerator defaultUp = new DungeonGenerator(0, "/schematic/simpleStairsUp.schematic", true); - public void registerCustomDungeon(File schematicFile) { try - { - - if(schematicFile.getName().contains(".schematic")) { - String[] name = schematicFile.getName().split("_"); - - if(name.length<4) + if(schematicFile.getName().contains(".schematic")) { - System.out.println("Could not parse filename tags, not adding dungeon to generation lists"); - this.customDungeons.add(new DungeonGenerator(0,schematicFile.getAbsolutePath(),true)); - System.out.println("Imported "+schematicFile.getName()); + String[] name = schematicFile.getName().split("_"); - - } - else if(!(name[2].equals("open")||name[2].equals("closed"))||!this.tagList.contains(name[0])) - { - System.out.println("Could not parse filename tags, not adding dungeon to generation lists"); - this.customDungeons.add(new DungeonGenerator(0,schematicFile.getAbsolutePath(),true)); - System.out.println("Imported "+schematicFile.getName()); - - } - else - { - int count=0; - - boolean open= name[2].equals("open"); - - int weight = Integer.parseInt(name[3].replace(".schematic", "")); - - String path = schematicFile.getAbsolutePath(); - - while(count tileEntities = new ArrayList(); - - - ArrayList tileEntites= new ArrayList(); - byte[] blocks = new byte[width * height * length]; - byte[] addBlocks = null; - byte[] blockData = new byte[width * height * length]; - for (int x = 0; x < width; ++x) - { - for (int y = 0; y < height; ++y) - { - for (int z = 0; z < length; ++z) - { - int index = y * width * length + z * width + x; - int blockID = world.getBlockId(x+xMin, y+yMin, z+zMin); - int meta= world.getBlockMetadata(x+xMin, y+yMin, z+zMin); - - if(blockID==properties.DimensionalDoorID) - { - blockID=Block.doorIron.blockID; - } - if(blockID==properties.WarpDoorID) - { - blockID=Block.doorWood.blockID; + //ArrayList tileEntities = new ArrayList(); + ArrayList tileEntites = new ArrayList(); + byte[] blocks = new byte[width * height * length]; + byte[] addBlocks = null; + byte[] blockData = new byte[width * height * length]; - } - - // Save 4096 IDs in an AddBlocks section - if (blockID > 255) - { - if (addBlocks == null) - { // Lazily create section - addBlocks = new byte[(blocks.length >> 1) + 1]; - } + for (int x = 0; x < width; ++x) + { + for (int y = 0; y < height; ++y) + { + for (int z = 0; z < length; ++z) + { + int index = y * width * length + z * width + x; + int blockID = world.getBlockId(x+xMin, y+yMin, z+zMin); + int meta= world.getBlockMetadata(x+xMin, y+yMin, z+zMin); - addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? - addBlocks[index >> 1] & 0xF0 | (blockID >> 8) & 0xF - : addBlocks[index >> 1] & 0xF | ((blockID >> 8) & 0xF) << 4); - } + if(blockID==properties.DimensionalDoorID) + { + blockID=Block.doorIron.blockID; + } + if(blockID==properties.WarpDoorID) + { + blockID=Block.doorWood.blockID; - blocks[index] = (byte) blockID; - blockData[index] = (byte) meta; - - if (Block.blocksList[blockID] instanceof BlockContainer) - { - //TODO fix this - /** + } + + // Save 4096 IDs in an AddBlocks section + if (blockID > 255) + { + if (addBlocks == null) + { // Lazily create section + addBlocks = new byte[(blocks.length >> 1) + 1]; + } + + addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? + addBlocks[index >> 1] & 0xF0 | (blockID >> 8) & 0xF + : addBlocks[index >> 1] & 0xF | ((blockID >> 8) & 0xF) << 4); + } + + blocks[index] = (byte) blockID; + blockData[index] = (byte) meta; + + if (Block.blocksList[blockID] instanceof BlockContainer) + { + //TODO fix this + /** TileEntity tileEntityBlock = world.getBlockTileEntity(x+xMin, y+yMin, z+zMin); NBTTagCompound tag = new NBTTagCompound(); tileEntityBlock.writeToNBT(tag); - + CompoundTag tagC = new CompoundTag("TileEntity",Map.class.cast(tag.getTags())); - - + + // Get the list of key/values from the block - + if (tagC != null) { tileEntites.add(tagC); } - **/ - } - - - } - } - } - /** - * - * this.nbtdata.setShort("Width", width); + **/ + } + } + } + } + /** + * + * this.nbtdata.setShort("Width", width); this.nbtdata.setShort("Height", height); this.nbtdata.setShort("Length", length); - + this.nbtdata.setByteArray("Blocks", blocks); this.nbtdata.setByteArray("Data", blockData); - */ - - HashMap schematic = new HashMap(); - schematic.put("Blocks", new ByteArrayTag("Blocks", blocks)); - schematic.put("Data", new ByteArrayTag("Data", blockData)); - - schematic.put("Width", new ShortTag("Width", (short) width)); - schematic.put("Length", new ShortTag("Length", (short) length)); - schematic.put("Height", new ShortTag("Height", (short) height)); - schematic.put("TileEntites", new ListTag("TileEntities",CompoundTag.class,tileEntites)); - if (addBlocks != null) { - schematic.put("AddBlocks", new ByteArrayTag("AddBlocks", addBlocks)); - } - - CompoundTag schematicTag = new CompoundTag("Schematic", schematic); - try - { - - - NBTOutputStream stream = new NBTOutputStream(new FileOutputStream(file)); - stream.writeTag(schematicTag); - stream.close(); - } - catch(Exception e) - { - e.printStackTrace(); - } - this.registerCustomDungeon(new File(file)); - - return new DungeonGenerator(0,file,true); + */ + + HashMap schematic = new HashMap(); + schematic.put("Blocks", new ByteArrayTag("Blocks", blocks)); + schematic.put("Data", new ByteArrayTag("Data", blockData)); + + schematic.put("Width", new ShortTag("Width", (short) width)); + schematic.put("Length", new ShortTag("Length", (short) length)); + schematic.put("Height", new ShortTag("Height", (short) height)); + schematic.put("TileEntites", new ListTag("TileEntities",CompoundTag.class,tileEntites)); + if (addBlocks != null) { + schematic.put("AddBlocks", new ByteArrayTag("AddBlocks", addBlocks)); + } + + CompoundTag schematicTag = new CompoundTag("Schematic", schematic); + try + { + + + NBTOutputStream stream = new NBTOutputStream(new FileOutputStream(file)); + stream.writeTag(schematicTag); + stream.close(); + } + catch(Exception e) + { + e.printStackTrace(); + } + this.registerCustomDungeon(new File(file)); + + return new DungeonGenerator(0, file, true); } - - - + public void generateDungeonlink(LinkData incoming) { //DungeonGenerator dungeon = mod_pocketDim.registeredDungeons.get(new Random().nextInt(mod_pocketDim.registeredDungeons.size())); DungeonGenerator dungeon; int depth = dimHelper.instance.getDimDepth(incoming.locDimID)+2; - + int depthWeight = rand.nextInt(depth)+rand.nextInt(depth)-2; - + depth=depth-2; - // DungeonGenerator + // DungeonGenerator boolean flag = true; int count=10; try { - if(dimHelper.dimList.get(incoming.destDimID)!=null&&dimHelper.dimList.get(incoming.destDimID).dungeonGenerator!=null) { mod_pocketDim.loader.init(incoming); dimHelper.dimList.get(incoming.destDimID).dungeonGenerator=dimHelper.dimList.get(incoming.destDimID).dungeonGenerator; return; } - if(incoming.destYCoord>15) - { - do - { - count--; - flag = true; - dungeon = this.weightedDungeonGenList.get(rand.nextInt(weightedDungeonGenList.size())); - - if(depth<=1) + if(incoming.destYCoord>15) { - if(rand.nextBoolean()) + do { - dungeon = complexHalls.get(rand.nextInt(complexHalls.size())); + count--; + flag = true; + dungeon = this.weightedDungeonGenList.get(rand.nextInt(weightedDungeonGenList.size())); - } - else if(rand.nextBoolean()) - { - dungeon = hubs.get(rand.nextInt(hubs.size())); - - } - else if(rand.nextBoolean()) - { - dungeon = hubs.get(rand.nextInt(hubs.size())); - - } - else if(deadEnds.contains(dungeon)||exits.contains(dungeon)) + if(depth<=1) + { + if(rand.nextBoolean()) { - flag=false; + dungeon = complexHalls.get(rand.nextInt(complexHalls.size())); + } - - - - } - else if(depth<=3&&(deadEnds.contains(dungeon)||exits.contains(dungeon)||rand.nextBoolean())) - { - if(rand.nextBoolean()) - { - dungeon = hubs.get(rand.nextInt(hubs.size())); - - } - else if(rand.nextBoolean()) - { - dungeon = mazes.get(rand.nextInt(mazes.size())); - } - else if(rand.nextBoolean()) - { - dungeon = pistonTraps.get(rand.nextInt(pistonTraps.size())); + else if(rand.nextBoolean()) + { + dungeon = hubs.get(rand.nextInt(hubs.size())); - } - else - { - flag=false; - } - } - else if(rand.nextInt(3)==0&&!complexHalls.contains(dungeon)) - { - if(rand.nextInt(3)==0) - { - dungeon = simpleHalls.get(rand.nextInt(simpleHalls.size())); - } - else if(rand.nextBoolean()) - { - dungeon = pistonTraps.get(rand.nextInt(pistonTraps.size())); + } + else if(rand.nextBoolean()) + { + dungeon = hubs.get(rand.nextInt(hubs.size())); - } - else if(depth<4) - { - dungeon = hubs.get(rand.nextInt(hubs.size())); - - } - - } - else if(depthWeight-depthWeight/2>depth-4&&(deadEnds.contains(dungeon)||exits.contains(dungeon))) - { - if(rand.nextBoolean()) - { - dungeon = simpleHalls.get(rand.nextInt(simpleHalls.size())); - } - else if(rand.nextBoolean()) - { - dungeon = complexHalls.get(rand.nextInt(complexHalls.size())); - } - else if(rand.nextBoolean()) - { - dungeon = pistonTraps.get(rand.nextInt(pistonTraps.size())); - - } - else - { - flag=false; - } - } - else if(depthWeight>7&&hubs.contains(dungeon)) - { - if(rand.nextInt(12)+5depth-4&&(deadEnds.contains(dungeon)||exits.contains(dungeon))) + { + if(rand.nextBoolean()) + { + dungeon = simpleHalls.get(rand.nextInt(simpleHalls.size())); + } + else if(rand.nextBoolean()) + { + dungeon = complexHalls.get(rand.nextInt(complexHalls.size())); + } + else if(rand.nextBoolean()) + { + dungeon = pistonTraps.get(rand.nextInt(pistonTraps.size())); + + } + else + { + flag=false; + } + } + else if(depthWeight>7&&hubs.contains(dungeon)) + { + if(rand.nextInt(12)+510&&hubs.contains(dungeon)) + { + flag = false; } - - } - else - { - flag = false; } + while (!flag && count > 0); } - else if(depth>10&&hubs.contains(dungeon)) + else { - flag = false; + dungeon = defaultUp; } - } - while(!flag&&count>0); - } - else + catch (Exception e) { - dungeon= defaultUp; - } - } - catch(Exception e) - { - if(weightedDungeonGenList.size()>0) + if (weightedDungeonGenList.size() > 0) { dungeon = weightedDungeonGenList.get(rand.nextInt(weightedDungeonGenList.size())); } @@ -673,22 +580,6 @@ public class DungeonHelper return; } } - - - - dimHelper.dimList.get(incoming.destDimID).dungeonGenerator=dungeon; - //loader.generateSchematic(incoming,0,0,0); - - - - - - - - + dimHelper.dimList.get(incoming.destDimID).dungeonGenerator = dungeon; } - - - - } \ No newline at end of file