package StevenDimDoors.mod_pocketDim.helpers; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.DungeonGenerator; 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.ListTag; import StevenDimDoors.mod_pocketDim.helpers.jnbt.NBTOutputStream; import StevenDimDoors.mod_pocketDim.helpers.jnbt.ShortTag; import StevenDimDoors.mod_pocketDim.helpers.jnbt.Tag; public class DungeonHelper { 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 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 tagList = new ArrayList(); 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) { 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 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; } // 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("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); } 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 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(rand.nextBoolean()) { dungeon = complexHalls.get(rand.nextInt(complexHalls.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)) { flag=false; } } 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 { 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(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)+510&&hubs.contains(dungeon)) { flag = false; } } while (!flag && count > 0); } else { dungeon = defaultUp; } } catch (Exception e) { if (weightedDungeonGenList.size() > 0) { dungeon = weightedDungeonGenList.get(rand.nextInt(weightedDungeonGenList.size())); } else { e.printStackTrace(); return; } } dimHelper.dimList.get(incoming.destDimID).dungeonGenerator = dungeon; } }