mod support;
use ghast::{Behavior, Controls, Interests, TickContext};
const START: (f64, f64, f32, f32) = (
18.235_736_476_016_67,
21.138_383_760_069_11,
2.479_122_6,
10.004_477,
);
struct Edge {
runs: u32,
run: u32,
ticks: u32,
rng: u64,
expect_tp: bool,
setbacks: u32,
setback_runs: Vec<u32>,
unsent: u32,
start: (f64, f64, f32),
}
fn next(s: &mut u64) -> u64 {
*s ^= *s >> 12;
*s ^= *s << 25;
*s ^= *s >> 27;
s.wrapping_mul(0x2545_f491_4f6c_dd1d)
}
impl Behavior for Edge {
fn name(&self) -> &str {
"step_edge"
}
fn interests(&self) -> Interests {
Interests::CHAT
}
fn tick(&mut self, ctx: &mut TickContext<'_>) {
for m in &ctx.events().chat {
let t = m.plain();
if t.contains("Grim") || t.contains("failed") {
log::warn!("ALERT chat: {t}");
}
}
if ctx.events().teleported {
if self.expect_tp {
self.expect_tp = false;
} else {
self.setbacks += 1;
if self.setback_runs.last() != Some(&self.run) {
self.setback_runs.push(self.run);
}
let p = ctx.pose().pos;
log::warn!(
"ALERT setback in run {} (tick {}) to {} {} {}",
self.run,
self.ticks,
p.x,
p.y,
p.z
);
}
}
let (now, sent) = (ctx.pose().pos.y, ctx.sent().pos.y);
if now != sent && ctx.pose().on_ground {
self.unsent += 1;
log::info!(
"STEP CASE run {} tick {}: feet {now}, the server has {sent}",
self.run,
self.ticks
);
}
ctx.set_controls(Controls::NONE);
self.ticks += 1;
match self.ticks {
1 => {
if self.run == self.runs {
log::info!(
"EDGE done: {} runs, {} unsent feet heights, {} setbacks in runs {:?}",
self.runs,
self.unsent,
self.setbacks,
self.setback_runs
);
ctx.disconnect("done");
return;
}
let r = |s: &mut u64| (next(s) >> 11) as f64 / (1u64 << 53) as f64;
let dx = (r(&mut self.rng) - 0.5) * 0.6;
let dz = -r(&mut self.rng) * 0.6;
let dyaw = ((r(&mut self.rng) - 0.5) * 8.0) as f32;
self.start = (START.0 + dx, START.1 + dz, START.2 + dyaw);
self.expect_tp = true;
ctx.chat(format!(
"/tp @s {} 3.875 {} {} {}",
self.start.0, self.start.1, self.start.2, START.3
));
}
2..=30 => {}
31..=60 => {
let (_, pitch) = ctx.aim();
ctx.set_aim(self.start.2, pitch);
ctx.set_controls(Controls {
forward: true,
jump: true,
..Controls::NONE
});
}
61..=80 => {}
_ => {
let p = ctx.pose().pos;
log::info!("run {} ended at {} {} {}", self.run, p.x, p.y, p.z);
self.run += 1;
self.ticks = 0;
}
}
}
}
fn main() {
let mut args: Vec<String> = std::env::args().skip(1).collect();
let mut runs = 40;
if let Some(i) = args.iter().position(|a| a == "--runs") {
runs = args[i + 1].parse().expect("--runs <n>");
args.drain(i..=i + 1);
}
let opts = support::Options::parse_from(args);
support::init_logger(opts.verbose);
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("tokio runtime");
rt.block_on(async {
let client = match opts.client() {
Ok(c) => c,
Err(e) => {
log::error!("{e}");
std::process::exit(2);
}
};
let mut edge = Edge {
runs,
run: 0,
ticks: 0,
rng: opts.seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1,
expect_tp: false,
setbacks: 0,
setback_runs: Vec::new(),
unsent: 0,
start: (START.0, START.1, START.2),
};
let end = client
.run_until(&mut edge, async {
let _ = tokio::signal::ctrl_c().await;
})
.await;
match &end {
Ok(e) => log::info!("session ended: {e:?}"),
Err(e) => log::error!("session failed: {e}"),
}
});
}