public override IActorTask GetTask(IActor actor, ILogicStrategyData strategyData)
        {
            var inventory                  = actor.Person.GetModule <IInventoryModule>();
            var currentInventoryProps      = inventory.CalcActualItems();
            var currentInventoryEquipments = currentInventoryProps.OfType <Equipment>().ToArray();

            var equipmentCarrier = actor.Person.GetModule <IEquipmentModule>();

            for (var slotIndex = 0; slotIndex < equipmentCarrier.Slots.Length; slotIndex++)
            {
                var slot    = actor.Person.GetModule <IEquipmentModule>().Slots[slotIndex];
                var equiped = actor.Person.GetModule <IEquipmentModule>()[slotIndex];
                if (equiped == null)
                {
                    var availableEquipments = currentInventoryEquipments
                                              .Where(equipment => (equipment.Scheme.Equip.SlotTypes[0] & slot.Types) > 0)
                                              .Where(equipment => EquipmentCarrierHelper.CanBeEquiped(equipmentCarrier, slotIndex, equipment))
                                              .ToArray();

                    if (availableEquipments.Any())
                    {
                        var targetEquipmentFromInventory = availableEquipments.First();
                        var targetSlotIndex = slotIndex;

                        return(new EquipTask(actor, targetEquipmentFromInventory, targetSlotIndex));
                    }
                }
            }

            Complete = true;
            return(null);
        }
 private static bool CanBeEquiped(
     IEquipmentModule equipmentModule,
     int slotIndex,
     Equipment equipment)
 {
     return(EquipmentCarrierHelper.CanBeEquiped(equipmentModule, slotIndex, equipment));
 }
        protected override void ValidateSetEquipment(Equipment equipment, int slotIndex)
        {
            var slot = GetSlotForEquipByIndex(equipment, slotIndex);

            if (!EquipmentCarrierHelper.CheckSlotCompability(equipment, slot))
            {
                throw new ArgumentException(
                          $"Для экипировки указан слот {slot}, не подходящий для данного типа предмета {equipment}.");
            }

            if (!EquipmentCarrierHelper.CheckDualCompability(
                    equipmentModule: this,
                    equipment,
                    slotIndex))
            {
                throw new InvalidOperationException(
                          $"Попытка экипировать предмет {equipment}, несовместимый с текущий экипировкой.");
            }

            if (!EquipmentCarrierHelper.CheckShieldCompability(
                    equipmentCarrier: this,
                    equipment,
                    slotIndex))
            {
                throw new InvalidOperationException("Попытка экипировать два щита.");
            }
        }
        public bool Test(IActor actor, ISectorTaskSourceContext context, ILogicState currentState,
                         ILogicStrategyData strategyData)
        {
            if (actor is null)
            {
                throw new ArgumentNullException(nameof(actor));
            }

            if (currentState is null)
            {
                throw new ArgumentNullException(nameof(currentState));
            }

            if (strategyData is null)
            {
                throw new ArgumentNullException(nameof(strategyData));
            }

            if (!actor.Person.HasModule <IInventoryModule>() || !actor.Person.HasModule <IEquipmentModule>())
            {
                return(false);
            }

            var inventoryModule            = actor.Person.GetModule <IInventoryModule>();
            var inventoryProps             = inventoryModule.CalcActualItems();
            var currentInventoryEquipments = inventoryProps.OfType <Equipment>();

            if (!currentInventoryEquipments.Any())
            {
                strategyData.TargetEquipment          = null;
                strategyData.TargetEquipmentSlotIndex = null;
                return(false);
            }

            var equipmentModule = actor.Person.GetModule <IEquipmentModule>();

            for (var slotIndex = 0; slotIndex < equipmentModule.Slots.Length; slotIndex++)
            {
                var slotScheme = equipmentModule.Slots[slotIndex];
                var equiped    = equipmentModule[slotIndex];
                if (equiped is null)
                {
                    var availableEquipments = from equipment in currentInventoryEquipments
                                              where IsApplicableForSlot(equipment, slotScheme.Types)
                                              where EquipmentCarrierHelper.CanBeEquiped(equipmentModule, slotIndex,
                                                                                        equipment)
                                              select equipment;

                    var targetEquipmentFromInventory = availableEquipments.FirstOrDefault();
                    if (targetEquipmentFromInventory != null)
                    {
                        strategyData.TargetEquipment          = targetEquipmentFromInventory;
                        strategyData.TargetEquipmentSlotIndex = slotIndex;
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #5
0
        public override bool CanExecute()
        {
            if (_inventoryState.SelectedProp == null)
            {
                return(true);
            }

            var equipment = GetInventorySelectedEquipment();

            if (equipment == null && _inventoryState.SelectedProp != null)
            {
                return(false);
            }

            // Сломанную экипировку нельзя надевать
            //TODO Тут есть замечание, что equipment не проверяется.
            // Реорганизовать этот код в более понятный.
            if (equipment.Durable.Value <= 0)
            {
                return(false);
            }

            if (SlotIndex == null)
            {
                throw new InvalidOperationException("Для команды не указан слот.");
            }

            var equipmentCarrier = PlayerState.ActiveActor.Actor.Person.EquipmentCarrier;
            var slot             = equipmentCarrier.Slots[SlotIndex.Value];

            var canEquipInSlot = EquipmentCarrierHelper.CheckSlotCompability(equipment, slot);

            if (!canEquipInSlot)
            {
                return(false);
            }

            var canEquipDual = EquipmentCarrierHelper.CheckDualCompability(equipmentCarrier,
                                                                           equipment,
                                                                           slot,
                                                                           SlotIndex.Value);

            if (!canEquipDual)
            {
                return(false);
            }

            var canEquipShield = EquipmentCarrierHelper.CheckShieldCompability(equipmentCarrier,
                                                                               equipment,
                                                                               slot,
                                                                               SlotIndex.Value);

            if (!canEquipShield)
            {
                return(false);
            }

            return(true);
        }
Example #6
0
        public override CanExecuteCheckResult CanExecute()
        {
            if (_inventoryState.SelectedProp is null)
            {
                // Means equipment will be unequiped from the slot (specified in command).
                return(CanExecuteCheckResult.CreateSuccessful());
            }

            if (SlotIndex is null)
            {
                throw new InvalidOperationException("Для команды не указан слот.");
            }

            if (_inventoryState.SelectedProp is null)
            {
                return(new CanExecuteCheckResult
                {
                    IsSuccess = false,
                    FailureReason = "Item to equip is not selected."
                });
            }

            if (_inventoryState.SelectedProp.Prop is not Equipment equipmentFromInventory)
            {
                return(new CanExecuteCheckResult
                {
                    IsSuccess = false,
                    FailureReason = "It is attempt to equip non-equipment."
                });
            }

            // Сломанную экипировку нельзя надевать
            if (equipmentFromInventory.Durable.Value <= 0)
            {
                return(new CanExecuteCheckResult
                {
                    IsSuccess = false,
                    FailureReason = "The selected equipment is broken."
                });
            }

            var equipmentCarrier = PlayerState.ActiveActor !.Actor.Person.GetModule <IEquipmentModule>();
            var slot             = equipmentCarrier.Slots[SlotIndex.Value];

            var canEquipInSlot = EquipmentCarrierHelper.CheckSlotCompability(equipmentFromInventory !, slot);

            if (!canEquipInSlot)
            {
                return(new CanExecuteCheckResult
                {
                    IsSuccess = false,
                    FailureReason = $"Incompatible slot {slot?.Types} to assign equipment."
                });
            }

            var canEquipDual = EquipmentCarrierHelper.CheckDualCompability(equipmentCarrier,
                                                                           equipmentFromInventory,
                                                                           SlotIndex.Value);

            if (!canEquipDual)
            {
                return(new CanExecuteCheckResult
                {
                    IsSuccess = false,
                    FailureReason = "Equipment is not compatible to dual."
                });
            }

            var canEquipShield = EquipmentCarrierHelper.CheckShieldCompability(equipmentCarrier,
                                                                               equipmentFromInventory,
                                                                               SlotIndex.Value);

            if (!canEquipShield)
            {
                return(new CanExecuteCheckResult
                {
                    IsSuccess = false,
                    FailureReason = "It is attempt to equip second shield."
                });
            }

            var is1hTo2hSlot = Check2hOnlyInMainHandSlot(equipmentCarrier,
                                                         equipmentFromInventory,
                                                         SlotIndex.Value);

            if (is1hTo2hSlot != null && is1hTo2hSlot == false)
            {
                return(new CanExecuteCheckResult
                {
                    IsSuccess = false,
                    FailureReason = "It is attempt to equip two-handed in not main hand slot."
                });
            }

            return(new CanExecuteCheckResult {
                IsSuccess = true
            });
        }