2013-06-10 17:03:52 -04:00
|
|
|
package StevenDimDoors.mod_pocketDim.helpers;
|
|
|
|
|
2013-08-01 13:00:27 -04:00
|
|
|
import java.io.BufferedReader;
|
2013-06-10 17:03:52 -04:00
|
|
|
import java.io.File;
|
2013-08-22 02:25:26 -04:00
|
|
|
import java.io.FileFilter;
|
2013-08-21 14:26:10 -04:00
|
|
|
import java.io.FileNotFoundException;
|
2013-08-01 13:00:27 -04:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2013-06-10 17:03:52 -04:00
|
|
|
import java.util.ArrayList;
|
2013-06-17 06:18:15 -04:00
|
|
|
import java.util.Collection;
|
2013-06-17 06:31:53 -04:00
|
|
|
import java.util.Collections;
|
2013-08-21 14:26:10 -04:00
|
|
|
import java.util.HashMap;
|
2013-06-15 11:21:49 -04:00
|
|
|
import java.util.HashSet;
|
2013-08-05 20:16:45 -04:00
|
|
|
import java.util.LinkedList;
|
2013-08-01 00:05:08 -04:00
|
|
|
import java.util.List;
|
2013-08-05 20:16:45 -04:00
|
|
|
import java.util.Queue;
|
2013-06-10 17:03:52 -04:00
|
|
|
import java.util.Random;
|
2013-06-15 10:25:50 -04:00
|
|
|
import java.util.regex.Pattern;
|
2013-06-10 17:03:52 -04:00
|
|
|
|
2013-08-21 14:26:10 -04:00
|
|
|
import net.minecraft.util.WeightedRandom;
|
2013-06-10 17:03:52 -04:00
|
|
|
import net.minecraft.world.World;
|
2013-06-13 19:01:54 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.DDProperties;
|
2013-06-10 17:03:52 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.mod_pocketDim;
|
2013-09-02 11:47:12 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.core.DimLink;
|
|
|
|
import StevenDimDoors.mod_pocketDim.core.LinkTypes;
|
2013-08-29 02:14:24 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.core.NewDimData;
|
|
|
|
import StevenDimDoors.mod_pocketDim.core.PocketManager;
|
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.DungeonData;
|
2013-07-29 09:58:47 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.DungeonSchematic;
|
2013-08-04 19:27:34 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack;
|
2013-08-05 09:48:49 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPackConfig;
|
2013-08-20 18:54:30 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPackConfigReader;
|
2013-08-05 09:48:49 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonType;
|
2013-08-31 16:00:18 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.items.ItemDimensionalDoor;
|
2013-08-20 18:54:30 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.util.ConfigurationProcessingException;
|
2013-08-21 14:26:10 -04:00
|
|
|
import StevenDimDoors.mod_pocketDim.util.WeightedContainer;
|
2013-06-10 17:03:52 -04:00
|
|
|
|
|
|
|
public class DungeonHelper
|
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
//TODO: File-handling functionality should be spun off to a helper class later
|
|
|
|
private static class DirectoryFilter implements FileFilter
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file)
|
|
|
|
{
|
|
|
|
return file.isDirectory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SchematicFileFilter implements FileFilter
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file)
|
|
|
|
{
|
|
|
|
return file.isFile() && file.getName().endsWith(SCHEMATIC_FILE_EXTENSION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-16 01:00:05 -04:00
|
|
|
private static DungeonHelper instance = null;
|
2013-06-13 19:01:54 -04:00
|
|
|
private static DDProperties properties = null;
|
2013-08-05 09:48:49 -04: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 13:00:27 -04:00
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
public static final String SCHEMATIC_FILE_EXTENSION = ".schematic";
|
|
|
|
|
2013-08-01 13:00:27 -04:00
|
|
|
private static final String DEFAULT_ERROR_SCHEMATIC_PATH = "/schematics/core/somethingBroke.schematic";
|
2013-08-02 04:54:24 -04:00
|
|
|
private static final String DUNGEON_CREATION_GUIDE_SOURCE_PATH = "/mods/DimDoors/text/How_to_add_dungeons.txt";
|
2013-08-22 02:25:26 -04:00
|
|
|
private static final String RUINS_PACK_PATH = "/schematics/ruins";
|
|
|
|
private static final String BUNDLED_RUINS_LIST_PATH = "/schematics/ruins.txt";
|
|
|
|
private static final String STANDARD_CONFIG_FILE_NAME = "rules.txt";
|
2013-08-21 14:26:10 -04:00
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
private static final int NETHER_DIMENSION_ID = -1;
|
2013-08-21 14:26:10 -04:00
|
|
|
|
|
|
|
private static final int MIN_PACK_SWITCH_CHANCE = 0;
|
|
|
|
private static final int PACK_SWITCH_CHANCE_PER_LEVEL = 1;
|
|
|
|
private static final int MAX_PACK_SWITCH_CHANCE = 500;
|
|
|
|
private static final int START_PACK_SWITCH_CHANCE = MAX_PACK_SWITCH_CHANCE / 9;
|
|
|
|
|
2013-06-15 10:25:50 -04:00
|
|
|
private static final int DEFAULT_DUNGEON_WEIGHT = 100;
|
2013-08-21 14:26:10 -04:00
|
|
|
public static final int MIN_DUNGEON_WEIGHT = 1; //Prevents MC's random selection algorithm from throwing an exception
|
2013-06-25 13:55:13 -04:00
|
|
|
public static final int MAX_DUNGEON_WEIGHT = 10000; //Used to prevent overflows and math breaking down
|
2013-08-22 02:25:26 -04:00
|
|
|
|
2013-07-15 00:47:32 -04:00
|
|
|
private static final int MAX_EXPORT_RADIUS = 50;
|
2013-07-31 08:05:58 -04:00
|
|
|
public static final short MAX_DUNGEON_WIDTH = 2 * MAX_EXPORT_RADIUS + 1;
|
2013-08-02 04:54:24 -04:00
|
|
|
public static final short MAX_DUNGEON_HEIGHT = MAX_DUNGEON_WIDTH;
|
|
|
|
public static final short MAX_DUNGEON_LENGTH = MAX_DUNGEON_WIDTH;
|
2013-07-15 00:54:53 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
private ArrayList<DungeonData> untaggedDungeons = new ArrayList<DungeonData>();
|
|
|
|
private ArrayList<DungeonData> registeredDungeons = new ArrayList<DungeonData>();
|
2013-08-01 13:00:27 -04:00
|
|
|
|
2013-08-21 14:49:41 -04:00
|
|
|
private DungeonPack RuinsPack;
|
2013-08-21 14:26:10 -04:00
|
|
|
private HashMap<String, DungeonPack> dungeonPackMapping = new HashMap<String, DungeonPack>();
|
|
|
|
private ArrayList<DungeonPack> dungeonPackList = new ArrayList<DungeonPack>();
|
2013-08-05 09:48:49 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
private DungeonData defaultError;
|
2013-06-16 02:59:02 -04:00
|
|
|
|
2013-06-16 01:00:05 -04:00
|
|
|
private DungeonHelper()
|
2013-06-15 08:46:28 -04:00
|
|
|
{
|
2013-06-15 11:21:49 -04:00
|
|
|
//Load our reference to the DDProperties singleton
|
2013-06-15 08:46:28 -04:00
|
|
|
if (properties == null)
|
|
|
|
properties = DDProperties.instance();
|
2013-06-16 01:13:06 -04:00
|
|
|
|
2013-08-05 09:48:49 -04:00
|
|
|
registerDungeons();
|
2013-06-16 01:13:06 -04:00
|
|
|
}
|
|
|
|
|
2013-06-16 12:06:21 -04:00
|
|
|
public static DungeonHelper initialize()
|
2013-06-16 01:00:05 -04:00
|
|
|
{
|
|
|
|
if (instance == null)
|
2013-06-16 01:13:06 -04:00
|
|
|
{
|
2013-06-16 01:00:05 -04:00
|
|
|
instance = new DungeonHelper();
|
2013-06-16 01:13:06 -04:00
|
|
|
}
|
2013-06-16 01:00:05 -04:00
|
|
|
else
|
2013-06-16 01:13:06 -04:00
|
|
|
{
|
2013-06-16 12:06:21 -04:00
|
|
|
throw new IllegalStateException("Cannot initialize DungeonHelper twice");
|
2013-06-16 01:13:06 -04:00
|
|
|
}
|
2013-06-16 01:00:05 -04: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 12:06:21 -04:00
|
|
|
throw new IllegalStateException("Instance of DungeonHelper requested before initialization");
|
2013-06-16 01:00:05 -04:00
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:48:49 -04:00
|
|
|
private void registerDungeons()
|
2013-08-01 00:05:08 -04:00
|
|
|
{
|
|
|
|
File file = new File(properties.CustomSchematicDirectory);
|
|
|
|
if (file.exists() || file.mkdir())
|
|
|
|
{
|
2013-08-02 04:54:24 -04:00
|
|
|
copyfile.copyFile(DUNGEON_CREATION_GUIDE_SOURCE_PATH, file.getAbsolutePath() + "/How_to_add_dungeons.txt");
|
2013-08-01 00:05:08 -04:00
|
|
|
}
|
2013-08-05 09:48:49 -04:00
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
DungeonPackConfigReader reader = new DungeonPackConfigReader();
|
|
|
|
registerBundledDungeons(reader);
|
|
|
|
registerCustomDungeons(properties.CustomSchematicDirectory, reader);
|
2013-08-05 09:48:49 -04:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
private static DungeonPackConfig loadDungeonPackConfig(String configPath, String name, boolean isInternal, DungeonPackConfigReader reader)
|
2013-08-05 09:48:49 -04:00
|
|
|
{
|
2013-08-20 18:54:30 -04:00
|
|
|
try
|
2013-08-05 09:48:49 -04:00
|
|
|
{
|
2013-08-21 14:26:10 -04:00
|
|
|
DungeonPackConfig config;
|
|
|
|
if (isInternal)
|
|
|
|
{
|
|
|
|
config = reader.readFromResource(configPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config = reader.readFromFile(configPath);
|
|
|
|
}
|
|
|
|
config.setName(name);
|
2013-08-20 18:54:30 -04:00
|
|
|
return config;
|
2013-08-05 09:48:49 -04:00
|
|
|
}
|
2013-08-20 18:54:30 -04:00
|
|
|
catch (ConfigurationProcessingException e)
|
2013-08-05 09:48:49 -04:00
|
|
|
{
|
2013-08-21 14:26:10 -04:00
|
|
|
System.err.println(e.getMessage());
|
|
|
|
if (e.getCause() != null)
|
|
|
|
{
|
|
|
|
System.err.println(e.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException e)
|
|
|
|
{
|
|
|
|
System.err.println("Could not find a dungeon pack config file: " + configPath);
|
2013-08-05 09:48:49 -04:00
|
|
|
}
|
2013-08-21 14:26:10 -04:00
|
|
|
return null;
|
2013-08-01 00:05:08 -04:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
private void registerDungeonPack(String directory, Iterable<String> schematics, boolean isInternal, boolean verbose, DungeonPackConfigReader reader)
|
|
|
|
{
|
|
|
|
//First determine the pack's name and validate it
|
|
|
|
File packDirectory = new File(directory);
|
|
|
|
String name = packDirectory.getName().toUpperCase();
|
|
|
|
//TODO: ADD VALIDATION HERE?
|
|
|
|
|
|
|
|
//Check for naming conflicts
|
|
|
|
//That could happen if a user has a custom pack with a name that conflicts with a bundled pack,
|
|
|
|
//or if a user is running Linux and has two directories with names differing only by capitalization.
|
|
|
|
|
|
|
|
DungeonPack pack = dungeonPackMapping.get(name);
|
|
|
|
if (pack == null)
|
|
|
|
{
|
|
|
|
//Load the pack's configuration file
|
2013-08-22 22:57:26 -04:00
|
|
|
|
|
|
|
String configPath;
|
|
|
|
if (isInternal)
|
|
|
|
{
|
|
|
|
configPath = directory + "/" + STANDARD_CONFIG_FILE_NAME;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
configPath = directory + File.separator + STANDARD_CONFIG_FILE_NAME;
|
|
|
|
}
|
2013-08-22 02:25:26 -04:00
|
|
|
DungeonPackConfig config = loadDungeonPackConfig(configPath, name, isInternal, reader);
|
|
|
|
if (config == null)
|
|
|
|
{
|
|
|
|
System.err.println("Could not load config file: " + configPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Register the pack
|
|
|
|
pack = new DungeonPack(config);
|
|
|
|
dungeonPackMapping.put(name, pack);
|
|
|
|
dungeonPackList.add(pack);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Show a warning that there is a naming conflict but keep going. People can use this to extend
|
|
|
|
//our built-in packs with custom schematics without tampering with our mod's JAR file.
|
|
|
|
System.err.println("A dungeon pack has the same name as another pack that has already been loaded: " + directory);
|
|
|
|
System.err.println("We will try to load its schematics but will not check its config file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Register the dungeons! ^_^
|
|
|
|
for (String schematicPath : schematics)
|
|
|
|
{
|
|
|
|
registerDungeon(schematicPath, pack, isInternal, verbose);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
public List<DungeonData> getRegisteredDungeons()
|
2013-08-01 00:05:08 -04:00
|
|
|
{
|
|
|
|
return Collections.unmodifiableList(this.registeredDungeons);
|
|
|
|
}
|
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
public List<DungeonData> getUntaggedDungeons()
|
2013-08-01 00:05:08 -04:00
|
|
|
{
|
|
|
|
return Collections.unmodifiableList(this.untaggedDungeons);
|
|
|
|
}
|
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
public DungeonData getDefaultErrorDungeon()
|
2013-08-01 13:00:27 -04:00
|
|
|
{
|
|
|
|
return defaultError;
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
public DungeonPack getDungeonPack(String name)
|
|
|
|
{
|
|
|
|
//TODO: This function might be obsolete after the new save format is implemented.
|
|
|
|
return dungeonPackMapping.get(name.toUpperCase());
|
|
|
|
}
|
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
private DungeonPack getDimDungeonPack(NewDimData data)
|
2013-08-21 14:49:41 -04:00
|
|
|
{
|
|
|
|
DungeonPack pack;
|
2013-08-29 02:14:24 -04:00
|
|
|
DungeonData dungeon = data.dungeon();
|
|
|
|
if (dungeon != null)
|
2013-08-21 14:49:41 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
pack = dungeon.dungeonType().Owner;
|
2013-08-21 14:49:41 -04:00
|
|
|
|
|
|
|
//Make sure the pack isn't null. This can happen for dungeons with the special UNKNOWN type.
|
|
|
|
if (pack == null)
|
|
|
|
{
|
|
|
|
pack = RuinsPack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
if (data.id() == NETHER_DIMENSION_ID)
|
2013-08-22 02:25:26 -04:00
|
|
|
{
|
|
|
|
//TODO: Change this to the nether-side pack later ^_^
|
|
|
|
pack = RuinsPack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pack = RuinsPack;
|
|
|
|
}
|
2013-08-21 14:49:41 -04:00
|
|
|
}
|
|
|
|
return pack;
|
|
|
|
}
|
|
|
|
|
2013-09-02 11:47:12 -04:00
|
|
|
public DimLink createCustomDungeonDoor(World world, int x, int y, int z)
|
2013-06-18 10:23:31 -04:00
|
|
|
{
|
|
|
|
//Create a link above the specified position. Link to a new pocket dimension.
|
2013-08-29 02:14:24 -04:00
|
|
|
NewDimData dimension = PocketManager.getDimensionData(world);
|
2013-09-02 11:47:12 -04:00
|
|
|
DimLink link = dimension.createLink(x, y + 1, z, LinkTypes.POCKET);
|
2013-06-18 10:23:31 -04:00
|
|
|
|
|
|
|
//Place a Warp Door linked to that pocket
|
2013-08-31 16:00:18 -04:00
|
|
|
ItemDimensionalDoor.placeDoorBlock(world, x, y, z, 3, mod_pocketDim.warpDoor);
|
2013-06-18 10:23:31 -04:00
|
|
|
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
public boolean validateDungeonType(String type, DungeonPack pack)
|
2013-06-25 13:55:13 -04:00
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
return pack.isKnownType(type);
|
2013-06-25 13:55:13 -04:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
public boolean validateSchematicName(String name, DungeonPack pack)
|
2013-06-15 10:25:50 -04:00
|
|
|
{
|
2013-06-22 14:27:59 -04: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 10:25:50 -04:00
|
|
|
|
|
|
|
//Check for a valid number of parts
|
|
|
|
if (dungeonData.length < 3 || dungeonData.length > 4)
|
|
|
|
return false;
|
|
|
|
|
2013-06-15 11:21:49 -04:00
|
|
|
//Check if the dungeon type is valid
|
2013-08-22 02:25:26 -04:00
|
|
|
if (!validateDungeonType(dungeonData[0], pack))
|
2013-06-15 10:25:50 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
//Check if the name is valid
|
2013-08-05 09:48:49 -04:00
|
|
|
if (!SCHEMATIC_NAME_PATTERN.matcher(dungeonData[1]).matches())
|
2013-06-15 10:25:50 -04: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-08-21 14:26:10 -04:00
|
|
|
if (weight < MIN_DUNGEON_WEIGHT || weight > MAX_DUNGEON_WEIGHT)
|
2013-06-15 10:25:50 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (NumberFormatException e)
|
|
|
|
{
|
|
|
|
//Not a number
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
public void registerDungeon(String schematicPath, DungeonPack pack, boolean isInternal, boolean verbose)
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-08-01 13:00:27 -04: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 10:25:50 -04:00
|
|
|
String name = schematicFile.getName();
|
2013-08-01 13:00:27 -04:00
|
|
|
String path = isInternal ? schematicPath : schematicFile.getAbsolutePath();
|
2013-06-10 17:03:52 -04:00
|
|
|
try
|
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
if (validateSchematicName(name, pack))
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-06-15 10:25:50 -04: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-22 02:25:26 -04:00
|
|
|
DungeonType dungeonType = pack.getType(dungeonData[0]);
|
2013-06-22 14:27:59 -04:00
|
|
|
boolean isOpen = dungeonData[2].equalsIgnoreCase("open");
|
2013-06-15 10:25:50 -04:00
|
|
|
int weight = (dungeonData.length == 4) ? Integer.parseInt(dungeonData[3]) : DEFAULT_DUNGEON_WEIGHT;
|
2013-06-15 11:21:49 -04:00
|
|
|
|
|
|
|
//Add this custom dungeon to the list corresponding to its type
|
2013-08-29 02:14:24 -04:00
|
|
|
DungeonData dungeon = new DungeonData(path, isInternal, dungeonType, isOpen, weight);
|
2013-06-15 10:25:50 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
pack.addDungeon(dungeon);
|
|
|
|
registeredDungeons.add(dungeon);
|
2013-08-01 13:00:27 -04:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
System.out.println("Registered dungeon: " + name);
|
|
|
|
}
|
2013-06-15 10:25:50 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-01 13:00:27 -04:00
|
|
|
if (verbose)
|
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
System.out.println("The following dungeon name is invalid for its given pack. It will not be generated naturally: " + schematicPath);
|
2013-08-01 13:00:27 -04:00
|
|
|
}
|
2013-08-29 02:14:24 -04:00
|
|
|
untaggedDungeons.add(new DungeonData(path, isInternal, DungeonType.UNKNOWN_TYPE, true, DEFAULT_DUNGEON_WEIGHT));
|
2013-08-01 13:00:27 -04:00
|
|
|
System.out.println("Registered untagged dungeon: " + name);
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 02:25:26 -04:00
|
|
|
catch (Exception e)
|
2013-06-15 08:07:33 -04:00
|
|
|
{
|
2013-08-01 13:00:27 -04:00
|
|
|
System.err.println("Failed to register dungeon: " + name);
|
2013-06-15 08:07:33 -04:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
2013-06-15 08:07:33 -04:00
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
private void registerCustomDungeons(String path, DungeonPackConfigReader reader)
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
File[] schematics;
|
|
|
|
File[] packDirectories;
|
|
|
|
File[] packFiles;
|
|
|
|
ArrayList<String> packFilePaths;
|
2013-06-17 06:31:53 -04:00
|
|
|
File directory = new File(path);
|
2013-08-22 02:25:26 -04:00
|
|
|
SchematicFileFilter schematicFileFilter = new SchematicFileFilter();
|
2013-06-15 08:07:33 -04:00
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
//Check that the Ruins pack has been loaded
|
|
|
|
if (RuinsPack == null)
|
|
|
|
{
|
|
|
|
throw new IllegalStateException("Cannot register custom dungeons without first loading the Ruins dungeon pack.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Load stray dungeons directly in the custom dungeons folder
|
|
|
|
schematics = directory.listFiles(schematicFileFilter);
|
|
|
|
if (schematics != null)
|
|
|
|
{
|
|
|
|
for (File schematicFile : schematics)
|
|
|
|
{
|
|
|
|
registerDungeon(schematicFile.getPath(), RuinsPack, false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-06-15 08:07:33 -04:00
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
System.err.println("Could not retrieve the list of schematics stored in the custom dungeons directory!");
|
|
|
|
}
|
|
|
|
schematics = null; //Release memory
|
|
|
|
|
|
|
|
//Load the custom dungeon packs
|
|
|
|
packDirectories = directory.listFiles(new DirectoryFilter());
|
|
|
|
if (packDirectories != null)
|
|
|
|
{
|
|
|
|
//Loop through each directory, which is assumed to be a dungeon pack
|
|
|
|
for (File packDirectory : packDirectories)
|
2013-06-15 08:07:33 -04:00
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
//List the schematics within the dungeon pack directory
|
|
|
|
packFiles = packDirectory.listFiles(schematicFileFilter);
|
|
|
|
if (packFiles != null)
|
2013-06-17 06:31:53 -04:00
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
//Copy the pack files' paths into an ArrayList for use with registerDungeonPack()
|
|
|
|
packFilePaths = new ArrayList<String>(packFiles.length);
|
|
|
|
for (File packFile : packFiles)
|
|
|
|
{
|
|
|
|
packFilePaths.add(packFile.getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
registerDungeonPack(packDirectory.getAbsolutePath(), packFilePaths, false, true, reader);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
System.err.println("Could not retrieve the list of schematics in a dungeon pack: " + packDirectory.getPath());
|
2013-06-17 06:31:53 -04:00
|
|
|
}
|
2013-06-15 08:07:33 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 02:25:26 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
System.err.println("Could not retrieve the list of dungeon pack directories in the custom dungeons directory!");
|
|
|
|
}
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
private void registerBundledDungeons(DungeonPackConfigReader reader)
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-08-01 13:00:27 -04:00
|
|
|
//Register the core schematics
|
|
|
|
//These are used for debugging and in case of unusual errors while loading dungeons
|
2013-08-29 02:14:24 -04:00
|
|
|
defaultError = new DungeonData(DEFAULT_ERROR_SCHEMATIC_PATH, true, DungeonType.UNKNOWN_TYPE, true, DEFAULT_DUNGEON_WEIGHT);
|
2013-08-01 13:00:27 -04:00
|
|
|
|
|
|
|
//Open the list of dungeons packaged with our mod and register their schematics
|
2013-08-22 02:25:26 -04:00
|
|
|
registerBundledPack(BUNDLED_RUINS_LIST_PATH, RUINS_PACK_PATH, "Ruins", reader);
|
|
|
|
RuinsPack = getDungeonPack("Ruins");
|
|
|
|
|
|
|
|
System.out.println("Finished registering bundled dungeon packs");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerBundledPack(String listPath, String packPath, String name, DungeonPackConfigReader reader)
|
|
|
|
{
|
|
|
|
System.out.println("Registering bundled dungeon pack: " + name);
|
|
|
|
|
|
|
|
InputStream listStream = this.getClass().getResourceAsStream(listPath);
|
2013-08-01 13:00:27 -04:00
|
|
|
if (listStream == null)
|
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
System.err.println("Unable to open list of bundled dungeon schematics for " + name);
|
2013-08-01 13:00:27 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-06-17 22:26:39 -04:00
|
|
|
|
2013-08-01 13:00:27 -04:00
|
|
|
try
|
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
//Read the list of schematics that come with a bundled pack
|
2013-08-01 13:00:27 -04:00
|
|
|
BufferedReader listReader = new BufferedReader(new InputStreamReader(listStream));
|
2013-08-22 02:25:26 -04:00
|
|
|
ArrayList<String> schematics = new ArrayList<String>();
|
2013-08-01 13:00:27 -04:00
|
|
|
String schematicPath = listReader.readLine();
|
|
|
|
while (schematicPath != null)
|
|
|
|
{
|
|
|
|
schematicPath = schematicPath.trim();
|
|
|
|
if (!schematicPath.isEmpty())
|
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
schematics.add(schematicPath);
|
2013-08-01 13:00:27 -04:00
|
|
|
}
|
|
|
|
schematicPath = listReader.readLine();
|
|
|
|
}
|
|
|
|
listReader.close();
|
2013-08-22 02:25:26 -04:00
|
|
|
|
|
|
|
//Register the pack
|
|
|
|
registerDungeonPack(packPath, schematics, true, false, reader);
|
2013-08-01 13:00:27 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-08-22 02:25:26 -04:00
|
|
|
System.err.println("An exception occurred while reading the list of bundled dungeon schematics for " + name);
|
2013-08-01 13:00:27 -04:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
2013-06-15 08:07:33 -04:00
|
|
|
|
2013-07-15 01:58:01 -04:00
|
|
|
public boolean exportDungeon(World world, int centerX, int centerY, int centerZ, String exportPath)
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-07-15 04:15:58 -04:00
|
|
|
//Write schematic data to a file
|
2013-06-15 08:07:33 -04:00
|
|
|
try
|
|
|
|
{
|
2013-07-29 09:58:47 -04:00
|
|
|
DungeonSchematic dungeon = DungeonSchematic.copyFromWorld(world,
|
2013-07-31 08:05:58 -04: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 13:58:14 -04:00
|
|
|
dungeon.applyExportFilters(properties);
|
2013-07-29 09:58:47 -04:00
|
|
|
dungeon.writeToFile(exportPath);
|
2013-06-16 05:59:53 -04:00
|
|
|
return true;
|
2013-06-15 08:07:33 -04:00
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
2013-06-16 05:59:53 -04:00
|
|
|
return false;
|
2013-06-15 08:07:33 -04:00
|
|
|
}
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
2013-06-15 08:07:33 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
public DungeonData selectDungeon(NewDimData dimension, Random random)
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
DungeonPack pack = getDimDungeonPack(dimension);
|
|
|
|
DungeonData selection;
|
2013-08-21 14:26:10 -04:00
|
|
|
DungeonPackConfig config;
|
|
|
|
DungeonPack selectedPack;
|
2013-08-04 19:27:34 -04:00
|
|
|
|
2013-06-10 17:03:52 -04:00
|
|
|
try
|
2013-08-21 14:26:10 -04:00
|
|
|
{
|
|
|
|
config = pack.getConfig();
|
|
|
|
selectedPack = pack;
|
|
|
|
|
2013-08-22 02:25:26 -04:00
|
|
|
//Are we allowed to switch to another dungeon pack?
|
2013-08-21 14:26:10 -04:00
|
|
|
if (config.allowPackChangeOut())
|
|
|
|
{
|
|
|
|
//Calculate the chance of switching to a different pack type
|
|
|
|
int packSwitchChance;
|
2013-08-29 02:14:24 -04:00
|
|
|
if (dimension.depth() == 1)
|
2013-08-21 14:26:10 -04:00
|
|
|
{
|
|
|
|
packSwitchChance = START_PACK_SWITCH_CHANCE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
packSwitchChance = MIN_PACK_SWITCH_CHANCE + dimension.parent().packDepth() * PACK_SWITCH_CHANCE_PER_LEVEL;
|
2013-08-21 14:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Decide randomly whether to switch packs or not
|
|
|
|
if (random.nextInt(MAX_PACK_SWITCH_CHANCE) < packSwitchChance)
|
|
|
|
{
|
|
|
|
//Find another pack
|
|
|
|
selectedPack = getRandomDungeonPack(pack, random);
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 02:25:26 -04:00
|
|
|
|
|
|
|
//Pick the next dungeon
|
2013-08-29 02:14:24 -04:00
|
|
|
selection = selectedPack.getNextDungeon(dimension, random);
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
2013-06-15 08:07:33 -04:00
|
|
|
catch (Exception e)
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-08-04 19:27:34 -04:00
|
|
|
System.err.println("An exception occurred while selecting a dungeon:");
|
2013-06-16 14:40:26 -04:00
|
|
|
e.printStackTrace();
|
2013-08-04 19:27:34 -04:00
|
|
|
|
|
|
|
if (!pack.isEmpty())
|
2013-06-10 17:03:52 -04:00
|
|
|
{
|
2013-08-04 19:27:34 -04:00
|
|
|
selection = pack.getRandomDungeon(random);
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-04 19:27:34 -04:00
|
|
|
selection = defaultError;
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 02:14:24 -04:00
|
|
|
return selection;
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|
2013-06-17 06:18:15 -04:00
|
|
|
|
2013-08-21 14:26:10 -04:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private DungeonPack getRandomDungeonPack(DungeonPack current, Random random)
|
|
|
|
{
|
|
|
|
DungeonPack selection = current;
|
|
|
|
ArrayList<WeightedContainer<DungeonPack>> packs = new ArrayList<WeightedContainer<DungeonPack>>(dungeonPackList.size());
|
|
|
|
|
|
|
|
//Load up a list of weighted items with any usable dungeon packs that is not the current pack
|
|
|
|
for (DungeonPack pack : dungeonPackList)
|
|
|
|
{
|
|
|
|
DungeonPackConfig config = pack.getConfig();
|
|
|
|
if (pack != current && config.allowPackChangeIn() && !pack.isEmpty())
|
|
|
|
{
|
|
|
|
packs.add(new WeightedContainer<DungeonPack>(pack, config.getPackWeight()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!packs.isEmpty())
|
|
|
|
{
|
|
|
|
//Pick a random dungeon pack
|
|
|
|
selection = ((WeightedContainer<DungeonPack>) WeightedRandom.getRandomItem(random, packs)).getData();
|
|
|
|
}
|
|
|
|
return selection;
|
|
|
|
}
|
|
|
|
|
2013-06-17 06:18:15 -04:00
|
|
|
public Collection<String> getDungeonNames() {
|
2013-06-17 06:31:53 -04:00
|
|
|
|
2013-06-17 06:18:15 -04: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-07-31 23:56:26 -04:00
|
|
|
dungeonNames.addAll( parseDungeonNames(untaggedDungeons) );
|
2013-06-17 06:31:53 -04:00
|
|
|
|
|
|
|
//Sort dungeon names alphabetically
|
|
|
|
ArrayList<String> sortedNames = new ArrayList<String>(dungeonNames);
|
2013-07-31 23:56:26 -04:00
|
|
|
Collections.sort(sortedNames, String.CASE_INSENSITIVE_ORDER);
|
2013-06-17 06:31:53 -04:00
|
|
|
return sortedNames;
|
2013-06-17 06:18:15 -04:00
|
|
|
}
|
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
private static ArrayList<String> parseDungeonNames(ArrayList<DungeonData> dungeons)
|
2013-06-17 06:18:15 -04:00
|
|
|
{
|
|
|
|
String name;
|
|
|
|
File schematic;
|
|
|
|
ArrayList<String> names = new ArrayList<String>(dungeons.size());
|
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
for (DungeonData dungeon : dungeons)
|
2013-06-17 06:18:15 -04:00
|
|
|
{
|
2013-06-17 06:34:35 -04:00
|
|
|
//Retrieve the file name and strip off the file extension
|
2013-08-29 02:14:24 -04:00
|
|
|
schematic = new File(dungeon.schematicPath());
|
2013-06-17 06:18:15 -04:00
|
|
|
name = schematic.getName();
|
|
|
|
name = name.substring(0, name.length() - SCHEMATIC_FILE_EXTENSION.length());
|
|
|
|
names.add(name);
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2013-06-17 22:26:39 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
public static ArrayList<DungeonData> getDungeonChainHistory(NewDimData dimension, DungeonPack pack, int maxSize)
|
2013-06-17 22:26:39 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
if (dimension == null)
|
2013-08-05 20:16:45 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
throw new IllegalArgumentException("dimension cannot be null.");
|
2013-08-05 20:16:45 -04:00
|
|
|
}
|
2013-08-04 19:27:34 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
int count = 0;
|
|
|
|
NewDimData tail = dimension;
|
|
|
|
DungeonData dungeon = tail.dungeon();
|
|
|
|
ArrayList<DungeonData> history = new ArrayList<DungeonData>();
|
2013-08-21 14:26:10 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
while (count < maxSize && dungeon != null && dungeon.dungeonType().Owner == pack)
|
2013-08-21 14:26:10 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
history.add(dungeon);
|
|
|
|
tail = tail.parent();
|
|
|
|
dungeon = tail.dungeon();
|
|
|
|
count++;
|
2013-08-21 14:26:10 -04:00
|
|
|
}
|
2013-08-29 02:14:24 -04:00
|
|
|
return history;
|
2013-08-21 14:26:10 -04:00
|
|
|
}
|
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
public static ArrayList<DungeonData> getFlatDungeonTree(NewDimData dimension, int maxSize)
|
2013-08-05 20:16:45 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
NewDimData root = dimension;
|
|
|
|
ArrayList<DungeonData> dungeons = new ArrayList<DungeonData>();
|
|
|
|
Queue<NewDimData> pendingDimensions = new LinkedList<NewDimData>();
|
2013-08-05 20:16:45 -04:00
|
|
|
|
2013-08-29 02:14:24 -04:00
|
|
|
if (root.dungeon() == null)
|
2013-08-05 20:16:45 -04:00
|
|
|
{
|
|
|
|
return dungeons;
|
|
|
|
}
|
|
|
|
pendingDimensions.add(root);
|
|
|
|
|
|
|
|
while (dungeons.size() < maxSize && !pendingDimensions.isEmpty())
|
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
NewDimData current = pendingDimensions.remove();
|
|
|
|
for (NewDimData child : current.children())
|
2013-07-12 23:40:21 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
if (child.dungeon() != null)
|
2013-07-12 23:40:21 -04:00
|
|
|
{
|
2013-08-29 02:14:24 -04:00
|
|
|
dungeons.add(child.dungeon());
|
2013-08-05 20:16:45 -04:00
|
|
|
pendingDimensions.add(child);
|
|
|
|
}
|
|
|
|
if (dungeons.size() == maxSize)
|
|
|
|
{
|
|
|
|
break;
|
2013-07-12 23:40:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-05 20:16:45 -04:00
|
|
|
return dungeons;
|
2013-07-12 23:40:21 -04:00
|
|
|
}
|
2013-06-10 17:03:52 -04:00
|
|
|
}
|