Movement along a linear route starting from the closest to us point

This function allows you to continue movement along a predetermined route from any point, i.e. find yourself on this route even if we are somewhere in the middle.

// function of movement on the specified route
// path - array view [x1,y1,z1, x2,y2,z2, ...]
function PathMoveTo(path: array of integer): boolean;                
var i, start: integer;
begin
  result:= false;
  if (Length(path) >= 3) and (Length(path) mod 3 = 0) then begin   // first we check that the given route is set correctly   
    start:= 0;                                                     // set the index of the nearest point by default - 0
    for i:= 0 to Length(path) do begin                             // go over the array of coordinates
      if (i mod 3 <> 0) then continue;                             // work only with every third index (we are looking for X coordinates)
      // if the distance to the current point being checked is < distance to the point we marked as the starting point in the route, then
      // remember the current point as a new starting point
      if (User.DistTo(path[i], path[i+1], path[i+2]) < User.DistTo(path[start], path[start+1], path[start+2])) then start:= i;
    end;
  end else exit;                                                   // if the coordinate array is incorrect, then immediately exit
  
  for i:= start to Length(path)-1 do begin                         // now we are going over the route from the point closest to us and to the end
    if (i mod 3 <> 0) then continue;                               // work only with every third index (we are looking for X coordinates)
    if (User.Dead) then exit;                                      // if we are dead, stop the function
    result:= Engine.MoveTo(path[i], path[i+1], path[i+2]);         // move to the coordinates of the next point of the route
  end;
end;

var
  path_from_GK_to_NPC: array of integer = [108869, -173711, -579,  108592, -173446, -579,  108211, -173558, -579];

begin
  // in the script, call the function either by explicitly supplying an array of coordinates as an argument:
  PathMoveTo([108869, -173711, -579,  108592, -173446, -579,  108211, -173558, -579,  107717, -173864, -488,  107704, -174198, -430,  108325, -174110, -436]);

  // or using a previously declared variable:
  PathMoveTo(path_from_GK_to_NPC);
end.