Merged inventory saving/loading, item-based capacity
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"){{
|
||||
|
||||
@@ -7,6 +7,7 @@ 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;
|
||||
@@ -22,6 +23,12 @@ public abstract class StorageBlock extends Block{
|
||||
hasItems = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBars(){
|
||||
super.setBars();
|
||||
bars.remove(BarType.inventory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean outputsItems(){
|
||||
return false;
|
||||
@@ -30,7 +37,7 @@ public abstract class StorageBlock extends Block{
|
||||
@Override
|
||||
public void onProximityAdded(Tile tile){
|
||||
StorageEntity entity = tile.entity();
|
||||
entity.graph.add(tile);
|
||||
entity.graph.set(tile);
|
||||
|
||||
for(Tile prox : tile.entity.proximity()){
|
||||
if(prox.block() instanceof StorageBlock){
|
||||
@@ -92,7 +99,10 @@ public abstract class StorageBlock extends Block{
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -11,33 +11,70 @@ 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;
|
||||
private int cores;
|
||||
|
||||
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)){
|
||||
if(tile.block() instanceof CoreBlock) cores ++;
|
||||
capacity += tile.block().itemCapacity;
|
||||
|
||||
if(tile.entity.items != items){
|
||||
items.addAll(tile.entity.items);
|
||||
}
|
||||
if(!tiles.add(tile)) return;
|
||||
|
||||
tile.entity.items = items;
|
||||
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){
|
||||
other.<StorageEntity>entity().graph = null;
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
cores = 0;
|
||||
//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()){
|
||||
@@ -53,11 +90,16 @@ public class StorageGraph{
|
||||
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);
|
||||
@@ -70,9 +112,14 @@ public class StorageGraph{
|
||||
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){
|
||||
StorageEntity e = tile.entity();
|
||||
e.graph = this;
|
||||
add(tile);
|
||||
}
|
||||
}
|
||||
@@ -82,21 +129,13 @@ public class StorageGraph{
|
||||
}
|
||||
|
||||
public int accept(Item item, int amount){
|
||||
if(hasCores()){
|
||||
return Math.min(capacity - items.get(item), amount);
|
||||
}else{
|
||||
return Math.min(capacity - items.total(), amount);
|
||||
}
|
||||
return Math.min(capacity - items.get(item), amount);
|
||||
}
|
||||
|
||||
public ObjectSet<Tile> getTiles(){
|
||||
return tiles;
|
||||
}
|
||||
|
||||
public boolean hasCores(){
|
||||
return cores > 0;
|
||||
}
|
||||
|
||||
public int getID(){
|
||||
return id;
|
||||
}
|
||||
@@ -108,4 +147,5 @@ public class StorageGraph{
|
||||
public ItemModule items(){
|
||||
return items;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,8 +11,11 @@ import java.util.Arrays;
|
||||
import static io.anuke.mindustry.Vars.content;
|
||||
|
||||
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++){
|
||||
@@ -111,6 +114,8 @@ public class ItemModule 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++;
|
||||
@@ -128,6 +133,7 @@ public class ItemModule extends BlockModule{
|
||||
|
||||
@Override
|
||||
public void read(DataInput stream) throws IOException{
|
||||
id = stream.readInt();
|
||||
byte count = stream.readByte();
|
||||
total = 0;
|
||||
|
||||
@@ -139,6 +145,14 @@ public class ItemModule 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