private void addComponent(WorldData World, AddComponentClientDTO data) { if (World.Entities.ContainsKey(data.TargetID)) { UMComponentDTO exsisting = World.Entities[data.TargetID].Components.Where((x) => { return(x.EntityID == data.EntityID); }).FirstOrDefault(); if (exsisting != null) { Console.WriteLine($"Component of type {data.EntityID} already exsists on entity {data.TargetID}"); return; } UMComponentDTO nComponent = new UMComponentDTO(); nComponent.EntityID = data.EntityID; nComponent.TargetID = data.TargetID; nComponent.ID = World.componentIdCounter; World.componentIdCounter++; World.Entities[data.TargetID].Components.Add(nComponent); World.EntityComponents.Add(nComponent.ID, nComponent); using (DarkRiftWriter entityWriter = DarkRiftWriter.Create()) { AddComponentServerDTO outData = new AddComponentServerDTO(); outData.ID = nComponent.ID; outData.EntityID = nComponent.EntityID; outData.TargetID = nComponent.TargetID; entityWriter.Write(outData); using (Message addcompMessage = Message.Create(Tags.AddComponentTag, entityWriter)) { foreach (IClient c in World.GetClients()) { c.SendMessage(addcompMessage, SendMode.Reliable); } } } } else { Console.WriteLine("Trying to add component to non exsistant entity: " + data.TargetID); } }
public void SetTransform(WorldData World, uint id, float x = 0, float y = 0, float z = 0, float rx = 0, float ry = 0, float rz = 0, float sx = 1, float sy = 1, float sz = 1) { if (World.Entities.ContainsKey(id)) { World.Entities[id].X = x; World.Entities[id].Y = y; World.Entities[id].Z = z; World.Entities[id].rotX = rx; World.Entities[id].rotY = ry; World.Entities[id].rotZ = rz; World.Entities[id].scaleX = sx; World.Entities[id].scaleY = sy; World.Entities[id].scaleZ = sz; using (DarkRiftWriter entityTransformWriter = DarkRiftWriter.Create()) { entityTransformWriter.Write(id); entityTransformWriter.Write(x); entityTransformWriter.Write(y); entityTransformWriter.Write(z); entityTransformWriter.Write(rx); entityTransformWriter.Write(ry); entityTransformWriter.Write(rz); entityTransformWriter.Write(sx); entityTransformWriter.Write(sy); entityTransformWriter.Write(sz); using (Message setStateMessage = Message.Create(Tags.TransformEntityTag, entityTransformWriter)) { foreach (IClient c in World.GetClients()) { c.SendMessage(setStateMessage, SendMode.Unreliable); } } } } else { Console.WriteLine("Setting transform on non exsistant entity: " + id); } }
public void DespawnPlayer(WorldData world, ushort id) { if (world.AIPlayers.ContainsKey(id)) { world.AIPlayers.Remove(id); } using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(id); using (Message message = Message.Create(Tags.DespawnPlayerTag, writer)) { foreach (IClient _client in world.GetClients()) { _client.SendMessage(message, SendMode.Reliable); } } } }
private void setComponentProperty(WorldData World, ComponentPropertyDTO data) { if (World.EntityComponents.ContainsKey(data.ComponentID)) { World.EntityComponents[data.ComponentID].AddOrUpdateProperty(data); using (DarkRiftWriter entityWriter = DarkRiftWriter.Create()) { entityWriter.Write(data); using (Message addcompMessage = Message.Create(Tags.SetComponentPropertyTag, entityWriter)) { foreach (IClient c in World.GetClients()) { c.SendMessage(addcompMessage, SendMode.Reliable); } } } } }
public void setPhysics(WorldData World, SetPhysicsDTO inData) { if (World.Entities.ContainsKey(inData.Id)) { World.Entities[inData.Id].hasPhysics = inData.HasPhysics; using (DarkRiftWriter physSettingsWriter = DarkRiftWriter.Create()) { // TODO: Anti cheat goes here.. physSettingsWriter.Write(inData); using (Message setPhysMessage = Message.Create(Tags.SetPhysicsEntityTag, physSettingsWriter)) { Console.WriteLine("setphysics relayed"); foreach (IClient c in World.GetClients()) { c.SendMessage(setPhysMessage, SendMode.Reliable); } } } } }
public void RegisterClient(IClient client) { if (playerManager == null) { playerManager = PluginManager.GetPluginByType <UMPlayerManager>(); } if (worldManager == null) { worldManager = PluginManager.GetPluginByType <UMWorldManager>(); } Console.WriteLine("Weaponmanager registered player"); client.MessageReceived += Client_MessageReceived; WorldData World = worldManager.clients[client].World; foreach (Player p in World.players.Values) { if (p.WeaponEntityID != ushort.MaxValue) { using (DarkRiftWriter weaponSwitchWriter = DarkRiftWriter.Create()) { WeaponSwitchServerDTO switchData = new WeaponSwitchServerDTO(); switchData.playerId = client.ID; switchData.weaponEntityId = p.WeaponEntityID; switchData.weaponSlot = 0; weaponSwitchWriter.Write(switchData); using (Message fireStartMessage = Message.Create(Tags.WeaponSwitchTag, weaponSwitchWriter)) //Repeat the incoming tagname as all message bodies are the same { client.SendMessage(fireStartMessage, SendMode.Reliable); } } } } }
public void WeaponSwitchMessageRecieved(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.WeaponSwitchTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { WorldData World = worldManager.clients[e.Client].World; WeaponSwitchClientDTO data = reader.ReadSerializable <WeaponSwitchClientDTO>(); Player cPlayer = World.players[e.Client]; cPlayer.WeaponEntityID = data.weaponEntityId; using (DarkRiftWriter weaponSwitchWriter = DarkRiftWriter.Create()) { WeaponSwitchServerDTO switchData = new WeaponSwitchServerDTO(); switchData.playerId = e.Client.ID; switchData.weaponEntityId = data.weaponEntityId; switchData.weaponSlot = data.weaponSlot; weaponSwitchWriter.Write(switchData); using (Message fireStartMessage = Message.Create(Tags.WeaponSwitchTag, weaponSwitchWriter)) //Repeat the incoming tagname as all message bodies are the same { foreach (IClient client in World.GetClients().Where(x => x != e.Client)) { client.SendMessage(fireStartMessage, SendMode.Reliable); } } } } } } } }
public void RegisterClient(IClient client, WorldData World) { if (WorldManager == null) { WorldManager = PluginManager.GetPluginByType <UMWorldManager>(); } if (WeaponManager == null) { WeaponManager = PluginManager.GetPluginByType <UMWeaponManager>(); } WorldManager.clients[client].World = World; client.MessageReceived += Client_MessageReceived; //Send exsisting players using (DarkRiftWriter playerWriter = DarkRiftWriter.Create()) { //PlayerSpawnServerDTO spawnData = new PlayerSpawnServerDTO(); foreach (Player player in World.players.Values) { this.WritePlayerSpawnData(playerWriter, player); } foreach (Player player in World.AIPlayers.Values) { this.WritePlayerSpawnData(playerWriter, player); } using (Message playerMessage = Message.Create(Tags.SpawnPlayerTag, playerWriter)) client.SendMessage(playerMessage, SendMode.Reliable); } //Register WeaponManager WeaponManager.RegisterClient(client); }
public void DamageHurtableRecieved(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.DamageHurtableTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { WorldData World = worldManager.clients[e.Client].World; DamageHurtableClientDTO data = reader.ReadSerializable <DamageHurtableClientDTO>(); using (DarkRiftWriter damageHurtableWriter = DarkRiftWriter.Create()) { DamageHurtableServerDTO damageHurtableData = new DamageHurtableServerDTO(); damageHurtableData.InstagatorID = e.Client.ID; damageHurtableData.VictimID = data.VictimID; damageHurtableData.damage = data.damage; Console.WriteLine($"{damageHurtableData.InstagatorID} damaging {damageHurtableData.VictimID} for {damageHurtableData.damage} hp"); damageHurtableWriter.Write(damageHurtableData); using (Message damageHurtableMessage = Message.Create(Tags.DamageHurtableTag, damageHurtableWriter)) { foreach (IClient client in World.GetClients()) { client.SendMessage(damageHurtableMessage, SendMode.Reliable); } } } } } } } }
public void DespawnEntity(WorldData World, uint id) { if (World.Entities.ContainsKey(id)) { World.Entities.Remove(id); using (DarkRiftWriter entityWriter = DarkRiftWriter.Create()) { entityWriter.Write(id); using (Message despawnEntityMsg = Message.Create(Tags.DespawnEntityTag, entityWriter)) { foreach (IClient c in World.GetClients()) { c.SendMessage(despawnEntityMsg, SendMode.Reliable); } } } } else { Console.WriteLine("Trying to despawn non exsistant entity: " + id); } }
private void PlayerUpdateMessageRecieved(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.PlayerUpdateTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { WorldData World = WorldManager.clients[e.Client].World; PlayerUpdateClientDTO playerUpdateData = reader.ReadSerializable <PlayerUpdateClientDTO>(); Player player; if (playerUpdateData.PlayerID < 128) { player = World.players[e.Client]; } else { player = World.AIPlayers[playerUpdateData.PlayerID]; } player.X = playerUpdateData.x; player.Y = playerUpdateData.y; player.Z = playerUpdateData.z; player.RX = playerUpdateData.rx; player.RY = playerUpdateData.ry; player.RZ = playerUpdateData.rz; player.VX = playerUpdateData.vx; player.VY = playerUpdateData.vy; player.VZ = playerUpdateData.vz; using (DarkRiftWriter writer = DarkRiftWriter.Create()) { PlayerUpdateServerDTO playerUpdateOutData = new PlayerUpdateServerDTO(); playerUpdateOutData.ID = player.ID; playerUpdateOutData.x = player.X; playerUpdateOutData.y = player.Y; playerUpdateOutData.z = player.Z; playerUpdateOutData.rx = player.RX; playerUpdateOutData.ry = player.RY; playerUpdateOutData.rz = player.RZ; playerUpdateOutData.vx = player.VX; playerUpdateOutData.vy = player.VY; playerUpdateOutData.vz = player.VZ; playerUpdateOutData.triggerQueue = playerUpdateData.triggerQueue; writer.Write(playerUpdateOutData); using (Message playerUpdateMessage = Message.Create(Tags.PlayerUpdateTag, writer)) { foreach (IClient c in World.GetClients().Where(x => x != e.Client)) { c.SendMessage(playerUpdateMessage, e.SendMode); } } } } } } } }
private void SpawnMessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { if (message.Tag == Tags.SpawnPlayerTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { PlayerSpawnClientDTO clientSpawnData = reader.ReadSerializable <PlayerSpawnClientDTO>(); WorldData World = WorldManager.clients[e.Client].World; ushort entityId = clientSpawnData.entityID; bool isNewPlayer = clientSpawnData.isAI ? !World.AIPlayers.ContainsKey(clientSpawnData.PlayerID) : !World.players.ContainsKey(e.Client); Player player; //TODO: use spawn rotation and health if (isNewPlayer) { player = new Player( clientSpawnData.isAI ? World.aiIDCounter : e.Client.ID, //Player id clientSpawnData.position.x, clientSpawnData.position.y, clientSpawnData.position.z, //Position x,y,z TODO: make this a UMVector3 entityId, 100f, // MaxHealth clientSpawnData.isAI ); if (clientSpawnData.isAI) { World.AIPlayers.Add(World.aiIDCounter, player); World.aiIDCounter++; } else { World.players.Add(e.Client, player); } } else { player = clientSpawnData.isAI ? World.AIPlayers[clientSpawnData.PlayerID] : World.players[e.Client]; player.entityId = clientSpawnData.entityID; player.X = clientSpawnData.position.x; player.Y = clientSpawnData.position.y; player.Z = clientSpawnData.position.z; } Console.WriteLine($"{player.ID} ({e.Client.ID}) requested spawn. isNewPlayer {isNewPlayer}, entityID { player.entityId }"); using (DarkRiftWriter newPlayerWriter = DarkRiftWriter.Create()) { this.WritePlayerSpawnData(newPlayerWriter, player); using (Message newPlayerMessage = Message.Create(Tags.SpawnPlayerTag, newPlayerWriter)) { foreach (IClient client in World.GetClients()) { client.SendMessage(newPlayerMessage, SendMode.Reliable); } } } } } } } }
private void Client_MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { WorldData World = WorldManager.clients[e.Client].World; if (message.Tag == Tags.SpawnEntityTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { SpawnEntityClientDTO data = reader.ReadSerializable <SpawnEntityClientDTO>(); this.SpawnEntity(World, data); } } } else if (message.Tag == Tags.SpawnProceduralShapeEntityTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { SpawnProceduralShapeEntityClientDTO data = reader.ReadSerializable <SpawnProceduralShapeEntityClientDTO>(); Console.WriteLine("Got spawn procedural message"); this.SpawnProceduralShapeEntity(World, data); } } } else if (message.Tag == Tags.DespawnEntityTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { uint id = reader.ReadUInt32(); this.DespawnEntity(World, id); } } } else if (message.Tag == Tags.SetEntityParentTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { SetParentDTO data = reader.ReadSerializable <SetParentDTO>(); SetParent(World, data); } } } else if (message.Tag == Tags.SetStateEntityTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { uint id = reader.ReadUInt32(); ushort newState = reader.ReadUInt16(); SetState(World, id, newState); } } } else if (message.Tag == Tags.TransformEntityTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { uint id = reader.ReadUInt32(); float x = reader.ReadSingle(); float y = reader.ReadSingle(); float z = reader.ReadSingle(); float rx = reader.ReadSingle(); float ry = reader.ReadSingle(); float rz = reader.ReadSingle(); float sx = reader.ReadSingle(); float sy = reader.ReadSingle(); float sz = reader.ReadSingle(); SetTransform(World, id, x, y, z, rx, ry, rz, sx, sy, sz); } } } else if (message.Tag == Tags.PhysicsUpdateEntityTag) { if (e.Client == WorldManager.clients[e.Client].World.PhysicsHost) { PhysicsUpdate(sender, e, WorldManager.clients[e.Client].World); } } else if (message.Tag == Tags.SetPhysicsEntityTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { //Console.WriteLine("recieved physmessage"); SetPhysicsDTO data = reader.ReadSerializable <SetPhysicsDTO>(); setPhysics(World, data); } } } else if (message.Tag == Tags.AddComponentTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { //Console.WriteLine("recieved physmessage"); AddComponentClientDTO data = reader.ReadSerializable <AddComponentClientDTO>(); addComponent(World, data); } } } else if (message.Tag == Tags.RemoveComponentTag) { using (DarkRiftReader reader = message.GetReader()) { while (reader.Position < reader.Length) { //Console.WriteLine("recieved physmessage"); RemoveComponentDTO data = reader.ReadSerializable <RemoveComponentDTO>(); removeComponent(World, data); } } } else if (message.Tag == Tags.SetComponentPropertyTag) { using (DarkRiftReader reader = message.GetReader()) { //while (reader.Position < reader.Length) //{ ComponentPropertyDTO data = reader.ReadSerializable <ComponentPropertyDTO>(); setComponentProperty(World, data); //} } } } }