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);
    }
Esempio n. 2
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);
    }
    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);
    }
Esempio n. 4
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);
    }
    public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
    {
        pos = gridScript.GetGrid();

        GameObject go = pos[x, y];

        Material mat = go.GetComponent <MeshRenderer>().sharedMaterial;

        if (mat.name == "Forest" || mat.name == "Water")
        {
            return(1000000000);
        }

        else
        {
            return(0);
        }
    }
Esempio n. 6
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);
    }
    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));
    }
Esempio n. 8
0
    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));
    }
Esempio n. 9
0
 public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
 {
     return(gridScript.GetMovementCost(gridScript.GetGrid()[x, y]));
 }
Esempio n. 10
0
        public override float Hueristic(int x, int y, Vector3 start, Vector3 goal, GridScript gridScript)
        {
            //from matt
//			if (x == goal.x && y == goal.y) {
//				Debug.LogError ("0");
////				return 0;
//			}
//
//			GameObject[,] grid = gridScript.GetGrid();
//
//			Vector3 vec = grid[x, y].transform.position;
//
////			return gridScript.GetMovementCost (grid [x, y]) /
////			((ManHattanHeuristic (grid [x, y].transform.position, goal)));
//
//			float score = 0;
//
//			for(int dx = -1; dx <= 1; dx++){
//				for(int dy = -1; dy <= 1; dy++){
//					if (dx == 0 || dy == 0) {
//
//						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 + 0f;

            //greedy best first
//			return (Mathf.Abs (x - goal.x) + Mathf.Abs (y - goal.y)) * gridScript.costs[0]; //290, (25,2)385
//			return (Mathf.Abs (x - goal.x) + Mathf.Abs (y - goal.y)) * gridScript.costs [0] + 0.000001f; //287


            //I did my best
            if (x == goal.x && y == goal.y)
            {
//				Debug.LogError ("0");
                return(0);
            }

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

            List <Vector2> t_currentShortList = new List <Vector2> ();

            t_currentShortList.Add(new Vector2(x - 1, y));
            t_currentShortList.Add(new Vector2(x + 1, y));
            t_currentShortList.Add(new Vector2(x, y - 1));
            t_currentShortList.Add(new Vector2(x, y + 1));

            for (int i = 0; i < t_currentShortList.Count; i++)
            {
                if (t_currentShortList[i].x < 0 || t_currentShortList[i].x >= grid.GetLength(0) ||
                    t_currentShortList[i].y < 0 || t_currentShortList[i].y >= grid.GetLength(1))
                {
                    t_currentShortList.RemoveAt(i);
                    i--;
                }
            }

            float t_currentShortValue = GetCost((Vector3)t_currentShortList [0], gridScript);

            for (int i = 1; i < t_currentShortList.Count; i++)
            {
                t_currentShortValue = Mathf.Min(t_currentShortValue, GetCost((Vector3)t_currentShortList [i], gridScript));
            }

            for (int i = 0; i < t_currentShortList.Count; i++)
            {
                if (GetCost((Vector3)t_currentShortList [i], gridScript) != t_currentShortValue)
                {
                    t_currentShortList.RemoveAt(i);
                    i--;
                }
            }

            List <Vector2> t_endShortList = new List <Vector2> ();

            t_endShortList.Add(new Vector2(goal.x - 1, goal.y));
            t_endShortList.Add(new Vector2(goal.x + 1, goal.y));
            t_endShortList.Add(new Vector2(goal.x, goal.y - 1));
            t_endShortList.Add(new Vector2(goal.x, goal.y + 1));

            for (int i = 0; i < t_endShortList.Count; i++)
            {
                if (t_endShortList[i].x < 0 || t_endShortList[i].x >= grid.GetLength(0) ||
                    t_endShortList[i].y < 0 || t_endShortList[i].y >= grid.GetLength(1))
                {
                    t_endShortList.RemoveAt(i);
                    i--;
                }
            }

            float t_endShortValue = GetCost((Vector3)t_endShortList [0], gridScript);

            for (int i = 1; i < t_endShortList.Count; i++)
            {
                t_endShortValue = Mathf.Min(t_endShortValue, GetCost((Vector3)t_endShortList [i], gridScript));
            }

            for (int i = 0; i < t_endShortList.Count; i++)
            {
                if (GetCost((Vector3)t_endShortList [i], gridScript) != t_endShortValue)
                {
                    t_endShortList.RemoveAt(i);
                    i--;
                }
            }

            float t_ManHattanHeuristic = ManHattanHeuristic(t_currentShortList [0], t_endShortList [0]);

            for (int i = 0; i < t_currentShortList.Count; i++)
            {
                for (int j = 0; j < t_endShortList.Count; j++)
                {
                    t_ManHattanHeuristic = Mathf.Min(t_ManHattanHeuristic, ManHattanHeuristic(t_currentShortList [0], t_endShortList [0]));
                }
            }

            return(t_ManHattanHeuristic * gridScript.costs [0] + t_currentShortValue + t_endShortValue + GetCost(goal, gridScript));               // 291
        }
Esempio n. 11
0
 private float GetCost(Vector3 point, GridScript gridScript)
 {
     return(gridScript.GetMovementCost(gridScript.GetGrid() [(int)point.x, (int)point.y]));
 }