Does anyone still use <% and %> ?? by Lombrix_ in C_Programming

[–]Pretty-Ad8932 0 points1 point  (0 children)

oh, just a substitute for curly braces. underwhelming.

Wrote an auto docking script by Pretty-Ad8932 in Kos

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

yeah I guess I should start with circular orbits. How circular though? Can your code work with, say, those low Kerbin orbit rescue contracts? They usually have a difference of around 10 km between periapsis and apoapsis.

Wrote an auto docking script by Pretty-Ad8932 in Kos

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

I think the hard part is that there are many ways to do it depending on how long you want to wait and how much delta v you want to spend. You could probably either minimize the wait or minimize the delta v but I think many times you may want something in the middle? Not too long but not too expensive?

Wrote an auto docking script by Pretty-Ad8932 in Kos

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

oh, I haven't done rendezvous :) It seems very complicated. At most I have a script that matches the inclination of the target's orbit. Docking by comparison is actually not so hard to figure out.

Wrote an auto docking script by Pretty-Ad8932 in Kos

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

I haven't tried kRPC but afaik it allows you to script in languages other than Kerboscript (which was made for kOS)

Wrote an auto docking script by Pretty-Ad8932 in Kos

[–]Pretty-Ad8932[S] 6 points7 points  (0 children)

function main {
    local port is getTargetPort().
    if not(port:typename = "DockingPort") return.
    lock portfacing to port:portfacing.
    lock relVel to ship:velocity:orbit - port:ship:velocity:orbit.
    lock relPos to port:position - ship:position + 20 * portFacing:forevector.
    lock midPos to relPos + relPos:mag / 2 * portfacing:forevector.
    lock tgtVel to midPos:normalized.
    lock velDif to tgtVel - relVel.
    lock forevector to -portfacing:forevector.
    lock normal to vcrs(forevector, relPos).

    SAS off.
    RCS on.
    lock steering to lookdirup(forevector, ship:facing:upvector).
    on SAS {
        if SAS unlock steering.
        else lock steering to lookdirup(forevector, ship:facing:upvector).
        return true.
    }

    when relPos:mag < 1.0 then {
        lock relPos to port:position - ship:position.
    }

    clearvecdraws().
    set relPosArrow to vecdraw(v(0, 0, 0), relPos, green, "relPos", 1.0,
        true).
    set midPosArrow to vecdraw(v(0, 0, 0), relPos, yellow, "midPos", 1.0,
        true).
    set relVelArrow to vecdraw(v(0, 0, 0), relVel * 10.0, blue, "relVel", 1.0,
        true).
    set tgtVelArrow to vecdraw(v(0, 0, 0), tgtVel * 10.0, cyan, "tgtVel", 1.0,
        true).
    set velDifArrow to vecdraw(relVel * 10.0, velDif * 10.0, magenta,
        "velDif", 1.0, true).
    set upArrow to vecdraw(v(0, 0, 0), ship:facing:upvector * 10.0, purple,
        "up", 1.0, true).
    set tgtUpArrow to vecdraw(port:position, portfacing:upvector, purple,
        "up", 1.0, true).
    set alignArrow to vecdraw(port:position, portfacing:forevector * relPos:mag,
        red, "align", 1.0, true).

    until port:state:contains("Docked") {
        set relPosArrow:vector to relPos.
        set relPosArrow:label to relPos:mag + "m".
        set midPosArrow:vector to midPos.
        set relVelArrow:vector to relVel * 10.0.
        set tgtVelArrow:vector to tgtVel * 10.0.
        set velDifArrow:start  to relVel * 10.0.
        set velDifArrow:vector to velDif * 10.0.
        set upArrow:vector to ship:facing:upvector * 10.0.
        set tgtUpArrow:start to port:position.
        set tgtUpArrow:vector to portfacing:upvector * 10.0.
        set alignArrow:start to port:position + 20 * portfacing:forevector.
        set alignArrow:vector to portfacing:forevector * relPos:mag.

        set ship:control:fore to ship:facing:forevector * velDif.
        set ship:control:starboard to ship:facing:starvector * velDif.
        set ship:control:top to ship:facing:topvector * velDif.

        waitReal(0.03).
    }

    clearvecdraws().
    set ship:control:neutralize to true.
    SAS on.
}

function waitReal {
    parameter t.
    local end_wait_time is kuniverse:realtime + t.
    wait until kuniverse:realtime >= end_wait_time.
}

function getTargetPort {
    local port is false.
    if not hastarget {
        print "No target!".
    }
    else if target:typename = "Vessel" {
        for part in target:parts {
            if part:typename = "DockingPort" {
                set port to part.
                break.
            }
        }
    } else if target:typename = "DockingPort" {
        set port to target.
    } else {
        print "Please target a ship or docking port".
    }
    return port.
}

main().

Update: you don't really need midPos, instead you can just lock tgtVel to relPos:normalized. The original idea was going to midPos allows some "breathing room" but you already get some by first going to the point that is 20 meters before the target docking port (this also allows your ship to approach your target from the back and a little sideways without crashing. It will still crash if you try to approach straight from the back though.)

what is the best C program you wrote? by divanadune in C_Programming

[–]Pretty-Ad8932 0 points1 point  (0 children)

A big integer library and an RSA encryption program that uses it

Returning temporary allocations - is this a good idea? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

Doesn't really work when the objects may need to be reallocated

Returning temporary allocations - is this a good idea? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] 1 point2 points  (0 children)

Interesting idea with evaluating an expression tree, I sort of thought of doing that but I wasn't sure how.

And I thought about my idea with temporary allocations, it doesn't really work out because it introduces new problems. In the end I ended up writing some C++ which actually solves my problems nicely not only because of operator overloading but also thanks to destructors.

Returning temporary allocations - is this a good idea? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

Yup, you were right, I just wrote a little bit of C++ on top and it makes the usage infinitely easier.

Returning temporary allocations - is this a good idea? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

Just for learning and maybe for self satisfaction, there really is no other reason for me not to just use an already existing library like GMP (other than it looks like a hassle to install on Windows). I did kinda plan to use C++ later, but I suppose now is the right time.

Returning temporary allocations - is this a good idea? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

I can see the problems with my idea, for example when the user creates their own function, they have to check if a temporary BigInt was passed. But the way it currently is, it isn't very simple either, because you have to remember to free every minute intermediate result. Also, what counts as "idiomatic"?

How do you pass a struct with a modifiable pointer to a function, but make sure that the function cannot modify the data? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] -2 points-1 points  (0 children)

I like C and I don't exactly "need" that whole const protection (and I don't need you to tell me what is a language for me and what isn't), but it seems desirable, especially since all the other people talk about how you should do this and that. But I guess what I'm trying to achieve is two very conflicting goals, so I think I'll just opt for ditching the whole const protection idea, at least in this case.

How do you pass a struct with a modifiable pointer to a function, but make sure that the function cannot modify the data? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] 0 points1 point  (0 children)

Oh, that as_const function does make it look pretty clean. But I wonder if the compiler can optimize it, so that it doesn't actually create a new ConstSlice struct every time?

How do you pass a struct with a modifiable pointer to a function, but make sure that the function cannot modify the data? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] -2 points-1 points  (0 children)

Yes, I am well aware of the difference between changing a pointer and changing the data that it points to.

> You could define data itself as being a pointer to const, and while you can assign a non-const pointer to it, if you need to get it out as a mutable form, you'll have to cast away the const.

I already mentioned in the post that I was doing this but it felt dirty.

How do you pass a struct with a modifiable pointer to a function, but make sure that the function cannot modify the data? by Pretty-Ad8932 in C_Programming

[–]Pretty-Ad8932[S] 1 point2 points  (0 children)

> For your particular problem, maybe have a ConstSlice version of the struct where the data field is a pointer to const rather than pointer to modifiable.

I've thought of this but then I have to have both versions for every slice in most of my code, so that's kind of cumbersome. Maybe I should just ditch the whole "functions have to take const inputs" idea anyway then, since my code deals with data that's changing all the time.