-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(insim): Missing C codepage, string methods tidy up (#174)
* fix: Missing codepage, start to tidy up string methods to make them more idiomatic * wip * fix(insim): RaceLaps overflow, fixes #175
- Loading branch information
1 parent
c1ab14b
commit 77a55ec
Showing
12 changed files
with
457 additions
and
238 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "strobe" | ||
version.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
license.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
clap = { workspace = true, features = ["derive"] } | ||
if_chain = { workspace = true } | ||
insim = { path = "../../insim" } | ||
tokio = { workspace = true, features = ["full"] } | ||
tracing = { workspace = true } | ||
tracing-subscriber = { workspace = true, features = ["env-filter"] } |
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,3 @@ | ||
# light-control | ||
|
||
Control the lights on your local vehicle. Turn them on and off in a random sequence. |
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,135 @@ | ||
//! High level example | ||
//! This example showcases the shortcut methods | ||
use std::{net::SocketAddr, time::Duration}; | ||
|
||
use clap::Parser; | ||
use insim::{ | ||
identifiers::{PlayerId, RequestId}, | ||
insim::{IsiFlags, LclFlags, PlayerType, Small, SmallType, Tiny, TinyType}, | ||
Packet, Result, | ||
}; | ||
use tokio::time::interval; | ||
|
||
#[derive(Parser)] | ||
#[command(author, version, about, long_about = None)] | ||
#[command(propagate_version = true)] | ||
struct Cli { | ||
/// host:port of LFS to connect to | ||
addr: SocketAddr, | ||
} | ||
|
||
struct ReversibleSequence<T> { | ||
sequence: Vec<T>, | ||
index: usize, | ||
reverse: bool, | ||
} | ||
|
||
impl<T: Copy> ReversibleSequence<T> { | ||
fn new(sequence: Vec<T>) -> Self { | ||
ReversibleSequence { | ||
sequence, | ||
index: 0, | ||
reverse: false, | ||
} | ||
} | ||
|
||
fn next(&mut self) -> &T { | ||
if self.index >= self.sequence.len() { | ||
self.reverse = !self.reverse; | ||
self.index = 0; | ||
} | ||
|
||
let result = if self.reverse { | ||
self.sequence.get(self.sequence.len() - 1 - self.index) | ||
} else { | ||
self.sequence.get(self.index) | ||
}; | ||
|
||
self.index += 1; | ||
result.unwrap() | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
pub async fn main() -> Result<()> { | ||
// Setup tracing_subcriber with some sane defaults | ||
tracing_subscriber::fmt::fmt() | ||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
.init(); | ||
|
||
// Parse our command line arguments, using clap | ||
let cli = Cli::parse(); | ||
|
||
let mut builder = insim::tcp(cli.addr); | ||
|
||
// set our IsiFlags | ||
builder = builder.isi_flags(IsiFlags::LOCAL); | ||
|
||
// Establish a connection | ||
let mut connection = builder.connect_async().await?; | ||
tracing::info!("Connected!"); | ||
|
||
connection | ||
.write(Tiny { | ||
subt: TinyType::Npl, | ||
reqi: RequestId(1), | ||
}) | ||
.await?; | ||
|
||
let mut plid: Option<PlayerId> = None; | ||
|
||
let mut interval = interval(Duration::from_millis(250)); | ||
|
||
let mut i: usize = 0; | ||
|
||
let mut sequence = ReversibleSequence::new(vec![ | ||
LclFlags::SIGNAL_LEFT | ||
| LclFlags::LIGHT_OFF | ||
| LclFlags::FOG_REAR_OFF | ||
| LclFlags::FOG_FRONT_OFF, | ||
LclFlags::SIGNAL_OFF | LclFlags::LIGHT_HIGH | LclFlags::FOG_REAR | LclFlags::FOG_FRONT, | ||
LclFlags::SIGNAL_RIGHT | ||
| LclFlags::LIGHT_OFF | ||
| LclFlags::FOG_REAR_OFF | ||
| LclFlags::FOG_FRONT_OFF, | ||
]); | ||
|
||
loop { | ||
tokio::select! { | ||
|
||
_ = interval.tick() => { | ||
if plid.is_none() { | ||
continue; | ||
} | ||
|
||
connection.write(Small { subt: SmallType::Lcl(*sequence.next()), ..Default::default() }).await?; | ||
|
||
i = i.wrapping_add(1); | ||
}, | ||
|
||
packet = connection.read() => { | ||
|
||
match packet? { | ||
Packet::Npl(npl) => { | ||
|
||
if !npl.ptype.contains(PlayerType::REMOTE) && !npl.ptype.contains(PlayerType::AI) { | ||
plid = Some(npl.plid); | ||
tracing::info!("Woot! local player joined! {:?}", plid); | ||
} | ||
}, | ||
|
||
Packet::Pll(pll) => { | ||
if plid.map_or(false, |p| p == pll.plid) { | ||
plid = None; | ||
|
||
tracing::info!("Local player left!"); | ||
} | ||
}, | ||
|
||
_ => {} | ||
|
||
} | ||
}, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.