-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindPathClass.cs
360 lines (332 loc) · 10.5 KB
/
FindPathClass.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using UnityEngine;
using System;
using System.Collections;
public class cell
{
public int x;//êîîðäèíàòû òî÷êè
public int y;
public int f;
public int g;//îöåíêà ïðîéäåííîãî ðàññòîÿíèÿ îò ñòàðòà äî ýòîé òî÷êè
public int h;//ïðèìåðíàÿ îöåíêà ðàññòîÿíèÿ îò ýòîé òî÷êè äî ôèíèøà
public bool passable;//èñòèííà, åñëè ÷åðåç òî÷êó ìîæíî ïðîéòè, ëîæü, åñëè â äàííîé òî÷êå íàõîäèòñÿ ïðåïÿòñòâèå
public bool passed;
public cell parent;//ðîäèòåëüñêàÿ òî÷êà - ýòî òî÷êà èç êîòîðîé ìû ïðèøëè
public cell(int nx,int ny){
this.x = nx;
this.y = ny;
parent = null;
passed = false;
}
public cell(Vector3 c){
cell conc = new cell();
conc = conc.toGrid(c);
this.x = conc.x;
this.y = conc.y;
parent = null;
passed = false;
}
public cell()
{
parent = null;
passed = false;
}
void add_parent(cell p)
{
parent = p;
}
//Сравнение работает другим образом - сравниваются только координаты, без родителей, f, g и h
public override bool Equals(System.Object obj)
{
if (obj == null)
{
return false;
}
cell p = obj as cell;
if ((System.Object)p == null)
{
return false;
}
bool result = false;
if((this.x == p.x)&&(this.y == p.y)){
result = true;
}
return result;
}
public bool Equals(cell p)
{
// If parameter is null return false:
if ((System.Object)p == null)
{
return false;
}
// Return true if the fields match:
return (this.x == p.x) && (this.y == p.y);
}
public override int GetHashCode()
{
return x ^ y;
}
public float estimate_cost(cell p)//ïðèìåðíîå ðàññòîÿíèå äî öåëåâîé òî÷êè âû÷èñëÿåòñÿ êàê ìàíõýòýíñêîå ðàññòîÿíèå
{
int result;
result = Math.Abs(this.x-p.x)-Math.Abs(this.y-p.y);
return result;
}
public cell toGrid(Vector3 point){
int x = Convert.ToInt32(point.x / levelSettings.objectSize);
int y = Convert.ToInt32(point.y / levelSettings.objectSize);
cell res = new cell(x,y);
return res;
}
public cell toGrid(float ax, float ay){
int x = Convert.ToInt32(ax / levelSettings.objectSize);
int y = Convert.ToInt32(ay / levelSettings.objectSize);
cell res = new cell(x,y);
return res;
}
public point toPoint(cell a){
point res;
float x = a.x * levelSettings.objectSize + levelSettings.objectSize/2;
float y = a.y * levelSettings.objectSize + levelSettings.objectSize/2;
res = new point(new Vector3(x,y,0));
return res;
}
public bool isPassable(){
bool res = true;
Vector3 t = new Vector3(0,0,0);
object[] obst = GameObject.FindObjectsOfType(typeof (obsctacleAStar));
foreach (object o in obst){
obsctacleAStar g = (obsctacleAStar) o;
for(int i=this.x*levelSettings.objectSize;i<this.x*levelSettings.objectSize+levelSettings.objectSize;i++){
for(int j=this.y*levelSettings.objectSize;j<this.y*levelSettings.objectSize+levelSettings.objectSize;j++){
t = new Vector3(i,j,0);
if(g.isPointIn(t)){res = false;}
}
}
}
return res;
}
}
public class FindPathClass
{
public ArrayList allset = new ArrayList();//ýòî íàáîð âñåõ òî÷åê, êîòîðûå îáñ÷èòûâàåò àëãîðèòì
private ArrayList openset = new ArrayList();//ýòî íàáîð âñåõ åùž íå ïðîéäåííûõ, íî óæå îöåíåííûõ òî÷åê
private ArrayList closedset = new ArrayList();//ýòî íàáîð ïðîéäåííûõ òî÷åê
public ArrayList path = new ArrayList();//ýòîò íàáîð õðàíèò òî÷êè ïóòè â ïîðÿäêå åãî ïðîõîæäåíèÿ
public ArrayList createAllset(){
cell conv = new cell();
int k = 0;
ArrayList resset = new ArrayList();
int cell_num = levelSettings.levelSize/levelSettings.objectSize;
for(int x=0-cell_num;x<cell_num;x++){
for(int y=0-cell_num;y<cell_num;x++){
//Vector3 vect = new Vector3(x*levelSettings.objectSize,y*levelSettings.objectSize,0);
conv = new cell(x,y);
//if(k==1 || k==4){Debug.Log(conv.x);}
k = resset.Add(conv);
}
}
Debug.Log(k - allset.Count);
/*for(int i = 0;i<1000;i++){
Debug.Log("allset["+Convert.ToString(i)+"]");
cell c = (cell)resset[i];
Debug.Log(c.x);
}*/
//Debug.Log(resset.Count);
return resset;
}
public ArrayList buildPath(cell start, cell goal)
{
/*int k = 0;
foreach(cell d in allset){
k++;
if(k>40000){break;}
Debug.Log(Convert.ToString(d.x)+' '+Convert.ToString(d.y));
}*/
return this.algAStar(/*this.allset, */start, goal);
}
public ArrayList retrievePath()
{
return path;
}
public ArrayList reconstructPath(cell start, cell goal)
{
ArrayList result = new ArrayList();
bool fl = false;
int k = 0;
cell current = (cell)allset[allset.Count-1];
while(!(fl) && k<allset.Count)
{
/*result.Add(current);
if(current.parent == null){
fl = true;
}else{
current = current.parent;
}*/
current = (cell)allset[k];
result.Add(allset[k]);
k++;
}
result.Reverse();
return result;
}
cell lowestF(ArrayList goal_set)//ýòîò ìåòîä íàõîäèò òî÷êó ñ íàèìåíüøèì F â ïåðåäàííîì åìó ìàññèâå òî÷åê
{
cell resultP = new cell();
int min_f = int.MaxValue;
foreach(cell x in goal_set)
{
if(min_f>x.f)
{
min_f = x.f;
resultP = x;
}
}
return resultP;
}
bool isInSet(cell x, ArrayList rset){
foreach(cell b in rset){
if(b.x == x.x && b.y == x.y){
return true;
}
}
return false;
}
int indexOfCell(cell x, ArrayList rset){
int i = 0;
foreach(cell b in rset){
if(b.x == x.x && b.y == x.y){
return i;
}
i++;
}
return -1;
}
ArrayList allcellsNearX(ArrayList openset, ArrayList closedset, cell a)//ýòîò ìåòîä íàõîäèò âñå òî÷êè, ðàñïîëîæåííûå ðÿäîì ñ öåëåâîé òî÷êîé
{
ArrayList result = new ArrayList();
cell b = new cell(0,0);
/*foreach(cell b in allset)
{
if((b.x == a.x - 1)&&(b.y == a.y + 1)){result.Add(b);}
if((b.x == a.x)&&(b.y == a.y + 1)){result.Add(b);}
if((b.x == a.x + 1)&&(b.y == a.y + 1)){result.Add(b);}
if((b.x == a.x - 1)&&(b.y == a.y)){result.Add(b);}
if((b.x == a.x + 1)&&(b.y == a.y)){result.Add(b);}
if((b.x == a.x - 1)&&(b.y == a.y - 1)){result.Add(b);}
if((b.x == a.x)&&(b.y == a.y - 1)){result.Add(b);}
if((b.x == a.x - 1)&&(b.y == a.y - 1)){result.Add(b);}
}*/
point p = a.toPoint(a);
Vector3 vect = p.getCoord();
int ob = levelSettings.objectSize;
cell t;
t = b.toGrid(vect.x+ob,vect.y);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
t = b.toGrid(vect.x+ob,vect.y+ob);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
t = b.toGrid(vect.x,vect.y+ob);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
t = b.toGrid(vect.x-ob,vect.y);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
t = b.toGrid(vect.x-ob,vect.y-ob);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
t = b.toGrid(vect.x,vect.y-ob);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
t = b.toGrid(vect.x+ob,vect.y-ob);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
t = b.toGrid(vect.x-ob,vect.y+ob);
if(!(isInSet(t,openset)) && !(isInSet(t,closedset))){
result.Add(t);
}else{
if(isInSet(t,openset)){result.Add(openset[indexOfCell(t,openset)]);}
}
return result;
}
public ArrayList algAStar(/*ArrayList allset, */cell start, cell goal)//ýòî ðåàëèçàöèÿ àëãîðèòìà À*
{
cell p = start;
int tentative_g_score;//îöåíêà ñòîèìîñòè ïóòè ÷åðåç òî÷êó
bool tentative_is_better;//èñòèííî, åñëè ïóòü ÷åðåç òî÷êó ëó÷øå óæå ïðîéäåííîãî ïóòè
ArrayList nearcells = new ArrayList();
//closedset = null;
openset.Add(start);
start.g = 0;
start.h = Convert.ToInt32(start.estimate_cost(goal));
nearcells = allcellsNearX(openset,closedset,start);
p = lowestF(nearcells);
start.f = start.g + start.h;
int k = 0;
while(openset.Count>0)
{
k++;
//Debug.Log ("P:"+Convert.ToString(p.x)+' '+Convert.ToString(p.y));
nearcells = allcellsNearX(openset,closedset,p);
p = lowestF(nearcells);
if(k==100 || p == goal){
return reconstructPath(start,goal);
}
/*foreach(cell c in nearcells){
Debug.Log (Convert.ToString(c.x)+' '+Convert.ToString(c.y));
}*/
openset.Remove(p);
closedset.Add(p);
foreach(cell y in nearcells)//ïðîâåðÿåì âñå òî÷êè, ïðèìûêàþùèå ê õ
{
if((closedset.IndexOf(y)!=-1)||(y.isPassable()==false))
{
continue;//åñëè òî÷êà y óæå ïðîéäåíà èëè íåïðîõîäèìà, ïðîïóñêàåì åž
}
tentative_g_score = p.g + 1;//îöåíèâàåì ðàññòîÿíèå äëÿ ïðîõîæäåíèÿ ÷åðåç òî÷êó
if(openset.IndexOf(y)==-1)
{
openset.Add(y);
tentative_is_better = true;
}
else
{
if(tentative_g_score < y.g)
{tentative_is_better = true;}
else{tentative_is_better = false;}
}
if(tentative_is_better)
{
y.parent = p;
y.g = tentative_g_score;
y.h = Convert.ToInt32(y.estimate_cost(goal));
y.f = y.h + y.g;
allset.Add(y);
}
}
}
return null;
}
}