Optimize with sleeps
Ehmm i meant recharge cycles, beep boop.
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package mindustry.world.blocks.distribution;
|
package mindustry.world.blocks.distribution;
|
||||||
|
|
||||||
import arc.*;
|
import arc.*;
|
||||||
|
import arc.graphics.*;
|
||||||
import arc.graphics.g2d.*;
|
import arc.graphics.g2d.*;
|
||||||
import arc.math.*;
|
import arc.math.*;
|
||||||
|
import arc.struct.*;
|
||||||
import arc.util.*;
|
import arc.util.*;
|
||||||
import mindustry.content.*;
|
import mindustry.content.*;
|
||||||
import mindustry.entities.*;
|
import mindustry.entities.*;
|
||||||
@@ -26,6 +28,8 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
private static final byte head = 1;
|
private static final byte head = 1;
|
||||||
private static final byte tail = 2;
|
private static final byte tail = 2;
|
||||||
|
|
||||||
|
private Array<Tile> tmptiles = new Array<>();
|
||||||
|
|
||||||
public float speed = 0f;
|
public float speed = 0f;
|
||||||
|
|
||||||
public CraterConveyor(String name){
|
public CraterConveyor(String name){
|
||||||
@@ -38,7 +42,7 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
hasItems = true;
|
hasItems = true;
|
||||||
itemCapacity = 4;
|
itemCapacity = 4;
|
||||||
conveyorPlacement = true;
|
conveyorPlacement = true;
|
||||||
entityType = TrackEntity::new;
|
entityType = CraterConveyorEntity::new;
|
||||||
|
|
||||||
idleSound = Sounds.conveyor;
|
idleSound = Sounds.conveyor;
|
||||||
idleSoundVolume = 0.004f;
|
idleSoundVolume = 0.004f;
|
||||||
@@ -74,7 +78,7 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
public void onProximityUpdate(Tile tile){
|
public void onProximityUpdate(Tile tile){
|
||||||
super.onProximityUpdate(tile);
|
super.onProximityUpdate(tile);
|
||||||
|
|
||||||
TrackEntity entity = tile.ent();
|
CraterConveyorEntity entity = tile.ent();
|
||||||
int[] bits = buildBlending(tile, tile.rotation(), null, true);
|
int[] bits = buildBlending(tile, tile.rotation(), null, true);
|
||||||
entity.blendbits = bits[0];
|
entity.blendbits = bits[0];
|
||||||
entity.blendsclx = bits[1];
|
entity.blendsclx = bits[1];
|
||||||
@@ -100,7 +104,7 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
class TrackEntity extends TileEntity{
|
class CraterConveyorEntity extends TileEntity{
|
||||||
int blendbits;
|
int blendbits;
|
||||||
int blendsclx, blendscly;
|
int blendsclx, blendscly;
|
||||||
|
|
||||||
@@ -132,7 +136,7 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Tile tile){
|
public void draw(Tile tile){
|
||||||
TrackEntity entity = tile.ent();
|
CraterConveyorEntity entity = tile.ent();
|
||||||
byte rotation = tile.rotation();
|
byte rotation = tile.rotation();
|
||||||
|
|
||||||
Draw.rect(regions[Mathf.clamp(entity.blendbits, 0, regions.length - 1)], tile.drawx(), tile.drawy(), tilesize * entity.blendsclx, tilesize * entity.blendscly, rotation * 90);
|
Draw.rect(regions[Mathf.clamp(entity.blendbits, 0, regions.length - 1)], tile.drawx(), tile.drawy(), tilesize * entity.blendsclx, tilesize * entity.blendscly, rotation * 90);
|
||||||
@@ -145,7 +149,12 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawLayer(Tile tile){
|
public void drawLayer(Tile tile){
|
||||||
TrackEntity entity = tile.ent();
|
CraterConveyorEntity entity = tile.ent();
|
||||||
|
|
||||||
|
// fixme: use this for debugging
|
||||||
|
Lines.stroke(1f, entity.isSleeping() ? Color.red : Color.green);
|
||||||
|
Lines.circle(tile.drawx() + (tilesize/3f), tile.drawy() - (tilesize/3f), 0.5f);
|
||||||
|
Draw.reset();
|
||||||
|
|
||||||
// no from == no crater
|
// no from == no crater
|
||||||
if(entity.from == Pos.invalid) return;
|
if(entity.from == Pos.invalid) return;
|
||||||
@@ -177,7 +186,7 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Tile tile){
|
public void update(Tile tile){
|
||||||
TrackEntity entity = tile.ent();
|
CraterConveyorEntity entity = tile.ent();
|
||||||
|
|
||||||
// only update once per frame
|
// only update once per frame
|
||||||
if(entity.lastFrameUpdated == Core.graphics.getFrameId()) return;
|
if(entity.lastFrameUpdated == Core.graphics.getFrameId()) return;
|
||||||
@@ -188,13 +197,17 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
// ensure a crater exists below this block
|
// ensure a crater exists below this block
|
||||||
if(entity.from == Pos.invalid){
|
if(entity.from == Pos.invalid){
|
||||||
// poof in crater
|
// poof in crater
|
||||||
if(entity.items.total() <= 0 || entity.reload > 0) return;
|
if(entity.items.total() <= 0 || entity.reload > 0){
|
||||||
|
entity.sleep();
|
||||||
|
return;
|
||||||
|
}
|
||||||
Effects.effect(Fx.plasticburn, tile.drawx(), tile.drawy());
|
Effects.effect(Fx.plasticburn, tile.drawx(), tile.drawy());
|
||||||
entity.from = tile.pos();
|
entity.from = tile.pos();
|
||||||
}else{
|
}else{
|
||||||
// poof out crater
|
// poof out crater
|
||||||
if(entity.items.total() == 0){
|
if(entity.items.total() == 0){
|
||||||
entity.from = Pos.invalid;
|
entity.from = Pos.invalid;
|
||||||
|
entity.sleep();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -216,13 +229,14 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
if(!(destination.block() instanceof CraterConveyor) && (entity.from != tile.pos() || !((entity.snekbit & head) == head))){ // ...and if its not a crater conveyor, start unloading (everything)
|
if(!(destination.block() instanceof CraterConveyor) && (entity.from != tile.pos() || !((entity.snekbit & head) == head))){ // ...and if its not a crater conveyor, start unloading (everything)
|
||||||
while(entity.items.total() > 0 && entity.items.first() != null && offloadDir(tile, entity.items.first())) entity.items.remove(entity.items.first(), 1);
|
while(entity.items.total() > 0 && entity.items.first() != null && offloadDir(tile, entity.items.first())) entity.items.remove(entity.items.first(), 1);
|
||||||
if(entity.items.total() == 0) Effects.effect(Fx.plasticburn, tile.drawx(), tile.drawy());
|
if(entity.items.total() == 0) Effects.effect(Fx.plasticburn, tile.drawx(), tile.drawy());
|
||||||
|
if(entity.items.total() == 0) bump(tile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// when basically exactly on the center:
|
// when basically exactly on the center:
|
||||||
if(entity.reload == 0){
|
if(entity.reload == 0){
|
||||||
if(destination.block() instanceof CraterConveyor){
|
if(destination.block() instanceof CraterConveyor){
|
||||||
TrackEntity e = destination.ent();
|
CraterConveyorEntity e = destination.ent();
|
||||||
|
|
||||||
// check if next crater conveyor is not occupied
|
// check if next crater conveyor is not occupied
|
||||||
if(e.items.total() == 0){
|
if(e.items.total() == 0){
|
||||||
@@ -237,6 +251,11 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
// transfer inventory of conveyor
|
// transfer inventory of conveyor
|
||||||
e.items.addAll(entity.items);
|
e.items.addAll(entity.items);
|
||||||
entity.items.clear();
|
entity.items.clear();
|
||||||
|
|
||||||
|
e.noSleep();
|
||||||
|
bump(tile);
|
||||||
|
}else{
|
||||||
|
entity.sleep();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,7 +264,7 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean acceptItem(Item item, Tile tile, Tile source){
|
public boolean acceptItem(Item item, Tile tile, Tile source){
|
||||||
TrackEntity entity = tile.ent();
|
CraterConveyorEntity entity = tile.ent();
|
||||||
|
|
||||||
if(!((entity.snekbit & head) == head) && !(source.block() instanceof CraterConveyor)) return false;
|
if(!((entity.snekbit & head) == head) && !(source.block() instanceof CraterConveyor)) return false;
|
||||||
if(entity.items.total() > 0 && !entity.items.has(item)) return false;
|
if(entity.items.total() > 0 && !entity.items.has(item)) return false;
|
||||||
@@ -255,10 +274,18 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleItem(Item item, Tile tile, Tile source){
|
||||||
|
super.handleItem(item, tile, source);
|
||||||
|
|
||||||
|
tile.entity.noSleep();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int removeStack(Tile tile, Item item, int amount){
|
public int removeStack(Tile tile, Item item, int amount){
|
||||||
int i = super.removeStack(tile, item, amount);
|
int i = super.removeStack(tile, item, amount);
|
||||||
if(tile.entity.items.total() == 0) Effects.effect(Fx.plasticburn, tile.drawx(), tile.drawy());
|
if(tile.entity.items.total() == 0) Effects.effect(Fx.plasticburn, tile.drawx(), tile.drawy());
|
||||||
|
if(tile.entity.items.total() == 0) bump(tile);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +295,7 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean shouldLaunch(Tile tile){
|
public boolean shouldLaunch(Tile tile){
|
||||||
TrackEntity entity = tile.ent();
|
CraterConveyorEntity entity = tile.ent();
|
||||||
|
|
||||||
// its not a start tile so it should be moving
|
// its not a start tile so it should be moving
|
||||||
if(!((entity.snekbit & head) == head)) return true;
|
if(!((entity.snekbit & head) == head)) return true;
|
||||||
@@ -286,12 +313,15 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
|
|
||||||
// has no crater conveyors facing into it
|
// has no crater conveyors facing into it
|
||||||
private boolean isStart(Tile tile){
|
private boolean isStart(Tile tile){
|
||||||
Tile[] inputs = new Tile[]{tile.back(), tile.left(), tile.right()};
|
return inputs(tile).isEmpty();
|
||||||
for(Tile input : inputs){
|
|
||||||
if(input != null && input.getTeam() == tile.getTeam() && input.block() instanceof CraterConveyor && input.front() == tile) return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
private Array<Tile> inputs(Tile tile){
|
||||||
|
tmptiles.clear();
|
||||||
|
for(Tile input : new Tile[]{tile.back(), tile.left(), tile.right()}){
|
||||||
|
if(input != null && input.getTeam() == tile.getTeam() && input.block() instanceof CraterConveyor && input.front() == tile) tmptiles.add(input);
|
||||||
|
}
|
||||||
|
return tmptiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
// has no crater conveyor in front of it
|
// has no crater conveyor in front of it
|
||||||
@@ -307,4 +337,10 @@ public class CraterConveyor extends Block implements Autotiler{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// awaken inputting conveyors
|
||||||
|
private void bump(Tile tile){
|
||||||
|
for(Tile input : inputs(tile)){
|
||||||
|
if(input.entity.isSleeping()) input.entity.noSleep();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user