public static void Handle(MsgSpawn packet) { if (!World.UidExists(packet.UniqueId)) { var srcEntity = Database.Entities[packet.Model]; if (packet.UniqueId == TestingScene.Player.UniqueId) { var player = TestingScene.Player; player.Speed = 128; player.Position = new Vector2(packet.X, packet.Y); player.DrawableComponent.TextureName = srcEntity.TextureName; player.DrawableComponent.SrcRect = srcEntity.SrcRect; player.Name = packet.GetName(); } else { ref var entity = ref World.CreateEntity(packet.UniqueId); entity.Add <DbgBoundingBoxComponent>(); entity.Add(new SpeedComponent(32)); entity.Add(new PositionComponent(packet.X, packet.Y)); entity.Add(new DrawableComponent(srcEntity.TextureName, srcEntity.SrcRect)); var name = Global.Names[Global.Random.Next(0, Global.Names.Length)]; ref var npcNameTag = ref World.CreateEntity(); npcNameTag.Add(new TextComponent($"{entity.EntityId}: {name}")); npcNameTag.Add(new PositionComponent(-16, -16, 0)); entity.AddChild(ref npcNameTag); }
public static void SendSpawn(YiObj obj) { try { if (obj is Monster monster) { if (!Entities.ContainsKey(obj.UniqueId)) { Create(obj); } foreach (var uid in Entities[obj.UniqueId]) { if (!GameWorld.Maps[obj.MapId].Entities.TryGetValue(uid, out var found)) { continue; } if (found is Player player) { player.Send(MsgSpawn.Create(monster)); player.Send(MsgAction.SpawnEffect(monster)); } } } } catch (Exception e) { Output.WriteLine(e); } }
public void Add(YiObj human) { if (Members.Contains(human.UniqueId)) { return; } Members.Add(human.UniqueId); human.Guild = this; human.GuildRank = GuildRanks.Member; if (human is Player player) { player.Send(MsgSyndicateSpawn.Create(player)); Message.SendTo(player, $"{player.Name} has joined our guild!", MsgTextType.Guild); foreach (var guild in Allies) { player.Send(MsgSyndicate.Create(guild.UniqueId, GuildRequest.SetAlly)); } foreach (var guild in Enemies) { player.Send(MsgSyndicate.Create(guild.UniqueId, GuildRequest.SetEnemy)); } } ScreenSystem.Send(human, MsgSpawn.Create(human as Player), true); }
public static void Add(YiObj obj, YiObj target) { if (!Entities.ContainsKey(obj.UniqueId)) { Create(obj); } if (target.UniqueId == obj.UniqueId || Entities[obj.UniqueId].Contains(target.UniqueId)) { return; } Entities[obj.UniqueId].Add(target.UniqueId); Add(target, obj); switch (target) { case Bot botTarget: //(obj as Player)?.Send(MsgSpawn.Create(botTarget)); break; case Player playerTarget: (obj as Player)?.Send(MsgSpawn.Create(playerTarget)); break; case Monster monsterTarget: monsterTarget.Brain.PlayersInSight++; (obj as Player)?.Send(MsgSpawn.Create(monsterTarget)); break; case Npc npcTarget: (obj as Player)?.Send(MsgNpcSpawn.Create(npcTarget)); if (npcTarget.BoothId != 0) { (obj as Player)?.Send(LegacyPackets.SpawnCarpet(npcTarget, npcTarget.BoothId)); } break; case DynamicNpc dynNpcTarget: (obj as Player)?.Send(MsgDynNpcSpawn.Create(dynNpcTarget)); break; case FloorItem floorItemTarget: (obj as Player)?.Send(MsgFloorItem.Create(floorItemTarget, MsgFloorItemType.Create)); break; case Portal portal: break; case null: Debugger.Break(); break; default: Debugger.Break(); break; } }
public unsafe bool Unequip(MsgItemPosition position) { if (_owner.Inventory.Count == 40) { return(false); } if (TryRemove(position, out var item)) { _owner.Inventory.AddOrUpdate(item.UniqueId, item); (_owner as Player)?.ForceSend(new MsgItemInformation(item, MsgItemPosition.Inventory), (ushort)sizeof(MsgItemInformation)); ScreenSystem.Send(_owner, MsgSpawn.Create(_owner as Player)); EntityLogic.Recalculate(_owner); } return(true); }
public static bool SendJump(YiObj obj, ref MsgAction packet) { try { if (!Entities.ContainsKey(obj.UniqueId)) { Create(obj); } (obj as Player)?.ForceSend(packet, packet.Size); foreach (var o in Entities[obj.UniqueId].Where(o => !Position.CanSee(obj, GameWorld.Maps[obj.MapId].Entities[o]))) { Remove(obj, GameWorld.Maps[obj.MapId].Entities[o]); } var ret = false; foreach (var o in GameWorld.Maps[obj.MapId].Entities.Values.Where(o => obj.UniqueId != o.UniqueId).Where(o => !Entities[obj.UniqueId].Contains(o.UniqueId))) { if (Position.CanSee(obj, o) && !(o is Player)) { Add(obj, o); } if (o is Player player) { if (Position.GetDistance(o.Location.X, o.Location.Y, (ushort)packet.Param, (ushort)(packet.Param >> 16)) >= 18) { continue; } Add(player, obj); Entities[obj.UniqueId].Add(player.UniqueId); var point = Position.GetBorderCoords((ushort)packet.Param, (ushort)(packet.Param >> 16), player.Location.X, player.Location.Y); player.Send(MsgSpawn.Create(obj, point)); player.Send(packet); ret = true; } } return(ret); } catch (Exception e) { Output.WriteLine(e); return(false); } }
public unsafe bool Equip(Item item, MsgItemPosition position) { if (!_owner.Inventory.TryRemove(item.UniqueId, out item)) { return(false); } if (item.ItemId / 10000 == 105) { position = MsgItemPosition.LeftWeapon; } (_owner as Player)?.Send(MsgItem.Create(item.UniqueId, 0, 0, MsgItemType.RemoveInventory)); if (Unequip(position)) { (_owner as Player)?.ForceSend(new MsgItemInformation(item, position), (ushort)sizeof(MsgItemInformation)); AddOrUpdate(position, item); ScreenSystem.Send(_owner, MsgSpawn.Create(_owner as Player)); EntityLogic.Recalculate(_owner); } return(true); }
public static void Handle(ClientSocket socket, byte[] buffer) { var packetId = BitConverter.ToUInt16(buffer, 4); switch (packetId) { case 1000: { var msgLogin = (MsgLogin)buffer; var user = msgLogin.GetUsername(); var pass = msgLogin.GetPassword(); if (Global.Verbose) { Console.WriteLine($"Login request for {user} using password {pass}"); } msgLogin.UniqueId = Core.Random.Next(1_000_000, 10_000_000); var player = new Player(socket) { UniqueId = msgLogin.UniqueId, Username = user, Password = pass, Name = user }; socket.StateObject = player; foreach (var(uniqueId, entity) in Collections.Players) { if (entity.Username == user && entity.Password == pass) { player.UniqueId = uniqueId; break; } } Collections.Players.TryAdd(player.UniqueId, player); if (msgLogin.UniqueId != 0) { Console.WriteLine("Authentication successful. Your customer Id is: " + player.UniqueId); } else { Console.WriteLine("Authentication failed."); } player.Socket.Send(msgLogin); player.Socket.Send(MsgSpawn.Create(player.UniqueId, player.Location, 3, player.Name)); break; } case 1001: { var msgWalk = (MsgWalk)buffer; var player = (Player)socket.StateObject; if (player == null) { break; } player.Location = new Vector2(msgWalk.X, msgWalk.Y); msgWalk.TickCount = DateTime.UtcNow.Ticks; if (Global.Verbose) { Console.WriteLine($"Player: {player.Username} ({msgWalk.UniqueId}) moved to: {player.Location.X},{player.Location.Y}"); } foreach (var(_, entity) in Collections.Players) { if (entity.UniqueId == player.UniqueId) { continue; } entity.Socket.Send(msgWalk); } break; } case 1002: { var msgPing = (MsgPing)buffer; var delta = DateTime.UtcNow.Ticks - msgPing.TickCount; var ms = delta / 10000; msgPing.Ping = (short)ms; socket.Send(msgPing); Console.WriteLine($"{DateTime.Now.ToLongTimeString()} Received Ping from {socket.Socket.RemoteEndPoint} - {ms}ms."); break; } } }