Implemented multiblock output, broke router chaining

This commit is contained in:
Anuken 2018-03-01 23:36:48 -05:00
parent 59e8a85c7e
commit 5faff6260e
14 changed files with 550 additions and 442 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 296 B

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 85 KiB

View File

@ -1,7 +1,7 @@
#Autogenerated file. Do not modify.
#Wed Feb 28 23:04:20 EST 2018
#Thu Mar 01 23:34:49 EST 2018
version=release
androidBuildCode=315
androidBuildCode=317
name=Mindustry
code=3.4
build=custom build

View File

@ -186,7 +186,9 @@ public class NetworkIO {
}
if(tile.entity != null){
stream.writeShort(tile.getPackedData());
stream.writeByte(tile.getRotation());
stream.writeByte(tile.getDump());
stream.writeByte(tile.getExtra());
stream.writeShort((short)tile.entity.health); //health
//items
@ -317,11 +319,12 @@ public class NetworkIO {
}
if(tile.entity != null){
short data = stream.readShort();
tile.setRotation(stream.readByte());
tile.setDump(stream.readByte());
tile.setExtra(stream.readByte());
short health = stream.readShort();
tile.entity.health = health;
tile.setPackedData(data);
for(int j = 0; j < tile.entity.items.length; j ++){
tile.entity.items[j] = stream.readInt();

View File

@ -26,6 +26,7 @@ public class Recipes {
new Recipe(distribution, DistributionBlocks.steelconveyor, stack(Item.steel, 1)),
new Recipe(distribution, DistributionBlocks.pulseconveyor, stack(Item.dirium, 1)),
new Recipe(distribution, DistributionBlocks.router, stack(Item.stone, 2)),
new Recipe(distribution, DistributionBlocks.vault, stack(Item.iron, 8)),
new Recipe(distribution, DistributionBlocks.junction, stack(Item.iron, 2)),
new Recipe(distribution, DistributionBlocks.tunnel, stack(Item.iron, 2)),
new Recipe(distribution, DistributionBlocks.sorter, stack(Item.steel, 2)),

View File

@ -2,6 +2,7 @@ package io.anuke.mindustry.world;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
@ -182,61 +183,52 @@ public class Block{
* Tries to put this item into a nearby container, if there are no available
* containers, it gets added to the block's inventory.*/
public void offloadNear(Tile tile, Item item){
if(Net.client() && syncBlockState){
handleItem(item, tile, tile);
return;
}
byte i = tile.getDump();
byte pdump = (byte)(i % 4);
GridPoint2[] nearby = Edges.getEdges(width);
for(int j = 0; j < 4; j ++){
Tile other = tile.getNearby(i);
if(other != null && other.block().acceptItem(item, other, tile)){
other.block().handleItem(item, other, tile);
tile.setDump((byte)((i+1)%4));
if(Net.server() && syncBlockState) NetEvents.handleTransfer(tile, i, item);
for(int j = 0; j < nearby.length; j ++){
Tile other = tile.getNearby(nearby[j]);
Tile in = tile.getNearby(Edges.getInsideEdges(width)[j]);
if(other != null && other.block().acceptItem(item, other, in)){
other.block().handleItem(item, other, in);
return;
}
i++;
i %= 4;
}
tile.setDump(pdump);
handleItem(item, tile, tile);
}
/** Try dumping any item near the tile. */
/**Try dumping any item near the tile.*/
protected boolean tryDump(Tile tile){
return tryDump(tile, -1, null);
return tryDump(tile, null);
}
/**
* Try dumping any item near the tile. -1 = any direction
*/
protected boolean tryDump(Tile tile, int direction, Item todump){
if(Net.client() && syncBlockState) return false;
int i = tile.getDump()%4;
/**Try dumping a specific item near the tile.*/
protected boolean tryDump(Tile tile, Item todump){
GridPoint2[] nearby = Edges.getEdges(width);
byte i = (byte)(tile.getDump() % nearby.length);
for(int j = 0; j < 4; j ++){
Tile other = tile.getNearby(i);
if(i == direction || direction == -1){
for(Item item : Item.getAllItems()){
if(todump != null && item != todump) continue;
if(tile.entity.hasItem(item) && other != null && other.block().acceptItem(item, other, tile)){
other.block().handleItem(item, other, tile);
tile.entity.removeItem(item, 1);
tile.setDump((byte)((i+1)%4));
if(Net.server() && syncBlockState) NetEvents.handleTransfer(tile, (byte)i, item);
return true;
}
for(int j = 0; j < nearby.length; j ++){
Tile other;
Tile in;
for(Item item : Item.getAllItems()){
other = tile.getNearby(nearby[i]);
in = tile.getNearby(Edges.getInsideEdges(width)[i]);
if(todump != null && item != todump) continue;
if(tile.entity.hasItem(item) && other != null && other.block().acceptItem(item, other, in)){
other.block().handleItem(item, other, in);
tile.entity.removeItem(item, 1);
i = (byte)((i + 1) % nearby.length);
tile.setDump(i);
return true;
}
}
i++;
i %= 4;
i = (byte)((i + 1) % nearby.length);
tile.setDump(i);
}
return false;

View File

@ -8,6 +8,8 @@ import java.util.Arrays;
public class Edges {
private static final int maxSize = 11;
private static GridPoint2[][] edges = new GridPoint2[maxSize][0];
private static GridPoint2[][] edgeInside = new GridPoint2[maxSize][0];
private static GridPoint2[][] inside = new GridPoint2[maxSize][0];
static{
@ -30,16 +32,30 @@ public class Edges {
}
Arrays.sort(edges[i], (e1, e2) -> Float.compare(Mathf.atan2(e1.x, e1.y), Mathf.atan2(e2.x, e2.y)));
edgeInside[i] = new GridPoint2[edges[i].length];
for(int j = 0; j < edges[i].length; j ++){
GridPoint2 point = edges[i][j];
edgeInside[i][j] = new GridPoint2(Mathf.clamp(point.x, -(int)((i)/2f), (int)(i/2f + 0.5f)),
Mathf.clamp(point.y, -(int)((i)/2f), (int)(i/2f + 0.5f)));
}
}
}
public static GridPoint2[] getEdges(int size){
public static synchronized GridPoint2[] getEdges(int size){
if(size < 0 || size > maxSize) throw new RuntimeException("Block size must be between 0 and " + maxSize);
return edges[size - 1];
}
public static int getEdgeAmount(int size){
public static synchronized GridPoint2[] getInsideEdges(int size){
if(size < 0 || size > maxSize) throw new RuntimeException("Block size must be between 0 and " + maxSize);
return edgeInside[size - 1];
}
public static synchronized int getEdgeAmount(int size){
return getEdges(size).length;
}
}

View File

@ -37,6 +37,10 @@ public class DistributionBlocks{
router = new Router("router"){{
}},
vault = new Router("vault"){{
width = height = 2;
}},
junction = new Junction("junction"){{

View File

@ -7,7 +7,6 @@ import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.util.Mathf;
public class Router extends Block{
protected final int timerDump = timers++;
@ -20,7 +19,7 @@ public class Router extends Block{
solid = true;
bars.add(new BlockBar(Color.GREEN, true, tile -> (float)tile.entity.totalItems()/capacity));
}
@Override
public void getStats(Array<String> list){
super.getStats(list);
@ -34,19 +33,11 @@ public class Router extends Block{
@Override
public void update(Tile tile){
tile.setRotation((byte)Mathf.mod(tile.getRotation(), 4));
int iterations = Math.max(1, (int) (Timers.delta() + 0.4f));
for(int i = 0; i < iterations; i ++) {
if (tile.entity.totalItems() > 0) {
if (tile.getExtra() != tile.getRotation()
|| Mathf.chance(0.35)) { //sometimes dump backwards at a 0.35 chance... this somehow works?
tryDump(tile, tile.getRotation(), null);
}
tile.setRotation((byte) ((tile.getRotation() + 1) % 4));
tryDump(tile);
}
}
}

View File

@ -77,7 +77,7 @@ public class LiquidCrafter extends LiquidBlock{
}
if(entity.timer.get(timerDump, 15)){
tryDump(tile, -1, output);
tryDump(tile, output);
}
}

View File

@ -66,7 +66,7 @@ public class PowerSmelter extends PowerBlock {
PowerSmelterEntity entity = tile.entity();
if(entity.timer.get(timerDump, 5) && entity.hasItem(result)){
tryDump(tile, -1, result);
tryDump(tile, result);
}
float used = powerDrain * Timers.delta();

View File

@ -61,7 +61,7 @@ public class Smelter extends Block{
CrafterEntity entity = tile.entity();
if(entity.timer.get(timerDump, 5) && entity.hasItem(result)){
tryDump(tile, -1, result);
tryDump(tile, result);
}
//add fuel

View File

@ -7,7 +7,6 @@ import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.util.Mathf;
public class Vault extends Block {
public int capacity;
@ -27,21 +26,11 @@ public class Vault extends Block {
@Override
public void update(Tile tile){
tile.setRotation((byte) Mathf.mod(tile.getRotation(), 4));
int iterations = Math.max(1, (int) (Timers.delta() + 0.4f));
for(int i = 0; i < iterations; i ++) {
if(!canOutput(tile, tile.getNearby(tile.getRotation()))){
tile.setRotation((byte) ((tile.getRotation() + 1) % 4));
}else if (tile.entity.totalItems() > 0) {
if (tile.getExtra() != tile.getRotation()
|| Mathf.chance(0.35)) { //sometimes dump backwards at a 0.35 chance... this somehow works?
tryDump(tile, tile.getRotation(), null);
}
tile.setRotation((byte) ((tile.getRotation() + 1) % 4));
if (tile.entity.totalItems() > 0) { //TODO only output to the right blocks
tryDump(tile);
}
}
}