// tries to make pathfinding more natural like, instead of hugging walls the object tries to move toward corners and doors. public static void NaturalizePath(ref Vec2I[] path, int maxSteps) { if (path.Length < 3) { return; } if (maxSteps < 2) { return; } SingleLinkedList <Vec2I> naturalized = new SingleLinkedList <Vec2I>(); int s = 0; int e = 2; Vec2I[] lastLine = new Vec2I[0]; while (e < path.Length) { int steps = 0; again: Vec2I[] line = AxMath.RogueLine(path[s], path[e]).ToArray(); for (int i = 0; i < line.Length; i++) { if (!CanPath(line[i])) { goto failed; } } if (e < path.Length - 1 && steps <= maxSteps) { lastLine = line; steps++; e++; goto again; } failed: if (e > s + 2) { for (int i = 0; i < lastLine.Length; i++) { naturalized.InsertBack(lastLine[i]); } s = e; e = s + 2; continue; } naturalized.InsertBack(path[s]); s++; e = s + 2; } while (s < path.Length) { naturalized.InsertBack(path[s++]); } path = naturalized.ToArray(); }