public override void Generate() { status = Status.InProgress; //todo use seed int total = this.Nodes.Count * this.Nodes[0].Count; int visited = 1; List <Node> frontier = new List <Node>(); Node current = this.Nodes[(int)(_random.NextDouble() * 10) % this.Nodes.Count][(int)(_random.NextDouble() * 50) % this.Nodes[0].Count]; current.IsStart = true; for (int i = 0; i < current.Count; i++) { if (current[i] != null) { current[i].ParentInfo.Add(new ParentInfo(current, i)); frontier.Add(current[i]); current[i].IsFrontier = true; } } while (frontier.Count > 0) { current = frontier[(int)(_random.NextDouble() * 10) % frontier.Count]; current.IsFrontier = false; frontier.Remove(current); //random select a parent ParentInfo parentInfo = current.ParentInfo[(int)(_random.NextDouble() * 15) % current.ParentInfo.Count]; //break the wall //0-2 1-3 parentInfo.Parent.UnWall(parentInfo.Index); current.UnWall((parentInfo.Index + 2) % 4); //add new frontier for (int i = 0; i < current.Count; i++) { //not a frontier yet //and no walls destroyed if (current[i] != null && current[i].ParentInfo.Count == 0 && current[i].Wall == 0) { frontier.Add(current[i]); current[i].ParentInfo.Add(new ParentInfo(current, i)); current[i].IsFrontier = true; } } visited++; OnProgressChanged(visited, total); } OnComplete(); status = Status.Complete; }
public override void Generate() { int total = this.Nodes.Count * this.Nodes[0].Count; int visited = 1; List <Node> frontier = new List <Node>(); Random r = new Random(); //Select start node int x, y; if (wantedStartPos.x > -1) { x = wantedStartPos.x; } else { x = (int)(r.NextDouble() * 10) % this.Nodes.Count; } if (wantedStartPos.y > -1) { y = wantedStartPos.y; } else { y = (int)(r.NextDouble() * 50) % this.Nodes[0].Count; } Node current = this.Nodes[x][y]; current.isStart = true; for (int i = 0; i < current.Count; i++) { if (current[i] != null) { current[i].parentInfo.Add(new ParentInfo(current, i)); frontier.Add(current[i]); current[i].isFrontier = true; } } System.Diagnostics.Debug.WriteLine("max room: " + maxRoom); while (frontier.Count > 0) { current = frontier[(int)(r.NextDouble() * 10) % frontier.Count]; current.isFrontier = false; frontier.Remove(current); //random select a parent ParentInfo parentInfo = current.parentInfo[(int)(r.NextDouble() * 15) % current.parentInfo.Count]; //break the wall //0-2 1-3 parentInfo.Parent.UnWall(parentInfo.Index); current.UnWall((parentInfo.Index + 2) % 4); //add new frontier for (int i = 0; i < current.Count; i++) { //not a frontier yet //and no walls destroyed if (current[i] != null && current[i].parentInfo.Count == 0 && current[i].Wall == 0) { frontier.Add(current[i]); current[i].parentInfo.Add(new ParentInfo(current, i)); current[i].isFrontier = true; } } visited++; OnProgressChanged(visited, total); /* * if (current.Pos.X == 1 && current.Pos.Y == 1) * { * break; * } */ if (maxRoom > -1 && visited >= maxRoom)// -1 because start { break; } } System.Diagnostics.Debug.WriteLine("visited: " + visited); OnComplete(); }