Page 1 of 2

Dragonscale Merchant

PostPosted: Sat Jan 16, 2010 5:03 pm
by geshi
Code: Select all
    public class GameDragonScaleMerchant : GameCountMerchant
    {
        public GameDragonScaleMerchant()
            : base()
        {
            m_countText = "Dragonscales";
        }
        GameInventoryItem[] TheScales = new GameInventoryItem[6]
            {
              GameInventoryItem.CreateFromTemplate("ElderDragonScale"),   // 
              GameInventoryItem.CreateFromTemplate("MoltedDragonScale"),  //
              GameInventoryItem.CreateFromTemplate("MoltedZhulrathuulScale"),
              GameInventoryItem.CreateFromTemplate("MoltedXanxicarScale"),
              GameInventoryItem.CreateFromTemplate("YoungDragonScale"),
              GameInventoryItem.CreateFromTemplate("AdultDragonScale")
            }; // array ends
        public override void OnPlayerBuy(GamePlayer player, int item_slot, int number)
        {
            if (TheScales[0] == null || TheScales[0].Item == null)
                return;
            //Get the template
            int pagenumber = item_slot / MerchantTradeItems.MAX_ITEM_IN_TRADEWINDOWS;
            int slotnumber = item_slot % MerchantTradeItems.MAX_ITEM_IN_TRADEWINDOWS;

            ItemTemplate template = this.TradeItems.GetItem(pagenumber, (eMerchantWindowSlot)slotnumber);
            if (template == null) return;

            //Calculate the amout of items
            int amountToBuy = number;
            if (template.PackSize > 0)
                amountToBuy *= template.PackSize;

            if (amountToBuy <= 0) return;

            //Calculate the value of items
            long totalValue = number * template.Value;

            lock (player.Inventory)
            {
                int MasterAmount = 0;
                for (int i = 0; i < TheScales.Length; i++)
                {
                    MasterAmount += player.Inventory.CountItemTemplate(TheScales[i].Item.Id_nb, eInventorySlot.FirstBackpack, eInventorySlot.LastBackpack);
                }
                if (MasterAmount < totalValue)
                {
                    player.Out.SendMessage(LanguageMgr.GetTranslation(player.Client, "GameMerchant.OnPlayerBuy.YouNeed2", totalValue, m_countText), eChatType.CT_System, eChatLoc.CL_SystemWindow);
                    return;
                }
                if (!player.Inventory.AddTemplate(template, amountToBuy, eInventorySlot.FirstBackpack, eInventorySlot.LastBackpack))
                {
                    player.Out.SendMessage(LanguageMgr.GetTranslation(player.Client, "GameMerchant.OnPlayerBuy.NotInventorySpace"), eChatType.CT_System, eChatLoc.CL_SystemWindow);

                    return;
                }
                //Generate the buy message
                string message;
                if (amountToBuy > 1)
                    message = LanguageMgr.GetTranslation(player.Client, "GameMerchant.OnPlayerBuy.BoughtPieces2", amountToBuy, template.GetName(1, false), totalValue, m_countText);
                else
                    message = LanguageMgr.GetTranslation(player.Client, "GameMerchant.OnPlayerBuy.Bought2", template.GetName(1, false), totalValue, m_countText);

                System.Collections.IList items = (System.Collections.IList)player.Inventory.GetItemRange(eInventorySlot.FirstBackpack, eInventorySlot.LastBackpack);
                int removed = 0;

                foreach (InventoryItem item in items)
                {
                    int check = 0;
                    for (int i = 0; i < TheScales.Length; i++)
                    {
                        if (item.Id_nb != TheScales[i].Item.Id_nb)
                            check += 1;
                    }
                    if (check == TheScales.Length)
                        continue;
                    int remFromStack = Math.Min(item.Count, (int)(totalValue - removed));
                    player.Inventory.RemoveCountFromStack(item, remFromStack);
                    removed += remFromStack;
                    if (removed == totalValue)
                        break;
                }
                player.Out.SendInventoryItemsUpdate(items);
                player.Out.SendMessage(message, eChatType.CT_Merchant, eChatLoc.CL_SystemWindow);
            }
        }
        public override bool Interact(GamePlayer player)
        {
            if (!base.Interact(player))
                return false;

            TurnTo(player, 10000);
            string text = "Dragonscales";
            player.Out.SendMessage(LanguageMgr.GetTranslation(player.Client, "GameMerchant.GetExamineMessages.BuyItemsFor", this.Name, text), eChatType.CT_Say, eChatLoc.CL_ChatWindow);
            return true;
        }


    }


Hi here is Dragonscale Merchant.. i think i added all the scales that he should take....

Please tell me if i could have done it better i want to learn more :P

Re: Dragonscale Merchant

PostPosted: Sat Jan 16, 2010 10:17 pm
by Graveen
interesting geshi, ty.

Re: Dragonscale Merchant

PostPosted: Sat Jan 16, 2010 10:37 pm
by stephenxpimentel
this will not work i don't think. and the reason for this is because if you have more than 1 stack with more than enough in it, it will take from them all, or if u have 1 stack with 1 in it (on top) then 1 with more than enough below, it will only remove the 1 stack on top.. look at ur inventory like this.


1 Super Dragonscale
99 Mediocre Dragonscales


then you buy something for say 23 scales, your inventory will look like this.

99 Mediocre Dragonscales

This is my thoughts anyways, i had this problem with the Casino manager i was working on with you, and i fixed it but the fix can't apply to this. Test it and let me know =]

Re: Dragonscale Merchant

PostPosted: Sat Jan 16, 2010 11:36 pm
by geshi
Me & Stephen just tested it more and it works fine :D

Re: Dragonscale Merchant

PostPosted: Sun Apr 11, 2010 4:13 pm
by Graveen
Thank you. This NPC is livelike ?

Re: Dragonscale Merchant

PostPosted: Sun Apr 11, 2010 4:59 pm
by geshi
yes, but this script need editing, I will in 2 months :( PC is broken and in 2 months i get new :D

Re: Dragonscale Merchant

PostPosted: Sun Apr 11, 2010 5:03 pm
by Graveen
Ok, i wait you could rewrite it ! If it is livelike, no troubles to have it in svn ;)

Re: Dragonscale Merchant

PostPosted: Sun Apr 11, 2010 5:22 pm
by Aredhel
geshi wrote:yes, but this script need editing, I will in 2 months :( PC is broken and in 2 months i get new :D


Two months without a PC.. I think I'd die if that happened to me :shock:

Re: Dragonscale Merchant

PostPosted: Sun Apr 11, 2010 5:26 pm
by geshi
Aredhel wrote:
Two months without a PC.. I think I'd die if that happened to me :shock:

hehe i have laptop but it can just about run firefox :roll:

Graveen wrote:Ok, i wait you could rewrite it ! If it is livelike, no troubles to have it in svn ;)


It's npc from live that takes several currency for Dragon Items (Fine Steel Longsword etc) not with merchantlist though..

Re: Dragonscale Merchant

PostPosted: Mon Apr 12, 2010 10:07 am
by Dinberg
Whats the use of this bit in the code btw?

Code: Select all
            if (TheScales[0] == null || TheScales[0].Item == null)
                return;

Re: Dragonscale Merchant

PostPosted: Mon Apr 12, 2010 11:10 am
by geshi
Code: Select all
              GameInventoryItem.CreateFromTemplate("ElderDragonScale"),   // 


If ElderDragonScale does not exist then the npc won't do anything, this is a bad fix and it really should check for all the scales and not just one type.

The script looks so bad now :(

Re: Dragonscale Merchant

PostPosted: Fri Apr 16, 2010 9:45 pm
by stephenxpimentel
do an i variable imo :)

Re: Dragonscale Merchant

PostPosted: Fri Apr 16, 2010 11:20 pm
by geshi
stephenxpimentel wrote:do an i variable imo :)

why i ? i like to giev my variables names so I remember what they are for :cry: or what do you mean? :s

Re: Dragonscale Merchant

PostPosted: Sat Apr 17, 2010 3:37 am
by stephenxpimentel
geshi wrote:
stephenxpimentel wrote:do an i variable imo :)

why i ? i like to giev my variables names so I remember what they are for :cry: or what do you mean? :s


is exactly what i meant hehe

for (int i ) etc.

tbh i like to give them names too, but in this case what would u do?

Code: Select all
for (int scaleType = 0; scaleType < TheScales.Length; i++)
{
if (TheScales[scaleType] == null || TheScales[scaleType].Item == null)
return;
}


seems a little funky but its fine, your variables ur names!! =]

Re: Dragonscale Merchant

PostPosted: Sat Apr 17, 2010 8:25 am
by Aredhel
Only use capitalized names for classes, methods and properties - is TheScales an indexed property or a mere variable/class member?