Skip to content

Commit

Permalink
Add launch function
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Jul 8, 2024
1 parent 79e227d commit c1a448e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 153 deletions.
160 changes: 18 additions & 142 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rlviser-py"
version = "0.6.8"
version = "0.6.9"
edition = "2021"
description = "Python implementation that manages a UDP connection to RLViser"
license = "MIT"
Expand All @@ -16,7 +16,7 @@ name = "rlviser_py"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.21.0"
pyo3 = { version = "0.22.1", features = ["py-clone"] }

[profile.release]
lto = true
Expand Down
28 changes: 19 additions & 9 deletions pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
import rlviser_py as vis
import RocketSim as rs

if __name__ == "__main__":
game_mode = rs.GameMode.HOOPS

# Create example arena
arena = rs.Arena(game_mode)

# Set boost pad locations
vis.set_boost_pad_locations([pad.get_pos().as_tuple() for pad in arena.get_boost_pads()])

def run(arena, game_mode):
# Setup example arena
car = arena.add_car(rs.Team.BLUE)
car.set_state(rs.CarState(pos=rs.Vec(z=17), vel=rs.Vec(x=50), boost=100))
Expand Down Expand Up @@ -40,6 +32,24 @@
time.sleep(max(0, start_time + steps / arena.tick_rate - time.time()))
steps += 1

if __name__ == "__main__":
game_mode = rs.GameMode.HOOPS

# Create example arena
arena = rs.Arena(game_mode)

# Set boost pad locations
vis.set_boost_pad_locations([pad.get_pos().as_tuple() for pad in arena.get_boost_pads()])
run(arena, game_mode)

# Tell RLViser to exit
print("Closing...")
vis.quit()
time.sleep(1)

print("Re-opening then running...")
vis.launch()
run(arena, game_mode)

print("Exiting...")
vis.quit()
5 changes: 5 additions & 0 deletions rlviser_py.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@ The items are (car_id, team, car_config, car_state) respectively
def render(tick_count: int, tick_rate: float, game_mode: GameMode, boost_pad_states: Sequence[bool], ball: BallState, cars: Sequence[CarData]) -> ...:
pass

def launch():
"""
Opens RLViser. Useful if `quit()` was called and you want to open it again.
"""

def quit() -> ...:
pass
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pynamedmodule! {
report_game_speed,
report_game_paused,
render,
launch,
quit
],
vars: [
Expand Down Expand Up @@ -135,6 +136,11 @@ fn report_game_paused(paused: bool) {

type Car = (u32, u8, CarConfig, CarState);

#[pyfunction]
fn launch() {
socket::launch().unwrap();
}

#[pyfunction]
fn render(tick_count: u64, tick_rate: f32, game_mode: u8, boost_pad_states: Vec<bool>, ball: BallState, cars: Vec<Car>) {
let game_state = GameState {
Expand Down
8 changes: 8 additions & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ pub fn report_game_paused(paused: bool) -> io::Result<()> {
Ok(())
}

pub fn launch() -> io::Result<()> {
if let Err(e) = Command::new(RLVISER_PATH).spawn() {
println!("Failed to launch RLViser ({RLVISER_PATH}): {e}");
}

Ok(())
}

pub fn quit() -> io::Result<()> {
if let Some(socket_handler) = SOCKET.get() {
socket_handler.send_quit()?;
Expand Down

0 comments on commit c1a448e

Please sign in to comment.