Skip to content

Commit

Permalink
Add Max Acceleration Yaw Offset (#101)
Browse files Browse the repository at this point in the history
* Add max accel yaw offset and goodies

* update BXT exported function

* update hltas
  • Loading branch information
khanghugo authored Jul 28, 2024
1 parent 2b28140 commit 2b06276
Show file tree
Hide file tree
Showing 12 changed files with 1,178 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ color-eyre = { version = "0.6.2", default-features = false }
crossbeam-channel = "0.5.8"
git-version = "0.3.5"
glam = "0.24.1"
hltas = { version = "0.8.0", features = ["serde1"] }
hltas = { version = "0.9.0", features = ["serde1"] }
ipc-channel = "0.16.1"
itertools = "0.11.0"
libc = "0.2.147"
Expand Down Expand Up @@ -51,7 +51,7 @@ features = ["libloaderapi", "psapi", "winuser", "synchapi", "handleapi", "proces

[dev-dependencies]
expect-test = "1.4.1"
hltas = { version = "0.8.0", features = ["serde1", "proptest1"] }
hltas = { version = "0.9.0", features = ["serde1", "proptest1"] }
proptest = "1.2.0"

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion bxt-ipc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ edition = "2021"

[dependencies]
bxt-strafe = { version = "0.1.0", path = "../bxt-strafe" }
hltas = { version = "0.8.0", features = ["serde1"] }
hltas = { version = "0.9.0", features = ["serde1"] }
serde = { version = "1.0.174", features = ["derive"] }
4 changes: 2 additions & 2 deletions bxt-strafe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"
arrayvec = { version = "0.7.4", features = ["serde"] }
bxt-vct = { path = "../bxt-vct" }
glam = { version = "0.24.1", features = ["serde"] }
hltas = { version = "0.8.0" }
hltas = { version = "0.9.0" }
serde = { version = "1.0.174", features = ["derive"] }
tap = "1.0.1"

[dev-dependencies]
ncollide3d = "0.33.0"
proptest = "1.2.0"
proptest-derive = "0.3.0"
hltas = { version = "0.8.0", features = ["proptest1"] }
hltas = { version = "0.9.0", features = ["proptest1"] }
12 changes: 12 additions & 0 deletions bxt-strafe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ pub struct State {
// Number of frames for [`StrafeDir::LeftRight`] or [`StrafeDir::RightLeft`] which goes from
// `0` to `count - 1`.
pub strafe_cycle_frame_count: u32,
// Accelerated yaw speed specifics
pub max_accel_yaw_offset_value: f32,
// These values are to indicate whether we are in a "different" frame bulk.
pub prev_max_accel_yaw_offset_start: f32,
pub prev_max_accel_yaw_offset_target: f32,
pub prev_max_accel_yaw_offset_accel: f32,
pub prev_max_accel_yaw_offset_right: bool,
// In case of yaw and pitch override, this might be useful.
pub rendered_viewangles: Vec3,
}
Expand All @@ -147,6 +154,11 @@ impl State {
jumped: false,
move_traces: ArrayVec::new(),
strafe_cycle_frame_count: 0,
max_accel_yaw_offset_value: 0.,
prev_max_accel_yaw_offset_start: 0.,
prev_max_accel_yaw_offset_target: 0.,
prev_max_accel_yaw_offset_accel: 0.,
prev_max_accel_yaw_offset_right: false,
rendered_viewangles: Vec3::ZERO,
};

Expand Down
55 changes: 54 additions & 1 deletion bxt-strafe/src/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<S: Step> Step for Strafe<S> {
frame_bulk.auto_actions.movement
{
let theta = match type_ {
StrafeType::MaxAccel => match dir {
StrafeType::MaxAccel | StrafeType::MaxAccelYawOffset { .. } => match dir {
StrafeDir::Left => max_accel_theta(parameters, &state),
StrafeDir::Right => -max_accel_theta(parameters, &state),
StrafeDir::Yaw(yaw) => {
Expand Down Expand Up @@ -447,6 +447,18 @@ impl<S: Step> Step for Strafe<S> {
(camera_yaw, entry)
};

let camera_yaw = if matches!(type_, StrafeType::MaxAccelYawOffset { .. }) {
// theta < 0. = is right
// If is right then we decreases yaw by offset.
// Therefore, positive offset in framebulk mean going more on that side.
let offset = state.max_accel_yaw_offset_value.to_radians();
let offset = if theta < 0. { -offset } else { offset };

camera_yaw + angle_mod_rad(offset)
} else {
camera_yaw
};

input.yaw = camera_yaw;
input.forward = entry.forward as f32;
input.side = entry.side as f32;
Expand Down Expand Up @@ -545,6 +557,47 @@ impl<S: Step> Step for ResetFields<S> {
state.strafe_cycle_frame_count = 0;
}

// If we have some acceleration, then this kicks in.
// It will preserve the final value across split segments.
if let Some(AutoMovement::Strafe(StrafeSettings {
type_:
StrafeType::MaxAccelYawOffset {
start,
target,
accel,
},
dir,
})) = frame_bulk.auto_actions.movement
{
let right = matches!(dir, StrafeDir::Right);

// Flip start and target when accel is negative.
state.max_accel_yaw_offset_value = (state.max_accel_yaw_offset_value + accel)
.max(start)
.min(target);

// Reset value if we have different inputs.
// This means that if we split a s5x bulk,
// there won't be any side effects.
if start != state.prev_max_accel_yaw_offset_start
|| target != state.prev_max_accel_yaw_offset_target
|| accel != state.prev_max_accel_yaw_offset_accel
|| right != state.prev_max_accel_yaw_offset_right
{
state.max_accel_yaw_offset_value = if accel.is_sign_negative() {
target
} else {
start
};

// Update so next time we know what to compare against.
state.prev_max_accel_yaw_offset_start = start;
state.prev_max_accel_yaw_offset_target = target;
state.prev_max_accel_yaw_offset_accel = accel;
state.prev_max_accel_yaw_offset_right = right;
};
}

self.0
.simulate(tracer, parameters, frame_bulk, state, input)
}
Expand Down
19 changes: 15 additions & 4 deletions src/hooks/bxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use hltas::HLTAS;
use crate::modules::tas_studio;
use crate::utils::{abort_on_panic, MainThreadMarker, Pointer, PointerTrait};

pub static BXT_ON_TAS_PLAYBACK_FRAME: Pointer<
pub static BXT_ON_TAS_PLAYBACK_FRAME_V2: Pointer<
*mut Option<unsafe extern "C" fn(OnTasPlaybackFrameData) -> c_int>,
> = Pointer::empty(b"bxt_on_tas_playback_frame\0");
> = Pointer::empty(b"bxt_on_tas_playback_frame_v2\0");
pub static BXT_ON_TAS_PLAYBACK_STOPPED: Pointer<*mut Option<unsafe extern "C" fn()>> =
Pointer::empty(b"bxt_on_tas_playback_stopped\0");
pub static BXT_SIMULATION_IPC_IS_CLIENT_INITIALIZED: Pointer<unsafe extern "C" fn() -> c_int> =
Expand All @@ -31,7 +31,7 @@ pub static BXT_TAS_STUDIO_FREECAM_SET_ORIGIN: Pointer<unsafe extern "C" fn([c_fl
Pointer::empty(b"bxt_tas_studio_freecam_set_origin\0");

static POINTERS: &[&dyn PointerTrait] = &[
&BXT_ON_TAS_PLAYBACK_FRAME,
&BXT_ON_TAS_PLAYBACK_FRAME_V2,
&BXT_ON_TAS_PLAYBACK_STOPPED,
&BXT_SIMULATION_IPC_IS_CLIENT_INITIALIZED,
&BXT_TAS_LOAD_SCRIPT_FROM_STRING,
Expand Down Expand Up @@ -79,7 +79,7 @@ pub unsafe fn find_pointers(marker: MainThreadMarker) {
}

fn set_callbacks(marker: MainThreadMarker) {
if let Some(bxt_on_tas_playback_frame) = BXT_ON_TAS_PLAYBACK_FRAME.get_opt(marker) {
if let Some(bxt_on_tas_playback_frame) = BXT_ON_TAS_PLAYBACK_FRAME_V2.get_opt(marker) {
// SAFETY: this is a global variable in BXT which is accessed only from the main game thread
// (which is the current thread as we have a marker).
unsafe {
Expand Down Expand Up @@ -144,9 +144,20 @@ pub struct OnTasPlaybackFrameData {
pub strafe_cycle_frame_count: u32,
pub prev_predicted_trace_fractions: [f32; 4],
pub prev_predicted_trace_normal_zs: [f32; 4],
pub max_accel_yaw_offset: OnTasPlaybackFrameMaxAccelYawOffset,
pub rendered_viewangles: [f32; 3],
}

#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct OnTasPlaybackFrameMaxAccelYawOffset {
pub value: f32,
pub start: f32,
pub target: f32,
pub accel: f32,
pub dir: u8,
}

unsafe extern "C" fn on_tas_playback_frame(data: OnTasPlaybackFrameData) -> c_int {
abort_on_panic(move || {
let marker = MainThreadMarker::new();
Expand Down
20 changes: 18 additions & 2 deletions src/modules/tas_optimizer/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ impl Optimizer {
Line::Change(_) => (),
Line::TargetYawOverride(_) => (),
Line::RenderYawOverride(_) => (),
Line::PitchOverride(_) => (),
Line::RenderPitchOverride(_) => (),
}
}

Expand Down Expand Up @@ -537,9 +539,12 @@ fn mutate_single_frame_bulk<R: Rng>(change_pitch: bool, hltas: &mut HLTAS, rng:
frame_bulk.auto_actions.movement.as_mut()
{
// Mutate strafe type.
*type_ = if let StrafeType::ConstYawspeed(yawspeed) = *type_ {
*type_ = if let StrafeType::ConstYawspeed(_) = *type_ {
// Constant yawspeed will not be selected unless specified in bulk.
StrafeType::ConstYawspeed(yawspeed)
*type_
} else if let StrafeType::MaxAccelYawOffset { .. } = *type_ {
// Max accel yaw offset will not be selected unless specified in bulk.
*type_
} else {
let p = rng.gen::<f32>();
if p < 0.01 {
Expand All @@ -558,6 +563,17 @@ fn mutate_single_frame_bulk<R: Rng>(change_pitch: bool, hltas: &mut HLTAS, rng:
*yawspeed = (*yawspeed + rng.gen_range(-1f32..1f32)).abs();
};

if let StrafeType::MaxAccelYawOffset {
ref mut start,
ref mut target,
ref mut accel,
} = *type_
{
*start += rng.gen_range(-1f32..1f32);
*target += rng.gen_range(-1f32..1f32);
*accel += rng.gen_range(-1f32..1f32);
}

// Mutate strafe direction.
match dir {
StrafeDir::Yaw(yaw) => {
Expand Down
Loading

0 comments on commit 2b06276

Please sign in to comment.