kRPC: Control the game using C#, C++, Java, Lua, Python...

kRPC: Control the game using C#, C++, Java, Lua, Python...

7.8k Downloads

Time-warp is disabled when vessel is throttled, but not accelerating

bismurphy opened this issue ยท 3 comments

commented

Edit: Please view my comment below! Most of the info in this initial post is incorrect and not useful.

What happened?

I am new to KRPC and am still learning the integrations. I have been going through Career Mode using KRPC for all my flights. I have successfully managed staging, parachutes, fuel monitoring, etc, and now that I'm doing longer flights, I'd like to use the Time Warp feature. However, with my Python integration, this fails. Setting conn.space_center.rails_warp_factor to any chosen factor has no effect (and after doing so, reading that value stays on zero), and conn.space_center.maximum_rails_warp_factor seems stuck at 0.

How can someone else reproduce it?

  1. Create a new CKAN instance, where KRPC is the only installed mod.
  2. Launch the game. Go into the VAB. Build a rocket which consists simply of two parts: A Mk 1 Command Pod, mounted on top of a BACC "Thumper" Solid Fuel Booster. The default delta-v readout should show 2212 m/s.
  3. Go to the launch pad. Ensure the KRPC server is running. Do not touch any controls to pilot the vessel.
  4. Run the Python flight code below. It should give a launch countdown, followed by a launch. It will print out the vessel's altitude every second, helping to confirm that the Python/KRPC connection is solid.
  5. Your booster should burn out around T+00:42, at 31km altitude and 2050 m/s. From there, the script will wait until you coast above 100 km.
  6. At 100 km, you should see the Python printout change from printing altitude to printing the warp state. You'll also see it attempt to warp to a factor of 3.
  7. It will continously print that the current warp is 0, and the maximum warp is 0.
  8. In-game, press the . key to step time-warp forward. You will now see "Current warp: 1; Maximum warp: 0". This should be impossible. You can freely increase and decrease the time warp factor; KRPC will correctly print the current warp no matter what it is, but will always say maximum warp is 0.
import time
import krpc

conn = krpc.connect(name='Flight Python')
vessel = conn.space_center.active_vessel
print(vessel.name)
vessel.auto_pilot.target_pitch_and_heading(90, 90)
vessel.auto_pilot.engage()
vessel.control.throttle = 1
for i in range(3,0,-1):
    print(f"{i}...")
    time.sleep(1)
print("Launch!")
vessel.control.activate_next_stage()

state = "flight"
while(True):
    time.sleep(1)
    if state == "flight":
        print(f"Altitude: {vessel.flight().mean_altitude}")
        if vessel.flight().mean_altitude > 100000:
            print("Attempting to enter timewarp")
            conn.space_center.rails_warp_factor = 3
            print("Commanded warp 3")
            print(f"Actual warp {conn.space_center.rails_warp_factor}")
            state = "timewarp"
    if state == "timewarp":
        print("Timewarp mode")
        print(f"Current warp: {conn.space_center.rails_warp_factor}")
        print(f"Maximum warp: {conn.space_center.maximum_rails_warp_factor}")

What is your environment?

Mod version (according to the in-game server window) is 0.5.2. Game version (according to main menu) is 1.12.5.3190. My computer is on Linux Mint 21, and my Python version is 3.10.12.

Anything else we need to know?

I'm more than happy to iterate on this in order to isolate the issue. I suspect the problem is something I'm doing rather than in KRPC itself, but would love to determine what might be causing this. If nothing else, maybe I'm making a dumb mistake that could have a warning in the documentation somewhere.

commented

I have identified the source of this issue; It is here: https://github.com/krpc/krpc/blob/main/service/SpaceCenter/src/Services/SpaceCenter.cs#L694

In KSP, it is not possible to time-warp while accelerating (ie, firing an engine). KRPC uses the logic of having nonzero throttle to detect this. However, if the vessel's engines are not firing (usually due to being out of fuel, or simply not having any engines onboard), then there will be no thrust. KSP will allow timewarp, but KRPC will not. This logic should be adjusted so that, in all situations where KSP allows timewarp, KRPC will also allow it.

I have been experimenting with vessel.acceleration.magnitude, but this shows all kinematic acceleration. I then tried checking vessel.graviticAcceleration.magnitude, and it appears that gravitic is usually extremely close, but different (by about 0.007 in my test cases) from the total acceleration when non-throttled.

It may be possible to use if abs(accel - graviticAccel) > 0.01 as the logic, or something similar, but this does not seem quite right. I am continuing to look through the KSP API documentation to see if there is a better value we can reference.

Otherwise, if this code is not to be changed, then mentioning this behavioral discrepancy between KSP's and KRPC's timewarp rules in documentation seems like a good idea, as I spent a while trying to identify the source of this problem.

commented

Another suitable solution would be if setting RailsWarpFactor (here: https://github.com/krpc/krpc/blob/main/service/SpaceCenter/src/Services/SpaceCenter.cs#L655) were to set the throttle to 0, prior to attempting to warp. In KSP, if you activate time warp while throttled-up but not thrusting (the condition of interest here), then the throttle gets set to 0, meaning that this would be a relatively transparent change to make. I will submit a PR with this solution, but I am happy to discuss alternative approaches.

commented

Fixed in #793