kRPC - Remote Procedure Calls for Kitten Space Agency#
kRPC for KSA lets you control Kitten Space Agency from scripts running outside the game. A server inside the game exposes the game’s API, and the kRPC 0.7.0 client libraries call it from Python, C#, C++, C, Java and Lua.
Note
kRPC for KSA is experimental, and its API may change between releases.
This script launches a rocket from Cape Canaveral straight up, then cuts the engines once it reaches an altitude of 10 km:
import krpc
conn = krpc.connect(name="Launch")
ksa = conn.ksa
vehicle = ksa.launch_vehicle("Rocket", "CCSFS LC-39A")
flight = vehicle.flight
vehicle.auto_pilot.target_pitch_and_heading(90, 90)
vehicle.auto_pilot.engage()
vehicle.control.throttle = 1
vehicle.control.activate_next_sequence()
vehicle.control.engine_on = True
print(f"{vehicle.name} has launched")
event = conn.add_event(lambda: flight.mean_altitude > 10000)
with event.condition:
event.wait()
vehicle.control.engine_on = False
vehicle.auto_pilot.disengage()
print(f"{vehicle.name} reached 10 km")
using System;
using KRPC.Client;
using KRPC.Client.Services.KSA;
class Launch
{
public static void Main ()
{
var conn = new Connection ("Launch");
var vehicle = conn.KSA ().LaunchVehicle ("Rocket", "CCSFS LC-39A");
var flight = vehicle.Flight;
vehicle.AutoPilot.TargetPitchAndHeading (90, 90);
vehicle.AutoPilot.Engage ();
vehicle.Control.Throttle = 1;
vehicle.Control.ActivateNextSequence ();
vehicle.Control.EngineOn = true;
Console.WriteLine (vehicle.Name + " has launched");
var evnt = conn.AddEvent (() => flight.MeanAltitude > 10000);
lock (evnt.Condition) {
evnt.Wait ();
}
vehicle.Control.EngineOn = false;
vehicle.AutoPilot.Disengage ();
Console.WriteLine (vehicle.Name + " reached 10 km");
}
}
#include <iostream>
#include <krpc.hpp>
#include <krpc/function_stream.hpp>
#include <krpc/services/krpc.hpp>
#include <krpc/services/ksa.hpp>
int main() {
krpc::Client conn = krpc::connect("Launch");
krpc::services::KSA ksa(&conn);
auto vehicle = ksa.launch_vehicle("Rocket", "CCSFS LC-39A");
auto flight = vehicle.flight();
vehicle.auto_pilot().target_pitch_and_heading(90, 90);
vehicle.auto_pilot().engage();
vehicle.control().set_throttle(1);
vehicle.control().activate_next_sequence();
vehicle.control().set_engine_on(true);
std::cout << vehicle.name() << " has launched" << std::endl;
typedef krpc::services::KRPC::Expression Expr;
auto expr = Expr::greater_than(
conn, Expr::call(conn, flight.mean_altitude_call()), Expr::constant_double(conn, 10000));
auto event = krpc::add_event(expr);
event.acquire();
event.wait();
event.release();
vehicle.control().set_engine_on(false);
vehicle.auto_pilot().disengage();
std::cout << vehicle.name() << " reached 10 km" << std::endl;
}
#include <stdio.h>
#include <stdlib.h>
#include <krpc_cnano.h>
#include <krpc_cnano/services/ksa.h>
int main() {
krpc_connection_t conn;
krpc_connection_config_t config = {"127.0.0.1", 50000};
krpc_open(&conn, &config);
krpc_connect(conn, "Launch");
krpc_KSA_Vehicle_t vehicle;
krpc_KSA_LaunchVehicle(conn, &vehicle, "Rocket", "CCSFS LC-39A");
krpc_KSA_AutoPilot_t auto_pilot;
krpc_KSA_Vehicle_AutoPilot(conn, &auto_pilot, vehicle);
krpc_KSA_Control_t control;
krpc_KSA_Vehicle_Control(conn, &control, vehicle);
krpc_KSA_Flight_t flight;
krpc_KSA_Vehicle_Flight(conn, &flight, vehicle);
char * name = NULL;
krpc_KSA_Vehicle_Name(conn, &name, vehicle);
krpc_KSA_AutoPilot_TargetPitchAndHeading(conn, auto_pilot, 90, 90);
krpc_KSA_AutoPilot_Engage(conn, auto_pilot);
krpc_KSA_Control_set_Throttle(conn, control, 1);
krpc_KSA_Control_ActivateNextSequence(conn, NULL, control);
krpc_KSA_Control_set_EngineOn(conn, control, true);
printf("%s has launched\n", name);
double mean_altitude = 0;
while (mean_altitude < 10000)
krpc_KSA_Flight_MeanAltitude(conn, &mean_altitude, flight);
krpc_KSA_Control_set_EngineOn(conn, control, false);
krpc_KSA_AutoPilot_Disengage(conn, auto_pilot);
printf("%s reached 10 km\n", name);
free(name);
}
import krpc.client.Connection;
import krpc.client.Event;
import krpc.client.RPCException;
import krpc.client.StreamException;
import krpc.client.services.KRPC.Expression;
import krpc.client.services.KSA;
import krpc.schema.KRPC.ProcedureCall;
import java.io.IOException;
public class Launch {
public static void main(String[] args) throws IOException, RPCException, StreamException {
try (Connection connection = Connection.newInstance("Launch")) {
KSA ksa = KSA.newInstance(connection);
KSA.Vehicle vehicle = ksa.launchVehicle("Rocket", "CCSFS LC-39A");
KSA.Flight flight = vehicle.getFlight();
vehicle.getAutoPilot().targetPitchAndHeading(90, 90);
vehicle.getAutoPilot().engage();
vehicle.getControl().setThrottle(1);
vehicle.getControl().activateNextSequence();
vehicle.getControl().setEngineOn(true);
System.out.println(vehicle.getName() + " has launched");
ProcedureCall meanAltitude = connection.getCall(flight, "getMeanAltitude");
Expression expr = Expression.greaterThan(
connection,
Expression.call(connection, meanAltitude),
Expression.constantDouble(connection, 10000));
Event event = connection.addEvent(expr);
synchronized (event.getCondition()) {
event.waitFor();
}
vehicle.getControl().setEngineOn(false);
vehicle.getAutoPilot().disengage();
System.out.println(vehicle.getName() + " reached 10 km");
}
}
}
local krpc = require 'krpc'
local platform = require 'krpc.platform'
local conn = krpc.connect('Launch')
local vehicle = conn.ksa:launch_vehicle('Rocket', 'CCSFS LC-39A')
local flight = vehicle.flight
vehicle.auto_pilot:target_pitch_and_heading(90, 90)
vehicle.auto_pilot:engage()
vehicle.control.throttle = 1
vehicle.control:activate_next_sequence()
vehicle.control.engine_on = true
print(vehicle.name .. ' has launched')
while flight.mean_altitude < 10000 do
platform.sleep(0.1)
end
vehicle.control.engine_on = false
vehicle.auto_pilot:disengage()
print(vehicle.name .. ' reached 10 km')
Install the server mod, install a client and run your first script.
How the server and the game behave: reference frames, object lifetime, the auto-pilot, server side functions and timing.
Complete scripts that launch into orbit, plan burns and change orbits.
The guide and API reference for each client library.
Build the mod, its client stubs and its documentation from source, and run the tests.
The kRPC documentation, for Kerbal Space Program.