Skip to content

Instantly share code, notes, and snippets.

@agoodman
Created February 11, 2023 22:37
Show Gist options
  • Save agoodman/6eae5dce6f78916321f9582ca1358dd0 to your computer and use it in GitHub Desktop.
Save agoodman/6eae5dce6f78916321f9582ca1358dd0 to your computer and use it in GitHub Desktop.
Client/server solution for kOS mod in Kerbal Space Program
set should_continue to true.
print "client starting".
set throttle to 0.
//wait random() * 3.
toggle ag1.
lock steering to up.
wait 0.025.
print "assigning ship name".
set randomnumber to floor(random() * 1000).
set shipname to "client" + randomnumber.
wait 0.25.
set queue to ship:messages.
print "waiting for assignment".
set host_vessel to vessel("host").
// message struct { xx }
// xx - 2-digit header code
// 01 assignment request
// 02 assignment response
// 03 throttle request
// 04 throttle response
// 05 heading request
// 06 heading response
if not host_vessel:connection:isconnected {
print "bummer".
set should_continue to false.
}.
if should_continue {
print "requesting assignment".
set payload to sendAndReceive(host_vessel:connection, queue, "01").
set raw_code to payload:substring(0, 2).
set code to raw_code:tonumber.
if code = 2 {
print payload.
set raw_code to payload:substring(2, payload:length-2).
set code to raw_code:tonumber.
print "assigned burger "+code.
toggle ag1.
set throttle to 1.
wait 2 + 2 * random().
}
else {
print "received unexpected "+payload.
set should_continue to false.
}.
}.
if should_continue {
print "requesting throttle".
set payload to sendAndReceive(host_vessel:connection, queue, "03").
print payload.
set code to payload:substring(0, 2):tonumber.
if code = 4 {
set rsp to payload:substring(2, payload:length-2):tonumber.
print "received throttle "+rsp.
set throttle to rsp / 100.
}
else {
print "mygiveup "+code.
set throttle to 0.
set should_continue to false.
}.
}.
if should_continue {
print "requesting heading".
set payload to sendAndReceive(host_vessel:connection, queue, "05").
print payload.
set code to payload:substring(0, 2):tonumber.
if code = 6 {
set az to payload:substring(2, 3):tonumber.
set el to payload:substring(5, 2):tonumber.
print "received heading "+az+", "+el.
set steering to heading(az,el).
}
else {
print "mygiveup "+code.
set throttle to 0.
set should_continue to false.
}.
}.
function sendAndReceive {
parameter connection.
parameter queue.
parameter message.
set num_retries to 0.
set response to "".
until num_retries > 5 OR response:length > 0 {
connection:sendmessage(message).
set rcvd to 0.
until rcvd = 1 {
if not queue:empty {
set response to queue:pop:content.
set rcvd to 1.
}
else {
wait 0.05.
}
}.
set num_retries to num_retries + 1.
}.
return response.
}.
//CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal").
print "host starting".
set throttle to 0.
lock steering to up.
print "assigning ship name".
set shipname to "host".
set num_clients to 0.
set clients to list().
set queue to ship:messages.
set start_time to time:seconds.
until 0 {
if not queue:empty {
set msg to queue:pop.
set rcv to msg:content.
if msg:hassender {
set sender to msg:sender.
set raw_code to rcv:substring(0, 2).
set code to raw_code:tonumber.
if code = 1 { // make me a burger
set num_clients to num_clients + 1.
clients:add(sender).
sender:connection:sendmessage("02"+num_clients).
print "assigned burger " + num_clients + " to " + sender.
}
else if code = 3 { // give me a throttle
// assign random throttle 60-70%
set throttle_rsp to 60 + random() * 10.
set throttle_rsp to floor(throttle_rsp).
sender:connection:sendmessage("04"+throttle_rsp).
print "assigned throttle "+throttle_rsp+" to " + sender.
}
else if code = 5 { // give me a heading
// assign common heading 270,45
sender:connection:sendmessage("0627045").
print "assigned heading 270,45 to " + sender.
}
else {
sender:connection:sendmessage("00").
}.
}.
}
else {
wait 0.00001.
}.
}.
set end_time to time:seconds.
set elapsed to end_time - start_time.
print "assigned clients in " + elapsed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment