Moved generic methods to utility classes

-Moved several methods to DDRandomUtils and DDMathUtils
-Added several config options
-Corrected some config options' grammar and spelling
This commit is contained in:
Mathijs Riezebos 2017-03-19 13:34:27 +01:00
parent d6d8a2bb49
commit 3b41149c16
16 changed files with 273 additions and 79 deletions

View file

@ -61,7 +61,7 @@ public class DimDoors {
event.registerServerCommand(new TeleportCommand());
//@todo event.registerServerCommand( new DDCommand() ); //to register commands that this mod offers?
RiftRegistry.Instance.reset();
PocketRegistry.Instance.reset();
PocketRegistry.INSTANCE.reset();
RiftSavedData.get(getDefWorld());
PocketSavedData.get(getDefWorld());
SchematicHandler.Instance.loadSchematics();

View file

@ -12,6 +12,7 @@ import java.util.List;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import scala.actors.threadpool.Arrays;
/**
*
@ -19,7 +20,7 @@ import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
*/
public class DDConfig {
public static final boolean haveConfigDefaultsBeenCheckedForCorrectness = false; //@todo check this at each non-alpha release. This field does not have a use in the mod itself, but should ensure that the developers of this mod, don't forget to reset the config defaults to the right values before releasing a non-alpha release
public static final boolean HAVE_CONFIG_DEFAULTS_BEEN_CHECKED_FOR_CORRECTNESS = false; //@todo check this at each non-alpha release. This field does not have a use in the mod itself, but should ensure that the developers of this mod, don't forget to reset the config defaults to the right values before releasing a non-alpha release
public static File configurationFolder;
private static int pocketGridSize = 8;
@ -28,6 +29,11 @@ public class DDConfig {
private static int publicPocketSize = 2;
private static int baseDimID = 684;
private static String[] dungeonSchematicNames = {}; //@todo set default dungeon names
private static int maxDungeonDepth = 8;
private static int owCoordinateOffsetBase = 64;
private static double owCoordinateOffsetPower = 1.3;
private static int[] doorRelativeDepths = new int[]{-1, 0, 1};
private static int[] doorRelativeDepthWeights = new int[]{20, 30, 50};
private static int setConfigIntWithMaxAndMin(Configuration config, String category, String key, int defaultValue, String comment, int minValue, int maxValue) {
Property prop = config.get(category, key, defaultValue,
@ -60,26 +66,64 @@ public class DDConfig {
DimDoors.log(DDConfig.class, "pocketGridSize was set to " + pocketGridSize);
maxPocketSize = setConfigIntWithMaxAndMin(config, Configuration.CATEGORY_GENERAL, "maxPocketSize", maxPocketSize,
"Sets how many deep and wide any pocket can be. [min: 1, max: pocketGridSize/2, default: 4]", 1, (int) (((double) pocketGridSize / 2) - 0.5));
"Sets how deep and wide any pocket can be. [min: 1, max: pocketGridSize/2, default: 4]", 1, (int) (((double) pocketGridSize / 2) - 0.5));
privatePocketSize = setConfigIntWithMaxAndMin(config, Configuration.CATEGORY_GENERAL, "privatePocketSize", privatePocketSize,
"Sets how many deep and wide any personal pocket can be. [min: 1, max: maxPocketsSize, default: 3]", 1, maxPocketSize);
"Sets how deep and wide any personal pocket can be. [min: 1, max: maxPocketsSize, default: 3]", 1, maxPocketSize);
publicPocketSize = setConfigIntWithMaxAndMin(config, Configuration.CATEGORY_GENERAL, "publicPocketSize", publicPocketSize,
"Sets how many deep and wide any public pocket can be. [min: 1, max: maxPocketsSize, default: 2]", 1, maxPocketSize);
"Sets how deep and wide any public pocket can be. [min: 1, max: maxPocketsSize, default: 2]", 1, maxPocketSize);
prop = config.get(Configuration.CATEGORY_GENERAL, "dungeonSchematicNames", dungeonSchematicNames,
"List of names of Pockets' jSon- and Schematic file names excluding extention. Custom json and schematic files can be dropped in the corresponding folders.");
"List of names of Pockets' jSon- and Schematic file names excluding extension. Custom json and schematic files can be dropped in the corresponding folders.");
dungeonSchematicNames = prop.getStringList();
prop = config.get(Configuration.CATEGORY_GENERAL, "baseDimID", baseDimID,
"Dimension ID of the first Dimensional Doors dimension. Other dimensions will use consecutive IDs. NB: If you change this after creating a world, you may lose these dimensions. [default: 684]");
baseDimID = prop.getInt(baseDimID);
maxDungeonDepth = setConfigIntWithMaxAndMin(config, Configuration.CATEGORY_GENERAL, "maxDungeonDepth", maxDungeonDepth,
"Sets the maximum (deepest) depth that a dungeon pocket can be at. [min: 1, max: 32, default: 8]", 1, 32);
owCoordinateOffsetBase = setConfigIntWithMaxAndMin(config, Configuration.CATEGORY_GENERAL, "owCoordinateOffsetBase", owCoordinateOffsetBase,
"Determines how heavy the depth weighs when determining the overworld coordinates corresponding to a dungeon pocket. [min: 1, max: 128, default: 64]", 1, 128);
prop = config.get(Configuration.CATEGORY_GENERAL, "owCoordinateOffsetPower", owCoordinateOffsetPower,
"Determines how heavy the depth weighs when determining the overworld coordinates corresponding to a dungeon pocket. [default: 1.3]"
+ System.getProperty("line.separator") + "max offset = (depth * owCoordinateOffsetBase)^owCoordinateOffsetPower");
owCoordinateOffsetPower = prop.getDouble(owCoordinateOffsetPower);
prop = config.get(Configuration.CATEGORY_GENERAL, "doorRelativeDepths", doorRelativeDepths,
"List of possible depths that a new dungeon Pocket can generate at, relative to the origin door.");
doorRelativeDepths = prop.getIntList();
prop = config.get(Configuration.CATEGORY_GENERAL, "doorRelativeDepthWeights", doorRelativeDepthWeights,
"List of weights (chances) of the relative depths in doorRelativeDepths. This list needs to have the same size.");
doorRelativeDepthWeights = prop.getIntList();
checkAndCorrectDoorRelativeDepths(config);
// Save config
config.save();
}
private static void checkAndCorrectDoorRelativeDepths(Configuration config) {
int d = doorRelativeDepths.length;
int dw = doorRelativeDepthWeights.length;
if (d != dw) {
Property prop;
if (d > dw) {
doorRelativeDepths = Arrays.copyOf(doorRelativeDepths, dw);
prop = config.get(Configuration.CATEGORY_GENERAL, "doorRelativeDepths", doorRelativeDepths); //I hope that this works (not a disaster if it doesn't).
prop.set(doorRelativeDepths);
} else {
doorRelativeDepthWeights = Arrays.copyOf(doorRelativeDepthWeights, d);
prop = config.get(Configuration.CATEGORY_GENERAL, "doorRelativeDepthWeights", doorRelativeDepthWeights);
prop.set(doorRelativeDepthWeights);
}
}
}
public static int getPocketGridSize() {
return pocketGridSize;
}
@ -107,4 +151,39 @@ public class DDConfig {
public static int getBaseDimID() {
return baseDimID;
}
/**
* @return the owCoordinateOffsetBase
*/
public static int getOwCoordinateOffsetBase() {
return owCoordinateOffsetBase;
}
/**
* @return the owCoordinateOffsetPower
*/
public static double getOwCoordinateOffsetPower() {
return owCoordinateOffsetPower;
}
/**
* @return the doorRelativeDepths
*/
public static int[] getDoorRelativeDepths() {
return doorRelativeDepths;
}
/**
* @return the doorRelativeDepthWeights
*/
public static int[] getDoorRelativeDepthWeights() {
return doorRelativeDepthWeights;
}
/**
* @return the maxDungeonDepth
*/
public static int getMaxDungeonDepth() {
return maxDungeonDepth;
}
}

View file

@ -20,7 +20,7 @@ public class DDEventHandler {
@SubscribeEvent
public void onPlayerJoinWorld(EntityJoinWorldEvent event) { //@todo, probably move this to another class
//check if config default values have been checked
if (!DDConfig.haveConfigDefaultsBeenCheckedForCorrectness) {
if (!DDConfig.HAVE_CONFIG_DEFAULTS_BEEN_CHECKED_FOR_CORRECTNESS) {
if (!DimDoors.VERSION.contains("a")) { //if it is not an alpha version
Entity entity = event.getEntity();
if (entity instanceof EntityPlayer) {

View file

@ -47,7 +47,7 @@ public class Pocket {
this.riftIDs = riftIDs;
this.depthZeroLocation = depthZeroLocation;
playerUUIDs = new ArrayList();
PocketRegistry.Instance.registerNewPocket(this, typeID);
PocketRegistry.INSTANCE.registerNewPocket(this, typeID); //@todo, maybe not register pockets inside their own constructor?
for (int riftID : riftIDs) {
Location riftLocation = RiftRegistry.Instance.getRiftLocation(riftID);

View file

@ -7,15 +7,14 @@ package com.zixiken.dimdoors.shared;
import com.zixiken.dimdoors.shared.util.Location;
import com.zixiken.dimdoors.DimDoors;
import com.zixiken.dimdoors.shared.util.DDRandomUtils;
import com.zixiken.dimdoors.shared.world.DimDoorDimensions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
/**
*
@ -23,7 +22,7 @@ import net.minecraft.util.EnumFacing;
*/
public class PocketRegistry {
public static final PocketRegistry Instance = new PocketRegistry();
public static final PocketRegistry INSTANCE = new PocketRegistry();
// Privates
private int gridSize; //determines how much pockets in their dimension are spaced
@ -142,7 +141,7 @@ public class PocketRegistry {
pocketLists.get(pocketType).put(nextUnusedIDs.get(pocketType), pocket);
pocket.setID(nextUnusedIDs.get(pocketType));
nextUnusedIDs.put(pocketType, nextUnusedIDs.get(pocketType) + 1);
nextUnusedIDs.put(pocketType, nextUnusedIDs.get(pocketType) + 1); //increase the counter
PocketSavedData.get(DimDoors.getDefWorld()).markDirty(); //Notify that this needs to be saved on world save
return nextUnusedIDs.get(pocketType) - 1;
}
@ -160,36 +159,38 @@ public class PocketRegistry {
}
public int getEntranceDoorIDOfNewPocket(EnumPocketType typeID, int depth, Location origRiftLocation) {//should return the riftID of the entrance door of the newly generated pocket
Location shortenedLocation = getGenerationlocation(nextUnusedIDs.get(typeID), typeID);
int x = shortenedLocation.getPos().getX();
int z = shortenedLocation.getPos().getZ();
Pocket pocket = generateRandomPocketAt(typeID, depth, shortenedLocation, origRiftLocation); //registers the pocket as well
Pocket pocket = generateRandomPocketAt(typeID, depth, origRiftLocation); //@todo registers the pocket as well in constructor (should probably be changed)
int entranceDoorID = pocket.getEntranceDoorID();
return entranceDoorID;
}
private Pocket generateRandomPocketAt(EnumPocketType typeID, int depth, Location shortenedLocation, Location origRiftLocation) {
int shortenedX = shortenedLocation.getPos().getX();
int shortenedZ = shortenedLocation.getPos().getZ();
int dimID = shortenedLocation.getDimensionID();
//correction just in case
private Pocket generateRandomPocketAt(EnumPocketType typeID, int depth, Location origRiftLocation) {
//Correcting the depth. Just in case...
if (typeID == EnumPocketType.DUNGEON) {
if (depth <= 0) {
depth = 1;
} else if (depth > DDConfig.getMaxDungeonDepth()) {
depth = DDConfig.getMaxDungeonDepth();
}
} else {
depth = 0;
}
//Fetching the pocket template
PocketTemplate pocketTemplate = getRandomPocketTemplate(typeID, depth, maxPocketSize);
//Getting the physical grid-location and the Overworld coordinates
Location shortenedLocation = getGenerationlocation(nextUnusedIDs.get(typeID), typeID);
int shortenedX = shortenedLocation.getPos().getX();
int shortenedZ = shortenedLocation.getPos().getZ();
int dimID = shortenedLocation.getDimensionID();
Location depthZeroLocation;
if (typeID != EnumPocketType.PRIVATE) {
depthZeroLocation = transformLocationRandomly(depth, origRiftLocation);
depthZeroLocation = DDRandomUtils.transformLocationRandomly(DDConfig.getOwCoordinateOffsetBase(), DDConfig.getOwCoordinateOffsetPower(), depth, origRiftLocation);
} else {
depthZeroLocation = origRiftLocation;
}
Pocket pocket = pocketTemplate.place(shortenedX, 0, shortenedZ, gridSize, dimID, nextUnusedIDs.get(typeID), depth, typeID, depthZeroLocation);
return pocket;
}
@ -201,7 +202,7 @@ public class PocketRegistry {
private Location getGenerationlocation(int nextUnusedID, EnumPocketType typeID) { //typeID is for determining the dimension
int x = getSimpleX(nextUnusedID, typeID);
int y = 0;
int z = getSimpleZ(nextUnusedID, typeID);;
int z = getSimpleZ(nextUnusedID, typeID);
int dimID = DimDoorDimensions.getPocketDimensionType(typeID).getId();
Location location = new Location(dimID, x, y, z);
@ -269,11 +270,4 @@ public class PocketRegistry {
}
return group;
}
private Location transformLocationRandomly(int depth, Location origLocation) {
Random random = new Random();
int xOffset = ((64 * depth) ^ (9 / 7)) * (random.nextBoolean() ? 1 : -1);
int zOffset = ((64 * depth) ^ (9 / 7)) * (random.nextBoolean() ? 1 : -1);
return new Location(origLocation.getWorld(), origLocation.getPos().offset(EnumFacing.EAST, xOffset).offset(EnumFacing.SOUTH, zOffset));
}
}

View file

@ -46,7 +46,7 @@ public class PocketSavedData extends DDSavedData {
public NBTTagCompound writeToNBT(NBTTagCompound pocketnbt) {
NBTTagCompound pockets = new NBTTagCompound();
PocketRegistry.Instance.writeToNBT(pockets);
PocketRegistry.INSTANCE.writeToNBT(pockets);
pocketnbt.setTag("pockets", pockets);
return pocketnbt;
@ -58,7 +58,7 @@ public class PocketSavedData extends DDSavedData {
if (pocketnbt != null) {
if (pocketnbt.hasKey("pockets")) {
NBTTagCompound pockets = pocketnbt.getCompoundTag("pockets");
PocketRegistry.Instance.readFromNBT(pockets);
PocketRegistry.INSTANCE.readFromNBT(pockets);
}
}
}

View file

@ -68,11 +68,11 @@ public class SchematicHandler {
}
public void loadSchematics() {
personalPocketTemplate = loadTemplatesFromJson("defaultPrivate", PocketRegistry.Instance.getPrivatePocketSize()).get(0);
publicPocketTemplate = loadTemplatesFromJson("defaultPublic", PocketRegistry.Instance.getPublicPocketSize()).get(0);
personalPocketTemplate = loadTemplatesFromJson("defaultPrivate", PocketRegistry.INSTANCE.getPrivatePocketSize()).get(0);
publicPocketTemplate = loadTemplatesFromJson("defaultPublic", PocketRegistry.INSTANCE.getPublicPocketSize()).get(0);
dungeonTemplates = new ArrayList();
List<String> dungeonSchematicNameStrings = DDConfig.getDungeonSchematicNames();
int maxPocketSize = PocketRegistry.Instance.getMaxPocketSize();
int maxPocketSize = PocketRegistry.INSTANCE.getMaxPocketSize();
for (String nameString : dungeonSchematicNameStrings) {
List<PocketTemplate> templates = loadTemplatesFromJson(nameString, maxPocketSize);
if (templates != null) {

View file

@ -151,7 +151,7 @@ public abstract class DDTileEntityBase extends TileEntity {
if (!isInPocket || pocketType == EnumPocketType.PRIVATE) {
return;
} else {
Pocket pocket = PocketRegistry.Instance.getPocket(pocketID, pocketType);
Pocket pocket = PocketRegistry.INSTANCE.getPocket(pocketID, pocketType);
pocket.validatePlayerEntry(player);
}
}

View file

@ -1,12 +1,14 @@
package com.zixiken.dimdoors.shared.tileentities;
import com.zixiken.dimdoors.DimDoors;
import com.zixiken.dimdoors.shared.DDConfig;
import com.zixiken.dimdoors.shared.EnumPocketType;
import com.zixiken.dimdoors.shared.PocketRegistry;
import com.zixiken.dimdoors.shared.blocks.BlockDimDoor;
import com.zixiken.dimdoors.shared.util.Location;
import com.zixiken.dimdoors.shared.RiftRegistry;
import com.zixiken.dimdoors.shared.TeleportHelper;
import com.zixiken.dimdoors.shared.util.DDRandomUtils;
import com.zixiken.dimdoors.shared.world.DimDoorDimensions;
import java.util.Random;
import javax.annotation.Nullable;
@ -91,21 +93,21 @@ public class TileEntityDimDoor extends DDTileEntityBase {
}
protected int getNewTeleportDestination() {
int otherRiftID = -1;
int otherRiftID;
Location locationOfThisRift = RiftRegistry.Instance.getRiftLocation(this.riftID);
if (locationOfThisRift.getDimensionID() == DimDoorDimensions.getPocketDimensionType(EnumPocketType.DUNGEON).getId()) { //if this dimdoor is in a pocket Dungeon
//@todo choose between generating a new pocket or connecting to another door on a similar or close depth
if (randomBooleanChoice(20, 80)) {
if (locationOfThisRift.getDimensionID() == DimDoorDimensions.getPocketDimensionType(EnumPocketType.DUNGEON).getId()) { //if this dimdoor is a pocket Dungeon
//choose between generating a new pocket or connecting to another door on a similar or close depth
if (DDRandomUtils.weightedBoolean(20, 80)) { //@todo make this configurable
otherRiftID = RiftRegistry.Instance.getRandomUnpairedRiftIDAroundDepth(getRiftID(), depth);
if (otherRiftID < 0) {
if (otherRiftID < 0) { //ergo: no other rift can be found
//@todo, this should rarely happen. Put in an easter egg?
otherRiftID = PocketRegistry.Instance.getEntranceDoorIDOfNewPocket(EnumPocketType.DUNGEON, getRandomisedDepth(), locationOfThisRift);
otherRiftID = PocketRegistry.INSTANCE.getEntranceDoorIDOfNewPocket(EnumPocketType.DUNGEON, getRandomlyTransFormedDepth(), locationOfThisRift);
}
} else {
otherRiftID = PocketRegistry.Instance.getEntranceDoorIDOfNewPocket(EnumPocketType.DUNGEON, getRandomisedDepth(), locationOfThisRift);
otherRiftID = PocketRegistry.INSTANCE.getEntranceDoorIDOfNewPocket(EnumPocketType.DUNGEON, getRandomlyTransFormedDepth(), locationOfThisRift);
}
} else {
otherRiftID = PocketRegistry.Instance.getEntranceDoorIDOfNewPocket(EnumPocketType.PUBLIC, 0, locationOfThisRift); //@todo should this depth be 1 instead?
otherRiftID = PocketRegistry.INSTANCE.getEntranceDoorIDOfNewPocket(EnumPocketType.PUBLIC, 0, locationOfThisRift); //@todo should this depth be 1 instead?
}
if (otherRiftID < 0) {
@ -118,17 +120,7 @@ public class TileEntityDimDoor extends DDTileEntityBase {
return otherRiftID;
}
private boolean randomBooleanChoice(int trueWeight, int falseWeight) { //@todo make this a utility function
if (trueWeight <= 0 || falseWeight <= 0) {
throw new IllegalArgumentException("Either of both weights was 0 or lower. Both should be at least 1.");
}
Random random = new Random();
return (random.nextInt(trueWeight + falseWeight) < trueWeight);
}
protected int getRandomisedDepth() {
Random random = new Random();
int choice = random.nextInt(100);
return (choice < 20 ? depth - 1 : choice < 50 ? depth : depth + 1); //@todo get rid of hardcoded stuff
protected int getRandomlyTransFormedDepth() {
return DDRandomUtils.transformRandomly(depth, DDConfig.getDoorRelativeDepths(), DDConfig.getDoorRelativeDepthWeights());
}
}

View file

@ -5,6 +5,7 @@ import com.zixiken.dimdoors.shared.EnumPocketType;
import com.zixiken.dimdoors.shared.IChunkLoader;
import com.zixiken.dimdoors.shared.PocketRegistry;
import com.zixiken.dimdoors.shared.RiftRegistry;
import com.zixiken.dimdoors.shared.util.DDRandomUtils;
import com.zixiken.dimdoors.shared.util.Location;
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.ForgeChunkManager.Ticket;
@ -69,15 +70,15 @@ public class TileEntityDimDoorGold extends TileEntityDimDoor implements IChunkLo
int otherRiftID = RiftRegistry.Instance.getRandomUnpairedRiftIDAtDepth(getRiftID(), depth);
if (otherRiftID < 0) {
Location locationOfThisRift = RiftRegistry.Instance.getRiftLocation(this.riftID);
otherRiftID = PocketRegistry.Instance.getEntranceDoorIDOfNewPocket(EnumPocketType.DUNGEON, getRandomisedDepth(), locationOfThisRift);
otherRiftID = PocketRegistry.INSTANCE.getEntranceDoorIDOfNewPocket(EnumPocketType.DUNGEON, getRandomlyTransFormedDepth(), locationOfThisRift);
}
if (otherRiftID < 0) {
DimDoors.warn(this.getClass(), "No suitable destination rift was found. This probably means that a pocket was created without any Doors.");
} else {
RiftRegistry.Instance.pair(getRiftID(), otherRiftID);
}
return otherRiftID;
}
}

View file

@ -31,9 +31,9 @@ public class TileEntityDimDoorPersonal extends TileEntityDimDoor {
if (entity instanceof EntityPlayer) {
EntityPlayer entityPlayer = (EntityPlayer) entity;
if (locationOfThisRift.getDimensionID() == DimDoorDimensions.getPocketDimensionType(EnumPocketType.PRIVATE).getId()) {
tpLocation = PocketRegistry.Instance.getPocket(this.pocketID, EnumPocketType.PRIVATE).getDepthZeroLocation();
tpLocation = PocketRegistry.INSTANCE.getPocket(this.pocketID, EnumPocketType.PRIVATE).getDepthZeroLocation();
} else {
tpLocation = RiftRegistry.Instance.getTeleportLocation(PocketRegistry.Instance.getPrivateDimDoorID(entityPlayer.getCachedUniqueIdString()));
tpLocation = RiftRegistry.Instance.getTeleportLocation(PocketRegistry.INSTANCE.getPrivateDimDoorID(entityPlayer.getCachedUniqueIdString()));
}
} else {
return false;

View file

@ -19,7 +19,7 @@ public class TileEntityDimDoorWarp extends TileEntityDimDoor {
} else if (!(this.isInPocket)) {
return false;
} else {
Pocket pocket = PocketRegistry.Instance.getPocket(this.pocketID, this.getPocketType());
Pocket pocket = PocketRegistry.INSTANCE.getPocket(this.pocketID, this.getPocketType());
teleportLocation = pocket.getDepthZeroLocation();
}
return TeleportHelper.teleport(entity, teleportLocation);

View file

@ -40,7 +40,7 @@ public class TileEntityTransTrapdoor extends DDTileEntityBase {
} else if (!(this.isInPocket)) {
return false;
} else {
Pocket pocket = PocketRegistry.Instance.getPocket(this.pocketID, this.getPocketType());
Pocket pocket = PocketRegistry.INSTANCE.getPocket(this.pocketID, this.getPocketType());
teleportLocation = pocket.getDepthZeroLocation();
}
return TeleportHelper.teleport(entity, teleportLocation);

View file

@ -0,0 +1,61 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.zixiken.dimdoors.shared.util;
/**
*
* @author Robijnvogel
*/
public class DDMathUtils {
/**
* Compares the integers in two arrays and returns true if any integer in
* the first set is within range 0 to {@code difference} - 1 of any integer
* in the second array.
*
* @param setOne the first integer array to compare
* @param difference see method description
* @param setTwo the second integer array to compare
* @pre difference >= 0
* @throws IllegalArgumentException if precondition is violated
* @return {@code (\exists i, j; ; abs(setOne[i] - setTwo[j]) < difference }
*/
public static boolean withinDistanceOf(int[] setOne, int difference, int[] setTwo) throws IllegalArgumentException {
if (difference < 0) {
throw new IllegalArgumentException("precondition was violated");
}
for (int One : setOne) {
for (int Two : setTwo) {
if ((Math.max(One, Two) - Math.min(One, Two)) < difference) {
return true;
}
}
}
return false;
}
/**
* Returns the sum of the values of all integer elements in an integer array
*
* @param intArray
* @param flag this flag is meant to check if all elements in the array must
* be positive, negative, or it doesn't matter
* @pre
* {@code flag == 0 || (flag == 1 && (\forall i; intArray.has(i); i >= 0)) || (flag == 2 && (\forall i; intArray.has(i); i <= 0))}
* @throws IllegalArgumentException if precondition is violated
* @return {@code sum(i = 0; intArray.has(i); intArray[i]) }
*/
public static int arraySum(int[] intArray, short flag) {
int r = 0;
for (int i : intArray) { //check flag
if (flag == 0 && i < 0) { //
throw new IllegalArgumentException("all integers in array must be positive");
}
r += i;
}
return r;
}
}

View file

@ -0,0 +1,79 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.zixiken.dimdoors.shared.util;
import java.util.Random;
import net.minecraft.util.EnumFacing;
/**
*
* @author Robijnvogel
*/
public class DDRandomUtils {
/**
* Returns either true or false, based on a weighted random, taking in
* account the parameters as the weights for true and false as outcomes. For
* instance: (2, 3) should return true in 40% of the cases and false in 60%
* of the cases.
*
* @param trueWeight the chance weight that this method will return true
* @param falseWeight the chance weight that this method will return false
* @pre {@code trueWeight > 0 && falseWeight > 0}
* @throws IllegalArgumentException if precondition is violated
* @return true or false
*/
public static boolean weightedBoolean(int trueWeight, int falseWeight) { //@todo make this a utility function
if (trueWeight <= 0 || falseWeight <= 0) {
throw new IllegalArgumentException("Either of both weights were 0 or lower. Both should be at least 1.");
}
Random random = new Random();
return (random.nextInt(trueWeight + falseWeight) < trueWeight);
}
/**
* Hello world
*
* @param base
* @param transformations
* @param weights
* @pre transformations.length = weights.length
* @throws IllegalArgumentException if precondition is violated
* @return the sum of {@code base} and the value of an element in
* {@code transformations}
*/
public static int transformRandomly(int base, int[] transformations, int[] weights) {
if (transformations.length != weights.length) {
throw new IllegalArgumentException("pre was violated");
}
Random random = new Random();
int weightSum = DDMathUtils.arraySum(weights, (short) 1);
int choice = random.nextInt(weightSum);
for (int i = 0; i < weights.length; i++) {
choice -= weights[i];
if (choice < 0) {
return base + transformations[i];
}
}
throw new IllegalStateException("");
}
/**
*
* @param base
* @param power
* @param depth
* @param origLocation
* @return
*/
public static Location transformLocationRandomly(int base, double power, int depth, Location origLocation) {
Random random = new Random();
int xOffset = (int) Math.pow(base * depth, power) * (random.nextBoolean() ? 1 : -1);
int zOffset = (int) Math.pow(base * depth, power) * (random.nextBoolean() ? 1 : -1);
return new Location(origLocation.getWorld(), origLocation.getPos().offset(EnumFacing.EAST, xOffset).offset(EnumFacing.SOUTH, zOffset));
}
}

View file

@ -443,7 +443,7 @@ public class Schematic {
|| y == 0 || y == maxbound - 1
|| z == 0 || z == maxbound - 1) {
schematic.blockData[x][y][z] = 1; //outer dim wall
} else if (withinDistanceOf(new int[]{x, y, z}, 6, new int[]{0, maxbound})) {
} else if (DDMathUtils.withinDistanceOf(new int[]{x, y, z}, 6, new int[]{0, maxbound})) {
if (z == 4 && x == (maxbound - 1) / 2 && y > 4 && y < 7) {
if (y == 5) {
schematic.blockData[x][y][z] = 3; //door bottom
@ -471,16 +471,4 @@ public class Schematic {
SchematicHandler.Instance.saveSchematic(schematic, schematic.schematicName);
}
*/
private static boolean withinDistanceOf(int[] setOne, int difference, int[] setTwo) { //@todo add to a utilityclass
for (int One : setOne) {
for (int Two : setTwo) {
if ((Math.max(One, Two) - Math.min(One, Two)) < difference) {
return true;
}
}
}
return false;
}
}