Пример #1
0
        public static void HandleNPCChat(Character chr, Packet packet)
        {
            int npcId = packet.ReadInt();
            var Npc   = chr.Field.GetNPC(npcId);

            if (chr.AssertForHack(!chr.CanAttachAdditionalProcess, "Tried to chat to npc while not able to attach additional process"))
            {
                InventoryPacket.NoChange(chr);
                return;
            }

            // Npc doesnt exist
            if (Npc == null)
            {
                InventoryPacket.NoChange(chr);
                return;
            }

            int RealID = Npc.ID;

            if (!DataProvider.NPCs.TryGetValue(RealID, out NPCData npc))
            {
                return;
            }

            if (npc.Shop.Count > 0)
            {
                // It's a shop!
                chr.ShopNPCID = RealID;
                NpcPacket.SendShowNPCShop(chr, RealID);
            }
            else if (npc.Trunk > 0)
            {
                chr.TrunkNPCID = RealID;
                StoragePacket.SendShowStorage(chr, chr.TrunkNPCID);
            }
            else
            {
                Action <string> errorHandlerFnc = null;
                if (chr.IsGM)
                {
                    errorHandlerFnc = (script) =>
                    {
                        MessagePacket.SendNotice("Error compiling script '" + script + "'!", chr);
                    };
                }

                INpcScript NPC = null;
                if (NPC == null && npc.Quest != null)
                {
                    NPC = Server.Instance.TryGetOrCompileScript(npc.Quest, errorHandlerFnc);
                }
                if (NPC == null)
                {
                    NPC = Server.Instance.TryGetOrCompileScript(npc.ID.ToString(), errorHandlerFnc);
                }

                NpcChatSession.Start(RealID, NPC, chr);
            }
        }
Пример #2
0
        public static void Start(int npcId, INpcScript NPC, Character chr)
        {
            if (NPC == null)
            {
                return;
            }

            if (chr.NpcSession != null)
            {
                return;
            }

            var session = new NpcChatSession(npcId, chr);

            session.SetScript((INpcScript)NPC.GetType().GetMethod("MemberwiseClone", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(NPC, null));
            session.HandleThing();
        }
Пример #3
0
        public static void HandleNPCChat(Character chr, Packet packet)
        {
            if (chr.NpcSession == null)
            {
                return;
            }

            NpcChatSession session = chr.NpcSession;
            byte           state   = packet.ReadByte();

            if (state != session.mLastSentType)
            {
                InventoryPacket.NoChange(chr);
                return;
            }

            if (!session.WaitingForResponse)
            {
                InventoryPacket.NoChange(chr);
                return;
            }
            session.WaitingForResponse = false;

            Trace.WriteLine(packet.ToString());

            byte option = packet.ReadByte();

            try
            {
                switch (state)
                {
                case 0:
                    switch (option)
                    {
                    case 0:         // Back button...
                        session.SendPreviousMessage();
                        break;

                    case 1:         // Next button...
                        session.SendNextMessage();
                        break;

                    default:
                        session.Stop();
                        break;
                    }
                    break;

                case 1:
                    switch (option)
                    {
                    case 0:         // No.
                        session.HandleThing(session.mRealState, 0, "", 0);
                        break;

                    case 1:         // Yes.
                        session.HandleThing(session.mRealState, 1, "", 0);
                        break;

                    default:
                        session.Stop();
                        break;
                    }
                    break;

                case 2:
                    switch (option)
                    {
                    case 0:         // No text :(
                        session.Stop();
                        break;

                    case 1:         // Oh yea, text
                        session.HandleThing(session.mRealState, 1, packet.ReadString(), 0);
                        break;

                    default:
                        session.Stop();
                        break;
                    }
                    break;

                case 3:
                    switch (option)
                    {
                    case 0:         // No int :(
                        session.Stop();
                        break;

                    case 1:         // Oh yea, int
                        session.HandleThing(session.mRealState, 1, "", packet.ReadShort());
                        break;

                    default:
                        session.Stop();
                        break;
                    }
                    break;

                case 4:
                case 5:
                    switch (option)
                    {
                    case 0:         // Stopping.
                        session.Stop();
                        break;

                    case 1:         // Got answer
                        var val = packet.ReadByte();
                        if (val == 255)
                        {
                            val = 0;                     // Menus do not correctly work when holding enter key
                        }
                        session.HandleThing(session.mRealState, val, "", 0);
                        break;

                    default:
                        session.Stop();
                        break;
                    }
                    break;

                default:
                    session.Stop();
                    Program.MainForm.LogAppend("Unknown NPC chat action: " + packet);
                    break;
                }
            }
            catch (Exception ex)
            {
                Program.MainForm.LogAppend($"Exception while handling NPC {session.mID} {session.mRealState}. Packet: " + packet + ". Exception: " + ex);
                InventoryPacket.NoChange(chr);
                session?.Stop();
            }
        }