Exemple #1
0
        public void Configure(MobAIBase aiBase, StateMachine <string, string> brain, string parentState)
        {
            m_aiBase = aiBase;
            FoundGroundItemTrigger = brain.SetTriggerParameters <ItemDrop>(Trigger.FoundGroundItem);
            m_searchRadius         = aiBase.Awareness * 5;



            brain.Configure(State.Main)
            .InitialTransition(State.SearchItemsOnGround)
            .SubstateOf(parentState)
            .PermitDynamic(Trigger.Timeout, () => FailState)
            .OnEntry(t =>
            {
                //Debug.Log("Entered SearchForItemsBehaviour");
            })
            .OnExit(t =>
            {
                KnownContainers.Peek()?.SetInUse(inUse: false);
            });

            brain.Configure(State.SearchItemsOnGround)
            .SubstateOf(State.Main)
            .Permit(FoundGroundItemTrigger.Trigger, State.MoveToGroundItem)
            .Permit(Trigger.Failed, State.SearchForRandomContainer)
            .OnEntry(t =>
            {
                ItemDrop groundItem = Common.GetNearbyItem(m_aiBase.Instance, Items, m_searchRadius);
                if (groundItem != null)
                {
                    m_aiBase.UpdateAiStatus($"Look, there is a {groundItem.m_itemData.m_shared.m_name} on da grund");
                    brain.Fire(FoundGroundItemTrigger, groundItem);
                    return;
                }
                m_aiBase.UpdateAiStatus($"I seen nottin on da ground.");
                brain.Fire(Trigger.Failed);
            });

            brain.Configure(State.SearchForRandomContainer)
            .SubstateOf(State.Main)
            .Permit(Trigger.ContainerFound, State.MoveToContainer)
            .PermitDynamic(Trigger.ContainerNotFound, () => FailState)
            .PermitDynamic(Trigger.Failed, () => FailState)
            .OnEntry(t =>
            {
                if (KnownContainers.Any())
                {
                    var matchingContainer = KnownContainers.Where(c => c.GetInventory().GetAllItems().Any(i => Items.Any(it => i.m_shared.m_name == it.m_shared.m_name))).RandomOrDefault();
                    if (matchingContainer != null)
                    {
                        KnownContainers.Remove(matchingContainer);
                        KnownContainers.Push(matchingContainer);
                        m_aiBase.UpdateAiStatus($"I seen this in that a bin");
                        brain.Fire(Trigger.ContainerFound);
                        return;
                    }
                }

                Container nearbyChest = Common.FindRandomNearbyContainer(m_aiBase.Instance, KnownContainers, AcceptedContainerNames, m_searchRadius);
                if (nearbyChest != null)
                {
                    KnownContainers.Push(nearbyChest);
                    m_aiBase.UpdateAiStatus($"Look a bin!");
                    m_aiBase.Brain.Fire(Trigger.ContainerFound);
                }
                else
                {
                    m_aiBase.UpdateAiStatus($"Me give up, nottin found!");
                    KnownContainers.Clear();
                    m_aiBase.Brain.Fire(Trigger.ContainerNotFound);
                }
            });

            brain.Configure(State.MoveToGroundItem)
            .SubstateOf(State.Main)
            .Permit(Trigger.GroundItemIsClose, State.PickUpItemFromGround)
            .Permit(Trigger.Failed, State.SearchItemsOnGround)
            .OnEntry(t =>
            {
                m_groundItem = t.Parameters[0] as ItemDrop;
                if (m_groundItem == null || Common.GetNView(m_groundItem)?.IsValid() != true)
                {
                    brain.Fire(Trigger.Failed);
                    return;
                }
                m_aiBase.UpdateAiStatus($"Heading to {m_groundItem.m_itemData.m_shared.m_name}");
            });

            brain.Configure(State.PickUpItemFromGround)
            .SubstateOf(State.Main)
            .PermitDynamic(Trigger.ItemFound, () => SuccessState)
            .Permit(Trigger.Failed, State.SearchItemsOnGround)
            .OnEntry(t =>
            {
                FoundItem = m_groundItem.m_itemData;
                if (m_groundItem == null || Common.GetNView(m_groundItem)?.IsValid() != true)
                {
                    brain.Fire(Trigger.Failed);
                    return;
                }
                m_aiBase.UpdateAiStatus($"Got a {FoundItem.m_shared.m_name} from the ground");
                if (m_groundItem.RemoveOne())
                {
                    brain.Fire(Trigger.ItemFound);
                }
                else
                {
                    brain.Fire(Trigger.Failed);
                }
            });

            brain.Configure(State.MoveToContainer)
            .SubstateOf(State.Main)
            .Permit(Trigger.ContainerIsClose, State.OpenContainer)
            .Permit(Trigger.Failed, State.SearchItemsOnGround)
            .PermitDynamic(Trigger.ContainerNotFound, () => FailState)
            .OnEntry(t =>
            {
                m_aiBase.UpdateAiStatus($"Heading to that a bin");
            });

            brain.Configure(State.OpenContainer)
            .SubstateOf(State.Main)
            .Permit(Trigger.ContainerOpened, State.SearchForItem)
            .Permit(Trigger.Failed, State.SearchItemsOnGround)
            .OnEntry(t =>
            {
                if (KnownContainers.Peek() == null || KnownContainers.Peek().IsInUse())
                {
                    KnownContainers.Pop();
                    brain.Fire(Trigger.Failed);
                }
                else
                {
                    KnownContainers.Peek().SetInUse(inUse: true);
                    m_openChestTimer = 0f;
                }
            });

            brain.Configure(State.SearchForItem)
            .SubstateOf(State.Main)
            .PermitDynamic(Trigger.ItemFound, () => SuccessState)
            .Permit(Trigger.Failed, State.SearchItemsOnGround)
            .OnEntry(t =>
            {
                if (KnownContainers.Peek() == null)
                {
                    brain.Fire(Trigger.Failed);
                    return;
                }
                FoundItem = KnownContainers.Peek().GetInventory().GetAllItems().Where(i => Items.Any(it => i.m_shared.m_name == it.m_shared.m_name)).RandomOrDefault();
                if (FoundItem != null)
                {
                    m_aiBase.UpdateAiStatus($"Found {FoundItem.m_shared.m_name} in this a bin!");
                    KnownContainers.Peek().GetInventory().RemoveItem(FoundItem, 1);
                    Common.Invoke <Container>(KnownContainers.Peek(), "Save");
                    Common.Invoke <Inventory>(KnownContainers.Peek().GetInventory(), "Changed");

                    brain.Fire(Trigger.ItemFound);
                }
                else
                {
                    m_aiBase.UpdateAiStatus($"Nottin in this a bin..");
                    brain.Fire(Trigger.Failed);
                }
            })
            .OnExit(t =>
            {
                KnownContainers.Peek().SetInUse(inUse: false);
            });
        }