Progress on Implementing Dungeon Packs

Completed enough of the implementation and integration to compile DD.
Some portions of the code are only for testing and will be removed
later. The configuration for default dungeons is hardcoded - we can
parse config files once we're certain that dungeon chains work. At the
moment, dungeons generate but it doesn't seem like the rules we set are
being followed properly.

Renamed OptimizedRule to DungeonChainRule, and renamed the old
DungeonChainRule to DungeonChainRuleDefinition, to match the role of
each class better. Added some hax to DungeonGenerator to get packs
integrated - the implementation will be much cleaner once the new save
format is done.
This commit is contained in:
SenseiKiwi
2013-08-05 09:48:49 -04:00
parent e96fc02747
commit 101e9e4ce6
10 changed files with 298 additions and 157 deletions

View File

@@ -1,13 +1,66 @@
package StevenDimDoors.mod_pocketDim.dungeon.pack;
import java.util.ArrayList;
import java.util.HashMap;
import StevenDimDoors.mod_pocketDim.util.WeightedContainer;
public class DungeonChainRule
{
private final int[] condition;
private final ArrayList<WeightedContainer<DungeonType>> products;
public DungeonChainRule(DungeonChainRuleDefinition source, HashMap<String, DungeonType> nameToTypeMapping)
{
ArrayList<String> conditionNames = source.getCondition();
ArrayList<WeightedContainer<String>> productNames = source.getProducts();
public OptimizedRule optimize(HashMap<String, DungeonType> nameToTypeMapping) {
// TODO Auto-generated method stub
return null;
condition = new int[conditionNames.size()];
for (int k = 0; k < condition.length; k++)
{
condition[k] = nameToTypeMapping.get(conditionNames.get(k)).ID;
}
products = new ArrayList<WeightedContainer<DungeonType>>();
for (WeightedContainer<String> product : productNames)
{
products.add(new WeightedContainer<DungeonType>(nameToTypeMapping.get(product.getData()), product.itemWeight ));
}
}
public int length()
{
return condition.length;
}
public boolean evaluate(int[] typeHistory)
{
if (typeHistory.length >= condition.length)
{
for (int k = 0; k < condition.length; k++)
{
if (condition[k] != 0 && typeHistory[k] != condition[k])
{
return false;
}
}
return true;
}
else
{
return false;
}
}
public ArrayList<WeightedContainer<DungeonType>> products()
{
//Create a deep copy of the internal list of products. That way, if the list is modified externally,
//it won't affect the reference copy inside this rule.
ArrayList<WeightedContainer<DungeonType>> copy = new ArrayList<WeightedContainer<DungeonType>>(products.size());
for (WeightedContainer<DungeonType> container : products)
{
copy.add(container.clone());
}
return copy;
}
}

View File

@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import net.minecraft.util.WeightedRandom;
@@ -16,9 +15,11 @@ import StevenDimDoors.mod_pocketDim.util.WeightedContainer;
public class DungeonPack
{
//Why final? I just felt like it, honestly. ~SenseiKiwi
private static final DungeonType WILDCARD_TYPE = new DungeonType(null, "?", 0);
//There is no precaution against having a dungeon type removed from a config file after dungeons of that type
//have been generated. That would likely cause one or two problems. It's hard to guard against when I don't know
//what the save format will be like completely. How should this class behave if it finds a "disowned" type?
//The ID numbers would be a problem since it couldn't have a valid number, since it wasn't initialized by the pack instance.
//FIXME: Do not release this code as an update without dealing with disowned types!
private final String name;
private final HashMap<String, DungeonType> nameToTypeMapping;
@@ -26,7 +27,7 @@ public class DungeonPack
private final ArrayList<DungeonGenerator> allDungeons;
private final DungeonPackConfig config;
private final int maxRuleLength;
private final ArrayList<OptimizedRule> rules;
private final ArrayList<DungeonChainRule> rules;
public DungeonPack(DungeonPackConfig config)
{
@@ -42,7 +43,7 @@ public class DungeonPack
this.groupedDungeons = new ArrayList<ArrayList<DungeonGenerator>>(typeCount);
this.groupedDungeons.add(allDungeons); //Make sure the list of all dungeons is placed at index 0
this.nameToTypeMapping.put(WILDCARD_TYPE.Name, WILDCARD_TYPE);
this.nameToTypeMapping.put(DungeonType.WILDCARD_TYPE.Name, DungeonType.WILDCARD_TYPE);
index = 1;
for (String typeName : config.getTypeNames())
@@ -53,22 +54,23 @@ public class DungeonPack
index++;
}
//Construct optimized rules from config rules
ArrayList<DungeonChainRule> chainRules = config.getRules();
this.rules = new ArrayList<OptimizedRule>(chainRules.size());
for (DungeonChainRule rule : chainRules)
//Construct optimized rules from definitions
ArrayList<DungeonChainRuleDefinition> definitions = config.getRules();
this.rules = new ArrayList<DungeonChainRule>(definitions.size());
for (DungeonChainRuleDefinition definition : definitions)
{
OptimizedRule optimized = rule.optimize(nameToTypeMapping);
this.rules.add(optimized);
if (maxLength < optimized.length())
DungeonChainRule rule = new DungeonChainRule(definition, nameToTypeMapping);
this.rules.add(rule);
if (maxLength < rule.length())
{
maxLength = optimized.length();
maxLength = rule.length();
}
}
this.maxRuleLength = maxLength;
//Remove the reference to the non-optimized rules to free up memory - we won't need them here
//Remove unnecessary references to save a little memory - we won't need them here
this.config.setRules(null);
this.config.setTypeNames(null);
}
public String getName()
@@ -84,7 +86,7 @@ public class DungeonPack
public DungeonType getType(String typeName)
{
DungeonType result = nameToTypeMapping.get(typeName.toUpperCase());
if (result != WILDCARD_TYPE)
if (result.Owner == this) //Filter out the wildcard dungeon type
{
return result;
}
@@ -99,6 +101,21 @@ public class DungeonPack
return (this.getType(typeName) != null);
}
public void addDungeon(DungeonGenerator generator)
{
//Make sure this dungeon really belongs in this pack
DungeonType type = generator.getDungeonType();
if (type.Owner == this)
{
allDungeons.add(generator);
groupedDungeons.get(type.ID).add(generator);
}
else
{
throw new IllegalArgumentException("The dungeon type of generator must belong to this instance of DungeonPack.");
}
}
public DungeonGenerator getNextDungeon(LinkData inbound, Random random)
{
if (allDungeons.isEmpty())
@@ -106,12 +123,14 @@ public class DungeonPack
return null;
}
//Retrieve a list of the previous dungeons in this chain. Restrict the length of the
//search to the length of the longest rule. Getting more data is useless.
dimHelper helper = dimHelper.instance;
//Retrieve a list of the previous dungeons in this chain.
//If we're not going to check for duplicates in chains, restrict the length of the history to the length
//of the longest rule we have. Getting any more data would be useless. This optimization could be significant
//for dungeon packs that can extend arbitrarily deep. We should probably set a reasonable limit anyway.
//TODO: Add dungeon pack parameter! We can't use dungeon types from other packs.
ArrayList<DungeonGenerator> history = DungeonHelper.getDungeonChainHistory(helper.getDimData(inbound.locDimID), maxRuleLength);
int maxSearchLength = config.allowDuplicatesInChain() ? maxRuleLength : 1337;
ArrayList<DungeonGenerator> history = DungeonHelper.getDungeonChainHistory(
dimHelper.instance.getDimData(inbound.locDimID), this, maxSearchLength);
return getNextDungeon(history, random);
}
@@ -123,10 +142,10 @@ public class DungeonPack
HashSet<DungeonGenerator> excludedDungeons = null;
for (index = 0; index < typeHistory.length; index++)
{
typeHistory[index] = getDungeonType(history.get(index)).ID;
typeHistory[index] = history.get(index).getDungeonType().ID;
}
for (OptimizedRule rule : rules)
for (DungeonChainRule rule : rules)
{
if (rule.evaluate(typeHistory))
{
@@ -139,9 +158,9 @@ public class DungeonPack
if (nextType != null)
{
//Initialize the set of excluded dungeons if needed
if (excludedDungeons == null && config.allowDuplicatesInChain())
if (excludedDungeons == null && !config.allowDuplicatesInChain())
{
//TODO: Finish implementing this!
excludedDungeons = new HashSet<DungeonGenerator>(history);
}
//List which dungeons are allowed
@@ -166,6 +185,8 @@ public class DungeonPack
{
return getRandomDungeon(random, candidates);
}
//If we've reached this point, then a dungeon was not selected. Discard the type and try again.
products.remove(nextType);
}
}
while (nextType != null);
@@ -175,16 +196,6 @@ public class DungeonPack
//None of the rules were applicable. Simply return a random dungeon.
return getRandomDungeon(random);
}
private DungeonType getDungeonType(DungeonGenerator generator)
{
//This function is a workaround for DungeonGenerator not having a dungeon type or pack field.
//I really don't want to go messing around with that serializable type.
//TODO: Remove this function once we transition to using the new save format. ~SenseiKiwi
//TODO: Finish implementing this!
return null;
}
public DungeonGenerator getRandomDungeon(Random random)
{
@@ -202,7 +213,7 @@ public class DungeonPack
ArrayList<ArrayList<DungeonGenerator>> groupedDungeons)
{
//TODO: Make this faster? This algorithm runs in quadratic time in the worst case because of the random-selection
//process and the removal search. Should be okay for normal use, though. ~SenseiKiwi
//process and the removal search. Might be okay for normal use, though. ~SenseiKiwi
//Pick a random dungeon type based on weights. Repeat this process until a non-empty group is found or all groups are checked.
while (!types.isEmpty())

View File

@@ -1,22 +1,36 @@
package StevenDimDoors.mod_pocketDim.dungeon.pack;
import java.util.ArrayList;
import java.util.List;
public class DungeonPackConfig
{
private String name;
private ArrayList<String> typeNames;
private boolean allowDuplicatesInChain;
private ArrayList<DungeonChainRuleDefinition> rules;
public DungeonPackConfig() { }
@SuppressWarnings("unchecked")
private DungeonPackConfig(DungeonPackConfig source)
{
this.name = source.name;
this.typeNames = (ArrayList<String>) source.typeNames.clone();
this.allowDuplicatesInChain = source.allowDuplicatesInChain;
this.rules = (ArrayList<DungeonChainRuleDefinition>) source.rules.clone();
}
public void validate()
{
if (this.name == null)
throw new NullPointerException("name cannot be null");
if (this.typeNames == null)
throw new NullPointerException("typeNames cannot be null");
if (this.rules == null)
throw new NullPointerException("rules cannot be null");
}
@Override
public DungeonPackConfig clone()
{
return new DungeonPackConfig(this);
@@ -24,26 +38,41 @@ public class DungeonPackConfig
public String getName()
{
return null;
return name;
}
public void setName(String name)
{
this.name = name;
}
public List<String> getTypeNames()
public ArrayList<String> getTypeNames()
{
return null;
return typeNames;
}
public void setTypeNames(ArrayList<String> typeNames)
{
this.typeNames = typeNames;
}
public boolean allowDuplicatesInChain()
{
return false;
return allowDuplicatesInChain;
}
public void setAllowDuplicatesInChain(boolean value)
{
allowDuplicatesInChain = value;
}
public void setRules(Object object) {
// TODO Auto-generated method stub
public void setRules(ArrayList<DungeonChainRuleDefinition> rules)
{
this.rules = rules;
}
public ArrayList<DungeonChainRule> getRules() {
// TODO Auto-generated method stub
return null;
public ArrayList<DungeonChainRuleDefinition> getRules()
{
return rules;
}
}

View File

@@ -2,6 +2,9 @@ package StevenDimDoors.mod_pocketDim.dungeon.pack;
public class DungeonType implements Comparable<DungeonType>
{
public static final DungeonType WILDCARD_TYPE = new DungeonType(null, "?", 0);
public static final DungeonType UNKNOWN_TYPE = new DungeonType(null, "!", -1);
public final DungeonPack Owner;
public final String Name;
public final int ID;

View File

@@ -1,25 +0,0 @@
package StevenDimDoors.mod_pocketDim.dungeon.pack;
import java.util.ArrayList;
import StevenDimDoors.mod_pocketDim.util.WeightedContainer;
public class OptimizedRule
{
public int length() {
// TODO Auto-generated method stub
return 0;
}
public boolean evaluate(int[] typeHistory) {
// TODO Auto-generated method stub
return false;
}
public ArrayList<WeightedContainer<DungeonType>> products() {
// TODO Auto-generated method stub
return null;
}
}