Exemple #1
0
        public void tryLootDrop(IEnumerable <Player> players, Bot dead, short posX, short posY)
        {
            if (players.Count() == 0)
            {
                return;
            }

            using (LogAssume.Assume(_logger))
            {
                LootTable lootTable = getLootTableByID(dead._type.Id);

                if (lootTable == null)
                {
                    Log.write("Could not find loot table for bot id {0}", dead._type.Id);
                    return;
                }

                int weightRoll = 0;
                //Spawn all of our normal items
                if (lootTable.normalLoot.Count > 0)
                {
                    foreach (LootInfo loot in lootTable.normalLoot)
                    {
                        if (loot._weight >= 100)
                        {
                            itemSpawn(loot._item, (ushort)loot._quantity, posX, posY, null);
                        }
                        else
                        {
                            weightRoll = _rand.Next(0, 100);
                            if (loot._weight >= weightRoll)
                            {
                                itemSpawn(loot._item, (ushort)loot._quantity, posX, posY, null);
                            }
                        }
                    }
                }


                int total    = 0;
                int position = 0;

                total += lootTable.commonChance;
                total += lootTable.uncommonChance;
                total += lootTable.setChance;
                total += lootTable.rareChance;

                Range commonRange = new Range(position, position + lootTable.commonChance);
                position += lootTable.commonChance;
                Range uncommonRange = new Range(position, position + lootTable.uncommonChance);
                position += lootTable.uncommonChance;
                Range setRange = new Range(position, position + lootTable.setChance);
                position += lootTable.setChance;
                Range rareRange = new Range(position, position + lootTable.rareChance);
                position  += lootTable.rareChance;
                weightRoll = 0;
                List <LootInfo> potentialLoot = new List <LootInfo>();
                foreach (Player player in players)
                {
                    Log.write("Trying loot drop for {0}", player._alias);
                    weightRoll = _rand.Next(0, total);

                    Log.write("Category Weight Roll: {0}", weightRoll);

                    if (weightRoll.IsWithin(commonRange.min, commonRange.max))
                    {
                        Log.write("selected commmon loot list");
                        potentialLoot = lootTable.commonLoot;
                    }
                    else if (weightRoll.IsWithin(uncommonRange.min, uncommonRange.max))
                    {
                        Log.write("selected uncommon loot list");
                        potentialLoot = lootTable.uncommonLoot;
                    }
                    else if (weightRoll.IsWithin(setRange.min, setRange.max))
                    {
                        Log.write("selected set loot list");
                        potentialLoot = lootTable.setLoot;
                    }
                    else if (weightRoll.IsWithin(rareRange.min, rareRange.max))
                    {
                        Log.write("selected rare loot list");
                        potentialLoot = lootTable.rareLoot;
                    }
                    else
                    {
                        //No loot for this guy :(
                        if (potentialLoot.Count == 0)
                        {
                            Log.write("no loot list selected");
                            continue;
                        }
                    }



                    int totalLootProbability = 0;
                    int rangePosition        = 0;
                    Dictionary <Range, LootInfo> lootRanges = new Dictionary <Range, LootInfo>();

                    foreach (LootInfo loot in potentialLoot)
                    {
                        totalLootProbability += loot._weight;
                        lootRanges.Add(new Range(rangePosition, rangePosition + loot._weight), loot);
                        rangePosition += loot._weight;
                    }

                    weightRoll = _rand.Next(0, totalLootProbability);
                    Log.write("Item Weight Roll: {0}", weightRoll);
                    LootInfo drop = lootRanges.FirstOrDefault(rng => weightRoll.IsWithin(rng.Key.min, rng.Key.max)).Value;

                    //Better luck next time!
                    if (drop == null)
                    {
                        continue;
                    }

                    //Likely a null item to simulate chance of no drop at all
                    if (drop._item == null)
                    {
                        continue;
                    }

                    //Give the lad his loot!
                    privateItemSpawn(drop._item, (ushort)drop._quantity, dead._state.positionX, dead._state.positionY, player, drop._type, dead, drop._name);
                }
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public LootTable(string filename)
        {
            commonLoot   = new List <LootInfo>();
            uncommonLoot = new List <LootInfo>();
            setLoot      = new List <LootInfo>();
            rareLoot     = new List <LootInfo>();
            normalLoot   = new List <LootInfo>();

            XmlDocument Doc = new XmlDocument();

            Doc.Load(filename);

            XmlNode header = Doc.SelectSingleNode("lootTable");

            botID = Convert.ToInt32(header.Attributes["botID"].Value);
            name  = header.Attributes["name"].Value;

            foreach (XmlNode Node in Doc.SelectNodes("lootTable/normal"))
            {
                foreach (XmlNode child in Node.ChildNodes)
                {
                    name = child.Attributes["name"].Value;
                    int itemid   = Convert.ToInt32(child.Attributes["itemID"].Value);
                    int chance   = Convert.ToInt32(child.Attributes["chance"].Value);
                    int quantity = Convert.ToInt32(child.Attributes["quantity"].Value);

                    LootInfo lootInfo = new LootInfo(name, itemid, chance, quantity, LootType.Normal);
                    normalLoot.Add(lootInfo);
                }
            }

            foreach (XmlNode Node in Doc.SelectNodes("lootTable/common"))
            {
                commonChance = Convert.ToInt32(Node.Attributes["chance"].Value);
                commonCount  = Convert.ToInt32(Node.Attributes["count"].Value);

                foreach (XmlNode child in Node.ChildNodes)
                {
                    name = child.Attributes["name"].Value;
                    int itemid   = Convert.ToInt32(child.Attributes["itemID"].Value);
                    int chance   = Convert.ToInt32(child.Attributes["chance"].Value);
                    int quantity = Convert.ToInt32(child.Attributes["quantity"].Value);

                    LootInfo lootInfo = new LootInfo(name, itemid, chance, quantity, LootType.Common);
                    commonLoot.Add(lootInfo);
                }
            }
            foreach (XmlNode Node in Doc.SelectNodes("lootTable/uncommon"))
            {
                uncommonChance = Convert.ToInt32(Node.Attributes["chance"].Value);
                uncommonCount  = Convert.ToInt32(Node.Attributes["count"].Value);
                foreach (XmlNode child in Node.ChildNodes)
                {
                    name = child.Attributes["name"].Value;
                    int      itemid   = Convert.ToInt32(child.Attributes["itemID"].Value);
                    int      chance   = Convert.ToInt32(child.Attributes["chance"].Value);
                    int      quantity = Convert.ToInt32(child.Attributes["quantity"].Value);
                    LootInfo lootInfo = new LootInfo(name, itemid, chance, quantity, LootType.Uncommon);
                    uncommonLoot.Add(lootInfo);
                }
            }
            foreach (XmlNode Node in Doc.SelectNodes("lootTable/set"))
            {
                setChance = Convert.ToInt32(Node.Attributes["chance"].Value);
                setCount  = Convert.ToInt32(Node.Attributes["count"].Value);
                foreach (XmlNode child in Node.ChildNodes)
                {
                    name = child.Attributes["name"].Value;
                    int      itemid   = Convert.ToInt32(child.Attributes["itemID"].Value);
                    int      chance   = Convert.ToInt32(child.Attributes["chance"].Value);
                    int      quantity = Convert.ToInt32(child.Attributes["quantity"].Value);
                    LootInfo lootInfo = new LootInfo(name, itemid, chance, quantity, LootType.Set);
                    setLoot.Add(lootInfo);
                }
            }
            foreach (XmlNode Node in Doc.SelectNodes("lootTable/rare"))
            {
                rareChance = Convert.ToInt32(Node.Attributes["chance"].Value);
                rareCount  = Convert.ToInt32(Node.Attributes["count"].Value);
                foreach (XmlNode child in Node.ChildNodes)
                {
                    name = child.Attributes["name"].Value;
                    int      itemid   = Convert.ToInt32(child.Attributes["itemID"].Value);
                    int      chance   = Convert.ToInt32(child.Attributes["chance"].Value);
                    int      quantity = Convert.ToInt32(child.Attributes["quantity"].Value);
                    LootInfo lootInfo = new LootInfo(name, itemid, chance, quantity, LootType.Rare);
                    rareLoot.Add(lootInfo);
                }
            }
        }