mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-02-21 20:18:14 +07:00
First iteration of deleting networked entities
This iteration assumes removing Networked component implies deletion Deletion is managed as a flag within the EntitySync packet header
This commit is contained in:
parent
7aa3f448ee
commit
7404ea63a6
@ -2,10 +2,11 @@
|
||||
|
||||
package com.riiablo.net.packet.d2gs;
|
||||
|
||||
import java.nio.*;
|
||||
import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import com.google.flatbuffers.Table;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class EntitySync extends Table {
|
||||
@ -16,33 +17,37 @@ public final class EntitySync extends Table {
|
||||
|
||||
public int entityId() { int o = __offset(4); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
|
||||
public int type() { int o = __offset(6); return o != 0 ? bb.get(o + bb_pos) & 0xFF : 0; }
|
||||
public byte componentType(int j) { int o = __offset(8); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int componentTypeLength() { int o = __offset(8); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer componentTypeAsByteBuffer() { return __vector_as_bytebuffer(8, 1); }
|
||||
public ByteBuffer componentTypeInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 8, 1); }
|
||||
public Table component(Table obj, int j) { int o = __offset(10); return o != 0 ? __union(obj, __vector(o) + j * 4 - bb_pos) : null; }
|
||||
public int componentLength() { int o = __offset(10); return o != 0 ? __vector_len(o) : 0; }
|
||||
public boolean delete() { int o = __offset(8); return o != 0 ? 0!=bb.get(o + bb_pos) : false; }
|
||||
public byte componentType(int j) { int o = __offset(10); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
|
||||
public int componentTypeLength() { int o = __offset(10); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer componentTypeAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
|
||||
public ByteBuffer componentTypeInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 10, 1); }
|
||||
public Table component(Table obj, int j) { int o = __offset(12); return o != 0 ? __union(obj, __vector(o) + j * 4 - bb_pos) : null; }
|
||||
public int componentLength() { int o = __offset(12); return o != 0 ? __vector_len(o) : 0; }
|
||||
|
||||
public static int createEntitySync(FlatBufferBuilder builder,
|
||||
int entityId,
|
||||
int type,
|
||||
boolean delete,
|
||||
int component_typeOffset,
|
||||
int componentOffset) {
|
||||
builder.startObject(4);
|
||||
builder.startObject(5);
|
||||
EntitySync.addComponent(builder, componentOffset);
|
||||
EntitySync.addComponentType(builder, component_typeOffset);
|
||||
EntitySync.addEntityId(builder, entityId);
|
||||
EntitySync.addDelete(builder, delete);
|
||||
EntitySync.addType(builder, type);
|
||||
return EntitySync.endEntitySync(builder);
|
||||
}
|
||||
|
||||
public static void startEntitySync(FlatBufferBuilder builder) { builder.startObject(4); }
|
||||
public static void startEntitySync(FlatBufferBuilder builder) { builder.startObject(5); }
|
||||
public static void addEntityId(FlatBufferBuilder builder, int entityId) { builder.addInt(0, entityId, 0); }
|
||||
public static void addType(FlatBufferBuilder builder, int type) { builder.addByte(1, (byte)type, (byte)0); }
|
||||
public static void addComponentType(FlatBufferBuilder builder, int componentTypeOffset) { builder.addOffset(2, componentTypeOffset, 0); }
|
||||
public static void addDelete(FlatBufferBuilder builder, boolean delete) { builder.addBoolean(2, delete, false); }
|
||||
public static void addComponentType(FlatBufferBuilder builder, int componentTypeOffset) { builder.addOffset(3, componentTypeOffset, 0); }
|
||||
public static int createComponentTypeVector(FlatBufferBuilder builder, byte[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addByte(data[i]); return builder.endVector(); }
|
||||
public static void startComponentTypeVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
|
||||
public static void addComponent(FlatBufferBuilder builder, int componentOffset) { builder.addOffset(3, componentOffset, 0); }
|
||||
public static void addComponent(FlatBufferBuilder builder, int componentOffset) { builder.addOffset(4, componentOffset, 0); }
|
||||
public static int createComponentVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
|
||||
public static void startComponentVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
|
||||
public static int endEntitySync(FlatBufferBuilder builder) {
|
||||
|
@ -337,6 +337,14 @@ public class ClientNetworkReceiver extends IntervalSystem {
|
||||
|
||||
private void Synchronize(EntitySync entityData) {
|
||||
int entityId = syncIds.get(entityData.entityId());
|
||||
if (entityData.delete()) {
|
||||
if (entityId != Engine.INVALID_ENTITY) {
|
||||
world.delete(entityId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (entityId == Engine.INVALID_ENTITY) {
|
||||
syncIds.put(entityData.entityId(), entityId = createEntity(entityData));
|
||||
}
|
||||
|
@ -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.Deleted;
|
||||
import com.riiablo.engine.server.component.Item;
|
||||
import com.riiablo.engine.server.component.Monster;
|
||||
import com.riiablo.engine.server.component.Player;
|
||||
@ -42,6 +43,8 @@ import com.riiablo.net.packet.d2gs.EntitySync;
|
||||
|
||||
import net.mostlyoriginal.api.system.core.PassiveSystem;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
public class SerializationManager extends PassiveSystem {
|
||||
private static final String TAG = "SerializationManager";
|
||||
private static final boolean DEBUG = true;
|
||||
@ -56,6 +59,8 @@ public class SerializationManager extends PassiveSystem {
|
||||
private Class<? extends Component>[] deserializers;
|
||||
private final EntitySync sync = new EntitySync();
|
||||
|
||||
protected ComponentMapper<Deleted> mDeleted;
|
||||
|
||||
protected ComponentMapper<com.riiablo.engine.server.component.Class> mClass;
|
||||
protected ComponentMapper<CofComponents> mCofComponents;
|
||||
protected ComponentMapper<CofTransforms> mCofTransforms;
|
||||
@ -122,6 +127,14 @@ public class SerializationManager extends PassiveSystem {
|
||||
components.clear();
|
||||
|
||||
int type = mClass.get(entityId).type.ordinal();
|
||||
|
||||
boolean deleted = mDeleted.has(entityId);
|
||||
if (deleted) {
|
||||
int dataTypeOffset = EntitySync.createComponentTypeVector(builder, ArrayUtils.EMPTY_BYTE_ARRAY);
|
||||
int dataOffset = EntitySync.createComponentVector(builder, ArrayUtils.EMPTY_INT_ARRAY);
|
||||
return EntitySync.createEntitySync(builder, entityId, type, deleted, dataTypeOffset, dataOffset);
|
||||
}
|
||||
|
||||
componentManager.getComponentsFor(entityId, components);
|
||||
for (Component c : components) {
|
||||
FlatBuffersSerializer serializer = serializers.get(c.getClass());
|
||||
@ -144,7 +157,7 @@ public class SerializationManager extends PassiveSystem {
|
||||
for (int i = 0; i < dataSize; i++) builder.addOffset(data[i]);
|
||||
int dataOffset = builder.endVector();
|
||||
|
||||
return EntitySync.createEntitySync(builder, entityId, type, dataTypeOffset, dataOffset);
|
||||
return EntitySync.createEntitySync(builder, entityId, type, deleted, dataTypeOffset, dataOffset);
|
||||
}
|
||||
|
||||
public void deserialize(int entityId, D2GS packet) {
|
||||
|
@ -18,6 +18,7 @@ union ComponentP {
|
||||
table EntitySync {
|
||||
entityId:int32;
|
||||
type:uint8;
|
||||
delete:bool = false;
|
||||
component:[ComponentP];
|
||||
}
|
||||
|
||||
|
@ -3,12 +3,15 @@ package com.riiablo.server.d2gs;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.artemis.BaseEntitySystem;
|
||||
import com.artemis.ComponentMapper;
|
||||
import com.artemis.annotations.All;
|
||||
import com.artemis.annotations.Wire;
|
||||
import com.artemis.utils.IntBag;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.utils.IntIntMap;
|
||||
import com.riiablo.engine.server.SerializationManager;
|
||||
import com.riiablo.engine.server.component.Class;
|
||||
import com.riiablo.engine.server.component.Deleted;
|
||||
import com.riiablo.engine.server.component.Networked;
|
||||
import com.riiablo.net.packet.d2gs.D2GS;
|
||||
import com.riiablo.net.packet.d2gs.D2GSData;
|
||||
@ -27,11 +30,28 @@ public class NetworkSynchronizer extends BaseEntitySystem {
|
||||
@Wire(name = "player")
|
||||
protected IntIntMap players;
|
||||
|
||||
protected ComponentMapper<Class> mClass;
|
||||
protected ComponentMapper<Deleted> mDeleted;
|
||||
|
||||
@Override
|
||||
protected boolean checkProcessing() {
|
||||
return players.size > 0;
|
||||
}
|
||||
|
||||
// FIXME: this assumes that removing Networked component implies deletion -- may not always be case
|
||||
@Override
|
||||
protected void removed(int entityId) {
|
||||
Class.Type type = mClass.get(entityId).type;
|
||||
switch (type) {
|
||||
case PLR:
|
||||
// TODO: handled by disconnection packet, need to handle here also
|
||||
break;
|
||||
default:
|
||||
mDeleted.create(entityId);
|
||||
process(entityId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSystem() {
|
||||
IntBag entities = subscription.getEntities();
|
||||
|
Loading…
Reference in New Issue
Block a user