Exemplo n.º 1
0
        public AIObjective CreateObjective(Order order, string option, Character orderGiver, bool isAutonomous, float priorityModifier = 1)
        {
            if (order == null || order.Identifier == "dismissed")
            {
                return(null);
            }
            AIObjective newObjective;

            switch (order.Identifier.ToLowerInvariant())
            {
            case "follow":
                if (orderGiver == null)
                {
                    return(null);
                }
                newObjective = new AIObjectiveGoTo(orderGiver, character, this, repeat: true, priorityModifier: priorityModifier)
                {
                    CloseEnough                = Rand.Range(90, 100) + Rand.Range(50, 70) * Math.Min(HumanAIController.CountCrew(c => c.ObjectiveManager.CurrentOrder is AIObjectiveGoTo gotoOrder && gotoOrder.Target == orderGiver, onlyBots: true), 4),
                    extraDistanceOutsideSub    = 100,
                    extraDistanceWhileSwimming = 100,
                    AllowGoingOutside          = true,
                    IgnoreIfTargetDead         = true,
                    followControlledCharacter  = true,
                    mimic = true,
                    DialogueIdentifier = "dialogcannotreachplace"
                };
                break;

            case "wait":
                newObjective = new AIObjectiveGoTo(order.TargetSpatialEntity ?? character, character, this, repeat: true, priorityModifier: priorityModifier)
                {
                    AllowGoingOutside = character.Submarine == null || (order.TargetSpatialEntity != null && character.Submarine != order.TargetSpatialEntity.Submarine)
                };
                break;

            case "fixleaks":
                newObjective = new AIObjectiveFixLeaks(character, this, priorityModifier: priorityModifier, prioritizedHull: order.TargetEntity as Hull);
                break;

            case "chargebatteries":
                newObjective = new AIObjectiveChargeBatteries(character, this, option, priorityModifier);
                break;

            case "rescue":
                newObjective = new AIObjectiveRescueAll(character, this, priorityModifier);
                break;

            case "repairsystems":
            case "repairmechanical":
            case "repairelectrical":
                newObjective = new AIObjectiveRepairItems(character, this, priorityModifier: priorityModifier, prioritizedItem: order.TargetEntity as Item)
                {
                    RelevantSkill         = order.AppropriateSkill,
                    RequireAdequateSkills = isAutonomous
                };
                break;

            case "pumpwater":
                if (order.TargetItemComponent is Pump targetPump)
                {
                    if (!order.TargetItemComponent.Item.IsInteractable(character))
                    {
                        return(null);
                    }
                    newObjective = new AIObjectiveOperateItem(targetPump, character, this, option, false, priorityModifier: priorityModifier)
                    {
                        IsLoop   = true,
                        Override = orderGiver != null && orderGiver.IsPlayer
                    };
                    // ItemComponent.AIOperate() returns false by default -> We'd have to set IsLoop = false and implement a custom override of AIOperate for the Pump.cs,
                    // if we want that the bot just switches the pump on/off and continues doing something else.
                    // If we want that the bot does the objective and then forgets about it, I think we could do the same plus dismiss when the bot is done.
                }
                else
                {
                    newObjective = new AIObjectivePumpWater(character, this, option, priorityModifier: priorityModifier);
                }
                break;

            case "extinguishfires":
                newObjective = new AIObjectiveExtinguishFires(character, this, priorityModifier);
                break;

            case "fightintruders":
                newObjective = new AIObjectiveFightIntruders(character, this, priorityModifier);
                break;

            case "steer":
                var steering = (order?.TargetEntity as Item)?.GetComponent <Steering>();
                if (steering != null)
                {
                    steering.PosToMaintain = steering.Item.Submarine?.WorldPosition;
                }
                if (order.TargetItemComponent == null)
                {
                    return(null);
                }
                if (!order.TargetItemComponent.Item.IsInteractable(character))
                {
                    return(null);
                }
                newObjective = new AIObjectiveOperateItem(order.TargetItemComponent, character, this, option,
                                                          requireEquip: false, useController: order.UseController, controller: order.ConnectedController, priorityModifier: priorityModifier)
                {
                    IsLoop = true,
                    // Don't override unless it's an order by a player
                    Override = orderGiver != null && orderGiver.IsPlayer
                };
                break;

            case "setchargepct":
                newObjective = new AIObjectiveOperateItem(order.TargetItemComponent, character, this, option, false, priorityModifier: priorityModifier)
                {
                    IsLoop              = false,
                    Override            = !character.IsDismissed,
                    completionCondition = () =>
                    {
                        if (float.TryParse(option, out float pct))
                        {
                            var targetRatio  = Math.Clamp(pct, 0f, 1f);
                            var currentRatio = (order.TargetItemComponent as PowerContainer).RechargeRatio;
                            return(Math.Abs(targetRatio - currentRatio) < 0.05f);
                        }
                        return(true);
                    }
                };
                break;

            case "getitem":
                newObjective = new AIObjectiveGetItem(character, order.TargetEntity as Item ?? order.TargetItemComponent?.Item, this, false, priorityModifier: priorityModifier)
                {
                    MustBeSpecificItem = true
                };
                break;

            case "cleanupitems":
                if (order.TargetEntity is Item targetItem)
                {
                    if (targetItem.HasTag("allowcleanup") && targetItem.ParentInventory == null && targetItem.OwnInventory != null)
                    {
                        // Target all items inside the container
                        newObjective = new AIObjectiveCleanupItems(character, this, targetItem.OwnInventory.AllItems, priorityModifier);
                    }
                    else
                    {
                        newObjective = new AIObjectiveCleanupItems(character, this, targetItem, priorityModifier);
                    }
                }
                else
                {
                    newObjective = new AIObjectiveCleanupItems(character, this, priorityModifier: priorityModifier);
                }
                break;

            default:
                if (order.TargetItemComponent == null)
                {
                    return(null);
                }
                if (!order.TargetItemComponent.Item.IsInteractable(character))
                {
                    return(null);
                }
                newObjective = new AIObjectiveOperateItem(order.TargetItemComponent, character, this, option,
                                                          requireEquip: false, useController: order.UseController, controller: order.ConnectedController, priorityModifier: priorityModifier)
                {
                    IsLoop = true,
                    // Don't override unless it's an order by a player
                    Override = orderGiver != null && orderGiver.IsPlayer
                };
                if (newObjective.Abandon)
                {
                    return(null);
                }
                break;
            }
            return(newObjective);
        }
        protected override void Act(float deltaTime)
        {
            if (item.IgnoreByAI(character))
            {
                Abandon = true;
                return;
            }
            if (item.ParentInventory != null)
            {
                if (item.Container != null && !AIObjectiveCleanupItems.IsValidContainer(item.Container, character, allowUnloading: objectiveManager.HasOrder <AIObjectiveCleanupItems>()))
                {
                    // Target was picked up or moved by someone.
                    Abandon = true;
                    return;
                }
            }
            // Only continue when the get item sub objectives have been completed.
            if (subObjectives.Any())
            {
                return;
            }
            if (HumanAIController.FindSuitableContainer(character, item, ignoredContainers, ref itemIndex, out Item suitableContainer))
            {
                itemIndex = 0;
                if (suitableContainer != null)
                {
                    bool equip = item.GetComponent <Holdable>() != null ||
                                 item.AllowedSlots.None(s =>
                                                        s == InvSlotType.Card ||
                                                        s == InvSlotType.Head ||
                                                        s == InvSlotType.Headset ||
                                                        s == InvSlotType.InnerClothes ||
                                                        s == InvSlotType.OuterClothes);

                    TryAddSubObjective(ref decontainObjective, () => new AIObjectiveDecontainItem(character, item, objectiveManager, targetContainer: suitableContainer.GetComponent <ItemContainer>())
                    {
                        Equip          = equip,
                        TakeWholeStack = true,
                        DropIfFails    = true
                    },
                                       onCompleted: () =>
                    {
                        if (equip)
                        {
                            HumanAIController.ReequipUnequipped();
                        }
                        IsCompleted = true;
                    },
                                       onAbandon: () =>
                    {
                        if (equip)
                        {
                            HumanAIController.ReequipUnequipped();
                        }
                        if (decontainObjective != null && decontainObjective.ContainObjective != null && decontainObjective.ContainObjective.CanBeCompleted)
                        {
                            ignoredContainers.Add(suitableContainer);
                        }
                        else
                        {
                            Abandon = true;
                        }
                    });
                }
                else
                {
                    Abandon = true;
                }
            }
            else
            {
                objectiveManager.GetObjective <AIObjectiveIdle>().Wander(deltaTime);
            }
        }