Esempio n. 1
0
        public bool DuplicateControl(WorldDynamic currentState,
                                     PlanAction action,
                                     StoryGraph currentGraph,
                                     KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                     StoryNode currentNode,
                                     int globalNodeNumber,
                                     bool succsessControl)
        {
            StoryNode testNode = currentGraph.CreateTestNode(currentState, action, agent, currentNode, false, globalNodeNumber, succsessControl);

            testNode.UpdateHashCode();

            foreach (var checkedNode in currentGraph.GetNodes())
            {
                checkedNode.UpdateHashCode();
                if (currentGraph.TwoNodesComparison(testNode, checkedNode))
                {
                    currentGraph.DeleteTestNode(ref testNode);
                    return(false);
                }
            }

            currentGraph.DeleteTestNode(ref testNode);
            return(true);
        }
Esempio n. 2
0
        public void MultiAVandAC(ref PlanAction receivedAction,
                                 WorldDynamic currentState,
                                 KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                 CSP_Module cspModule,
                                 StoryGraph currentGraph,
                                 StoryNode currentNode,
                                 bool root,
                                 ref int globalNodeNumber,
                                 ref Queue <StoryNode> queue)
        {
            List <PlanAction> actionsList = cspModule.MassiveAssignVariables(ref receivedAction, currentState, agent);

            AgentStateStatic  sCurrentAgent = (AgentStateStatic)agent.Key.Clone();
            AgentStateDynamic dCurrentAgent = (AgentStateDynamic)agent.Value.Clone();
            KeyValuePair <AgentStateStatic, AgentStateDynamic> currentAgent =
                new KeyValuePair <AgentStateStatic, AgentStateDynamic>(sCurrentAgent, dCurrentAgent);

            WorldDynamic statePrefab = (WorldDynamic)currentState.Clone();

            foreach (var a in actionsList)
            {
                ActionControl(a, currentGraph, currentAgent, statePrefab, currentNode, root, ref globalNodeNumber, ref queue);
            }

            // Cleaning
            actionsList = null;
            currentNode = null;
            statePrefab = null;
            GC.Collect();
        }
Esempio n. 3
0
        public void DuplicateNodeConnecting(WorldDynamic currentState,
                                            PlanAction action,
                                            KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                            StoryNode currentNode,
                                            int globalNodeNumber,
                                            ref Queue <StoryNode> queue,
                                            bool succsessControl,
                                            ref bool skip)
        {
            StoryNode testNode = CreateTestNode(currentState, action, agent, currentNode, false, globalNodeNumber, succsessControl);

            testNode.UpdateHashCode();

            if (!testNode.Equals(currentNode))
            {
                foreach (var checkedNode in nodes)
                {
                    if (TwoNodesComparison(testNode, checkedNode) && !currentNode.ConnectedWith(checkedNode))
                    {
                        DeleteTestNode(ref testNode);
                        ConnectionTwoNodes(action, currentNode, checkedNode, true);
                        break;
                    }
                }
            }
            else
            {
                DeleteTestNode(ref testNode);
                skip = true;
            }
        }
Esempio n. 4
0
        public void CounterreactionControl(PlanAction action,
                                           StoryGraph currentGraph,
                                           KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                           WorldDynamic currentState,
                                           StoryNode currentNode,
                                           bool root,
                                           ref int globalNodeNumber,
                                           ref Queue <StoryNode> queue,
                                           ref bool controlOne,
                                           ref bool controlTwo)
        {
            bool succsessControl = ProbabilityCalculating(action);

            action.success = succsessControl;
            action.fail    = !succsessControl;

            bool constraintsControl = ConstraintsControl(currentState, action, succsessControl);
            bool deadEndsControl    = DeadEndsControl(action, currentState, agent, succsessControl);
            bool duplicateControl   = DuplicateControl(currentState, action, currentGraph, agent, currentNode, globalNodeNumber, succsessControl);
            //bool cyclesControl = CyclesControl(currentState, action, currentGraph, agent, currentNode, duplicateControl, globalNodeNumber, succsessControl);
            bool cyclesControl = true;

            controlOne = constraintsControl & deadEndsControl & cyclesControl;
            controlTwo = duplicateControl;
        }
Esempio n. 5
0
        /// <summary>
        /// Checking whether the application of an action would violate the established constraints.
        /// </summary>
        public bool ConstraintsControl(WorldDynamic currentState, PlanAction action, bool succsessControl)
        {
            WorldDynamic worldForTest = (WorldDynamic)currentState.Clone();

            if (!succsessControl)
            {
                action.Fail(ref worldForTest);
            }
            else
            {
                action.ApplyEffects(ref worldForTest);
            }

            foreach (var constraint in constraints)
            {
                if (!constraint.IsSatisfied(worldForTest))
                {
                    // Cleaning
                    worldForTest = null;
                    GC.Collect();

                    // Return result
                    return(false);
                }
            }

            // Cleaning
            worldForTest = null;
            GC.Collect();

            // Return result
            return(true);
        }
Esempio n. 6
0
        public bool CyclesControl(WorldDynamic currentState,
                                  PlanAction action,
                                  StoryGraph currentGraph,
                                  KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                  StoryNode currentNode,
                                  bool duplicated,
                                  int globalNodeNumber,
                                  bool succsessControl)
        {
            bool result = false;

            // We create a test node similar to the one we are going to add to the graph as a result of the current action.
            StoryNode testNode = currentGraph.CreateTestNode(currentState, action, agent, currentNode, !duplicated, globalNodeNumber, succsessControl);

            StoryNode duplicatedNode = null;
            Edge      testEdge       = new Edge();

            if (!duplicated)
            {
                duplicatedNode = currentGraph.GetNode(testNode);

                if (currentNode.Equals(duplicatedNode))
                {
                    return(false);
                }

                testEdge.SetUpperNode(ref currentNode);
                testEdge.SetLowerNode(ref duplicatedNode);

                currentNode.AddEdge(testEdge);
                duplicatedNode.AddEdge(testEdge);

                currentNode.AddLinkToNode(ref duplicatedNode);
                duplicatedNode.AddLinkToNode(ref currentNode);
            }

            string[] colors = new string[currentGraph.GetNodes().Count + 2];
            for (int i = 0; i < currentGraph.GetNodes().Count + 2; i++)
            {
                colors[i] = "white";
            }

            result = TarjanAlgStep(currentGraph.GetRoot(), ref colors, !duplicated, duplicatedNode);

            if (!duplicated)
            {
                currentNode.RemoveEdge(testEdge);
                currentNode.DeleteLink(duplicatedNode);
                duplicatedNode.RemoveEdge(testEdge);
                duplicatedNode.DeleteLink(currentNode);
                testEdge.ClearUpperNode();
                testEdge.ClearLowerNode();
                testEdge = null;
            }

            // We delete the test node and mark the loop test as passed.
            currentGraph.DeleteTestNode(ref testNode);

            return(result);
        }
Esempio n. 7
0
        /// <summary>
        /// Create a new node for the story graph and inserts it.
        /// </summary>
        public void CreateNewNode(PlanAction action,
                                  KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                  WorldDynamic currentState,
                                  StoryNode currentNode,
                                  ref int globalNodeNumber,
                                  bool succsessControl,
                                  bool counteract)
        {
            WorldDynamic newState = (WorldDynamic)currentState.Clone();

            if (!succsessControl)
            {
                action.Fail(ref newState);
            }
            else
            {
                action.ApplyEffects(ref newState);
            }
            newState.UpdateHashCode();

            // Create an empty new node.
            StoryNode newNode = new StoryNode();

            if (counteract)
            {
                newNode.counteract = true;
            }

            // We assign the state of the world (transferred) to the new node.
            newNode.SetWorldState((WorldDynamic)newState.Clone());

            newNode.SetActiveAgent(newNode.GetWorldState().GetAgentByName(agent.Key.GetName()));
            if (agent.Key.GetRole() == AgentRole.PLAYER)
            {
                newNode.SetActivePlayer(true);
            }
            else
            {
                newNode.SetActivePlayer(false);
            }

            ConnectionTwoNodes(action, currentNode, newNode, false);

            globalNodeNumber++;
            newNode.SetNumberInSequence(globalNodeNumber);

            if (nodes.Contains(newNode))
            {
                bool test = true;
            }

            // Add a new node to the graph.
            AddNode(newNode);
        }
Esempio n. 8
0
        public StoryNode CreateTestNode(WorldDynamic currentState,
                                        PlanAction action,
                                        KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                        StoryNode currentNode,
                                        bool connection,
                                        int globalNodeNumber,
                                        bool succsessControl)
        {
            WorldDynamic worldForTest = (WorldDynamic)currentState.Clone();

            if (!succsessControl)
            {
                action.Fail(ref worldForTest);
            }
            else
            {
                action.ApplyEffects(ref worldForTest);
            }
            worldForTest.UpdateHashCode();

            StoryNode testNode = new StoryNode();

            //if (counteract) { testNode.counteract = true; }
            //testNode.SetWorldState(worldForTest);
            testNode.SetWorldState((WorldDynamic)worldForTest.Clone());

            // Create a clone of the agent.
            //KeyValuePair<AgentStateStatic, AgentStateDynamic> newAgent =
            //    new KeyValuePair<AgentStateStatic, AgentStateDynamic>((AgentStateStatic)agent.Key.Clone(), (AgentStateDynamic)agent.Value.Clone());
            testNode.SetActiveAgent(testNode.GetWorldState().GetAgentByName(agent.Key.GetName()));

            // We take the last node from the list of all nodes and assign whether the player is active and which of the agents was active on this turn.
            if (agent.Key.GetRole() == AgentRole.PLAYER)
            {
                testNode.SetActivePlayer(true);
            }
            else
            {
                testNode.SetActivePlayer(false);
            }

            //testNode.SetActiveAgent(newAgent);

            /*if (connection)
             * {
             *  ConnectionTwoNodes(action, currentNode, testNode, false);
             * }*/

            testNode.SetNumberInSequence(globalNodeNumber + 1);

            return(testNode);
        }
Esempio n. 9
0
        /// <summary>
        /// Checking the action for violation of the established constraints and the reachability of the goal state (control of cycles and deadends).
        /// </summary>
        public void ActionControl(PlanAction action,
                                  StoryGraph currentGraph,
                                  KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                  WorldDynamic currentState,
                                  StoryNode currentNode,
                                  bool root,
                                  ref int globalNodeNumber,
                                  ref Queue <StoryNode> queue)
        {
            bool succsessControl = ProbabilityCalculating(action);

            action.success = succsessControl;
            action.fail    = !succsessControl;

            bool constraintsControl = ConstraintsControl(currentState, action, succsessControl);
            bool deadEndsControl    = DeadEndsControl(action, currentState, agent, succsessControl);
            bool duplicateControl   = DuplicateControl(currentState, action, currentGraph, agent, currentNode, globalNodeNumber, succsessControl);
            //bool cyclesControl = CyclesControl(currentState, action, currentGraph, agent, currentNode, duplicateControl, globalNodeNumber, succsessControl);
            bool cyclesControl = true;

            if (constraintsControl && deadEndsControl && cyclesControl && duplicateControl)
            {
                // If all checks are passed, then we apply the action.
                ApplyAction(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, succsessControl, false);
            }
            else if (!constraintsControl && deadEndsControl && cyclesControl && duplicateControl)
            {
                // If the action violates the constraints, then convergence will not apply it, but will apply its counter-reaction.
                ActionCounteract(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue);
            }
            else if (!duplicateControl && cyclesControl)
            {
                bool skip = false;

                // connection current node --> finded node
                currentGraph.DuplicateNodeConnecting(currentState, action, agent, currentNode, globalNodeNumber, ref queue, succsessControl, ref skip);

                if (skip)
                {
                    //NothingToDo newAction = new NothingToDo();
                    //newAction.Arguments.Add(agent);
                    //ApplyAction(newAction, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, succsessControl, true);

                    ActionCounteract(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue);
                }
            }
            else
            {
                ActionCounteract(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue);
            }
        }
Esempio n. 10
0
        public void SingleAVandAC(ref PlanAction receivedAction,
                                  WorldDynamic currentState,
                                  KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                  CSP_Module cspModule,
                                  StoryGraph currentGraph,
                                  StoryNode currentNode,
                                  bool root,
                                  ref int globalNodeNumber,
                                  ref Queue <StoryNode> queue)
        {
            cspModule.AssignVariables(ref receivedAction, currentState, agent);

            ActionControl(receivedAction, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue);

            // Cleaning
            currentNode = null;
            GC.Collect();
        }
Esempio n. 11
0
        public bool DeadEndsControl(PlanAction action,
                                    WorldDynamic currentState,
                                    KeyValuePair <AgentStateStatic, AgentStateDynamic> player,
                                    bool succsessControl)
        {
            if (player.Key.GetRole() == AgentRole.PLAYER)
            {
                WorldDynamic worldForTest = (WorldDynamic)currentState.Clone();
                if (!succsessControl)
                {
                    action.Fail(ref worldForTest);
                }
                else
                {
                    action.ApplyEffects(ref worldForTest);
                }

                worldForTest.GetAgentByRole(AgentRole.PLAYER).Value.CalculatePlan(worldForTest.GetAgentByRole(AgentRole.PLAYER), worldForTest);

                if (worldForTest.GetAgentByRole(AgentRole.PLAYER).Value.GetPlanStatus())
                {
                    // Cleaning
                    worldForTest = null;
                    GC.Collect();

                    // Return result
                    return(true);
                }
                else
                {
                    // Cleaning
                    worldForTest = null;
                    GC.Collect();

                    // Return result
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
Esempio n. 12
0
 /// <summary>
 /// The probability of success of the action is calculated, and if successful, it is applied.
 /// </summary>
 public void ApplyAction(PlanAction action,
                         StoryGraph currentGraph,
                         KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                         WorldDynamic currentState,
                         StoryNode currentNode,
                         bool root,
                         ref int globalNodeNumber,
                         bool succsessControl,
                         bool counteract)
 {
     // We apply a successful/unsuccessful option to perform an action.
     if (root)
     {
         currentGraph.CreateRootNode(action, agent, currentState, currentNode, ref globalNodeNumber, succsessControl);
     }
     else
     {
         currentGraph.CreateNewNode(action, agent, currentState, currentNode, ref globalNodeNumber, succsessControl, counteract);
     }
 }
Esempio n. 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="firstNode">Current node</param>
        /// <param name="secondNode">New node</param>
        public void ConnectionTwoNodes(PlanAction action, StoryNode firstNode, StoryNode secondNode, bool duplicate)
        {
            Edge newEdge = new Edge();

            newEdge.SetAction(action);

            newEdge.SetUpperNode(ref firstNode);
            newEdge.SetLowerNode(ref secondNode);

            firstNode.AddEdge(newEdge);
            secondNode.AddEdge(newEdge);

            firstNode.AddLinkToNode(ref secondNode);
            secondNode.AddLinkToNode(ref firstNode);

            if (firstNode.GetEdges().Count != firstNode.GetLinks().Count)
            {
                bool test = true;
            }
        }
Esempio n. 14
0
        public void CreateRootNode(PlanAction action,
                                   KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                   WorldDynamic currentState,
                                   StoryNode currentNode,
                                   ref int globalNodeNumber,
                                   bool succsessControl)
        {
            WorldDynamic newState = (WorldDynamic)currentState.Clone();

            if (!succsessControl)
            {
                action.Fail(ref newState);
            }
            else
            {
                action.ApplyEffects(ref newState);
            }

            if (agent.Key.GetRole() == AgentRole.PLAYER)
            {
                currentNode.SetActivePlayer(true);
            }
            else
            {
                currentNode.SetActivePlayer(false);
            }

            // We assign the state of the world (transferred) to the new node.
            currentNode.SetWorldState((WorldDynamic)newState.Clone());

            //Edge newEdge = new Edge();

            // We adjust the edge - assign its action and indicate the nodes that it connects.
            //newEdge.SetAction(action);
            //newEdge.SetUpperNode(ref currentNode);

            //currentNode.AddEdge(newEdge);

            globalNodeNumber++;
            currentNode.SetNumberInSequence(globalNodeNumber);
        }
Esempio n. 15
0
        public List<PlanAction> MassiveAssignVariables(ref PlanAction action, 
                                           WorldDynamic currentState, 
                                           KeyValuePair<AgentStateStatic, AgentStateDynamic> initiator)
        {
            List<PlanAction> actions = new List<PlanAction>();

            if (action is Move)
            {
                int locationsCount = 
                    currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()).Key.GetConnectedLocations().Count;

                List<Move> moveArr = new List<Move>();

                for (int i = 0; i < locationsCount; i++)
                {
                    Move move = new Move();
                    moveArr.Add(move);
                }

                for (int i = 0; i < locationsCount; i++)
                {
                    moveArr[i].Arguments.Add(initiator);
                    moveArr[i].Arguments.Add(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()));
                    moveArr[i].Arguments.Add(currentState.GetLocationByName
                        (currentState.GetLocationByName(
                            currentState.SearchAgentAmongLocations(initiator.Key).GetName()).Key.GetConnectedLocationsFromIndex(i).GetName()));
                }

                foreach (var a in moveArr)
                {
                    actions.Add(a);
                }
            }

            return actions;
        }
Esempio n. 16
0
        public bool AssignVariables(ref PlanAction action, WorldDynamic currentState, KeyValuePair<AgentStateStatic, AgentStateDynamic> initiator)
        {
            if (action is Entrap || action is CounterEntrap)
            {
                foreach (var agent in currentState.GetAgents())
                {
                    if ((agent.Key.GetRole() == AgentRole.USUAL || agent.Key.GetRole() == AgentRole.PLAYER) && agent.Value.GetStatus() 
                        && !currentState.GetLocation(currentState.SearchAgentAmongLocations(agent.Key)).Value.SearchAgent(initiator.Key) 
                        && !agent.Equals(initiator))
                    {
                        action.Arguments.Add(agent);
                        action.Arguments.Add(initiator); 
                        action.Arguments.Add(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()));
                        return true;
                    }
                }
                return false;
            }
            else if (action is Fight || action is CounterFight)
            {
                foreach (var agent in currentState.GetAgents())
                {
                    if ((agent.Key.GetRole() == AgentRole.USUAL || agent.Key.GetRole() == AgentRole.PLAYER) && agent.Value.GetStatus() 
                        && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key))
                    {
                        action.Arguments.Add(initiator);
                        action.Arguments.Add(agent);
                        action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)));
                        return true;
                    }
                }
                return false;
            }
            else if (action is InvestigateRoom || action is CounterInvestigateRoom)
            {
                foreach (var killer in currentState.GetAgents())
                {
                    if (killer.Key.GetRole() == AgentRole.KILLER && killer.Value.GetStatus())
                    {
                        action.Arguments.Add(initiator);
                        action.Arguments.Add(killer);
                        action.Arguments.Add(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()));
                        return true;
                    }
                }
                return false;
            }
            else if (action is Kill || action is CounterKill)
            {
                foreach (var agent in currentState.GetAgents())
                {
                    if ((agent.Key.GetRole() == AgentRole.USUAL || agent.Key.GetRole() == AgentRole.PLAYER) && agent.Value.GetStatus() 
                        && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key))
                    {
                        action.Arguments.Add(agent);
                        action.Arguments.Add(initiator);
                        action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)));
                        return true;
                    }
                }
                return false;
            }
            else if (action is Move || action is CounterMove)
            {
                if (action.Arguments.Count() != 0)
                {
                    List<string> arguments = new List<string>();

                    foreach (var argument in action.Arguments)
                    {
                        arguments.Add((string)argument);
                    }

                    action.Arguments.Clear();

                    action.Arguments.Add(initiator);
                    action.Arguments.Add(currentState.GetLocationByName(arguments[1]));
                    action.Arguments.Add(currentState.GetLocationByName(arguments[2]));
                    return true;
                }
                else
                {
                    action.Arguments.Add(initiator);
                    action.Arguments.Add(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()));

                    if (initiator.Value.GetTargetLocation() != null && initiator.Key.GetRole() != AgentRole.PLAYER &&
                        currentState.SearchAgentAmongLocations(initiator.Key).ConnectionChecking(initiator.Value.GetTargetLocation()))
                    {
                        action.Arguments.Add(currentState.GetLocationByName(initiator.Value.GetTargetLocation().GetName()));
                        return true;
                    }
                    else
                    {
                        KeyValuePair<LocationStatic, LocationDynamic> randLoc = currentState.
                          GetRandomConnectedLocation(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()));

                        if (currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()).
                            Key.ConnectionChecking(randLoc.Key))
                        {
                            action.Arguments.Add(randLoc);
                            return true;
                        }
                    }
                }
                return false;
            }
            else if (action is NeutralizeKiller || action is CounterNeutralizeKiller)
            {
                foreach (var killer in currentState.GetAgents())
                {
                    if (killer.Key.GetRole() == AgentRole.KILLER && killer.Value.GetStatus())
                    {
                        action.Arguments.Add(initiator);
                        action.Arguments.Add(killer);
                        action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)));
                        return true;
                    }
                }
                return false;
            }
            else if (action is Reassure || action is CounterReassure)
            {
                foreach (var agent in currentState.GetAgents())
                {
                    if (agent.Key.GetRole() == AgentRole.USUAL && agent.Value.GetStatus()
                        && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key)
                        && !agent.Key.Equals(initiator.Key) && agent.Value.AngryCheck())
                    {
                        action.Arguments.Add(agent);
                        action.Arguments.Add(initiator);
                        action.Arguments.Add(currentState.GetAgentByName(agent.Value.GetObjectOfAngry().GetObjectOfAngry().GetName()));
                        break;
                    }
                }

                foreach (var killer in currentState.GetAgents())
                {
                    if (killer.Key.GetRole() == AgentRole.KILLER && killer.Value.GetStatus())
                    {
                        action.Arguments.Add(killer);
                        break;
                    }
                }

                action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)));
                return true;
            }
            else if (action is Run || action is CounterRun)
            {
                action.Arguments.Add(initiator);
                action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)));
                action.Arguments.Add(currentState.
                        GetRandomLocationWithout(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName())));
                return true;
            }
            else if (action is NothingToDo)
            {
                action.Arguments.Add(initiator);
            }
            else if (action is TellAboutASuspicious || action is CounterTellAboutASuspicious)
            {
                foreach (var agent in currentState.GetAgents())
                {
                    if (agent.Key.GetRole() == AgentRole.USUAL && agent.Value.GetStatus() 
                        && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key))
                    {
                        action.Arguments.Add(agent);
                        break;
                    }
                }

                action.Arguments.Add(initiator);
                action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)));
                action.Arguments.Add(currentState.GetRandomLocationWithout
                    (currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName())));
                return true;
            }
            else if (action is Talk || action is CounterTalk)
            {
                action.Arguments.Add(initiator);

                foreach (var agent in currentState.GetAgents())
                {
                    if (agent.Value.GetStatus()
                        && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key)
                        && agent.Key.GetName() != initiator.Key.GetName())
                    {
                        action.Arguments.Add(agent);
                        return true;
                    }
                }

                return false;
            }
            return false;
        }
Esempio n. 17
0
        /// <summary>
        /// The agent updates his beliefs, calculates a plan, chooses an action, assigns variables to it, and sends it for further control.
        /// </summary>
        public void ActionRequest(KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                  ref StoryGraph currentGraph,
                                  ref WorldDynamic currentState,
                                  StoryNode currentNode,
                                  bool root,
                                  ref int globalNodeNumber,
                                  ref Queue <StoryNode> queue)
        {
            CSP_Module cspModule = new CSP_Module();

            if (agent.Key.GetRole().Equals(AgentRole.PLAYER))
            {
                agent.Value.RefreshBeliefsAboutTheWorld(currentState, agent);
                agent.Value.GenerateNewPDDLProblem(agent, currentState);
                agent.Value.ReceiveAvailableActions(agent);

                List <PlanAction> receivedActions = agent.Value.GetAvailableActions();

                for (int i = 0; i < receivedActions.Count; i++)
                {
                    PlanAction receivedAction = receivedActions[i];

                    if (receivedAction != null)
                    {
                        switch (receivedAction.GetType().ToString().Remove(0, 20))
                        {
                        case "Move":
                            MultiAVandAC(ref receivedAction, currentState, agent, cspModule, currentGraph, currentNode, root,
                                         ref globalNodeNumber, ref queue);
                            break;

                        case "Fight":     // Not relevant yet
                            break;

                        case "InvestigateRoom":
                            SingleAVandAC(ref receivedAction, currentState, agent, cspModule, currentGraph, currentNode, root,
                                          ref globalNodeNumber, ref queue);
                            break;

                        case "NeutralizeKiller":     // Not relevant yet
                            break;

                        case "NothingToDo": SkipTurn(currentState);
                            break;

                        case "Reassure":     // Not relevant yet
                            break;

                        case "Run":     // Not relevant yet
                            break;

                        case "Talk":     // Not relevant yet
                            break;
                        }

                        // Cleaning
                        receivedAction = null;
                        GC.Collect();
                    }
                }

                // Cleaning
                receivedActions = null;
                GC.Collect();
            }
            else
            {
                agent.Value.RefreshBeliefsAboutTheWorld(currentState, agent);
                agent.Value.GenerateNewPDDLProblem(agent, currentState);
                agent.Value.CalculatePlan(agent, currentState);
                agent.Value.ReceiveAvailableActions(agent);

                PlanAction receivedAction = agent.Value.ChooseAction();

                if (receivedAction != null)
                {
                    cspModule.AssignVariables(ref receivedAction, currentState, agent);

                    ActionControl(receivedAction, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue);

                    // Cleaning
                    receivedAction = null;
                    currentNode    = null;
                    GC.Collect();
                }
                else
                {
                    SkipTurn(currentState);
                }
            }
        }
Esempio n. 18
0
        public void ActionCounteract(PlanAction action,
                                     StoryGraph currentGraph,
                                     KeyValuePair <AgentStateStatic, AgentStateDynamic> agent,
                                     WorldDynamic currentState,
                                     StoryNode currentNode,
                                     bool root,
                                     ref int globalNodeNumber,
                                     ref Queue <StoryNode> queue)
        {
            CSP_Module cspModule = new CSP_Module();

            bool stageOne_NewNode       = true;
            bool stageTwo_ConnectedNode = false;
            bool counterreactionFound   = false;

            bool skip = false;

            string currentAction = action.GetType().ToString().Remove(0, 20);

            while (!counterreactionFound)
            {
                PlanAction counterreactionTalk = new CounterTalk();
                bool       assignVariables     = cspModule.AssignVariables(ref counterreactionTalk, currentState, agent);
                counterreactionTalk.Arguments.Add(currentAction);

                if (assignVariables && counterreactionTalk.CheckPreconditions(currentState))
                {
                    bool constractionAndDeadEndAndCicle = false;
                    bool duplicate = false;

                    CounterreactionControl(counterreactionTalk, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                           ref constractionAndDeadEndAndCicle, ref duplicate);

                    if (stageOne_NewNode)
                    {
                        if (constractionAndDeadEndAndCicle && duplicate)
                        {
                            ApplyAction(counterreactionTalk, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                            counterreactionFound = true;
                        }
                    }
                    else if (stageTwo_ConnectedNode)
                    {
                        if (constractionAndDeadEndAndCicle && !duplicate)
                        {
                            currentGraph.DuplicateNodeConnecting(currentState, counterreactionTalk, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                            counterreactionFound = true;
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionEntrap = new CounterEntrap();
                    assignVariables = cspModule.AssignVariables(ref counterreactionEntrap, currentState, agent);
                    counterreactionEntrap.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionEntrap.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionEntrap, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionEntrap, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionEntrap, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionKill = new CounterKill();
                    assignVariables = cspModule.AssignVariables(ref counterreactionKill, currentState, agent);
                    counterreactionKill.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionKill.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionKill, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionKill, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionKill, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionIR = new InvestigateRoom();
                    assignVariables = cspModule.AssignVariables(ref counterreactionIR, currentState, agent);
                    counterreactionIR.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionIR.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionIR, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionIR, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionIR, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionNK = new CounterNeutralizeKiller();
                    assignVariables = cspModule.AssignVariables(ref counterreactionNK, currentState, agent);
                    counterreactionNK.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionNK.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionNK, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionNK, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionNK, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionTalkAboutSuspicious = new CounterTellAboutASuspicious();
                    assignVariables = cspModule.AssignVariables(ref counterreactionTalkAboutSuspicious, currentState, agent);
                    counterreactionTalkAboutSuspicious.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionTalkAboutSuspicious.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionTalkAboutSuspicious, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionTalkAboutSuspicious, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionTalkAboutSuspicious, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionFight = new CounterFight();
                    assignVariables = cspModule.AssignVariables(ref counterreactionFight, currentState, agent);
                    counterreactionFight.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionFight.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionFight, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionFight, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionFight, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionReassure = new CounterReassure();
                    assignVariables = cspModule.AssignVariables(ref counterreactionReassure, currentState, agent);
                    counterreactionReassure.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionReassure.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionReassure, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionReassure, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionReassure, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionRun = new CounterRun();
                    assignVariables = cspModule.AssignVariables(ref counterreactionRun, currentState, agent);
                    counterreactionRun.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionRun.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionRun, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionRun, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionRun, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (!counterreactionFound)
                {
                    PlanAction counterreactionMove = new CounterMove();
                    assignVariables = cspModule.AssignVariables(ref counterreactionMove, currentState, agent);
                    counterreactionMove.Arguments.Add(currentAction);

                    if (assignVariables && counterreactionMove.CheckPreconditions(currentState))
                    {
                        bool constractionAndDeadEndAndCicle = false;
                        bool duplicate = false;

                        CounterreactionControl(counterreactionMove, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                               ref constractionAndDeadEndAndCicle, ref duplicate);

                        if (stageOne_NewNode)
                        {
                            if (constractionAndDeadEndAndCicle && duplicate)
                            {
                                ApplyAction(counterreactionMove, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                                counterreactionFound = true;
                            }
                        }
                        else if (stageTwo_ConnectedNode)
                        {
                            if (constractionAndDeadEndAndCicle && !duplicate)
                            {
                                currentGraph.DuplicateNodeConnecting(currentState, counterreactionMove, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                                counterreactionFound = true;
                            }
                        }
                    }
                }

                if (stageTwo_ConnectedNode)
                {
                    PlanAction counterreactionSkip = new NothingToDo();
                    counterreactionSkip.Arguments.Add(agent);

                    bool constractionAndDeadEndAndCicle = false;
                    bool duplicate = false;

                    CounterreactionControl(counterreactionSkip, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue,
                                           ref constractionAndDeadEndAndCicle, ref duplicate);

                    if (constractionAndDeadEndAndCicle && duplicate)
                    {
                        ApplyAction(counterreactionSkip, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true);
                        counterreactionFound = true;
                    }
                    else if (constractionAndDeadEndAndCicle && !duplicate)
                    {
                        currentGraph.DuplicateNodeConnecting(currentState, counterreactionSkip, agent, currentNode, globalNodeNumber, ref queue, true, ref skip);
                        counterreactionFound = true;
                    }
                }

                //stageOne_NewNode = false;
                //stageTwo_ConnectedNode = true;


                if (stageOne_NewNode)
                {
                    stageOne_NewNode       = false;
                    stageTwo_ConnectedNode = true;
                }
                else if (stageTwo_ConnectedNode)
                {
                    stageOne_NewNode       = true;
                    stageTwo_ConnectedNode = false;
                }
            }
        }
Esempio n. 19
0
 public void ClearAction()
 {
     action = null;
 }
Esempio n. 20
0
 public void SetAction(PlanAction action)
 {
     this.action = action;
 }
Esempio n. 21
0
        /// <summary>
        /// Calculation of the probability of success of the action.
        /// </summary>
        public bool ProbabilityCalculating(PlanAction action)
        {
            Random random      = new Random();
            int    probability = random.Next(0, 100);
            int    threshold   = 0;

            if (action is Entrap)
            {
                threshold = 80;
            }
            else if (action is CounterEntrap)
            {
                threshold = 100;
            }
            else if (action is Fight)
            {
                threshold = 75;
            }
            else if (action is CounterFight)
            {
                threshold = 100;
            }
            else if (action is InvestigateRoom || action is CounterInvestigateRoom)
            {
                threshold = 60;
            }
            else if (action is Kill || action is CounterKill)
            {
                threshold = 100;
            }
            else if (action is Move || action is CounterMove)
            {
                threshold = 100;
            }
            else if (action is NeutralizeKiller || action is CounterNeutralizeKiller)
            {
                threshold = 100;
            }
            else if (action is NothingToDo)
            {
                threshold = 100;
            }
            else if (action is Reassure)
            {
                threshold = 80;
            }
            else if (action is CounterReassure)
            {
                threshold = 100;
            }
            else if (action is Run || action is CounterRun)
            {
                threshold = 100;
            }
            else if (action is TellAboutASuspicious)
            {
                threshold = 80;
            }
            else if (action is CounterTellAboutASuspicious)
            {
                threshold = 100;
            }
            else if (action is Talk || action is CounterTalk)
            {
                threshold = 100;
            }

            if (probability <= threshold)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }