Limited Sizes of Generated Dungeons

Added validation to SchematicLoader that prevents dungeons beyond
certain maximum dimensions from being loaded. Our current limits are the
same as the largest dungeon we could export. Rewrote
generateDungeonPocket() to handle that possible failure better.
This commit is contained in:
SenseiKiwi
2013-07-31 08:05:58 -04:00
parent f7ad55eea0
commit 356bfe4bd0
2 changed files with 60 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
package StevenDimDoors.mod_pocketDim;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import net.minecraft.world.World;
import StevenDimDoors.mod_pocketDim.dungeon.DungeonSchematic;
@@ -12,38 +13,49 @@ public class SchematicLoader
{
private SchematicLoader() { }
public static void generateDungeonPocket(LinkData link, DDProperties properties)
public static boolean generateDungeonPocket(LinkData link, DDProperties properties)
{
//TODO: Phase this function out in the next update. ~SenseiKiwi
String filePath=DungeonHelper.instance().defaultBreak.schematicPath;
if(dimHelper.dimList.containsKey(link.destDimID))
if (link == null || properties == null)
{
if(dimHelper.dimList.get(link.destDimID).dungeonGenerator == null)
return false;
}
try
{
String schematicPath;
int originDimID = link.locDimID;
int destDimID = link.destDimID;
HashMap<Integer, DimData> dimList = dimHelper.dimList;
if (dimList.containsKey(destDimID))
{
if (dimList.get(destDimID).dungeonGenerator == null)
{
DungeonHelper.instance().generateDungeonLink(link);
}
filePath = dimHelper.dimList.get(link.destDimID).dungeonGenerator.schematicPath;
}
//this.generateSchematic(link.destXCoord, link.destYCoord, link.destZCoord, link.linkOrientation, link.destDimID, link.locDimID, filePath);
try
{
int originDimID = link.locDimID;
int destDimID = link.destDimID;
DungeonSchematic dungeon;
if ((new File(filePath)).exists())
{
dungeon = DungeonSchematic.readFromFile(filePath);
schematicPath = dimList.get(destDimID).dungeonGenerator.schematicPath;
}
else
{
dungeon = DungeonSchematic.readFromResource(filePath);
return false;
}
dungeon.applyImportFilters(properties);
dimHelper.dimList.get(destDimID).hasBeenFilled = true;
DungeonSchematic dungeon = checkSourceAndLoad(schematicPath);
//Validate the dungeon's dimensions
if (!hasValidDimensions(dungeon))
{
//TODO: In the future, remove this dungeon from the generation lists altogether.
//That will have to wait until our code is updated to support that more easily.
System.err.println("The following schematic file has dimensions that exceed the maximum permitted dimensions for dungeons: " + schematicPath);
System.err.println("The dungeon will not be loaded.");
dungeon = checkSourceAndLoad(DungeonHelper.instance().defaultBreak.schematicPath);
}
dungeon.applyImportFilters(properties);
dimList.get(destDimID).hasBeenFilled = true;
if (dimHelper.getWorld(destDimID) == null)
{
dimHelper.initDimension(destDimID);
@@ -51,14 +63,34 @@ public class SchematicLoader
World world = dimHelper.getWorld(destDimID);
dungeon.copyToWorld(world, new Point3D(link.destXCoord, link.destYCoord, link.destZCoord), link.linkOrientation, originDimID, destDimID);
return true;
}
catch (FileNotFoundException e)
catch (Exception e)
{
e.printStackTrace();
return false;
}
catch (InvalidSchematicException e)
}
private static boolean hasValidDimensions(DungeonSchematic dungeon)
{
e.printStackTrace();
}
return (dungeon.getWidth() <= DungeonHelper.MAX_DUNGEON_WIDTH &&
dungeon.getHeight() <= DungeonHelper.MAX_DUNGEON_HEIGHT &&
dungeon.getLength() <= DungeonHelper.MAX_DUNGEON_LENGTH);
}
private static DungeonSchematic checkSourceAndLoad(String schematicPath) throws FileNotFoundException, InvalidSchematicException
{
//TODO: Change this code once we introduce an isInternal flag in dungeon data
DungeonSchematic dungeon;
if ((new File(schematicPath)).exists())
{
dungeon = DungeonSchematic.readFromFile(schematicPath);
}
else
{
dungeon = DungeonSchematic.readFromResource(schematicPath);
}
return dungeon;
}
}

View File

@@ -31,6 +31,9 @@ public class DungeonHelper
private static final int DEFAULT_DUNGEON_WEIGHT = 100;
public static final int MAX_DUNGEON_WEIGHT = 10000; //Used to prevent overflows and math breaking down
private static final int MAX_EXPORT_RADIUS = 50;
public static final short MAX_DUNGEON_WIDTH = 2 * MAX_EXPORT_RADIUS + 1;
public static final short MAX_DUNGEON_HEIGHT = 2 * MAX_EXPORT_RADIUS + 1;
public static final short MAX_DUNGEON_LENGTH = 2 * MAX_EXPORT_RADIUS + 1;
public static final int FABRIC_OF_REALITY_EXPORT_ID = 1973;
public static final int PERMAFABRIC_EXPORT_ID = 220;
@@ -340,9 +343,9 @@ public class DungeonHelper
//Write schematic data to a file
try
{
short size = (short) 2 * MAX_EXPORT_RADIUS + 1;
DungeonSchematic dungeon = DungeonSchematic.copyFromWorld(world,
centerX - MAX_EXPORT_RADIUS, centerY - MAX_EXPORT_RADIUS, centerZ - MAX_EXPORT_RADIUS, size, size, size, true);
centerX - MAX_EXPORT_RADIUS, centerY - MAX_EXPORT_RADIUS, centerZ - MAX_EXPORT_RADIUS,
MAX_DUNGEON_WIDTH, MAX_DUNGEON_HEIGHT, MAX_DUNGEON_LENGTH, true);
dungeon.applyExportFilters(properties);
dungeon.writeToFile(exportPath);
return true;