Пример #1
0
        private object repairStructure(BaseCombatEntity entity, BasePlayer player)
        {
            if (entity.SecondsSinceAttacked <= (int)Config["nTimeSinceAttacked"])
            {
                return(null);
            }
            if (!(bool)Config["bChargeForRepairs"])
            {
                entity.health = entity.MaxHealth();
                entity.OnRepair();
                entity.SendNetworkUpdateImmediate(true);
                return(false);
            }

            float hp = entity.health;
            int   i  = 0;
            Dictionary <int, int> charge = new Dictionary <int, int>();

            while (hp < entity.MaxHealth())
            {
                if (i >= 30)
                {
                    Puts("Breaking loop -- Something went wrong");
                    break;
                }
                i += 1;
                float single  = 50f;
                float single1 = entity.MaxHealth() - hp;
                single1 = Mathf.Clamp(single1, 0f, single);
                float single2 = single1 / entity.MaxHealth();
                if (single2 == 0f)
                {
                    return(false);
                }
                List <ItemAmount> itemAmounts = entity.RepairCost(single2);
                if (itemAmounts == null)
                {
                    return(false);
                }
                float single3 = itemAmounts.Sum <ItemAmount>((ItemAmount x) => x.amount);
                if (single3 <= 0f)
                {
                    hp = entity.MaxHealth();
                }
                else
                {
                    float single4 = itemAmounts.Min <ItemAmount>((ItemAmount x) => Mathf.Clamp01((float)player.inventory.GetAmount(x.itemid) / x.amount));
                    if (single4 == 0f)
                    {
                        return(false);
                    }
                    int num = 0;
                    foreach (ItemAmount itemAmount in itemAmounts)
                    {
                        int num1 = Mathf.CeilToInt(single4 * itemAmount.amount);
                        int num2 = num1;
                        num = num + num2;
                        if (charge.ContainsKey(itemAmount.itemid))
                        {
                            charge[itemAmount.itemid] = charge[itemAmount.itemid] + num2;
                        }
                        else
                        {
                            charge.Add(itemAmount.itemid, num2);
                        }
                    }
                    float single5 = (float)num / (float)single3;
                    hp = hp + single1 * single5;
                    entity.OnRepair();
                }
            }
            foreach (KeyValuePair <int, int> item in charge)
            {
                ItemDefinition defs;
                defs = ItemManager.FindItemDefinition(item.Key);
                if (player.inventory.GetAmount(defs.itemid) < item.Value)
                {
                    return(null);
                }
            }
            foreach (KeyValuePair <int, int> item in charge)
            {
                player.inventory.Take(null, item.Key, item.Value);
                player.Command("note.inv", new object[] { item.Key, item.Value * -1 });
            }
            entity.health = entity.MaxHealth();
            entity.SendNetworkUpdateImmediate(true);
            return(false);
        }