Debian Codegraph @

src/transform/castlesectors.pas

raw
1 {
2 Copyright 2006-2018 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Sectors and waypoints, to improve creature AI in 3D levels.
17 This is a simple "navigation mesh". }
18 unit CastleSectors;
19
20 {$I castleconf.inc}
21
22 interface
23
24 uses SysUtils, Classes, Generics.Collections,
25 CastleUtils, CastleClassUtils, CastleVectors, CastleBoxes;
26
27 type
28 TSectorList = class;
29
30 TWaypoint = class
31 private
32 FSectors: TSectorList;
33 public
34 constructor Create;
35 destructor Destroy; override;
36 public
37 Position: TVector3;
38
39 { Box of the waypoint is only used by TSectorList.LinkToWaypoints,
40 to detect which sectors contain ths waypoint (presumably,
41 on their border).
42 This box is @italic(not) used by actual AI using waypoints/sectors
43 (only @link(Position) matters then). }
44 Box: TBox3D;
45
46 { Sectors that contain this waypoint. }
47 property Sectors: TSectorList read FSectors;
48 end;
49
50 TWaypointList = class({$ifdef FPC}specialize{$endif} TObjectList<TWaypoint>)
51 end;
52
53 TSector = class
54 private
55 FBoxes: TBox3DList;
56 FVisibleSectors: TBooleanList;
57 FWaypoints: TWaypointList;
58 public
59 constructor Create;
60 destructor Destroy; override;
61
62 property Boxes: TBox3DList read FBoxes;
63
64 { Is Point inside the sector. }
65 function Contains(const Point: TVector3): boolean;
66
67 { Does the box collide (at least partially) with sector. }
68 function Collision(const Box: TBox3D): boolean;
69
70 { What sectors are visible from this sector.
71
72 When reading this, you should generally be prepared that length
73 of it may be smaller than all sectors of your scene.
74 That's because sometimes we don't initialize VisibleSectors fully.
75
76 In this case you should assume that not initialized sector
77 indexes are visible. And always you can assume that
78 Always Sectors[I].VisibleSectors[I] = @true, i.e. the sector
79 is visible from itself (assuming that VisibleSectors has
80 enough length to contain I). }
81 property VisibleSectors: TBooleanList read FVisibleSectors;
82
83 { Waypoints that are included in this sector. }
84 property Waypoints: TWaypointList read FWaypoints;
85 end;
86
87 ESectorNotInitialized = class(Exception);
88 EWaypointNotInitialized = class(Exception);
89
90 TSectorList = class({$ifdef FPC}specialize{$endif} TObjectList<TSector>)
91 public
92 { Connect sectors and waypoints into a graph.
93 Adds appropriate waypoints to sectors and sectors to waypoints,
94 knowing which waypoint belongs (presumably, lies at the border of)
95 to which sector.
96 A waypoint belongs to the sector simply when TWaypoint.Box
97 collides with one of the sector boxes.
98
99 @raises ESectorNotInitialized When some sector is nil.
100 @raises EWaypointNotInitialized When some waypoint is nil. }
101 procedure LinkToWaypoints(Waypoints: TWaypointList);
102
103 { Returns sector with given point (using @link(TSector.Contains) of each sector).
104 Returns nil if no such sector. }
105 function SectorWithPoint(const Point: TVector3): TSector;
106
107 { This sets Waypoints contents to the list of waypoints
108 that must be passed to travel from sector SectorBegin to SectorEnd.
109
110 Special cases: when either SectorBegin or SectorEnd are nil
111 (this can easily happen if you pass here results
112 of SectorWithPoint method), or when SectorBegin = SectorEnd,
113 or when there is no possible way, it returns @false and
114 just clears Waypoints (i.e. sets Waypoints.Count to 0).
115
116 Otherwise (if a way is found) it returns @true and sets
117 Waypoints items as appropriate. The order of Waypoints
118 is significant: starting from SectorBegin, you should
119 first travel to Waypoints[0], then to Waypoints[1] etc.
120 In this case for sure we have at least one Waypoint.
121
122 (So the result of this function is actually just a comfortable
123 thing, you can get the same result just checking
124 Waypoints.Count <> 0)
125
126 TODO: This should use breadth-first search.
127 Right now it uses depth-first search. For small sectors+waypoints
128 graphs it doesn't matter. }
129 class function FindWay(SectorBegin, SectorEnd: TSector;
130 Waypoints: TWaypointList): boolean;
131 end;
132
133 var
134 LogSectors: boolean = false;
135
136 implementation
137
138 uses CastleStringUtils, CastleLog;
139
140 { TWaypoint ------------------------------------------------------------- }
141
142 constructor TWaypoint.Create;
143 begin
144 inherited Create;
145 FSectors := TSectorList.Create(false);
146 end;
147
148 destructor TWaypoint.Destroy;
149 begin
150 FreeAndNil(FSectors);
151 inherited;
152 end;
153
154 { TSector --------------------------------------------------------------- }
155
156 constructor TSector.Create;
157 begin
158 inherited Create;
159 FBoxes := TBox3DList.Create;
160 FVisibleSectors := TBooleanList.Create;
161 FWaypoints := TWaypointList.Create(false);
162 end;
163
164 destructor TSector.Destroy;
165 begin
166 FreeAndNil(FBoxes);
167 FreeAndNil(FVisibleSectors);
168 FreeAndNil(FWaypoints);
169 inherited;
170 end;
171
172 function TSector.Contains(const Point: TVector3): boolean;
173 var
174 I: Integer;
175 begin
176 for I := 0 to Boxes.Count - 1 do
177 if Boxes.L[I].Contains(Point) then
178 Exit(true);
179 Result := false;
180 end;
181
182 function TSector.Collision(const Box: TBox3D): boolean;
183 var
184 I: Integer;
185 begin
186 for I := 0 to Boxes.Count - 1 do
187 if Boxes.L[I].Collision(Box) then
188 Exit(true);
189 Result := false;
190 end;
191
192 { TSectorList -------------------------------------------------------- }
193
194 procedure TSectorList.LinkToWaypoints(Waypoints: TWaypointList);
195 var
196 S: TSector;
197 W: TWaypoint;
198 SectorIndex, WaypointIndex: Integer;
199 begin
200 for SectorIndex := 0 to Count - 1 do
201 begin
202 S := Items[SectorIndex];
203 if S = nil then
204 raise ESectorNotInitialized.CreateFmt('Sector %d not initialized',
205 [SectorIndex]);
206
207 for WaypointIndex := 0 to Waypoints.Count - 1 do
208 begin
209 W := Waypoints[WaypointIndex];
210 if W = nil then
211 raise EWaypointNotInitialized.CreateFmt('Waypoint %d not initialized',
212 [WaypointIndex]);
213
214 if S.Collision(W.Box) then
215 begin
216 S.Waypoints.Add(W);
217 W.Sectors.Add(S);
218 if LogSectors then
219 WritelnLog('Sectors', Format('Waypoint %d links to sector %d',
220 [WaypointIndex, SectorIndex]));
221 end;
222 end;
223 end;
224
225 if LogSectors then
226 begin
227 for WaypointIndex := 0 to Waypoints.Count - 1 do
228 if Waypoints[WaypointIndex].Sectors.Count <= 1 then
229 WritelnWarning('Sectors', Format('Waypoint %d only links to %d sectors. Waypoints that link to 1 or 0 sectors are useless.',
230 [WaypointIndex, Waypoints[WaypointIndex].Sectors.Count]));
231 for SectorIndex := 0 to Count - 1 do
232 if Items[SectorIndex].Waypoints.Count = 0 then
233 WritelnWarning('Sectors', Format('Sector %d is not connected to any waypoint. Such sectors are useless.',
234 [SectorIndex]));
235 end;
236 end;
237
238 function TSectorList.SectorWithPoint(const Point: TVector3):
239 TSector;
240 var
241 I: Integer;
242 begin
243 for I := 0 to Count - 1 do
244 begin
245 Result := Items[I];
246 if Result.Contains(Point) then
247 Exit;
248 end;
249 Result := nil;
250 end;
251
252 class function TSectorList.FindWay(SectorBegin, SectorEnd: TSector;
253 Waypoints: TWaypointList): boolean;
254 var
255 { This is used to avoid falling into loops. }
256 SectorsVisited: TSectorList;
257
258 function FindWayToSectorEnd(SectorNow: TSector;
259 SectorDistance: Integer): boolean;
260 var
261 WaypointIndex, SectorIndex: Integer;
262 W: TWaypoint;
263 begin
264 if SectorsVisited.IndexOf(SectorNow) <> -1 then
265 Exit(false);
266 SectorsVisited.Add(SectorNow);
267
268 if SectorNow = SectorEnd then
269 begin
270 Waypoints.Count := SectorDistance;
271 Exit(true);
272 end;
273
274 for WaypointIndex := 0 to SectorNow.Waypoints.Count - 1 do
275 begin
276 W := SectorNow.Waypoints[WaypointIndex];
277 for SectorIndex := 0 to W.Sectors.Count - 1 do
278 if FindWayToSectorEnd(W.Sectors[SectorIndex], SectorDistance + 1) then
279 begin
280 Waypoints[SectorDistance] := W;
281 Exit(true);
282 end;
283 end;
284
285 Result := false;
286 end;
287
288 begin
289 Waypoints.Count := 0;
290 if (SectorBegin = nil) or
291 (SectorEnd = nil) or
292 (SectorBegin = SectorEnd) then
293 Exit(false);
294
295 { Note that we know here that SectorBegin <> SectorEnd,
296 so the first call to FindWayToSectorEnd will not immediately
297 return with true, so Waypoints[0] will for sure be filled...
298 so Waypoints.Count will have to be > 0 in this case.
299 Just like I promised in the interface. }
300
301 SectorsVisited := TSectorList.Create(false);
302 try
303 Result := FindWayToSectorEnd(SectorBegin, 0);
304 finally SectorsVisited.Free end;
305 end;
306
307 end.