AI help

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: another_commander, winston

Post Reply
DGill
---- E L I T E ----
---- E L I T E ----
Posts: 271
Joined: Thu Jan 01, 2009 9:45 am

AI help

Post 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
User avatar
Norby
---- E L I T E ----
---- E L I T E ----
Posts: 2577
Joined: Mon May 20, 2013 9:53 pm
Location: Budapest, Hungary (Mainly Agricultural Democracy, TL10)
Contact:

Re: AI help

Post 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.
DGill
---- E L I T E ----
---- E L I T E ----
Posts: 271
Joined: Thu Jan 01, 2009 9:45 am

Re: AI help

Post by DGill »

Thanks for the advice - I'll give it a try.
Post Reply