refactor: replace sentinel worm pattern with TeamBlockHeader struct
Browse filesIntroduce TeamBlockHeader with named fields (eliminated, alliance,
active_worm, worm_count, weapon_alliance, team_name) and TeamBlockSlot0
union to replace the dual-purpose WormEntry sentinel accessor hacks.
Remove all 6 unsafe sentinel_* methods from WormEntry. Validated at
runtime: 96/96 checks pass.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
crates/openwa-types/src/ddgame.rs
CHANGED
|
@@ -191,7 +191,7 @@ pub mod offsets {
|
|
| 191 |
/// `blocks_ptr = (tws_base as *const u8).sub(ARENA_TO_BLOCKS) as *const FullTeamBlock`
|
| 192 |
///
|
| 193 |
/// entry_ptr(0) = DDGame+0x4628 = TEAM_BLOCKS + 0x598.
|
| 194 |
-
/// 0x598 = sizeof(FullTeamBlock) + 0x7C = one block + offset into
|
| 195 |
pub const ARENA_TO_BLOCKS: usize = 0x598;
|
| 196 |
|
| 197 |
// === FUN_00526120 init offsets (stride 0x194, 10 entries) ===
|
|
@@ -268,11 +268,7 @@ const _: () = assert!(core::mem::size_of::<SoundQueueEntry>() == 0x24);
|
|
| 268 |
/// WA supports up to 8 playable worms per team. The original code accesses
|
| 269 |
/// worms via raw pointer arithmetic from the team entry pointer, using
|
| 270 |
/// stride 0x9C. This means the 8th worm crosses the FullTeamBlock boundary
|
| 271 |
-
/// into the next block's
|
| 272 |
-
///
|
| 273 |
-
/// Slot 0 of each block is dual-purpose: its high-offset fields (+0x6C, +0x70,
|
| 274 |
-
/// +0x74, +0x78) store sentinel/metadata for the team, while its low-offset
|
| 275 |
-
/// fields may hold data for the 8th worm of the previous team (when present).
|
| 276 |
///
|
| 277 |
/// Field offsets confirmed by runtime memory dump (validator DLL):
|
| 278 |
/// - state at 0x00: 0x67 = active/selected, 0x65 = idle, 0x80+ = special
|
|
@@ -280,14 +276,8 @@ const _: () = assert!(core::mem::size_of::<SoundQueueEntry>() == 0x24);
|
|
| 280 |
/// - max_health at 0x58: initial health value (100)
|
| 281 |
/// - health at 0x5C: current health (100 = full)
|
| 282 |
/// - name at 0x78: null-terminated worm name string (~20 bytes)
|
| 283 |
-
///
|
| 284 |
-
/// Sentinel (slot 0) has different layout — see `sentinel_*` methods:
|
| 285 |
-
/// - +0x6C (_unknown_60\[0x0C\]): eliminated flag
|
| 286 |
-
/// - +0x78 (name\[0..4\]): worm_count
|
| 287 |
-
/// - +0x80 (name\[8..12\]): alliance_id
|
| 288 |
-
/// - +0x84 (name\[12..\]): team name string
|
| 289 |
-
/// - +0x98 (_unknown_90\[8..12\]): value 0x190 (400), unknown purpose
|
| 290 |
#[repr(C)]
|
|
|
|
| 291 |
pub struct WormEntry {
|
| 292 |
/// 0x00: Worm state machine state.
|
| 293 |
/// Values: 0x65=idle, 0x67=active/selected, 0x68=active variant.
|
|
@@ -304,11 +294,8 @@ pub struct WormEntry {
|
|
| 304 |
/// 0x5C: Current health. Used by GetTeamTotalHealth.
|
| 305 |
pub health: i32,
|
| 306 |
/// 0x60-0x77: Unknown.
|
| 307 |
-
/// In sentinel: +0x6C = eliminated flag for the team.
|
| 308 |
pub _unknown_60: [u8; 0x18],
|
| 309 |
/// 0x78: Worm name, null-terminated ASCII string (~20 bytes).
|
| 310 |
-
/// In sentinel: +0x78 = worm_count (i32), +0x80 = alliance_id (i32),
|
| 311 |
-
/// +0x84 = team name string.
|
| 312 |
pub name: [u8; 0x18],
|
| 313 |
/// 0x90-0x9B: Unknown (zeroed in runtime dump for playable worms).
|
| 314 |
/// GetWormPosition reads pos_x/pos_y from +0x90/+0x94 via negative entry_ptr
|
|
@@ -319,97 +306,78 @@ pub struct WormEntry {
|
|
| 319 |
|
| 320 |
const _: () = assert!(core::mem::size_of::<WormEntry>() == 0x9C);
|
| 321 |
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
///
|
| 334 |
-
|
| 335 |
-
///
|
| 336 |
-
|
| 337 |
-
///
|
| 338 |
-
pub
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
///
|
| 344 |
-
|
| 345 |
-
///
|
| 346 |
-
|
| 347 |
-
///
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
///
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
///
|
| 357 |
-
/// # Safety
|
| 358 |
-
/// Only valid when called on a sentinel worm (slot 0 of a FullTeamBlock).
|
| 359 |
-
pub unsafe fn sentinel_active_worm(&self) -> i32 {
|
| 360 |
-
*(self._unknown_60.as_ptr().add(0x14) as *const i32)
|
| 361 |
-
}
|
| 362 |
|
| 363 |
-
|
| 364 |
-
///
|
| 365 |
-
/// # Safety
|
| 366 |
-
/// Only valid when called on a sentinel worm (slot 0 of a FullTeamBlock).
|
| 367 |
-
pub unsafe fn set_sentinel_active_worm(&mut self, val: i32) {
|
| 368 |
-
*(self._unknown_60.as_mut_ptr().add(0x14) as *mut i32) = val;
|
| 369 |
-
}
|
| 370 |
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
///
|
| 381 |
-
pub
|
| 382 |
-
|
| 383 |
-
|
| 384 |
}
|
| 385 |
|
| 386 |
-
/// Full per-team data block (0x51C bytes,
|
| 387 |
///
|
| 388 |
-
///
|
| 389 |
-
/// team metadata
|
|
|
|
|
|
|
| 390 |
///
|
| 391 |
/// **Block indexing**: Block 0 is unused (preamble, all zeros). Actual team
|
| 392 |
-
/// data starts at block 1.
|
| 393 |
-
///
|
| 394 |
-
/// block\[N\]'s worm data. So entry_ptr(1) accesses block\[1\]'s worms.
|
| 395 |
-
///
|
| 396 |
-
/// **Worm access**: Playable worms are accessed via raw pointer arithmetic
|
| 397 |
-
/// from the team entry pointer (stride 0x9C). For teams with 8 worms, the
|
| 398 |
-
/// 8th worm crosses the block boundary — its early fields (state, health)
|
| 399 |
-
/// spill into block\[N+1\].worms\[0\], which is also the sentinel. Use
|
| 400 |
-
/// `TeamArenaRef::team_worm()` instead of direct array indexing.
|
| 401 |
///
|
| 402 |
-
/// **
|
| 403 |
-
///
|
| 404 |
-
/// - +0x6C: eliminated flag
|
| 405 |
-
/// - +0x70: alliance_id (Pattern B)
|
| 406 |
-
/// - +0x74: active_worm (Pattern B)
|
| 407 |
-
/// - +0x78: worm_count
|
| 408 |
-
/// - +0x84: team name string
|
| 409 |
#[repr(C)]
|
| 410 |
pub struct FullTeamBlock {
|
| 411 |
-
/// 0x000-
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
| 413 |
/// 0x4E0-0x51B: Team metadata (0x3C bytes)
|
| 414 |
pub _metadata: [u8; 0x3C],
|
| 415 |
}
|
|
@@ -438,8 +406,8 @@ pub struct TeamArenaState {
|
|
| 438 |
/// This region contains 7 team entries at stride 0x51C (1-indexed, index 0
|
| 439 |
/// is preamble). The original WA.exe accesses team data via raw pointer
|
| 440 |
/// arithmetic relative to the arena base. In our Rust code, team data is
|
| 441 |
-
/// accessed through `TeamArenaRef`
|
| 442 |
-
///
|
| 443 |
///
|
| 444 |
/// The 7th entry (team 6, 1-indexed) only has 8 bytes before team_count;
|
| 445 |
/// the rest overlaps with weapon_slots.
|
|
@@ -575,13 +543,13 @@ impl TeamArenaRef {
|
|
| 575 |
self.base.sub(offsets::ARENA_TO_BLOCKS) as *const FullTeamBlock
|
| 576 |
}
|
| 577 |
|
| 578 |
-
/// Get the
|
| 579 |
///
|
| 580 |
-
/// Returns `block[team_idx+1].
|
| 581 |
-
/// worm_count
|
| 582 |
#[inline]
|
| 583 |
-
pub unsafe fn
|
| 584 |
-
&(*self.blocks().add(team_idx + 1)).
|
| 585 |
}
|
| 586 |
|
| 587 |
/// Get a playable worm entry by 1-indexed worm number (1..=8).
|
|
@@ -590,8 +558,8 @@ impl TeamArenaRef {
|
|
| 590 |
/// `base + team_idx * 0x51C + worm_num * 0x9C - 0x598`.
|
| 591 |
/// This naturally crosses FullTeamBlock boundaries when worm_num = 8,
|
| 592 |
/// since the 8th worm's early fields (state, health) spill into the
|
| 593 |
-
/// next block's
|
| 594 |
-
///
|
| 595 |
#[inline]
|
| 596 |
pub unsafe fn team_worm(&self, team_idx: usize, worm_num: usize) -> &WormEntry {
|
| 597 |
let ptr = self.base
|
|
@@ -601,40 +569,40 @@ impl TeamArenaRef {
|
|
| 601 |
&*(ptr as *const WormEntry)
|
| 602 |
}
|
| 603 |
|
| 604 |
-
/// Get a team's
|
| 605 |
///
|
| 606 |
-
/// Returns `(block[team_idx], block[team_idx+1].
|
| 607 |
-
/// The block contains worm data
|
| 608 |
-
///
|
| 609 |
///
|
| 610 |
/// **Note**: For accessing worms, prefer `team_worm()` which handles
|
| 611 |
/// 8-worm teams correctly via cross-boundary pointer arithmetic.
|
| 612 |
#[inline]
|
| 613 |
-
pub unsafe fn
|
| 614 |
let blocks = self.blocks();
|
| 615 |
let block = &*blocks.add(team_idx);
|
| 616 |
-
let
|
| 617 |
-
(block,
|
| 618 |
}
|
| 619 |
|
| 620 |
-
/// Get
|
| 621 |
///
|
| 622 |
/// Pattern B indexes from block[i+2] for 0-indexed team `i`:
|
| 623 |
-
/// `base + 0x510 + i*0x51C` = `blocks[i+2].
|
| 624 |
#[inline]
|
| 625 |
-
pub unsafe fn
|
| 626 |
-
&(*self.blocks().add(team_idx + 2)).
|
| 627 |
}
|
| 628 |
|
| 629 |
-
/// Get mutable
|
| 630 |
#[inline]
|
| 631 |
-
pub unsafe fn
|
| 632 |
-
&mut (*(self.blocks() as *mut FullTeamBlock).add(team_idx + 2)).
|
| 633 |
}
|
| 634 |
|
| 635 |
/// Compute the flat index for ammo/delay table access.
|
| 636 |
///
|
| 637 |
-
/// Reads the weapon alliance ID from the
|
| 638 |
/// 1-indexed team, then computes `alliance_id * 142 + weapon_id`.
|
| 639 |
///
|
| 640 |
/// The weapon_slots array is interleaved: per alliance, 71 ammo slots
|
|
@@ -643,7 +611,7 @@ impl TeamArenaRef {
|
|
| 643 |
/// Delay: `weapon_slots[alliance_id * 142 + 71 + weapon_id]`
|
| 644 |
#[inline]
|
| 645 |
pub unsafe fn ammo_index(&self, team_index: usize, weapon_id: u32) -> usize {
|
| 646 |
-
let alliance_id = self.
|
| 647 |
alliance_id * 142 + weapon_id as usize
|
| 648 |
}
|
| 649 |
}
|
|
|
|
| 191 |
/// `blocks_ptr = (tws_base as *const u8).sub(ARENA_TO_BLOCKS) as *const FullTeamBlock`
|
| 192 |
///
|
| 193 |
/// entry_ptr(0) = DDGame+0x4628 = TEAM_BLOCKS + 0x598.
|
| 194 |
+
/// 0x598 = sizeof(FullTeamBlock) + 0x7C = one block + offset into TeamBlockHeader.
|
| 195 |
pub const ARENA_TO_BLOCKS: usize = 0x598;
|
| 196 |
|
| 197 |
// === FUN_00526120 init offsets (stride 0x194, 10 entries) ===
|
|
|
|
| 268 |
/// WA supports up to 8 playable worms per team. The original code accesses
|
| 269 |
/// worms via raw pointer arithmetic from the team entry pointer, using
|
| 270 |
/// stride 0x9C. This means the 8th worm crosses the FullTeamBlock boundary
|
| 271 |
+
/// into the next block's header slot — see `TeamArenaRef::team_worm()`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
///
|
| 273 |
/// Field offsets confirmed by runtime memory dump (validator DLL):
|
| 274 |
/// - state at 0x00: 0x67 = active/selected, 0x65 = idle, 0x80+ = special
|
|
|
|
| 276 |
/// - max_health at 0x58: initial health value (100)
|
| 277 |
/// - health at 0x5C: current health (100 = full)
|
| 278 |
/// - name at 0x78: null-terminated worm name string (~20 bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
#[repr(C)]
|
| 280 |
+
#[derive(Clone, Copy)]
|
| 281 |
pub struct WormEntry {
|
| 282 |
/// 0x00: Worm state machine state.
|
| 283 |
/// Values: 0x65=idle, 0x67=active/selected, 0x68=active variant.
|
|
|
|
| 294 |
/// 0x5C: Current health. Used by GetTeamTotalHealth.
|
| 295 |
pub health: i32,
|
| 296 |
/// 0x60-0x77: Unknown.
|
|
|
|
| 297 |
pub _unknown_60: [u8; 0x18],
|
| 298 |
/// 0x78: Worm name, null-terminated ASCII string (~20 bytes).
|
|
|
|
|
|
|
| 299 |
pub name: [u8; 0x18],
|
| 300 |
/// 0x90-0x9B: Unknown (zeroed in runtime dump for playable worms).
|
| 301 |
/// GetWormPosition reads pos_x/pos_y from +0x90/+0x94 via negative entry_ptr
|
|
|
|
| 306 |
|
| 307 |
const _: () = assert!(core::mem::size_of::<WormEntry>() == 0x9C);
|
| 308 |
|
| 309 |
+
/// Team-level metadata stored at slot 0 of each FullTeamBlock (0x9C bytes).
|
| 310 |
+
///
|
| 311 |
+
/// This struct overlays the same memory as a WormEntry but interprets the
|
| 312 |
+
/// high offsets (0x6C+) as team metadata rather than worm data. The low
|
| 313 |
+
/// offsets (0x00-0x5F) may contain data from the previous team's 8th worm
|
| 314 |
+
/// when that team has 8 worms — they are treated as opaque padding.
|
| 315 |
+
///
|
| 316 |
+
/// Accessed via `TeamArenaRef::team_header()` and `team_header_b()`.
|
| 317 |
+
#[repr(C)]
|
| 318 |
+
#[derive(Clone, Copy)]
|
| 319 |
+
pub struct TeamBlockHeader {
|
| 320 |
+
/// 0x00-0x5F: Opaque — may hold 8th worm data from previous team.
|
| 321 |
+
pub _worm_overlap: [u8; 0x60],
|
| 322 |
+
/// 0x60-0x6B: Unknown padding.
|
| 323 |
+
pub _unknown_60: [u8; 0x0C],
|
| 324 |
+
/// 0x6C: Team eliminated flag (nonzero = eliminated).
|
| 325 |
+
pub eliminated: i32,
|
| 326 |
+
/// 0x70: Alliance ID used by CountTeamsByAlliance and SetActiveWorm_Maybe.
|
| 327 |
+
pub alliance: i32,
|
| 328 |
+
/// 0x74: Active worm index (0 = none, N = worm N is active).
|
| 329 |
+
pub active_worm: i32,
|
| 330 |
+
/// 0x78: Number of worms on this team.
|
| 331 |
+
pub worm_count: i32,
|
| 332 |
+
/// 0x7C: Unknown.
|
| 333 |
+
pub _unknown_7c: [u8; 4],
|
| 334 |
+
/// 0x80: Alliance ID for ammo/delay table indexing (GetAmmo/AddAmmo/SubtractAmmo).
|
| 335 |
+
/// Teams with the same weapon_alliance share ammo pools. Distinct from
|
| 336 |
+
/// `alliance` at 0x70 which is used by CountTeamsByAlliance.
|
| 337 |
+
pub weapon_alliance: i32,
|
| 338 |
+
/// 0x84: Team name, null-terminated ASCII string.
|
| 339 |
+
pub team_name: [u8; 0x14],
|
| 340 |
+
/// 0x98: Unknown trailing bytes.
|
| 341 |
+
pub _unknown_98: [u8; 4],
|
| 342 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
+
const _: () = assert!(core::mem::size_of::<TeamBlockHeader>() == 0x9C);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
+
/// Union for slot 0 of a FullTeamBlock.
|
| 347 |
+
///
|
| 348 |
+
/// This slot is dual-purpose: its high offsets store team metadata
|
| 349 |
+
/// (`TeamBlockHeader`), while its low offsets may contain the 8th worm
|
| 350 |
+
/// of the previous team (`WormEntry`). The two uses don't conflict
|
| 351 |
+
/// because worm data occupies 0x00-0x5F and header data starts at 0x6C.
|
| 352 |
+
#[repr(C)]
|
| 353 |
+
#[derive(Clone, Copy)]
|
| 354 |
+
pub union TeamBlockSlot0 {
|
| 355 |
+
/// View as worm data (used when the previous team has 8 worms).
|
| 356 |
+
pub worm: core::mem::ManuallyDrop<WormEntry>,
|
| 357 |
+
/// View as team metadata (eliminated, alliance, worm_count, etc.).
|
| 358 |
+
pub team: core::mem::ManuallyDrop<TeamBlockHeader>,
|
| 359 |
}
|
| 360 |
|
| 361 |
+
/// Full per-team data block (0x51C bytes, 7 blocks in DDGame).
|
| 362 |
///
|
| 363 |
+
/// Each block starts with a `TeamBlockSlot0` union (0x9C bytes) that serves
|
| 364 |
+
/// dual purpose: its high offsets hold team metadata (`TeamBlockHeader`),
|
| 365 |
+
/// while its low offsets may contain the 8th worm of the previous team.
|
| 366 |
+
/// The remaining 7 worm slots follow, then 0x3C bytes of metadata.
|
| 367 |
///
|
| 368 |
/// **Block indexing**: Block 0 is unused (preamble, all zeros). Actual team
|
| 369 |
+
/// data starts at block 1. Worms are accessed via `TeamArenaRef::team_worm()`
|
| 370 |
+
/// which uses raw pointer arithmetic and naturally crosses block boundaries.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
///
|
| 372 |
+
/// **Header access**: Use `TeamArenaRef::team_header(idx)` to get the
|
| 373 |
+
/// `TeamBlockHeader` for a team (reads from `blocks[idx+1].header.team`).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
#[repr(C)]
|
| 375 |
pub struct FullTeamBlock {
|
| 376 |
+
/// 0x000-0x09B: Header slot (union of TeamBlockHeader and WormEntry).
|
| 377 |
+
/// Team metadata at high offsets; may hold 8th worm data at low offsets.
|
| 378 |
+
pub header: TeamBlockSlot0,
|
| 379 |
+
/// 0x09C-0x4DF: 7 worm entries (slots 1-7, stride 0x9C)
|
| 380 |
+
pub worms: [WormEntry; 7],
|
| 381 |
/// 0x4E0-0x51B: Team metadata (0x3C bytes)
|
| 382 |
pub _metadata: [u8; 0x3C],
|
| 383 |
}
|
|
|
|
| 406 |
/// This region contains 7 team entries at stride 0x51C (1-indexed, index 0
|
| 407 |
/// is preamble). The original WA.exe accesses team data via raw pointer
|
| 408 |
/// arithmetic relative to the arena base. In our Rust code, team data is
|
| 409 |
+
/// accessed through `TeamArenaRef`, `FullTeamBlock`, and `TeamBlockHeader`,
|
| 410 |
+
/// so this region is treated as opaque padding.
|
| 411 |
///
|
| 412 |
/// The 7th entry (team 6, 1-indexed) only has 8 bytes before team_count;
|
| 413 |
/// the rest overlaps with weapon_slots.
|
|
|
|
| 543 |
self.base.sub(offsets::ARENA_TO_BLOCKS) as *const FullTeamBlock
|
| 544 |
}
|
| 545 |
|
| 546 |
+
/// Get the team header (metadata) for a team.
|
| 547 |
///
|
| 548 |
+
/// Returns `&block[team_idx+1].header.team`, which holds team metadata:
|
| 549 |
+
/// worm_count, eliminated flag, weapon_alliance, team_name.
|
| 550 |
#[inline]
|
| 551 |
+
pub unsafe fn team_header(&self, team_idx: usize) -> &TeamBlockHeader {
|
| 552 |
+
&(*self.blocks().add(team_idx + 1)).header.team
|
| 553 |
}
|
| 554 |
|
| 555 |
/// Get a playable worm entry by 1-indexed worm number (1..=8).
|
|
|
|
| 558 |
/// `base + team_idx * 0x51C + worm_num * 0x9C - 0x598`.
|
| 559 |
/// This naturally crosses FullTeamBlock boundaries when worm_num = 8,
|
| 560 |
/// since the 8th worm's early fields (state, health) spill into the
|
| 561 |
+
/// next block's header slot. The header metadata lives at high offsets
|
| 562 |
+
/// (0x6C+) that don't conflict with worm data (0x00-0x5F).
|
| 563 |
#[inline]
|
| 564 |
pub unsafe fn team_worm(&self, team_idx: usize, worm_num: usize) -> &WormEntry {
|
| 565 |
let ptr = self.base
|
|
|
|
| 569 |
&*(ptr as *const WormEntry)
|
| 570 |
}
|
| 571 |
|
| 572 |
+
/// Get a team's block and its header in one call.
|
| 573 |
///
|
| 574 |
+
/// Returns `(block[team_idx], &block[team_idx+1].header.team)`.
|
| 575 |
+
/// The block contains worm data, and the header (slot 0 of the next block)
|
| 576 |
+
/// holds team metadata (worm_count, eliminated flag).
|
| 577 |
///
|
| 578 |
/// **Note**: For accessing worms, prefer `team_worm()` which handles
|
| 579 |
/// 8-worm teams correctly via cross-boundary pointer arithmetic.
|
| 580 |
#[inline]
|
| 581 |
+
pub unsafe fn team_and_header(&self, team_idx: usize) -> (&FullTeamBlock, &TeamBlockHeader) {
|
| 582 |
let blocks = self.blocks();
|
| 583 |
let block = &*blocks.add(team_idx);
|
| 584 |
+
let header = &(*blocks.add(team_idx + 1)).header.team;
|
| 585 |
+
(block, header)
|
| 586 |
}
|
| 587 |
|
| 588 |
+
/// Get team header for Pattern B access (alliance/active_worm at +0x70/+0x74).
|
| 589 |
///
|
| 590 |
/// Pattern B indexes from block[i+2] for 0-indexed team `i`:
|
| 591 |
+
/// `base + 0x510 + i*0x51C` = `blocks[i+2].header.team + 0x70`.
|
| 592 |
#[inline]
|
| 593 |
+
pub unsafe fn team_header_b(&self, team_idx: usize) -> &TeamBlockHeader {
|
| 594 |
+
&(*self.blocks().add(team_idx + 2)).header.team
|
| 595 |
}
|
| 596 |
|
| 597 |
+
/// Get mutable team header for Pattern B access.
|
| 598 |
#[inline]
|
| 599 |
+
pub unsafe fn team_header_b_mut(&self, team_idx: usize) -> &mut TeamBlockHeader {
|
| 600 |
+
&mut (*(self.blocks() as *mut FullTeamBlock).add(team_idx + 2)).header.team
|
| 601 |
}
|
| 602 |
|
| 603 |
/// Compute the flat index for ammo/delay table access.
|
| 604 |
///
|
| 605 |
+
/// Reads the weapon alliance ID from the team header for the given
|
| 606 |
/// 1-indexed team, then computes `alliance_id * 142 + weapon_id`.
|
| 607 |
///
|
| 608 |
/// The weapon_slots array is interleaved: per alliance, 71 ammo slots
|
|
|
|
| 611 |
/// Delay: `weapon_slots[alliance_id * 142 + 71 + weapon_id]`
|
| 612 |
#[inline]
|
| 613 |
pub unsafe fn ammo_index(&self, team_index: usize, weapon_id: u32) -> usize {
|
| 614 |
+
let alliance_id = self.team_header(team_index).weapon_alliance as usize;
|
| 615 |
alliance_id * 142 + weapon_id as usize
|
| 616 |
}
|
| 617 |
}
|
crates/openwa-wormkit/src/replacements/team.rs
CHANGED
|
@@ -21,8 +21,8 @@ use crate::hook::{self, usercall_trampoline};
|
|
| 21 |
// __usercall: EAX = base, EDI = alliance_id
|
| 22 |
// plain RET
|
| 23 |
//
|
| 24 |
-
// NOTE: This function uses Pattern B
|
| 25 |
-
// alliance and active_worm, accessed via
|
| 26 |
|
| 27 |
unsafe extern "cdecl" fn count_teams_by_alliance_impl(arena: TeamArenaRef, alliance_id: i32) {
|
| 28 |
let state = arena.state_mut();
|
|
@@ -32,9 +32,9 @@ unsafe extern "cdecl" fn count_teams_by_alliance_impl(arena: TeamArenaRef, allia
|
|
| 32 |
state.enemy_team_count = 0;
|
| 33 |
|
| 34 |
for i in 0..state.team_count as usize {
|
| 35 |
-
let
|
| 36 |
-
let team_alliance =
|
| 37 |
-
let active_worm =
|
| 38 |
if active_worm != 0 && team_alliance >= 0 {
|
| 39 |
state.active_team_count += 1;
|
| 40 |
if team_alliance == alliance_id {
|
|
@@ -56,13 +56,13 @@ usercall_trampoline!(fn trampoline_count_teams_by_alliance; impl_fn = count_team
|
|
| 56 |
// plain RET, returns EAX = total health
|
| 57 |
|
| 58 |
unsafe extern "cdecl" fn get_team_total_health_impl(team_index: u32, arena: TeamArenaRef) -> i32 {
|
| 59 |
-
let
|
| 60 |
|
| 61 |
-
if
|
| 62 |
return 0;
|
| 63 |
}
|
| 64 |
|
| 65 |
-
let worm_count =
|
| 66 |
let mut total = 0i32;
|
| 67 |
for w in 1..=worm_count as usize {
|
| 68 |
total += arena.team_worm(team_index as usize, w).health;
|
|
@@ -125,11 +125,11 @@ unsafe extern "cdecl" fn check_worm_state_0x64_impl(arena: TeamArenaRef) -> u32
|
|
| 125 |
|
| 126 |
// Iterate real teams (1-indexed: team 1..=team_count)
|
| 127 |
for i in 1..=state.team_count as usize {
|
| 128 |
-
let
|
| 129 |
-
if
|
| 130 |
continue;
|
| 131 |
}
|
| 132 |
-
let worm_count =
|
| 133 |
for w in 1..=worm_count as usize {
|
| 134 |
if arena.team_worm(i, w).state == 0x64 {
|
| 135 |
return 1;
|
|
@@ -151,13 +151,13 @@ usercall_trampoline!(fn trampoline_check_worm_state_0x64; impl_fn = check_worm_s
|
|
| 151 |
// Per-team version of CheckWormState0x64. 1 xref (FUN_00556ad0).
|
| 152 |
|
| 153 |
unsafe extern "cdecl" fn check_team_worm_state_0x64_impl(arena: TeamArenaRef, team_idx: u32) -> u32 {
|
| 154 |
-
let
|
| 155 |
|
| 156 |
-
if
|
| 157 |
return 0;
|
| 158 |
}
|
| 159 |
|
| 160 |
-
let worm_count =
|
| 161 |
for w in 1..=worm_count as usize {
|
| 162 |
if arena.team_worm(team_idx as usize, w).state == 0x64 {
|
| 163 |
return 1;
|
|
@@ -181,11 +181,11 @@ unsafe extern "cdecl" fn check_any_worm_state_0x8b_impl(arena: TeamArenaRef) ->
|
|
| 181 |
let state = arena.state();
|
| 182 |
|
| 183 |
for i in 1..=state.team_count as usize {
|
| 184 |
-
let
|
| 185 |
-
if
|
| 186 |
continue;
|
| 187 |
}
|
| 188 |
-
let worm_count =
|
| 189 |
for w in 1..=worm_count as usize {
|
| 190 |
if arena.team_worm(i, w).state == 0x8b {
|
| 191 |
return 1;
|
|
@@ -211,15 +211,15 @@ usercall_trampoline!(fn trampoline_check_any_worm_state_0x8b; impl_fn = check_an
|
|
| 211 |
unsafe extern "cdecl" fn set_active_worm_impl(arena: TeamArenaRef, team_idx: u32, worm_index: i32) {
|
| 212 |
let state = arena.state_mut();
|
| 213 |
|
| 214 |
-
// Pattern B
|
| 215 |
-
// team_idx=1 →
|
| 216 |
-
let
|
| 217 |
|
| 218 |
if worm_index == 0 {
|
| 219 |
// Deactivate team
|
| 220 |
-
if
|
| 221 |
state.active_worm_count -= 1;
|
| 222 |
-
let alliance =
|
| 223 |
if alliance >= 0 {
|
| 224 |
state.active_team_count -= 1;
|
| 225 |
if alliance == state.current_alliance {
|
|
@@ -228,14 +228,14 @@ unsafe extern "cdecl" fn set_active_worm_impl(arena: TeamArenaRef, team_idx: u32
|
|
| 228 |
state.enemy_team_count -= 1;
|
| 229 |
}
|
| 230 |
}
|
| 231 |
-
arena.
|
| 232 |
}
|
| 233 |
} else {
|
| 234 |
// Activate team — only update counters if not already active
|
| 235 |
-
if
|
| 236 |
state.active_worm_count += 1;
|
| 237 |
state.last_active_team = team_idx as i32;
|
| 238 |
-
let alliance =
|
| 239 |
state.last_active_alliance = alliance;
|
| 240 |
if alliance >= 0 {
|
| 241 |
state.active_team_count += 1;
|
|
@@ -247,7 +247,7 @@ unsafe extern "cdecl" fn set_active_worm_impl(arena: TeamArenaRef, team_idx: u32
|
|
| 247 |
}
|
| 248 |
}
|
| 249 |
// Always write worm_index (original writes ESI unconditionally)
|
| 250 |
-
arena.
|
| 251 |
}
|
| 252 |
}
|
| 253 |
|
|
|
|
| 21 |
// __usercall: EAX = base, EDI = alliance_id
|
| 22 |
// plain RET
|
| 23 |
//
|
| 24 |
+
// NOTE: This function uses Pattern B header fields (+0x70/+0x74) for
|
| 25 |
+
// alliance and active_worm, accessed via team_header_b().
|
| 26 |
|
| 27 |
unsafe extern "cdecl" fn count_teams_by_alliance_impl(arena: TeamArenaRef, alliance_id: i32) {
|
| 28 |
let state = arena.state_mut();
|
|
|
|
| 32 |
state.enemy_team_count = 0;
|
| 33 |
|
| 34 |
for i in 0..state.team_count as usize {
|
| 35 |
+
let header = arena.team_header_b(i);
|
| 36 |
+
let team_alliance = header.alliance;
|
| 37 |
+
let active_worm = header.active_worm;
|
| 38 |
if active_worm != 0 && team_alliance >= 0 {
|
| 39 |
state.active_team_count += 1;
|
| 40 |
if team_alliance == alliance_id {
|
|
|
|
| 56 |
// plain RET, returns EAX = total health
|
| 57 |
|
| 58 |
unsafe extern "cdecl" fn get_team_total_health_impl(team_index: u32, arena: TeamArenaRef) -> i32 {
|
| 59 |
+
let header = arena.team_header(team_index as usize);
|
| 60 |
|
| 61 |
+
if header.eliminated != 0 {
|
| 62 |
return 0;
|
| 63 |
}
|
| 64 |
|
| 65 |
+
let worm_count = header.worm_count;
|
| 66 |
let mut total = 0i32;
|
| 67 |
for w in 1..=worm_count as usize {
|
| 68 |
total += arena.team_worm(team_index as usize, w).health;
|
|
|
|
| 125 |
|
| 126 |
// Iterate real teams (1-indexed: team 1..=team_count)
|
| 127 |
for i in 1..=state.team_count as usize {
|
| 128 |
+
let header = arena.team_header(i);
|
| 129 |
+
if header.eliminated != 0 {
|
| 130 |
continue;
|
| 131 |
}
|
| 132 |
+
let worm_count = header.worm_count;
|
| 133 |
for w in 1..=worm_count as usize {
|
| 134 |
if arena.team_worm(i, w).state == 0x64 {
|
| 135 |
return 1;
|
|
|
|
| 151 |
// Per-team version of CheckWormState0x64. 1 xref (FUN_00556ad0).
|
| 152 |
|
| 153 |
unsafe extern "cdecl" fn check_team_worm_state_0x64_impl(arena: TeamArenaRef, team_idx: u32) -> u32 {
|
| 154 |
+
let header = arena.team_header(team_idx as usize);
|
| 155 |
|
| 156 |
+
if header.eliminated != 0 {
|
| 157 |
return 0;
|
| 158 |
}
|
| 159 |
|
| 160 |
+
let worm_count = header.worm_count;
|
| 161 |
for w in 1..=worm_count as usize {
|
| 162 |
if arena.team_worm(team_idx as usize, w).state == 0x64 {
|
| 163 |
return 1;
|
|
|
|
| 181 |
let state = arena.state();
|
| 182 |
|
| 183 |
for i in 1..=state.team_count as usize {
|
| 184 |
+
let header = arena.team_header(i);
|
| 185 |
+
if header.eliminated != 0 {
|
| 186 |
continue;
|
| 187 |
}
|
| 188 |
+
let worm_count = header.worm_count;
|
| 189 |
for w in 1..=worm_count as usize {
|
| 190 |
if arena.team_worm(i, w).state == 0x8b {
|
| 191 |
return 1;
|
|
|
|
| 211 |
unsafe extern "cdecl" fn set_active_worm_impl(arena: TeamArenaRef, team_idx: u32, worm_index: i32) {
|
| 212 |
let state = arena.state_mut();
|
| 213 |
|
| 214 |
+
// Pattern B header: team_idx is 1-indexed, team_header_b is 0-indexed
|
| 215 |
+
// team_idx=1 → team_header_b(0) → blocks[2].header
|
| 216 |
+
let header = arena.team_header_b(team_idx as usize - 1);
|
| 217 |
|
| 218 |
if worm_index == 0 {
|
| 219 |
// Deactivate team
|
| 220 |
+
if header.active_worm != 0 {
|
| 221 |
state.active_worm_count -= 1;
|
| 222 |
+
let alliance = header.alliance;
|
| 223 |
if alliance >= 0 {
|
| 224 |
state.active_team_count -= 1;
|
| 225 |
if alliance == state.current_alliance {
|
|
|
|
| 228 |
state.enemy_team_count -= 1;
|
| 229 |
}
|
| 230 |
}
|
| 231 |
+
arena.team_header_b_mut(team_idx as usize - 1).active_worm = 0;
|
| 232 |
}
|
| 233 |
} else {
|
| 234 |
// Activate team — only update counters if not already active
|
| 235 |
+
if header.active_worm == 0 {
|
| 236 |
state.active_worm_count += 1;
|
| 237 |
state.last_active_team = team_idx as i32;
|
| 238 |
+
let alliance = header.alliance;
|
| 239 |
state.last_active_alliance = alliance;
|
| 240 |
if alliance >= 0 {
|
| 241 |
state.active_team_count += 1;
|
|
|
|
| 247 |
}
|
| 248 |
}
|
| 249 |
// Always write worm_index (original writes ESI unconditionally)
|
| 250 |
+
arena.team_header_b_mut(team_idx as usize - 1).active_worm = worm_index;
|
| 251 |
}
|
| 252 |
}
|
| 253 |
|
crates/openwa-wormkit/src/replacements/weapon.rs
CHANGED
|
@@ -94,8 +94,8 @@ usercall_trampoline!(fn trampoline_get_ammo; impl_fn = get_ammo_impl;
|
|
| 94 |
// plain RET, returns EAX = bool (1 if >1 worm alive on team)
|
| 95 |
|
| 96 |
unsafe extern "cdecl" fn count_alive_worms_impl(team_index: u32, arena: TeamArenaRef) -> u32 {
|
| 97 |
-
let
|
| 98 |
-
let worm_count =
|
| 99 |
let mut alive = 0i32;
|
| 100 |
for w in 1..=worm_count as usize {
|
| 101 |
if arena.team_worm(team_index as usize, w).health > 0 {
|
|
|
|
| 94 |
// plain RET, returns EAX = bool (1 if >1 worm alive on team)
|
| 95 |
|
| 96 |
unsafe extern "cdecl" fn count_alive_worms_impl(team_index: u32, arena: TeamArenaRef) -> u32 {
|
| 97 |
+
let header = arena.team_header(team_index as usize);
|
| 98 |
+
let worm_count = header.worm_count;
|
| 99 |
let mut alive = 0i32;
|
| 100 |
for w in 1..=worm_count as usize {
|
| 101 |
if arena.team_worm(team_index as usize, w).health > 0 {
|
crates/openwa-wormkit/src/validation/mod.rs
CHANGED
|
@@ -445,25 +445,29 @@ fn dump_team_blocks() {
|
|
| 445 |
b, blocks_addr + b * 0x51C));
|
| 446 |
|
| 447 |
if (b + 1) < 7 {
|
| 448 |
-
let
|
| 449 |
-
let worm_count =
|
| 450 |
-
let eliminated =
|
| 451 |
|
| 452 |
let entry_ptr = tws_base.add(b as usize * 0x51C);
|
| 453 |
let raw_worm_count = *(entry_ptr.sub(4) as *const i32);
|
| 454 |
let raw_alliance = *(entry_ptr.add(4) as *const i32);
|
| 455 |
|
| 456 |
result.check(
|
| 457 |
-
&format!("block[{}]
|
| 458 |
worm_count == raw_worm_count,
|
| 459 |
&format!("struct={}, raw={}", worm_count, raw_worm_count),
|
| 460 |
);
|
| 461 |
|
| 462 |
-
let _ = log_validation(&format!("
|
| 463 |
worm_count, eliminated, raw_alliance));
|
| 464 |
|
| 465 |
for w in 0..8usize {
|
| 466 |
-
let worm =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
let active = worm.active_flag;
|
| 468 |
let name_bytes = &worm.name;
|
| 469 |
let name_len = name_bytes.iter().position(|&c| c == 0).unwrap_or(name_bytes.len());
|
|
@@ -496,7 +500,7 @@ fn dump_team_blocks() {
|
|
| 496 |
}
|
| 497 |
|
| 498 |
if worm_count > 0 {
|
| 499 |
-
let struct_state = block.worms[
|
| 500 |
let raw_state = *(entry_ptr.sub(0x598).add(0x9C) as *const u32);
|
| 501 |
result.check(
|
| 502 |
&format!("block[{}] worm[1].state (struct vs raw)", b),
|
|
|
|
| 445 |
b, blocks_addr + b * 0x51C));
|
| 446 |
|
| 447 |
if (b + 1) < 7 {
|
| 448 |
+
let header = &(*blocks.add(b as usize + 1)).header.team;
|
| 449 |
+
let worm_count = header.worm_count;
|
| 450 |
+
let eliminated = header.eliminated;
|
| 451 |
|
| 452 |
let entry_ptr = tws_base.add(b as usize * 0x51C);
|
| 453 |
let raw_worm_count = *(entry_ptr.sub(4) as *const i32);
|
| 454 |
let raw_alliance = *(entry_ptr.add(4) as *const i32);
|
| 455 |
|
| 456 |
result.check(
|
| 457 |
+
&format!("block[{}] header.worm_count vs raw", b),
|
| 458 |
worm_count == raw_worm_count,
|
| 459 |
&format!("struct={}, raw={}", worm_count, raw_worm_count),
|
| 460 |
);
|
| 461 |
|
| 462 |
+
let _ = log_validation(&format!(" header: worm_count={}, eliminated={}, alliance(entry_ptr+4)={}",
|
| 463 |
worm_count, eliminated, raw_alliance));
|
| 464 |
|
| 465 |
for w in 0..8usize {
|
| 466 |
+
let worm = if w == 0 {
|
| 467 |
+
&*block.header.worm
|
| 468 |
+
} else {
|
| 469 |
+
&block.worms[w - 1]
|
| 470 |
+
};
|
| 471 |
let active = worm.active_flag;
|
| 472 |
let name_bytes = &worm.name;
|
| 473 |
let name_len = name_bytes.iter().position(|&c| c == 0).unwrap_or(name_bytes.len());
|
|
|
|
| 500 |
}
|
| 501 |
|
| 502 |
if worm_count > 0 {
|
| 503 |
+
let struct_state = block.worms[0].state;
|
| 504 |
let raw_state = *(entry_ptr.sub(0x598).add(0x9C) as *const u32);
|
| 505 |
result.check(
|
| 506 |
&format!("block[{}] worm[1].state (struct vs raw)", b),
|