From 6b57dc8813ef59bda9e0f4292be9aa8f876cc226 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sat, 28 Dec 2019 16:56:26 -0800 Subject: [PATCH] Added serialization and network synchronization for monster entities Added serialization and network synchronization for monster entities Blocked local monster creation when socket is non-null --- .../com/riiablo/net/packet/d2gs/MonsterP.java | 34 +++++++++++++++++++ .../com/riiablo/net/packet/d2gs/SyncData.java | 3 +- .../engine/client/ClientNetworkReceiver.java | 8 +++-- .../engine/server/SerializationManager.java | 6 ++++ .../engine/server/ServerEntityFactory.java | 1 + .../serializer/MonsterSerializer.java | 33 ++++++++++++++++++ core/src/com/riiablo/map/Act1MapBuilder.java | 5 +++ core/src/com/riiablo/net/d2gs/Sync.fbs | 5 +++ 8 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 core/gen/com/riiablo/net/packet/d2gs/MonsterP.java create mode 100644 core/src/com/riiablo/engine/server/component/serializer/MonsterSerializer.java diff --git a/core/gen/com/riiablo/net/packet/d2gs/MonsterP.java b/core/gen/com/riiablo/net/packet/d2gs/MonsterP.java new file mode 100644 index 00000000..7fab77b1 --- /dev/null +++ b/core/gen/com/riiablo/net/packet/d2gs/MonsterP.java @@ -0,0 +1,34 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package com.riiablo.net.packet.d2gs; + +import com.google.flatbuffers.FlatBufferBuilder; +import com.google.flatbuffers.Table; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +@SuppressWarnings("unused") +public final class MonsterP extends Table { + public static MonsterP getRootAsMonsterP(ByteBuffer _bb) { return getRootAsMonsterP(_bb, new MonsterP()); } + public static MonsterP getRootAsMonsterP(ByteBuffer _bb, MonsterP obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; vtable_start = bb_pos - bb.getInt(bb_pos); vtable_size = bb.getShort(vtable_start); } + public MonsterP __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } + + public int monsterId() { int o = __offset(4); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; } + + public static int createMonsterP(FlatBufferBuilder builder, + int monsterId) { + builder.startObject(1); + MonsterP.addMonsterId(builder, monsterId); + return MonsterP.endMonsterP(builder); + } + + public static void startMonsterP(FlatBufferBuilder builder) { builder.startObject(1); } + public static void addMonsterId(FlatBufferBuilder builder, int monsterId) { builder.addShort(0, (short)monsterId, (short)0); } + public static int endMonsterP(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } +} + diff --git a/core/gen/com/riiablo/net/packet/d2gs/SyncData.java b/core/gen/com/riiablo/net/packet/d2gs/SyncData.java index 1a42de0d..047bbfd6 100644 --- a/core/gen/com/riiablo/net/packet/d2gs/SyncData.java +++ b/core/gen/com/riiablo/net/packet/d2gs/SyncData.java @@ -15,8 +15,9 @@ public final class SyncData { public static final byte PlayerP = 8; public static final byte DS1ObjectWrapperP = 9; public static final byte WarpP = 10; + public static final byte MonsterP = 11; - public static final String[] names = { "NONE", "ClassP", "CofComponentsP", "CofTransformsP", "CofAlphasP", "PositionP", "VelocityP", "AngleP", "PlayerP", "DS1ObjectWrapperP", "WarpP", }; + public static final String[] names = { "NONE", "ClassP", "CofComponentsP", "CofTransformsP", "CofAlphasP", "PositionP", "VelocityP", "AngleP", "PlayerP", "DS1ObjectWrapperP", "WarpP", "MonsterP", }; public static String name(int e) { return names[e]; } } diff --git a/core/src/com/riiablo/engine/client/ClientNetworkReceiver.java b/core/src/com/riiablo/engine/client/ClientNetworkReceiver.java index 8eccf736..77a9aab2 100644 --- a/core/src/com/riiablo/engine/client/ClientNetworkReceiver.java +++ b/core/src/com/riiablo/engine/client/ClientNetworkReceiver.java @@ -40,6 +40,7 @@ import com.riiablo.net.packet.d2gs.D2GS; import com.riiablo.net.packet.d2gs.D2GSData; import com.riiablo.net.packet.d2gs.DS1ObjectWrapperP; import com.riiablo.net.packet.d2gs.Disconnect; +import com.riiablo.net.packet.d2gs.MonsterP; import com.riiablo.net.packet.d2gs.PlayerP; import com.riiablo.net.packet.d2gs.PositionP; import com.riiablo.net.packet.d2gs.Sync; @@ -235,9 +236,11 @@ public class ClientNetworkReceiver extends IntervalSystem { String objectType = Riiablo.files.MonPreset.getPlace(ds1ObjectWrapper.act(), ds1ObjectWrapper.id()); MonStats.Entry monstats = Riiablo.files.monstats.get(objectType); return factory.createMonster(monstats, position.x(), position.y()); + } else { + PositionP position = findTable(sync, SyncData.PositionP, new PositionP()); + MonsterP monster = findTable(sync, SyncData.MonsterP, new MonsterP()); + return factory.createMonster(monster.monsterId(), position.x(), position.y()); } - - return Engine.INVALID_ENTITY; } case PLR: { PlayerP player = findTable(sync, SyncData.PlayerP, new PlayerP()); @@ -282,6 +285,7 @@ public class ClientNetworkReceiver extends IntervalSystem { case SyncData.PlayerP: case SyncData.DS1ObjectWrapperP: case SyncData.WarpP: + case SyncData.MonsterP: break; case SyncData.CofComponentsP: { CofComponentsP data = (CofComponentsP) sync.data(new CofComponentsP(), i); diff --git a/core/src/com/riiablo/engine/server/SerializationManager.java b/core/src/com/riiablo/engine/server/SerializationManager.java index 84c4d568..1848ac76 100644 --- a/core/src/com/riiablo/engine/server/SerializationManager.java +++ b/core/src/com/riiablo/engine/server/SerializationManager.java @@ -15,6 +15,7 @@ import com.riiablo.engine.server.component.CofAlphas; import com.riiablo.engine.server.component.CofComponents; import com.riiablo.engine.server.component.CofTransforms; import com.riiablo.engine.server.component.DS1ObjectWrapper; +import com.riiablo.engine.server.component.Monster; import com.riiablo.engine.server.component.Player; import com.riiablo.engine.server.component.Position; import com.riiablo.engine.server.component.Velocity; @@ -26,6 +27,7 @@ import com.riiablo.engine.server.component.serializer.CofComponentsSerializer; import com.riiablo.engine.server.component.serializer.CofTransformsSerializer; import com.riiablo.engine.server.component.serializer.DS1ObjectWrapperSerializer; import com.riiablo.engine.server.component.serializer.FlatBuffersSerializer; +import com.riiablo.engine.server.component.serializer.MonsterSerializer; import com.riiablo.engine.server.component.serializer.PlayerSerializer; import com.riiablo.engine.server.component.serializer.PositionSerializer; import com.riiablo.engine.server.component.serializer.VelocitySerializer; @@ -80,6 +82,7 @@ public class SerializationManager extends PassiveSystem { serializers.put(Player.class, new PlayerSerializer()); serializers.put(DS1ObjectWrapper.class, new DS1ObjectWrapperSerializer()); serializers.put(Warp.class, new WarpSerializer()); + serializers.put(Monster.class, new MonsterSerializer()); deserializers = (Class[]) new Class[SyncData.names.length]; deserializers[SyncData.ClassP] = com.riiablo.engine.server.component.Class.class; @@ -92,6 +95,7 @@ public class SerializationManager extends PassiveSystem { deserializers[SyncData.PlayerP] = Player.class; deserializers[SyncData.DS1ObjectWrapperP] = DS1ObjectWrapper.class; deserializers[SyncData.WarpP] = Warp.class; + deserializers[SyncData.MonsterP] = Monster.class; cm = new ComponentMapper[SyncData.names.length]; cm[SyncData.ClassP] = mClass; @@ -104,6 +108,7 @@ public class SerializationManager extends PassiveSystem { cm[SyncData.PlayerP] = null; cm[SyncData.DS1ObjectWrapperP] = mDS1ObjectWrapper; cm[SyncData.WarpP] = null; + cm[SyncData.MonsterP] = null; } @SuppressWarnings("unchecked") @@ -185,6 +190,7 @@ public class SerializationManager extends PassiveSystem { case SyncData.PlayerP: case SyncData.DS1ObjectWrapperP: case SyncData.WarpP: + case SyncData.MonsterP: break; default: { Class clazz = deserializers[dataType]; diff --git a/core/src/com/riiablo/engine/server/ServerEntityFactory.java b/core/src/com/riiablo/engine/server/ServerEntityFactory.java index f984e07f..ff71bdb7 100644 --- a/core/src/com/riiablo/engine/server/ServerEntityFactory.java +++ b/core/src/com/riiablo/engine/server/ServerEntityFactory.java @@ -176,6 +176,7 @@ public class ServerEntityFactory extends EntityFactory { mInteractable.create(id).set(size, ai); } + mNetworked.create(id); return id; } diff --git a/core/src/com/riiablo/engine/server/component/serializer/MonsterSerializer.java b/core/src/com/riiablo/engine/server/component/serializer/MonsterSerializer.java new file mode 100644 index 00000000..3bda7f0b --- /dev/null +++ b/core/src/com/riiablo/engine/server/component/serializer/MonsterSerializer.java @@ -0,0 +1,33 @@ +package com.riiablo.engine.server.component.serializer; + +import com.google.flatbuffers.FlatBufferBuilder; + +import com.riiablo.engine.server.component.Monster; +import com.riiablo.net.packet.d2gs.MonsterP; +import com.riiablo.net.packet.d2gs.Sync; +import com.riiablo.net.packet.d2gs.SyncData; + +public class MonsterSerializer implements FlatBuffersSerializer { + public static final MonsterP table = new MonsterP(); + + @Override + public byte getDataType() { + return SyncData.MonsterP; + } + + @Override + public int putData(FlatBufferBuilder builder, Monster c) { + return MonsterP.createMonsterP(builder, c.monstats.hcIdx); + } + + @Override + public MonsterP getTable(Sync sync, int j) { + sync.data(table, j); + return table; + } + + @Override + public Monster getData(Sync sync, int j, Monster c) { + throw new UnsupportedOperationException("Not supported!"); + } +} diff --git a/core/src/com/riiablo/map/Act1MapBuilder.java b/core/src/com/riiablo/map/Act1MapBuilder.java index fa85ebb3..e34acdc8 100644 --- a/core/src/com/riiablo/map/Act1MapBuilder.java +++ b/core/src/com/riiablo/map/Act1MapBuilder.java @@ -3,6 +3,7 @@ package com.riiablo.map; import com.artemis.annotations.Wire; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.net.Socket; import com.riiablo.Riiablo; import com.riiablo.codec.excel.Levels; import com.riiablo.codec.excel.LvlPrest; @@ -21,6 +22,9 @@ public enum Act1MapBuilder implements MapBuilder { @Wire(name = "factory") protected EntityFactory factory; + @Wire(name = "client.socket", failOnNull = false) + protected Socket socket; + @Override public void generate(Map map, int seed, int diff) { int def = Map.ACT_DEF[0]; @@ -106,6 +110,7 @@ public enum Act1MapBuilder implements MapBuilder { for (int y = 0; y < zone.gridSizeY; y++, ty++) { // TODO: Zone.index() can be replaced with incrementer zone.getLayer(Map.FLOOR_OFFSET)[Zone.index(zone.tilesX, tx, ty)] = dt1s.get(0); + if (socket != null) continue; if (MathUtils.randomBoolean(SPAWN_MULT * zone.level.MonDen[zone.diff] / 100000f)) { int i = MathUtils.random(monsters.length - 1); MonStats.Entry monster = monsters[i]; diff --git a/core/src/com/riiablo/net/d2gs/Sync.fbs b/core/src/com/riiablo/net/d2gs/Sync.fbs index 13c46314..8c4cffec 100644 --- a/core/src/com/riiablo/net/d2gs/Sync.fbs +++ b/core/src/com/riiablo/net/d2gs/Sync.fbs @@ -11,6 +11,7 @@ union SyncData { PlayerP, DS1ObjectWrapperP, WarpP, + MonsterP, } table Sync { @@ -62,4 +63,8 @@ table DS1ObjectWrapperP { table WarpP { index:int32; +} + +table MonsterP { + monsterId:uint16; } \ No newline at end of file