CLEARSCREEN.

SET TERMINAL:WIDTH TO 80.

SET TERMINAL:HEIGHT TO 24.

SET CURMAXTHRUST TO SHIP:MAXTHRUST.

PRINT "LAUNCHING IN...".

LOCK THROTTLE TO 1.0.

SET MYSTEER TO HEADING(90,90).

LOCK STEERING TO MYSTEER.

//FROM {LOCAL COUNTDOWN IS 10.} UNTIL COUNTDOWN = 0 STEP {SET COUNTDOWN TO COUNTDOWN - 1.} DO {

// PRINT "..." + COUNTDOWN.

// WAIT 1.

//}

WHEN (SHIP:MAXTHRUST < CURMAXTHRUST AND STAGE:READY AND STAGE:NUMBER > 0) OR (ROUND(SHIP:VERTICALSPEED,0) = 0) THEN {

PRINT ROUND(SHIP:MAXTHRUST,0) + " < " + ROUND(CURMAXTHRUST,0) + " - STAGING " + STAGE:NUMBER AT (0,4).

SET CURMAXTHRUST TO SHIP:MAXTHRUST.

STAGE.

SET ENGINECOUNT TO 0.

LIST ENGINES IN ENGINE_LIST.

FOR E IN ENGINE_LIST {

IF E:STAGE >= STAGE:NUMBER {

SET ENGINECOUNT TO ENGINECOUNT + 1.

BREAK.

}

}

IF ENGINECOUNT = 0 {

WAIT UNTIL STAGE:READY.

STAGE.

}

PRESERVE.

}

CLEARSCREEN.

SET NEWPITCH TO 90.

UNTIL SHIP:APOAPSIS > 90000 {

IF SHIP:VELOCITY:SURFACE:MAG < 100 {

SET MYSTEER TO HEADING(90,90).

}

PRINT "APOAPSIS: " + ROUND(SHIP:APOAPSIS,0) + " M" AT (0,0).

PRINT "VELOCITY: " + ROUND(SHIP:VELOCITY:SURFACE:MAG,0) + " M/S" AT (0,2).

PRINT "VERT SPEED: " + ROUND(SHIP:VERTICALSPEED,0) + " m/s" AT (0,3).

PRINT "MAXTHRUST: " + ROUND(SHIP:MAXTHRUST,0) AT (0,5).

PRINT "CURMAXTHRUST: " + ROUND(CURMAXTHRUST) AT (40,5).

IF SHIP:VERTICALSPEED > 200 AND NEWPITCH > 15 {

SET NEWPITCH TO NEWPITCH - 3.

SET MYSTEER TO HEADING(90,NEWPITCH).

WAIT 3.

}

}

LOCK STEERING TO PROGRADE.

LOCK THROTTLE TO 0.

WHEN SHIP:ALTITUDE > 60000 THEN {

PANELS ON.

}

clearscreen.

PRINT("WAITING FOR CIRC BURN.")

PRINT("CALCULATING CIRC BURN.")

//Calculate necessary DeltaV and time for circularization by guess-and-check.

WHEN SHIP:ALTITUDE > 70000 {

//Time on node creation is universal time. Using ETA:APOAPSIS will get the node

//roughly at the apoapsis.

SET circNode to NODE(TIME:SECONDS + ETA:APOAPSIS,0,0,0).

ADD CIRCNODE.

SET PROGRADEDELTAV TO 0.

//increments the value to get a rough approximation of the needed Delta V.

//precise enough for basic starting orbits. Keeps adding on when desired apoapsis isn't

//high enough.

UNTIL CIRCNODE:ORBIT:PERIAPSIS < (SHIP:APOAPSIS - 500) {

SET PROGRADEDELTAV TO PROGRADEDELTAV + 1.

SET CIRCNODE TO NODE(TIME:SECONDS + ETA:APOAPSIS,0,0,PROGRADEDELTAV).

}

}

PRINT("CALCULATED. ETA: " + CIRCNODE:ETA + "s, DeltaV: " + PROGRADEDELTAV + " m/s").

//TODO: Calculate how long the burn will take for the needed Delta V.

//Will need to calculate acceleration generated by a stage, fuel consumption, and aggregate fuel.

SET BURNTIME TO 0.

SET AVAILDELTAV TO 0.

SET CURRSTAGE TO STAGE:NUMBER.

UNTIL AVAILDELTAV >= PROGRADEDELTAV {

//Calculate stage dry mass.

SET STAGEDRYMASS TO 0.

LIST SHIP:PARTS IN PARTSLIST.

FOR P IN PARTSLIST {

IF P:STAGE = CURRSTAGE {

SET STAGEDRYMASS TO STAGEDRYMASS + P:DRYMASS.

}

ELSE {

SET STAGEDRYMASS TO STAGEDRYMASS + P:MASS.

}

}

//Get the engine in the stage. A single engine for each stage is assumed.

SET STAGE_ENGINE TO 0.

LIST ENGINES IN ENGINE_LIST.

FOR E IN ENGINE_LIST {

IF P:STAGE = CURRSTAGE {

SET STAGE_ENGINE TO E.

BREAK.

}

}

//Get ISP of engine.

}

//TODO: Execute burn. Start at start time calculated before. Go through stages if necessary.

//SET CURAPOAPSIS TO SHIP:APOAPSIS.

//WAIT UNTIL SHIP:ALTITUDE > (SHIP:APOAPSIS - 100).

//PRINT("BURNING.")

//LOCK THROTTLE TO 1.

//WAIT UNTIL SHIP:PERIAPSIS >= (CURAPOAPSIS - 500).

//SET THROTTLE TO 0.

UNLOCK THROTTLE.

UNLOCK STEERING.

CLEARSCREEN.