Jump to content

kOS Scriptable Autopilot System 0.9


KevinLaity

Recommended Posts

for (i = 0; i< ships.count(); i++)

if ( distanceTo(ship) < 1000) {

moveHeadTo(ship);

openDocks();

break;

}

set target to "shipname".

if target:distance < 1000 {

set direction to target:heading.

wait 5.

lock throttle to 1.

}

That probably wont work without some additional code, but since only I spent under 5 minutes glancing over the readme and typing that up, it might not work even with more work, I take no responsibility for typos, incorrect variable names or silly code, YMMV. :rolleyes:

If you intend to dock, that can be a bit tricker, since I don't think there is RCS translation control in kOS yet and afaik it isn't possible to target parts(e.g. docking ports) of a ship yet.

Link to comment
Share on other sites

for this i should enumerate ships.

list ships gets them, how to enumerate?

for (i = 0; i< ships.count(); i++)

if ( distanceTo(ship) < 1000) {

moveHeadTo(ship);

openDocks();

break;

}

i was reading readme and faq.

and find all without enumeration ("for" cycle)

there is " until " cycle, but how can i select SHIP[0].NAME , SHIP[1].NAME ?

how can i get SHIP.SIZE() ?

i find my ships by name of them, for example.

That is not KerboScript, that is for sure :) I did a little testing for you and this works:


set ships1 to 1.
set numberofships1 to 3.
until ships1 > numberofships1 {
set target to "ship" + ships1.
print target + ships1 + " is " + target:distance + " meters away.".
set ships1 to ships1 + 1.
}.

Of course you have to name your ships ship1, ship2 and ship3. I retyped this code because my rig is temporarily without internet, so beware of small mistakes, but it does work. A simple compare could do the rest and get you the ship closest to you, or within a 1 km range. Simple navigation scripts can be found in Kevin's example video's.

You could even differentiate between different vehicle groups by switching making the "ship" string variable to get the closest aircraft, probe or whatever.

As soon as the list command is made to yield usable variables this will not be neccesary anymore, but for now this works.

Edited by Camacha
Link to comment
Share on other sites

I use to use
lock steering to up + r(0,45,90)

or something like that I forget exactly how i used it but lately have just been using ...heading 90 by 90. How would you use it in the way you mentioned? For the most part though I don't really have an issue with what I'm doing now, though I could maybe see it being a problem if im flying a plane or something else.

I am not sure what you mean. You can simply use the command lock steering to heading (90, 45, 270). to get for example a rocket to head east, up 45 degrees and do a half rotation off the launchpad. It is half a rotation and not three quarters because the rockets usually start off 90 degrees rotated.

lock steering to heading (90, 45, 270) doest essentially the same as lock steering to heading 90 by 90, but ads a information about the rotation. It behaves the same in all other ways.

Link to comment
Share on other sites

set ships1 to 1.

set numberofships1 to 3.

until ships1 > numberofships1 {

set target to "ship" + ships1.

print target + ships1 + " is " + target:distance + " meters away.".

set ships1 to ships1 + 1.

}.

guys.

i dont know the name of the ship friend ship in-coding-time.

and its name not like "SHIP0" ... "SHIPN", its like "my super unknown ship"

i know name only of my ships.

so how to get total universe ships count?

and how to enumerate them?

that what you wrote is half of my task) but thanx for it!!!

Link to comment
Share on other sites

guys.

i dont know the name of the ship friend ship in-coding-time.

and its name not like "SHIP0" ... "SHIPN", its like "my super unknown ship"

i know name only of my ships.

so how to get total universe ships count?

and how to enumerate them?

I don't think it is possible at the moment. Maybe if you would have kOS open the persistance file you could work something out, but that is a pretty shaky strategy and I am not sure kOS is capable of accessing external files like that. At the moment I do not see a way without a usable list command.

Link to comment
Share on other sites

I am not sure what you mean. You can simply use the command lock steering to heading (90, 45, 270). to get for example a rocket to head east, up 45 degrees and do a half rotation off the launchpad. It is half a rotation and not three quarters because the rockets usually start off 90 degrees rotated.

lock steering to heading (90, 45, 270) doest essentially the same as lock steering to heading 90 by 90, but ads a information about the rotation. It behaves the same in all other ways.

I didn't even really know that heading (?,?,?) was really a thing. I only knew it could heading ? by ? I'll have to give it a try sometime.

Link to comment
Share on other sites

I think you're looking for MechJeb.

Haha.. nice ;)

Just because kOS doesn't have the ability to "move towards nearest ship"(yet) doesn't make it useless. It can do a lot more than just that. Some people (Steven Madding) have made auto landing scripts. I'm writing (as are others I'm sure) scripts to launch kerbosyncronus "komm" sats into orbit. Others are flying planes with it. I'd hardly call that useless being as it is missing one ability.

Also, kOS currently doesn't have docking ability just yet anyway. Though I imagine if you know the exact position of a ship you could maybe work out a way to get close enough to dock.

As others were saying I would imagine you COULD make a "list" with a bunch of if statements that check distances to known target ships, but it sounds like you want to be able to target any close ship. Is this for docking or for targeting weapons fire? The more I think about it the more it sounds like targeting weapons.

Edited by Sma
Link to comment
Share on other sites

Is there a way to get mission time to display in the "clock" format like session time?

Session time is mission time, or time from vehicle load. Do you mean a clock format like universal time? There does not seem to be a function that does that, but converting the seconds to a clock is not hard:

//kOS code by Camacha. Released under the BSD license. Only for non-commerial use and free of charge distribution.
set seconds1 to sessiontime.
set hours1 to floor(seconds1/3600).
if hours1 > 0 {set seconds1 to seconds1 - (hours1*3600).}.
set minutes1 to floor(seconds1/60).
if minutes1 > 0 {set seconds1 to seconds1 - (minutes1*60).}.
if minutes1 < 10 {set minutes1 to "0" + minutes1.}.
set seconds1 to floor(seconds1).
if seconds1 < 10 {set seconds1 to "0" + seconds1.}.

print hours1 + ":" + minutes1 + ":" + seconds1.
//(C) 2013.

Feel free to use the code, as long as you leave the comments in.

Edited by Camacha
Link to comment
Share on other sites

Is there a way to get the parent body of a body?

For example, Mun:PARENT would return Kerbin.

I seem to remember there being something along these lines mentioned in the latest updates video, I'll check...

Okay, so you can get the parent body of a target at least with target:body, not sure about an untargeted celestial body. :)

Link to comment
Share on other sites

You can do mun:body, etc. To get the name use mun:body:name. Works on all celestial bodies.

That's not the question I asked. I was talking about the PARENT of the body. To write a generic routine.

Instead of hardcoding something like this:

"I know I'm at the Mun. I know the body I'll end up orbiting when I leave the Moon's SOI is Kerbin".

I'd like to do a thing like this:

"I know I am in the SOI of body X. I want to know about the parent body Y. Find Y given X."

For the time being I'm still using my own bodyDB file for stuff like that, but it would be a good thing to have in the built-in BODY structure.

Link to comment
Share on other sites

mun:body:name returns kerbin and mun:body:body:name returns sun. Works on anything, so eve:body is sun and gilly:body is eve. I guess i just don't get your question.

Oh no, that explains it. I didn't get your explanation. It sounded like you were just telling me that Mun:body:name returns "Mun".

So to get my current parent body is just Body(body):body then?

Link to comment
Share on other sites

BODY("bodyname"):BODY will get you a structure about bodynames' parent body but that is the same as bodyname:body. To get the parent body of a ship or target you can just call SHIP:BODY or TARGET:BODY, "BODY." is a call to SHIP:BODY. All those return a string with the name of the parent body of vessel/target. When targeting a celestial body TARGET:BODY returns a structure. I hope that is all correct, it all jumbles together when you are digging through code.

Edited by a1270
Link to comment
Share on other sites

As others were saying I would imagine you COULD make a "list" with a bunch of if statements that check distances to known target ships, but it sounds like you want to be able to target any close ship. Is this for docking or for targeting weapons fire? The more I think about it the more it sounds like targeting weapons.

is there a planned todo list to view development progress and requests?

its usefull to enumerate ships not only for targeting, but also for auto sending fuel tanks to the station which would stop near it and waiting for dock without hardcoded names of them. This can be autonavigation zond for rescue team - find and land near them with ability turning on kOS with RemoteTech and so on and so on.

it can be enumerate simply with this:

set a = `list ships`

set b = a[3]

so we got name of third ship in list

Link to comment
Share on other sites

is there a planned todo list to view development progress and requests?

its usefull to enumerate ships not only for targeting, but also for auto sending fuel tanks to the station which would stop near it and waiting for dock without hardcoded names of them. This can be autonavigation zond for rescue team - find and land near them with ability turning on kOS with RemoteTech and so on and so on.

it can be enumerate simply with this:

set a = `list ships`

set b = a[3]

so we got name of third ship in list

There are many things for which having some type of array variable type would be useful.

Link to comment
Share on other sites

mun:body:name returns kerbin and mun:body:body:name returns sun.
To get the parent body of a ship or target you can just call SHIP:BODY or TARGET:BODY, "BODY." is a call to SHIP:BODY.

So extrapolating from that, it would be ship:body:body:name to get the parent body of the current body the ship is orbiting? e.g. when orbiting Kerbin, ship:body:body:name would return the sun, and when orbiting the Mun/Minmus it would return Kerbin, yes?

Link to comment
Share on other sites

No, SHIP:BODY is a string. It'll return the name of the parent body only. I am debating submitting a pull request to change that to a structure but that'll break scripts.

Edited by a1270
Link to comment
Share on other sites

No, SHIP:BODY is a string. It'll return the name of the parent body only. I am debating submitting a pull request to change that to a structure but that'll break scripts.

*scratches head* Okay.

set x to ship:body. // assuming orbiting mun

print x:body. // this then prints kerbin?

Or can I not do that either? :blush:

I really should open up KSP and just try this stuff out myself :D

[edit] yeah that didn't work, nevermind x)

[edit 2] So this works:

set y to ship:body. // on kerbin, since you can't target your current body, when you

set target to y. // target kerbin, the game will clear your target a moment later

set x to target:body. // but before that happens(YMMV), you can store the body of the target

print x. // and then print out BODY("Sun")

I'm drawing a blank on why I started thinking about this to begin with, so I'll just leave that there in case it's of any use to someone :)

Doing "set target to ship:body." probably works too, not sure why I did the extra step. Due to the timings required that does need to be in a program, preferably without a lot of other stuff going on with kOS :P

Edited by Sacred Aardvark
typos and some clarification
Link to comment
Share on other sites

I'm drawing a blank on why I started thinking about this to begin with, so I'll just leave that there in case it's of any use to someone :)

Doing "set target to ship:body." probably works too, not sure why I did the extra step. Due to the timings required that does need to be in a program, preferably without a lot of other stuff going on with kOS :P

It would be better to use a normal homemade variable instead of target here. Because target is a special variable tied to KSP's current target, and KSP itself refuses to let you target your current SOI body, you might not be able to set target to ship:body.

Link to comment
Share on other sites

It would be better to use a normal homemade variable instead of target here. Because target is a special variable tied to KSP's current target, and KSP itself refuses to let you target your current SOI body, you might not be able to set target to ship:body.

Yeah, it's a dirty hack that sort of works. I'm pretty sure at some point I just forgot why I was doing what I was doing and sledgehammered out a solution to get it over with :D

Just to clarify and nitpick a bit, you can set your target to your current SOI, or ship:body, but keeping that target is a whole another story. That "hack" exploits the fact that it takes a split second for the game to clear your target, before which kOS can yank a command or twos worth of info on it into variables. However it is hardly reliable, since a slight hiccup and lag in kOS and you'll miss your window. Not recommended for use, but I'll leave it there as a quirk of ksp or whatever. :)

Edited by Sacred Aardvark
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...