JSAI: shuttle AI

This commit is contained in:
cim 2013-07-21 23:25:41 +01:00
parent 228344c540
commit 968eace6db
6 changed files with 198 additions and 15 deletions

View File

@ -0,0 +1,88 @@
/*
shuttleAI.js
Priority-based AI for in-system shuttles
Oolite
Copyright © 2004-2013 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
"use strict";
this.name = "Oolite Shuttle AI";
this.version = "1.79";
this.aiStarted = function() {
var ai = new worldScripts["oolite-libPriorityAI"].AILib(this.ship);
ai.setParameter("oolite_flag_sendsDistressCalls",true);
ai.setParameter("oolite_flag_allowPlanetaryLanding",true);
ai.setCommunication("oolite_landingOnPlanet","Commencing final approach to %N spaceport");
ai.setPriorities([
{
condition: ai.conditionInCombat,
behaviour: ai.behaviourFleeCombat
},
{
condition: ai.conditionHasSelectedStation,
truebranch: [
{
condition: ai.conditionSelectedStationNearby,
configuration: ai.configurationSetSelectedStationForDocking,
behaviour: ai.behaviourDockWithStation,
reconsider: 30
},
{
configuration: ai.configurationSetDestinationToSelectedStation,
behaviour: ai.behaviourApproachDestination,
reconsider: 30
}
]
},
{
condition: ai.conditionHasSelectedPlanet,
truebranch: [
{
preconfiguration: ai.configurationSetDestinationToSelectedPlanet,
condition: ai.conditionNearDestination,
behaviour: ai.behaviourLandOnPlanet
},
{
behaviour: ai.behaviourApproachDestination,
reconsider: 30
}
]
},
/* TODO: need to try to hitchhike out! */
{
condition: ai.conditionInInterstellarSpace,
configuration: ai.configurationSetDestinationToWitchpoint,
behaviour: ai.behaviourApproachDestination,
reconsider: 30
},
{
configuration: ai.configurationSelectShuttleDestination,
behaviour: ai.behaviourApproachDestination,
reconsider: 1
}
]);
}

View File

@ -24,7 +24,7 @@
"police" = "policeAI.js";
"trader" = "traderAI.js";
"scavenger" = "scavengerAI.plist";
"shuttle" = "fallingShuttleAI.plist";
"shuttle" = "shuttleAI.js";
"sunskim-trader" = "traderAI.js";
"wingman" = "policeAI.js";
}

View File

@ -520,7 +520,7 @@ this.AILib = function(ship)
this.conditionMothershipInCombat = function()
{
if (this.ship.group && this.ship.group.leader != this.ship && this.ship.group.leader.escortGroup.containsShip(this.ship))
if (this.ship.group && this.ship.group.leader && this.ship.group.leader != this.ship && this.ship.group.leader.escortGroup.containsShip(this.ship))
{
var leader = this.ship.group.leader;
if (leader.position.distanceTo(this.ship) > this.ship.scannerRange)
@ -710,7 +710,7 @@ this.AILib = function(ship)
this.conditionHasSelectedStation = function()
{
var station = this.getParameter("oolite_selectedStation");
if (station && !station.isValid)
if (station && (!station.isValid || !station.isStation))
{
this.setParameter("oolite_selectedStation",null);
return false;
@ -718,6 +718,18 @@ this.AILib = function(ship)
return station != null;
}
this.conditionHasSelectedPlanet = function()
{
var planet = this.getParameter("oolite_selectedPlanet");
if (planet && (!planet.isValid || !planet.isPlanet))
{
this.setParameter("oolite_selectedPlanet",null);
return false;
}
return planet != null;
}
this.conditionInInterstellarSpace = function()
{
return system.isInterstellarSpace;
@ -1296,6 +1308,9 @@ this.AILib = function(ship)
if (blocker)
{
if (blocker.isPlanet || blocker.isSun)
{
// the selected planet can't block
if (blocker.isSun || this.getParameter("oolite_selectedPlanet") != blocker)
{
if (this.ship.position.distanceTo(blocker) < blocker.radius * 3)
{
@ -1306,7 +1321,7 @@ this.AILib = function(ship)
waypoints.push(this.ship.getSafeCourseToDestination());
this.ship.destination = waypoints[waypoints.length-1];
this.ship.desiredRange = 1000;
log(this.ship,"Avoiding blocker "+blocker);
}
}
}
else if (blocker.isShip)
@ -1323,7 +1338,6 @@ this.AILib = function(ship)
waypoints.push(this.ship.getSafeCourseToDestination());
this.ship.destination = waypoints[waypoints.length-1];
this.ship.desiredRange = 1000;
log(this.ship,"Avoiding blocker "+blocker);
}
}
}
@ -1775,6 +1789,15 @@ this.AILib = function(ship)
this.ship.performTumble();
}
this.behaviourLandOnPlanet = function()
{
this.ship.desiredSpeed = this.ship.maxSpeed / 4;
this.ship.performLandOnPlanet();
this.ship.AIScriptWakeTime = 0; // cancel reconsiderations
this.setUpHandlers({}); // cancel interruptions
this.communicate("oolite_landingOnPlanet");
}
/* ****************** Configuration functions ************** */
/* Configurations. Configurations are set up actions for a behaviour
@ -1939,6 +1962,55 @@ this.AILib = function(ship)
this.ship.target = this.getParameter("oolite_scanResultSpecific");
}
this.configurationSelectShuttleDestination = function()
{
var possibles = system.planets.concat(system.stations);
var destinations1 = [];
var destinations2 = [];
for (var i = 0; i < possibles.length ; i++)
{
var possible = possibles[i];
// travel at least a little way
var distance = possible.position.distanceTo(this.ship);
if (distance > possible.collisionRadius + 10000)
{
// must be friendly destination and not moving too fast
if (possible.isPlanet || this.friendlyStation(possible) || possible.maxSpeed > this.ship.maxSpeed / 5)
{
if (distance > system.mainPlanet.radius * 5)
{
destinations2.push(possible);
}
else
{
destinations1.push(possible);
}
}
}
}
// no nearby destinations
if (destinations1.length == 0)
{
destinations1 = destinations2;
}
// no destinations
if (destinations1.length == 0)
{
return;
}
var destination = destinations1[Math.floor(Math.random()*destinations1.length)];
if (destination.isPlanet)
{
this.setParameter("oolite_selectedPlanet",destination);
this.setParameter("oolite_selectedStation",null);
}
else
{
this.setParameter("oolite_selectedStation",destination);
this.setParameter("oolite_selectedPlanet",null);
}
}
this.configurationSelectRandomTradeStation = function()
{
var stations = system.stations;
@ -2011,6 +2083,18 @@ this.AILib = function(ship)
}
}
this.configurationSetDestinationToSelectedPlanet = function()
{
var planet = this.getParameter("oolite_selectedPlanet");
if (planet)
{
this.ship.destination = planet.position;
this.ship.desiredRange = planet.radius+100;
this.ship.desiredSpeed = this.cruiseSpeed();
}
}
this.configurationSetDestinationToPirateLurk = function()
{
var lurk = this.getParameter("oolite_pirateLurk");
@ -2411,9 +2495,20 @@ this.AILib = function(ship)
}
}
handlers.approachingPlanetSurface = function()
{
if (this.getParameter("oolite_flag_allowPlanetaryLanding"))
{
this.ship.desiredSpeed = this.ship.maxSpeed / 4;
this.ship.performLandOnPlanet();
this.ship.AIScriptWakeTime = 0; // cancel reconsiderations
this.setUpHandlers({}); // cancel interruptions
this.communicate("oolite_landingOnPlanet");
}
else
{
this.reconsiderNow();
}
}
handlers.shipLaunchedFromStation = function(station)
{
// clear the station

View File

@ -568,7 +568,7 @@ static OOColor *ColorWithHSBColor(Vector c)
[shuttle_ship setScanClass: CLASS_NEUTRAL];
[shuttle_ship setCargoFlag:CARGO_FLAG_FULL_PLENTIFUL];
[shuttle_ship switchAITo:@"risingShuttleAI.plist"];
[shuttle_ship switchAITo:@"shuttleAI.js"];
[UNIVERSE addEntity:shuttle_ship]; // STATUS_IN_FLIGHT, AI state GLOBAL
_shuttlesOnGround--;
_lastLaunchTime = [UNIVERSE getTime];

View File

@ -1194,7 +1194,7 @@ static const BaseFace kTexturedFaces[][3] =
[shuttle_ship setScanClass: CLASS_NEUTRAL];
[shuttle_ship setCargoFlag:CARGO_FLAG_FULL_PLENTIFUL];
[shuttle_ship switchAITo:@"risingShuttleAI.plist"];
[shuttle_ship switchAITo:@"shuttleAI.js"];
[UNIVERSE addEntity:shuttle_ship];
[shuttle_ship release];

View File

@ -1860,7 +1860,7 @@ NSDictionary *OOMakeDockingInstructions(StationEntity *station, HPVector coords,
docked_shuttles--;
[shuttle_ship setScanClass: CLASS_NEUTRAL];
[shuttle_ship setCargoFlag:CARGO_FLAG_FULL_SCARCE];
[shuttle_ship switchAITo:@"fallingShuttleAI.plist"];
[shuttle_ship switchAITo:@"shuttleAI.js"];
[self addShipToLaunchQueue:shuttle_ship withPriority:NO];
[shuttle_ship autorelease];