diff --git a/StevenDimDoors/mod_pocketDim/DimData.java b/StevenDimDoors/mod_pocketDim/DimData.java new file mode 100644 index 00000000..7bf4ed06 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/DimData.java @@ -0,0 +1,257 @@ +package StevenDimDoors.mod_pocketDim; +/**Class that contains all the information about a specific dim that is pertienent to Dim Doors. Holds all the rifts present in the dim sorted by x,y,z and +* wether or not the dim is a pocket or not, along with its depth. +* @Return +*/ +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + +import net.minecraft.util.MathHelper; +import net.minecraft.world.World; + +public class DimData implements Serializable +{ + public int dimID; + public int depth; + public int dimOrientation; + + public World world; + + public LinkData exitDimLink; + + public boolean isPocket; + public boolean hasBeenFilled=false; + public boolean hasDoor=false; + public boolean isDimRandomRift=false; + public Object dungeonGenerator = null; + //public boolean isPrivatePocket = false; + public HashMap<Integer, HashMap<Integer, HashMap<Integer, LinkData>>> linksInThisDim = new HashMap(); + HashMap<Integer, LinkData> dimX; + HashMap<Integer, HashMap<Integer, LinkData>> dimY ; + + static final long serialVersionUID = 454342L; + + public DimData(int dimID, boolean isPocket, int depth, LinkData exitLinkData) + { + this.dimID=dimID; + this.depth=depth; + this.isPocket=isPocket; + + this.exitDimLink= exitLinkData; + } + + public DimData(int dimID, boolean isPocket, int depth, int exitLinkDimID, int exitX, int exitY, int exitZ) + { + this(dimID, isPocket, depth, new LinkData(exitLinkDimID, exitX, exitY, exitZ)); + } + + public LinkData findNearestRift(World world, int range, int x, int y, int z) + { + LinkData nearest=null; + float distance=range+1; + int i=-range; + int j=-range; + int k=-range; + DDProperties properties = DDProperties.instance(); + + while (i<range) + { + while (j<range) + { + while (k<range) + { + if (world.getBlockId(x+i, y+j, z+k) == properties.RiftBlockID && MathHelper.abs(i)+MathHelper.abs(j)+MathHelper.abs(k)<distance) + { + if(MathHelper.abs(i)+MathHelper.abs(j)+MathHelper.abs(k)!=0) + { + nearest=this.findLinkAtCoords(x+i, y+j, z+k); + distance=MathHelper.abs(i)+MathHelper.abs(j)+MathHelper.abs(k); + } + + } + k++; + } + k=-range; + j++; + + } + j=-range; + i++; + + } + + + return nearest; + + } + + public ArrayList findRiftsInRange(World world, int range, int x, int y, int z) + { + LinkData nearest=null; + ArrayList rifts = new ArrayList(); + int i=-range; + int j=-range; + int k=-range; + DDProperties properties = DDProperties.instance(); + + while (i<range) + { + while (j<range) + { + while (k<range) + { + if(world.getBlockId(x+i, y+j, z+k)==properties.RiftBlockID) + { + if(MathHelper.abs(i)+MathHelper.abs(j)+MathHelper.abs(k)!=0) + { + nearest=this.findLinkAtCoords(x+i, y+j, z+k); + if(nearest!=null) + { + rifts.add(nearest); + } + } + + } + k++; + } + k=-range; + j++; + + } + j=-range; + i++; + + } + + + return rifts; + + } + + + + public LinkData addLinkToDim(LinkData link) + { + if(this.linksInThisDim.containsKey(link.locZCoord)) + { + this.dimY=this.linksInThisDim.get(link.locZCoord); + + if(this.dimY.containsKey(link.locYCoord)) + { + this.dimX=this.dimY.get(link.locYCoord); + } + else + { + this.dimX=new HashMap<Integer, LinkData>(); + } + } + else + { + this.dimX=new HashMap<Integer, LinkData>(); + this.dimY=new HashMap<Integer, HashMap<Integer, LinkData>>(); + } + + this.dimX.put(link.locXCoord, link); + this.dimY.put(link.locYCoord, dimX); + this.linksInThisDim.put(link.locZCoord, dimY); + + //System.out.println("added link to dim "+this.dimID); + return link; + + } + + public LinkData addLinkToDim( int destinationDimID, int locationXCoord, int locationYCoord, int locationZCoord, int destinationXCoord, int destinationYCoord, int destinationZCoord, int linkOrientation) + { + LinkData linkData= new LinkData(this.dimID, destinationDimID, locationXCoord, locationYCoord, locationZCoord, destinationXCoord, destinationYCoord,destinationZCoord,this.isPocket,linkOrientation); + + return this.addLinkToDim(linkData); + } + + public boolean isLimbo() + { + return (this.dimID == DDProperties.instance().LimboDimensionID); + } + + public void removeLinkAtCoords(LinkData link) + { + this.removeLinkAtCoords(link.locDimID, link.locXCoord, link.locYCoord, link.locZCoord); + } + + public void removeLinkAtCoords(int locationID, int locationXCoord, int locationYCoord, int locationZCoord) + { + if (this.linksInThisDim.containsKey(locationZCoord)) + { + this.dimY=this.linksInThisDim.get(locationZCoord); + + if(this.dimY.containsKey(locationYCoord)) + { + this.dimX=this.dimY.get(locationYCoord); + } + else + { + this.dimX=new HashMap<Integer, LinkData>(); + } + } + else + { + this.dimX=new HashMap<Integer, LinkData>(); + this.dimY=new HashMap<Integer, HashMap<Integer, LinkData>>(); + } + + this.dimX.remove(locationXCoord); + this.dimY.put(locationYCoord, dimX); + this.linksInThisDim.put(locationZCoord, dimY); + } + + public LinkData findLinkAtCoords(int locationXCoord, int locationYCoord, int locationZCoord) + { + try + { + if(this.linksInThisDim.containsKey(locationZCoord)) + { + this.dimY=this.linksInThisDim.get(locationZCoord); + + if(this.dimY.containsKey(locationYCoord)) + { + this.dimX=this.dimY.get(locationYCoord); + + if(this.dimX.containsKey(locationXCoord)) + { + return this.dimX.get(locationXCoord); + } + + } + } + } + catch(Exception E) + { + return null; + } + return null; + } + + public ArrayList<LinkData> getLinksInDim() + { + //TODO: We might want to modify this function, but I'm afraid of breaking something right now. + //To begin with, the name is wrong. This doesn't print anything! >_o ~SenseiKiwi + + ArrayList<LinkData> links = new ArrayList<LinkData>(); + if (this.linksInThisDim == null) + { + return links; + } + for (HashMap<Integer, HashMap<Integer, LinkData>> first : this.linksInThisDim.values()) + { + for (HashMap<Integer, LinkData> second : first.values()) + { + for (LinkData linkData : second.values()) + { + links.add(linkData); + } + } + } + return links; + } +} \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/DungeonGenerator.java b/StevenDimDoors/mod_pocketDim/DungeonGenerator.java new file mode 100644 index 00000000..e8ec50c7 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/DungeonGenerator.java @@ -0,0 +1,84 @@ +package StevenDimDoors.mod_pocketDim; + +import java.io.File; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; + +import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack; +import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType; +import StevenDimDoors.mod_pocketDim.helpers.DungeonHelper; + +public class DungeonGenerator implements Serializable +{ + //This static field is hax so that I don't have to add an instance field to DungeonGenerator to support DungeonType. + //Otherwise it would have to be serializable and all sorts of problems would arise. + private static final HashMap<DungeonGenerator, DungeonType> dungeonTypes = new HashMap<DungeonGenerator, DungeonType>(); + + public int weight; + public String schematicPath; + public ArrayList<HashMap> sideRifts = new ArrayList<HashMap>(); + public LinkData exitLink; + public boolean isOpen; + + public int sideDoorsSoFar=0; + public int exitDoorsSoFar=0; + public int deadEndsSoFar=0; + + public DungeonGenerator(int weight, String schematicPath, boolean isOpen, DungeonType dungeonType) + { + this.weight = weight; + this.schematicPath = schematicPath; + this.isOpen = isOpen; + + dungeonTypes.put(this, dungeonType); //Hax... + } + + public DungeonType getDungeonType() + { + DungeonType type = dungeonTypes.get(this); + if (type == null) + { + //Infer the dungeon's type from its file name + //There is minimal risk of us applying this to untagged dungeons and this'll be phased out + //when we get the new save format. + try + { + File file = new File(schematicPath); + String typeName = file.getName().split("_")[0]; + String packName = file.getParentFile().getName(); + DungeonPack pack = DungeonHelper.instance().getDungeonPack(packName); + if (pack == null) + { + pack = DungeonHelper.instance().getDungeonPack("ruins"); + } + type = pack.getType(typeName); + } + catch (Exception e) { } + if (type == null) + { + type = DungeonType.UNKNOWN_TYPE; + } + dungeonTypes.put(this, type); + } + return type; + } + + @Override + public int hashCode() + { + return (schematicPath != null) ? schematicPath.hashCode() : 0; + } + + @Override + public boolean equals(Object other) + { + return equals((DungeonGenerator) other); + } + + public boolean equals(DungeonGenerator other) + { + return ((this.schematicPath != null && this.schematicPath.equals(other.schematicPath)) || + (this.schematicPath == other.schematicPath)); + } +} \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/LinkData.java b/StevenDimDoors/mod_pocketDim/LinkData.java new file mode 100644 index 00000000..62130292 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/LinkData.java @@ -0,0 +1,68 @@ +package StevenDimDoors.mod_pocketDim; + +import java.io.Serializable; + +public class LinkData implements Serializable +{ + + public int locXCoord; + public int locYCoord; + public int locZCoord; + + public int destXCoord; + public int destYCoord; + public int destZCoord; + public int numberofChildren; + public boolean isLocPocket; + public int linkOrientation; + + public int destDimID; + public int locDimID; + + public boolean exists=false; + public boolean hasGennedDoor=false; + + static final long serialVersionUID = 45544342L; + + + public LinkData() + { + this.exists=false; + } + + public LinkData(int exitLinkDimID, int exitX, int exitY, int exitZ) + { + this.destDimID=exitLinkDimID; + this.destXCoord=exitX; + this.destYCoord=exitY; + this.destZCoord=exitZ; + } + + public LinkData(int locationDimID, int destinationDimID, int locationXCoord, int locationYCoord, int locationZCoord, int destinationXCoord, int destinationYCoord, int destinationZCoord, boolean isPocket,int orientation) + { + this.exists = true; + this.locXCoord=locationXCoord; + this.locYCoord=locationYCoord; + this.locZCoord=locationZCoord; + + this.destXCoord=destinationXCoord; + this.destYCoord=destinationYCoord; + this.destZCoord=destinationZCoord; + + this.destDimID=destinationDimID; + this.locDimID=locationDimID; + this.isLocPocket=isPocket; + this.linkOrientation=orientation; + } + + public String printLinkData() + { + //TODO: Rewrite this to make it prettier. @_@ I'm afraid of changing it to ToString() on the off + //chance it'll cause explosions and sadness. Damn serialization! ~SenseiKiwi + + String linkInfo; + linkInfo = String.valueOf(this.locDimID) + "locDimID "+String.valueOf(this.locXCoord)+":locXCoord "+String.valueOf(this.locYCoord)+":locYCoord "+String.valueOf(this.locZCoord)+":locZCoord "; + linkInfo.concat("\n"+ String.valueOf(this.destDimID)+"DestDimID "+String.valueOf(this.destXCoord)+":destXCoord "+String.valueOf(this.destYCoord)+":destYCoord "+String.valueOf(this.destZCoord)+":destZCoord "); + return linkInfo; + } +} \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index f3571714..79b11304 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -20,6 +20,7 @@ import StevenDimDoors.mod_pocketDim.helpers.Compactor; import StevenDimDoors.mod_pocketDim.helpers.DeleteFolder; import StevenDimDoors.mod_pocketDim.saving.DDSaveHandler; import StevenDimDoors.mod_pocketDim.saving.IPackable; +import StevenDimDoors.mod_pocketDim.saving.OldSaveImporter; import StevenDimDoors.mod_pocketDim.saving.PackedDimData; import StevenDimDoors.mod_pocketDim.saving.PackedDungeonData; import StevenDimDoors.mod_pocketDim.saving.PackedLinkData; @@ -380,9 +381,30 @@ public class PocketManager { System.out.println(!FMLCommonHandler.instance().getSide().isClient()); - if (DimensionManager.getCurrentSaveRootDirectory() != null) + File saveDir = DimensionManager.getCurrentSaveRootDirectory(); + if (saveDir != null) { // Load and register blacklisted dimension IDs + File oldSaveData = new File(saveDir+"/DimensionalDoorsData"); + if(oldSaveData.exists()) + { + try + { + System.out.println("Importing old DD save data..."); + OldSaveImporter.importOldSave(oldSaveData); + oldSaveData.delete(); + + System.out.println("Import Succesful!"); + + } + catch (Exception e) + { + //TODO handle fail cases + System.out.println("Import failed!"); + e.printStackTrace(); + } + return; + } // Load save data System.out.println("Loading Dimensional Doors save data..."); diff --git a/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java b/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java index 1b038a6d..efdf19ce 100644 --- a/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java +++ b/StevenDimDoors/mod_pocketDim/saving/DDSaveHandler.java @@ -75,7 +75,7 @@ public class DDSaveHandler * @param packedDims * @return */ - private static boolean unpackDimData(List<PackedDimData> packedDims) + public static boolean unpackDimData(List<PackedDimData> packedDims) { List<PackedDimData> unpackedDims = new ArrayList<PackedDimData>(); @@ -114,7 +114,7 @@ public class DDSaveHandler return true; } - private static boolean unpackLinkData(List<PackedLinkData> linksToUnpack) + public static boolean unpackLinkData(List<PackedLinkData> linksToUnpack) { Point3D fakePoint = new Point3D(-1,-1,-1); List<PackedLinkData> unpackedLinks = new ArrayList<PackedLinkData>(); @@ -181,6 +181,17 @@ public class DDSaveHandler File basePathFile = new File(basePath); Files.createParentDirs(basePathFile); basePathFile.mkdir(); + + FileFilter dataFileFilter = new FileFilters.RegexFileFilter("dim_-?\\d+\\.txt"); + + //TODO Deal with temp files correctly + File[] dataFiles = basePathFile.listFiles(dataFileFilter); + for (File dataFile : dataFiles) + { + dataFile.delete(); + } + + basePathFile = null; basePath += "dim_"; @@ -250,7 +261,7 @@ public class DDSaveHandler { if(data.schematicName().equals(packedDungeon.SchematicName)) { - return data; + //return data; } } return null; diff --git a/StevenDimDoors/mod_pocketDim/saving/OldSaveImporter.java b/StevenDimDoors/mod_pocketDim/saving/OldSaveImporter.java new file mode 100644 index 00000000..cefaf36d --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/saving/OldSaveImporter.java @@ -0,0 +1,73 @@ +package StevenDimDoors.mod_pocketDim.saving; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import StevenDimDoors.mod_pocketDim.DimData; +import StevenDimDoors.mod_pocketDim.LinkData; +import StevenDimDoors.mod_pocketDim.Point3D; + +import StevenDimDoors.mod_pocketDim.ObjectSaveInputStream; +import StevenDimDoors.mod_pocketDim.util.Point4D; + +public class OldSaveImporter +{ + public static void importOldSave(File file) throws IOException, ClassNotFoundException + { + FileInputStream saveFile = new FileInputStream(file); + ObjectSaveInputStream save = new ObjectSaveInputStream(saveFile); + HashMap comboSave =((HashMap) save.readObject()); + save.close(); + + List<PackedLinkData> allPackedLinks = new ArrayList<PackedLinkData>(); + List<PackedDimData> newPackedDimData = new ArrayList<PackedDimData>(); + + HashMap<Integer, DimData> dimMap; + + try + { + dimMap = (HashMap<Integer, DimData>) comboSave.get("dimList"); + } + catch(Exception e) + { + System.out.println("Could not import old save data"); + return; + } + + for(DimData data : dimMap.values()) + { + List<PackedLinkData> newPackedLinkData = new ArrayList<PackedLinkData>(); + List<Integer> childDims = new ArrayList<Integer>(); + + for(LinkData link : data.getLinksInDim()) + { + Point4D source = new Point4D(link.locXCoord,link.locYCoord,link.locZCoord,link.locDimID); + Point4D destintion = new Point4D(link.destXCoord,link.destYCoord,link.destZCoord,link.destDimID); + PackedLinkTail tail = new PackedLinkTail(destintion, link.linkOrientation); + List<Point3D> children = new ArrayList<Point3D>(); + + PackedLinkData newPackedLink = new PackedLinkData(source, new Point3D(-1,-1,-1), tail, link.linkOrientation,children); + + newPackedLinkData.add(newPackedLink); + allPackedLinks.add(newPackedLink); + + } + + PackedDimData dim = new PackedDimData(data.dimID, data.depth, data.depth, data.exitDimLink.locDimID, data.exitDimLink.locDimID, 0, data.dungeonGenerator!=null, data.hasBeenFilled, null, new Point3D(0,64,0), childDims, newPackedLinkData, null); + newPackedDimData.add(dim); + + DDSaveHandler.unpackDimData(newPackedDimData); + DDSaveHandler.unpackLinkData(allPackedLinks); + + + } + + + } + +}