Fixed many crashes, refactored block bars

This commit is contained in:
Anuken 2018-03-10 12:13:42 -05:00
parent d88a908e29
commit 83452b1e36
27 changed files with 1551 additions and 1300 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
#Autogenerated file. Do not modify.
#Fri Mar 09 23:07:56 EST 2018
#Sat Mar 10 12:12:41 EST 2018
version=release
androidBuildCode=420
androidBuildCode=424
name=Mindustry
code=3.4
build=custom build

View File

@ -478,7 +478,7 @@ public class Renderer extends RendererModule{
if(target.entity != null) {
int bot = 0, top = 0;
for (BlockBar bar : target.block().bars) {
for (BlockBar bar : target.block().bars.list()) {
float offset = Mathf.sign(bar.top) * (target.block().size / 2f * tilesize + 3f + 4f * ((bar.top ? top : bot))) +
(bar.top ? -1f : 0f);
@ -486,7 +486,7 @@ public class Renderer extends RendererModule{
if(MathUtils.isEqual(value, -1f)) continue;
drawBar(bar.color, target.drawx(), target.drawy() + offset, value);
drawBar(bar.type.color, target.drawx(), target.drawy() + offset, value);
if (bar.top)
top++;

View File

@ -200,7 +200,6 @@ public class Save16 extends SaveFileVersion {
if(tile.entity != null){
byte rotation = stream.readByte();
short health = stream.readShort();
int items = stream.readByte();
tile.entity.health = health;
tile.setRotation(rotation);

View File

@ -9,7 +9,7 @@ import io.anuke.mindustry.input.InputHandler;
import io.anuke.mindustry.resource.*;
import io.anuke.mindustry.ui.dialogs.FloatingDialog;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.blocks.types.BlockStats;
import io.anuke.mindustry.world.BlockStats;
import io.anuke.ucore.graphics.Draw;
import io.anuke.ucore.graphics.Hue;
import io.anuke.ucore.scene.Element;

View File

@ -0,0 +1,17 @@
package io.anuke.mindustry.world;
import com.badlogic.gdx.graphics.Color;
public enum BarType {
health(Color.RED),
inventory(Color.GREEN),
power(Color.YELLOW),
liquid(Color.ROYAL),
heat(Color.CORAL);
public final Color color;
BarType(Color color){
this.color = color;
}
}

View File

@ -1,6 +1,5 @@
package io.anuke.mindustry.world;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
@ -14,7 +13,6 @@ import io.anuke.mindustry.net.NetEvents;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.resource.Liquid;
import io.anuke.mindustry.world.blocks.BaseBlock;
import io.anuke.mindustry.world.blocks.types.BlockStats;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Effects.Effect;
import io.anuke.ucore.graphics.Draw;
@ -87,13 +85,13 @@ public class Block extends BaseBlock {
public Layer layer = null;
/**Extra layer to draw extra extra stuff on.*/
public Layer layer2 = null;
/**list of displayed block status bars. Defaults to health bar.*/
public Array<BlockBar> bars = Array.with(new BlockBar(Color.RED, false, tile -> tile.entity.health / (float)tile.block().health));
/**whether this block can be replaced in all cases*/
public boolean alwaysReplace = false;
/**whether this block has instant transfer checking. used for calculations to prevent infinite loops.*/
public boolean instantTransfer = false;
/**list of displayed block status bars. Defaults to health bar.*/
public BlockBars bars = new BlockBars();
/**List of block stats.*/
public BlockStats stats = new BlockStats();
public Block(String name) {
@ -148,9 +146,9 @@ public class Block extends BaseBlock {
//TODO make this easier to config.
public void setBars(){
if(hasPower) bars.add(new BlockBar(Color.YELLOW, true, tile -> tile.entity.power.amount / powerCapacity));
if(hasLiquids) bars.add(new BlockBar(Color.ROYAL, true, tile -> tile.entity.liquid.amount / liquidCapacity));
if(hasInventory) bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.entity.inventory.totalItems() / itemCapacity));
if(hasPower) bars.add(new BlockBar(BarType.power, true, tile -> tile.entity.power.amount / powerCapacity));
if(hasLiquids) bars.add(new BlockBar(BarType.liquid, true, tile -> tile.entity.liquid.amount / liquidCapacity));
if(hasInventory) bars.add(new BlockBar(BarType.inventory, true, tile -> (float)tile.entity.inventory.totalItems() / itemCapacity));
}
public String name(){

View File

@ -1,15 +1,13 @@
package io.anuke.mindustry.world;
import com.badlogic.gdx.graphics.Color;
public class BlockBar {
public final ValueSupplier value;
public final Color color;
public final BarType type;
public final boolean top;
public BlockBar(Color color, boolean top, ValueSupplier value) {
public BlockBar(BarType type, boolean top, ValueSupplier value) {
this.value = value;
this.color = color;
this.type = type;
this.top = top;
}

View File

@ -0,0 +1,41 @@
package io.anuke.mindustry.world;
import com.badlogic.gdx.utils.Array;
public class BlockBars {
private Array<BlockBar> list = Array.with(new BlockBar(BarType.health, false, tile -> tile.entity.health / (float)tile.block().health));
public void add(BlockBar bar){
list.add(bar);
}
public void replace(BlockBar bar){
remove(bar.type);
list.add(bar);
}
public void remove(BarType type){
for(BlockBar bar : list){
if(bar.type == type){
list.removeValue(bar, true);
break;
}
}
}
public void removeAll(BarType type){
Array<BlockBar> removals = new Array<>(4);
for(BlockBar bar : list){
if(bar.type == type){
removals.add(bar);
}
}
list.removeAll(removals, true);
}
public Array<BlockBar> list() {
return list;
}
}

View File

@ -1,4 +1,4 @@
package io.anuke.mindustry.world.blocks.types;
package io.anuke.mindustry.world;
import com.badlogic.gdx.utils.OrderedMap;
import io.anuke.ucore.util.Bundles;

View File

@ -147,7 +147,7 @@ public class ProductionBlocks{
{
resource = Blocks.stone;
result = Item.stone;
drillTime = 40;
drillTime = 240;
}
},
@ -155,7 +155,7 @@ public class ProductionBlocks{
{
resource = Blocks.iron;
result = Item.iron;
drillTime = 60;
drillTime = 360;
}
},
@ -163,7 +163,7 @@ public class ProductionBlocks{
{
resource = Blocks.coal;
result = Item.coal;
drillTime = 60;
drillTime = 360;
}
},
@ -171,7 +171,7 @@ public class ProductionBlocks{
{
resource = Blocks.uranium;
result = Item.uranium;
drillTime = 70;
drillTime = 540;
}
},
@ -179,13 +179,13 @@ public class ProductionBlocks{
{
resource = Blocks.titanium;
result = Item.titanium;
drillTime = 70;
drillTime = 540;
}
},
laserdrill = new GenericDrill("laserdrill"){
{
drillTime = 40;
drillTime = 240;
size = 2;
powerUse = 0.08f;
}
@ -193,7 +193,7 @@ public class ProductionBlocks{
nucleardrill = new GenericDrill("nucleardrill"){
{
drillTime = 40;
drillTime = 240;
size = 3;
powerUse = 0.32f;
}
@ -202,7 +202,7 @@ public class ProductionBlocks{
plasmadrill = new GenericDrill("plasmadrill"){
{
inputLiquid = Liquid.plasma;
drillTime = 40;
drillTime = 240;
size = 4;
powerUse = 0.16f;
}
@ -212,7 +212,7 @@ public class ProductionBlocks{
{
resource = Blocks.sand;
result = Item.quartz;
drillTime = 50;
drillTime = 300;
size = 2;
}
},

View File

@ -11,6 +11,7 @@ public abstract class PowerBlock extends Block{
super(name);
update = true;
solid = true;
hasPower = true;
}
@Override

View File

@ -28,7 +28,6 @@ public class RepairTurret extends PowerTurret{
public void setStats(){
super.setStats();
//TODO remove extra crap
stats.add("repairssecond", Strings.toFixed(60f/reload * repairFrac * 100, 1) + "%");
}

View File

@ -7,10 +7,7 @@ import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.enemies.Enemy;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Layer;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.*;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.core.Effects.Effect;
import io.anuke.ucore.core.Timers;
@ -59,8 +56,11 @@ public class Turret extends Block{
update = true;
solid = true;
layer = Layer.turret;
}
bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.<TurretEntity>entity().ammo / maxammo));
@Override
public void setBars(){
bars.replace(new BlockBar(BarType.inventory, true, tile -> (float)tile.<TurretEntity>entity().ammo / maxammo));
}
@Override

View File

@ -24,7 +24,6 @@ import io.anuke.ucore.util.Translator;
import static io.anuke.mindustry.Vars.*;
public class Generator extends PowerBlock{
public static final int powerTime = 2;
public static boolean drawRangeOverlay = false;
protected Translator t1 = new Translator();

View File

@ -3,6 +3,7 @@ package io.anuke.mindustry.world.blocks.types.generation;
import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.BarType;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Effects;
@ -23,8 +24,12 @@ public class ItemPowerGenerator extends Generator{
public ItemPowerGenerator(String name) {
super(name);
outputOnly = true;
}
bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.entity.inventory.getItem(generateItem) / itemCapacity));
@Override
public void setBars(){
super.setBars();
bars.replace(new BlockBar(BarType.inventory, true, tile -> (float)tile.entity.inventory.getItem(generateItem) / itemCapacity));
}
@Override

View File

@ -23,12 +23,12 @@ public class LiquidPowerGenerator extends Generator{
super(name);
outputOnly = true;
liquidCapacity = 30f;
hasLiquids = true;
}
@Override
public void setStats(){
super.setStats();
stats.add("liquidcapacity", (int)liquidCapacity);
stats.add("powerliquid", Strings.toFixed(powerPerLiquid, 2) + " power/liquid");
stats.add("maxliquidsecond", Strings.toFixed(maxLiquidGenerate*60f, 2) + " liquid/s");
stats.add("input", generateLiquid);
@ -40,8 +40,6 @@ public class LiquidPowerGenerator extends Generator{
TileEntity entity = tile.entity();
if(entity.liquid.liquid == null) return;
Draw.color(entity.liquid.liquid.color);
Draw.alpha(entity.liquid.amount / liquidCapacity);
drawLiquidCenter(tile);

View File

@ -6,6 +6,7 @@ import io.anuke.mindustry.entities.effect.DamageArea;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.Liquid;
import io.anuke.mindustry.world.BarType;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Effects;
@ -48,9 +49,13 @@ public class NuclearReactor extends LiquidPowerGenerator{
explosive = true;
powerCapacity = 80f;
powerSpeed = 0.5f;
}
bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.entity.inventory.getItem(generateItem) / itemCapacity));
bars.add(new BlockBar(Color.ORANGE, true, tile -> tile.<NuclearReactorEntity>entity().heat));
@Override
public void setBars(){
super.setBars();
bars.replace(new BlockBar(BarType.inventory, true, tile -> (float)tile.entity.inventory.getItem(generateItem) / itemCapacity));
bars.add(new BlockBar(BarType.heat, true, tile -> tile.<NuclearReactorEntity>entity().heat));
}
@Override
@ -183,7 +188,7 @@ public class NuclearReactor extends LiquidPowerGenerator{
return new NuclearReactorEntity();
}
public static class NuclearReactorEntity extends PowerEntity{
public static class NuclearReactorEntity extends GeneratorEntity{
public float heat;
public float flash;

View File

@ -21,7 +21,7 @@ public class Drill extends Block{
protected Block resource;
protected Item result;
protected float drillTime = 5;
protected float drillTime = 300;
protected Effect drillEffect = Fx.spark;
public Drill(String name) {

View File

@ -46,7 +46,7 @@ public class GenericDrill extends Drill{
}
}
if(toAdd.size > 0 && entity.power.amount > powerUse && entity.time >= drillTime
if(toAdd.size > 0 && entity.time >= drillTime
&& tile.entity.inventory.totalItems() < itemCapacity){
for(Item item : toAdd) offloadNear(tile, item);
Effects.effect(drillEffect, tile.drawx(), tile.drawy());

View File

@ -1,10 +1,10 @@
package io.anuke.mindustry.world.blocks.types.production;
import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.Liquid;
import io.anuke.mindustry.world.BarType;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.types.LiquidBlock;
@ -34,8 +34,15 @@ public class LiquidCrafter extends LiquidBlock{
solid = true;
health = 60;
liquidCapacity = 21f;
}
bars.add(new BlockBar(Color.GREEN, true, tile -> input == null ? -1f : (float)tile.entity.inventory.getItem(input) / itemCapacity));
@Override
public void setBars(){
super.setBars();
bars.remove(BarType.inventory);
bars.add(new BlockBar(BarType.inventory, true,
tile -> input == null ? -1f : (float)tile.entity.inventory.getItem(input) / itemCapacity));
}
@Override

View File

@ -1,10 +1,10 @@
package io.anuke.mindustry.world.blocks.types.production;
import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.world.BarType;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.types.PowerBlock;
@ -42,9 +42,12 @@ public class PowerSmelter extends PowerBlock {
}
@Override
public void init(){
public void setBars(){
super.setBars();
bars.remove(BarType.inventory);
for(ItemStack item : inputs){
bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.entity.inventory.getItem(item.item)/capacity));
bars.add(new BlockBar(BarType.inventory, true, tile -> (float)tile.entity.inventory.getItem(item.item)/capacity));
}
}
@ -52,7 +55,7 @@ public class PowerSmelter extends PowerBlock {
public void setStats(){
super.setStats();
stats.add("input", Arrays.toString(inputs));
stats.add("powerdrainsecond", Strings.toFixed(powerDrain*60f, 2));
stats.add("powersecond", Strings.toFixed(powerDrain*60f, 2));
stats.add("output", result);
stats.add("fuelduration", Strings.toFixed(burnDuration/60f, 1));
stats.add("maxoutputsecond", Strings.toFixed(60f/craftTime, 1));

View File

@ -1,9 +1,9 @@
package io.anuke.mindustry.world.blocks.types.production;
import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.BarType;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
@ -37,9 +37,9 @@ public class Smelter extends Block{
}
@Override
public void init(){
public void setBars(){
for(Item item : inputs){
bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.entity.inventory.getItem(item)/capacity));
bars.add(new BlockBar(BarType.inventory, true, tile -> (float)tile.entity.inventory.getItem(item)/capacity));
}
}
@ -51,7 +51,7 @@ public class Smelter extends Block{
stats.add("output", result);
stats.add("fuelduration", Strings.toFixed(burnDuration/60f, 1));
stats.add("maxoutputsecond", Strings.toFixed(60f/craftTime, 1));
stats.add("input capacity", capacity);
stats.add("inputcapacity", capacity);
stats.add("outputcapacity", capacity);
}

View File

@ -1,5 +1,6 @@
package io.anuke.mindustry.world.blocks.types.storage;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.Tile;
@ -17,6 +18,7 @@ public class CoreBlock extends StorageBlock {
solid = true;
destructible = true;
size = 3;
hasInventory = false;
}
@Override
@ -31,6 +33,28 @@ public class CoreBlock extends StorageBlock {
@Override
public boolean acceptItem(Item item, Tile tile, Tile source){
return item.material && tile.entity.inventory.getItem(item) < capacity;
return item.material && state.inventory.getAmount(item) < capacity;
}
@Override
public Item removeItem(Tile tile){
for(int i = 0; i < state.inventory.getItems().length; i ++){
if(state.inventory.getItems()[i] > 0){
if(Net.server() || !Net.active()) state.inventory.getItems()[i] --;
return Item.getByID(i);
}
}
return null;
}
@Override
public boolean hasItem(Tile tile){
TileEntity entity = tile.entity;
for(int i = 0; i < state.inventory.getItems().length; i ++){
if(state.inventory.getItems()[i] > 0){
return true;
}
}
return false;
}
}

View File

@ -22,4 +22,14 @@ public abstract class StorageBlock extends Block {
}
return null;
}
public boolean hasItem(Tile tile){
TileEntity entity = tile.entity;
for(int i = 0; i < entity.inventory.items.length; i ++){
if(entity.inventory.items[i] > 0){
return true;
}
}
return false;
}
}

View File

@ -19,7 +19,7 @@ public class Unloader extends Block {
if(tile.entity.inventory.totalItems() == 0 && tile.entity.timer.get(timerUnload, 5)){
tile.allNearby(other -> {
if(other.block() instanceof StorageBlock && tile.entity.inventory.totalItems() == 0 &&
other.entity.inventory.totalItems() > 0){
((StorageBlock)other.block()).hasItem(other)){
offloadNear(tile, ((StorageBlock)other.block()).removeItem(other));
}
});
@ -35,4 +35,7 @@ public class Unloader extends Block {
Block block = to.target().block();
return !(block instanceof StorageBlock);
}
@Override
public void setBars(){}
}

View File

@ -1,19 +1,16 @@
package io.anuke.mindustry.world.blocks.types.storage;
import com.badlogic.gdx.graphics.Color;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Timers;
public class Vault extends StorageBlock {
public int capacity = 1000;
public Vault(String name){
super(name);
solid = true;
update = true;
bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.entity.inventory.totalItems()/capacity));
itemCapacity = 1000;
}
@Override
@ -35,7 +32,7 @@ public class Vault extends StorageBlock {
@Override
public boolean acceptItem(Item item, Tile tile, Tile source) {
return tile.entity.inventory.totalItems() < capacity;
return tile.entity.inventory.totalItems() < itemCapacity;
}
@Override