Worked on power issues

This commit is contained in:
DarkGuardsman 2013-09-06 04:23:36 -04:00
parent 9fe604449b
commit d1cd75c612
9 changed files with 310 additions and 94 deletions

View file

@ -1,52 +0,0 @@
package dark.api.energy;
import cpw.mods.fml.common.Loader;
public enum PowerSystems
{
INDUSTRIALCRAFT("IC2"),
MEKANISM("Mekanism"),
BUILDCRAFT("BuildCraft|Energy");
public String id;
public static final PowerSystems[] UE_SUPPORTED_SYSTEMS = new PowerSystems[] { INDUSTRIALCRAFT, MEKANISM, BUILDCRAFT };
private PowerSystems(String id)
{
this.id = id;
}
private static boolean init = false;
private static Boolean[] loaded;
/** Checks to see if something can run powerless based on mods loaded
*
* @param optional - power system that the device can use
* @return true if free power is to be generated */
public static boolean runPowerLess(PowerSystems... optional)
{
for (int i = 0; i < optional.length; i++)
{
if (isPowerSystemLoaded(optional[i], false))
{
return false;
}
}
return true;
}
/** Check to see if one of the mods listed in the PowerSystem enum is loaded */
public static boolean isPowerSystemLoaded(PowerSystems power, boolean force)
{
if (!init || force)
{
loaded = new Boolean[PowerSystems.values().length];
for (int i = 0; i < PowerSystems.values().length; i++)
{
loaded[i] = Loader.isModLoaded(PowerSystems.values()[i].id);
}
init = true;
}
return power != null && loaded[power.ordinal()];
}
}

View file

@ -124,6 +124,7 @@ public class DarkMain extends ModPrefab
public void init(FMLInitializationEvent event)
{
BlockRegistry.registerAllBlocks();
ExternalModHandler.init();
super.init(event);
if (CoreRecipeLoader.blockOre != null)

View file

@ -1,32 +1,79 @@
package dark.core.common;
import java.util.HashMap;
import cpw.mods.fml.common.Loader;
import net.minecraft.tileentity.TileEntity;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.IPipeTile.PipeType;
import dark.core.prefab.helpers.Pair;
/** Handles working with other mod without or without the need of the APIs.
*
* @author DarkGuardsman */
public class ExternalModHandler
{
private static HashMap<String, Pair<Integer, Integer>> pipeMap = new HashMap<String, Pair<Integer, Integer>>();
private static boolean init = false;
public static void mapBuildCraftPipes()
/** Calls this to load all external mod settings and handling */
public static void init()
{
//TODO map pipe blockIDs, and metadata for later use
if (!init)
{
for (MOD_ID mod : MOD_ID.values())
{
mod.loaded = Loader.isModLoaded(mod.modID);
}
}
}
/** Is the tileEntity an instanceof IPipeTile and of type fluid from BuildCraft */
public boolean isBCFluidPipe(TileEntity entity)
public static boolean isBCFluidPipe(TileEntity entity)
{
return entity instanceof IPipeTile && ((IPipeTile) entity).getPipeType() == PipeType.FLUID;
return MOD_ID.BUILCRAFT_TRANSPORT_MOD.loaded && entity instanceof IPipeTile && ((IPipeTile) entity).getPipeType() == PipeType.FLUID;
}
public boolean isBCPowerPipe(TileEntity entity)
/** Is the tileEntity an instanceof IPipeTile and of type power from BuildCraft */
public static boolean isBCPowerPipe(TileEntity entity)
{
return entity instanceof IPipeTile && ((IPipeTile) entity).getPipeType() == PipeType.POWER;
return MOD_ID.BUILCRAFT_TRANSPORT_MOD.loaded && entity instanceof IPipeTile && ((IPipeTile) entity).getPipeType() == PipeType.POWER;
}
/** Checks to see if something can run powerless based on mods loaded
*
* @param optional - power system that the device can use
* @return true if free power is to be generated */
public static boolean runPowerLess()
{
for (MOD_ID mod : MOD_ID.values())
{
if (mod.validPowerSystem)
{
return false;
}
}
return true;
}
/** Enum storing MOD_IDs and some general info on mods */
public static enum MOD_ID
{
BUILCRAFT_MOD("BuildCraft|Core", false),
BUILCRAFT_ENERGY_MOD("BuildCraft|Energy", true),
BUILCRAFT_FACTORY_MOD("BuildCraft|Factory", false),
BUILCRAFT_SILICON_MOD("BuildCraft|Silicon", false),
BUILCRAFT_BUILDERS_MOD("BuildCraft|Builders", false),
BUILCRAFT_TRANSPORT_MOD("BuildCraft|Transport", false),
INDUSTRIALCRAFT_MOD("IC2", true),
MEKANISM_MOD("Mekanism", true); //TODO add mek sub mods
public final String modID;
public String modNAME;
public boolean loaded;
public boolean validPowerSystem;
private MOD_ID(String modID, boolean power)
{
this.modID = modID;
this.validPowerSystem = power;
}
}
}

View file

@ -46,12 +46,6 @@ public class TileEntityInfLoad extends TileEntityUniversalElectrical
return 0;
}
@Override
public float getVoltage()
{
return 120;
}
@Override
public float getMaxEnergyStored()
{

View file

@ -49,12 +49,6 @@ public class TileEntityInfSupply extends TileEntityUniversalElectrical
return 1000;
}
@Override
public float getVoltage()
{
return 120;
}
@Override
public float getMaxEnergyStored()
{

View file

@ -96,7 +96,7 @@ public class ItemTools extends ItemBasic
{
if (tileEntity instanceof IToolReadOut)
{
String output = ((IToolReadOut) tileEntity).getMeterReading(player, ForgeDirection.getOrientation(side), EnumTools.PIPE_GUAGE);
String output = ((IToolReadOut) tileEntity).getMeterReading(player, ForgeDirection.getOrientation(side), tool);
if (output != null && !output.isEmpty())
{
if (output.length() > 100)
@ -134,7 +134,7 @@ public class ItemTools extends ItemBasic
{
float demand = ((IElectrical) tileEntity).getRequest(ForgeDirection.getOrientation(side).getOpposite());
float provide = ((IElectrical) tileEntity).getProvide(ForgeDirection.getOrientation(side).getOpposite());
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(" Voltage>" + ((IElectrical) tileEntity).getVoltage()));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(" Voltage>" + ((IElectrical) tileEntity).getVoltage()*1000));
if (demand > 0)
{
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(String.format(" RequiredWatts> %1$.2fKW", demand)));
@ -147,13 +147,13 @@ public class ItemTools extends ItemBasic
}
if (tileEntity instanceof IElectricalStorage)
{
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(String.format(" EnergyStored> %1$.2fKW of %2$.2fW max", ((IElectricalStorage) tileEntity).getEnergyStored(), ((IElectricalStorage) tileEntity).getMaxEnergyStored())));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(String.format(" EnergyStored> %1$.2fKW of %2$.2fKW max", ((IElectricalStorage) tileEntity).getEnergyStored(), ((IElectricalStorage) tileEntity).getMaxEnergyStored())));
out = true;
}
if (tileEntity instanceof IConductor)
{
out = true;
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(String.format(" Resistance> %1$.2fKW | AmpMax> %2$.2fW", ((IConductor) tileEntity).getResistance(), ((IConductor) tileEntity).getCurrentCapacity())));
player.sendChatToPlayer(ChatMessageComponent.func_111066_d(String.format(" Resistance> %1$.2fKW | AmpMax> %2$.2fA", ((IConductor) tileEntity).getResistance(), ((IConductor) tileEntity).getCurrentCapacity())));
if (((IConductor) tileEntity).getNetwork() != null)
{

View file

@ -25,14 +25,14 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.api.IDisableable;
import dark.api.energy.IPowerLess;
import dark.api.energy.PowerSystems;
import dark.core.common.ExternalModHandler;
import dark.core.interfaces.IExternalInv;
import dark.core.interfaces.IInvBox;
import dark.core.prefab.invgui.InvChest;
/** Prefab for most machines in the CoreMachine set. Provides basic power updates, packet updates,
* inventory handling, and other handy methods.
*
*
* @author DarkGuardsman */
public abstract class TileEntityMachine extends TileEntityUniversalElectrical implements ISidedInventory, IExternalInv, IDisableable, IPacketReceiver, IPowerLess
{
@ -94,6 +94,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
if (!this.worldObj.isRemote)
{
boolean prevRun = this.running;
this.running = this.canRun() && this.consumePower(this.WATTS_PER_TICK, true);
if (prevRun != this.running)
{
@ -109,6 +110,15 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
}
}
public void doPowerDebug()
{
System.out.println("\n CanRun: " + this.canRun());
System.out.println(" RedPower: " + this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord));
System.out.println(" IsDisabled: " + this.isDisabled());//TODO i'm going to kick myself if this is it, yep disabled
System.out.println(" HasPower: " + this.consumePower(WATTS_PER_TICK, false));
System.out.println(" IsRunning: " + this.running + " \n");
}
/** Called to consume power from the internal storage */
public boolean consumePower(float watts, boolean doDrain)
{
@ -132,7 +142,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
@Override
public boolean runPowerLess()
{
return this.unpowered || PowerSystems.runPowerLess(PowerSystems.UE_SUPPORTED_SYSTEMS);
return this.unpowered || ExternalModHandler.runPowerLess();
}
@Override
@ -172,8 +182,6 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
{
if (!this.runPowerLess() && receive != null && this.canConnect(from))
{
// Only do voltage disable if the voltage is higher than the peek voltage and if random chance
//TODO replace random with timed damage to only disable after so many ticks
if (receive != null && receive.voltage > (Math.sqrt(2) * this.getVoltage()) && this.worldObj.rand.nextBoolean())
{
if (doReceive)
@ -265,7 +273,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
}
/** Handles reduced data from the main packet method
*
*
* @param id - packet ID
* @param dis - data
* @param player - player

View file

@ -59,7 +59,7 @@ public class FluidHelper
}
/** Gets the block's fluid if it has one
*
*
* @param world - world we are working in
* @param vector - 3D location in world
* @return @Fluid that the block is */
@ -96,10 +96,10 @@ public class FluidHelper
}
/** Drains a block of fluid
*
*
* @Note sets the block with a client update only. Doesn't tick the block allowing for better
* placement of fluid that can flow infinitely
*
*
* @param doDrain - do the action
* @return FluidStack drained from the block */
public static FluidStack drainBlock(World world, Vector3 node, boolean doDrain)
@ -108,7 +108,7 @@ public class FluidHelper
}
/** Drains a block of fluid
*
*
* @param doDrain - do the action
* @param update - block update flag to use
* @return FluidStack drained from the block */
@ -128,7 +128,7 @@ public class FluidHelper
{
return ((IFluidBlock) block).drain(world, node.intX(), node.intY(), node.intZ(), doDrain);
}
else if (block.blockID == Block.waterStill.blockID && node.getBlockMetadata(world) == 0)
else if ((block.blockID == Block.waterStill.blockID || block.blockID == Block.waterMoving.blockID) && node.getBlockMetadata(world) == 0)
{
if (doDrain)
{
@ -145,7 +145,7 @@ public class FluidHelper
}
return new FluidStack(FluidRegistry.getFluid("water"), FluidContainerRegistry.BUCKET_VOLUME);
}
else if (block.blockID == Block.lavaStill.blockID && node.getBlockMetadata(world) == 0)
else if ((block.blockID == Block.lavaStill.blockID || block.blockID == Block.lavaMoving.blockID) && node.getBlockMetadata(world) == 0)
{
if (doDrain)
{
@ -207,9 +207,9 @@ public class FluidHelper
}
/** Helper method to fill a location with a fluid
*
*
* Note: This does not update the block to prevent the liquid from flowing
*
*
* @return */
public static int fillBlock(World world, Vector3 node, FluidStack stack, boolean doFill)
{
@ -243,7 +243,7 @@ public class FluidHelper
}
/** Fills all instances of IFluidHandler surrounding the origin
*
*
* @param stack - FluidStack that will be filled into the tanks
* @param doFill - Actually perform the action or simulate action
* @param ignore - ForgeDirections to ignore
@ -276,7 +276,7 @@ public class FluidHelper
}
/** Fills an instance of IFluidHandler in the given direction
*
*
* @param stack - FluidStack to fill the tank will
* @param doFill - Actually perform the action or simulate action
* @param direction - direction to fill in from the origin

View file

@ -0,0 +1,224 @@
package dark.core.prefab.tilenetwork;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
/** Check if a conductor connects with another. */
public abstract class ResourcePathFinder
{
/** Curent world this pathfinder will operate in */
private World world;
/** List of all nodes traveled by the path finder */
public Set<Vector3> nodeList = new HashSet<Vector3>();
/** List of all nodes that match the search parms */
public Set<Vector3> results = new HashSet<Vector3>();
/** Are we looking for liquid fillable blocks */
private boolean fill = false;
/** priority search direction either up or down only */
private ForgeDirection priority;
/** Limit on the searched nodes per run */
private int resultLimit = 200;
private int resultsFound = 0;
private int resultRun = resultLimit;
private int runs = 0;
/** Start location of the pathfinder used for range calculations */
private Vector3 Start;
/** Range to limit the search to */
private double range;
/** List of forgeDirection to use that are shuffled to prevent strait lines */
List<ForgeDirection> shuffledDirections = new ArrayList<ForgeDirection>();
public ResourcePathFinder(final World world, final int resultLimit, final double range)
{
this.range = range;
this.world = world;
if (fill)
{
priority = ForgeDirection.DOWN;
}
else
{
priority = ForgeDirection.UP;
}
this.resultLimit = resultLimit;
this.reset();
shuffledDirections.add(ForgeDirection.EAST);
shuffledDirections.add(ForgeDirection.WEST);
shuffledDirections.add(ForgeDirection.NORTH);
shuffledDirections.add(ForgeDirection.SOUTH);
}
public void addNode(Vector3 vec)
{
if (!this.nodeList.contains(vec))
{
this.nodeList.add(vec);
}
}
public void addResult(Vector3 vec)
{
if (!this.results.contains(vec))
{
this.resultsFound++;
this.results.add(vec);
}
}
/** Searches for nodes attached to the given node
*
* @return True on success finding, false on failure. */
public boolean findNodes(Vector3 node)
{
if (node == null)
{
return false;
}
try
{
this.addNode(node);
if (this.isValidResult(node))
{
this.addResult(node);
}
if (this.isDone(node.clone()))
{
return false;
}
if (find(this.priority, node.clone()))
{
return true;
}
Collections.shuffle(shuffledDirections);
for (ForgeDirection direction : shuffledDirections)
{
if (find(direction, node.clone()))
{
return true;
}
}
if (find(this.priority.getOpposite(), node.clone()))
{
return true;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
/** Finds a node in a give direction
*
* Note: Calls findNode if the next code is valid */
public boolean find(ForgeDirection direction, Vector3 origin)
{
this.runs++;
Vector3 vec = origin.clone().modifyPositionFromSide(direction);
double distance = vec.toVector2().distanceTo(this.Start.toVector2());
if (distance <= this.range && this.isValidNode(vec))
{
if (onFind())
{
return true;
}
if (this.findNodes(vec))
{
return true;
}
}
return false;
}
/** Called when the pathfinder jumps to the next block. Use this to inject other processes into
* the find method
*
* @return true if you found results, or just want the calling method to return true */
public boolean onFind()
{
return false;
}
/** Checks to see if this node is valid to path find threw */
public abstract boolean isValidNode(Vector3 pos);
public abstract boolean isValidResult(Vector3 node);
/** Checks to see if we are done pathfinding */
public abstract boolean isDone(Vector3 vec);
/** Called to execute the pathfinding operation. */
public ResourcePathFinder start(final Vector3 startNode, int runCount, final boolean fill)
{
this.Start = startNode;
this.fill = fill;
this.runs = 0;
this.resultsFound = 0;
this.resultRun = runCount;
this.find(ForgeDirection.UNKNOWN, startNode);
this.refresh();
this.sortResults(Start, results);
return this;
}
public ResourcePathFinder reset()
{
this.nodeList.clear();
this.results.clear();
return this;
}
public ResourcePathFinder refresh()
{
Iterator<Vector3> it = this.nodeList.iterator();
while (it.hasNext())
{
Vector3 vec = it.next();
if (!this.isValidNode(vec))
{
it.remove();
}
if (this.isValidResult(vec))
{
this.addResult(vec);
}
}
it = this.results.iterator();
while (it.hasNext())
{
Vector3 vec = it.next();
if (!this.isValidResult(vec))
{
it.remove();
}
if (this.isValidNode(vec))
{
this.addNode(vec);
}
}
return this;
}
public void sortResults(Vector3 start, Set<Vector3> list)
{
}
}