From 559b1053f202c544f22a5a37cbe1605bfb16f67c Mon Sep 17 00:00:00 2001 From: Askigh Date: Tue, 19 Nov 2019 00:52:02 +0100 Subject: [PATCH 1/4] Minor changes, such as - Usage of primitives instead of wrappers - Usage of proper builder for ControlsOuput --- src/main/java/rlbotexample/JavaExample.java | 5 +- src/main/java/rlbotexample/SampleBot.java | 5 +- .../rlbotexample/output/ControlsOutput.java | 164 ++++++++++-------- .../java/rlbotexample/vector/Vector2.java | 5 + .../java/rlbotexample/vector/Vector3.java | 5 + 5 files changed, 110 insertions(+), 74 deletions(-) diff --git a/src/main/java/rlbotexample/JavaExample.java b/src/main/java/rlbotexample/JavaExample.java index c188933..13c7085 100644 --- a/src/main/java/rlbotexample/JavaExample.java +++ b/src/main/java/rlbotexample/JavaExample.java @@ -2,6 +2,7 @@ import rlbot.manager.BotManager; import rlbotexample.util.PortReader; +import rlbotexample.vector.Vector2; import javax.swing.*; import javax.swing.border.EmptyBorder; @@ -18,12 +19,12 @@ */ public class JavaExample { - private static final Integer DEFAULT_PORT = 17357; + private static final int DEFAULT_PORT = 17357; public static void main(String[] args) { BotManager botManager = new BotManager(); - Integer port = PortReader.readPortFromArgs(args).orElseGet(() -> { + int port = PortReader.readPortFromArgs(args).orElseGet(() -> { System.out.println("Could not read port from args, using default!"); return DEFAULT_PORT; }); diff --git a/src/main/java/rlbotexample/SampleBot.java b/src/main/java/rlbotexample/SampleBot.java index 80c5e3d..096c3a3 100644 --- a/src/main/java/rlbotexample/SampleBot.java +++ b/src/main/java/rlbotexample/SampleBot.java @@ -53,9 +53,10 @@ private ControlsOutput processInput(DataPacket input) { RLBotDll.sendQuickChat(playerIndex, false, QuickChatSelection.Compliments_NiceOne); } - return new ControlsOutput() + return ControlsOutput.builder() .withSteer(goLeft ? -1 : 1) - .withThrottle(1); + .withThrottle(1) + .build(); } /** diff --git a/src/main/java/rlbotexample/output/ControlsOutput.java b/src/main/java/rlbotexample/output/ControlsOutput.java index ace5d65..4710148 100644 --- a/src/main/java/rlbotexample/output/ControlsOutput.java +++ b/src/main/java/rlbotexample/output/ControlsOutput.java @@ -10,6 +10,9 @@ */ public class ControlsOutput implements ControllerState { + // Single builder instance to avoid multiple object instantiation per game tick + private static final Builder BUILDER = new Builder(); + // 0 is straight, -1 is hard left, 1 is hard right. private float steer; @@ -30,76 +33,97 @@ public class ControlsOutput implements ControllerState { private boolean slideDepressed; private boolean useItemDepressed; - public ControlsOutput() { - } - - public ControlsOutput withSteer(float steer) { - this.steer = clamp(steer); - return this; - } - - public ControlsOutput withPitch(float pitch) { - this.pitch = clamp(pitch); - return this; - } - - public ControlsOutput withYaw(float yaw) { - this.yaw = clamp(yaw); - return this; - } - - public ControlsOutput withRoll(float roll) { - this.roll = clamp(roll); - return this; - } - - public ControlsOutput withThrottle(float throttle) { - this.throttle = clamp(throttle); - return this; - } - - public ControlsOutput withJump(boolean jumpDepressed) { - this.jumpDepressed = jumpDepressed; - return this; - } - - public ControlsOutput withBoost(boolean boostDepressed) { - this.boostDepressed = boostDepressed; - return this; - } - - public ControlsOutput withSlide(boolean slideDepressed) { - this.slideDepressed = slideDepressed; - return this; - } - - public ControlsOutput withUseItem(boolean useItemDepressed) { - this.useItemDepressed = useItemDepressed; - return this; - } - - public ControlsOutput withJump() { - this.jumpDepressed = true; - return this; - } - - public ControlsOutput withBoost() { - this.boostDepressed = true; - return this; - } - - public ControlsOutput withSlide() { - this.slideDepressed = true; - return this; - } - - public ControlsOutput withUseItem() { - this.useItemDepressed = true; - return this; - } - - private float clamp(float value) { - return Math.max(-1, Math.min(1, value)); + public ControlsOutput() { } + + public static class Builder { + + private ControlsOutput controlsOutput; + + private Builder() { + reset(); + } + + private void reset() { + this.controlsOutput = new ControlsOutput(); + } + + public Builder withSteer(float steer) { + controlsOutput.steer = clamp(steer); + return this; + } + + public Builder withPitch(float pitch) { + controlsOutput.pitch = clamp(pitch); + return this; + } + + public Builder withYaw(float yaw) { + controlsOutput.yaw = clamp(yaw); + return this; + } + + public Builder withRoll(float roll) { + controlsOutput.roll = clamp(roll); + return this; + } + + public Builder withThrottle(float throttle) { + controlsOutput.throttle = clamp(throttle); + return this; + } + + public Builder withJump(boolean jumpDepressed) { + controlsOutput.jumpDepressed = jumpDepressed; + return this; + } + + public Builder withBoost(boolean boostDepressed) { + controlsOutput.boostDepressed = boostDepressed; + return this; + } + + public Builder withSlide(boolean slideDepressed) { + controlsOutput.slideDepressed = slideDepressed; + return this; + } + + public Builder withUseItem(boolean useItemDepressed) { + controlsOutput.useItemDepressed = useItemDepressed; + return this; + } + + public Builder withJump() { + controlsOutput.jumpDepressed = true; + return this; + } + + public Builder withBoost() { + controlsOutput.boostDepressed = true; + return this; + } + + public Builder withSlide() { + controlsOutput.slideDepressed = true; + return this; + } + + public Builder withUseItem() { + controlsOutput.useItemDepressed = true; + return this; + } + + public ControlsOutput build() { + return controlsOutput; + } + + private float clamp(float value) { + return Math.max(-1, Math.min(1, value)); + } + } + + public static Builder builder() { + BUILDER.reset(); + return BUILDER; } @Override diff --git a/src/main/java/rlbotexample/vector/Vector2.java b/src/main/java/rlbotexample/vector/Vector2.java index 2197505..dad1633 100644 --- a/src/main/java/rlbotexample/vector/Vector2.java +++ b/src/main/java/rlbotexample/vector/Vector2.java @@ -98,4 +98,9 @@ public double correctionAngle(Vector2 ideal) { public static double angle(Vector2 a, Vector2 b) { return Math.abs(a.correctionAngle(b)); } + + @Override + public String toString() { + return String.format("(%s, %s)", x, y); + } } diff --git a/src/main/java/rlbotexample/vector/Vector3.java b/src/main/java/rlbotexample/vector/Vector3.java index b359c52..506ab21 100644 --- a/src/main/java/rlbotexample/vector/Vector3.java +++ b/src/main/java/rlbotexample/vector/Vector3.java @@ -99,4 +99,9 @@ public Vector3 crossProduct(Vector3 v) { double tz = x * v.y - y * v.x; return new Vector3(tx, ty, tz); } + + @Override + public String toString() { + return String.format("(%s, %s, %s)", x, y, z); + } } From 065403bafb8a675af3e250d8f96282245b49d750 Mon Sep 17 00:00:00 2001 From: Askigh Date: Tue, 19 Nov 2019 00:59:39 +0100 Subject: [PATCH 2/4] Generified type declarations + added missing @Override annotations --- src/main/java/rlbotexample/SampleBot.java | 1 + src/main/java/rlbotexample/SamplePythonInterface.java | 1 + src/main/java/rlbotexample/boost/BoostManager.java | 11 ++++++----- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/rlbotexample/SampleBot.java b/src/main/java/rlbotexample/SampleBot.java index 096c3a3..8853f2c 100644 --- a/src/main/java/rlbotexample/SampleBot.java +++ b/src/main/java/rlbotexample/SampleBot.java @@ -122,6 +122,7 @@ public ControllerState processInput(GameTickPacket packet) { return controlsOutput; } + @Override public void retire() { System.out.println("Retiring sample bot " + playerIndex); } diff --git a/src/main/java/rlbotexample/SamplePythonInterface.java b/src/main/java/rlbotexample/SamplePythonInterface.java index 829aa93..aa679bc 100644 --- a/src/main/java/rlbotexample/SamplePythonInterface.java +++ b/src/main/java/rlbotexample/SamplePythonInterface.java @@ -10,6 +10,7 @@ public SamplePythonInterface(int port, BotManager botManager) { super(port, botManager); } + @Override protected Bot initBot(int index, String botType, int team) { return new SampleBot(index); } diff --git a/src/main/java/rlbotexample/boost/BoostManager.java b/src/main/java/rlbotexample/boost/BoostManager.java index a9fa1bf..b93d7a6 100644 --- a/src/main/java/rlbotexample/boost/BoostManager.java +++ b/src/main/java/rlbotexample/boost/BoostManager.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.List; /** * Information about where boost pads are located on the field and what status they have. @@ -17,15 +18,15 @@ */ public class BoostManager { - private static final ArrayList orderedBoosts = new ArrayList<>(); - private static final ArrayList fullBoosts = new ArrayList<>(); - private static final ArrayList smallBoosts = new ArrayList<>(); + private static final List orderedBoosts = new ArrayList<>(); + private static final List fullBoosts = new ArrayList<>(); + private static final List smallBoosts = new ArrayList<>(); - public static ArrayList getFullBoosts() { + public static List getFullBoosts() { return fullBoosts; } - public static ArrayList getSmallBoosts() { + public static List getSmallBoosts() { return smallBoosts; } From 11fe7ca99a1e82fa38fd16a59641ef819152c15f Mon Sep 17 00:00:00 2001 From: Askigh Date: Tue, 19 Nov 2019 01:00:41 +0100 Subject: [PATCH 3/4] deleted useless import --- src/main/java/rlbotexample/JavaExample.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/rlbotexample/JavaExample.java b/src/main/java/rlbotexample/JavaExample.java index 13c7085..4cf1a66 100644 --- a/src/main/java/rlbotexample/JavaExample.java +++ b/src/main/java/rlbotexample/JavaExample.java @@ -2,7 +2,6 @@ import rlbot.manager.BotManager; import rlbotexample.util.PortReader; -import rlbotexample.vector.Vector2; import javax.swing.*; import javax.swing.border.EmptyBorder; From 8f295cd36b8958494fa23660e14ad6c92442da4d Mon Sep 17 00:00:00 2001 From: Askigh Date: Tue, 19 Nov 2019 21:22:51 +0100 Subject: [PATCH 4/4] removed builder, added method displayWindow --- src/main/java/rlbotexample/JavaExample.java | 4 + src/main/java/rlbotexample/SampleBot.java | 5 +- .../rlbotexample/output/ControlsOutput.java | 136 ++++++++---------- 3 files changed, 62 insertions(+), 83 deletions(-) diff --git a/src/main/java/rlbotexample/JavaExample.java b/src/main/java/rlbotexample/JavaExample.java index 4cf1a66..d3d7230 100644 --- a/src/main/java/rlbotexample/JavaExample.java +++ b/src/main/java/rlbotexample/JavaExample.java @@ -31,6 +31,10 @@ public static void main(String[] args) { SamplePythonInterface pythonInterface = new SamplePythonInterface(port, botManager); new Thread(pythonInterface::start).start(); + displayWindow(botManager, port); + } + + private static void displayWindow(BotManager botManager, int port) { JFrame frame = new JFrame("Java Bot"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/main/java/rlbotexample/SampleBot.java b/src/main/java/rlbotexample/SampleBot.java index 8853f2c..0a82f17 100644 --- a/src/main/java/rlbotexample/SampleBot.java +++ b/src/main/java/rlbotexample/SampleBot.java @@ -53,10 +53,9 @@ private ControlsOutput processInput(DataPacket input) { RLBotDll.sendQuickChat(playerIndex, false, QuickChatSelection.Compliments_NiceOne); } - return ControlsOutput.builder() + return new ControlsOutput() .withSteer(goLeft ? -1 : 1) - .withThrottle(1) - .build(); + .withThrottle(1); } /** diff --git a/src/main/java/rlbotexample/output/ControlsOutput.java b/src/main/java/rlbotexample/output/ControlsOutput.java index 4710148..9cdbd2b 100644 --- a/src/main/java/rlbotexample/output/ControlsOutput.java +++ b/src/main/java/rlbotexample/output/ControlsOutput.java @@ -10,9 +10,6 @@ */ public class ControlsOutput implements ControllerState { - // Single builder instance to avoid multiple object instantiation per game tick - private static final Builder BUILDER = new Builder(); - // 0 is straight, -1 is hard left, 1 is hard right. private float steer; @@ -33,97 +30,76 @@ public class ControlsOutput implements ControllerState { private boolean slideDepressed; private boolean useItemDepressed; - public ControlsOutput() { } - - public static class Builder { - - private ControlsOutput controlsOutput; - - private Builder() { - reset(); - } - - private void reset() { - this.controlsOutput = new ControlsOutput(); - } - - public Builder withSteer(float steer) { - controlsOutput.steer = clamp(steer); - return this; - } - - public Builder withPitch(float pitch) { - controlsOutput.pitch = clamp(pitch); - return this; - } + public ControlsOutput() { + } - public Builder withYaw(float yaw) { - controlsOutput.yaw = clamp(yaw); - return this; - } + public ControlsOutput withSteer(float steer) { + this.steer = clamp(steer); + return this; + } - public Builder withRoll(float roll) { - controlsOutput.roll = clamp(roll); - return this; - } + public ControlsOutput withPitch(float pitch) { + this.pitch = clamp(pitch); + return this; + } - public Builder withThrottle(float throttle) { - controlsOutput.throttle = clamp(throttle); - return this; - } + public ControlsOutput withYaw(float yaw) { + this.yaw = clamp(yaw); + return this; + } - public Builder withJump(boolean jumpDepressed) { - controlsOutput.jumpDepressed = jumpDepressed; - return this; - } + public ControlsOutput withRoll(float roll) { + this.roll = clamp(roll); + return this; + } - public Builder withBoost(boolean boostDepressed) { - controlsOutput.boostDepressed = boostDepressed; - return this; - } + public ControlsOutput withThrottle(float throttle) { + this.throttle = clamp(throttle); + return this; + } - public Builder withSlide(boolean slideDepressed) { - controlsOutput.slideDepressed = slideDepressed; - return this; - } + public ControlsOutput withJump(boolean jumpDepressed) { + this.jumpDepressed = jumpDepressed; + return this; + } - public Builder withUseItem(boolean useItemDepressed) { - controlsOutput.useItemDepressed = useItemDepressed; - return this; - } + public ControlsOutput withBoost(boolean boostDepressed) { + this.boostDepressed = boostDepressed; + return this; + } - public Builder withJump() { - controlsOutput.jumpDepressed = true; - return this; - } + public ControlsOutput withSlide(boolean slideDepressed) { + this.slideDepressed = slideDepressed; + return this; + } - public Builder withBoost() { - controlsOutput.boostDepressed = true; - return this; - } + public ControlsOutput withUseItem(boolean useItemDepressed) { + this.useItemDepressed = useItemDepressed; + return this; + } - public Builder withSlide() { - controlsOutput.slideDepressed = true; - return this; - } + public ControlsOutput withJump() { + this.jumpDepressed = true; + return this; + } - public Builder withUseItem() { - controlsOutput.useItemDepressed = true; - return this; - } + public ControlsOutput withBoost() { + this.boostDepressed = true; + return this; + } - public ControlsOutput build() { - return controlsOutput; - } + public ControlsOutput withSlide() { + this.slideDepressed = true; + return this; + } - private float clamp(float value) { - return Math.max(-1, Math.min(1, value)); - } + public ControlsOutput withUseItem() { + this.useItemDepressed = true; + return this; } - public static Builder builder() { - BUILDER.reset(); - return BUILDER; + private float clamp(float value) { + return Math.max(-1, Math.min(1, value)); } @Override @@ -170,4 +146,4 @@ public boolean holdHandbrake() { public boolean holdUseItem() { return useItemDepressed; } -} +} \ No newline at end of file