Experimental flow display
This commit is contained in:
@@ -1,21 +1,54 @@
|
||||
package mindustry.world.modules;
|
||||
|
||||
import arc.math.*;
|
||||
import arc.struct.*;
|
||||
import arc.util.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
import arc.util.io.*;
|
||||
import mindustry.type.Item;
|
||||
import mindustry.type.ItemStack;
|
||||
import mindustry.type.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.*;
|
||||
|
||||
import static mindustry.Vars.content;
|
||||
|
||||
public class ItemModule extends BlockModule{
|
||||
private int[] items = new int[content.items().size];
|
||||
private int total;
|
||||
private static final int windowSize = 10;
|
||||
|
||||
protected int[] items = new int[content.items().size];
|
||||
protected int total;
|
||||
// Make the take() loop persistent so it does not return the same item twice in a row unless there is nothing else to return.
|
||||
protected int takeRotation;
|
||||
|
||||
public void forEach(ItemConsumer cons){
|
||||
private @Nullable WindowedMean[] flow;
|
||||
private @Nullable Bits flownIds;
|
||||
|
||||
public void update(boolean showFlow){
|
||||
if(showFlow){
|
||||
if(flow == null){
|
||||
flow = new WindowedMean[items.length];
|
||||
flownIds = new Bits(items.length);
|
||||
}
|
||||
}else{
|
||||
flow = null;
|
||||
flownIds = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** @return a specific item's flow rate in items/s; any value < 0 means not ready.*/
|
||||
public float getFlowRate(Item item){
|
||||
if(flow == null || flow[item.id] == null || flow[item.id].getValueCount() <= 2){
|
||||
return - 1f;
|
||||
}
|
||||
|
||||
//TODO this isn't very accurate
|
||||
return 60f / ((flow[item.id].getLatest() - flow[item.id].getOldest()) / (flow[item.id].getValueCount() - 1));
|
||||
}
|
||||
|
||||
public @Nullable Bits flownBits(){
|
||||
return flownIds;
|
||||
}
|
||||
|
||||
public void each(ItemConsumer cons){
|
||||
for(int i = 0; i < items.length; i++){
|
||||
if(items[i] > 0){
|
||||
cons.accept(content.item(i), items[i]);
|
||||
@@ -108,6 +141,11 @@ public class ItemModule extends BlockModule{
|
||||
public void add(Item item, int amount){
|
||||
items[item.id] += amount;
|
||||
total += amount;
|
||||
if(flow != null){
|
||||
if(flow[item.id] == null) flow[item.id] = new WindowedMean(windowSize);
|
||||
flow[item.id].addValue(Time.time());
|
||||
flownIds.set(item.id);
|
||||
}
|
||||
}
|
||||
|
||||
public void addAll(ItemModule items){
|
||||
|
||||
@@ -1,21 +1,45 @@
|
||||
package mindustry.world.modules;
|
||||
|
||||
import arc.math.*;
|
||||
import arc.util.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
import arc.util.io.*;
|
||||
import mindustry.type.Liquid;
|
||||
import mindustry.type.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.*;
|
||||
|
||||
import static mindustry.Vars.content;
|
||||
|
||||
public class LiquidModule extends BlockModule{
|
||||
private static final int windowSize = 60, updateInterval = 60;
|
||||
private static Interval flowTimer = new Interval(1);
|
||||
|
||||
private float[] liquids = new float[content.liquids().size];
|
||||
private float total;
|
||||
private Liquid current = content.liquid(0);
|
||||
private float smoothLiquid;
|
||||
|
||||
public void update(){
|
||||
private @Nullable WindowedMean flow;
|
||||
private float lastAdded, currentFlowRate;
|
||||
|
||||
public void update(boolean showFlow){
|
||||
smoothLiquid = Mathf.lerpDelta(smoothLiquid, currentAmount(), 0.1f);
|
||||
if(showFlow){
|
||||
if(flow == null) flow = new WindowedMean(windowSize);
|
||||
flow.addValue(lastAdded);
|
||||
lastAdded = 0;
|
||||
if(currentFlowRate < 0 || flowTimer.get(updateInterval)){
|
||||
currentFlowRate = flow.hasEnoughData() ? flow.getMean() : -1f;
|
||||
}
|
||||
}else{
|
||||
currentFlowRate = -1f;
|
||||
flow = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** @return current liquid's flow rate in u/s; any value < 0 means 'not ready'. */
|
||||
public float getFlowRate(){
|
||||
return currentFlowRate;
|
||||
}
|
||||
|
||||
public float smoothAmount(){
|
||||
@@ -58,6 +82,10 @@ public class LiquidModule extends BlockModule{
|
||||
liquids[liquid.id] += amount;
|
||||
total += amount;
|
||||
current = liquid;
|
||||
|
||||
if(flow != null){
|
||||
lastAdded += Math.max(amount, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Liquid liquid, float amount){
|
||||
|
||||
Reference in New Issue
Block a user