private void SendEntityToClient(RemoteClient client, IEntity entity) { if (entity.EntityID == -1) { return; // We haven't finished setting this entity up yet } client.Log("Spawning entity {0} ({1}) at {2}", entity.EntityID, entity.GetType().Name, (Coordinates3D)entity.Position); RemoteClient spawnedClient = null; if (entity is PlayerEntity) { spawnedClient = (RemoteClient)GetClientForEntity(entity as PlayerEntity); } client.KnownEntities.Add(entity); client.QueuePacket(entity.SpawnPacket); if (entity is IPhysicsEntity) { var pentity = entity as IPhysicsEntity; client.QueuePacket(new EntityVelocityPacket { EntityID = entity.EntityID, XVelocity = (short)(pentity.Velocity.X * 320), YVelocity = (short)(pentity.Velocity.Y * 320), ZVelocity = (short)(pentity.Velocity.Z * 320), }); } if (entity.SendMetadataToClients) { client.QueuePacket(new EntityMetadataPacket(entity.EntityID, entity.Metadata)); } if (spawnedClient != null) { // Send equipment when spawning player entities client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 0, spawnedClient.SelectedItem.ID, spawnedClient.SelectedItem.Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 4, spawnedClient.InventoryWindow.Armor[0].ID, spawnedClient.InventoryWindow.Armor[0].Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 3, spawnedClient.InventoryWindow.Armor[1].ID, spawnedClient.InventoryWindow.Armor[1].Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 2, spawnedClient.InventoryWindow.Armor[2].ID, spawnedClient.InventoryWindow.Armor[2].Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 1, spawnedClient.InventoryWindow.Armor[3].ID, spawnedClient.InventoryWindow.Armor[3].Metadata)); } }
private void AcceptClient(object sender, SocketAsyncEventArgs args) { try { var client = new RemoteClient(this, PacketReader, PacketHandlers, args.AcceptSocket); lock (ClientLock) Clients.Add(client); } catch { // Who cares } finally { args.AcceptSocket = null; if (!ShuttingDown && !Listener.Server.AcceptAsync(args)) { AcceptClient(this, args); } } }
private void AcceptClient(object sender, SocketAsyncEventArgs args) { try { var client = new RemoteClient(this, PacketReader, PacketHandlers, args.AcceptSocket); lock (ClientLock) Clients.Add(client); } catch { // Who cares } finally { args.AcceptSocket = null; if (!ShuttingDown && !Listener.Server.AcceptAsync(args)) AcceptClient(this, args); } }
private void SendEntityToClient(RemoteClient client, IEntity entity) { if (entity.EntityID == -1) return; // We haven't finished setting this entity up yet client.Log("Spawning entity {0} ({1}) at {2}", entity.EntityID, entity.GetType().Name, (Coordinates3D)entity.Position); RemoteClient spawnedClient = null; if (entity is PlayerEntity) spawnedClient = (RemoteClient)GetClientForEntity(entity as PlayerEntity); client.KnownEntities.Add(entity); client.QueuePacket(entity.SpawnPacket); if (entity is IPhysicsEntity) { var pentity = entity as IPhysicsEntity; client.QueuePacket(new EntityVelocityPacket { EntityID = entity.EntityID, XVelocity = (short)(pentity.Velocity.X * 320), YVelocity = (short)(pentity.Velocity.Y * 320), ZVelocity = (short)(pentity.Velocity.Z * 320), }); } if (entity.SendMetadataToClients) client.QueuePacket(new EntityMetadataPacket(entity.EntityID, entity.Metadata)); if (spawnedClient != null) { // Send equipment when spawning player entities client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 0, spawnedClient.SelectedItem.ID, spawnedClient.SelectedItem.Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 4, spawnedClient.InventoryWindow.Armor[0].ID, spawnedClient.InventoryWindow.Armor[0].Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 3, spawnedClient.InventoryWindow.Armor[1].ID, spawnedClient.InventoryWindow.Armor[1].Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 2, spawnedClient.InventoryWindow.Armor[2].ID, spawnedClient.InventoryWindow.Armor[2].Metadata)); client.QueuePacket(new EntityEquipmentPacket(entity.EntityID, 1, spawnedClient.InventoryWindow.Armor[3].ID, spawnedClient.InventoryWindow.Armor[3].Metadata)); } }
internal void UpdateClientEntities(RemoteClient client) { var entity = client.Entity; // Calculate entities you shouldn't know about anymore for (int i = 0; i < client.KnownEntities.Count; i++) { var knownEntity = client.KnownEntities[i]; if (knownEntity.Position.DistanceTo(entity.Position) > client.ChunkRadius * Chunk.Depth) { client.QueuePacket(new DestroyEntityPacket(knownEntity.EntityID)); client.KnownEntities.Remove(knownEntity); i--; // Make sure you're despawned on other clients if you move away from stationary players if (knownEntity is PlayerEntity) { var c = (RemoteClient)GetClientForEntity(knownEntity as PlayerEntity); if (c.KnownEntities.Contains(entity)) { c.KnownEntities.Remove(entity); c.QueuePacket(new DestroyEntityPacket(entity.EntityID)); c.Log("Destroying entity {0} ({1})", knownEntity.EntityID, knownEntity.GetType().Name); } } client.Log("Destroying entity {0} ({1})", knownEntity.EntityID, knownEntity.GetType().Name); } } // Calculate entities you should now know about var toSpawn = GetEntitiesInRange(entity, client.ChunkRadius); foreach (var e in toSpawn) { if (e != entity && !client.KnownEntities.Contains(e)) { SendEntityToClient(client, e); // Make sure other players know about you since you've moved if (e is PlayerEntity) { var c = (RemoteClient)GetClientForEntity(e as PlayerEntity); if (!c.KnownEntities.Contains(entity)) SendEntityToClient(c, entity); } } } }