//Add a node to a parent node, ignoring if impassable or already closed, //adding if new, updating if new and better route private RouteNode AddAdjacentNode(int dx, int dy, int cost, RouteNode parent) { int x = parent.X + dx; int y = parent.Y + dy; int newCost = parent.MovementCost + cost; //Get terrain cost. -1 is not passable, 0 no cost, 1,2,3 etc higher cost int terraincost = GetTerrainCost(x, y); if (terraincost == -1) { return(null); } //Check if item has been added to the grid already RouteNode existing = (RouteNode)mGrid.Item(x, y); if (existing != null) { if (!existing.Closed && newCost < existing.MovementCost) { existing.MovementCost = newCost; existing.TotalCost = existing.MovementCost + existing.Heuristic; existing.Parent = parent; mOpenList.Update(existing); } return(existing); } //Create a new node RouteNode node = new RouteNode(); node.Parent = parent; node.X = x; node.Y = y; node.MovementCost = newCost; //Add the cost to the parent cost node.Heuristic = GetHeuristic(node); node.TotalCost = node.MovementCost + node.Heuristic; //Add terrain cost node.MovementCost += terraincost; node.TotalCost += terraincost; //Set generation node.Generation = mGeneration; mGeneration++; //Add to the open list and the grid mOpenList.Push(node); mGrid.Add(node); return(node); }
//Add a node to a parent node, ignoring if impassable or already closed, //adding if new, updating if new and better route private void AddAdjacentNode(int dx, int dy, int cost, RouteNode parent) { int x = parent.X + dx; int y = parent.Y + dy; int newCost = parent.MovementCost + cost; //Get terrain cost. -1 is not passable, 0 no cost, 1,2,3 etc higher cost int terraincost = GetTerrainCost(x, y); if (terraincost == -1) { return; } //Check if item has been added to the grid already RouteNode existing = _grid.Get(x, y); if (existing != null) { if (!existing.Closed && newCost < existing.MovementCost) { existing.MovementCost = newCost; existing.TotalCost = newCost + existing.Heuristic; existing.Parent = parent; if (_movementList.Contains(existing)) { _movementList.Update(existing); } else { _openList.Update(existing); } } return; } //Create a new node RouteNode node = new RouteNode(); node.X = x; node.Y = y; //Set the parent after the location so that the Direction can be set correctly node.Parent = parent; node.MovementCost = newCost; //Add the cost to the parent cost node.Heuristic = GetHeuristic(node); node.TotalCost = newCost + node.Heuristic; PushNode(node); //Add to the grid _grid.Add(x, y, node); }
private void AddRectangleToTerrain(Rectangle rect, int terraincost) { //Round down rect int x1 = Convert.ToInt32(rect.X / mGrain) * mGrain; int y1 = Convert.ToInt32(rect.Y / mGrain) * mGrain; int x2 = Convert.ToInt32(rect.Right / mGrain) * mGrain; int y2 = Convert.ToInt32(rect.Bottom / mGrain) * mGrain; //Add the top and bottom, if already exists then is ignored for (int i = x1; i <= x2; i += mGrain) { mTerrain.Add(new TerrainNode(i, y1, terraincost)); mTerrain.Add(new TerrainNode(i, y2, terraincost)); } //Add the left and right, if already exists then is ignored for (int i = y1; i <= y2; i += mGrain) { mTerrain.Add(new TerrainNode(x1, i, terraincost)); mTerrain.Add(new TerrainNode(x2, i, terraincost)); } }