Exemplo n.º 1
0
        protected void takeStep(Agent agent, Node algorithmCurrent, Model model)
        {
            /* check to see if we didn't move at all */
            if (algorithmCurrent.isEqual(agent.getNode(Agent.NodeType.Current)))
                return;

            /* check to see if we need to backtrack */
            while (!algorithmCurrent.getParent().isEqual(agent.getNode(Agent.NodeType.Current)))
                algorithmCurrent = algorithmCurrent.getParent();

            /* take a step! */
            agent.setNode(Agent.NodeType.Current, algorithmCurrent);

            /* increment step counter */
            agent.getCounters().incSteps();
        }
Exemplo n.º 2
0
        public bool SetAgentTarget(Agent agent)
        {
            Agent agentInView = this.isAgentInView(agent);

            /* check if agent is in view */
            if (agentInView != null && (simulation.getAgentCooperation() == View.AgentCooperation.Enabled))
            {
                /* agent in view, check if meeting has already occurred */
                if (agent.meetingListCheck(agentInView.getId()))
                {
                    /* meeting has already occurred so set the target node to finish */
                    agent.setNode(Agent.NodeType.Target, agent.getNode(Agent.NodeType.Finish));
                }
                else
                {
                    /* agent in view, check if both agents occupy the same same node or adjacent node */
                    if (agentInView.getNode(Agent.NodeType.Current).isEqual(agent.getNode(Agent.NodeType.Current)) ||
                        agentInView.getNode(Agent.NodeType.Current).isAdjacent(agent.getNode(Agent.NodeType.Current), simulation.get(View.ViewNumericUpDown.MapSize)))
                    {
                        /* agent in view & agents are in sharing distance, therefore update the visible flags */
                        int sharedNodeCount = 0;

                        foreach (Node agentInViewMapNode in agentInView.getMap().getNodes())
                        {
                            /* get the agent map node */
                            Node agentMapNode = agent.getMap().getNodeAtLocation(agentInViewMapNode.getPosition());

                            /* skip nodes that are not visible */
                            if (!agentInViewMapNode.getFlag(Node.Flag.IsVisible))
                                continue;

                            /* skip nodes that are already visible in the agent's map */
                            if (agentMapNode.getFlag(Node.Flag.IsVisible))
                                continue;

                            /* don't share shared nodes (IsShared flag is cleared after both agents complete the sharing) */
                            if (agentInViewMapNode.getFlag(Node.Flag.IsShared))
                                continue;

                            /* set flags flags */
                            agentMapNode.setFlag(Node.Flag.IsVisible, true);
                            agentMapNode.setFlag(Node.Flag.IsWalkable, agentInViewMapNode.getFlag(Node.Flag.IsWalkable));
                            agentMapNode.setFlag(Node.Flag.IsShared, true);

                            /* increment the shared node counter */
                            sharedNodeCount++;

                            /* paint the shared map if the agent is active */
                            if (agent.isActive())
                            {
                                /* get the shared map node */
                                Node sharedMapNode = map.getNodeAtLocation(agentMapNode.getPosition());

                                bool isWalkable = agentMapNode.getFlag(Node.Flag.IsWalkable);
                                bool isPathNode = map.isNodeFlag(sharedMapNode, Node.Flag.IsPath);
                                bool isSpecialNode = this.isSpecialNode(sharedMapNode);
                                bool isVisualizations = (simulation.getVisualizations() == View.Visualizations.Enabled) ? true : false;

                                /* dont repaint if its the start node, finish node, or a path node */
                                if (!isSpecialNode && !isPathNode)
                                {
                                    if (isWalkable)
                                    {
                                        if (isVisualizations)
                                            sharedMapNode.repaintNode(Color.White, Color.Black, sharedMapNode.Text);
                                        else
                                            sharedMapNode.repaintNode(Color.White, Color.White, "");
                                    }
                                    else
                                        sharedMapNode.repaintNode(Color.Gray, Color.Gray, "");
                                }
                            }
                        }

                        agent.meetingListAdd(agentInView.getId());
                        agent.getCounters().incNodesShared(sharedNodeCount);

                        /* set the target node to the finish node */
                        agent.setNode(Agent.NodeType.Target, agent.getNode(Agent.NodeType.Finish));

                        /* indicate that an algorithm reset is required because the target node was changed */
                        return true;
                    }
                    else
                    {
                        /* agent in view but both agents are not occupying the same node yet */
                        /* set the agent target node to the agent in view's current node */

                        Node target = agent.getMap().getNodeAtLocation(agentInView.getNode(Agent.NodeType.Current).getPosition());
                        agent.setNode(Agent.NodeType.Target, target);

                        /* indicate that an algorithm reset is required because a new target node has been set */
                        return true;
                    }
                }
            }
            else
            {
                /* agent not in view so just keep going to the agents finish node */
                agent.setNode(Agent.NodeType.Target, agent.getNode(Agent.NodeType.Finish));
            }

            /* indicate that an algorithm reset is not required because the target node was not changed */
            return false;
        }