Page 1 of 1

AI help

Posted: Wed Dec 28, 2016 4:16 pm
by DGill
I have a stranded liner - I set fuel = 0

I have NPC deliver fuel (see photo) using AI:

GLOBAL = {
ENTER = ("setStateTo: WAIT_FOR_PLAYER");
EXIT = ();
UPDATE = ();
};


"WAIT_FOR_PLAYER" = {
ENTER = ("setStateTo: FIND_STATION");
NOTHING_FOUND = ("setStateTo: HUNT_FOR_STATION");
EXIT = ();
UPDATE = ("scanForNearestShipWithRole: patrollinerwreck");
};

"FIND_STATION" = {
ENTER = ("scanForNearestShipWithRole: patrollinerwreck");
TARGET_FOUND = (setTargetToFoundTarget, "setAITo: dockingAI.plist");
NOTHING_FOUND = ("setStateTo: HUNT_FOR_STATION");
"RESTARTED" = ("scanForNearestShipWithRole: patrollinerwreck");
};

"HUNT_FOR_STATION" = {
ENTER = ("setSpeedFactorTo: 0.5", "targetFirstBeaconWithCode: Marooned StarLiner");
TARGET_FOUND = (setTargetToFoundTarget, "setStateTo: APPROACH_STATION");
NOTHING_FOUND = (setTargetToNearestStation, "setStateTo: APPROACH_STATION");
"RESTARTED" = ("setSpeedFactorTo: 0.5", "targetFirstBeaconWithCode: Marooned StarLiner");
};

"APPROACH_STATION" = {
ENTER = ("setSpeedFactorTo: 0.5", setDestinationToTarget, "setDesiredRangeTo: 10000");
"DESIRED_RANGE_ACHIEVED" = (dockEscorts, "setAITo: dockingAI.plist");
UPDATE = ("setDesiredRangeTo: 10000", "pauseAI: 5.0");
"RESTARTED" = ("setSpeedFactorTo: 0.5", "setStateTo: FIND_STATION");
};

How do I detect that the NPC has docked and assign fuel to the liner?

Image

Re: AI help

Posted: Wed Dec 28, 2016 6:20 pm
by Norby
Use the shipDockedWithStation event handler within a ship script attached to the NPC ship. There are more ways to do this.

1. You can set the script in shipdata-plist if this is a special ship in your addon. It is simple but unusable for non-specific ships.

2. You can replace the actual ship script anytime using ship.setScript("yourshipscript.js") if you are sure that nothing in the old script what you lose with a replace. There is a high risk due to other addons can store values here, so you should use the following instead.

3. You can inject code into the current ship script, replacing the handler only. You should save the original handler and call it at the end of your function:

Code: Select all

npcship.script.myOldShipDockedWithStation = npcship.script.shipDockedWithStation;
npcship.script.shipDockedWithStation = function(station) {
     // Your code here
     if(this.myOldShipDockedWithStation) this.myOldShipDockedWithStation(station);
}
Before this you must set npcship to that ship, for example after you spawned that ship in a script.
Alternatively you can place this code within a shipSpawned event handler in a worldScript, as you can see in this example.

Re: AI help

Posted: Wed Dec 28, 2016 7:34 pm
by DGill
Thanks for the advice - I'll give it a try.