Skip to content

Commit

Permalink
Added pathfinding + saving of path
Browse files Browse the repository at this point in the history
  • Loading branch information
devedse committed Nov 10, 2016
1 parent 0fbba21 commit d1e89c4
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 51 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projects": [ "src", "test", "ImageSharp/src" ],
"sdk": {
"version": "1.0.0-preview2-003121"
"version": "1.0.0-preview2-003133"
}
}
3 changes: 2 additions & 1 deletion src/DeveMazeGenerator/Generators/AlgorithmBacktrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using DeveMazeGenerator.Factories;
using DeveMazeGenerator.Structures;

namespace DeveMazeGenerator.Generators
{
Expand All @@ -16,7 +17,7 @@ public override InnerMap GoGenerate<M>(IInnerMapFactory<M> mapFactory, IRandomFa
return GoGenerateInternal(innerMap, random, pixelChangedCallback);
}

public InnerMap GoGenerateInternal(InnerMap map, IRandom random, Action<int, int, long, long> pixelChangedCallback)
private InnerMap GoGenerateInternal(InnerMap map, IRandom random, Action<int, int, long, long> pixelChangedCallback)
{
long totSteps = (map.Width - 1L) / 2L * ((map.Height - 1L) / 2L);
long currentStep = 1;
Expand Down
10 changes: 2 additions & 8 deletions src/DeveMazeGenerator/Generators/AlgorithmDivisionDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using DeveMazeGenerator.Factories;
using DeveMazeGenerator.Helpers;
using DeveMazeGenerator.Structures;

namespace DeveMazeGenerator.Generators
{
Expand Down Expand Up @@ -102,7 +103,6 @@ public InnerMap GenerateMapPart(int xStart, int yStart, int width, int height, i
random.Reinitialise(curRect.Seed);

bool horizontalSplit = true;
//form.drawRectangle(curRect.X, curRect.Y, curRect.Width, curRect.Height, Brushes.Pink);

if (curRect.Width > curRect.Height)
{
Expand All @@ -128,7 +128,6 @@ public InnerMap GenerateMapPart(int xStart, int yStart, int width, int height, i
Rectangle rect1 = new Rectangle(curRect.X, curRect.Y, curRect.Width, splitnumber + 1, random.Next());
Rectangle rect2 = new Rectangle(curRect.X, curRect.Y + splitnumber, curRect.Width, curRect.Height - splitnumber, random.Next());


int xStartDraw = Math.Max(0, curRect.X - xStart);
int xEndDraw = Math.Min(widthPart, curRect.X - xStart + curRect.Width);

Expand Down Expand Up @@ -162,7 +161,6 @@ public InnerMap GenerateMapPart(int xStart, int yStart, int width, int height, i
Rectangle rect1 = new Rectangle(curRect.X, curRect.Y, splitnumber + 1, curRect.Height, random.Next());
Rectangle rect2 = new Rectangle(curRect.X + splitnumber, curRect.Y, curRect.Width - splitnumber, curRect.Height, random.Next());


var yStartDraw = Math.Max(0, curRect.Y - yStart);
int yEndDraw = Math.Min(heightPart, curRect.Y - yStart + curRect.Height);

Expand All @@ -178,8 +176,7 @@ public InnerMap GenerateMapPart(int xStart, int yStart, int width, int height, i
}
}
}



if (IsValidRect(visibleRectangle, rect1))
{
rectangles.Push(rect1);
Expand All @@ -189,9 +186,6 @@ public InnerMap GenerateMapPart(int xStart, int yStart, int width, int height, i
rectangles.Push(rect2);
}
}



}

return map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DeveMazeGenerator.Generators.Helpers;
using DeveMazeGenerator.Helpers;
using DeveMazeGenerator.InnerMaps;
using DeveMazeGenerator.Structures;
using System;
using System.Collections.Generic;

Expand Down
22 changes: 0 additions & 22 deletions src/DeveMazeGenerator/Generators/InnerMapType.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/DeveMazeGenerator/Helpers/MazeVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DeveMazeGenerator.Generators;
using DeveMazeGenerator.InnerMaps;
using DeveMazeGenerator.Structures;
using System.Collections.Generic;

namespace DeveMazeGenerator.Helpers
Expand Down
68 changes: 68 additions & 0 deletions src/DeveMazeGenerator/Imageification/WithPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using DeveMazeGenerator.InnerMaps;
using DeveMazeGenerator.Structures;
using ImageSharp;
using System.Collections.Generic;
using System.IO;

namespace DeveMazeGenerator.Imageification
{
public class WithPath
{
private void SaveMazeAsImageDeluxePng(InnerMap map, List<MazePointPos> pathPosjes, Stream stream)
{
pathPosjes.Sort((first, second) =>
{
if (first.Y == second.Y)
{
return first.X - second.X;
}
return first.Y - second.Y;
});


int curpos = 0;

var image = new Image(map.Width - 1, map.Height - 1);
using (var pixels = image.Lock())
{
for (int y = 0; y < map.Height - 1; y++)
{
for (int x = 0; x < map.Width - 1; x++)
{
int r = 0;
int g = 0;
int b = 0;

MazePointPos curPathPos;
if (curpos < pathPosjes.Count)
{
curPathPos = pathPosjes[curpos];
if (curPathPos.X == x && curPathPos.Y == y)
{
r = curPathPos.RelativePos;
g = 255 - curPathPos.RelativePos;
b = 0;
curpos++;
}
else if (map[x, y])
{
r = 255;
g = 255;
b = 255;
}
}
else if (map[x, y])
{
r = 255;
g = 255;
b = 255;
}
pixels[x, y] = new Color((byte)r, (byte)g, (byte)b);
}
//lineSavingProgress(y, this.Height - 2);
}
}
image.SaveAsPng(stream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using ImageSharp;
using System.IO;

namespace DeveMazeGenerator.Helpers
namespace DeveMazeGenerator.Imageification
{
public class MazeImager
public class WithoutPath
{
public static void MazeToImage(InnerMap map, Stream stream)
{
Expand Down
158 changes: 158 additions & 0 deletions src/DeveMazeGenerator/PathFinders/PathFinderDepthFirstSmart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using DeveMazeGenerator.InnerMaps;
using DeveMazeGenerator.Structures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DeveMazeGenerator.PathFinders
{
public class PathFinderDepthFirstSmart
{
/// <summary>
/// Finds the path between the start and the endpoint in a maze
/// </summary>
/// <param name="map">The maze.InnerMap</param>
/// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param>
/// <returns>The shortest path in a list of points</returns>
public static List<MazePoint> GoFind(InnerMap map, Action<int, int, Boolean> callBack)
{
return GoFind(new MazePoint(1, 1), new MazePoint(map.Width - 3, map.Height - 3), map, callBack);
}

/// <summary>
/// Finds the path between the start and the endpoint in a maze
/// </summary>
/// <param name="start">The start point</param>
/// <param name="end">The end point</param>
/// <param name="map">The maze.InnerMap</param>
/// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param>
/// <returns>The shortest path in a list of points</returns>
public static List<MazePoint> GoFind(MazePoint start, MazePoint end, InnerMap map, Action<int, int, Boolean> callBack)
{
if (callBack == null)
{
callBack = (x, y, z) => { };
}


//Callback won't work nice with this since it will find its path from back to front
//Swap them so we don't have to reverse at the end ;)
//MazePoint temp = start;
//start = end;
//end = temp;



int width = map.Width;
int height = map.Height;


List<MazePoint> stackje = new List<MazePoint>();
stackje.Add(start);

MazePoint cur = new MazePoint();
MazePoint prev = new MazePoint(-1, -1);


var lastBackTrackDir = -1;

while (stackje.Count != 0)
{

cur = stackje[stackje.Count - 1];
var x = cur.X;
var y = cur.Y;


MazePoint target = new MazePoint(-1, -1);
//Make sure the point was not the previous point, also make sure that if we backtracked we don't go to a direction we already went to, also make sure that the point is white
if ((prev.X != x + 1 || prev.Y != y) && lastBackTrackDir < 0 && x + 1 < width - 1 && map[x + 1, y])
{
target = new MazePoint(x + 1, y);
}
else if ((prev.X != x || prev.Y != y + 1) && lastBackTrackDir < 1 && y + 1 < height - 1 && map[x, y + 1])
{
target = new MazePoint(x, y + 1);
}
else if ((prev.X != x - 1 || prev.Y != y) && lastBackTrackDir < 2 && x - 1 > 0 && map[x - 1, y])
{
target = new MazePoint(x - 1, y);
}
else if ((prev.X != x || prev.Y != y - 1) && lastBackTrackDir < 3 && y - 1 > 0 && map[x, y - 1])
{
target = new MazePoint(x, y - 1);
}
else
{
var prepoppy = stackje[stackje.Count - 1];
stackje.RemoveAt(stackje.Count - 1);

if (stackje.Count == 0)
{
//No path found
break;
}

var newcur = stackje[stackje.Count - 1];

//Set the new previous point
if (stackje.Count == 1)
{
prev = new MazePoint(-1, -1);
}
else
{
prev = stackje.ElementAt(stackje.Count - 2);
}

//Console.WriteLine("Backtracking to X: " + newcur.X + " Y: " + newcur.Y);
//Console.WriteLine("Setting new prev: " + prev.X + " Y: " + prev.Y);

callBack.Invoke(prepoppy.X, prepoppy.Y, false);

//Set the direction we backtracked from
if (prepoppy.X > newcur.X)
{
lastBackTrackDir = 0;
}
else if (prepoppy.Y > newcur.Y)
{
lastBackTrackDir = 1;
}
else if (prepoppy.X < newcur.X)
{
lastBackTrackDir = 2;
}
else if (prepoppy.Y < newcur.Y)
{
lastBackTrackDir = 3;
}

//Console.WriteLine("Lastbacktrackdir: " + lastBackTrackDir);
continue;

}

lastBackTrackDir = -1;

//Console.WriteLine("Going to X: " + target.X + " Y: " + target.Y);

callBack.Invoke(x, y, true);

stackje.Add(target);

if (target.X == end.X && target.Y == end.Y)
{
//Path found
break;
}

prev = cur;

}

return stackje;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DeveMazeGenerator.Generators
namespace DeveMazeGenerator.Structures
{
/// <summary>
/// Contains a position.
Expand All @@ -16,7 +16,7 @@ public MazePoint(int X, int Y)

public override string ToString()
{
return "MazePoint, X: " + X + ", Y: " + Y;
return $"MazePointPos(X: {X} Y: {Y})";
}
}
}
Loading

0 comments on commit d1e89c4

Please sign in to comment.