Skip to content

Instantly share code, notes, and snippets.

@Caellian
Created September 7, 2023 09:06
Show Gist options
  • Save Caellian/7674f834fe86be7df9298da57e914089 to your computer and use it in GitHub Desktop.
Save Caellian/7674f834fe86be7df9298da57e914089 to your computer and use it in GitHub Desktop.
Ping pong expanded
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use litesim::prelude::*;
pub struct Player;
#[cfg(feature = "rewind")]
impl<'s> RewindModel<'s> for Player {
type RewindBackup = Self;
fn backup(&self, _: ModelCtx<'s>) -> Self::RewindBackup {
Player
}
fn unwind_to_state(
&mut self,
_: &Self::RewindBackup,
_: ModelCtx<'s>,
) -> Result<(), SimulationError> {
Ok(())
}
}
impl<'s> Model<'s> for Player {
fn input_connectors(&self) -> Vec<&'static str> {
<[_]>::into_vec(
#[rustc_box]
::alloc::boxed::Box::new(["receive"]),
)
}
fn get_input_handler<'h>(&self, index_: usize) -> Option<Box<dyn ErasedInputHandler<'h, 's>>>
where
's: 'h,
{
match index_ {
0usize => {
let handler: Box<
&dyn Fn(
&mut Player,
::litesim::event::Event<()>,
::litesim::simulation::ModelCtx<'s>,
) -> Result<(), ::litesim::error::SimulationError>,
> = Box::new(
&(|self_: &mut Player,
_: ::litesim::event::Event<()>,
ctx: ::litesim::simulation::ModelCtx<'s>| {
ctx.schedule_update(Now)?;
Ok(())
}),
);
return Some(handler);
}
_ => return None,
}
}
fn output_connectors(&self) -> Vec<OutputConnectorInfo> {
<[_]>::into_vec(
#[rustc_box]
::alloc::boxed::Box::new([::litesim::routes::OutputConnectorInfo::new::<()>("send")]),
)
}
fn handle_update(&mut self, ctx: ModelCtx<'s>) -> Result<(), SimulationError> {
{
let lvl = ::log::Level::Info;
if lvl <= ::log::STATIC_MAX_LEVEL && lvl <= ::log::max_level() {
::log::__private_api::log(
format_args!(
"Player {0} got the ball at {1}",
ctx.model_id.as_ref(),
ctx.time
),
lvl,
&("ping_pong", "ping_pong", "examples/ping_pong.rs"),
34u32,
::log::__private_api::Option::None,
);
}
};
ctx.push_event_with_time::<()>(
::litesim::event::Signal(),
std::borrow::Cow::Borrowed("send"),
In(ctx.rand_range(0.0..1.0)),
)?;
Ok(())
}
fn type_id(&self) -> std::any::TypeId {
std::any::TypeId::of::<Self>()
}
}
fn main() {
env_logger::init();
let system = System::new()
.with_model("p1", Player)
.with_model("p2", Player)
.with_route(
::litesim::routes::ConnectorPath {
model: std::borrow::Cow::Borrowed("p1"),
connector: std::borrow::Cow::Borrowed("send"),
},
::litesim::routes::ConnectorPath {
model: std::borrow::Cow::Borrowed("p2"),
connector: std::borrow::Cow::Borrowed("receive"),
},
)
.with_route(
::litesim::routes::ConnectorPath {
model: std::borrow::Cow::Borrowed("p2"),
connector: std::borrow::Cow::Borrowed("send"),
},
::litesim::routes::ConnectorPath {
model: std::borrow::Cow::Borrowed("p1"),
connector: std::borrow::Cow::Borrowed("receive"),
},
);
let mut sim = Simulation::new(rand::thread_rng(), system, 0.0).expect("invalid model");
sim.schedule_event(
0.5,
Signal(),
::litesim::routes::ConnectorPath {
model: std::borrow::Cow::Borrowed("p1"),
connector: std::borrow::Cow::Borrowed("receive"),
},
)
.expect("unable to schedule initial event");
sim.run_until(50.0).expect("simulation error");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment