//CoastGuard-RescueFishermen - KSC Coast Guard: S.O.S. Reported: @/distressShipName

CONTRACT_TYPE
{


//REQUIREMENTS FOR CONTRACT TO APPEAR

	REQUIREMENT
	{
		name = CompleteContract
		type = CompleteContract

		contractType = KSP-CarrierLanding
		minCount = 1
		
	}

	
//DATA NODES TO PROCESS FOR CONTRACT USE

	//List of Coast Guard Team Members
	DATA
	{
		type = List<Kerbal>
		
		kerbalsCoastGuard = [ NewKerbal( Male , "Lt. Dan Kerman" , "Pilot") ]
		
	}

	//List of Kerbals in Distress
	DATA
	{
		type = List<Kerbal>
		
		kerbalsInDistress = [ NewKerbal(), NewKerbal() ]
	
	}	

	//Random Distress Ship Name (Based on Kerbal name generator, cool idea huh?)
	DATA
	{
		type = string
	
		distressShipName = "S.S. " + RandomKerbalName(Random()).FirstWord()
	
	}
	
	//Distress Ship Craft List
	DATA
	{
		type = List<string>
		
		distressShipCraftList = [ "ContractPacks/GAP/Assets/Sea/Attack Boat.craft" , "ContractPacks/GAP/Assets/Sea/Bayou Fishing Boat.craft" , "ContractPacks/GAP/Assets/Sea/Castaway Raft.craft" , "ContractPacks/GAP/Assets/Sea/Day Sailer.craft" , "ContractPacks/GAP/Assets/Sea/Dipper.craft" , "ContractPacks/GAP/Assets/Sea/Elcano.craft" ,	"ContractPacks/GAP/Assets/Sea/Pontoon.craft" , "ContractPacks/GAP/Assets/Sea/Powerboat.craft" , "ContractPacks/GAP/Assets/Sea/Show Boat.craft" , "ContractPacks/GAP/Assets/Sea/Speedboat.craft" , "ContractPacks/GAP/Assets/Sea/Tour Boat.craft" ]
	}
	
	//Select a Random Distress Ship Craft to Use
	DATA
	{
		type = string
		
		distressShipCraft = @/distressShipCraftList.Random()
	
	}
	
	
	//List of Possible Distress Reports
	DATA
	{
		type = List<string>
		
		distressReportData = [ "their engine has taken on water" , "they had an engine fire" , "their rudder is disabled", "their ship was hit by a rogue wave in the recent storm", "there was a fight over the last remaining snacks and in the scuffle the keys to the engine were lost overboard", "caught such a large fish that that their boat flipped over twice (the dispatcher doesn't quite believe this fish story)", "that they were attacked by what they thought was a sea kracken (but dispatch believes they forgot to top off their fuel tanks before leaving port)", "that they thought they spotted Scott Manley off the port bow, but it turned out to be nothing more than a piece of floating debris from KSC, which they attempted to haul on board, and in the process of doing so, they sliced through their engine control cables" ]

	}
	
	//Pick One Random Distress Report to Use for Contract
	DATA
	{
		type = string
		
		distressReport = @/distressReportData.Random()
		
	}
	
	//SpawnVessel Roll List Values for Capsize Determination
	DATA
	{
		type = List<int>
		
		capsizeValueList = [ 0 , 180 ]
		
	}
	
	//Random Capsize Decision
	DATA
	{
		type = int
		
		capsizeValue = @capsizeValueList.Random()
	}
	
	//sets capsize indicator, if true use element 0 in the string
	DATA
	{
		type = int
		
		capsizeLogic = @capsizeValue == 180 ? 1 : 0
	}
	
	DATA
	{
		type = List<string>
		
		capsizeStringList = [ "have capsized" , "are adrift" ]
	
	}
	
	//Sets the capsizeString for use in the Contract Description
	DATA
	{
		type = string
		
		capsizeString = @capsizeStringList.ElementAt(@capsizeLogic)
		
	}
	
	//Pick 1 Zone for Distress Location and 1 Zone for Aircraft Carrier for Use by Waypoint Generator Behavior
	DATA
	{
		type = List<int>
		
		zoneList = [ 0, 1 , 2 , 3 , 4 ].Random(2)

	}
	
	//Distress Location and Carrier Location Pulled from Waypoint Generator Behaviour
	DATA
	{
		type = double
		
		distressLat = @/SeaZones.Waypoints().ElementAt(5).Latitude()
		distressLon = @/SeaZones.Waypoints().ElementAt(5).Longitude()
		
		carrierLat = @/SeaZones.Waypoints().ElementAt(6).Latitude()
		carrierLon = @/SeaZones.Waypoints().ElementAt(6).Longitude()

	}

	
	
//CONTRACT DESCRIPTION

	name = CoastGuard-RescueFishermen-Carrier
	title = S.O.S. Reported: @/distressShipName
	group = KSC Coast Guard
	agent = KSC Coast Guard

	description = An S.O.S. was reported by the fishing vessel @/distressShipName somewhere in the waters east of KSC.\n\nThe captain reports @/distressReport, and they @/capsizeString. Send out a Search and Rescue team to save the crew and return them to an authorized Coast Guard landing area for a safe recovery. Lt. Dan Kerman from the KSC Coast Guard will be your diving officer should the need arise. You may use an airplane, helicopter, or VTOL aircraft.

	synopsis = KSC Coast Guard: Our motto is Always Ready - Kemper Keratus!

	notes = Rescue all crew members of the @/distressShipName and bring them to any KSC Coast Guard rescue station.
	
	completedMessage = KSC Coast Guard rescue mission accomplished! The crew of the @/distressShipName thank you for rescuing them from the waters off KSC. Your heroics have not gone unnoticed and your reputation has now grown a little bit more!

//Contract Limits
	maxCompletions = 0
	maxSimultaneous = 1
	weight = 10.0

	autoAccept = false
	declinable = true
	cancellable = true

	minExpiry = 1.0
	maxExpiry = 1.0
	deadline = 1

//Contract Reward Modifiers
	prestige = Trivial
	targetBody = Kerbin
	
//Contract Rewards
  	advanceFunds = 5000.0
	rewardFunds = 5000.0
  	rewardReputation = 25.0
 	rewardScience = 0.0
	
//Contract Penalties
  	failureFunds = 10000.0
	failureReputation = 50.0
	

//BEHAVIOURS TO DO WHEN CREATING CONTRACT
	BEHAVIOUR
	{
		name = SpawnPassengers
		type = SpawnPassengers
		
		kerbal = @/kerbalsCoastGuard.ElementAt(0)
		kerbalType = Crew
		
	}

	BEHAVIOUR
	{
		name = SpawnKerbal
		type = SpawnKerbal

		KERBAL
		{
			kerbal = @/kerbalsInDistress.ElementAt(0)
			kerbalType = Unowned
			owned = false
			addToRoster = false
			
			lat = @/distressLat - 0.00004
			lon = @/distressLon - 0.00003
			alt = 0
			heading = 350
			
		}

		KERBAL
		{
			kerbal = @/kerbalsInDistress.ElementAt(1)
			kerbalType = Unowned
			owned = false
			addToRoster = false
			
			lat = @/distressLat + 0.00004
			lon = @/distressLon + 0.00003
			alt = 0
			heading = 270
			
		}
		
	}
	
//Spawn Vessel in Distress
	BEHAVIOUR
	{
		name = SpawnVessel
		type = SpawnVessel
		deferVesselCreation = false

		VESSEL
		{
			name = @/distressShipName
			craftURL = @/distressShipCraft
			vesselType = Ship
			owned = false
			targetBody = Kerbin
			
			lat = @/distressLat
			lon = @/distressLon
			alt = -5
			heading = 133.5
			pitch = 90
			roll = @/capsizeValue
			
		}
		
	}

//Spawn Aircraft Carrier
	BEHAVIOUR
	{
		name = SpawnVessel
		type = SpawnVessel
		deferVesselCreation = false

		VESSEL
		{
			name = KSS Rix
			craftURL = ContractPacks/GAP/Assets/Sea/Inigma Class Carrier.craft
			vesselType = Ship
			owned = false
			targetBody = Kerbin
			
			lat = @/carrierLat
			lon = @/carrierLon
			alt = -1
			heading = 90
			
		}
		
	}
	
//Remove all SpawnVessels when done
	BEHAVIOUR
	{
		name = DestroyVessel
		type = DestroyVessel
		
		vessel = @/distressShipName
		vessel = KSS Rix
		
		onState = CONTRACT_COMPLETED
		
	}

//Generate Sea Zones and Define Random Waypoints
	BEHAVIOUR
	{
		name = SeaZones
		type = WaypointGenerator
		
		//Rescue Areas
		
		PQS_CITY
		{
			name = KSC Sea North Central Waypoint
			
			targetBody = Kerbin
			pqsCity = IslandAirfield 
			pqsOffset = -1847.25488069029, -20058.7598335702, -453.396403818988
			
			hidden = true
			
		}		

		PQS_CITY
		{
			name = KSC Sea Central Central Waypoint
			
			targetBody = Kerbin
			pqsCity = IslandAirfield 
			pqsOffset = 20550.8508273184, 1062.93579726917, -468.244493385362
			
			hidden = true
			
		}
		
		PQS_CITY
		{
			name = KSC Sea South Central Waypoint
			
			targetBody = Kerbin
			pqsCity = IslandAirfield 
			pqsOffset = 29140.5802921804, 15569.5873430977, -1025.49583004066
			
			hidden = true
			
		}
		
		PQS_CITY
		{
			name = East Islands Sea Central Waypoint
			
			targetBody = Kerbin
			pqsCity = IslandAirfield 
			pqsOffset = -13479.0987451144, 22129.2856859656, -674.933362233103
			
			hidden = true
			
		}
		
		PQS_CITY
		{
			name = South Islands Sea Central Waypoint
			
			targetBody = Kerbin
			pqsCity = IslandAirfield 
			pqsOffset = 4175.6529485127, 49157.8040008561, -2146.85443912575
			
			hidden = true
			
		}
		
		//Distress Ship Random Waypoint Within Random Sea Zone
	    RANDOM_WAYPOINT_NEAR
		{
		
			name = S.O.S Search Area
			icon = marker
			
			targetBody = Kerbin
			nearIndex = @/zoneList.ElementAt(0)
			maxDistance = 7500.0
			altitude = 0.0
		
		}
		
		//Aircraft Carrier Waypoint
		RANDOM_WAYPOINT_NEAR
		{
			name = Aircraft Carrier
			icon = marker
			
			targetBody = Kerbin
			nearIndex = @/zoneList.ElementAt(1)
			maxDistance = 7500.0
			altitude = 6
			hidden = true
		
		}
		
	}
	
	BEHAVIOUR
	{
		name = LandingZones
		type = WaypointGenerator
		
		//Island Airfield Waypoint
		PQS_CITY
		{
			name = KSC Island Airfield
			icon = marker
			
			targetBody = Kerbin
			pqsCity = IslandAirfield
			pqsOffset = -44.0509605753032, -247.057216484056, 18.0797328829649
			altitude = 24
			hidden = true
		
		}

	}

	
//PARAMETERS FOR CONTRACT COMPLETION

//Craft definition
	PARAMETER
	{
		name = VesselParameterGroup
		type = VesselParameterGroup
		title = Your craft must
		
		define = craft
		dissassociateVesselsOnContractCompletion = true

		PARAMETER
		{
			name = HasCrew
			type = HasCrew
			title = have a Kerbal in command
			
			minCrew = 1
		
			disableOnStateChange = false
			hideChildren = true

		}
		
		PARAMETER
		{
			name = HasCrewCapacity
			type = HasCrewCapacity
			title = have at least 2 passenger seats
			
			minCapacity = 3
			
			disableOnStateChange = false
			
		}
		
		disableOnStateChange = false

	}

//Passenger Manifest - Load Passengers
	PARAMETER
	{
		name = All
		type = All
		
		title = load your Coast Guard rescue officer
		
		PARAMETER
		{
			name = HasPassengers
			type = HasPassengers
			title = @kerbal.First()
	
			ITERATOR
			{
				type = Kerbal
				kerbal = @/kerbalsCoastGuard
				
			}
			
			hideChildren = true
			
		}

	}

//Contract Goals
	PARAMETER
	{
		name = VisitWaypoint
		type = VisitWaypoint
		title = navigate to S.O.S Waypoint
		
		index = 5
		distance = 500
		hideOnCompletion = true
		
		disableOnStateChange = true
		hideChildren = true
		
	}

	PARAMETER
	{
		name = HasCrew
		type = HasCrew
		title =  rescue the crew of the @/distressShipName
		
		kerbal = @/kerbalsInDistress

		completeInSequence = true
		disableOnStateChange = false
	
	}

//Recovery Parameter - Landing
	PARAMETER
	{
		name = Any
		type = Any
		title = and then land
		
		PARAMETER
		{
			name = Any
			type = Any
			title = at either
			
			PARAMETER
			{
				name = VesselParameterGroup
				type = VesselParameterGroup
				title = the KSC Island Airfield
				
				vessel = craft
				
				REQUIREMENT
				{
					name = CompleteContract
					type = CompleteContract

					contractType = KSP-IslandPrep
					minCount = 1
					
				}

				PARAMETER
				{
					name = VisitWaypoint
					type = VisitWaypoint
					index = 7

					distance = 800
					hideOnCompletion = true
					
					disableOnStateChange = false
					hideChildren = true
					
				}
				
				PARAMETER
				{			            
					name = ReachState   
					type = ReachState

					situation = LANDED
					maxSpeed = 0.0
					
					disableOnStateChange = false
					hideChildren = true
					
				}

				disableOnStateChange = false
				hideChildren = true

			}
			
			PARAMETER
			{
				name = VesselParameterGroup
				type = VesselParameterGroup
				title = the KSS Rix
				
				vessel = craft
				
				REQUIREMENT
				{
					name = CompleteContract
					type = CompleteContract

					contractType = KSP-CarrierLanding
					minCount = 1
					
				}
			
				PARAMETER
				{
					name = VisitWaypoint
					type = VisitWaypoint
					index = 6

					distance = 55
					hideOnCompletion = false
					
					completeInSequence = true
					disableOnStateChange = false
					hideChildren = true
					
				}
				
				PARAMETER
				{			            
					name = ReachState   
					type = ReachState

					situation = LANDED
					maxSpeed = 5.0
					minAltitude = 3
					
					completeInSequence = true
					disableOnStateChange = false
					hideChildren = true
					
				}

				disableOnStateChange = true
				hideChildren = true	

			}
			
			PARAMETER
			{
				name = VesselParameterGroup
				type = VesselParameterGroup
				title = the KSC Runway
				
				vessel = craft

			PARAMETER
				{
					name = ReachState
					type = ReachState
					
					targetBody = Kerbin
					biome = Runway
					situation = LANDED
					maxSpeed = 0.0

					disableOnStateChange = false
					hideChildren = true

				}
				
				disableOnStateChange = false
				hideChildren = true

			}
			
			PARAMETER
			{
				name = VesselParameterGroup
				type = VesselParameterGroup
				title = the Spaceplane Hangar Air Terminal
				
				vessel = craft

			PARAMETER
				{
					name = ReachState
					type = ReachState
					
					targetBody = Kerbin
					biome = SPH
					situation = LANDED
					maxSpeed = 0.0

					disableOnStateChange = false
					hideChildren = true

				}
				
				disableOnStateChange = false
				hideChildren = true

			}
			
			PARAMETER
			{
				name = VesselParameterGroup
				type = VesselParameterGroup
				title = the Vehicle Assembly Building Helipad
				
				vessel = craft

			PARAMETER
				{
					name = ReachState
					type = ReachState
					
					targetBody = Kerbin
					biome = VAB Main Building
					situation = LANDED
					maxSpeed = 0.0

					disableOnStateChange = false
					hideChildren = true

				}
				
				disableOnStateChange = false
				hideChildren = true		

			}
			
			PARAMETER
			{
				name = VesselParameterGroup
				type = VesselParameterGroup
				title = or the Administration Building Helipad
				
				vessel = craft

				PARAMETER
				{
					name = ReachState
					type = ReachState

					targetBody = Kerbin
					biome = Administration	
					situation = LANDED
					maxSpeed = 0.0
					
					disableOnStateChange = false
					hideChildren = true
					
				}
				
				disableOnStateChange = false
				hideChildren = true	

			}
			
			completeInSequence = true
			disableOnStateChange = false
		
		}
		
		completeInSequence = true
		disableOnStateChange = false

	}

//Recovery Parameter - Craft & Kerbal Safety Check
	PARAMETER
	{
		name = All
		type = All
		title = safely
		
		completeInSequence = true
		disableOnStateChange = true
		
		PARAMETER
		{
			name = VesselNotDestroyed
			type = VesselNotDestroyed
			title = without destroying your craft
			
			vessel = craft

		}
		
		PARAMETER
		{
			name = KerbalDeaths
			type = KerbalDeaths
			title = or killing anyone
			
			countMax = 1

		}
		
	}
	
//Recovery Parameter - Recover Kerbals
	PARAMETER
	{
		name = All
		type = All
		title = and recover
		
		PARAMETER
		{
			name = RecoverKerbal
			type = RecoverKerbal
			title = @kerbal.First()
	
			ITERATOR
			{
				type = Kerbal
				kerbal = @/kerbalsCoastGuard
				
			}
			
			hideChildren = true
			
		}
		
		PARAMETER
		{
			name = RecoverKerbal
			type = RecoverKerbal
			title = @kerbal.First()
	
			ITERATOR
			{
				type = Kerbal
				kerbal = @/kerbalsInDistress
				
			}
			
			hideChildren = true
			
		}
		
	}
	
}