-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainUnit.pas
274 lines (213 loc) · 8.75 KB
/
MainUnit.pas
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
// originally written by William Cairns - http://www.cairnsgames.co.za
// http://www.pascalgamedevelopment.com/forums/profile.php?mode=viewprofile&u=65
// Enchanchements, additional code by Jernej L.
// http://www.gtatools.com
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, Grids, Astar;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
MainMenu1: TMainMenu;
EndPoint1: TMenuItem;
Block1: TMenuItem;
CLEARDATA1: TMenuItem;
TESTO1: TMenuItem;
EndPoint2: TMenuItem;
Floor1: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
procedure StartPoint1Click(Sender: TObject);
procedure EndPoint1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Block1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
procedure CLEARDATA1Click(Sender: TObject);
procedure TESTO1Click(Sender: TObject);
procedure Floor1Click(Sender: TObject);
procedure FormResize(Sender: TObject);
private
FStartPoint: TPoint;
FEndPoint: TPoint;
procedure SetStartPoint(const Value: TPoint);
procedure SetEndPoint(const Value: TPoint);
private
{ Private declarations }
Searching, Found: Boolean;
property StartPoint: TPoint read FStartPoint write SetStartPoint;
property EndPoint: TPoint read FEndPoint write SetEndPoint;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SetEndPoint(const Value: TPoint);
begin
StringGrid1.Cells[FEndPoint.X, FEndPoint.Y] := '';
FEndPoint := Value;
// The Cell with Z in it is the goal Point of the Search
StringGrid1.Cells[FEndPoint.X, FEndPoint.Y] := 'Z';
end;
procedure TForm1.SetStartPoint(const Value: TPoint);
begin
StringGrid1.Cells[FStartPoint.X, FStartPoint.Y] := '';
FStartPoint := Value;
// The Cell with A in it is the starting Point of the Search
StringGrid1.Cells[FStartPoint.X, FStartPoint.Y] := 'A';
end;
procedure TForm1.StartPoint1Click(Sender: TObject);
begin
StartPoint := Point(StringGrid1.Selection.Left, StringGrid1.Selection.Top);
end;
procedure TForm1.EndPoint1Click(Sender: TObject);
begin
EndPoint := Point(StringGrid1.Selection.Left, StringGrid1.Selection.Top);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StartPoint := Point(3, 5);
EndPoint := Point(7, 5);
Searching := False;
Found := False;
end;
procedure TForm1.Block1Click(Sender: TObject);
var
I, J: Integer;
begin
// X indicates unpassable
// Ensure Start and End Points are not overwritten
for I := StringGrid1.Selection.Left to StringGrid1.Selection.Right do
for J := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do
if not ((I = StartPoint.X) and (J = StartPoint.Y)) and not ((I = EndPoint.X) and (J = EndPoint.Y)) then
StringGrid1.Cells[I, J] := 'X';
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
food: single;
begin
//inherited;
with Sender as TDrawGrid do
begin
Canvas.Brush.Color := clwindow;
if gdselected in State then
begin
Canvas.Brush.Color := clbtnshadow;
end;
if StringGrid1.cells[ACol, ARow] = 'A' then
Canvas.Brush.Color := clyellow;
if StringGrid1.cells[ACol, ARow] = 'Z' then
Canvas.Brush.Color := clblue;
if StringGrid1.cells[ACol, ARow] = 'X' then
Canvas.Brush.Color := clblack;
if StringGrid1.cells[ACol, ARow] = 'P' then
Canvas.Brush.Color := clred;
Canvas.FillRect(Rect);
food := strtointdef(StringGrid1.cells[ACol, ARow], 0);
if (maxval = 0) or (food = 0) then
exit;
try
food := 128 / maxval;
Canvas.Brush.Color := rgb(255 - round(food * strtointdef(StringGrid1.cells[ACol, ARow], 0)), 255 - round(food * strtointdef(StringGrid1.cells[ACol, ARow], 0)), 255 - round(food * strtointdef(StringGrid1.cells[ACol, ARow], 0)));
except
end;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.left, Rect.top, StringGrid1.cells[ACol, ARow]);
end;
//Canvas.DrawFocusRect(Rect);
end;
procedure TForm1.CLEARDATA1Click(Sender: TObject);
var
I, J: Integer;
begin
for I := 0 to StringGrid1.colcount do
for J := 0 to StringGrid1.rowcount do
if not ((I = StartPoint.X) and (J = StartPoint.Y)) and not ((I = EndPoint.X) and (J = EndPoint.Y)) then
if StringGrid1.Cells[I, J] <> 'X' then
StringGrid1.Cells[I, J] := '';
Searching := false;
Found := false;
setlength(Astack, 0);
end;
procedure TForm1.Floor1Click(Sender: TObject);
var
I, J: Integer;
begin
// X indicates unpassable
// Ensure Start and End Points are not overwritten
for I := StringGrid1.Selection.Left to StringGrid1.Selection.Right do
for J := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do
if not ((I = StartPoint.X) and (J = StartPoint.Y)) and not ((I = EndPoint.X) and (J = EndPoint.Y)) then
StringGrid1.Cells[I, J] := '';
end;
procedure TForm1.FormResize(Sender: TObject);
begin
StringGrid1.rowcount := StringGrid1.height div StringGrid1.defaultrowheight - 1;
StringGrid1.colcount := StringGrid1.width div StringGrid1.defaultcolwidth - 1;
end;
function blocktester(X, Y, Fx, Fy: integer): integer;
begin
// this function is called when path finder asks you weither it can process coordinate X,Y
// Fx, Fy are the coordinates from where the pathfinder is coming, you can use
// that to make some blocks only passable thru some sides and not all 4.
// you return -1 if you dont allow pathfinder to go to that block - like walls,
// this can be used to limit search area of the pathfinder as well.
// if you allow the pathfinder to go to that specific block, return a positive number,
// returning zero means you want to terminate path finding.
result := -1; // if it isnt anything else - it is wall
with Form1 do
begin
// you MUST allow it to go into empty cubes AND start cube as well otherwise it won't find path back to the start!
if (StringGrid1.Cells[X, Y] = '') or // allow empty cells
(StringGrid1.Cells[X, Y] = 'A') // allow to go back to the goal
// THIS is the guts of path finding, the magic formula that tells the pathfinder
// which blocks are worth more, so you can make it to rush to the goal or to
// find the best path ever.. depends on how much cpu power you can spend!
// the default cost for passing 1 cube sideways is 10
// if you use diagonal movement those cost 14
// the final formula is sideways/diagonal cost + what you tell it here
then
result := ((ABS(EndPoint.X - X) + ABS(EndPoint.Y - Y)) * 3);
end;
end;
procedure TForm1.TESTO1Click(Sender: TObject);
var
I, J: Integer;
ms: integer;
begin
CLEARDATA1Click(CLEARDATA1);
ms := gettickcount;
Astar.findpath(StartPoint, EndPoint, point(StringGrid1.colcount, StringGrid1.rowcount), true, true, @blocktester);
ms := gettickcount - ms;
// note that the timing is limited to 16 ms windows timer precision...
// show what type of path this is
if astar.IsClosest = true then
caption := 'Pathfinder - Closest Path' // close as it gets
else if (high(astar.path) = -1) and (astar.Found = true) then
caption := 'Pathfinder - Immediate Path' // no actual path - the goal is right next to start
else
caption := 'Pathfinder - Direct Path'; // normal path to the goal
if astar.Found = false then
caption := 'Pathfinder - No Path';
caption := format('%s (%d Ms)', [caption, ms]);
if astar.Found = false then
exit; // no drawing
{ For I := 0 to StringGrid1.colcount-1 do
For J := 0 to StringGrid1.rowcount-1 do begin
if (StringGrid1.Cells[I,J] <> 'A') then
if (StringGrid1.Cells[I,J] <> 'Z') then
if (StringGrid1.Cells[I,J] <> 'X') then
StringGrid1.Cells[I,J]:= inttostr(astar.grid[i, j]);
end; }
for I := 0 to high(astar.path) do
StringGrid1.Cells[astar.path[I].X, astar.path[I].Y] := 'P';
// highlight searching points
//for i:= 0 to high(astar.astack) do
//StringGrid1.Cells[astar.Astack[i].pt.x, astar.Astack[i].pt.y]:= 'P';
end;
end.