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 java.util.regex.Pattern; 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(); private static final String SCHEMATIC_FILE_EXTENSION = ".schematic"; private static final int DEFAULT_DUNGEON_WEIGHT = 100; public static Pattern NamePattern = Pattern.compile("[A-Za-z0-9_]+"); public HashMap customDungeonStatus = new HashMap(); public ArrayList customDungeons = new ArrayList(); public ArrayList registeredDungeons = new ArrayList(); private ArrayList weightedDungeonGenList = new ArrayList(); private ArrayList simpleHalls = new ArrayList(); private ArrayList complexHalls = new ArrayList(); private ArrayList deadEnds = new ArrayList(); private ArrayList hubs = new ArrayList(); private ArrayList mazes = new ArrayList(); private ArrayList pistonTraps = new ArrayList(); private 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 DungeonHelper() { if (properties == null) properties = DDProperties.instance(); } public boolean validateSchematicName(String name) { String[] dungeonData = name.split("_"); //Check for a valid number of parts if (dungeonData.length < 3 || dungeonData.length > 4) return false; //Check if the category is valid if (!tagList.contains(dungeonData[0])) return false; //Check if the name is valid if (!NamePattern.matcher(dungeonData[1]).matches()) return false; //Check if the open/closed flag is present if (!dungeonData[2].equalsIgnoreCase("open") && !dungeonData[2].equalsIgnoreCase("closed")) return false; //If the weight is present, check that it is valid if (dungeonData.length == 4) { try { int weight = Integer.parseInt(dungeonData[3]); if (weight < 0) return false; } catch (NumberFormatException e) { //Not a number return false; } } return true; } public void registerCustomDungeon(File schematicFile) { String name = schematicFile.getName(); try { if (name.endsWith(SCHEMATIC_FILE_EXTENSION) && validateSchematicName(name)) { //Strip off the file extension while splitting the file name String[] dungeonData = name.substring(0, name.length() - SCHEMATIC_FILE_EXTENSION.length()).split("_"); String path = schematicFile.getAbsolutePath(); boolean open = dungeonData[2].equals("open"); int weight = (dungeonData.length == 4) ? Integer.parseInt(dungeonData[3]) : DEFAULT_DUNGEON_WEIGHT; //Change this code so that instead of using IFs, we use a hash table mapping (category) -> (list) /*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; } }