fix pathfiner, add gui textures

This commit is contained in:
Robert 2013-12-22 03:59:30 -05:00
parent 738ffa7b2a
commit e0fc1ca8f6
14 changed files with 297 additions and 13 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 815 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 978 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 875 B

View file

@ -10,8 +10,11 @@ import net.minecraft.nbt.NBTTagFloat;
import net.minecraft.nbt.NBTTagLong; import net.minecraft.nbt.NBTTagLong;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
import universalelectricity.api.CompatibilityModule;
import universalelectricity.api.energy.IEnergyContainer; import universalelectricity.api.energy.IEnergyContainer;
import universalelectricity.api.energy.IEnergyInterface; import universalelectricity.api.energy.IEnergyInterface;
import universalelectricity.api.vector.Vector3;
import universalelectricity.api.vector.VectorHelper;
import com.builtbroken.minecraft.interfaces.IPowerLess; import com.builtbroken.minecraft.interfaces.IPowerLess;
@ -103,7 +106,7 @@ public abstract class TileEntityEnergyMachine extends TileEntityMachine implemen
@Override @Override
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
{ {
if (!this.runPowerLess() && receive > 0 && this.canConnect(from)) if (!this.runPowerLess() && receive > 0 && this.getInputDirections().contains(from))
{ {
long prevEnergyStored = this.getEnergy(from); long prevEnergyStored = this.getEnergy(from);
long newStoredEnergy = Math.min(this.getEnergy(from) + receive, this.getEnergyCapacity(from)); long newStoredEnergy = Math.min(this.getEnergy(from) + receive, this.getEnergyCapacity(from));
@ -151,6 +154,17 @@ public abstract class TileEntityEnergyMachine extends TileEntityMachine implemen
return 0; return 0;
} }
protected void produce()
{
for (ForgeDirection direction : this.getOutputDirections())
{
if (this.onExtractEnergy(direction.getOpposite(), CompatibilityModule.receiveEnergy(VectorHelper.getTileEntityFromSide(this.worldObj, new Vector3(this), direction), direction, this.onExtractEnergy(direction.getOpposite(), this.JOULES_PER_TICK, false), true), true) > 0)
{
break;
}
}
}
/* ******************************************** /* ********************************************
* Electricity connection logic * Electricity connection logic
***********************************************/ ***********************************************/

View file

@ -1,9 +0,0 @@
package com.builtbroken.minecraft.prefab.invgui;
/** Same as the GuiMachineBase but supports inventory pages
*
* @author DarkGuardsman */
public class GuiInvMachineBase
{
}

View file

@ -0,0 +1,25 @@
package com.builtbroken.minecraft.tilenetwork;
import java.util.Set;
import universalelectricity.api.vector.Vector3;
public interface IPathCallBack
{
/**
* @param finder - The Pathfinder object.
* @param currentNode - The node being iterated through.
* @return A set of nodes connected to the currentNode. Essentially one should return a set of
* neighboring nodes.
*/
public Set<Vector3> getConnectedNodes(Pathfinder finder, Vector3 currentNode);
/**
* Called when looping through nodes.
*
* @param finder - The Pathfinder.
* @param node - The node being searched.
* @return True to stop the path finding operation.
*/
public boolean onSearch(Pathfinder finder, Vector3 node);
}

View file

@ -0,0 +1,79 @@
package com.builtbroken.minecraft.tilenetwork;
import java.util.HashSet;
import java.util.Set;
import universalelectricity.api.vector.Vector3;
/**
* A class that allows flexible pathfinding for different positions. Compared to AStar pathfinding,
* this version is faster but does not calculated the most optimal path.
*
* @author Calclavia
*
*/
public class Pathfinder
{
/**
* A pathfinding call back interface used to call back on paths.
*/
public IPathCallBack callBackCheck;
/**
* A list of nodes that the pathfinder already went through.
*/
public Set<Vector3> closedSet;
/**
* The resulted path found by the pathfinder. Could be null if no path was found.
*/
public Set<Vector3> results;
public Pathfinder(IPathCallBack callBack)
{
this.callBackCheck = callBack;
this.reset();
}
/**
* @return True on success finding, false on failure.
*/
public boolean findNodes(Vector3 currentNode)
{
this.closedSet.add(currentNode);
if (this.callBackCheck.onSearch(this, currentNode))
{
return false;
}
for (Vector3 node : this.callBackCheck.getConnectedNodes(this, currentNode))
{
if (!this.closedSet.contains(node))
{
if (this.findNodes(node))
{
return true;
}
}
}
return false;
}
/**
* Called to execute the pathfinding operation.
*/
public Pathfinder init(Vector3 startNode)
{
this.findNodes(startNode);
return this;
}
public Pathfinder reset()
{
this.closedSet = new HashSet<Vector3>();
this.results = new HashSet<Vector3>();
return this;
}
}

View file

@ -0,0 +1,175 @@
package com.builtbroken.minecraft.tilenetwork;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.vector.Vector3;
/**
* An advanced version of pathfinding to find the shortest path between two points. Uses the A*
* Pathfinding algorithm.
*
* @author Calclavia
*
*/
public class PathfinderAStar extends Pathfinder
{
/**
* A pathfinding call back interface used to call back on paths.
*/
public IPathCallBack callBackCheck;
/**
* The set of tentative nodes to be evaluated, initially containing the start node
*/
public Set<Vector3> openSet;
/**
* The map of navigated nodes storing the data of which position came from which in the format
* of: X came from Y.
*/
public HashMap<Vector3, Vector3> navigationMap;
/**
* Score values, used to determine the score for a path to evaluate how optimal the path is.
* G-Score is the cost along the best known path while F-Score is the total cost.
*/
public HashMap<Vector3, Double> gScore, fScore;
/**
* The node in which the pathfinder is trying to reach.
*/
public Vector3 goal;
public PathfinderAStar(IPathCallBack callBack, Vector3 goal)
{
super(callBack);
this.goal = goal;
}
@Override
public boolean findNodes(Vector3 start)
{
this.openSet.add(start);
this.gScore.put(start, 0d);
this.fScore.put(start, this.gScore.get(start) + getHeuristicEstimatedCost(start, this.goal));
while (!this.openSet.isEmpty())
{
// Current is the node in openset having the lowest f_score[] value
Vector3 currentNode = null;
double lowestFScore = 0;
for (Vector3 node : this.openSet)
{
if (currentNode == null || this.fScore.get(node) < lowestFScore)
{
currentNode = node;
lowestFScore = this.fScore.get(node);
}
}
if (currentNode == null)
{
break;
}
if (this.callBackCheck.onSearch(this, currentNode))
{
return false;
}
if (currentNode.equals(this.goal))
{
this.results = reconstructPath(this.navigationMap, goal);
return true;
}
this.openSet.remove(currentNode);
this.closedSet.add(currentNode);
for (Vector3 neighbor : getNeighborNodes(currentNode))
{
double tentativeGScore = this.gScore.get(currentNode) + currentNode.distance(neighbor);
if (this.closedSet.contains(neighbor))
{
if (tentativeGScore >= this.gScore.get(neighbor))
{
continue;
}
}
if (!this.openSet.contains(neighbor) || tentativeGScore < this.gScore.get(neighbor))
{
this.navigationMap.put(neighbor, currentNode);
this.gScore.put(neighbor, tentativeGScore);
this.fScore.put(neighbor, gScore.get(neighbor) + getHeuristicEstimatedCost(neighbor, goal));
this.openSet.add(neighbor);
}
}
}
return false;
}
@Override
public Pathfinder reset()
{
this.openSet = new HashSet<Vector3>();
this.navigationMap = new HashMap<Vector3, Vector3>();
return super.reset();
}
/**
* A recursive function to back track and find the path in which we have analyzed.
*/
public Set<Vector3> reconstructPath(HashMap<Vector3, Vector3> nagivationMap, Vector3 current_node)
{
Set<Vector3> path = new HashSet<Vector3>();
path.add(current_node);
if (nagivationMap.containsKey(current_node))
{
path.addAll(reconstructPath(nagivationMap, nagivationMap.get(current_node)));
return path;
}
else
{
return path;
}
}
/**
* @return An estimated cost between two points.
*/
public double getHeuristicEstimatedCost(Vector3 start, Vector3 goal)
{
return start.distance(goal);
}
/**
* @return A Set of neighboring Vector3 positions.
*/
public Set<Vector3> getNeighborNodes(Vector3 vector)
{
if (this.callBackCheck != null)
{
return this.callBackCheck.getConnectedNodes(this, vector);
}
else
{
Set<Vector3> neighbors = new HashSet<Vector3>();
for (int i = 0; i < 6; i++)
{
neighbors.add(vector.clone().modifyPositionFromSide(ForgeDirection.getOrientation(i)));
}
return neighbors;
}
}
}

View file

@ -6,10 +6,10 @@ import java.util.Set;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.Vector3;
import universalelectricity.core.path.IPathCallBack;
import universalelectricity.core.path.Pathfinder;
import com.builtbroken.minecraft.tilenetwork.INetworkPart; import com.builtbroken.minecraft.tilenetwork.INetworkPart;
import com.builtbroken.minecraft.tilenetwork.IPathCallBack;
import com.builtbroken.minecraft.tilenetwork.Pathfinder;
/** Check if a conductor connects with another. */ /** Check if a conductor connects with another. */
public class NetworkPathFinder extends Pathfinder public class NetworkPathFinder extends Pathfinder

View file

@ -9,10 +9,10 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.Vector3;
import universalelectricity.api.vector.VectorHelper; import universalelectricity.api.vector.VectorHelper;
import universalelectricity.core.path.Pathfinder;
import com.builtbroken.minecraft.tilenetwork.INetworkPart; import com.builtbroken.minecraft.tilenetwork.INetworkPart;
import com.builtbroken.minecraft.tilenetwork.ITileNetwork; import com.builtbroken.minecraft.tilenetwork.ITileNetwork;
import com.builtbroken.minecraft.tilenetwork.Pathfinder;
public class NetworkTileEntities implements ITileNetwork public class NetworkTileEntities implements ITileNetwork
{ {