Fixed incorrect mode selection, updated TODO

This commit is contained in:
Anuken
2018-02-14 17:50:44 -05:00
parent c03619c5de
commit 008889966f
12 changed files with 99 additions and 73 deletions

View File

@ -20,6 +20,7 @@ import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.Map;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.ProductionBlocks;
import io.anuke.mindustry.world.blocks.types.distribution.Teleporter;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.BaseBulletType;
import io.anuke.ucore.entities.Entities;
@ -226,14 +227,7 @@ public class NetClient extends Module {
while (stream.available() > 0) {
int pos = stream.readInt();
//TODO what if there's no entity? new code
Tile tile = world.tile(pos % world.width(), pos / world.width());
byte times = stream.readByte();
for (int i = 0; i < times; i++) {
tile.entity.timer.getTimes()[i] = stream.readFloat();
}
Tile tile = world.tile(pos);
short data = stream.readShort();
tile.setPackedData(data);
@ -286,6 +280,9 @@ public class NetClient extends Module {
Tile next = tile.getNearby(packet.rotation);
tile.entity.items[packet.itemid] --;
next.block().handleItem(Item.getByID(packet.itemid), next, tile);
if(tile.block() instanceof Teleporter)
Log.info("Recieved dump for teleporter! items: {0}", tile.entity.totalItems());
};
if(threads.isEnabled()){
@ -308,6 +305,21 @@ public class NetClient extends Module {
r.run();
}
});
Net.handleClient(ItemOffloadPacket.class, packet -> {
Runnable r = () -> {
Tile tile = world.tile(packet.position);
if (tile == null || tile.entity == null) return;
Tile next = tile.getNearby(tile.getRotation());
next.block().handleItem(Item.getByID(packet.itemid), next, tile);
};
if(threads.isEnabled()){
threads.run(r);
}else{
r.run();
}
});
}
@Override

View File

@ -1,9 +1,6 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.utils.ByteArray;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.*;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.SyncEntity;
@ -12,6 +9,7 @@ import io.anuke.mindustry.io.Platform;
import io.anuke.mindustry.io.Version;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.NetConnection;
import io.anuke.mindustry.net.NetworkIO;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.resource.Upgrade;
@ -24,6 +22,7 @@ import io.anuke.ucore.entities.Entities;
import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.modules.Module;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Timer;
import java.io.ByteArrayInputStream;
@ -36,9 +35,11 @@ import static io.anuke.mindustry.Vars.*;
public class NetServer extends Module{
private final static float serverSyncTime = 4, itemSyncTime = 10, blockSyncTime = 120;
private final static boolean sendBlockSync = false;
private final static int timerEntitySync = 0;
private final static int timerStateSync = 1;
private final static int timerBlockSync = 2;
/**Maps connection IDs to players.*/
private IntMap<Player> connections = new IntMap<>();
@ -301,8 +302,8 @@ public class NetServer extends Module{
Net.send(packet, SendMode.udp);
}
/*
if(Timers.get("serverBlockSync", blockSyncTime)){
if(sendBlockSync && timer.get(timerBlockSync, blockSyncTime)){
Array<NetConnection> connections = Net.getConnections();
@ -316,7 +317,7 @@ public class NetServer extends Module{
int h = 16;
sendBlockSync(id, x, y, w, h);
}
}*/
}
}
public void sendBlockSync(int client, int x, int y, int viewx, int viewy){
@ -334,29 +335,17 @@ public class NetServer extends Module{
for (int ry = -viewy / 2; ry <= viewy / 2; ry++) {
Tile tile = world.tile(x + rx, y + ry);
if (tile == null || tile.entity == null) continue;
if (tile == null || tile.entity == null || !tile.block().syncEntity()) continue;
stream.writeInt(tile.packedPosition());
byte times = 0;
for(; times < tile.entity.timer.getTimes().length; times ++){
if(tile.entity.timer.getTimes()[times] <= 1f){
break;
}
}
stream.writeByte(times);
for(int i = 0; i < times; i ++){
stream.writeFloat(tile.entity.timer.getTimes()[i]);
}
stream.writeShort(tile.getPackedData());
tile.entity.write(stream);
}
}
Log.info("Sent {0} bytes of block data.", stream.size());
}catch (IOException e){
throw new RuntimeException(e);
}

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
@ -429,7 +430,7 @@ public class Renderer extends RendererModule{
Draw.reset();
//draw selected block health
//draw selected block bars and info
if(input.recipe == null && !ui.hasMouse()){
Tile tile = world.tileWorld(Graphics.mouseWorld().x, Graphics.mouseWorld().y);
@ -444,9 +445,6 @@ public class Renderer extends RendererModule{
Draw.color();
}
//if(target.entity != null)
// drawHealth(target.drawx(), target.drawy() - 3f - target.block().height / 2f * tilesize, target.entity.health, target.entity.tile.block().health);
if(target.entity != null) {
int bot = 0, top = 0;
for (BlockBar bar : target.block().bars) {
@ -455,7 +453,7 @@ public class Renderer extends RendererModule{
float value = bar.value.get(target);
if(value <= -1f) continue;
if(MathUtils.isEqual(value, -1f)) continue;
drawBar(bar.color, target.drawx(), target.drawy() + offset, value);

View File

@ -144,4 +144,11 @@ public class NetEvents {
packet.itemid = (byte)item.id;
Net.send(packet, SendMode.udp);
}
public static void handleOffload(Tile tile, Item item){
ItemOffloadPacket packet = new ItemOffloadPacket();
packet.position = tile.packedPosition();
packet.itemid = (byte)item.id;
Net.send(packet, SendMode.udp);
}
}

View File

@ -523,7 +523,24 @@ public class Packets {
}
}
public static class ItemAddPacket implements Packet{
public static class ItemAddPacket implements Packet, UnimportantPacket{
public int position;
public byte itemid;
@Override
public void write(ByteBuffer buffer) {
buffer.putInt(position);
buffer.put(itemid);
}
@Override
public void read(ByteBuffer buffer) {
position = buffer.getInt();
itemid = buffer.get();
}
}
public static class ItemOffloadPacket implements Packet{
public int position;
public byte itemid;

View File

@ -40,6 +40,7 @@ public class Registrator {
EntitySpawnPacket.class,
ItemTransferPacket.class,
ItemAddPacket.class,
ItemOffloadPacket.class
};
private static ObjectIntMap<Class<?>> ids = new ObjectIntMap<>();

View File

@ -50,11 +50,11 @@ public class LevelDialog extends FloatingDialog{
selmode.add("$text.level.mode").padRight(15f);
for(GameMode mode : GameMode.values()){
TextButton b = Elements.newButton("$mode."+mode.name()+".name", "toggle", ()->{
state.mode = mode;
});
group.add(b);
selmode.add(b).size(130f, 54f);
TextButton[] b = {null};
b[0] = Elements.newButton("$mode." + mode.name() + ".name", "toggle", () -> state.mode = mode);
b[0].update(() -> b[0].setChecked(state.mode == mode));
group.add(b[0]);
selmode.add(b[0]).size(130f, 54f);
}
content().add(selmode);

View File

@ -172,6 +172,10 @@ public class Block{
return new TileEntity();
}
public boolean syncEntity(){
return true;
}
/**
* Tries to put this item into a nearby container, if there are no available
* containers, it gets added to the block's inventory.*/
@ -237,7 +241,7 @@ public class Block{
}
/**
* Try offloading an item to a nearby container. Returns true if success.
* Try offloading an item to a nearby container in its facing direction. Returns true if success.
*/
protected boolean offloadDir(Tile tile, Item item){
Tile other = tile.getNearby(tile.getRotation());

View File

@ -72,17 +72,23 @@ public class Conveyor extends Block{
byte rotation = tile.getRotation();
for(int i = 0; i < entity.convey.size; i ++){
ItemPos pos = drawpos.set(entity.convey.get(i), ItemPos.drawShorts);
try {
if(pos.item == null) continue;
for (int i = 0; i < entity.convey.size; i++) {
ItemPos pos = drawpos.set(entity.convey.get(i), ItemPos.drawShorts);
tr1.trns(rotation * 90, tilesize, 0);
tr2.trns(rotation * 90, -tilesize / 2, pos.x*tilesize/2);
if (pos.item == null) continue;
Draw.rect(pos.item.region,
tile.x * tilesize + tr1.x * pos.y + tr2.x,
tile.y * tilesize + tr1.y * pos.y + tr2.y, itemSize, itemSize);
tr1.trns(rotation * 90, tilesize, 0);
tr2.trns(rotation * 90, -tilesize / 2, pos.x * tilesize / 2);
Draw.rect(pos.item.region,
tile.x * tilesize + tr1.x * pos.y + tr2.x,
tile.y * tilesize + tr1.y * pos.y + tr2.y, itemSize, itemSize);
}
}catch (IndexOutOfBoundsException e){
Log.err(e);
}
}
@ -94,7 +100,7 @@ public class Conveyor extends Block{
int minremove = Integer.MAX_VALUE;
for(int i = 0; i < entity.convey.size; i ++){
for(int i = entity.convey.size - 1; i >= 0; i --){
long value = entity.convey.get(i);
ItemPos pos = pos1.set(value, ItemPos.updateShorts);
@ -171,6 +177,11 @@ public class Conveyor extends Block{
}
}
@Override
public boolean syncEntity(){
return false;
}
/**
* Conveyor data format:
* [0] item ordinal

View File

@ -7,6 +7,7 @@ import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.NetEvents;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.world.BlockBar;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.types.PowerBlock;
import io.anuke.ucore.core.Timers;
@ -46,6 +47,8 @@ public class Teleporter extends PowerBlock{
solid = true;
health = 80;
powerCapacity = 30f;
bars.add(new BlockBar(Color.RED, true, tile -> tile.entity.totalItems() / 4f));
}
@Override
@ -135,7 +138,6 @@ public class Teleporter extends PowerBlock{
Array<Tile> links = findLinks(tile);
if(links.size > 0){
if(Net.server() || !Net.active()){
Tile target = links.random();
target.entity.addItem(item, 1);
@ -169,7 +171,7 @@ public class Teleporter extends PowerBlock{
if(other.block() instanceof Teleporter){
if(other.<TeleporterEntity>entity().color != entity.color){
removal.add(other);
}else if(other.entity.totalItems() == 0){
}else if(other.entity.totalItems() <= 0){
returns.add(other);
}
}else{