public bool ShouldVisitChildren(Equipment equipment, int times)
 {
     return(times == 0 && Children.Count > 0 && _conditionResolution.IsValid(Condition, equipment));
 }
        public void CountSubconditionIsValidTest()
        {
            int range        = 10;
            int conditionMin = _random.Next(range);
            int conditionMax = range + _random.Next(range);

            string modType = "TestModType";

            var subcondition = CreateCraftingSubcondition(
                SubconditionAggregateType.Count,
                StatValueType.Flat,
                1,
                1,
                CreateTestAffix(conditionMin, conditionMax, modType));

            CraftingCondition condition = CreateTestCondition(subcondition);

            Equipment item = new Equipment();

            Stat stat = new Stat();

            stat.Value1 = range;

            Affix affix = new Affix();

            affix.Group          = modType;
            affix.GenerationType = "Prefix";

            stat.Affix = affix;

            item.Stats.Add(stat);

            Assert.IsTrue(_conditionResolver.IsValid(condition, item));
        }
Example #3
0
        private SimulationArtifacts Run(CancellationToken ct)
        {
            // Check for recursive/duplicate crafting steps
            if (RecursionCheck(new HashSet <Model.Crafting.Steps.ICraftingStep>(), _craftingInfo.CraftingSteps))
            {
                throw new ArgumentException("Crafting steps are infinitely recursive");
            }

            // Pay for the first item
            SimulationArtifacts.CostInChaos += _baseItemInfo.ItemCost;

            double previousProgress = -1;

            var baseInfluence   = InfluenceToDomain(_baseItemInfo.Influence);
            var craftingSteps   = CraftingStepsToDomain(_craftingInfo.CraftingSteps);
            var craftingTargets = _craftingInfo.CraftingTargets.Select(x =>

                                                                       new PoeCraftLib.Entities.Items.CraftingTarget()
            {
                Name      = x.Name,
                Value     = x.Value,
                Condition = ConditionToDomain(x.Condition)
            }).ToList();

            for (ProgressManager progressManager = GetProgressManager(); progressManager.Progress < 100; previousProgress = progressManager.Progress)
            {
                // Craft item
                var item    = _itemFactory.ToEquipment(_baseItem, _baseItemInfo.ItemLevel, baseInfluence);
                var results = _craftingManager.Craft(craftingSteps, item, _affixManager, ct, progressManager);

                bool saved = false;

                // No normal items are evaluated since that would cause a lot of clutter
                if (results.Result.Rarity != EquipmentRarity.Normal)
                {
                    var equipment = EquipmentToClient(results.Result);
                    SimulationArtifacts.AllGeneratedItems.Add(equipment);

                    foreach (var craftingTarget in craftingTargets)
                    {
                        if (craftingTarget.Condition != null &&
                            _conditionResolution.IsValid(craftingTarget.Condition, results.Result))
                        {
                            if (!SimulationArtifacts.MatchingGeneratedItems.ContainsKey(craftingTarget.Name))
                            {
                                SimulationArtifacts.MatchingGeneratedItems.Add(craftingTarget.Name, new List <Equipment>());
                            }

                            SimulationArtifacts.MatchingGeneratedItems[craftingTarget.Name].Add(equipment);
                            saved = true;
                            break;
                        }
                    }
                }

                // Update crafting cost
                foreach (var result in results.CraftingStepMetadata)
                {
                    foreach (var currency in result.Value.CurrencyAmounts)
                    {
                        if (!SimulationArtifacts.CurrencyUsed.ContainsKey(currency.Key))
                        {
                            SimulationArtifacts.CurrencyUsed.Add(currency.Key, 0);
                        }
                        // The progress manager is updated with this information in the craft manager
                        int amountOfCurrencyUsed = currency.Value * result.Value.TimesModified;
                        SimulationArtifacts.CurrencyUsed[currency.Key] += amountOfCurrencyUsed;
                        SimulationArtifacts.CostInChaos += amountOfCurrencyUsed * _currencyValues[currency.Key];
                    }
                }

                if (progressManager.Progress < 100)
                {
                    // Get a new item ready.
                    if (saved ||
                        results.Result.Corrupted ||
                        results.Result.Rarity != EquipmentRarity.Normal &&
                        _baseItemInfo.ItemCost <= _currencyValues[CurrencyNames.ScouringOrb])
                    {
                        SimulationArtifacts.CostInChaos += _baseItemInfo.ItemCost;
                        progressManager.AddCost(_baseItemInfo.ItemCost);
                    }
                    else if (results.Result.Rarity != EquipmentRarity.Normal)
                    {
                        SimulationArtifacts.CostInChaos += _currencyValues[CurrencyNames.ScouringOrb];
                        progressManager.SpendCurrency(CurrencyNames.ScouringOrb, 1);
                    }
                }

                // Check for no crafting steps
                if (Math.Abs(previousProgress - progressManager.Progress) < double.Epsilon)
                {
                    throw new ArgumentException("Crafting steps do not spend currency");
                }
            }

            if (OnSimulationComplete != null)
            {
                Status = SimulationStatus.Completed;

                var args = new SimulationCompleteEventArgs
                {
                    SimulationArtifacts = SimulationArtifacts
                };

                OnSimulationComplete(args);
            }

            return(SimulationArtifacts);
        }
Example #4
0
        private SimulationArtifacts Run(CancellationToken ct)
        {
            // Pay for the first item
            _simulationArtifacts.CostInChaos += _baseItemInfo.ItemCost;

            for (Progress = 0; Progress < 100; Progress = GetSimulationProgress())
            {
                // Craft item
                var item    = _itemFactory.ToEquipment(_baseItem, _baseItemInfo.ItemLevel, _baseItemInfo.Influence);
                var results = _craftingManager.Craft(_craftingInfo.CraftingSteps, item, _affixManager, ct, _financeInfo.BudgetInChaos - _simulationArtifacts.CostInChaos);

                bool saved = false;
                // Update item results
                foreach (var craftingTarget in _craftingInfo.CraftingTargets)
                {
                    if (craftingTarget.Condition != null && _conditionResolution.IsValid(craftingTarget.Condition, results.Result))
                    {
                        _simulationArtifacts.MatchingGeneratedItems[craftingTarget].Add(results.Result);
                        saved = true;
                        break;
                    }
                }

                _simulationArtifacts.AllGeneratedItems.Add(results.Result);

                // Update crafting cost
                foreach (var result in results.CraftingStepMetadata)
                {
                    foreach (var currency in result.Value.CurrencyAmounts)
                    {
                        if (!_simulationArtifacts.CurrencyUsed.ContainsKey(currency.Key))
                        {
                            _simulationArtifacts.CurrencyUsed.Add(currency.Key, 0);
                        }

                        int amountOfCurrencyUsed = currency.Value * result.Value.TimesModified;
                        _simulationArtifacts.CurrencyUsed[currency.Key] += amountOfCurrencyUsed;
                        _simulationArtifacts.CostInChaos += amountOfCurrencyUsed * _currencyValues[currency.Key];
                    }
                }

                if (GetSimulationProgress() < 100)
                {
                    // Get a new item ready.
                    if (saved ||
                        results.Result.Corrupted ||
                        results.Result.Rarity != EquipmentRarity.Normal &&
                        _baseItemInfo.ItemCost <= _currencyValues[CurrencyNames.ScouringOrb])
                    {
                        _simulationArtifacts.CostInChaos += _baseItemInfo.ItemCost;
                    }
                    else if (results.Result.Rarity != EquipmentRarity.Normal)
                    {
                        _simulationArtifacts.CostInChaos += _currencyValues[CurrencyNames.ScouringOrb];
                    }
                }

                // Update progress results
                if (OnProgressUpdate != null)
                {
                    var args = new ProgressUpdateEventArgs
                    {
                        Progress = Progress
                    };

                    OnProgressUpdate(args);
                }
            }

            if (OnSimulationComplete != null)
            {
                Status = SimulationStatus.Completed;

                var args = new SimulationCompleteEventArgs
                {
                    SimulationArtifacts = _simulationArtifacts
                };

                OnSimulationComplete(args);
            }

            return(_simulationArtifacts);
        }
Example #5
0
        private void Run(CancellationToken ct)
        {
            var scourCost = _currencyFactory.Currency.First(x => x.Name == CurrencyNames.ScouringOrb).Value;

            for (var progress = 0d; progress < 100; progress = _craftingTree.GetCurrencySpent(ScourCount, BaseItemCount, BaseItemCost) / Currency * 100)
            {
                if (ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();
                }

                var newItem = _equipmentFactory.CreateEquipment();
                var result  = _craftingTree.Craft(newItem, ct);
                EquipmentList.Add(result);

                if (!result.Corrupted && result.Rarity == EquipmentRarity.Normal)
                {
                    continue;
                }

                bool added = false;

                ConditionResolver resolver = new ConditionResolver();

                foreach (var prototype in _itemPrototypes)
                {
                    if (resolver.IsValid(prototype.Condition, result))
                    {
                        MatchingItems[prototype].Add(result);
                        added = true;
                        BaseItemCount++;
                        break;
                    }
                }

                if (!added)
                {
                    if (!result.Corrupted && result.Rarity != EquipmentRarity.Normal && scourCost < BaseItemCost)
                    {
                        ScourCount++;
                    }
                    else
                    {
                        BaseItemCount++;
                    }
                }

                Progress = progress;
                OnPropertyChanged(nameof(Progress));
            }

            Progress   = 100;
            IsCrafting = false;
            OnPropertyChanged(nameof(Progress));


            App.Current.Dispatcher.Invoke((Action) delegate
            {
                OnCompletion?.Invoke();
            });
        }