refactor: replace raw pointer arithmetic with typed struct access
Browse files- weapon_fire.rs: use DDGame.level_width/level_height instead of
.add(0x77C0/0x77C4); use new CTaskWorm accessors instead of
.add(0x34/0x48) on raw worm pointer
- sound_ops.rs: use CGameTask.pos_y instead of .add(0x88)
- sound_ops.rs + active_sound.rs: use SoundEmitter.local_ref_count
instead of .add(8) on raw emitter pointer
- game_task.rs: split SoundEmitter._unknown_04 to expose ref count
field at offset 0x08
- worm.rs: add fire_subtype_1/set_fire_subtype_1_raw/set_action_field_raw
accessors; rewrite state() to use subclass_data slice indexing
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
crates/openwa-core/src/audio/active_sound.rs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
use crate::engine::ddgame::DDGame;
|
| 2 |
use crate::fixed::Fixed;
|
|
|
|
| 3 |
|
| 4 |
/// Active sound tracking table — manages positional (local) sound playback.
|
| 5 |
///
|
|
@@ -72,9 +73,7 @@ impl ActiveSoundTable {
|
|
| 72 |
|
| 73 |
// Release the emitter reference
|
| 74 |
if !entry.emitter.is_null() {
|
| 75 |
-
|
| 76 |
-
let ref_count = entry.emitter.add(8) as *mut i32;
|
| 77 |
-
*ref_count -= 1;
|
| 78 |
entry.emitter = core::ptr::null_mut();
|
| 79 |
}
|
| 80 |
|
|
|
|
| 1 |
use crate::engine::ddgame::DDGame;
|
| 2 |
use crate::fixed::Fixed;
|
| 3 |
+
use crate::task::SoundEmitter;
|
| 4 |
|
| 5 |
/// Active sound tracking table — manages positional (local) sound playback.
|
| 6 |
///
|
|
|
|
| 73 |
|
| 74 |
// Release the emitter reference
|
| 75 |
if !entry.emitter.is_null() {
|
| 76 |
+
(*(entry.emitter as *mut SoundEmitter)).local_ref_count -= 1;
|
|
|
|
|
|
|
| 77 |
entry.emitter = core::ptr::null_mut();
|
| 78 |
}
|
| 79 |
|
crates/openwa-core/src/audio/sound_ops.rs
CHANGED
|
@@ -17,7 +17,7 @@ use crate::audio::{play_sound, play_sound_pooled, SoundId};
|
|
| 17 |
use crate::engine::{DDGame, DDGameWrapper, SoundQueueEntry};
|
| 18 |
use crate::fixed::Fixed;
|
| 19 |
use crate::task::worm::CTaskWorm;
|
| 20 |
-
use crate::task::{CGameTask, CTask};
|
| 21 |
|
| 22 |
// ============================================================
|
| 23 |
// Sound queue insertion
|
|
@@ -172,8 +172,8 @@ pub unsafe fn play_worm_sound_2(
|
|
| 172 |
}
|
| 173 |
|
| 174 |
// 2. Start new sound
|
| 175 |
-
//
|
| 176 |
-
let worm_y =
|
| 177 |
let new_handle = if worm_y < -0x270F_FFFF && sound_id.0 == 0x36 {
|
| 178 |
// Special teleport case: play at weapon target position
|
| 179 |
load_and_play_streaming_positional(
|
|
@@ -414,10 +414,9 @@ unsafe fn record_active_sound(
|
|
| 414 |
entry.sequence = t.counter as i32;
|
| 415 |
entry.channel_handle = channel_handle as u32;
|
| 416 |
|
| 417 |
-
// Increment emitter ref count
|
| 418 |
if !emitter.is_null() {
|
| 419 |
-
|
| 420 |
-
*ref_count += 1;
|
| 421 |
}
|
| 422 |
|
| 423 |
t.counter as i32
|
|
|
|
| 17 |
use crate::engine::{DDGame, DDGameWrapper, SoundQueueEntry};
|
| 18 |
use crate::fixed::Fixed;
|
| 19 |
use crate::task::worm::CTaskWorm;
|
| 20 |
+
use crate::task::{CGameTask, CTask, SoundEmitter};
|
| 21 |
|
| 22 |
// ============================================================
|
| 23 |
// Sound queue insertion
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
// 2. Start new sound
|
| 175 |
+
// Check if worm is extremely high (CGameTask.pos_y, fixed-point).
|
| 176 |
+
let worm_y = (*worm).base.pos_y.0;
|
| 177 |
let new_handle = if worm_y < -0x270F_FFFF && sound_id.0 == 0x36 {
|
| 178 |
// Special teleport case: play at weapon target position
|
| 179 |
load_and_play_streaming_positional(
|
|
|
|
| 414 |
entry.sequence = t.counter as i32;
|
| 415 |
entry.channel_handle = channel_handle as u32;
|
| 416 |
|
| 417 |
+
// Increment emitter ref count
|
| 418 |
if !emitter.is_null() {
|
| 419 |
+
(*(emitter as *mut SoundEmitter)).local_ref_count += 1;
|
|
|
|
| 420 |
}
|
| 421 |
|
| 422 |
t.counter as i32
|
crates/openwa-core/src/game/weapon_fire.rs
CHANGED
|
@@ -561,12 +561,11 @@ unsafe fn fire_teleport(worm: *mut CTaskWorm) {
|
|
| 561 |
let fire_y = (*worm).weapon_param_2;
|
| 562 |
call_spawn_effect(fire_x, fire_y, rb(0x547C30));
|
| 563 |
|
| 564 |
-
// Temporarily swap
|
| 565 |
-
let
|
| 566 |
-
|
| 567 |
-
*(worm_raw.add(0x34) as *mut i32) = (*worm)._unknown_190;
|
| 568 |
call_position_update(worm, fire_x, fire_y, rb(0x4FE070));
|
| 569 |
-
|
| 570 |
|
| 571 |
// Compute new state: version < 455 → Idle (0x65), else → 0x8B
|
| 572 |
let ddgame = CTask::ddgame_raw(worm as *const CTask);
|
|
@@ -579,7 +578,7 @@ unsafe fn fire_teleport(worm: *mut CTaskWorm) {
|
|
| 579 |
CTaskWorm::set_state_raw(worm, new_state);
|
| 580 |
|
| 581 |
// Clear action fields
|
| 582 |
-
|
| 583 |
(*worm)._unknown_208 = 0;
|
| 584 |
(*worm)._unknown_198 = 0;
|
| 585 |
(*worm)._unknown_19c = 0;
|
|
@@ -836,9 +835,8 @@ unsafe fn fire_armageddon(worm: *mut CTaskWorm) {
|
|
| 836 |
|
| 837 |
// If old game version or worm state 0x69, set gravity center
|
| 838 |
if game_version < 0x50 || (*worm).is_in_state(WormState::Unknown_0x69) {
|
| 839 |
-
let
|
| 840 |
-
let
|
| 841 |
-
let level_h = *(ddgame_raw.add(0x77C4) as *const i32);
|
| 842 |
// SHL 16; CDQ; SUB EAX,EDX; SAR 1 — round-toward-zero divide by 2
|
| 843 |
let half_x = ((level_w << 16) + (if level_w < 0 { 1 } else { 0 })) >> 1;
|
| 844 |
let half_y = ((level_h << 16) + (if level_h < 0 { 1 } else { 0 })) >> 1;
|
|
|
|
| 561 |
let fire_y = (*worm).weapon_param_2;
|
| 562 |
call_spawn_effect(fire_x, fire_y, rb(0x547C30));
|
| 563 |
|
| 564 |
+
// Temporarily swap fire_subtype_1 (+0x34) with _unknown_190, call position update, restore
|
| 565 |
+
let saved_subtype1 = CTaskWorm::fire_subtype_1(worm);
|
| 566 |
+
CTaskWorm::set_fire_subtype_1_raw(worm, (*worm)._unknown_190);
|
|
|
|
| 567 |
call_position_update(worm, fire_x, fire_y, rb(0x4FE070));
|
| 568 |
+
CTaskWorm::set_fire_subtype_1_raw(worm, saved_subtype1);
|
| 569 |
|
| 570 |
// Compute new state: version < 455 → Idle (0x65), else → 0x8B
|
| 571 |
let ddgame = CTask::ddgame_raw(worm as *const CTask);
|
|
|
|
| 578 |
CTaskWorm::set_state_raw(worm, new_state);
|
| 579 |
|
| 580 |
// Clear action fields
|
| 581 |
+
CTaskWorm::set_action_field_raw(worm, 0);
|
| 582 |
(*worm)._unknown_208 = 0;
|
| 583 |
(*worm)._unknown_198 = 0;
|
| 584 |
(*worm)._unknown_19c = 0;
|
|
|
|
| 835 |
|
| 836 |
// If old game version or worm state 0x69, set gravity center
|
| 837 |
if game_version < 0x50 || (*worm).is_in_state(WormState::Unknown_0x69) {
|
| 838 |
+
let level_w = (*ddgame).level_width as i32;
|
| 839 |
+
let level_h = (*ddgame).level_height as i32;
|
|
|
|
| 840 |
// SHL 16; CDQ; SUB EAX,EDX; SAR 1 — round-toward-zero divide by 2
|
| 841 |
let half_x = ((level_w << 16) + (if level_w < 0 { 1 } else { 0 })) >> 1;
|
| 842 |
let half_y = ((level_h << 16) + (if level_h < 0 { 1 } else { 0 })) >> 1;
|
crates/openwa-core/src/task/game_task.rs
CHANGED
|
@@ -69,8 +69,11 @@ const _: () = assert!(core::mem::size_of::<CGameTask>() == 0xFC);
|
|
| 69 |
pub struct SoundEmitter {
|
| 70 |
/// +0x00: Vtable pointer
|
| 71 |
pub vtable: *const SoundEmitterVTable,
|
| 72 |
-
/// +0x04
|
| 73 |
-
pub _unknown_04:
|
|
|
|
|
|
|
|
|
|
| 74 |
/// +0x0C: Number of active local sounds
|
| 75 |
pub local_sound_count: i32,
|
| 76 |
/// +0x10: Back-pointer to containing CGameTask
|
|
|
|
| 69 |
pub struct SoundEmitter {
|
| 70 |
/// +0x00: Vtable pointer
|
| 71 |
pub vtable: *const SoundEmitterVTable,
|
| 72 |
+
/// +0x04: Unknown field
|
| 73 |
+
pub _unknown_04: u32,
|
| 74 |
+
/// +0x08: Reference count — incremented when an ActiveSoundEntry holds this emitter,
|
| 75 |
+
/// decremented when the entry is released.
|
| 76 |
+
pub local_ref_count: i32,
|
| 77 |
/// +0x0C: Number of active local sounds
|
| 78 |
pub local_sound_count: i32,
|
| 79 |
/// +0x10: Back-pointer to containing CGameTask
|
crates/openwa-core/src/task/worm.rs
CHANGED
|
@@ -123,6 +123,23 @@ impl CTaskWorm {
|
|
| 123 |
let base = this as *mut u8;
|
| 124 |
core::ptr::write(base.add(0x3C) as *mut i32, value);
|
| 125 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
}
|
| 127 |
|
| 128 |
crate::define_addresses! {
|
|
@@ -427,7 +444,7 @@ impl CTaskWorm {
|
|
| 427 |
/// Returns the worm's current state code (lives at offset +0x44, inside
|
| 428 |
/// `base.subclass_data`). See [`WormState`] for known values.
|
| 429 |
pub fn state(&self) -> u32 {
|
| 430 |
-
|
| 431 |
}
|
| 432 |
|
| 433 |
pub fn is_in_state(&self, state: WormState) -> bool {
|
|
|
|
| 123 |
let base = this as *mut u8;
|
| 124 |
core::ptr::write(base.add(0x3C) as *mut i32, value);
|
| 125 |
}
|
| 126 |
+
|
| 127 |
+
/// Fire subtype 1 at CGameTask+0x34 (subclass_data[4..8]).
|
| 128 |
+
/// Air strike temporarily swaps this with `_unknown_190` during position update.
|
| 129 |
+
pub unsafe fn fire_subtype_1(this: *const CTaskWorm) -> i32 {
|
| 130 |
+
i32::from_ne_bytes((&(*this).base.subclass_data)[4..8].try_into().unwrap())
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
/// Set fire subtype 1 without creating `&mut self` (avoids LLVM noalias violations).
|
| 134 |
+
pub unsafe fn set_fire_subtype_1_raw(this: *mut CTaskWorm, value: i32) {
|
| 135 |
+
(&mut (*this).base.subclass_data)[4..8].copy_from_slice(&value.to_ne_bytes());
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
/// Set the action field at CGameTask+0x48 (subclass_data[0x18..0x1C]).
|
| 139 |
+
/// Cleared after air strike completion alongside `_unknown_208` / `_unknown_19x`.
|
| 140 |
+
pub unsafe fn set_action_field_raw(this: *mut CTaskWorm, value: i32) {
|
| 141 |
+
(&mut (*this).base.subclass_data)[0x18..0x1C].copy_from_slice(&value.to_ne_bytes());
|
| 142 |
+
}
|
| 143 |
}
|
| 144 |
|
| 145 |
crate::define_addresses! {
|
|
|
|
| 444 |
/// Returns the worm's current state code (lives at offset +0x44, inside
|
| 445 |
/// `base.subclass_data`). See [`WormState`] for known values.
|
| 446 |
pub fn state(&self) -> u32 {
|
| 447 |
+
u32::from_ne_bytes(self.base.subclass_data[0x14..0x18].try_into().unwrap())
|
| 448 |
}
|
| 449 |
|
| 450 |
pub fn is_in_state(&self, state: WormState) -> bool {
|