Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // converts seconds into a string of years, days, minutes and seconds, showing only the values that exist (no "0y, 0d")
- function FormatTime {
- parameter seconds.
- set days to 0.
- set hours to 0.
- set minutes to 0.
- set formattedTime to "".
- if seconds >= 86400 {
- set days to floor(seconds / 86400).
- set seconds to seconds - (days * 86400).
- set formattedTime to days + "d, ".
- }.
- if seconds >= 3600 {
- set hours to floor(seconds / 3600).
- set seconds to seconds - (hours * 3600).
- set formattedTime to formattedTime + hours + "h ".
- }.
- if seconds >= 60 {
- set minutes to floor(seconds / 60).
- set seconds to seconds - (minutes * 60).
- set formattedTime to formattedTime + minutes + "m ".
- }.
- set formattedTime to formattedTime + floor(seconds) + "s".
- return formattedTime.
- }.
- // from the KSLib - https://github.com/KSP-KOS/KSLib
- function circle_bearing {
- parameter
- p1, //...this point...
- p2. //...to this point.
- return mod(360+arctan2(sin(p2:lng-p1:lng)*cos(p2:lat),cos(p1:lat)*sin(p2:lat)-sin(p1:lat)*cos(p2:lat)*cos(p2:lng-p1:lng)),360).
- }.
- // from the KSLib - https://github.com/KSP-KOS/KSLib
- function circle_distance {
- parameter
- p1, //...this point...
- p2, //...to this point...
- radius. //...around a body of this radius. (note: if you are flying you may want to use ship:body:radius + altitude).
- local A is sin((p1:lat-p2:lat)/2)^2 + cos(p1:lat)*cos(p2:lat)*sin((p1:lng-p2:lng)/2)^2.
- return radius*constant():PI*arctan2(sqrt(A),sqrt(1-A))/90.
- }.
- // Action groups bounded to a script trigger via Action Groups Extended
- on AG11 {
- set waypointShift to 1.
- preserve.
- }.
- on AG12 {
- set waypointShift to -1.
- preserve.
- }.
- on AG13 {
- set running to false.
- }.
- // initialize our data
- set waypointLatLng to list(latlng(82.6172, -158.5547), // ice caps
- latlng(-16.1719, 42.3633), // badlands
- latlng(2.1567, 26.6113), // lushlands
- latlng(-0.1025, -73.5753)). // KSC
- set waypointText to list("Ice Caps", "Badlands", "Lushlands", "KSC").
- set waypoint to 0.
- set running to true.
- set waypointShift to 0.
- set update to time:seconds.
- set liquidFuel to 7.
- set dstTraveled to 0.
- // print out the initial information for us to update as we go along
- clearscreen.
- print "Current Waypoint: " + waypointText[waypoint].
- set bearing to circle_bearing(ship:geoposition, waypointLatLng[waypoint]).
- print "Bearing: " + bearing + "°".
- set distance to circle_distance(ship:geoposition, waypointLatlng[waypoint], ship:body:radius + altitude).
- print "Distance: " + distance + "m".
- print "ETA:".
- // create the CSV header
- log "UT,WaypointName,WaypointLat,WaypointLng,WaypointDistance,PlaneLat,PlaneLng,Heading,Speed,ASL,Fuel,DstTraveled" to PlaneLog.
- // loop until program exit is triggered by action group
- until not running {
- // update our distance traveled every second based on our speed (which is in m/s)
- set dstTraveled to dstTraveled + ship:surfacespeed.
- // update information text with new data
- print "Current Waypoint: " + waypointText[waypoint] + " " at (0,0).
- set bearing to circle_bearing(ship:geoposition, waypointLatLng[waypoint]).
- print "Bearing: " + bearing + "° " at (0,1).
- set distance to circle_distance(ship:geoposition, waypointLatlng[waypoint], ship:body:radius + altitude).
- print "Distance: " + distance + "m " at (0,2).
- // don't calculate ETA if we're not moving
- if ship:airspeed > 1 {
- print "ETA: " + FormatTime(distance/ship:airspeed) + " " at (0,3).
- } else {
- print "ETA: N/A " at (0,3).
- }
- // has the user selected a new waypoint?
- if waypointShift <> 0 {
- set newWaypoint to waypoint + waypointShift.
- // if the new waypoint takes us out of bounds, reset to the current waypoint
- // otherwise update the screen with new waypoint data
- if newWaypoint = waypointText:length or newWaypoint < 0 {
- set newWaypoint to Waypoint.
- } else {
- clearscreen.
- set waypoint to newWaypoint.
- print "Current Waypoint: " + waypointText[waypoint].
- set bearing to circle_bearing(ship:geoposition, waypointLatLng[waypoint]).
- print "Bearing: " + bearing + "°".
- set distance to circle_distance(ship:geoposition, waypointLatlng[waypoint], ship:body:radius + altitude).
- print "Distance: " + distance + "m".
- }.
- // we don't want to keep shifting waypoints...
- set waypointShift to 0.
- }.
- // only log the plane data every minute
- if time:seconds - update > 60 {
- // this code for determining the heading of the aircraft only works if no target is selected!!
- if ship:bearing < 0 {
- set hdg to abs(ship:bearing).
- } else {
- set hdg to (180 - ship:bearing) + 180.
- }
- // log all the data
- log round(time:seconds) + "," + waypointText[waypoint] + "," + waypointLatLng[waypoint]:lat + "," +
- waypointLatLng[waypoint]:lng + "," + distance + "," + ship:latitude + "," + ship:longitude + "," + hdg + "," + ship:airspeed +
- "," + ship:altitude + "," + round(100*ship:resources[liquidFuel]:amount/ship:resources[liquidFuel]:capacity)
- + "," + dstTraveled to PlaneLog.
- copy PlaneLog to 0.
- // wait for the next 60s
- set update to time:seconds.
- }.
- // update once per second
- wait 1.
- }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement