Made in 2016

Pathfinding

. Project LinkGithub

Introduction

This is a small project that I made. This project uses the A* algorithem to find the fastes route from a to b. I see this more like a prove of my understanding of A* than anything else. To make it more intersting I made it so the villager has to get sticks and small rocks to get tools so he can cut down wood and stones to get more faster. The villager also has to eat and drink. He can eat berries taht can be gathered and drink from the well. I made his path visual with unity gizmos,.

Damagebles

This is the Node Class This class holds the data of each point.public class Node{ public Vector3 nodeWorldPos; public bool walkable; public int parentNodeX, parentNodeY; public int myNodeX, myNodeY; public int aCost, bCost, tCost; public bool Check; //this sets certain values when the class is made public Node(Vector3 _NodeWorldPos, bool _walkable, int _myNodeX, int _myNodeY){ myNodeX = _myNodeX; myNodeY = _myNodeY; nodeWorldPos = _NodeWorldPos; walkable = _walkable; } //this changes the costs public void ChangeCost(int _aCost, int _bCost){ aCost = _aCost; bCost = _bCost; tCost = aCost + bCost; } //this changes the parrent node of this node public void ChangeParent(int _parentNodeX, int _parentNodeY){ parentNodeX = _parentNodeX; parentNodeY = _parentNodeY; } }

Grid

This is part of the Grid class this function gives back a node pos(the closest position of the node in world poss) when given a position. //converting the given node to a nodepos public NodePos checkNode(Vector3 _nodeToCheck){ NodePos newPos = new NodePos(Mathf.Clamp(Mathf.RoundToInt(((_nodeToCheck.x - transform.position.x) - nodeSize / 2) / nodeSize), 1, gridSizeX - 1), Mathf.Clamp(Mathf.RoundToInt(((_nodeToCheck.z - transform.position.z) - nodeSize / 2) / nodeSize),1,gridSizeY - 1)); if(!grid[newPos.x,newPos.y].walkable){ List<NodePos> checkList = new List<NodePos>(); List<NodePos> doneList = new List<NodePos>(); checkList.Add(newPos); while (checkList.Count > 0){ for (int x = -1; x < 2; x++){ for (int y = -1; y < 2; y++){ if(!(x == 0 && y == 0) && checkList[0].x + x > 0 && checkList[0].x + x < gridSizeX && checkList[0].y + y > 0 && checkList[0].y + y < gridSizeY && !grid[checkList[0].x + x, checkList[0].y + y].Check){ grid[checkList[0].x + x, checkList[0].y + y].Check = true; checkList.Add(new NodePos(checkList[0].x + x, checkList[0].y + y)); if (grid[checkList[0].x + x, checkList[0].y + y].walkable){ foreach (NodePos v in checkList){ grid[v.x, v.y].Check = false; } foreach (NodePos n in doneList){ grid[n.x, n.y].Check = false; } return (new NodePos(checkList[0].x + x, checkList[0].y + y)); } } } } doneList.Add(checkList[0]); checkList.RemoveAt(0); } } return (newPos); }

Grid

This is part of the Grid class this geves back a vector3 list. This list is the path the AI can follow to get to thier goal. //Creating the path public List<Vector3> MakePath(Vector3 _targetPos, Vector3 _myPos){ targetNode.ChangeNodePos(checkNode(_targetPos).x, checkNode(_targetPos).y); myNode.ChangeNodePos(checkNode(_myPos).x, checkNode(_myPos).y); //Searching for path to the target FindNeighbors(myNode); List<NodePos> closeList = new List<NodePos>(); while (openList.Count > 0){ int lowest = 0; for (int i = 0; i < openList.Count; i++){ if (grid[openList[i].x, openList[i].y].tCost < grid[openList[lowest].x, openList[lowest].y].tCost){ lowest = i; } } closeList.Add(openList[lowest]); if (FindNeighbors(openList[lowest])){ break; } openList.RemoveAt(lowest); } //Backtracking most efficient path List<Vector3> pathList = new List<Vector3>(); NodePos nodeToAdd = closeList[closeList.Count - 1]; int l = 0; pathList.Add(grid[targetNode.x, targetNode.y].nodeWorldPos); while (l < maxTravelDistance){ l++; if (myNode.x == nodeToAdd.x && myNode.y == nodeToAdd.y){ gizmoPathList = pathList; //Remove gizmoCloseList = closeList; //Remove foreach (NodePos v in closeList){ grid[v.x, v.y].Check = false; } foreach (NodePos n in openList){ grid[n.x, n.y].Check = false; } openList.Clear(); return (pathList); } pathList.Add(grid[nodeToAdd.x, nodeToAdd.y].nodeWorldPos); nodeToAdd = new NodePos(grid[nodeToAdd.x, nodeToAdd.y].parentNodeX, grid[nodeToAdd.x, nodeToAdd.y].parentNodeY); } return (new List<Vector3>()); }

Grid

This is part of the Grid class this creates the grid used. void Awake(){ CreatGrid(); } //Creating the grid void CreatGrid() { Pathfinder = this; grid = new Node[gridSizeX, gridSizeY]; for(int x = 0; x < gridSizeX; x++){ for(int y = 0; y < gridSizeY; y++){ Vector3 newNodePos = new Vector3(transform.position.x + (x * nodeSize) + nodeSize / 2,0, transform.position.z + (y * nodeSize) + nodeSize / 2); grid[x, y] = new Node(newNodePos, !Physics.CheckSphere(newNodePos, nodeSize / 2,2) ,x,y); } } }

. Project LinkGithub

s