-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: heading controller API, TOGA-aware autopilot heading control
- Loading branch information
1 parent
3b7c9ed
commit 8cf2dab
Showing
16 changed files
with
189 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/octol1ttle/flightassistant/computers/api/ControllerPriority.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ru.octol1ttle.flightassistant.computers.api; | ||
|
||
public enum ControllerPriority { | ||
HIGHEST(0), | ||
HIGH(1), | ||
NORMAL(2), | ||
LOW(3), | ||
LOWEST(4); | ||
|
||
public final int priority; | ||
|
||
ControllerPriority(int priority) { | ||
this.priority = priority; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/ru/octol1ttle/flightassistant/computers/api/IHeadingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ru.octol1ttle.flightassistant.computers.api; | ||
|
||
import net.minecraft.util.Pair; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface IHeadingController { | ||
/** | ||
* Gets the target heading that this controller wants | ||
* @return a pair of two floats: the first one is the target heading, second is the delta time multiplier. If 'null', the controller doesn't want any yaw at this moment | ||
*/ | ||
@Nullable | ||
Pair<@NotNull Float, @NotNull Float> getControlledHeading(); | ||
|
||
ControllerPriority getPriority(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/HeadingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package ru.octol1ttle.flightassistant.computers.impl.autoflight; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import net.minecraft.util.Pair; | ||
import net.minecraft.util.math.MathHelper; | ||
import org.jetbrains.annotations.NotNull; | ||
import ru.octol1ttle.flightassistant.computers.api.ControllerPriority; | ||
import ru.octol1ttle.flightassistant.computers.api.IHeadingController; | ||
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; | ||
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer; | ||
import ru.octol1ttle.flightassistant.registries.ComputerRegistry; | ||
import ru.octol1ttle.flightassistant.registries.events.ComputerRegisteredCallback; | ||
|
||
public class HeadingController implements ITickableComputer { | ||
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); | ||
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class); | ||
private final List<IHeadingController> controllers = new ArrayList<>(); | ||
|
||
public HeadingController() { | ||
ComputerRegisteredCallback.EVENT.register((computer -> { | ||
if (computer instanceof IHeadingController controller) { | ||
controllers.add(controller); | ||
} | ||
})); | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
if (!data.canAutomationsActivate()) { | ||
return; | ||
} | ||
|
||
ControllerPriority lastPriority = null; | ||
for (IHeadingController controller : controllers) { | ||
if (lastPriority != null && controller.getPriority() != lastPriority) { | ||
break; | ||
} | ||
|
||
Pair<@NotNull Float, @NotNull Float> targetPitch = controller.getControlledHeading(); | ||
if (targetPitch != null) { | ||
smoothSetHeading(targetPitch.getLeft(), MathHelper.clamp(time.deltaTime * targetPitch.getRight(), 0.001f, 1.0f)); | ||
lastPriority = controller.getPriority(); | ||
} | ||
} | ||
} | ||
|
||
private void smoothSetHeading(Float heading, float delta) { | ||
if (heading == null) { | ||
return; | ||
} | ||
|
||
float difference = heading - data.heading(); | ||
if (difference < -180.0f) { | ||
difference += 360.0f; | ||
} | ||
if (difference > 180.0f) { | ||
difference -= 360.0f; | ||
} | ||
|
||
float newHeading; | ||
if (Math.abs(difference) < 0.05f) { | ||
newHeading = heading; | ||
} else { | ||
newHeading = data.heading() + difference * delta; | ||
} | ||
|
||
data.player().setYaw(newHeading - 180.0f); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "heading_ctl"; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
src/main/java/ru/octol1ttle/flightassistant/computers/impl/autoflight/YawController.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.