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
This commit is contained in:
Collin Smith 2019-12-28 16:56:26 -08:00
parent dd6dbca0f6
commit 6b57dc8813
8 changed files with 92 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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]; }
}

View File

@ -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);

View File

@ -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<? extends Component>[]) 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<? extends Component> clazz = deserializers[dataType];

View File

@ -176,6 +176,7 @@ public class ServerEntityFactory extends EntityFactory {
mInteractable.create(id).set(size, ai);
}
mNetworked.create(id);
return id;
}

View File

@ -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<Monster, MonsterP> {
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!");
}
}

View File

@ -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];

View File

@ -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;
}