@@ -172,8 +172,8 @@ begin
172172 itemsA := Self.KDTree.KNearest([a.X, a.Y], amount, [ETreeSetting.IgnoreHidden]);
173173 itemsB := Self.KDTree.KNearest([b.X, b.Y], amount, [ETreeSetting.IgnoreHidden]);
174174
175- for iA to High(itemsA) do
176- for iB to High(itemsB) do
175+ for iA := 0 to High(itemsA) do
176+ for iB := 0 to High(itemsB) do
177177 if itemsA[iA].Ref = itemsB[iB].Ref then
178178 Exit([itemsA[iA], itemsB[iB]]);
179179
@@ -282,22 +282,16 @@ begin
282282 raise GetDebugLn('WebGraph', 'Points ' + ToStr(a) + ' and ' + ToStr(b) + ' don''t connect! No paths available.');
283283
284284 for i := 0 to High(intsA) do
285- begin
286- if intsA[i] = -1 then
287- Continue;
288285 for j := 0 to High(intsB) do
289286 begin
290- if intsB[j ] = -1 then
287+ if intsA[i ] = intsB[j] then
291288 Continue;
292- if intsA[i] = intsB[j] then Continue;
293-
294289 indices := Self.FindPath(intsA[i],intsB[j], rnd);
295- if Length( indices) > 0 then
290+ if indices <> [] then
296291 Break(2);
297292 end;
298- end;
299293
300- if Length( indices) = 0 then
294+ if indices = [] then
301295 raise GetDebugLn('WebGraph', 'Points ' + ToStr(a) + ' and ' + ToStr(b) + ' don''t connect!');
302296
303297 Result += a;
@@ -318,15 +312,15 @@ between.
318312function TWebGraph.NearestPathable(me: TPoint; destinations: TPointArray): TPoint;
319313var
320314 path: TPointArray;
321- shortPaths: array of TPointArray;
322- best, dist, i: Int32;
315+ best, dist, i, n: Int32;
323316 destination: TPoint;
324317 pathDist: Double;
325318begin
326319 best := $FFFFFF;
327320
328321 for destination in destinations do
329322 begin
323+ //straight line is further than best, no point in going further
330324 if not me.InRange(destination, best) then
331325 Continue;
332326
@@ -351,19 +345,20 @@ begin
351345
352346 if dist < best then
353347 begin
354- shortPaths := [ path] ;
348+ Result := path.Last ;
355349 best := dist;
356- end
357- else if dist = best then
358- shortPaths += path;
350+ n := 1;
351+ path := [];
352+ Continue;
353+ end;
354+
355+ if (dist = best) and (Random(Inc(n)) = 0) then
356+ Result := path.Last; //if multiple 1/n chance of replacing best
359357
360358 path := [];
361359 end;
362360
363- if shortPaths = [] then
364- Exit(destinations.NearestPoint(me));
365-
366- Result := shortPaths.Random().Last;
361+ Result := destinations.NearestPoint(me);
367362end;
368363
369364
0 commit comments