"use strict";

this.name = "hOopy Casino Station AI";

this.aiStarted = function() {
	var ai = new worldScripts["oolite-libPriorityAI"].PriorityAIController(this.ship);
        ai.setCommunicationsRole("station");
        ai.setParameter("oolite_friendlyRoles",["oolite-trader","oolite-bounty-hunter","oolite-scavenger","oolite-shuttle"]);

	ai.setPriorities([
		//set up the standard handlers
		{
			configuration: function() {
				var handlers = {}; // new handler object
				this.responsesAddStation(handlers); // add standard response set
				this.applyHandlers(handlers);
			}
		},
		/* Hyperspace out if things look bad */
		{
			condition: function() {
				if (this.ship.energy / this.ship.maxEnergy < 0.25)
					return true;
				else return false;
			},
			truebranch: ai.templateWitchspaceJumpAnywhere(),
			reconsider: 5
		},
		/* Fight */
		{
			preconfiguration: ai.configurationStationValidateTarget,
			condition: ai.conditionInCombat,
			behaviour: function() {
				ai.broadcastDistressMessage();
				ai.behaviourStationLaunchDefenseShips();
			},
			reconsider: 5
		},
		/* Advertise and stop for the player*/
		{
			condition: ai.conditionPlayerNearby,
			behaviour: function() {
				this.ship.desiredSpeed = 0;
				if (this.ship.alertCondition !== 3)
					this.ship.commsMessage("[hoopycasino-solicit]");
				ai.behaviourStationManageTraffic();
			},
			reconsider: 20
		},
		/* Cruise around the main station */
		{
			condition: ai.conditionHasWaypoint,
			configuration: ai.configurationSetDestinationToWaypoint,
			behaviour: ai.behaviourApproachDestination,
			reconsider: 30
		},
		{
			configuration: ai.configurationStationReduceAlertLevel,
            behaviour: function() {
				var badDestination = true;
				while (badDestination) {
					var newWayPoint = system.mainStation.position.add(Vector3D.randomDirection().multiply(50000));
					//keep the destination away from the planet.
					if (newWayPoint.distanceTo(system.mainPlanet.position) > system.mainStation.position.distanceTo(system.mainPlanet.position))
						badDestination = false;
				}
				ai.setParameter("oolite_waypoint", newWayPoint);
				ai.setParameter("oolite_waypointRange", 1000);
				ai.behaviourStationManageTraffic();
			},
			reconsider: 30
		}
	]);
}
