-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeGenerator.cs
210 lines (159 loc) · 6.33 KB
/
MazeGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Runtime.InteropServices;
using Colorful;
using Spectre.Console.Rendering;
using Resourses.Tools;
namespace Resourses.Logic;
public partial class Maze
{
//[i] - WSAD... ah... this no look gut :'c
Direction wsad = new();
//[i] - Here I will put all the methods nessesary for
// for starting to build the maze.
public void Create(Type type)
{
BuildMazeLogic();
Builder(type);
}
//[i] - First stage of the building process
// Here I create "logic" rooms, connections
// starting points and int[,] Lee for
// placing MapObjects
public void BuildMazeLogic()
{
PathMaker([0,0]);
}
private void PathMaker(int[] startPos)
{
//this ALL most be OPTIMIZED... what a maze XDDDDD...
Direction dir = new(); //and this... I should have the
//wsad as a class? how can I
//have an instanse of this available
//everywhere...
//lets see... WSAD as a parameter!!!!
int[] randNearStep = RandNearUnconnected_DirStep(startPos);
int[] nextPos = [startPos[0] + randNearStep[0], startPos[1] + randNearStep[1]];
if(TL.ArrEqual(startPos, nextPos))
{
// + + + rememer that next starting pos is con
// near to uncon
nextPos = ConNearUnconRoomPos();
if(!TL.ArrEqual(nextPos, [-1,-1])) PathMaker(nextPos);
}
else
{
Connecter(startPos, dir.GetInt(randNearStep));
PathMaker(nextPos);
}
}
private int[] ConNearUnconRoomPos()
{
for(int i = 0; i < mazeRooms.GetLength(0); i++)
{
for(int j = 0; j < mazeRooms.GetLength(1); j++)
{
int[] unconArr = UnconRoom_DirStep([i,j]);
if(mazeRooms[i,j].IsConnected() && !TL.ArrEqual( unconArr,[0,0,0,0,0,0,0,0])) //usa el m'etodo que vas a crear en tools
{
return [i,j];
}
}
}
return [-1,-1];
}
public int[] UnconRoom_DirStep(int[] pos)
{
//System.Console.WriteLine("UnconRoom_DirStep debug");
//System.Console.WriteLine(" ");
Direction wsad = new();
//method for geting the DirStep of near
//unconected rooms
int[] exit = wsad.GetDirsArr();
//System.Console.WriteLine("pos = ["+ pos[0] + ", " + pos[1] +"] exit = ["+ exit[0] + ", " + pos[1] + "]");
//System.Console.WriteLine(" ");
//each i value is a wsad direction
for(int i = 0; i < 4; i++)
{
//get the position of an step
int[] newPos = TL.PosStep(pos, i);
//System.Console.WriteLine("i = "+ i +" newPos = [" + newPos[0] + ", " + newPos[1] +"]");
//checks if the position dont exits or is connected
if(TL.PosStepInRange(pos, mazeRooms.GetLength(0), mazeRooms.GetLength(1), i))
{
//System.Console.WriteLine("position exist");
if(mazeRooms[newPos[0], newPos[1]].IsConnected())
{
//System.Console.WriteLine("is connected");
exit[i*2] = 0;
exit[(i*2)+1] = 0;
}
//the step in that direction will be 0,0
}
else
{
//System.Console.WriteLine("position dont exist");
exit[i*2] = 0;
exit[(i*2)+1] = 0;
}
//System.Console.WriteLine(" ");
}
return exit;
}
private int[] RandNearUnconnected_DirStep(int[] pos)
{
int[] optionsDir = UnconRoom_DirStep(pos);
int optionsNum = 4 - TL.CountArrInArrFixed(optionsDir, [0,0]);
//System.Console.WriteLine("debug RandNearUnconnected_DirStep");
//System.Console.WriteLine("optionsNum = " + optionsNum);
//System.Console.WriteLine("optionsDir = {[" + optionsDir[0] + ", " + optionsDir[1] + "]");
//System.Console.WriteLine(" [" + optionsDir[2] + ", " + optionsDir[3] + "]");
//System.Console.WriteLine(" [" + optionsDir[4] + ", " + optionsDir[5] + "]");
//System.Console.WriteLine(" [" + optionsDir[6] + ", " + optionsDir[7] + "]}");
//System.Console.WriteLine(" ");
int dice = new Random().Next(0, optionsNum);
//System.Console.WriteLine("dice = " + dice);
// + + here there are a lot of unnecessary things...
int unnecessaryCounter = 0;
//System.Console.WriteLine(" ");
//System.Console.WriteLine("random selection for()");
for(int i = 0; i < 4; i++)
{
//System.Console.WriteLine("i =" + i);
// + +
if(!(optionsDir[i*2] == 0 && optionsDir[(i*2)+1] == 0))
{
if(unnecessaryCounter == dice)
{
return [optionsDir[i*2],optionsDir[(i*2)+1]];
}
unnecessaryCounter++;
}
}
return [0,0];
}
private void Connecter(int[] pos, int dirInt)
{
//System.Console.WriteLine(" ");
//System.Console.WriteLine("debug Connecter");
//System.Console.WriteLine(" ");
//Excpt.InRange(0,3,dir); //esto debe estar en Directions.cs not here
//connect the current room with the next one
mazeRooms[pos[0], pos[1]].Connect(dirInt);
Direction wsad = new Direction();
//gets next room dir...
//[!]-Maybe this most be the other parameter of the
// method... the Dir up and down and all that
// just in the path maker...
pos = [pos[0] + wsad.GetDir(dirInt)[0], pos[1] + wsad.GetDir(dirInt)[1]];
//connect next room in the oposit direction...
//chechk math in Directions.cs preset wsad values...
if(dirInt%2 == 0)
{
mazeRooms[pos[0], pos[1]].Connect(dirInt+1);
}
else
{
mazeRooms[pos[0], pos[1]].Connect(dirInt-1);
}
}
}