From 4b26c57a20072372d98c2f43f9b7e09cc2594901 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 12 Sep 2019 21:14:09 -0400 Subject: [PATCH] Added direct core-vault chaining --- core/assets/bundles/bundle.properties | 1 + .../mindustry/entities/type/TileEntity.java | 10 ++- .../world/blocks/power/ThermalGenerator.java | 2 +- .../world/blocks/storage/CoreBlock.java | 88 ++++++++++++++++++- .../world/blocks/storage/StorageBlock.java | 21 ++++- 5 files changed, 114 insertions(+), 8 deletions(-) diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 382212d6ed..54a5a6cbe3 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -481,6 +481,7 @@ bar.powerbalance = Power: {0}/s bar.poweramount = Power: {0} bar.poweroutput = Power Output: {0} bar.items = Items: {0} +bar.capacity = Capacity: {0} bar.liquid = Liquid bar.heat = Heat bar.power = Power diff --git a/core/src/io/anuke/mindustry/entities/type/TileEntity.java b/core/src/io/anuke/mindustry/entities/type/TileEntity.java index 3205eee53a..ae2e912467 100644 --- a/core/src/io/anuke/mindustry/entities/type/TileEntity.java +++ b/core/src/io/anuke/mindustry/entities/type/TileEntity.java @@ -207,14 +207,12 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{ if(other == null) continue; if(other.entity == null || !(other.interactable(tile.getTeam()))) continue; - other.block().onProximityUpdate(other); - - tmpTiles.add(other); - //add this tile to proximity of nearby tiles if(!other.entity.proximity.contains(tile, true)){ other.entity.proximity.add(tile); } + + tmpTiles.add(other); } //using a set to prevent duplicates @@ -224,6 +222,10 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{ block.onProximityAdded(tile); block.onProximityUpdate(tile); + + for(Tile other : tmpTiles){ + other.block().onProximityUpdate(other); + } } public Array proximity(){ diff --git a/core/src/io/anuke/mindustry/world/blocks/power/ThermalGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/ThermalGenerator.java index 092fcbd3e8..2415a84578 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/ThermalGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/ThermalGenerator.java @@ -47,6 +47,6 @@ public class ThermalGenerator extends PowerGenerator{ @Override public boolean canPlaceOn(Tile tile){ //make sure there's heat at this location - return tile.getLinkedTilesAs(this, tempTiles).sum(other -> other.floor().attributes.get(Attribute.heat)) > 0.01f; + return tile.getLinkedTilesAs(this, tempTiles).sumf(other -> other.floor().attributes.get(Attribute.heat)) > 0.01f; } } diff --git a/core/src/io/anuke/mindustry/world/blocks/storage/CoreBlock.java b/core/src/io/anuke/mindustry/world/blocks/storage/CoreBlock.java index 4b90693a8e..f176c81c33 100644 --- a/core/src/io/anuke/mindustry/world/blocks/storage/CoreBlock.java +++ b/core/src/io/anuke/mindustry/world/blocks/storage/CoreBlock.java @@ -3,7 +3,11 @@ package io.anuke.mindustry.world.blocks.storage; import io.anuke.annotations.Annotations.*; import io.anuke.arc.*; import io.anuke.arc.collection.*; +import io.anuke.arc.collection.ObjectSet.*; +import io.anuke.arc.function.*; +import io.anuke.arc.graphics.g2d.*; import io.anuke.arc.math.*; +import io.anuke.arc.math.geom.*; import io.anuke.mindustry.content.*; import io.anuke.mindustry.entities.*; import io.anuke.mindustry.entities.traits.*; @@ -12,9 +16,11 @@ import io.anuke.mindustry.game.EventType.*; import io.anuke.mindustry.gen.*; import io.anuke.mindustry.graphics.*; import io.anuke.mindustry.type.*; +import io.anuke.mindustry.ui.*; import io.anuke.mindustry.world.*; import io.anuke.mindustry.world.blocks.*; import io.anuke.mindustry.world.meta.*; +import io.anuke.mindustry.world.modules.*; import static io.anuke.mindustry.Vars.*; @@ -46,19 +52,80 @@ public class CoreBlock extends StorageBlock{ entity.spawnPlayer = null; } + @Override + public void setStats(){ + super.setStats(); + + bars.add("capacity", e -> + new Bar( + () -> Core.bundle.format("bar.capacity", ui.formatAmount(((CoreEntity)e).storageCapacity)), + () -> Pal.items, + () -> e.items.total() / (float)(((CoreEntity)e).storageCapacity * content.items().count(i -> i.type == ItemType.material)) + )); + } + + @Override + public boolean acceptItem(Item item, Tile tile, Tile source){ + return tile.entity.items.get(item) < getMaximumAccepted(tile, item); + } + @Override public int getMaximumAccepted(Tile tile, Item item){ - return item.type == ItemType.material ? itemCapacity * state.teams.get(tile.getTeam()).cores.size : 0; + CoreEntity entity = tile.entity(); + return item.type == ItemType.material ? entity.storageCapacity : entity.storageCapacity; } @Override public void onProximityUpdate(Tile tile){ + CoreEntity entity = tile.entity(); + for(Tile other : state.teams.get(tile.getTeam()).cores){ if(other != tile){ - tile.entity.items = other.entity.items; + entity.items = other.entity.items; } } state.teams.get(tile.getTeam()).cores.add(tile); + + entity.storageCapacity = entity.proximity().sum(e -> isContainer(e) ? e.block().itemCapacity : 0); + entity.proximity().each(this::isContainer, t -> { + t.entity.items = entity.items; + t.entity().linkedCore = tile; + }); + + for(Tile other : state.teams.get(tile.getTeam()).cores){ + entity.storageCapacity += other.block().itemCapacity; + } + + for(Item item : content.items()){ + entity.items.set(item, Math.min(entity.items.get(item), entity.storageCapacity)); + } + + for(Tile other : state.teams.get(tile.getTeam()).cores){ + CoreEntity oe = other.entity(); + oe.storageCapacity = entity.storageCapacity; + } + } + + @Override + public void drawSelect(Tile tile){ + Lines.stroke(1f, Pal.accent); + Consumer outline = t -> { + for(int i = 0; i < 4; i++){ + Point2 p = Geometry.d8edge[i]; + float offset = -Math.max(t.block().size - 1, 0) / 2f * tilesize; + Draw.rect("block-select", t.drawx() + offset * p.x, t.drawy() + offset * p.y, i * 90); + } + }; + if(tile.entity.proximity().contains(e -> isContainer(e) && e.entity.items == tile.entity.items)){ + outline.accept(tile); + } + tile.entity.proximity().each(e -> isContainer(e) && e.entity.items == tile.entity.items, outline); + Draw.reset(); + } + + + public boolean isContainer(Tile tile){ + return tile.entity instanceof StorageBlockEntity; } @Override @@ -68,12 +135,28 @@ public class CoreBlock extends StorageBlock{ @Override public void removed(Tile tile){ + int total = tile.entity.proximity().count(e -> e.entity.items == tile.entity.items); + float fract = 1f / total / state.teams.get(tile.getTeam()).cores.size; + + tile.entity.proximity().each(e -> isContainer(e) && e.entity.items == tile.entity.items, t -> { + StorageBlockEntity ent = (StorageBlockEntity)t.entity; + ent.linkedCore = null; + ent.items = new ItemModule(); + for(Item item : content.items()){ + ent.items.set(item, (int)(fract * tile.entity.items.get(item))); + } + }); + state.teams.get(tile.getTeam()).cores.remove(tile); int max = itemCapacity * state.teams.get(tile.getTeam()).cores.size; for(Item item : content.items()){ tile.entity.items.set(item, Math.min(tile.entity.items.get(item), max)); } + + for(Tile other : new ObjectSetIterator<>(state.teams.get(tile.getTeam()).cores)){ + other.block().onProximityUpdate(other); + } } @Override @@ -141,6 +224,7 @@ public class CoreBlock extends StorageBlock{ protected float progress; protected float time; protected float heat; + protected int storageCapacity; @Override public boolean hasUnit(Unit unit){ diff --git a/core/src/io/anuke/mindustry/world/blocks/storage/StorageBlock.java b/core/src/io/anuke/mindustry/world/blocks/storage/StorageBlock.java index 58739c63c5..28566f1ca7 100644 --- a/core/src/io/anuke/mindustry/world/blocks/storage/StorageBlock.java +++ b/core/src/io/anuke/mindustry/world/blocks/storage/StorageBlock.java @@ -1,5 +1,6 @@ package io.anuke.mindustry.world.blocks.storage; +import io.anuke.annotations.Annotations.*; import io.anuke.mindustry.entities.type.TileEntity; import io.anuke.mindustry.type.Item; import io.anuke.mindustry.world.Block; @@ -14,7 +15,8 @@ public abstract class StorageBlock extends Block{ @Override public boolean acceptItem(Item item, Tile tile, Tile source){ - return tile.entity.items.get(item) < getMaximumAccepted(tile, item); + StorageBlockEntity entity = tile.entity(); + return entity.linkedCore != null ? entity.linkedCore.block().acceptItem(item, entity.linkedCore, source) : tile.entity.items.get(item) < getMaximumAccepted(tile, item); } @Override @@ -22,6 +24,14 @@ public abstract class StorageBlock extends Block{ return itemCapacity; } + @Override + public void drawSelect(Tile tile){ + StorageBlockEntity entity = tile.entity(); + if(entity.linkedCore != null){ + entity.linkedCore.block().drawSelect(entity.linkedCore); + } + } + @Override public boolean outputsItems(){ return false; @@ -58,4 +68,13 @@ public abstract class StorageBlock extends Block{ return entity.items.has(item); } } + + @Override + public TileEntity newEntity(){ + return new StorageBlockEntity(); + } + + public class StorageBlockEntity extends TileEntity{ + protected @Nullable Tile linkedCore; + } }