2013-06-10 23:03:52 +02:00
|
|
|
package StevenDimDoors.mod_pocketDim.helpers;
|
|
|
|
|
2013-08-01 19:00:27 +02:00
|
|
|
import java.io.BufferedReader;
|
2013-06-10 23:03:52 +02:00
|
|
|
import java.io.File;
|
2013-08-01 19:00:27 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2013-06-10 23:03:52 +02:00
|
|
|
import java.util.ArrayList;
|
2013-08-05 15:48:49 +02:00
|
|
|
import java.util.Arrays;
|
2013-06-17 12:18:15 +02:00
|
|
|
import java.util.Collection;
|
2013-06-17 12:31:53 +02:00
|
|
|
import java.util.Collections;
|
2013-06-15 17:21:49 +02:00
|
|
|
import java.util.HashSet;
|
2013-08-06 02:16:45 +02:00
|
|
|
import java.util.LinkedList;
|
2013-08-01 06:05:08 +02:00
|
|
|
import java.util.List;
|
2013-08-06 02:16:45 +02:00
|
|
|
import java.util.Queue;
|
2013-06-10 23:03:52 +02:00
|
|
|
import java.util.Random;
|
2013-06-15 16:25:50 +02:00
|
|
|
import java.util.regex.Pattern;
|
2013-06-10 23:03:52 +02:00
|
|
|
|
|
|
|
import net.minecraft.world.World;
|
2013-06-14 01:01:54 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.DDProperties;
|
2013-07-13 05:40:21 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.DimData;
|
2013-06-11 20:42:11 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.DungeonGenerator;
|
2013-06-10 23:03:52 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.LinkData;
|
|
|
|
import StevenDimDoors.mod_pocketDim.mod_pocketDim;
|
2013-07-29 15:58:47 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.DungeonSchematic;
|
2013-08-05 15:48:49 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonChainRuleDefinition;
|
2013-08-05 01:27:34 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack;
|
2013-08-05 15:48:49 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPackConfig;
|
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType;
|
2013-06-18 16:23:31 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.items.itemDimDoor;
|
2013-08-05 15:48:49 +02:00
|
|
|
import StevenDimDoors.mod_pocketDim.util.WeightedContainer;
|
2013-06-10 23:03:52 +02:00
|
|
|
|
|
|
|
public class DungeonHelper
|
|
|
|
{
|
2013-06-16 07:00:05 +02:00
|
|
|
private static DungeonHelper instance = null;
|
2013-06-14 01:01:54 +02:00
|
|
|
private static DDProperties properties = null;
|
2013-08-05 15:48:49 +02:00
|
|
|
|
|
|
|
public static final Pattern SCHEMATIC_NAME_PATTERN = Pattern.compile("[A-Za-z0-9_\\-]+");
|
|
|
|
public static final Pattern DUNGEON_NAME_PATTERN = Pattern.compile("[A-Za-z0-9\\-]+");
|
2013-08-01 19:00:27 +02:00
|
|
|
|
|
|
|
private static final String DEFAULT_UP_SCHEMATIC_PATH = "/schematics/core/simpleStairsUp.schematic";
|
|
|
|
private static final String DEFAULT_DOWN_SCHEMATIC_PATH = "/schematics/core/simpleStairsDown.schematic";
|
|
|
|
private static final String DEFAULT_ERROR_SCHEMATIC_PATH = "/schematics/core/somethingBroke.schematic";
|
|
|
|
private static final String BUNDLED_DUNGEONS_LIST_PATH = "/schematics/schematics.txt";
|
2013-08-02 10:54:24 +02:00
|
|
|
private static final String DUNGEON_CREATION_GUIDE_SOURCE_PATH = "/mods/DimDoors/text/How_to_add_dungeons.txt";
|
2013-06-15 17:21:49 +02:00
|
|
|
|
2013-06-25 19:55:13 +02:00
|
|
|
public static final String SCHEMATIC_FILE_EXTENSION = ".schematic";
|
2013-06-15 16:25:50 +02:00
|
|
|
private static final int DEFAULT_DUNGEON_WEIGHT = 100;
|
2013-06-25 19:55:13 +02:00
|
|
|
public static final int MAX_DUNGEON_WEIGHT = 10000; //Used to prevent overflows and math breaking down
|
2013-07-15 06:47:32 +02:00
|
|
|
private static final int MAX_EXPORT_RADIUS = 50;
|
2013-07-31 14:05:58 +02:00
|
|
|
public static final short MAX_DUNGEON_WIDTH = 2 * MAX_EXPORT_RADIUS + 1;
|
2013-08-02 10:54:24 +02:00
|
|
|
public static final short MAX_DUNGEON_HEIGHT = MAX_DUNGEON_WIDTH;
|
|
|
|
public static final short MAX_DUNGEON_LENGTH = MAX_DUNGEON_WIDTH;
|
2013-07-15 06:54:53 +02:00
|
|
|
|
2013-08-01 06:05:08 +02:00
|
|
|
private ArrayList<DungeonGenerator> untaggedDungeons = new ArrayList<DungeonGenerator>();
|
|
|
|
private ArrayList<DungeonGenerator> registeredDungeons = new ArrayList<DungeonGenerator>();
|
2013-08-01 19:00:27 +02:00
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
public DungeonPack RuinsPack;
|
|
|
|
|
2013-08-01 19:00:27 +02:00
|
|
|
private DungeonGenerator defaultUp;
|
|
|
|
private DungeonGenerator defaultDown;
|
|
|
|
private DungeonGenerator defaultError;
|
2013-06-16 08:59:02 +02:00
|
|
|
|
2013-06-16 07:00:05 +02:00
|
|
|
private DungeonHelper()
|
2013-06-15 14:46:28 +02:00
|
|
|
{
|
2013-06-15 17:21:49 +02:00
|
|
|
//Load our reference to the DDProperties singleton
|
2013-06-15 14:46:28 +02:00
|
|
|
if (properties == null)
|
|
|
|
properties = DDProperties.instance();
|
2013-06-16 07:13:06 +02:00
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
registerDungeons();
|
2013-06-16 07:13:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-16 18:06:21 +02:00
|
|
|
public static DungeonHelper initialize()
|
2013-06-16 07:00:05 +02:00
|
|
|
{
|
|
|
|
if (instance == null)
|
2013-06-16 07:13:06 +02:00
|
|
|
{
|
2013-06-16 07:00:05 +02:00
|
|
|
instance = new DungeonHelper();
|
2013-06-16 07:13:06 +02:00
|
|
|
}
|
2013-06-16 07:00:05 +02:00
|
|
|
else
|
2013-06-16 07:13:06 +02:00
|
|
|
{
|
2013-06-16 18:06:21 +02:00
|
|
|
throw new IllegalStateException("Cannot initialize DungeonHelper twice");
|
2013-06-16 07:13:06 +02:00
|
|
|
}
|
2013-06-16 07:00:05 +02:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DungeonHelper instance()
|
|
|
|
{
|
|
|
|
if (instance == null)
|
|
|
|
{
|
|
|
|
//This is to prevent some frustrating bugs that could arise when classes
|
|
|
|
//are loaded in the wrong order. Trust me, I had to squash a few...
|
2013-06-16 18:06:21 +02:00
|
|
|
throw new IllegalStateException("Instance of DungeonHelper requested before initialization");
|
2013-06-16 07:00:05 +02:00
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
private void registerDungeons()
|
2013-08-01 06:05:08 +02:00
|
|
|
{
|
|
|
|
File file = new File(properties.CustomSchematicDirectory);
|
|
|
|
if (file.exists() || file.mkdir())
|
|
|
|
{
|
2013-08-02 10:54:24 +02:00
|
|
|
copyfile.copyFile(DUNGEON_CREATION_GUIDE_SOURCE_PATH, file.getAbsolutePath() + "/How_to_add_dungeons.txt");
|
2013-08-01 06:05:08 +02:00
|
|
|
}
|
2013-08-05 15:48:49 +02:00
|
|
|
|
|
|
|
RuinsPack = new DungeonPack(createRuinsConfig());
|
|
|
|
|
2013-08-01 19:00:27 +02:00
|
|
|
registerBundledDungeons();
|
2013-08-05 15:48:49 +02:00
|
|
|
registerCustomDungeons(properties.CustomSchematicDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static DungeonPackConfig createRuinsConfig()
|
|
|
|
{
|
|
|
|
//This is a temporarily function for testing dungeon packs.
|
|
|
|
//It'll be removed later when we read dungeon configurations from files.
|
|
|
|
|
|
|
|
ArrayList<DungeonChainRuleDefinition> rules = new ArrayList<DungeonChainRuleDefinition>();
|
2013-08-15 13:29:37 +02:00
|
|
|
|
|
|
|
rules.add(parseDefinitionUnsafe("? ? ? ? ? ? ? ? -> Trap#20 SimpleHall#40 ComplexHall#10 Exit#20 DeadEnd#10"));
|
|
|
|
|
|
|
|
rules.add(parseDefinitionUnsafe("? ? ? ? -> Trap#18 SimpleHall#40 ComplexHall#10 Exit#18 DeadEnd#10 Hub#4"));
|
|
|
|
|
|
|
|
rules.add(parseDefinitionUnsafe("? ? ? -> ComplexHall Hub Trap SimpleHall Maze"));
|
|
|
|
|
|
|
|
rules.add(parseDefinitionUnsafe("? ? -> ComplexHall Hub Trap SimpleHall Maze"));
|
|
|
|
|
|
|
|
rules.add(parseDefinitionUnsafe("? -> ComplexHall#40 Hub#30 Trap#10 SimpleHall#10 Maze#10"));
|
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
rules.add(parseDefinitionUnsafe("-> ComplexHall#40 Hub#30 Trap#10 SimpleHall#10 Maze#10"));
|
|
|
|
|
|
|
|
String[] typeNames = "Hub Trap Maze Exit DeadEnd SimpleHall ComplexHall".toUpperCase().split(" ");
|
|
|
|
|
|
|
|
DungeonPackConfig config = new DungeonPackConfig();
|
|
|
|
config.setName("ruins");
|
|
|
|
config.setAllowDuplicatesInChain(false);
|
|
|
|
config.setRules(rules);
|
|
|
|
config.setTypeNames(new ArrayList<String>(Arrays.asList(typeNames)));
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static DungeonChainRuleDefinition parseDefinitionUnsafe(String definition)
|
|
|
|
{
|
|
|
|
//This is an improvised parsing function for rule definitions. Only for testing!!!
|
|
|
|
definition = definition.toUpperCase();
|
|
|
|
String[] parts = definition.split("->");
|
|
|
|
ArrayList<String> condition = new ArrayList<String>();
|
|
|
|
ArrayList<WeightedContainer<String>> products = new ArrayList<WeightedContainer<String>>();
|
|
|
|
|
|
|
|
for (String conditionPart : parts[0].split(" "))
|
|
|
|
{
|
|
|
|
if (!conditionPart.isEmpty())
|
|
|
|
condition.add(conditionPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String product : parts[1].split(" "))
|
|
|
|
{
|
|
|
|
if (!product.isEmpty())
|
|
|
|
{
|
|
|
|
String[] productParts = product.split("#");
|
|
|
|
String productType = productParts[0];
|
|
|
|
int weight = (productParts.length > 1) ? Integer.parseInt(productParts[1]) : 100;
|
|
|
|
products.add(new WeightedContainer<String>(productType, weight));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new DungeonChainRuleDefinition(condition, products);
|
2013-08-01 06:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<DungeonGenerator> getRegisteredDungeons()
|
|
|
|
{
|
|
|
|
return Collections.unmodifiableList(this.registeredDungeons);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<DungeonGenerator> getUntaggedDungeons()
|
|
|
|
{
|
|
|
|
return Collections.unmodifiableList(this.untaggedDungeons);
|
|
|
|
}
|
|
|
|
|
2013-08-01 19:00:27 +02:00
|
|
|
public DungeonGenerator getDefaultErrorDungeon()
|
|
|
|
{
|
|
|
|
return defaultError;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DungeonGenerator getDefaultUpDungeon()
|
|
|
|
{
|
|
|
|
return defaultUp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DungeonGenerator getDefaultDownDungeon()
|
|
|
|
{
|
|
|
|
return defaultDown;
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:23:31 +02:00
|
|
|
public LinkData createCustomDungeonDoor(World world, int x, int y, int z)
|
|
|
|
{
|
|
|
|
//Create a link above the specified position. Link to a new pocket dimension.
|
|
|
|
LinkData link = new LinkData(world.provider.dimensionId, 0, x, y + 1, z, x, y + 1, z, true, 3);
|
|
|
|
link = dimHelper.instance.createPocket(link, true, false);
|
|
|
|
|
|
|
|
//Place a Warp Door linked to that pocket
|
|
|
|
itemDimDoor.placeDoorBlock(world, x, y, z, 3, mod_pocketDim.ExitDoor);
|
|
|
|
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
2013-06-25 19:55:13 +02:00
|
|
|
public boolean validateDungeonType(String type)
|
|
|
|
{
|
|
|
|
//Check if the dungeon type is valid
|
2013-08-05 15:48:49 +02:00
|
|
|
return RuinsPack.isKnownType(type);
|
2013-06-25 19:55:13 +02:00
|
|
|
}
|
|
|
|
|
2013-06-15 16:25:50 +02:00
|
|
|
public boolean validateSchematicName(String name)
|
|
|
|
{
|
2013-06-22 20:27:59 +02:00
|
|
|
String[] dungeonData;
|
|
|
|
|
|
|
|
if (!name.endsWith(SCHEMATIC_FILE_EXTENSION))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dungeonData = name.substring(0, name.length() - SCHEMATIC_FILE_EXTENSION.length()).split("_");
|
2013-06-15 16:25:50 +02:00
|
|
|
|
|
|
|
//Check for a valid number of parts
|
|
|
|
if (dungeonData.length < 3 || dungeonData.length > 4)
|
|
|
|
return false;
|
|
|
|
|
2013-06-15 17:21:49 +02:00
|
|
|
//Check if the dungeon type is valid
|
2013-08-05 15:48:49 +02:00
|
|
|
if (!validateDungeonType(dungeonData[0]))
|
2013-06-15 16:25:50 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
//Check if the name is valid
|
2013-08-05 15:48:49 +02:00
|
|
|
if (!SCHEMATIC_NAME_PATTERN.matcher(dungeonData[1]).matches())
|
2013-06-15 16:25:50 +02:00
|
|
|
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]);
|
2013-06-15 17:30:05 +02:00
|
|
|
if (weight < 0 || weight > MAX_DUNGEON_WEIGHT)
|
2013-06-15 16:25:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (NumberFormatException e)
|
|
|
|
{
|
|
|
|
//Not a number
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-01 19:00:27 +02:00
|
|
|
public void registerDungeon(String schematicPath, boolean isInternal, boolean verbose)
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-08-01 19:00:27 +02:00
|
|
|
//We use schematicPath as the real path for internal files (inside our JAR) because it seems
|
|
|
|
//that File tries to interpret it as a local drive path and mangles it.
|
|
|
|
File schematicFile = new File(schematicPath);
|
2013-06-15 16:25:50 +02:00
|
|
|
String name = schematicFile.getName();
|
2013-08-01 19:00:27 +02:00
|
|
|
String path = isInternal ? schematicPath : schematicFile.getAbsolutePath();
|
2013-06-10 23:03:52 +02:00
|
|
|
try
|
|
|
|
{
|
2013-06-22 20:27:59 +02:00
|
|
|
if (validateSchematicName(name))
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-06-15 16:25:50 +02:00
|
|
|
//Strip off the file extension while splitting the file name
|
|
|
|
String[] dungeonData = name.substring(0, name.length() - SCHEMATIC_FILE_EXTENSION.length()).split("_");
|
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
DungeonType dungeonType = RuinsPack.getType(dungeonData[0]);
|
2013-06-22 20:27:59 +02:00
|
|
|
boolean isOpen = dungeonData[2].equalsIgnoreCase("open");
|
2013-06-15 16:25:50 +02:00
|
|
|
int weight = (dungeonData.length == 4) ? Integer.parseInt(dungeonData[3]) : DEFAULT_DUNGEON_WEIGHT;
|
2013-06-15 17:21:49 +02:00
|
|
|
|
|
|
|
//Add this custom dungeon to the list corresponding to its type
|
2013-08-05 15:48:49 +02:00
|
|
|
DungeonGenerator generator = new DungeonGenerator(weight, path, isOpen, dungeonType);
|
2013-06-15 16:25:50 +02:00
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
RuinsPack.addDungeon(generator);
|
2013-06-15 17:21:49 +02:00
|
|
|
registeredDungeons.add(generator);
|
2013-08-01 19:00:27 +02:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
System.out.println("Registered dungeon: " + name);
|
|
|
|
}
|
2013-06-15 16:25:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-01 19:00:27 +02:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
System.out.println("Could not parse dungeon filename, not adding dungeon to generation lists");
|
|
|
|
}
|
2013-08-05 15:48:49 +02:00
|
|
|
untaggedDungeons.add(new DungeonGenerator(DEFAULT_DUNGEON_WEIGHT, path, true, DungeonType.UNKNOWN_TYPE));
|
2013-08-01 19:00:27 +02:00
|
|
|
System.out.println("Registered untagged dungeon: " + name);
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 14:07:33 +02:00
|
|
|
catch(Exception e)
|
|
|
|
{
|
2013-08-01 19:00:27 +02:00
|
|
|
System.err.println("Failed to register dungeon: " + name);
|
2013-06-15 14:07:33 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
2013-06-15 14:07:33 +02:00
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
private void registerCustomDungeons(String path)
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-06-17 12:31:53 +02:00
|
|
|
File directory = new File(path);
|
|
|
|
File[] schematicNames = directory.listFiles();
|
2013-06-15 14:07:33 +02:00
|
|
|
|
2013-06-17 12:31:53 +02:00
|
|
|
if (schematicNames != null)
|
2013-06-15 14:07:33 +02:00
|
|
|
{
|
2013-06-17 12:31:53 +02:00
|
|
|
for (File schematicFile: schematicNames)
|
2013-06-15 14:07:33 +02:00
|
|
|
{
|
2013-06-17 12:31:53 +02:00
|
|
|
if (schematicFile.getName().endsWith(SCHEMATIC_FILE_EXTENSION))
|
|
|
|
{
|
2013-08-01 19:00:27 +02:00
|
|
|
registerDungeon(schematicFile.getPath(), false, true);
|
2013-06-17 12:31:53 +02:00
|
|
|
}
|
2013-06-15 14:07:33 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
|
|
|
|
2013-08-01 19:00:27 +02:00
|
|
|
private void registerBundledDungeons()
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-08-01 19:00:27 +02:00
|
|
|
//Register the core schematics
|
|
|
|
//These are used for debugging and in case of unusual errors while loading dungeons
|
2013-08-05 15:48:49 +02:00
|
|
|
defaultUp = new DungeonGenerator(DEFAULT_DUNGEON_WEIGHT, DEFAULT_UP_SCHEMATIC_PATH, true, DungeonType.UNKNOWN_TYPE);
|
|
|
|
defaultDown = new DungeonGenerator(DEFAULT_DUNGEON_WEIGHT, DEFAULT_DOWN_SCHEMATIC_PATH, true, DungeonType.UNKNOWN_TYPE);
|
|
|
|
defaultError = new DungeonGenerator(DEFAULT_DUNGEON_WEIGHT, DEFAULT_ERROR_SCHEMATIC_PATH, true, DungeonType.UNKNOWN_TYPE);
|
2013-08-01 19:00:27 +02:00
|
|
|
|
|
|
|
//Open the list of dungeons packaged with our mod and register their schematics
|
|
|
|
InputStream listStream = this.getClass().getResourceAsStream(BUNDLED_DUNGEONS_LIST_PATH);
|
|
|
|
if (listStream == null)
|
|
|
|
{
|
|
|
|
System.err.println("Unable to open list of bundled dungeon schematics.");
|
|
|
|
return;
|
|
|
|
}
|
2013-06-18 04:26:39 +02:00
|
|
|
|
2013-08-01 19:00:27 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
BufferedReader listReader = new BufferedReader(new InputStreamReader(listStream));
|
|
|
|
String schematicPath = listReader.readLine();
|
|
|
|
while (schematicPath != null)
|
|
|
|
{
|
|
|
|
schematicPath = schematicPath.trim();
|
|
|
|
if (!schematicPath.isEmpty())
|
|
|
|
{
|
|
|
|
registerDungeon(schematicPath, true, false);
|
|
|
|
}
|
|
|
|
schematicPath = listReader.readLine();
|
|
|
|
}
|
|
|
|
listReader.close();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
System.err.println("An exception occurred while reading the list of bundled dungeon schematics.");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
2013-06-15 14:07:33 +02:00
|
|
|
|
2013-07-15 07:58:01 +02:00
|
|
|
public boolean exportDungeon(World world, int centerX, int centerY, int centerZ, String exportPath)
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-07-15 10:15:58 +02:00
|
|
|
//Write schematic data to a file
|
2013-06-15 14:07:33 +02:00
|
|
|
try
|
|
|
|
{
|
2013-07-29 15:58:47 +02:00
|
|
|
DungeonSchematic dungeon = DungeonSchematic.copyFromWorld(world,
|
2013-07-31 14:05:58 +02:00
|
|
|
centerX - MAX_EXPORT_RADIUS, centerY - MAX_EXPORT_RADIUS, centerZ - MAX_EXPORT_RADIUS,
|
|
|
|
MAX_DUNGEON_WIDTH, MAX_DUNGEON_HEIGHT, MAX_DUNGEON_LENGTH, true);
|
2013-07-30 19:58:14 +02:00
|
|
|
dungeon.applyExportFilters(properties);
|
2013-07-29 15:58:47 +02:00
|
|
|
dungeon.writeToFile(exportPath);
|
2013-06-16 11:59:53 +02:00
|
|
|
return true;
|
2013-06-15 14:07:33 +02:00
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
2013-06-16 11:59:53 +02:00
|
|
|
return false;
|
2013-06-15 14:07:33 +02:00
|
|
|
}
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
2013-06-15 14:07:33 +02:00
|
|
|
|
2013-08-05 01:27:34 +02:00
|
|
|
public void generateDungeonLink(LinkData inbound, DungeonPack pack, Random random)
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-08-05 01:27:34 +02:00
|
|
|
DungeonGenerator selection;
|
|
|
|
|
2013-06-10 23:03:52 +02:00
|
|
|
try
|
2013-08-05 01:27:34 +02:00
|
|
|
{
|
|
|
|
selection = pack.getNextDungeon(inbound, random);
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
2013-06-15 14:07:33 +02:00
|
|
|
catch (Exception e)
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-08-05 01:27:34 +02:00
|
|
|
System.err.println("An exception occurred while selecting a dungeon:");
|
2013-06-16 20:40:26 +02:00
|
|
|
e.printStackTrace();
|
2013-08-05 01:27:34 +02:00
|
|
|
|
|
|
|
if (!pack.isEmpty())
|
2013-06-10 23:03:52 +02:00
|
|
|
{
|
2013-08-05 01:27:34 +02:00
|
|
|
selection = pack.getRandomDungeon(random);
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-05 01:27:34 +02:00
|
|
|
selection = defaultError;
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-05 01:27:34 +02:00
|
|
|
dimHelper.instance.getDimData(inbound.destDimID).dungeonGenerator = selection;
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|
2013-06-17 12:18:15 +02:00
|
|
|
|
|
|
|
public Collection<String> getDungeonNames() {
|
2013-06-17 12:31:53 +02:00
|
|
|
|
2013-06-17 12:18:15 +02:00
|
|
|
//Use a HashSet to guarantee that all dungeon names will be distinct.
|
|
|
|
//This shouldn't be necessary if we keep proper lists without repetitions,
|
|
|
|
//but it's a fool-proof workaround.
|
|
|
|
HashSet<String> dungeonNames = new HashSet<String>();
|
|
|
|
dungeonNames.addAll( parseDungeonNames(registeredDungeons) );
|
2013-08-01 05:56:26 +02:00
|
|
|
dungeonNames.addAll( parseDungeonNames(untaggedDungeons) );
|
2013-06-17 12:31:53 +02:00
|
|
|
|
|
|
|
//Sort dungeon names alphabetically
|
|
|
|
ArrayList<String> sortedNames = new ArrayList<String>(dungeonNames);
|
2013-08-01 05:56:26 +02:00
|
|
|
Collections.sort(sortedNames, String.CASE_INSENSITIVE_ORDER);
|
2013-06-17 12:31:53 +02:00
|
|
|
return sortedNames;
|
2013-06-17 12:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static ArrayList<String> parseDungeonNames(ArrayList<DungeonGenerator> dungeons)
|
|
|
|
{
|
|
|
|
String name;
|
|
|
|
File schematic;
|
|
|
|
ArrayList<String> names = new ArrayList<String>(dungeons.size());
|
|
|
|
|
|
|
|
for (DungeonGenerator dungeon : dungeons)
|
|
|
|
{
|
2013-06-17 12:34:35 +02:00
|
|
|
//Retrieve the file name and strip off the file extension
|
2013-06-17 12:18:15 +02:00
|
|
|
schematic = new File(dungeon.schematicPath);
|
|
|
|
name = schematic.getName();
|
|
|
|
name = name.substring(0, name.length() - SCHEMATIC_FILE_EXTENSION.length());
|
|
|
|
names.add(name);
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2013-06-18 04:26:39 +02:00
|
|
|
|
2013-08-05 15:48:49 +02:00
|
|
|
public static ArrayList<DungeonGenerator> getDungeonChainHistory(DimData dimData, DungeonPack pack, int maxSize)
|
2013-06-18 04:26:39 +02:00
|
|
|
{
|
2013-08-05 01:27:34 +02:00
|
|
|
//TODO: I've improved this code for the time being. However, searching across links is a flawed approach. A player could
|
|
|
|
//manipulate the output of this function by setting up links to mislead our algorithm or by removing links.
|
|
|
|
//Dimensions MUST have built-in records of their parent dimensions in the future. ~SenseiKiwi
|
2013-06-18 04:26:39 +02:00
|
|
|
|
2013-08-05 01:27:34 +02:00
|
|
|
ArrayList<DungeonGenerator> history = new ArrayList<DungeonGenerator>();
|
2013-08-06 02:16:45 +02:00
|
|
|
DimData tailDim = dimData;
|
|
|
|
boolean found = true;
|
|
|
|
|
|
|
|
if (dimData.dungeonGenerator == null || dimData.dungeonGenerator.getDungeonType().Owner != pack || maxSize < 1)
|
|
|
|
{
|
|
|
|
//The initial dimension is already outside our pack. Return an empty list.
|
|
|
|
return history;
|
|
|
|
}
|
|
|
|
history.add(dimData.dungeonGenerator);
|
2013-08-05 01:27:34 +02:00
|
|
|
|
2013-08-06 02:16:45 +02:00
|
|
|
for (int count = 1; count < maxSize && found; count++)
|
2013-07-13 05:40:21 +02:00
|
|
|
{
|
2013-08-06 02:16:45 +02:00
|
|
|
found = false;
|
|
|
|
for (LinkData link : tailDim.getLinksInDim())
|
2013-08-05 15:48:49 +02:00
|
|
|
{
|
2013-08-06 02:16:45 +02:00
|
|
|
DimData neighbor = dimHelper.instance.getDimData(link.destDimID);
|
|
|
|
if (neighbor.depth == tailDim.depth - 1 && neighbor.dungeonGenerator != null &&
|
|
|
|
neighbor.dungeonGenerator.getDungeonType().Owner == pack)
|
|
|
|
{
|
|
|
|
tailDim = neighbor;
|
|
|
|
history.add(tailDim.dungeonGenerator);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-08-05 15:48:49 +02:00
|
|
|
}
|
2013-08-06 02:16:45 +02:00
|
|
|
}
|
|
|
|
return history;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ArrayList<DungeonGenerator> getFlatDungeonTree(DimData dimData, int maxSize)
|
|
|
|
{
|
|
|
|
//TODO: I've improved this code for the time being. However, searching across links is a flawed approach. A player could
|
|
|
|
//manipulate the output of this function by setting up links to mislead our algorithm or by removing links.
|
|
|
|
//Dimensions MUST have built-in records of their parent dimensions in the future. ~SenseiKiwi
|
|
|
|
|
|
|
|
dimHelper helper = dimHelper.instance;
|
|
|
|
ArrayList<DungeonGenerator> dungeons = new ArrayList<DungeonGenerator>();
|
|
|
|
DimData root = helper.getDimData(helper.getLinkDataFromCoords(dimData.exitDimLink.destXCoord, dimData.exitDimLink.destYCoord, dimData.exitDimLink.destZCoord, dimData.exitDimLink.destDimID).destDimID);
|
|
|
|
HashSet<DimData> checked = new HashSet<DimData>();
|
|
|
|
Queue<DimData> pendingDimensions = new LinkedList<DimData>();
|
|
|
|
|
|
|
|
if (root.dungeonGenerator == null)
|
|
|
|
{
|
|
|
|
return dungeons;
|
|
|
|
}
|
|
|
|
pendingDimensions.add(root);
|
|
|
|
checked.add(root);
|
|
|
|
|
|
|
|
while (dungeons.size() < maxSize && !pendingDimensions.isEmpty())
|
|
|
|
{
|
|
|
|
DimData current = pendingDimensions.remove();
|
|
|
|
for (LinkData link : current.getLinksInDim())
|
2013-07-13 05:40:21 +02:00
|
|
|
{
|
2013-08-06 02:16:45 +02:00
|
|
|
DimData child = helper.getDimData(link.destDimID);
|
|
|
|
if (child.depth == current.depth + 1 && child.dungeonGenerator != null && checked.add(child))
|
2013-07-13 05:40:21 +02:00
|
|
|
{
|
2013-08-06 02:16:45 +02:00
|
|
|
dungeons.add(child.dungeonGenerator);
|
|
|
|
pendingDimensions.add(child);
|
|
|
|
}
|
|
|
|
if (dungeons.size() == maxSize)
|
|
|
|
{
|
|
|
|
break;
|
2013-07-13 05:40:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-06 02:16:45 +02:00
|
|
|
return dungeons;
|
2013-07-13 05:40:21 +02:00
|
|
|
}
|
2013-06-10 23:03:52 +02:00
|
|
|
}
|