public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
    {
        //we want low heuristic for best possible score
        //exploring lowest priority first gets you there quicker

        /*
         * x = x pos for node being checked
         * y = y pos for node being checked
         * start = startPos
         * goal = goalPos
         * gridScript = gridScript
         * abs(a.x - b.x) + abs(a.y - b.y)
         * (Mathf.Abs(start.x - goal.x)) + (Mathf.Abs(start.y - goal.y))
         *
         */

        float priority;
        float topSum     = 0;
        float bottomSum  = 0;
        float rightSum   = 0;
        float leftSum    = 0;
        float nodesCheck = 0;

        GameObject[,] pos = gridScript.GetGrid();

        //get the movementCost for each node above, below, left, and right of the main node
        //add those together and then divide that by the nodes checked to get the average
        //but also divide the nodesChecked by 5 because that's the number that gave me the lowest nodes
        if (x > 0 && x < gridScript.gridWidth && y + 1 > 0 && y + 1 < gridScript.gridHeight)
        {
            topSum = gridScript.GetMovementCost(pos[x, y + 1]);
            nodesCheck++;
        }
        if (x > 0 && x < gridScript.gridWidth && y - 1 > 0 && y - 1 < gridScript.gridHeight)
        {
            bottomSum = gridScript.GetMovementCost(pos[x, y - 1]);
            nodesCheck++;
        }
        if (x - 1 > 0 && x - 1 < gridScript.gridWidth && y > 0 && y < gridScript.gridHeight)
        {
            leftSum = gridScript.GetMovementCost(pos[x - 1, y]);
            nodesCheck++;
        }
        if (x + 1 > 0 && x + 1 < gridScript.gridWidth && y > 0 && y < gridScript.gridHeight)
        {
            rightSum = gridScript.GetMovementCost(pos[x + 1, y]);
            nodesCheck++;
        }

        priority = (Mathf.Abs(start.x - goal.x)) + (Mathf.Abs(start.y - goal.y)) * gridScript.costs[0];
        float otherSum = (topSum + bottomSum + leftSum + rightSum) / (nodesCheck * 5);

        priority += (otherSum + gridScript.GetMovementCost(pos[x, y]));
        //Debug.Log("priority = " + priority);

        return(priority);
    }
Example #2
0
    public virtual void Insert(int index, GameObject go)
    {
        float stepCost = gridScipt.GetMovementCost(go);

        score += stepCost;

        path.Insert(index, new Step(go, stepCost));

        steps++;
    }
    public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
    {
        return(ManHattanHeuristic(new Vector3(x, y), goal));

        GameObject[,] grid = gridScript.GetGrid();

        Vector3 vec = grid[x, y].transform.position;

        float score = 0;

        for (int dx = -1; dx <= 1; dx++)
        {
            for (int dy = -1; dy <= 1; dy++)
            {
                int nx = dx + x;
                int ny = dy + y;

                if (nx < 0 || nx >= grid.GetLength(0) ||
                    ny < 0 || ny >= grid.GetLength(1))
                {
                    score += 0;
                }
                else
                {
                    score += gridScript.GetMovementCost(grid[nx, ny]) /
                             ((ManHattanHeuristic(grid[nx, ny].transform.position, goal)));
                }
            }
        }

        return(score);
    }
Example #4
0
    public virtual float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
    {
        var _pos = gridScript.GetGrid();

        return(gridScript.GetMovementCost(_pos[x, y]) * Math.Abs(goal.x - x) + Math.Abs(goal.y - y));
        //return Random.Range(0, 500);
    }
Example #5
0
    public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
    {
        float priority;
        float topSum     = 0;
        float bottomSum  = 0;
        float rightSum   = 0;
        float leftSum    = 0;
        float nodesCheck = 0;

        GameObject[,] pos = gridScript.GetGrid();

        if (x > 0 && x < gridScript.gridWidth && y + 1 > 0 && y + 1 < gridScript.gridHeight)
        {
            topSum = gridScript.GetMovementCost(pos[x, y + 1]);
            nodesCheck++;
        }
        if (x > 0 && x < gridScript.gridWidth && y - 1 > 0 && y - 1 < gridScript.gridHeight)
        {
            bottomSum = gridScript.GetMovementCost(pos[x, y - 1]);
            nodesCheck++;
        }
        if (x - 1 > 0 && x - 1 < gridScript.gridWidth && y > 0 && y < gridScript.gridHeight)
        {
            leftSum = gridScript.GetMovementCost(pos[x - 1, y]);
            nodesCheck++;
        }
        if (x + 1 > 0 && x + 1 < gridScript.gridWidth && y > 0 && y < gridScript.gridHeight)
        {
            rightSum = gridScript.GetMovementCost(pos[x + 1, y]);
            nodesCheck++;
        }

        priority = (Mathf.Abs(start.x - goal.x)) + (Mathf.Abs(start.y - goal.y)) * gridScript.costs[0];
        float otherSum = (topSum + bottomSum + leftSum + rightSum) / (nodesCheck * 5);

        priority += (otherSum + gridScript.GetMovementCost(pos[x, y]));
//		Debug.Log("priority = " + priority);

        return(priority);
    }
Example #6
0
    public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
    {
        GameObject[,] grid = gridScript.GetGrid();

        float movementCost = gridScript.GetMovementCost(grid[x, y]);
        float damage       = ((DamageGridScript)gridScript).GetDamageCost(grid[x, y]);

        float distance = Vector2.Distance(start, goal);

        float cost = damage * avoidDamage + movementCost * shortestRoute;

        return(cost);
    }
    protected virtual void InitAstar(Path path)
    {
        this.path = path;

        _start = gridScript.start;
        _goal  = gridScript.goal;

        _gridWidth  = gridScript.gridWidth;
        _gridHeight = gridScript.gridHeight;

        _pos = gridScript.GetGrid();

        _frontier = new PriorityQueue <Vector3>();
        _frontier.Enqueue(_start, 0);

        _cameFrom.Add(_start, _start);
        _costSoFar.Add(_start, 0);

        var exploredNodes = 0;

        while (_frontier.Count != 0)
        {
            exploredNodes++;
            _current = _frontier.Dequeue();

            _visited.Add(_current);

            // _pos[(int)_current.x, (int)_current.y].transform.localScale = Vector3.Scale(_pos[(int)_current.x, (int)_current.y].transform.localScale, new Vector3(.8f, .8f, .8f));

            if (_current.Equals(_goal))
            {
                Debug.Log("GOOOAL!");
                break;
            }

            for (var x = -1; x < 2; x += 2)
            {
                AddNodesToFrontier((int)_current.x + x, (int)_current.y);
            }
            for (var y = -1; y < 2; y += 2)
            {
                AddNodesToFrontier((int)_current.x, (int)_current.y + y);
            }
        }

        _current = _goal;

        var line = GetComponent <LineRenderer>();

        var   i     = 0;
        float score = 0;

        while (!_current.Equals(_start))
        {
            line.positionCount++;

            var go = _pos[(int)_current.x, (int)_current.y];
            path.Insert(0, go, new Vector3((int)_current.x, (int)_current.y));

            _current = _cameFrom[_current];

            var vec = Util.clone(go.transform.position);
            vec.z = -1;

            line.SetPosition(i, vec);
            score += gridScript.GetMovementCost(go);
            i++;
        }

        path.Insert(0, _pos[(int)_current.x, (int)_current.y]);
        path.nodeInspected = exploredNodes;

        Debug.Log(path.pathName + " Terrain Score: " + score);
        Debug.Log(path.pathName + " Nodes Checked: " + exploredNodes);
        Debug.Log(path.pathName + " Total Score: " + (score + exploredNodes));
    }
    protected virtual void InitAstar(Path path)
    {
        this.path = path;

        start = gridScript.start;
        goal  = gridScript.goal;

        gridWidth  = gridScript.gridWidth;
        gridHeight = gridScript.gridHeight;

        pos = gridScript.GetGrid();

        frontier = new PriorityQueue <Vector3>();
        frontier.Enqueue(start, 0);

        cameFrom.Add(start, start);
        costSoFar.Add(start, 0);

        int exploredNodes = 0;

        while (frontier.Count != 0)
        {
            exploredNodes++;
            current = frontier.Dequeue();

            visited.Add(current);

            pos[(int)current.x, (int)current.y].transform.localScale =
                Vector3.Scale(pos[(int)current.x, (int)current.y].transform.localScale, new Vector3(.8f, .8f, .8f));

            if (current.Equals(goal))
            {
                Debug.Log("GOOOAL!");
                break;
            }

            for (int x = -1; x < 2; x += 2)
            {
                AddNodesToFrontier((int)current.x + x, (int)current.y);
            }
            for (int y = -1; y < 2; y += 2)
            {
                AddNodesToFrontier((int)current.x, (int)current.y + y);
            }
        }

        current = goal;

        LineRenderer line = GetComponent <LineRenderer>();

        int   i     = 0;
        float score = 0;

        while (!current.Equals(start))
        {
            line.positionCount++;

            GameObject go = pos[(int)current.x, (int)current.y];
            path.Insert(0, go, new Vector3((int)current.x, (int)current.y));

            current = cameFrom[current];

            Vector3 vec = Util.clone(go.transform.position);
            vec.z = -1;

            line.SetPosition(i, vec);
            score += gridScript.GetMovementCost(go);
            i++;
        }

        path.Insert(0, pos[(int)current.x, (int)current.y]);
        path.nodeInspected = exploredNodes;

        Debug.Log(path.pathName + " Terrian Score: " + score);
        Debug.Log(path.pathName + " Nodes Checked: " + exploredNodes);
        Debug.Log(path.pathName + " Total Score: " + (score + exploredNodes));
    }
Example #9
0
 public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
 {
     return(gridScript.GetMovementCost(gridScript.GetGrid()[x, y]));
 }
Example #10
0
 private float GetCost(Vector3 point, GridScript gridScript)
 {
     return(gridScript.GetMovementCost(gridScript.GetGrid() [(int)point.x, (int)point.y]));
 }