Exemplo n.º 1
0
        void SendTrainerList(ObjectGuid guid, string title, uint index = 0)
        {
            Creature unit = GetPlayer().GetNPCIfCanInteractWith(guid, NPCFlags.Trainer);

            if (unit == null)
            {
                Log.outDebug(LogFilter.Network, "WORLD: SendTrainerList - {0} not found or you can not interact with him.", guid.ToString());
                return;
            }

            // remove fake death
            if (GetPlayer().HasUnitState(UnitState.Died))
            {
                GetPlayer().RemoveAurasByType(AuraType.FeignDeath);
            }

            TrainerSpellData trainer_spells = unit.GetTrainerSpells();

            if (trainer_spells == null)
            {
                Log.outDebug(LogFilter.Network, "WORLD: SendTrainerList - Training spells not found for {0}", guid.ToString());
                return;
            }

            TrainerList packet = new TrainerList();

            packet.TrainerGUID = guid;
            packet.TrainerType = (int)trainer_spells.trainerType;
            packet.Greeting    = title;

            // reputation discount
            float fDiscountMod = GetPlayer().GetReputationPriceDiscount(unit);

            foreach (var tSpell in trainer_spells.spellList.Values)
            {
                if (index != 0 && tSpell.Index != index)
                {
                    continue;
                }

                bool valid = true;
                for (var i = 0; i < SharedConst.MaxTrainerspellAbilityReqs; i++)
                {
                    if (tSpell.ReqAbility[i] == 0)
                    {
                        continue;
                    }

                    if (!GetPlayer().IsSpellFitByClassAndRace(tSpell.ReqAbility[i]))
                    {
                        valid = false;
                        break;
                    }
                }

                if (!valid)
                {
                    continue;
                }

                TrainerSpellState state = GetPlayer().GetTrainerSpellState(tSpell);

                TrainerListSpell spell = new TrainerListSpell();
                spell.SpellID      = (int)tSpell.SpellID;
                spell.MoneyCost    = (int)Math.Floor(tSpell.MoneyCost * fDiscountMod);
                spell.ReqSkillLine = (int)tSpell.ReqSkillLine;
                spell.ReqSkillRank = (int)tSpell.ReqSkillRank;
                spell.ReqLevel     = (byte)tSpell.ReqLevel;
                spell.Usable       = (state == TrainerSpellState.GreenDisabled ? TrainerSpellState.Green : state);

                byte maxReq = 0;
                for (var i = 0; i < SharedConst.MaxTrainerspellAbilityReqs; ++i)
                {
                    if (tSpell.ReqAbility[i] == 0)
                    {
                        continue;
                    }

                    uint prevSpellId = Global.SpellMgr.GetPrevSpellInChain(tSpell.ReqAbility[i]);
                    if (prevSpellId != 0)
                    {
                        spell.ReqAbility[maxReq] = (int)prevSpellId;
                        ++maxReq;
                    }

                    if (maxReq == 2)
                    {
                        break;
                    }

                    var spellsRequired = Global.SpellMgr.GetSpellsRequiredForSpellBounds(tSpell.ReqAbility[i]);
                    for (var c = 0; c < spellsRequired.Count && maxReq < SharedConst.MaxTrainerspellAbilityReqs; ++c)
                    {
                        spell.ReqAbility[maxReq] = (int)spellsRequired[c];
                        ++maxReq;
                    }

                    if (maxReq == 2)
                    {
                        break;
                    }
                }

                packet.Spells.Add(spell);
            }

            SendPacket(packet);
        }
Exemplo n.º 2
0
        public static void HandleTrainerList(ref PacketReader packet, ref WorldSession session)
        {
            ulong  guid = packet.ReadUInt64();
            string str  = ObjMgr.GetCypherString(CypherStrings.NpcTrainerHello);

            Creature unit = session.GetPlayer().GetNPCIfCanInteractWith(guid, NPCFlags.Trainer);

            if (unit == null)
            {
                Log.outDebug("WORLD: SendTrainerList - Unit (GUID: {0}) not found or you can not interact with him.", ObjectGuid.GuidLowPart(guid));
                return;
            }

            // remove fake death
            //if (GetPlayer()->HasUnitState(UNIT_STATE_DIED))
            //GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);

            // trainer list loaded at check;
            if (!unit.isTrainerOf(session.GetPlayer(), true))
            {
                return;
            }

            CreatureTemplate ci = unit.GetCreatureTemplate();

            if (ci == null)
            {
                Log.outDebug("WORLD: SendTrainerList - (GUID: {0}) NO CREATUREINFO!", ObjectGuid.GuidLowPart(guid));
                return;
            }
            TrainerSpellData trainer_spells = unit.GetTrainerSpells();

            if (trainer_spells == null)
            {
                Log.outDebug("WORLD: SendTrainerList - Training spells not found for creature (GUID: {0} Entry: {1})", ObjectGuid.GuidLowPart(guid), unit.GetEntry());
                return;
            }
            PacketWriter data = new PacketWriter(Opcodes.SMSG_TrainerList);

            data.WriteUInt64(guid);
            data.WriteUInt32(trainer_spells.trainerType);
            data.WriteUInt32(133);

            int count_pos = data.wpos();

            data.WriteUInt32(trainer_spells.spellList.Count);

            // reputation discount
            float fDiscountMod           = session.GetPlayer().GetReputationPriceDiscount(unit);
            bool  can_learn_primary_prof = session.GetPlayer().GetFreePrimaryProfessionPoints() > 0;

            uint count = 0;

            foreach (var spell in trainer_spells.spellList.Values)
            {
                bool valid = true;
                bool primary_prof_first_rank = false;
                for (var i = 0; i < 3; ++i)
                {
                    if (spell.learnedSpell[i] == 0)
                    {
                        continue;
                    }
                    if (!session.GetPlayer().IsSpellFitByClassAndRace(spell.learnedSpell[i]))
                    {
                        valid = false;
                        break;
                    }
                    SpellInfo spellentry = SpellMgr.GetSpellInfo(spell.learnedSpell[i]);
                    if (spellentry.IsPrimaryProfessionFirstRank())
                    {
                        primary_prof_first_rank = true;
                    }
                }
                if (!valid)
                {
                    continue;
                }

                TrainerSpellState state = session.GetPlayer().GetTrainerSpellState(spell);
                data.WriteUInt32(spell.spellId);                      // learned spell (or cast-spell in profession case)
                data.WriteUInt8((byte)(state == TrainerSpellState.GreenDisabled ? TrainerSpellState.Green : state));
                data.WriteUInt32((uint)Math.Floor(spell.spellCost * fDiscountMod));

                data.WriteUInt8((byte)spell.reqLevel);
                data.WriteUInt32(spell.reqSkill);
                data.WriteUInt32(spell.reqSkillValue);
                //prev + req or req + 0
                var maxReq = 0;
                for (var i = 0; i < 3; ++i)
                {
                    if (spell.learnedSpell[i] == 0)
                    {
                        continue;
                    }
                    uint prevSpellId = SpellMgr.GetPrevSpellInChain(spell.learnedSpell[i]);
                    if (prevSpellId != 0)
                    {
                        data.WriteUInt32(prevSpellId);
                        maxReq++;
                    }
                    if (maxReq == 2)
                    {
                        break;
                    }

                    //SpellsRequiringSpellMapBounds spellsRequired = sSpellMgr->GetSpellsRequiredForSpellBounds(tSpell->learnedSpell[i]);
                    //for (SpellsRequiringSpellMap::const_iterator itr2 = spellsRequired.first; itr2 != spellsRequired.second && maxReq < 3; ++itr2)
                    {
                        //data.WriteUInt32(itr2->second);
                        //++maxReq;
                    }
                    //if (maxReq == 2)
                    //break;
                }
                while (maxReq < 2)
                {
                    data.WriteUInt32(0);
                    maxReq++;
                }

                data.WriteInt32(primary_prof_first_rank && can_learn_primary_prof ? 1 : 0);
                count++;
            }
            data.WriteCString(str);
            data.Replace <uint>(count_pos, count);
            session.Send(data);
        }