Pathing logic help

https://playcanvas.com/editor/scene/989888

Run
Click on Hydro on the top of the screen

Script source: “shipPath.js”

Can someone help with the logic here? i’ve been going at it for hours and hours but i cant seem to grasp the right logic… i just need it to go via a path pattern as in the image
( Imgur: The magic of the Internet )

Right now it just goes in the correct pattern, but at times it just goes out of order…