Merge branch 'optimizations' of https://github.com/Anuken/Mindustry
This commit is contained in:
@@ -17,12 +17,12 @@ public class StorageBlocks extends BlockList implements ContentList{
|
||||
|
||||
vault = new Vault("vault"){{
|
||||
size = 3;
|
||||
itemCapacity = 2000;
|
||||
itemCapacity = 1000;
|
||||
}};
|
||||
|
||||
container = new Vault("container"){{
|
||||
size = 2;
|
||||
itemCapacity = 500;
|
||||
itemCapacity = 250;
|
||||
}};
|
||||
|
||||
unloader = new SortedUnloader("unloader"){{
|
||||
|
||||
@@ -322,9 +322,9 @@ public class BlockFx extends FxList implements ContentList{
|
||||
});
|
||||
|
||||
shieldBreak = new Effect(40, e -> {
|
||||
Draw.color(Color.WHITE);
|
||||
Draw.color(Palette.accent);
|
||||
Lines.stroke(3f * e.fout());
|
||||
Lines.poly(e.x, e.y, 6, e.rotation, 90);
|
||||
Lines.poly(e.x, e.y, 6, e.rotation + e.fin(), 90);
|
||||
Draw.reset();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import io.anuke.mindustry.net.Packets.*;
|
||||
import io.anuke.mindustry.net.TraceInfo;
|
||||
import io.anuke.mindustry.net.ValidateException;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.modules.InventoryModule;
|
||||
import io.anuke.mindustry.world.modules.ItemModule;
|
||||
import io.anuke.ucore.core.Core;
|
||||
import io.anuke.ucore.core.Settings;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
@@ -296,7 +296,7 @@ public class NetClient extends Module{
|
||||
if(tile != null && tile.entity != null){
|
||||
tile.entity.items.read(input);
|
||||
}else{
|
||||
new InventoryModule().read(input);
|
||||
new ItemModule().read(input);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.blocks.defense.Wall;
|
||||
import io.anuke.mindustry.world.consumers.Consume;
|
||||
import io.anuke.mindustry.world.modules.ConsumeModule;
|
||||
import io.anuke.mindustry.world.modules.InventoryModule;
|
||||
import io.anuke.mindustry.world.modules.ItemModule;
|
||||
import io.anuke.mindustry.world.modules.LiquidModule;
|
||||
import io.anuke.mindustry.world.modules.PowerModule;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
@@ -47,7 +47,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
|
||||
public float timeScale = 1f, timeScaleDuration;
|
||||
|
||||
public PowerModule power;
|
||||
public InventoryModule items;
|
||||
public ItemModule items;
|
||||
public LiquidModule liquids;
|
||||
public ConsumeModule cons;
|
||||
|
||||
@@ -157,9 +157,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
|
||||
}
|
||||
|
||||
public void removeFromProximity(){
|
||||
if(power != null){
|
||||
tile.block().powerGraphRemoved(tile);
|
||||
}
|
||||
tile.block().onProximityRemoved(tile);
|
||||
|
||||
GridPoint2[] nearby = Edges.getEdges(tile.block().size);
|
||||
for(GridPoint2 point : nearby){
|
||||
@@ -202,7 +200,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
|
||||
proximity.add(tile);
|
||||
}
|
||||
|
||||
if(tile.block().hasPower) tile.block().updatePowerGraph(tile);
|
||||
tile.block().onProximityAdded(tile);
|
||||
tile.block().onProximityUpdate(tile);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,12 +96,6 @@ public class FogRenderer implements Disposable{
|
||||
}));
|
||||
}
|
||||
|
||||
public void setLoadingOffset(int x, int y){
|
||||
isOffseted = true;
|
||||
offsettedX = x;
|
||||
offsettedY = y;
|
||||
}
|
||||
|
||||
public void writeFog(){
|
||||
if(buffer == null) return;
|
||||
|
||||
|
||||
@@ -139,7 +139,17 @@ public class Block extends BaseBlock {
|
||||
return drops != null && drops.item == item;
|
||||
}
|
||||
|
||||
public void updatePowerGraph(Tile tile){
|
||||
public void onProximityRemoved(Tile tile){
|
||||
if(tile.entity.power != null){
|
||||
tile.block().powerGraphRemoved(tile);
|
||||
}
|
||||
}
|
||||
|
||||
public void onProximityAdded(Tile tile){
|
||||
if(tile.block().hasPower) tile.block().updatePowerGraph(tile);
|
||||
}
|
||||
|
||||
protected void updatePowerGraph(Tile tile){
|
||||
TileEntity entity = tile.entity();
|
||||
|
||||
for(Tile other : getPowerConnections(tile, tempTiles)){
|
||||
@@ -149,7 +159,7 @@ public class Block extends BaseBlock {
|
||||
}
|
||||
}
|
||||
|
||||
public void powerGraphRemoved(Tile tile){
|
||||
protected void powerGraphRemoved(Tile tile){
|
||||
tile.entity.power.graph.remove(tile);
|
||||
for(int i = 0; i < tile.entity.power.links.size; i++){
|
||||
Tile other = world.tile(tile.entity.power.links.get(i));
|
||||
|
||||
@@ -10,7 +10,7 @@ import io.anuke.mindustry.game.Team;
|
||||
import io.anuke.mindustry.world.blocks.BlockPart;
|
||||
import io.anuke.mindustry.world.blocks.Floor;
|
||||
import io.anuke.mindustry.world.modules.ConsumeModule;
|
||||
import io.anuke.mindustry.world.modules.InventoryModule;
|
||||
import io.anuke.mindustry.world.modules.ItemModule;
|
||||
import io.anuke.mindustry.world.modules.LiquidModule;
|
||||
import io.anuke.mindustry.world.modules.PowerModule;
|
||||
import io.anuke.ucore.entities.trait.PosTrait;
|
||||
@@ -423,7 +423,7 @@ public class Tile implements PosTrait, TargetTrait{
|
||||
if(block.hasEntity()){
|
||||
entity = block.newEntity().init(this, block.update);
|
||||
entity.cons = new ConsumeModule();
|
||||
if(block.hasItems) entity.items = new InventoryModule();
|
||||
if(block.hasItems) entity.items = new ItemModule();
|
||||
if(block.hasLiquids) entity.liquids = new LiquidModule();
|
||||
if(block.hasPower){
|
||||
entity.power = new PowerModule();
|
||||
|
||||
@@ -22,7 +22,7 @@ import io.anuke.mindustry.world.BarType;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.meta.BlockBar;
|
||||
import io.anuke.mindustry.world.modules.InventoryModule;
|
||||
import io.anuke.mindustry.world.modules.ItemModule;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
import io.anuke.ucore.core.Graphics;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
@@ -246,7 +246,7 @@ public class BuildBlock extends Block{
|
||||
}
|
||||
}
|
||||
|
||||
private float checkRequired(InventoryModule inventory, float amount, boolean remove){
|
||||
private float checkRequired(ItemModule inventory, float amount, boolean remove){
|
||||
float maxProgress = amount;
|
||||
|
||||
for(int i = 0; i < recipe.requirements.length; i++){
|
||||
|
||||
@@ -7,9 +7,11 @@ import io.anuke.mindustry.content.fx.BulletFx;
|
||||
import io.anuke.mindustry.entities.TileEntity;
|
||||
import io.anuke.mindustry.entities.traits.AbsorbTrait;
|
||||
import io.anuke.mindustry.graphics.Palette;
|
||||
import io.anuke.mindustry.world.BarType;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.consumers.ConsumeLiquidFilter;
|
||||
import io.anuke.mindustry.world.meta.BlockBar;
|
||||
import io.anuke.mindustry.world.meta.BlockStat;
|
||||
import io.anuke.mindustry.world.meta.StatUnit;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
@@ -68,6 +70,13 @@ public class ForceProjector extends Block {
|
||||
stats.add(BlockStat.powerDamage, powerDamage, StatUnit.powerUnits);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBars(){
|
||||
super.setBars();
|
||||
|
||||
bars.add(new BlockBar(BarType.heat, true, tile -> tile.<ForceEntity>entity().buildup / breakage));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Tile tile){
|
||||
ForceEntity entity = tile.entity();
|
||||
@@ -91,7 +100,7 @@ public class ForceProjector extends Block {
|
||||
}
|
||||
|
||||
if(!entity.cons.valid() && !cheat){
|
||||
entity.warmup = Mathf.lerpDelta(entity.warmup, 0f, 0.1f);
|
||||
entity.warmup = Mathf.lerpDelta(entity.warmup, 0f, 0.15f);
|
||||
if(entity.warmup <= 0.09f){
|
||||
entity.broken = true;
|
||||
}
|
||||
@@ -136,6 +145,10 @@ public class ForceProjector extends Block {
|
||||
float hit = trait.getShieldDamage()*powerDamage;
|
||||
entity.hit = 1f;
|
||||
entity.power.amount -= Math.min(hit, entity.power.amount);
|
||||
|
||||
if(entity.power.amount <= 0.0001f){
|
||||
entity.buildup += trait.getShieldDamage() * entity.warmup*2f;
|
||||
}
|
||||
entity.buildup += trait.getShieldDamage() * entity.warmup;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -45,6 +45,7 @@ public class PhaseWeaver extends PowerSmelter{
|
||||
Shaders.build.region = weaveRegion;
|
||||
Shaders.build.progress = progress;
|
||||
Shaders.build.color.set(Palette.accent);
|
||||
Shaders.build.color.a = entity.heat;
|
||||
Shaders.build.time = -entity.time / 10f;
|
||||
|
||||
Graphics.shader(Shaders.build, false);
|
||||
@@ -53,6 +54,7 @@ public class PhaseWeaver extends PowerSmelter{
|
||||
Graphics.shader();
|
||||
|
||||
Draw.color(Palette.accent);
|
||||
Draw.alpha(entity.heat);
|
||||
|
||||
Lines.lineAngleCenter(
|
||||
tile.drawx() + Mathf.sin(entity.time, 6f, Vars.tilesize / 3f * size),
|
||||
|
||||
@@ -19,7 +19,6 @@ import io.anuke.mindustry.graphics.Shaders;
|
||||
import io.anuke.mindustry.maps.TutorialSector;
|
||||
import io.anuke.mindustry.net.Net;
|
||||
import io.anuke.mindustry.type.Item;
|
||||
import io.anuke.mindustry.type.ItemType;
|
||||
import io.anuke.mindustry.world.BarType;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.meta.BlockFlag;
|
||||
@@ -163,25 +162,6 @@ public class CoreBlock extends StorageBlock{
|
||||
return entity.solid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int acceptStack(Item item, int amount, Tile tile, Unit source){
|
||||
if(acceptItem(item, tile, tile) && hasItems && (source == null || source.getTeam() == tile.getTeam())){
|
||||
return Math.min(itemCapacity - tile.entity.items.get(item), amount);
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaximumAccepted(Tile tile, Item item){
|
||||
return itemCapacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptItem(Item item, Tile tile, Tile source){
|
||||
return tile.entity.items.get(item) < itemCapacity && item.type == ItemType.material;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleItem(Item item, Tile tile, Tile source){
|
||||
if(Net.server() || !Net.active()) super.handleItem(item, tile, source);
|
||||
@@ -242,7 +222,7 @@ public class CoreBlock extends StorageBlock{
|
||||
return new CoreEntity();
|
||||
}
|
||||
|
||||
public class CoreEntity extends TileEntity implements SpawnerTrait{
|
||||
public class CoreEntity extends StorageEntity implements SpawnerTrait{
|
||||
public Unit currentUnit;
|
||||
int droneID = -1;
|
||||
boolean solid = true;
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
package io.anuke.mindustry.world.blocks.storage;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import io.anuke.mindustry.entities.TileEntity;
|
||||
import io.anuke.mindustry.entities.Unit;
|
||||
import io.anuke.mindustry.graphics.Palette;
|
||||
import io.anuke.mindustry.graphics.Shaders;
|
||||
import io.anuke.mindustry.type.Item;
|
||||
import io.anuke.mindustry.world.BarType;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.core.Graphics;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
import io.anuke.ucore.graphics.Fill;
|
||||
|
||||
import static io.anuke.mindustry.Vars.tilesize;
|
||||
|
||||
public abstract class StorageBlock extends Block{
|
||||
|
||||
@@ -12,11 +23,90 @@ public abstract class StorageBlock extends Block{
|
||||
hasItems = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBars(){
|
||||
super.setBars();
|
||||
bars.remove(BarType.inventory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean outputsItems(){
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProximityAdded(Tile tile){
|
||||
StorageEntity entity = tile.entity();
|
||||
entity.graph.set(tile);
|
||||
|
||||
for(Tile prox : tile.entity.proximity()){
|
||||
if(prox.block() instanceof StorageBlock){
|
||||
StorageEntity other = prox.entity();
|
||||
entity.graph.merge(other.graph);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProximityRemoved(Tile tile){
|
||||
StorageEntity entity = tile.entity();
|
||||
entity.graph.remove(tile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawSelect(Tile tile){
|
||||
|
||||
StorageEntity entity = tile.entity();
|
||||
|
||||
if(entity.graph.getTiles().size > 1){
|
||||
|
||||
Shaders.outline.color.set(Palette.accent);
|
||||
Graphics.beginShaders(Shaders.outline);
|
||||
|
||||
for(Tile other : entity.graph.getTiles()){
|
||||
Fill.square(other.drawx(), other.drawy(), other.block().size * tilesize);
|
||||
}
|
||||
|
||||
Draw.color(Color.CLEAR);
|
||||
Graphics.endShaders();
|
||||
Draw.color();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptItem(Item item, Tile tile, Tile source){
|
||||
StorageEntity entity = tile.entity();
|
||||
return entity.graph.accept(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int acceptStack(Item item, int amount, Tile tile, Unit source){
|
||||
StorageEntity entity = tile.entity();
|
||||
if(acceptItem(item, tile, tile) && hasItems && (source == null || source.getTeam() == tile.getTeam())){
|
||||
return Math.min(entity.graph.accept(item, amount), amount);
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity newEntity(){
|
||||
return new StorageEntity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array<Object> getDebugInfo(Tile tile){
|
||||
Array<Object> arr = super.getDebugInfo(tile);
|
||||
|
||||
StorageEntity entity = tile.entity();
|
||||
arr.addAll("storage graph", entity.graph.getID(),
|
||||
"graph capacity", entity.graph.getCapacity(),
|
||||
"graph tiles", entity.graph.getTiles().size,
|
||||
"graph item ID", entity.graph.items().getID());
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an item and returns it. If item is not null, it should return the item.
|
||||
* Returns null if no items are there.
|
||||
@@ -48,4 +138,8 @@ public abstract class StorageBlock extends Block{
|
||||
return entity.items.has(item);
|
||||
}
|
||||
}
|
||||
|
||||
public class StorageEntity extends TileEntity{
|
||||
public StorageGraph graph = new StorageGraph();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package io.anuke.mindustry.world.blocks.storage;
|
||||
|
||||
import com.badlogic.gdx.utils.IntSet;
|
||||
import com.badlogic.gdx.utils.ObjectSet;
|
||||
import com.badlogic.gdx.utils.Queue;
|
||||
import io.anuke.mindustry.type.Item;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.blocks.storage.StorageBlock.StorageEntity;
|
||||
import io.anuke.mindustry.world.modules.ItemModule;
|
||||
|
||||
public class StorageGraph{
|
||||
private static IntSet closedSet = new IntSet();
|
||||
private static Queue<Tile> queue = new Queue<>();
|
||||
private static ObjectSet<ItemModule> itemSet = new ObjectSet<>();
|
||||
private static int lastID;
|
||||
|
||||
private final int id = lastID++;
|
||||
private ObjectSet<Tile> tiles = new ObjectSet<>();
|
||||
private ItemModule items = new ItemModule();
|
||||
private int capacity;
|
||||
|
||||
public void set(Tile tile){
|
||||
items.addAll(tile.entity.items);
|
||||
items.setID(tile.entity.items.getID());
|
||||
|
||||
add(tile);
|
||||
}
|
||||
|
||||
public void add(Tile tile){
|
||||
|
||||
if(!tiles.add(tile)) return;
|
||||
|
||||
StorageEntity e = tile.entity();
|
||||
e.graph = this;
|
||||
|
||||
capacity += tile.block().itemCapacity;
|
||||
|
||||
if(tile.entity.items != null && tile.entity.items.getID() != items.getID()){
|
||||
items.addAll(tile.entity.items);
|
||||
}
|
||||
|
||||
tile.entity.items = items;
|
||||
}
|
||||
|
||||
public void remove(Tile tile){
|
||||
if(!tiles.contains(tile)) return;
|
||||
|
||||
for(Tile other : tiles){
|
||||
if(other == tile) continue;
|
||||
|
||||
StorageEntity entity = other.entity();
|
||||
entity.graph = null;
|
||||
entity.items = new ItemModule();
|
||||
|
||||
float fraction = (float)other.block().itemCapacity / capacity;
|
||||
items.forEach((item, amount) -> {
|
||||
int added = (int)(fraction * amount);
|
||||
entity.items.add(item, added);
|
||||
items.remove(item, added);
|
||||
});
|
||||
}
|
||||
|
||||
//handle remaining items that didn't get added
|
||||
Item taken;
|
||||
while((taken = items.take()) != null){
|
||||
for(Tile other : tiles){
|
||||
if(other == tile) continue;
|
||||
|
||||
//insert item into first found block
|
||||
if(other.entity.items.get(taken) < other.block().itemCapacity){
|
||||
other.entity.items.add(taken, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
items.clear();
|
||||
capacity = 0;
|
||||
|
||||
for(Tile other : tile.entity.proximity()){
|
||||
if(other.block() instanceof StorageBlock && other.<StorageEntity>entity().graph == null){
|
||||
StorageGraph graph = new StorageGraph();
|
||||
other.<StorageEntity>entity().graph = graph;
|
||||
graph.reflow(tile, other);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void reflow(Tile base, Tile tile){
|
||||
queue.clear();
|
||||
queue.addLast(tile);
|
||||
closedSet.clear();
|
||||
itemSet.clear();
|
||||
|
||||
while(queue.size > 0){
|
||||
Tile child = queue.removeFirst();
|
||||
StorageEntity entity = child.entity();
|
||||
entity.graph = this;
|
||||
|
||||
if(!itemSet.add(child.entity.items)) child.entity.items = null;
|
||||
add(child);
|
||||
|
||||
for(Tile next : child.entity.proximity()){
|
||||
if(next != base && next.block() instanceof StorageBlock && next.<StorageEntity>entity().graph == null && !closedSet.contains(next.packedPosition())){
|
||||
queue.addLast(next);
|
||||
closedSet.add(next.packedPosition());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void merge(StorageGraph other){
|
||||
if(this == other || other == null) return;
|
||||
|
||||
itemSet.clear();
|
||||
for(Tile tile : other.tiles){
|
||||
if(!itemSet.add(tile.entity.items)){
|
||||
tile.entity.items = null;
|
||||
}
|
||||
}
|
||||
|
||||
for(Tile tile : other.tiles){
|
||||
add(tile);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean accept(Item item){
|
||||
return accept(item, 1) == 1;
|
||||
}
|
||||
|
||||
public int accept(Item item, int amount){
|
||||
return Math.min(capacity - items.get(item), amount);
|
||||
}
|
||||
|
||||
public ObjectSet<Tile> getTiles(){
|
||||
return tiles;
|
||||
}
|
||||
|
||||
public int getID(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getCapacity(){
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public ItemModule items(){
|
||||
return items;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +1,13 @@
|
||||
package io.anuke.mindustry.world.blocks.storage;
|
||||
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import io.anuke.mindustry.entities.TileEntity;
|
||||
import io.anuke.mindustry.type.Item;
|
||||
import io.anuke.mindustry.world.Edges;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class Vault extends StorageBlock{
|
||||
|
||||
public Vault(String name){
|
||||
super(name);
|
||||
solid = true;
|
||||
update = true;
|
||||
update = false;
|
||||
destructible = true;
|
||||
itemCapacity = 1000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Tile tile){
|
||||
int iterations = Math.max(1, (int) (Timers.delta() + 0.4f));
|
||||
|
||||
for(int i = 0; i < iterations; i++){
|
||||
if(tile.entity.items.total() > 0){
|
||||
tryDump(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptItem(Item item, Tile tile, Tile source){
|
||||
return tile.entity.items.total() < itemCapacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryDump(Tile tile, Item todump){
|
||||
TileEntity entity = tile.entity;
|
||||
if(entity == null || !hasItems || tile.entity.items.total() == 0 || (todump != null && !entity.items.has(todump)))
|
||||
return false;
|
||||
|
||||
Array<Tile> proximity = entity.proximity();
|
||||
int dump = tile.getDump();
|
||||
|
||||
if(proximity.size == 0) return false;
|
||||
|
||||
for(int i = 0; i < proximity.size; i++){
|
||||
Tile other = proximity.get((i + dump) % proximity.size);
|
||||
Tile in = Edges.getFacingEdge(tile, other);
|
||||
|
||||
if(other == null || !(other.block() instanceof StorageBlock)) continue;
|
||||
|
||||
if(!(other.block() instanceof Vault)){
|
||||
|
||||
for(int ii = 0; ii < content.items().size; ii++){
|
||||
Item item = content.item(ii);
|
||||
|
||||
if(entity.items.has(item) && other.block().acceptItem(item, other, in) && canDump(tile, other, item)){
|
||||
other.block().handleItem(item, other, in);
|
||||
tile.entity.items.remove(item, 1);
|
||||
incrementDump(tile, proximity.size);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
todump = content.item(0);
|
||||
|
||||
if(other.block().acceptItem(todump, other, in) && canDump(tile, other, todump)){
|
||||
other.block().handleItem(removeItem(tile, null), other, in);
|
||||
incrementDump(tile, proximity.size);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
incrementDump(tile, proximity.size);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDump(Tile tile, Tile to, Item item){
|
||||
return !(to.block() instanceof Vault) || (float) to.entity.items.total() / to.block().itemCapacity < (float) tile.entity.items.total() / itemCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import io.anuke.mindustry.world.meta.BlockBar;
|
||||
import io.anuke.mindustry.world.meta.BlockFlag;
|
||||
import io.anuke.mindustry.world.meta.BlockStat;
|
||||
import io.anuke.mindustry.world.meta.StatUnit;
|
||||
import io.anuke.mindustry.world.modules.InventoryModule;
|
||||
import io.anuke.mindustry.world.modules.ItemModule;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
import io.anuke.ucore.core.Graphics;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
@@ -211,7 +211,7 @@ public class UnitFactory extends Block{
|
||||
return new UnitFactoryEntity();
|
||||
}
|
||||
|
||||
protected boolean hasRequirements(InventoryModule inv, float fraction){
|
||||
protected boolean hasRequirements(ItemModule inv, float fraction){
|
||||
for(ItemStack stack : consumes.items()){
|
||||
if(!inv.has(stack.item, (int) (fraction * stack.amount))){
|
||||
return false;
|
||||
|
||||
@@ -7,11 +7,15 @@ import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static io.anuke.mindustry.Vars.content;
|
||||
|
||||
public class InventoryModule extends BlockModule{
|
||||
public class ItemModule extends BlockModule{
|
||||
private static int lastID;
|
||||
|
||||
private int[] items = new int[content.items().size];
|
||||
private int total;
|
||||
private int id = lastID ++;
|
||||
|
||||
public void forEach(ItemConsumer cons){
|
||||
for(int i = 0; i < items.length; i++){
|
||||
@@ -85,6 +89,13 @@ public class InventoryModule extends BlockModule{
|
||||
total += amount;
|
||||
}
|
||||
|
||||
public void addAll(ItemModule items){
|
||||
for(int i = 0; i < items.items.length; i++){
|
||||
this.items[i] += items.items[i];
|
||||
total += items.items[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Item item, int amount){
|
||||
amount = Math.min(amount, items[item.id]);
|
||||
|
||||
@@ -103,6 +114,8 @@ public class InventoryModule extends BlockModule{
|
||||
|
||||
@Override
|
||||
public void write(DataOutput stream) throws IOException{
|
||||
stream.writeInt(id); //unique ID
|
||||
|
||||
byte amount = 0;
|
||||
for(int item : items){
|
||||
if(item > 0) amount++;
|
||||
@@ -120,6 +133,7 @@ public class InventoryModule extends BlockModule{
|
||||
|
||||
@Override
|
||||
public void read(DataInput stream) throws IOException{
|
||||
id = stream.readInt();
|
||||
byte count = stream.readByte();
|
||||
total = 0;
|
||||
|
||||
@@ -131,6 +145,14 @@ public class InventoryModule extends BlockModule{
|
||||
}
|
||||
}
|
||||
|
||||
public int getID(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setID(int id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public interface ItemConsumer{
|
||||
void accept(Item item, float amount);
|
||||
}
|
||||
Reference in New Issue
Block a user