Rewrote parts of Junction and Tunnel to use custom scheduling
This commit is contained in:
@@ -189,6 +189,7 @@ public class Save15 extends SaveFileVersion {
|
||||
int blockid = stream.readInt();
|
||||
|
||||
Tile tile = world.tile(pos % world.width(), pos / world.width());
|
||||
|
||||
tile.setBlock(map.get(blockid));
|
||||
|
||||
if(blockid == Blocks.blockpart.id){
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.io.IOException;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class Net{
|
||||
public static final int version = 15;
|
||||
public static final int version = 17;
|
||||
|
||||
private static boolean server;
|
||||
private static boolean active;
|
||||
|
||||
@@ -59,7 +59,7 @@ public class Tile{
|
||||
}
|
||||
|
||||
/**Return relative rotation to a coordinate. Returns -1 if the coordinate is not near this tile.*/
|
||||
public int relativeTo(int cx, int cy){
|
||||
public byte relativeTo(int cx, int cy){
|
||||
if(x == cx && y == cy - 1) return 1;
|
||||
if(x == cx && y == cy + 1) return 3;
|
||||
if(x == cx - 1 && y == cy) return 0;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package io.anuke.mindustry.world.blocks.types.distribution;
|
||||
|
||||
import io.anuke.mindustry.entities.TileEntity;
|
||||
import io.anuke.mindustry.resource.Item;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.util.Bits;
|
||||
|
||||
public class Junction extends Block{
|
||||
float speed = 20; //frames taken to go through this junction
|
||||
|
||||
public Junction(String name) {
|
||||
super(name);
|
||||
@@ -20,22 +23,52 @@ public class Junction extends Block{
|
||||
|
||||
@Override
|
||||
public void handleItem(Item item, Tile tile, Tile source){
|
||||
int dir = source.relativeTo(tile.x, tile.y);
|
||||
Tile to = tile.getNearby(temptiles)[dir];
|
||||
|
||||
Timers.run(30, () -> {
|
||||
if(to == null) return;
|
||||
to.block().handleItem(item, to, tile);
|
||||
});
|
||||
|
||||
JunctionEntity entity = tile.entity();
|
||||
entity.items[entity.index ++] = Bits.packInt((short)item.id, source.relativeTo(tile.x, tile.y));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Tile tile){
|
||||
JunctionEntity entity = tile.entity();
|
||||
|
||||
if(entity.index > 0){
|
||||
entity.time += Timers.delta();
|
||||
if(entity.time >= speed){
|
||||
int i = entity.items[-- entity.index];
|
||||
|
||||
int item = Bits.getLeftShort(i);
|
||||
int direction = Bits.getRightShort(i);
|
||||
|
||||
Tile target = tile.getNearby(direction);
|
||||
|
||||
target.block().handleItem(Item.getByID(item), target, tile);
|
||||
|
||||
entity.time = 0f;
|
||||
}
|
||||
}else{
|
||||
entity.time = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptItem(Item item, Tile tile, Tile source){
|
||||
JunctionEntity entity = tile.entity();
|
||||
|
||||
if(entity.index >= entity.items.length - 1) return false;
|
||||
int dir = source.relativeTo(tile.x, tile.y);
|
||||
if(dir == -1) return false;
|
||||
Tile to = tile.getNearby(temptiles)[dir];
|
||||
Tile to = tile.getNearby(dir);
|
||||
return to != null && to.block().acceptItem(item, to, tile);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TileEntity getEntity() {
|
||||
return new JunctionEntity();
|
||||
}
|
||||
|
||||
class JunctionEntity extends TileEntity{
|
||||
int[] items = new int[16]; //16 item buffer
|
||||
int index;
|
||||
float time;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import io.anuke.mindustry.entities.TileEntity;
|
||||
import io.anuke.mindustry.resource.Item;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
@@ -18,7 +19,7 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Sorter extends Junction{
|
||||
public class Sorter extends Block{
|
||||
|
||||
public Sorter(String name) {
|
||||
super(name);
|
||||
@@ -35,6 +36,11 @@ public class Sorter extends Junction{
|
||||
|
||||
Draw.rect(Tmp.tr1, tile.worldx(), tile.worldy(), 4f, 4f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReplace(Block other){
|
||||
return other instanceof Conveyor || other instanceof Router;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptItem(Item item, Tile tile, Tile source){
|
||||
|
||||
@@ -4,9 +4,10 @@ import io.anuke.mindustry.resource.Item;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.util.Bits;
|
||||
import io.anuke.ucore.util.Log;
|
||||
|
||||
public class TunnelConveyor extends Block{
|
||||
public class TunnelConveyor extends Junction{
|
||||
protected int maxdist = 3;
|
||||
|
||||
protected TunnelConveyor(String name) {
|
||||
@@ -15,6 +16,7 @@ public class TunnelConveyor extends Block{
|
||||
update = true;
|
||||
solid = true;
|
||||
health = 70;
|
||||
speed = 25;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,6 +43,32 @@ public class TunnelConveyor extends Block{
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Tile tile){
|
||||
JunctionEntity entity = tile.entity();
|
||||
|
||||
if(entity.index > 0){
|
||||
entity.time += Timers.delta();
|
||||
if(entity.time >= speed){
|
||||
int i = entity.items[-- entity.index];
|
||||
entity.time = 0f;
|
||||
|
||||
int itemid = Bits.getLeftShort(i);
|
||||
|
||||
Item item = Item.getByID(itemid);
|
||||
|
||||
Tile tunnel = getDestTunnel(tile, item);
|
||||
if(tunnel == null) return;
|
||||
Tile target = tunnel.getNearby(tunnel.getRotation());
|
||||
if(target == null) return;
|
||||
|
||||
target.block().handleItem(item, target, tunnel);
|
||||
}
|
||||
}else{
|
||||
entity.time = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptItem(Item item, Tile tile, Tile source){
|
||||
int rot = source.relativeTo(tile.x, tile.y);
|
||||
@@ -68,12 +96,4 @@ public class TunnelConveyor extends Block{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Tile getOther(Tile tile, int dir, int amount){
|
||||
for(int i = 0; i < amount; i ++){
|
||||
if(tile == null) return null;
|
||||
tile = tile.getNearby()[dir];
|
||||
}
|
||||
return tile;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user