コード例 #1
0
    /// <summary>
    /// Returns the shortest path from source to destination
    /// using A* algorithm.
    /// </summary>
    /// <param name='src' type='GameObject'>
    /// Source.
    /// </param>
    /// <param name='dst' type='GameObject'>
    /// Destination
    /// </param>
    public List <GameObject> Astar(GameObject src, GameObject dst)
    {
        List <AiEdge> successors;
        AiVertex      auxVertex;
        int           cost = 0;
        bool          isInFrontier = false, isInExplored = false;
        AiNode        node    = new AiNode(src, 1);
        AiNode        dstNode = new AiNode(dst, 1);
        AiNode        child;

        GPWiki.BinaryHeap <AiNode> frontier = new GPWiki.BinaryHeap <AiNode>();
        List <GameObject>          explored = new List <GameObject>();

        frontier.Add(node);
        while (true)
        {
            if (frontier.Count == 0)
            {
                return(new List <GameObject>());
            }
            node = frontier.Remove();
            explored.Add(node.m_GameObject);
            if (node.Equals(dstNode))
            {
                return(explored);
            }
            auxVertex  = node.m_GameObject.GetComponent <AiVertex>();
            successors = auxVertex.m_Successors;
            foreach (AiEdge e in successors)
            {
                cost         = e.m_Cost;
                cost        += (int)Vector3.Distance(e.m_Vertex.gameObject.transform.position, dst.transform.position);
                child        = new AiNode(e.m_Vertex.gameObject, cost);
                isInFrontier = frontier.Contains(child);
                isInExplored = explored.Contains(child.m_GameObject);
                if (!isInExplored && !isInFrontier)
                {
                    frontier.Add(child);
                }
                else if (isInFrontier)
                {
                    foreach (AiNode n in frontier)
                    {
                        if (n.Equals(child) && (n.m_Cost > child.m_Cost))
                        {
                            frontier.Remove(child);
                            frontier.Add(child);
                        }
                    }
                }
            }
        }
    }
コード例 #2
0
    public List <GuildRecord> ComputeMapFlooding()
    {
        GPWiki.BinaryHeap <GuildRecord> open;
        open = new GPWiki.BinaryHeap <GuildRecord>();
        List <GuildRecord> closed;

        closed = new List <GuildRecord>();
        foreach (Guild g in guildList)
        {
            GuildRecord gr  = new GuildRecord();
            Vector3     pos = g.baseObject.transform.position;
            gr.location = GetNearestVertex(pos);
            gr.guild    = g;
            gr.strength = g.GetDropOff(0f);
            open.Add(gr);
        }
        while (open.Count != 0)
        {
            GuildRecord current;
            current = open.Remove();
            Vertex  v = current.location;
            Vector3 currPos;
            currPos = v.transform.position;
            Vertex[] neighbours;
            neighbours = GetNeighbours(v);
            foreach (Vertex n in neighbours)
            {
                Vector3 nPos     = n.transform.position;
                float   dist     = Vector3.Distance(currPos, nPos);
                float   strength = current.guild.GetDropOff(dist);
                if (strength < dropOffThreshold)
                {
                    continue;
                }
                GuildRecord neighGR = new GuildRecord();
                neighGR.location = n;
                neighGR.strength = strength;
                VertexInfluence vi;
                vi            = n as VertexInfluence;
                neighGR.guild = vi.guild;
                if (closed.Contains(neighGR))
                {
                    Vertex      location = neighGR.location;
                    int         index    = closed.FindIndex(x => x.location == location);
                    GuildRecord gr       = closed[index];
                    if (gr.guild.name != current.guild.name &&
                        gr.strength < strength)
                    {
                        continue;
                    }
                }
                else if (open.Contains(neighGR))
                {
                    bool mustContinue = false;
                    foreach (GuildRecord gr in open)
                    {
                        if (gr.Equals(neighGR))
                        {
                            mustContinue = true;
                            break;
                        }
                    }
                    if (mustContinue)
                    {
                        continue;
                    }
                }
                else
                {
                    neighGR          = new GuildRecord();
                    neighGR.location = n;
                }
                neighGR.guild    = current.guild;
                neighGR.strength = strength;
                open.Add(neighGR);
            }
            closed.Add(current);
        }
        return(closed);
    }