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"){{
|
vault = new Vault("vault"){{
|
||||||
size = 3;
|
size = 3;
|
||||||
itemCapacity = 2000;
|
itemCapacity = 1000;
|
||||||
}};
|
}};
|
||||||
|
|
||||||
container = new Vault("container"){{
|
container = new Vault("container"){{
|
||||||
size = 2;
|
size = 2;
|
||||||
itemCapacity = 500;
|
itemCapacity = 250;
|
||||||
}};
|
}};
|
||||||
|
|
||||||
unloader = new SortedUnloader("unloader"){{
|
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.Palette;
|
||||||
import io.anuke.mindustry.graphics.Shaders;
|
import io.anuke.mindustry.graphics.Shaders;
|
||||||
import io.anuke.mindustry.type.Item;
|
import io.anuke.mindustry.type.Item;
|
||||||
|
import io.anuke.mindustry.world.BarType;
|
||||||
import io.anuke.mindustry.world.Block;
|
import io.anuke.mindustry.world.Block;
|
||||||
import io.anuke.mindustry.world.Tile;
|
import io.anuke.mindustry.world.Tile;
|
||||||
import io.anuke.ucore.core.Graphics;
|
import io.anuke.ucore.core.Graphics;
|
||||||
@@ -22,6 +23,12 @@ public abstract class StorageBlock extends Block{
|
|||||||
hasItems = true;
|
hasItems = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBars(){
|
||||||
|
super.setBars();
|
||||||
|
bars.remove(BarType.inventory);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean outputsItems(){
|
public boolean outputsItems(){
|
||||||
return false;
|
return false;
|
||||||
@@ -30,7 +37,7 @@ public abstract class StorageBlock extends Block{
|
|||||||
@Override
|
@Override
|
||||||
public void onProximityAdded(Tile tile){
|
public void onProximityAdded(Tile tile){
|
||||||
StorageEntity entity = tile.entity();
|
StorageEntity entity = tile.entity();
|
||||||
entity.graph.add(tile);
|
entity.graph.set(tile);
|
||||||
|
|
||||||
for(Tile prox : tile.entity.proximity()){
|
for(Tile prox : tile.entity.proximity()){
|
||||||
if(prox.block() instanceof StorageBlock){
|
if(prox.block() instanceof StorageBlock){
|
||||||
@@ -92,7 +99,10 @@ public abstract class StorageBlock extends Block{
|
|||||||
Array<Object> arr = super.getDebugInfo(tile);
|
Array<Object> arr = super.getDebugInfo(tile);
|
||||||
|
|
||||||
StorageEntity entity = tile.entity();
|
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;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,33 +11,70 @@ import io.anuke.mindustry.world.modules.ItemModule;
|
|||||||
public class StorageGraph{
|
public class StorageGraph{
|
||||||
private static IntSet closedSet = new IntSet();
|
private static IntSet closedSet = new IntSet();
|
||||||
private static Queue<Tile> queue = new Queue<>();
|
private static Queue<Tile> queue = new Queue<>();
|
||||||
|
private static ObjectSet<ItemModule> itemSet = new ObjectSet<>();
|
||||||
private static int lastID;
|
private static int lastID;
|
||||||
|
|
||||||
private final int id = lastID++;
|
private final int id = lastID++;
|
||||||
private ObjectSet<Tile> tiles = new ObjectSet<>();
|
private ObjectSet<Tile> tiles = new ObjectSet<>();
|
||||||
private ItemModule items = new ItemModule();
|
private ItemModule items = new ItemModule();
|
||||||
private int capacity;
|
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){
|
public void add(Tile tile){
|
||||||
if(tiles.add(tile)){
|
|
||||||
if(tile.block() instanceof CoreBlock) cores ++;
|
|
||||||
capacity += tile.block().itemCapacity;
|
|
||||||
|
|
||||||
if(tile.entity.items != items){
|
if(!tiles.add(tile)) return;
|
||||||
items.addAll(tile.entity.items);
|
|
||||||
}
|
|
||||||
|
|
||||||
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){
|
public void remove(Tile tile){
|
||||||
|
if(!tiles.contains(tile)) return;
|
||||||
|
|
||||||
for(Tile other : tiles){
|
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;
|
capacity = 0;
|
||||||
|
|
||||||
for(Tile other : tile.entity.proximity()){
|
for(Tile other : tile.entity.proximity()){
|
||||||
@@ -53,11 +90,16 @@ public class StorageGraph{
|
|||||||
queue.clear();
|
queue.clear();
|
||||||
queue.addLast(tile);
|
queue.addLast(tile);
|
||||||
closedSet.clear();
|
closedSet.clear();
|
||||||
|
itemSet.clear();
|
||||||
|
|
||||||
while(queue.size > 0){
|
while(queue.size > 0){
|
||||||
Tile child = queue.removeFirst();
|
Tile child = queue.removeFirst();
|
||||||
StorageEntity entity = child.entity();
|
StorageEntity entity = child.entity();
|
||||||
entity.graph = this;
|
entity.graph = this;
|
||||||
|
|
||||||
|
if(!itemSet.add(child.entity.items)) child.entity.items = null;
|
||||||
add(child);
|
add(child);
|
||||||
|
|
||||||
for(Tile next : child.entity.proximity()){
|
for(Tile next : child.entity.proximity()){
|
||||||
if(next != base && next.block() instanceof StorageBlock && next.<StorageEntity>entity().graph == null && !closedSet.contains(next.packedPosition())){
|
if(next != base && next.block() instanceof StorageBlock && next.<StorageEntity>entity().graph == null && !closedSet.contains(next.packedPosition())){
|
||||||
queue.addLast(next);
|
queue.addLast(next);
|
||||||
@@ -70,9 +112,14 @@ public class StorageGraph{
|
|||||||
public void merge(StorageGraph other){
|
public void merge(StorageGraph other){
|
||||||
if(this == other || other == null) return;
|
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){
|
for(Tile tile : other.tiles){
|
||||||
StorageEntity e = tile.entity();
|
|
||||||
e.graph = this;
|
|
||||||
add(tile);
|
add(tile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,21 +129,13 @@ public class StorageGraph{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int accept(Item item, int amount){
|
public int accept(Item item, int amount){
|
||||||
if(hasCores()){
|
return Math.min(capacity - items.get(item), amount);
|
||||||
return Math.min(capacity - items.get(item), amount);
|
|
||||||
}else{
|
|
||||||
return Math.min(capacity - items.total(), amount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectSet<Tile> getTiles(){
|
public ObjectSet<Tile> getTiles(){
|
||||||
return tiles;
|
return tiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasCores(){
|
|
||||||
return cores > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getID(){
|
public int getID(){
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@@ -108,4 +147,5 @@ public class StorageGraph{
|
|||||||
public ItemModule items(){
|
public ItemModule items(){
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,11 @@ import java.util.Arrays;
|
|||||||
import static io.anuke.mindustry.Vars.content;
|
import static io.anuke.mindustry.Vars.content;
|
||||||
|
|
||||||
public class ItemModule extends BlockModule{
|
public class ItemModule extends BlockModule{
|
||||||
|
private static int lastID;
|
||||||
|
|
||||||
private int[] items = new int[content.items().size];
|
private int[] items = new int[content.items().size];
|
||||||
private int total;
|
private int total;
|
||||||
|
private int id = lastID ++;
|
||||||
|
|
||||||
public void forEach(ItemConsumer cons){
|
public void forEach(ItemConsumer cons){
|
||||||
for(int i = 0; i < items.length; i++){
|
for(int i = 0; i < items.length; i++){
|
||||||
@@ -111,6 +114,8 @@ public class ItemModule extends BlockModule{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(DataOutput stream) throws IOException{
|
public void write(DataOutput stream) throws IOException{
|
||||||
|
stream.writeInt(id); //unique ID
|
||||||
|
|
||||||
byte amount = 0;
|
byte amount = 0;
|
||||||
for(int item : items){
|
for(int item : items){
|
||||||
if(item > 0) amount++;
|
if(item > 0) amount++;
|
||||||
@@ -128,6 +133,7 @@ public class ItemModule extends BlockModule{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(DataInput stream) throws IOException{
|
public void read(DataInput stream) throws IOException{
|
||||||
|
id = stream.readInt();
|
||||||
byte count = stream.readByte();
|
byte count = stream.readByte();
|
||||||
total = 0;
|
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{
|
public interface ItemConsumer{
|
||||||
void accept(Item item, float amount);
|
void accept(Item item, float amount);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user