Rewrote Graph class
This commit is contained in:
parent
31e46db791
commit
fb4592007c
|
@ -0,0 +1,58 @@
|
|||
package edx.electrical.multimeter
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import resonant.lib.collection.EvictingList
|
||||
import resonant.lib.utility.nbt.ISaveObj
|
||||
import resonant.lib.wrapper.NBTWrapper._
|
||||
|
||||
import scala.collection.JavaConversions._
|
||||
|
||||
/**
|
||||
* Graph for the multimeter
|
||||
*
|
||||
* @author Calclavia
|
||||
*/
|
||||
class Graph[V](val name: String, val maxPoints: Int = 0)(implicit n: Numeric[V]) extends ISaveObj
|
||||
{
|
||||
/**
|
||||
* Each point represents a tick.
|
||||
*/
|
||||
protected var points = new EvictingList[V](maxPoints)
|
||||
/**
|
||||
* Queue for the next update to insert into the graph.
|
||||
*/
|
||||
protected var queue: V = default
|
||||
private var peak: V = default
|
||||
|
||||
def getPeak: V = peak
|
||||
|
||||
def head: V = apply(0)
|
||||
|
||||
def apply(x: Int): V = if (points.size > x) points.get(x) else default
|
||||
|
||||
def default: V = n.zero
|
||||
|
||||
def queue(value: V) = queue = value
|
||||
|
||||
def doneQueue = this += queue
|
||||
|
||||
def +=(y: V)
|
||||
{
|
||||
points.add(y)
|
||||
peak = default
|
||||
for (point <- points) if (n.gt(point, n.zero)) peak = y
|
||||
}
|
||||
|
||||
def load(nbt: NBTTagCompound)
|
||||
{
|
||||
points.clear()
|
||||
points.addAll(nbt.getArray("DataPoints").toList)
|
||||
}
|
||||
|
||||
def save(nbt: NBTTagCompound)
|
||||
{
|
||||
nbt.setArray("DataPoints", points.toArray)
|
||||
}
|
||||
|
||||
def getAverage: Double = if (points.size > 0) n.toDouble(points.foldLeft(n.zero)((b, a) => n.plus(b, a))) / points.size else 0
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package edx.electrical.multimeter
|
||||
|
||||
import edx.electrical.multimeter.graph._
|
||||
import net.minecraft.nbt.{NBTTagCompound, NBTTagList}
|
||||
import resonant.api.IUpdate
|
||||
import resonant.lib.grid.core.{Grid, UpdateTicker}
|
||||
|
@ -11,22 +10,21 @@ import resonant.lib.utility.science.UnitDisplay
|
|||
import scala.collection.convert.wrapAll._
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
|
||||
class MultimeterGrid extends Grid[PartMultimeter] with IUpdate
|
||||
class GridMultimeter extends Grid[PartMultimeter] with IUpdate
|
||||
{
|
||||
final val displayInformation = new ArrayBuffer[String]
|
||||
/**
|
||||
* The available graphs to be handled.
|
||||
*/
|
||||
final val graphs = new ArrayBuffer[Graph[_]]
|
||||
final val energyGraph: GraphL = new GraphL("energy", maxData)
|
||||
final val powerGraph: GraphL = new GraphL("power", maxData)
|
||||
final val energyCapacityGraph: GraphL = new GraphL("capacity", 1)
|
||||
final val voltageGraph: GraphL = new GraphL("voltage", maxData)
|
||||
final val torqueGraph: GraphD = new GraphD("torque", maxData)
|
||||
final val angularVelocityGraph: GraphD = new GraphD("speed", maxData)
|
||||
final val fluidGraph: GraphI = new GraphI("fluid", maxData)
|
||||
final val thermalGraph: GraphF = new GraphF("temperature", maxData)
|
||||
final val pressureGraph: GraphI = new GraphI("pressure", maxData)
|
||||
final val energyGraph = new Graph[Double]("energy", maxData)
|
||||
final val powerGraph = new Graph[Double]("power", maxData)
|
||||
final val voltageGraph = new Graph[Double]("voltage", maxData)
|
||||
final val torqueGraph = new Graph[Double]("torque", maxData)
|
||||
final val angularVelocityGraph = new Graph[Double]("speed", maxData)
|
||||
final val fluidGraph = new Graph[Int]("fluid", maxData)
|
||||
final val thermalGraph = new Graph[Int]("temperature", maxData)
|
||||
final val pressureGraph = new Graph[Int]("pressure", maxData)
|
||||
/**
|
||||
* Maximum data points a graph can store.
|
||||
*/
|
|
@ -45,7 +45,7 @@ class PartMultimeter extends PartFace with IRedstonePart with IPacketReceiver wi
|
|||
var isPrimary: Boolean = false
|
||||
private var detectMode = DetectModes.NONE
|
||||
private var doDetect: Boolean = true
|
||||
private var grid: MultimeterGrid = null
|
||||
private var grid: GridMultimeter = null
|
||||
|
||||
override def preRemove()
|
||||
{
|
||||
|
@ -161,11 +161,6 @@ class PartMultimeter extends PartFace with IRedstonePart with IPacketReceiver wi
|
|||
return world.getTileEntity(x + direction.offsetX, y + direction.offsetY, z + direction.offsetZ)
|
||||
}
|
||||
|
||||
def getDirection: ForgeDirection =
|
||||
{
|
||||
return ForgeDirection.getOrientation(this.placementSide.ordinal)
|
||||
}
|
||||
|
||||
override def write(packet: MCDataOutput, id: Int)
|
||||
{
|
||||
super.write(packet, id)
|
||||
|
@ -258,6 +253,21 @@ class PartMultimeter extends PartFace with IRedstonePart with IPacketReceiver wi
|
|||
{
|
||||
}
|
||||
|
||||
def getGrid: GridMultimeter =
|
||||
{
|
||||
if (grid == null)
|
||||
{
|
||||
grid = new GridMultimeter
|
||||
grid.add(this)
|
||||
}
|
||||
return grid
|
||||
}
|
||||
|
||||
def setGrid(network: GridMultimeter)
|
||||
{
|
||||
grid = network
|
||||
}
|
||||
|
||||
override def load(nbt: NBTTagCompound)
|
||||
{
|
||||
super.load(nbt)
|
||||
|
@ -338,6 +348,11 @@ class PartMultimeter extends PartFace with IRedstonePart with IPacketReceiver wi
|
|||
return connections
|
||||
}
|
||||
|
||||
def getDirection: ForgeDirection =
|
||||
{
|
||||
return ForgeDirection.getOrientation(this.placementSide.ordinal)
|
||||
}
|
||||
|
||||
def hasMultimeter(x: Int, y: Int, z: Int): Boolean =
|
||||
{
|
||||
return getMultimeter(x, y, z) != null
|
||||
|
@ -372,21 +387,6 @@ class PartMultimeter extends PartFace with IRedstonePart with IPacketReceiver wi
|
|||
return Cuboid6.full
|
||||
}
|
||||
|
||||
def getGrid: MultimeterGrid =
|
||||
{
|
||||
if (grid == null)
|
||||
{
|
||||
grid = new MultimeterGrid
|
||||
grid.add(this)
|
||||
}
|
||||
return grid
|
||||
}
|
||||
|
||||
def setGrid(network: MultimeterGrid)
|
||||
{
|
||||
grid = network
|
||||
}
|
||||
|
||||
override def toString: String = "[PartMultimeter]" + x + "x " + y + "y " + z + "z " + getSlotMask + "s "
|
||||
|
||||
protected def getItem: ItemStack =
|
||||
|
|
|
@ -103,7 +103,7 @@ object RenderMultimeter extends ISimpleItemRenderer
|
|||
|
||||
for (i <- 0 until part.getGrid.graphs.size)
|
||||
{
|
||||
if (part.getGrid.graphs(i).get != null && !(part.getGrid.graphs(i).get == part.getGrid.graphs(i).getDefault))
|
||||
if (part.getGrid.graphs(i).head != null && !(part.getGrid.graphs(i).head == part.getGrid.graphs(i).getDefault))
|
||||
{
|
||||
information :+= part.getGrid.getDisplay(i)
|
||||
}
|
||||
|
|
|
@ -1,77 +0,0 @@
|
|||
package edx.electrical.multimeter.graph;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import resonant.lib.collection.EvictingList;
|
||||
|
||||
/**
|
||||
* Graph for the multimeter
|
||||
*
|
||||
* @author Calclavia
|
||||
*/
|
||||
public abstract class Graph<V extends Comparable<V>>
|
||||
{
|
||||
public final String name;
|
||||
/**
|
||||
* Each point represents a tick.
|
||||
*/
|
||||
protected final EvictingList<V> points;
|
||||
private final int maxPoints;
|
||||
/**
|
||||
* Queue for the next update to insert into the graph.
|
||||
*/
|
||||
protected V queue = getDefault();
|
||||
private V peak = getDefault();
|
||||
|
||||
public Graph(String name, int maxPoints)
|
||||
{
|
||||
this.name = name;
|
||||
this.maxPoints = maxPoints;
|
||||
points = new EvictingList<V>(maxPoints);
|
||||
}
|
||||
|
||||
public void add(V y)
|
||||
{
|
||||
points.add(y);
|
||||
|
||||
peak = getDefault();
|
||||
|
||||
for (V point : points)
|
||||
if (point.compareTo(peak) > 0)
|
||||
peak = y;
|
||||
}
|
||||
|
||||
public V getPeak()
|
||||
{
|
||||
return peak;
|
||||
}
|
||||
|
||||
public V get(int x)
|
||||
{
|
||||
return points.size() > x ? points.get(x) : getDefault();
|
||||
}
|
||||
|
||||
public V get()
|
||||
{
|
||||
return get(0);
|
||||
}
|
||||
|
||||
public abstract void queue(V value);
|
||||
|
||||
public void doneQueue()
|
||||
{
|
||||
add(queue);
|
||||
}
|
||||
|
||||
public abstract V getDefault();
|
||||
|
||||
public void load(NBTTagCompound nbt)
|
||||
{
|
||||
points.clear();
|
||||
}
|
||||
|
||||
public abstract NBTTagCompound save();
|
||||
|
||||
public abstract double getDouble();
|
||||
|
||||
public abstract V getAverage();
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
package edx.electrical.multimeter.graph;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
public class GraphD extends Graph<Double>
|
||||
{
|
||||
public GraphD(String name, int maxPoints)
|
||||
{
|
||||
super(name, maxPoints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queue(Double value)
|
||||
{
|
||||
queue += value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doneQueue()
|
||||
{
|
||||
super.doneQueue();
|
||||
queue = 0d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDefault()
|
||||
{
|
||||
return 0d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(NBTTagCompound nbt)
|
||||
{
|
||||
super.load(nbt);
|
||||
|
||||
NBTTagList nbtList = nbt.getTagList("DataPoints", 0);
|
||||
|
||||
for (int i = 0; i < nbtList.tagCount(); ++i)
|
||||
{
|
||||
NBTTagCompound nbtPoint = (NBTTagCompound) nbtList.getCompoundTagAt(i);
|
||||
points.add(nbtPoint.getDouble("data"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound save()
|
||||
{
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
NBTTagList data = new NBTTagList();
|
||||
|
||||
for (Double value : points)
|
||||
{
|
||||
NBTTagCompound nbtPoint = new NBTTagCompound();
|
||||
nbtPoint.setDouble("data", value);
|
||||
data.appendTag(nbtPoint);
|
||||
}
|
||||
|
||||
nbt.setTag("DataPoints", data);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble()
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getAverage()
|
||||
{
|
||||
if (points.size() > 0)
|
||||
{
|
||||
double average = 0;
|
||||
|
||||
for (double point : points)
|
||||
{
|
||||
average += point;
|
||||
}
|
||||
|
||||
average /= points.size();
|
||||
|
||||
return average;
|
||||
}
|
||||
|
||||
return 0d;
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
package edx.electrical.multimeter.graph;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
public class GraphF extends Graph<Float>
|
||||
{
|
||||
public GraphF(String name, int maxPoints)
|
||||
{
|
||||
super(name, maxPoints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queue(Float value)
|
||||
{
|
||||
queue += value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doneQueue()
|
||||
{
|
||||
super.doneQueue();
|
||||
queue = 0f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getDefault()
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(NBTTagCompound nbt)
|
||||
{
|
||||
super.load(nbt);
|
||||
|
||||
NBTTagList nbtList = nbt.getTagList("DataPoints", 0);
|
||||
|
||||
for (int i = 0; i < nbtList.tagCount(); ++i)
|
||||
{
|
||||
NBTTagCompound nbtPoint = (NBTTagCompound) nbtList.getCompoundTagAt(i);
|
||||
points.add(nbtPoint.getFloat("data"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound save()
|
||||
{
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
NBTTagList data = new NBTTagList();
|
||||
|
||||
for (Float value : points)
|
||||
{
|
||||
NBTTagCompound nbtPoint = new NBTTagCompound();
|
||||
nbtPoint.setFloat("data", value);
|
||||
data.appendTag(nbtPoint);
|
||||
}
|
||||
|
||||
nbt.setTag("DataPoints", data);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble()
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getAverage()
|
||||
{
|
||||
if (points.size() > 0)
|
||||
{
|
||||
float average = 0;
|
||||
|
||||
for (float point : points)
|
||||
{
|
||||
average += point;
|
||||
}
|
||||
|
||||
average /= points.size();
|
||||
|
||||
return average;
|
||||
}
|
||||
|
||||
return 0f;
|
||||
}
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
package edx.electrical.multimeter.graph;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
public class GraphI extends Graph<Integer>
|
||||
{
|
||||
public GraphI(String name, int maxPoints)
|
||||
{
|
||||
super(name, maxPoints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queue(Integer value)
|
||||
{
|
||||
queue += value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doneQueue()
|
||||
{
|
||||
super.doneQueue();
|
||||
queue = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getDefault()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(NBTTagCompound nbt)
|
||||
{
|
||||
super.load(nbt);
|
||||
NBTTagList nbtList = nbt.getTagList("DataPoints", 0);
|
||||
|
||||
for (int i = 0; i < nbtList.tagCount(); ++i)
|
||||
{
|
||||
NBTTagCompound nbtPoint = (NBTTagCompound) nbtList.getCompoundTagAt(i);
|
||||
points.add(nbtPoint.getInteger("data"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound save()
|
||||
{
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
NBTTagList data = new NBTTagList();
|
||||
|
||||
for (Integer value : points)
|
||||
{
|
||||
NBTTagCompound nbtPoint = new NBTTagCompound();
|
||||
nbtPoint.setInteger("data", value);
|
||||
data.appendTag(nbtPoint);
|
||||
}
|
||||
|
||||
nbt.setTag("DataPoints", data);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble()
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getAverage()
|
||||
{
|
||||
if (points.size() > 0)
|
||||
{
|
||||
int average = 0;
|
||||
|
||||
for (int point : points)
|
||||
{
|
||||
average += point;
|
||||
}
|
||||
|
||||
average /= points.size();
|
||||
|
||||
return average;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
package edx.electrical.multimeter.graph;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
public class GraphL extends Graph<Double>
|
||||
{
|
||||
public GraphL(String name, int maxPoints)
|
||||
{
|
||||
super(name, maxPoints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queue(Double value)
|
||||
{
|
||||
queue += value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doneQueue()
|
||||
{
|
||||
super.doneQueue();
|
||||
queue = 0.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDefault()
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(NBTTagCompound nbt)
|
||||
{
|
||||
super.load(nbt);
|
||||
NBTTagList nbtList = nbt.getTagList("DataPoints", 0);
|
||||
|
||||
for (int i = 0; i < nbtList.tagCount(); ++i)
|
||||
{
|
||||
NBTTagCompound nbtPoint = (NBTTagCompound) nbtList.getCompoundTagAt(i);
|
||||
points.add(nbtPoint.getDouble("data"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound save()
|
||||
{
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
NBTTagList data = new NBTTagList();
|
||||
|
||||
for (Double value : points)
|
||||
{
|
||||
NBTTagCompound nbtPoint = new NBTTagCompound();
|
||||
nbtPoint.setDouble("data", value);
|
||||
data.appendTag(nbtPoint);
|
||||
}
|
||||
|
||||
nbt.setTag("DataPoints", data);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble()
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getAverage()
|
||||
{
|
||||
if (points.size() > 0)
|
||||
{
|
||||
double average = 0;
|
||||
|
||||
for (Double point : points)
|
||||
{
|
||||
average += point;
|
||||
}
|
||||
|
||||
average /= points.size();
|
||||
|
||||
return average;
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
}
|
|
@ -12,7 +12,7 @@ import net.minecraftforge.client.model.AdvancedModelLoader
|
|||
import net.minecraftforge.common.util.ForgeDirection
|
||||
import org.lwjgl.opengl.GL11._
|
||||
import resonant.api.recipe.{MachineRecipes, RecipeType}
|
||||
import resonant.lib.collection.Timer
|
||||
import resonant.lib.prefab.Timer
|
||||
import resonant.lib.prefab.damage.CustomDamageSource
|
||||
import resonant.lib.render.RenderUtility
|
||||
import resonant.lib.transform.region.Cuboid
|
||||
|
@ -137,11 +137,6 @@ class TileGrindingWheel extends TileMechanical(Material.rock)
|
|||
return results.length > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Can this machine work this tick?
|
||||
*/
|
||||
def canWork: Boolean = counter >= requiredTorque
|
||||
|
||||
override def collide(entity: Entity)
|
||||
{
|
||||
if (entity.isInstanceOf[EntityItem])
|
||||
|
@ -185,6 +180,11 @@ class TileGrindingWheel extends TileMechanical(Material.rock)
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Can this machine work this tick?
|
||||
*/
|
||||
def canWork: Boolean = counter >= requiredTorque
|
||||
|
||||
def canGrind(itemStack: ItemStack): Boolean = MachineRecipes.instance.getOutput(RecipeType.SIFTER.name, itemStack).length > 0
|
||||
|
||||
override def renderDynamic(pos: Vector3, frame: Float, pass: Int): Unit =
|
||||
|
|
|
@ -16,7 +16,7 @@ import net.minecraftforge.fluids.IFluidBlock
|
|||
import org.lwjgl.opengl.GL11
|
||||
import org.lwjgl.opengl.GL11._
|
||||
import resonant.api.recipe.{MachineRecipes, RecipeType}
|
||||
import resonant.lib.collection.Timer
|
||||
import resonant.lib.prefab.Timer
|
||||
import resonant.lib.render.RenderUtility
|
||||
import resonant.lib.transform.rotation.Quaternion
|
||||
import resonant.lib.transform.vector.Vector3
|
||||
|
|
Loading…
Reference in New Issue