using System; using System.Collections.Concurrent; using DragonDataSniffer.Network.Networking; using DragonDataSniffer.Data; namespace DragonDataSniffer.Object { public class NPC : MapObject { public ushort MapID { get; set; } public ushort MobID { get; private set; } public MobInfo PInfo { get; set; } public int X { get; private set; } public int Y { get; private set; } public byte Rot { get; private set; } public bool HasMenu { get; private set; } public NPCRole Role { get; set; } public NPCArgument Argument { get; set; } public byte IsGate { get; private set; } public string GateName { get; private set; } public ConcurrentDictionary ItemList { get; private set; } public NPC(SQLResult pRes, int i = 0) : base() { ushort MobID = pResult.Read(i, "MobID"); ushort MapID = pResult.Read(i, "MapID"); if (!MobDataProvider.GetMobInfoByID(MobID, out MobInfo mobInfo)) { throw new InvalidOperationException("Can't find MobInfo for NPC. Mob ID: " + MobID); } MobInfo = mobInfo; if (!MapDataProvider.GetMapInfoByID(MapID, out MapInfo mapInfo)) { throw new InvalidOperationException("Can't find MapInfo for NPC. Map ID: " + MapID); } int WayPointId = pResult.Read(i, "WayPoint"); WayPointInfo WayPoint = null; if (WayPointId != 0 && !MobDataProvider.GetWayPointById(WayPointId, out WayPoint)) { throw new InvalidOperationException($"Can't find Waypoint {WayPointId} for NPC MobId {MobID}"); } if (WayPoint != null) { WayPointInfo = WayPoint; } MapInfo = mapInfo; uint X = pResult.Read(i, "X"); uint Y = pResult.Read(i, "Y"); int RotationInt = pResult.Read(i, "Rotation"); Position = new Position(X, Y, (byte)(RotationInt < 0 ? ((360 + RotationInt) / 2) : (RotationInt / 2))); HasNPCMenu = pResult.Read(i, "HasMenu"); Role = (NPCRole)pResult.Read(i, "Role"); RoleArgument = (NPCArgument)pResult.Read(i, "RoleArgument"); } public NPC(FiestaPacket packet, ushort pMapID) : base() { string pGateName = ""; NPCRole role = NPCRole.None; if (!packet.TryReadUInt16(out ushort pObjectID) || !packet.TryReadByte(out byte pUnk) || !packet.TryReadUInt16(out ushort pMobID) || !packet.TryReadInt32(out int pX) || !packet.TryReadInt32(out int pY) || !packet.TryReadByte(out byte pRot) || !packet.TryReadByte(out byte pIsGate)) { return; } if (pIsGate == 1) { Role = NPCRole.Gate; packet.TryReadString(out pGateName, 12); packet.ReadSkip(125); } else { packet.ReadSkip(137); } ItemList = new ConcurrentDictionary(); MapObjectID = pObjectID; MobID = pMobID; MapID = pMapID; X = pX; Y = pY; Rot = pRot; IsGate = pIsGate; GateName = pGateName; Role = role; if (Manager.NPCDataManager.Instance.NPCByID.TryGetValue(pMobID, out MobInfo mInfo)) { PInfo = mInfo; } } public bool AddItem(NPCItem pItem, bool Save = false) { if (ItemList.TryAdd(pItem.Slot, pItem)) { if (Save) { pItem.AddToDB(); } return true; } return false; } public void UpdateArgument() { string update = "UPDATE NPCTable SET RoleArgument = '" + (byte)Argument + "',Role = '" + (byte)Role + "' WHERE MobID = '" + MobID + "'"; DatabaseManager.RunSQL(update); } public void AddToDB() { string NpcString = "INSERT INTO NPCTable (MobID,MapID,X,Y,Rotation,HasMenu,Role,RoleArgument)" + "VALUES (" + "'" + MobID + "'," + "'" + MapID + "'," + "'" + X + "'," + "'" + Y + "'," + "'" + Rot + "'," + "'" + Convert.ToByte(HasMenu) + "'," + "'" + (byte)Role + "'," + "'" + (byte)Argument + "')"; DatabaseManager.RunSQL(NpcString); } } }