public void EntityMovePacket() { var bytes = new byte[] { 0x0F, 0x00, 0x02, 0x00, 0x00, 0x00, 0x17, 0x11, 0x16, 0x99, 0x15, 0x00, 0x16, 0x16, 0x94, 0x15 }; var packet = new EntityMovePacket(new D2gsPacket(bytes)); Assert.Equal(2U, packet.UnitId); Assert.Equal(EntityType.Player, packet.UnitType); Assert.Equal(new Point(5654, 5524), packet.CurrentLocation); Assert.Equal(new Point(5649, 5529), packet.MoveToLocation); }
// server recieving entitymove means its a player moving public void OnEntityMovePacket(EntityMovePacket packet) { var player = Server.GetPlayerByConnectionId(packet.ClientId); var distanceMoved = MapHelpers.GetDistance(player.Position, packet.To); var timeToMove = Formulas.GetTimeToMoveBetweenTwoTiles(player.MoveSpeed); Log.Info("TIME TO MOVE " + timeToMove); var now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; // Player tryng to hack ? if (distanceMoved > 1 || now < player.CanMoveAgainTime) { Log.Debug($"Player time to move {player.CanMoveAgainTime - now}"); // send player back to the position client-side player.Tcp.Send(new SyncPacket() { Position = player.Position }); return; } // subtract the player latency for possibility of lag for a smoother movement player.CanMoveAgainTime = now + timeToMove - player.Tcp.Latency; var entityMoveEvent = new EntityMoveEvent() { From = packet.From, To = packet.To, Entity = player }; Server.Events.Call(entityMoveEvent); if (entityMoveEvent.IsCancelled) { // send player back to the position client-side player.Tcp.Send(new SyncPacket() { Position = player.Position }); return; } // Updating player position locally player.Position.X = packet.To.X; player.Position.Y = packet.To.Y; // updating in database PlayerService.UpdatePlayerPosition(player.UID, player.Position.X, player.Position.Y); }
public void OnEntityMove(EntityMovePacket packet) { var entityObj = GameObject.Find(packet.UID); if (entityObj != null) { var movingEntity = entityObj.GetComponent <MovingEntityBehaviour>(); if (movingEntity != null) { movingEntity.Route.Add(packet.To); if (UnityClient.Player.Target != null && UnityClient.Player.Target == entityObj) { UnityClient.Player.FindPathTo(UnityClient.Player.Target.GetMapPosition()); } } } }
public void OnEntityMove(EntityMovePacket packet) { var entityObj = GameObject.Find(packet.UID); if (entityObj != null) { var livingEntityBhv = entityObj.GetComponent <LivingEntityBehaviour>(); if (livingEntityBhv != null) { livingEntityBhv.Route.Add(packet.To); livingEntityBhv.ForceUpdate(); // to make sure its moved right away if (UnityClient.Player.Target != null && UnityClient.Player.Target == livingEntityBhv.Entity) { UnityClient.Player.FindPathTo(livingEntityBhv.Entity.Position); } } } }
public void OnEntityMove(EntityMoveEvent ev) { ev.Entity.LastPosition.X = ev.Entity.Position.X; ev.Entity.LastPosition.Y = ev.Entity.Position.Y; ev.Entity.Position.X = ev.To.X; ev.Entity.Position.Y = ev.To.Y; Server.Map.UpdateEntityPosition(ev.Entity, ev.Entity.LastPosition, ev.To); var nearPlayers = ev.Entity.GetPlayersNear(); var movePacket = new EntityMovePacket() { To = ev.Entity.Position, UID = ev.Entity.UID }; // Updating this movement to nearby players soo the client updates foreach (var nearPlayer in nearPlayers) { if (nearPlayer.UID != ev.Entity.UID) { nearPlayer.Tcp.Send(movePacket); } } if (typeof(LivingEntity).IsAssignableFrom(ev.Entity.GetType())) { // if im targeting something, i might attack it now after this movement var livingEntity = (LivingEntity)ev.Entity; if (livingEntity.Target != null) { livingEntity.TryAttacking(livingEntity.Target); } // if someone is targeting me, he might be able to hit me now foreach (var targettingThisEntity in livingEntity.BeingTargetedBy) { targettingThisEntity.TryAttacking(livingEntity); } } }
public void OnEntityMove(EntityMoveEvent ev) { var fromChunkX = ev.From.X >> 4; var fromChunkY = ev.From.Y >> 4; var toChunkX = ev.To.X >> 4; var toChunkY = ev.To.Y >> 4; var toChunk = Server.Map.GetChunk(toChunkX, toChunkY); var nearPlayers = ev.Entity.GetPlayersNear(); var movePacket = new EntityMovePacket() { From = ev.From, To = ev.To, UID = ev.Entity.UID }; // Update Entity Position Cache Server.Map.EntityPositions.RemoveEntity(ev.Entity, ev.From); Server.Map.EntityPositions.AddEntity(ev.Entity, ev.To); ev.Entity.LastPosition = ev.From; // Updating this movement to nearby players soo the client updates foreach (var nearPlayer in nearPlayers) { nearPlayer.Tcp.Send(movePacket); } // Changed chunk if (fromChunkX != toChunkX || fromChunkY != toChunkY) { var fromChunk = Server.Map.GetChunk(fromChunkX, fromChunkY); toChunk.MoveFromChunk(ev.Entity, fromChunk); } ev.Entity.Position = ev.To; }
// server recieving entitymove means its a player moving public void OnEntityMovePacket(EntityMovePacket packet) { var player = Server.GetPlayerByConnectionId(packet.ClientId); var originalPosition = new Position(player.Position.X, player.Position.Y); var distanceMoved = PositionExtensions.GetDistance(player.Position, packet.To); var timeToMove = Formulas.GetTimeToMoveBetweenTwoTiles(player.MoveSpeed); var now = GameThread.TIME_MS_NOW; // Player tryng to hack ? var isPassable = Server.Map.IsPassable(packet.To.X, packet.To.Y); if (distanceMoved > 1 || now < player.CanMoveAgainTime || !isPassable) { // send player back to the position client-side player.Tcp.Send(new SyncPacket() { Position = player.Position }); return; } // subtract the player latency for possibility of lag for a smoother movement player.CanMoveAgainTime = now + timeToMove - player.Tcp.Latency; var entityMoveEvent = new EntityMoveEvent() { To = packet.To, Entity = player }; Server.Events.Call(entityMoveEvent); // updating in database PlayerService.UpdatePlayerPosition(player.UID, player.Position.X, player.Position.Y); // update chunks for that player ChunkProvider.CheckChunks(player); }
//EntityMovePacket private static void HandlePacket(EntityMovePacket packet) { var id = packet.Id; var type = packet.Type; var mapId = packet.MapId; Entity en; if (type < EntityTypes.Event) { if (!Globals.Entities.ContainsKey(id)) { return; } en = Globals.Entities[id]; } else { var gameMap = MapInstance.Get(mapId); if (gameMap == null) { return; } if (!gameMap.LocalEntities.ContainsKey(id)) { return; } en = gameMap.LocalEntities[id]; } if (en == null) { return; } var entityMap = MapInstance.Get(en.CurrentMap); if (entityMap == null) { return; } if (en.Dashing != null || en.DashQueue.Count > 0) { return; } var map = mapId; var x = packet.X; var y = packet.Y; var dir = packet.Direction; var correction = packet.Correction; if ((en.CurrentMap != map || en.X != x || en.Y != y) && (en != Globals.Me || en == Globals.Me && correction) && en.Dashing == null) { en.CurrentMap = map; en.X = x; en.Y = y; en.Dir = dir; en.IsMoving = true; switch (en.Dir) { case 0: en.OffsetY = Options.TileWidth; en.OffsetX = 0; break; case 1: en.OffsetY = -Options.TileWidth; en.OffsetX = 0; break; case 2: en.OffsetY = 0; en.OffsetX = Options.TileWidth; break; case 3: en.OffsetY = 0; en.OffsetX = -Options.TileWidth; break; } } // Set the Z-Dimension if the player has moved up or down a dimension. if (entityMap.Attributes[en.X, en.Y] != null && entityMap.Attributes[en.X, en.Y].Type == MapAttributes.ZDimension) { if (((MapZDimensionAttribute)entityMap.Attributes[en.X, en.Y]).GatewayTo > 0) { en.Z = (byte)(((MapZDimensionAttribute)entityMap.Attributes[en.X, en.Y]).GatewayTo - 1); } } }
public void getPackets() { //GD.Print("Get Packets"); //GD.Print(wrapped_client.GetAvailablePacketCount()); if (connected && client.GetAvailableBytes() > 0) { //byte[] packet_data = wrapped_client.GetPacket(); //UInt32 packetLength = BitConverter.ToUInt32(packet_data, 0); //short packetId = BitConverter.ToInt16(packet_data, 4); UInt32 packetLength = (UInt32)client.GetU32(); short packetId = (short)client.Get16(); var packetData = client.GetData((int)packetLength - 2); //var data = new List<byte>(packet_data).GetRange(6, packet_data.Length -6).ToArray(); //GD.Print(BitConverter.ToString( (byte[]) packetData[1])); var data = (byte[])packetData[1]; var packet = Packets.Packets.decode(packetId, data); if (GetParent().GetNodeOrNull("GUI") != null) { var gui = (Control)GetParent().GetNodeOrNull("GUI"); gui.Call("recordPacket", packetLength + 4); } GD.Print(String.Format("Received packet {0}, ID: {1} Length: {2}", packet.name, packetId, packetLength)); if (packet is ReadyPacket) { ReadyPacket parsed_packet = (ReadyPacket)packet; if (parsed_packet.code == 0) { string token = (string)GetParent().GetNode("Discord Integration").Call("getToken"); var loginPacket = new LoginPacket(token); GD.Print("Sending login"); sendPacket(loginPacket); } else if (parsed_packet.code == 1) { var requestWorldPacket = new RequestWorldPacket(); sendPacket(requestWorldPacket); if (!joined) { var loadingRes = GD.Load <PackedScene>("res://scenes/world.tscn"); var node = loadingRes.Instance(); node.SetName("WorldScene"); var loadingGuiRes = GD.Load <PackedScene>("res://scenes/gui.tscn"); var gui = (Control)loadingGuiRes.Instance(); gui.SetName("GUI"); GetParent().Call("setState", 2); GetParent().AddChild(node); //GetParent().AddChild(gui); GetParent().AddChild(gui); //node.AddChild(gui); GetParent().GetNode("GameLoader").Free(); //var playerSpriteScene = (PackedScene) node.Call("getSprite", "rowan"); //var playerSprite = (AnimatedSprite) playerSpriteScene.Instance(); //playerSprite.SetName("PlayerSprite"); //node.GetNode("World/Player").AddChild(playerSprite); //playerSprite.Position = ((KinematicBody2D) node.GetNode("Player")).Position; //playerSprite.Visible = true; //GD.Print(playerSprite); joined = true; } } } else if (packet is PongPacket) { PongPacket parsed_packet = (PongPacket)packet; GD.Print("Got pong of " + parsed_packet.message); } else if (packet is LoginResultPacket) { LoginResultPacket parsed_packet = (LoginResultPacket)packet; GD.Print("Login Result: " + parsed_packet.responseCode.ToString() + " Name: " + parsed_packet.userId); var joinGamePacket = new JoinGamePacket(); sendPacket(joinGamePacket); } else if (packet is WorldPacket) { WorldPacket parsed_packet = (WorldPacket)packet; //GD.Print(parsed_packet.debug); GetParent().GetNode("WorldScene").Call("loadWorld", new object[] { parsed_packet.worldData, parsed_packet.bumpData, parsed_packet.height, parsed_packet.width }); } else if (packet is PlayerPositionPacket) { PlayerPositionPacket parsed_packet = (PlayerPositionPacket)packet; GetParent().GetNode("WorldScene/World/Player").Call("move", new object[] { parsed_packet.x, parsed_packet.y }); } else if (packet is ChatPacket) { ChatPacket parsed_packet = (ChatPacket)packet; GetNode("../GUI/Chat").Call("AddMessage", parsed_packet.author + ": " + parsed_packet.msg); } else if (packet is EntityPacket) { EntityPacket parsed_packet = (EntityPacket)packet; GD.Print("Got entity '", parsed_packet.sprite, "' at ", parsed_packet.x, ",", parsed_packet.y, " ID: ", parsed_packet.uuid); GetNode("../WorldScene").Call("addEntity", parsed_packet.x, parsed_packet.y, parsed_packet.type, parsed_packet.facing, parsed_packet.interactable, parsed_packet.sprite, parsed_packet.uuid, parsed_packet.type != 2); } else if (packet is EntityMovePacket) { EntityMovePacket parsed_packet = (EntityMovePacket)packet; GD.Print("Got entity moving to ", parsed_packet.x, ",", parsed_packet.y, " ID: ", parsed_packet.uuid); GetNode("../WorldScene").Call("moveEntity", parsed_packet.uuid, parsed_packet.x, parsed_packet.y, parsed_packet.facing); } else if (packet is InvalidateCachePacket) { InvalidateCachePacket parsed_packet = (InvalidateCachePacket)packet; GD.Print(parsed_packet.uuid, " Invalidated."); GetNode("../WorldScene").Call("hideEntity", parsed_packet.uuid); } else if (packet is DialoguePacket) { DialoguePacket parsed_packet = (DialoguePacket)packet; GD.Print("Got dialogue \"", parsed_packet.text, "\""); Window window = (Window)GetNode("../GUI/Window"); window.OpenDialoguePanel(); DialoguePanel dialoguePanel = window.OpenDialoguePanel(); //(DialoguePanel) GetNode("../GUI/Window/DialoguePanel"); dialoguePanel.SetDialogue(parsed_packet.text, parsed_packet.author, parsed_packet.sprite, parsed_packet.substitutions, parsed_packet.optionViews); } else if (packet is CloseDialoguePacket) { CloseDialoguePacket parsed_packet = (CloseDialoguePacket)packet; DialoguePanel dialoguePanel = (DialoguePanel)GetNode("../GUI/Window/DialoguePanel"); dialoguePanel.CloseDialogue(parsed_packet.guid); //dialoguePanel.SetDialogue(parsed_packet.text, parsed_packet.author, parsed_packet.sprite, parsed_packet.substitutions, parsed_packet.optionViews); } else if (packet is PlayerDataPacket) { PlayerDataPacket parsed_packet = (PlayerDataPacket)packet; var player = (Player)GetNode("../WorldScene/World/Player"); player.SetUuid(parsed_packet.guid); if (player.GetNodeOrNull("PlayerSprite") != null) { player.GetNodeOrNull("PlayerSprite").Free(); } var playerSpriteScene = (PackedScene)GetNode("../WorldScene").Call("getSprite", parsed_packet.sprite); var playerSprite = (AnimatedSprite)playerSpriteScene.Instance(); playerSprite.SetName("PlayerSprite"); player.AddChild(playerSprite); } else if (packet is InventoryPacket) { InventoryPacket parsed_packet = (InventoryPacket)packet; var player = (Player)GetNode("../WorldScene/World/Player"); if (player.guid == parsed_packet.inventory.guid) { player.inventory = parsed_packet.inventory; foreach (Item item in player.inventory.items) { GD.Print("Item: ", item.GetName(), " \"", item.GetDescription(), "\""); } } } else if (packet is AddItemPacket) { AddItemPacket parsed_packet = (AddItemPacket)packet; var player = (Player)GetNode("../WorldScene/World/Player"); if (player.guid == parsed_packet.guid) { //TODO: Make use of indices. player.inventory.AddItem(parsed_packet.item, true); } } else if (packet is ModifyItemPacket) { ModifyItemPacket parsed_packet = (ModifyItemPacket)packet; var player = (Player)GetNode("../WorldScene/World/Player"); if (player.guid == parsed_packet.guid) { //TODO: Make use of indices. player.inventory.UpdateItem(parsed_packet.item, parsed_packet.index); } } } else { var testPacket = new Packets.PingPacket("Hello There!"); //sendPacket(testPacket); } }