/// <summary> /// Set cell at hexagon coordinates in system quarter /// </summary> /// <param name="t"> /// cell to set /// </param> /// <param name="a"> /// 1st coordinate (plane) /// </param> /// <param name="b"> /// 2nd coordinate (plane) /// </param> /// <param name="quarter"> /// index of the system quarter /// </param> /// <returns> /// returns if cell got saved /// </returns> private bool SetElementAtQuarter(MyHexCell t, int a, int b, int quarter) { if (coords[quarter].Count <= a) { for (int i = coords[quarter].Count; i <= a; i++) { coords[quarter].Add(new List <MyHexCell>()); } } if (coords[quarter][a].Count <= b) { for (int i = coords[quarter][a].Count; i <= b; i++) { coords[quarter][a].Add(null); } } if (coords[quarter][a][b] != null) { Debugger.LogWarning("HexCell is already Set!"); if (Debugger.IsInEditorMode() && !Debugger.IsInEditorPlayMode()) { } return(false); } else { coords[quarter][a][b] = t; return(true); } }
/// <summary> /// Set cell at hexagon coordinates on height /// </summary> /// <param name="t"> /// cell to set /// </param> /// <param name="a"> /// 1st coordinate (plane) /// </param> /// <param name="b"> /// 2nd coordinate (plane) /// </param> /// <param name="h"> /// 3rd coordinate (height) /// </param> /// <returns> /// returns if cell got saved /// </returns> public bool SetElementAt(MyHexCell t, int a, int b, int h) { bool r = false; if (a >= 0 && b >= 0) { r = SetElementAtQuarter(t, a, b, 0); } else if (a < 0 && b >= 0) { r = SetElementAtQuarter(t, -a - 1, b, 1); } else if (a >= 0 && b < 0) { r = SetElementAtQuarter(t, a, -b - 1, 2); } else { r = SetElementAtQuarter(t, -a - 1, -b - 1, 3); } t.pos.A = a; t.pos.B = b; t.pos.H = h; t.SnapToGridWithOffset(); return(r); }
public HexGridPathfinding(MyHexCell start, MyHexCell end, int allowedHeightDist) { this.start = start; this.end = end; this.allowedHeightDist = allowedHeightDist; pathExists = GetShortestAStarPathFor(start, end); }
public HexCoordsPathNode(MyHexCell coords, HexCoordsPathNode anchor, int fCost, int gCost) { this.coords = coords; this.anchor = anchor; this.fCost = fCost; this.gCost = gCost; }
bool InClosedList(MyHexCell t) { for (int i = 0; i < closed.Count; i++) { if (t == closed[i].coords) { return(true); } } return(false); }
/// <summary> /// Get cell at hexagon coordinates in system quarter /// </summary> /// <param name="a"> /// 1st coordinate (plane) /// </param> /// <param name="b"> /// 2nd coordinate (plane) /// </param> /// <returns> /// returns cell at coordinates /// </returns> private MyHexCell GetElementAtQuarter(int a, int b, int quarter) { MyHexCell t = null; if (coords[quarter].Count > a) { if (coords[quarter][a].Count > b) { t = coords[quarter][a][b]; } } return(t); }
public bool GetShortestAStarPathFor(MyHexCell start, MyHexCell dest) { MyHexCell tempRef = null; tempRef = start; HexCoordsPathNode temp = new HexCoordsPathNode(tempRef, null, 0, tempRef.pos.SSteps()); AddToOrUpdateOpen(temp); while (temp.coords != dest) { int lScore = LowestScoreInOpen(); if (lScore == -1) { return(false); } temp = open[lScore]; open.RemoveAt(lScore); closed.Add(temp); for (int i = 0; i < HexCoords.directions.Length; i++) { MyHexCell t = MyHexGrid.Instance.GetElementAt(temp.coords.pos + HexCoords.directions[i]); if (t && !InClosedList(t) && (t.pos.H - temp.coords.pos.H) <= allowedHeightDist) { AddToOrUpdateOpen(t, temp, temp.cost + t.costs, tempRef.pos.SSteps()); } } } while (temp.coords != start) { aStarPath.Add(temp.coords.pos); temp = temp.anchor; } aStarPath.Reverse(); return(true); }
bool AddToOrUpdateOpen(MyHexCell coords, HexCoordsPathNode anchor, int fCost, int gCost) { for (int i = 0; i < open.Count; i++) { if (coords == open[i].coords) { if (fCost < open[i].fCost) { open[i] = new HexCoordsPathNode(coords, anchor, fCost, gCost); return(true); } else { return(false); } } } open.Add(new HexCoordsPathNode(coords, anchor, fCost, gCost)); return(true); }
/// <summary> /// Get cell at hexagon coordinates /// </summary> /// <param name="a"> /// 1st coordinate (plane) /// </param> /// <param name="b"> /// 2nd coordinate (plane) /// </param> /// <returns> /// cell at coordinates /// </returns> public MyHexCell GetElementAt(int a, int b) { MyHexCell t = null; if (a >= 0 && b >= 0) { t = GetElementAtQuarter(a, b, 0); } else if (a < 0 && b >= 0) { t = GetElementAtQuarter(a * (-1) - 1, b, 1); } else if (a >= 0 && b < 0) { t = GetElementAtQuarter(a, b * (-1) - 1, 2); } else { t = GetElementAtQuarter(a * (-1) - 1, b * (-1) - 1, 3); } return(t); }
/// <summary> /// Set cell at hexagon coordinates /// </summary> /// <param name="t"> /// cell to set /// </param> /// <param name="a"> /// 1st coordinate (plane) /// </param> /// <param name="b"> /// 2nd coordinate (plane) /// </param> /// <returns> /// returns if cell got saved /// </returns> public bool SetElementAt(MyHexCell t, int a, int b) { return(SetElementAt(t, a, b, 0)); }
/// <summary> /// Set cell at hexagon coordinates on height /// </summary> /// <param name="t"> /// cell to set /// </param> /// <param name="hC"> /// Hexagon Coordinates the Tile has to be set to /// </param> /// <returns> /// returns if cell got saved /// </returns> public bool SetElementAt(MyHexCell t, HexCoords hC) { return(SetElementAt(t, hC.A, hC.B, hC.H)); }