From 9dbed76a5313ec4cf79cc5921b824ef5c83c4868 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 3 Feb 2018 02:18:17 -0500 Subject: [PATCH] Experimental new block sync system --- .../io/anuke/mindustry/core/NetClient.java | 8 ++++++++ .../io/anuke/mindustry/core/NetServer.java | 12 +++++------ .../src/io/anuke/mindustry/net/NetEvents.java | 12 ++++++++++- core/src/io/anuke/mindustry/net/Packets.java | 20 +++++++++++++++++++ .../io/anuke/mindustry/net/Registrator.java | 1 + core/src/io/anuke/mindustry/world/Block.java | 7 +++++++ core/src/io/anuke/mindustry/world/Tile.java | 8 ++++++++ 7 files changed, 61 insertions(+), 7 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/NetClient.java b/core/src/io/anuke/mindustry/core/NetClient.java index 8f4d918804..6f2fcbf21f 100644 --- a/core/src/io/anuke/mindustry/core/NetClient.java +++ b/core/src/io/anuke/mindustry/core/NetClient.java @@ -15,6 +15,7 @@ import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.Net.SendMode; import io.anuke.mindustry.net.NetworkIO; import io.anuke.mindustry.net.Packets.*; +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; @@ -263,6 +264,13 @@ public class NetClient extends Module { }); Net.handleClient(FriendlyFireChangePacket.class, packet -> state.friendlyFire = packet.enabled); + + Net.handleClient(ItemTransferPacket.class, packet -> { + Tile tile = world.tile(packet.position); + Tile next = tile.getNearby(packet.rotation); + tile.entity.items[packet.itemid] --; + next.block().handleItem(Item.getByID(packet.itemid), next, tile); + }); } @Override diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index 8368fb6f03..4e841a7dfc 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -1,6 +1,9 @@ package io.anuke.mindustry.core; -import com.badlogic.gdx.utils.*; +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 io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.entities.Player; import io.anuke.mindustry.entities.SyncEntity; @@ -8,7 +11,6 @@ import io.anuke.mindustry.game.EventType.GameOverEvent; import io.anuke.mindustry.io.Platform; 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; @@ -21,7 +23,6 @@ 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 java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -288,8 +289,7 @@ public class NetServer extends Module{ Net.send(packet, SendMode.udp); } - - + /* if(Timers.get("serverBlockSync", blockSyncTime)){ Array connections = Net.getConnections(); @@ -304,7 +304,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){ diff --git a/core/src/io/anuke/mindustry/net/NetEvents.java b/core/src/io/anuke/mindustry/net/NetEvents.java index 6974932fe4..b794c1461e 100644 --- a/core/src/io/anuke/mindustry/net/NetEvents.java +++ b/core/src/io/anuke/mindustry/net/NetEvents.java @@ -6,12 +6,14 @@ import io.anuke.mindustry.entities.TileEntity; import io.anuke.mindustry.entities.enemies.Enemy; import io.anuke.mindustry.net.Net.SendMode; import io.anuke.mindustry.net.Packets.*; +import io.anuke.mindustry.resource.Item; import io.anuke.mindustry.resource.Weapon; import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Tile; import io.anuke.ucore.entities.Entity; -import static io.anuke.mindustry.Vars.*; +import static io.anuke.mindustry.Vars.netCommon; +import static io.anuke.mindustry.Vars.ui; public class NetEvents { @@ -127,4 +129,12 @@ public class NetEvents { packet.y = (short)y; Net.send(packet, SendMode.tcp); } + + public static void handleTransfer(Tile tile, byte rotation, Item item){ + ItemTransferPacket packet = new ItemTransferPacket(); + packet.position = tile.packedPosition(); + packet.rotation = rotation; + packet.itemid = (byte)item.id; + Net.send(packet, SendMode.udp); + } } diff --git a/core/src/io/anuke/mindustry/net/Packets.java b/core/src/io/anuke/mindustry/net/Packets.java index 143a953712..b095f4fa0c 100644 --- a/core/src/io/anuke/mindustry/net/Packets.java +++ b/core/src/io/anuke/mindustry/net/Packets.java @@ -500,4 +500,24 @@ public class Packets { @Override public void read(ByteBuffer buffer) { } } + + public static class ItemTransferPacket implements Packet{ + public int position; + public byte rotation; + public byte itemid; + + @Override + public void write(ByteBuffer buffer) { + buffer.putInt(position); + buffer.put(rotation); + buffer.put(itemid); + } + + @Override + public void read(ByteBuffer buffer) { + position = buffer.getInt(); + rotation = buffer.get(); + itemid = buffer.get(); + } + } } diff --git a/core/src/io/anuke/mindustry/net/Registrator.java b/core/src/io/anuke/mindustry/net/Registrator.java index 9f4d197bb1..b1544ffc27 100644 --- a/core/src/io/anuke/mindustry/net/Registrator.java +++ b/core/src/io/anuke/mindustry/net/Registrator.java @@ -38,6 +38,7 @@ public class Registrator { CustomMapPacket.class, MapAckPacket.class, EntitySpawnPacket.class, + ItemTransferPacket.class }; private static ObjectIntMap> ids = new ObjectIntMap<>(); diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index bbe92ae649..16527b8c81 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.utils.ObjectMap; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.entities.TileEntity; import io.anuke.mindustry.graphics.Fx; +import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.NetEvents; import io.anuke.mindustry.resource.Item; import io.anuke.mindustry.resource.ItemStack; @@ -173,6 +174,8 @@ 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.*/ protected void offloadNear(Tile tile, Item item){ + if(Net.client()) return; + byte i = tile.getDump(); byte pdump = (byte)(i % 4); @@ -183,6 +186,7 @@ public class Block{ if(other != null && other.block().acceptItem(item, other, tile)){ other.block().handleItem(item, other, tile); tile.setDump((byte)((i+1)%4)); + if(Net.server()) NetEvents.handleTransfer(tile, i, item); return; } i++; @@ -201,6 +205,8 @@ public class Block{ * Try dumping any item near the tile. -1 = any direction */ protected boolean tryDump(Tile tile, int direction, Item todump){ + if(Net.client()) return false; + int i = tile.getDump()%4; Tile[] tiles = tile.getNearby(temptiles); @@ -217,6 +223,7 @@ public class Block{ other.block().handleItem(item, other, tile); tile.entity.removeItem(item, 1); tile.setDump((byte)((i+1)%4)); + if(Net.server()) NetEvents.handleTransfer(tile, (byte)i, item); return true; } } diff --git a/core/src/io/anuke/mindustry/world/Tile.java b/core/src/io/anuke/mindustry/world/Tile.java index 2a3e5625a4..83234d0cff 100644 --- a/core/src/io/anuke/mindustry/world/Tile.java +++ b/core/src/io/anuke/mindustry/world/Tile.java @@ -229,6 +229,14 @@ public class Tile{ return world.tile(x - (dx - 8), y - (dy - 8)); } } + + public Tile getNearby(int rotation){ + if(rotation == 0) return world.tile(x + 1, y); + if(rotation == 1) return world.tile(x, y + 1); + if(rotation == 2) return world.tile(x - 1, y); + if(rotation == 3) return world.tile(x, y - 1); + return null; + } public Tile[] getNearby(){ return world.getNearby(x, y);