Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kerboscript and Kickstart language #5981

Merged
merged 11 commits into from
Feb 17, 2023
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@
[submodule "vendor/grammars/language-kak"]
path = vendor/grammars/language-kak
url = https://github.com/kakoune-editor/language-kak
[submodule "vendor/grammars/language-kerboscript"]
path = vendor/grammars/language-kerboscript
url = https://github.com/KSP-KOS/language-kerboscript.git
[submodule "vendor/grammars/language-kotlin"]
path = vendor/grammars/language-kotlin
url = https://github.com/nishtahir/language-kotlin
Expand Down
2 changes: 2 additions & 0 deletions grammars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,8 @@ vendor/grammars/language-jsonnet:
- source.jsonnet
vendor/grammars/language-kak:
- source.kakscript
vendor/grammars/language-kerboscript:
- source.kerboscript
vendor/grammars/language-kotlin:
- source.kotlin
vendor/grammars/language-less:
Expand Down
10 changes: 9 additions & 1 deletion lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3278,6 +3278,14 @@ KakouneScript:
- kakrc
ace_mode: text
language_id: 603336474
KerboScript:
type: programming
ace_mode: text
extensions:
- ".ks"
color: "#41adf0"
tm_scope: "source.kerboscript"
language_id: 59716426
KiCad Layout:
type: data
color: "#2f4aab"
Expand Down Expand Up @@ -4067,7 +4075,7 @@ Move:
type: programming
color: "#4a137a"
extensions:
- ".move"
- ".move"
tm_scope: source.move
ace_mode: text
language_id: 638334599
Expand Down
29 changes: 29 additions & 0 deletions samples/KerboScript/hoverslam.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// hoverslam
// A SpaceX style "hoverslam" script for the kOS mod for Kerbal Space Program
// Copyright © 2016 ayybradleyjh
// Lic. MIT

clearscreen.
set radarOffset to 9.184. // The value of alt:radar when landed (on gear)
lock trueRadar to alt:radar - radarOffset. // Offset radar to get distance from gear to ground
lock g to constant:g * body:mass / body:radius^2. // Gravity (m/s^2)
lock maxDecel to (ship:availablethrust / ship:mass) - g. // Maximum deceleration possible (m/s^2)
lock stopDist to ship:verticalspeed^2 / (2 * maxDecel). // The distance the burn will require
lock idealThrottle to stopDist / trueRadar. // Throttle required for perfect hoverslam
lock impactTime to trueRadar / abs(ship:verticalspeed). // Time until impact, used for landing gear

WAIT UNTIL ship:verticalspeed < -1.
print "Preparing for hoverslam...".
rcs on.
brakes on.
lock steering to srfretrograde.
when impactTime < 3 then {gear on.}

WAIT UNTIL trueRadar < stopDist.
print "Performing hoverslam".
lock throttle to idealThrottle.

WAIT UNTIL ship:verticalspeed > -0.01.
print "Hoverslam completed".
set ship:control:pilotmainthrottle to 0.
rcs off.
36 changes: 36 additions & 0 deletions samples/KerboScript/launch.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// launch
// This script was taken from the KSP-KOS documentation page.
// It is licensed under terms of GNU General Public License Version 3, 29 June 2007.

//First, we'll clear the terminal screen to make it look nice
CLEARSCREEN.

//Next, we'll lock our throttle to 100%.
LOCK THROTTLE TO 1.0. // 1.0 is the max, 0.0 is idle.

//This is our countdown loop, which cycles from 10 to 0
PRINT "Counting down:".
FROM {local countdown is 10.} UNTIL countdown = 0 STEP {SET countdown to countdown - 1.} DO {
PRINT "..." + countdown.
WAIT 1. // pauses the script here for 1 second.
}

//This is a trigger that constantly checks to see if our thrust is zero.
//If it is, it will attempt to stage and then return to where the script
//left off. The PRESERVE keyword keeps the trigger active even after it
//has been triggered.
WHEN MAXTHRUST = 0 THEN {
PRINT "Staging".
STAGE.
PRESERVE.
}.

LOCK STEERING TO UP.

WAIT UNTIL ALTITUDE > 70000.

// NOTE that it is vital to not just let the script end right away
// here. Once a kOS script just ends, it releases all the controls
// back to manual piloting so that you can fly the ship by hand again.
// If the program just ended here, then that would cause the throttle
// to turn back off again right away and nothing would happen.
86 changes: 86 additions & 0 deletions samples/KerboScript/lib_pid.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// lib_pid.ks provides routines to implement a simple generic PID controller.
// Copyright © 2015,2016,2019 KSLib team
// Lic. MIT
@LAZYGLOBAL off.

HUDTEXT("lib_pid.ks has been superseded by the kOS inbuilt PIDloop() function.", 10, 2, 30, RED, FALSE).
wait 0.5.
HUDTEXT("It is maintained for example purposes only, please see the kOS documentation for more.", 10, 4, 30, RED, FALSE).

function PID_init {
parameter
Kp, // gain of position
Ki, // gain of integral
Kd, // gain of derivative
cMin, // the bottom limit of the control range (to protect against integral windup)
cMax. // the the upper limit of the control range (to protect against integral windup)

local SeekP is 0. // desired value for P (will get set later).
local P is 0. // phenomenon P being affected.
local I is 0. // crude approximation of Integral of P.
local D is 0. // crude approximation of Derivative of P.
local oldT is -1. // (old time) start value flags the fact that it hasn't been calculated
local oldInput is 0. // previous return value of PID controller.

// Because we don't have proper user structures in kOS (yet?)
// I'll store the PID tracking values in a list like so:
//
local PID_array is list(Kp, Ki, Kd, cMin, cMax, SeekP, P, I, D, oldT, oldInput).

return PID_array.
}.

function PID_seek {
parameter
PID_array, // array built with PID_init.
seekVal, // value we want.
curVal. // value we currently have.

// Using LIST() as a poor-man's struct.

local Kp is PID_array[0].
local Ki is PID_array[1].
local Kd is PID_array[2].
local cMin is PID_array[3].
local cMax is PID_array[4].
local oldS is PID_array[5].
local oldP is PID_array[6].
local oldI is PID_array[7].
local oldD is PID_array[8].
local oldT is PID_array[9]. // Old Time
local oldInput is PID_array[10]. // prev return value, just in case we have to do nothing and return it again.

local P is seekVal - curVal.
local D is oldD. // default if we do no work this time.
local I is oldI. // default if we do no work this time.
local newInput is oldInput. // default if we do no work this time.

local t is time:seconds.
local dT is t - oldT.

if oldT < 0 {
// I have never been called yet - so don't trust any
// of the settings yet.
} else {
if dT > 0 { // Do nothing if no physics tick has passed from prev call to now.
set D to (P - oldP)/dT. // crude fake derivative of P
local onlyPD is Kp*P + Kd*D.
if (oldI > 0 or onlyPD > cMin) and (oldI < 0 or onlyPD < cMax) { // only do the I turm when within the control range
set I to oldI + P*dT. // crude fake integral of P
}.
set newInput to onlyPD + Ki*I.
}.
}.

set newInput to max(cMin,min(cMax,newInput)).

// remember old values for next time.
set PID_array[5] to seekVal.
set PID_array[6] to P.
set PID_array[7] to I.
set PID_array[8] to D.
set PID_array[9] to t.
set PID_array[10] to newInput.

return newInput.
}.
1 change: 1 addition & 0 deletions vendor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ This is a list of grammars that Linguist selects to provide syntax highlighting
- **Jupyter Notebook:** [Nixinova/NovaGrammars](https://github.com/Nixinova/NovaGrammars)
- **Kaitai Struct:** [atom/language-yaml](https://github.com/atom/language-yaml)
- **KakouneScript:** [kakoune-editor/language-kak](https://github.com/kakoune-editor/language-kak)
- **KerboScript:** [KSP-KOS/language-kerboscript](https://github.com/KSP-KOS/language-kerboscript)
- **KiCad Layout:** [Alhadis/language-pcb](https://github.com/Alhadis/language-pcb)
- **KiCad Legacy Layout:** [Alhadis/language-pcb](https://github.com/Alhadis/language-pcb)
- **KiCad Schematic:** [Alhadis/language-pcb](https://github.com/Alhadis/language-pcb)
Expand Down
1 change: 1 addition & 0 deletions vendor/grammars/language-kerboscript
Submodule language-kerboscript added at fabac0
31 changes: 31 additions & 0 deletions vendor/licenses/git_submodule/language-kerboscript.dep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: language-kerboscript
version: fabac004678aff28f443bb363b17ad045e33b1e4
type: git_submodule
homepage: https://github.com/KSP-KOS/language-kerboscript.git
license: mit
licenses:
- sources: LICENSE.md
text: |
Copyright (c) 2015 Tony Spataro
Copyright (c) 2017 KSP-KOS contributors (https://github.com/KSP-KOS)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
notices: []