untoldwind / KontrolSystem2

Autopilot scripting system for KSP2
Other
54 stars 14 forks source link

Enhancement kUniverse #152

Closed PhilouDS closed 6 days ago

PhilouDS commented 3 months ago

Me again :)

I like the kUniverse structure from kOS. https://ksp-kos.github.io/KOS/structures/misc/kuniverse.html

2 things in particular:

I use the latter for gravity turn tests.

Any chance we can have this in kontrol System too? šŸ˜¬

untoldwind commented 3 months ago

Looks like a lot of these functions are private. It should be possible to trigger the corresponding event, but it might be hard to get a clean callback when the operation has finished (the load/save operations seem to run in their own coroutines).

Surprisingly enough: The underlying functions to reset the "Universe" to a previous state are public, so it actually might be easier to have own inScript-savepoints to revert to.

Edit: This is a bit more complicated than expected, I will move that to the next release.

untoldwind commented 2 months ago

Added experimental support for this in pre-release 0.5.7.7 (https://github.com/untoldwind/KontrolSystem2/releases/tag/v0.5.7.7)

Quick-Save seems to work pretty reliable:

use { Vessel } from ksp::vessel
use { CONSOLE, RED, WHITE } from ksp::console
use { sleep, wait_while, wait_until, current_time } from ksp::game
use { DEBUG, SAVE_LOAD_CONTROL } from ksp::debug

pub fn main_flight(vessel: Vessel) -> Result<Unit, string> = {
    CONSOLE.clear()

    SAVE_LOAD_CONTROL.quick_save()

    wait_while(fn() -> SAVE_LOAD_CONTROL.is_saving) // If waiting for completion is necessary

    CONSOLE.print_line("Done")
}

Similarly a Quick-Load can be triggered with:

use { Vessel } from ksp::vessel
use { CONSOLE, RED, WHITE } from ksp::console
use { sleep, wait_while, wait_until, current_time } from ksp::game
use { DEBUG, SAVE_LOAD_CONTROL } from ksp::debug

pub fn main_flight(vessel: Vessel) -> Result<Unit, string> = {
    CONSOLE.clear()

    SAVE_LOAD_CONTROL.quick_load()
}

But: This will implicitly terminate all running scripts, including the script that triggered the re-load. This is probably a good thing, because all in-game references (like the reference to the active vessel) will become invalid anyway.

PhilouDS commented 2 months ago

Thanks

lefouvert commented 2 months ago

Out of curiosity, how bad would it be if a script would be able to Ā«auto-loadĀ» himself at flight start/quick save load ? Not from old references, of course, but from start, main_filght(vessel: Vessel, yapyapyap: Velociraptor) Not an actual need, just an idea to play with.

untoldwind commented 2 months ago

I guess it would be preferable to be able to distinguished between started-by-user and re-started-due-to-reload somehow. Unluckily there is a lot internal stuff happening during a reload (the loading screen is not just for show ;) ), so I still have to figure out how to ensure that the restart does not happen too early.

lefouvert commented 2 months ago

I understand your struggles. If it can solace you, remember KSP2 itself is not able to load itself properly : I have a craft which at first load violently Krakenify the firsts seconds of flight until it goes boom and fall down in pieces. Second load, everything is perfect, stable, etc. I dont know if it's aerodynamics or vessel's parts joint which are not correctly loaded, but undoubtly, something is missing. So be able to know when the Kontrol System layer is free to go had to be not so easy.

PhilouDS commented 2 months ago

I guess it would be preferable to be able to distinguished between started-by-user and re-started-due-to-reload somehow. Unluckily there is a lot internal stuff happening during a reload (the loading screen is not just for show ;) ), so I still have to figure out how to ensure that the restart does not happen too early.

I think this was the problem with kOS when they created the boot file.

Booting when spawning to launchpad When you first spawn a new vessel on the launchapd from the VAB (or when you spawn it to the runway from the SPH), kOS performs the following initial steps to get the boot file copied from archive to the ship:

Creates a folder called boot/ on the kOS computerā€™s local volume (1:/).

Copies the boot file from the archiveā€™s boot/ folder to the local volumeā€™s boot folder.

Important: NOW is the point where Kerbal Space Program saves the game for the purpose of being able to ā€œrevert to launchā€.

kOS begins running that local copy of the boot file.

Please make note of when during those steps Kerbal Space Program saved the game for the sake of doing a revert to launch. If you edit the boot file on the archive, and then revert to launch, then your vessel will not have the newly edited boot file copied to it because it doesnā€™t go all the way back to do step 1 and 2 from the above list again. To force it to use the new version of the boot file you will either have to revert it all the way to the assembly building and re-launch it from there, or stop the boot file with ctrl-C and manually copy the new file and reboot.

It might be great to have a boot file like this in Kontrol System but, as lefouvert said, KSP2 is not reliable yet.

untoldwind commented 2 months ago

It been been a while since I have used kOS, but if I remember correctly there was a special part that had to be attached to a vessel, and one of the properties of that part was the boot-file to "auto-start" for the vessel.

So far I avoided messing around with the part-definitions of KSP2, but I think there is now a plugin that support patch-files similar to the ModuleManager in KSP1. So maybe it would be (at the very least) feasible to add a PartModule to (lets say) the control-parts where a start-script could be configured

PhilouDS commented 2 months ago

Yes, you're right. The boot file needed to be loaded in the VAB from a special part. In RP1, you don't need the kOS part since you have all the kOS action in the avionics. So it could be a good idea to use a PartModule to control-parts.

lefouvert commented 2 months ago

As far I remember : Special part on your ship The part have a parameter to choose a specific file to boot on (file which have to be in a specific folder) Thoses two are saved in craft savefile As soon the inflight context is loaded, the selected bootscript start. This boot script HAVE TO wait for a variable (ship.unpacked ?) to be sure all parts are fully loaded. And there you go.

But since I haven't played since a while too, I maybe forgotten something. However, I don't expect Kontrol System to be Ā«KOs 2Ā», even if it's heavily inspried from, and some features of KOs were usefull.

PhilouDS commented 2 months ago

This boot script HAVE TO wait for a variable (ship.unpacked ?) to be sure all parts are fully loaded. And there you go.

It's not mandatory, the boot script doesn't need to wait for a variable.. BUT! because of what I shared in a previous message from the kOS documentation, the boot file is loaded before the craft is unpacked so you need to be cautious.

However, I don't expect Kontrol System to be Ā«KOs 2Ā», even if it's heavily inspried from, and some features of KOs were usefull.

You're right. to2 can't be a copy of kOS but I'm okay if we grab some piece of it to improve it šŸ˜‡

lefouvert commented 2 months ago

Yup, it's not a mandatory, but as soon as you want to compute anything vessel related (The main purpose of the mod), if you don't wait, you WILL have unexpected result, if you don't just crash your script.

For me, it's as Ā«the parachute is not a mandatory to experienced free fall when you jump from a planeĀ». A true assertion. But I won't try twice.

PhilouDS commented 2 months ago

@untoldwind May I ask for something more: the possibility to check if the vessel is recoverable and, if it is, to recover it (and thus, to end the script at the same time I suppose). Thank you very much. šŸ™ šŸ˜‡

untoldwind commented 2 months ago

The callback in the ESC-menu of the game is "private", and it looks like there are some hard-coded conditions like: A vessel is only recoverable of it splashed or landed on Kerbin. I think it should be possible to just re-implement these conditions in the addon, but there is a good chance that is might break with future releases of the game.

untoldwind commented 2 months ago

In 0.5.8.2 (https://github.com/untoldwind/KontrolSystem2/releases/tag/v0.5.8.2) I added a try_recover_vessel to ksp::debug::SAVE_LOAD_CONTROL, which should do the same as pressing the button in the ESC-menu.

PhilouDS commented 2 months ago

In 0.5.8.2 (https://github.com/untoldwind/KontrolSystem2/releases/tag/v0.5.8.2) I added a try_recover_vessel to ksp::debug::SAVE_LOAD_CONTROL, which should do the same as pressing the button in the ESC-menu.

I just finish to test it: it's perfect! thanks!

github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] commented 6 days ago

This issue was closed because it has been inactive for 14 days since being marked as stale.