Sometimes an oxp would like to make an NPC jump to another system. However, the NPC might well be blocked by a nearby mass, since it's not always obvious how far away from the station you can safely jump, here's a way to find the minimum distance from any station in order to hopefully jump successfully at the first attempt. It's a combination of AI.plist and ship script, I'm afraid...
NB: I'm using squared distance because it's faster to calculate than distance.
Code: Select all
this.minimumJumpSquaredDistance = function(other) {
// internal Oolite constants used: K = 0.1, SCANNER_MAX_RANGE = 25600
let dist=other.mass * 0.1;
return (dist > 655360000 ? 655360000 : dist); // 25600 * 25600
}
We can now select the nearest safe jump coordinates:
Code: Select all
this.selectNearestJumpPoint = function() {
let other = system.filteredEntities(this,function(e){return e.isStation;}, this.ship)[0]; // select closest station!
let A = this.ship.position.subtract(other.position);
let min2 = this.minimumJumpSquaredDistance(other));
if (min2 > A.squaredDistanceTo(other.position)){
let dir = A.magnitude() > 0 ? A.direction() : Vector3D(1,0,0);
this.ship.savedCoordinates=other.position.add(dir.multiply(Math.sqrt(min2)));
} else
reactToAIMessage("DESIRED_RANGE_ACHIEVED");
}
On the AI side you'll have to have something like this:
Code: Select all
"JUMP_AWAY" =
{
ENTER = ("sendScriptMessage: selectNearestJumpPoint",setDestinationFromCoordinates, "setDesiredRangeTo:0.5");
"COURSE_OK" = ("setSpeedFactorTo: 1.0", performFlyToRangeFromDestination);
"DESIRED_RANGE_ACHIEVED" = (performHyperSpaceExit);
UPDATE = ("sendScriptMessage: selectNearestJumpPoint",checkCourseToDestination, "pauseAI: 10.0");
};
"NEW_WAYPOINT" =
{
ENTER = ("setSpeedFactorTo: 1.0", setDesiredRangeForWaypoint, checkCourseToDestination, "pauseAI: 2.0");
"WAYPOINT_SET" = ("setStateTo: NEW_WAYPOINT");
"COURSE_OK" = ("setStateTo: JUMP_AWAY");
"DESIRED_RANGE_ACHIEVED" = ("setStateTo:JUMP_AWAY");
};
That should get your ship to go to the nearest available jump spot, then jump. If your ship is already in the clear, it should jump away immediately...
Of course, assuming that the nearest station is the mass of a coriolis station, all you have to do is set coordinates to a random spot about 5-6 km from the station, and your ship should be good to go!
Still, these chunks of code might give an idea or two to some enterprising OXP makers, so I thought I'd share!
PS & NB: I haven't tested the code above too rigorously, so there might well be a couple of bugs lurking in there...