paavohtl Claude Opus 4.6 (1M context) commited on
Commit
9f3cb75
·
1 Parent(s): 86781eb

refactor: type check_weapon_avail, add CoordList struct, name DDGame/GameInfo fields

Browse files

Replace raw pointer arithmetic in check_weapon_avail with typed field
access (*mut DDGame + *mut GameInfo). Add CoordList and CoordListEntry
structs for the dynamic terrain coordinate array at DDGame+0x50C.

New DDGame fields carved from _unknown_7d84:
- weapon_restriction_active (0x7E25), version_flag_1/2/3 (0x7E2E-3F),
hurry_flag (0x7E40)

New GameInfo fields (7 weapon-related):
- num_teams (0x00), weapon_avail_threshold (0xD932),
super_weapon_allowed (0xD93C), weapon_36_disabled (0xD94C),
weapon_index_offset (0xD956), weapon_version_gate (0xD959),
net_weapon_exception (0xD9A2), weapon_42_mode (0xDB08)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

crates/openwa-core/src/engine/ddgame.rs CHANGED
@@ -94,8 +94,8 @@ pub struct DDGame {
94
  pub sprite_regions: [*mut u8; 8],
95
  /// 0x48C-0x508: Arrow collision region pointers (32 entries)
96
  pub arrow_collision_regions: [*mut u8; 32],
97
- /// 0x50C: Coordinate list object (capacity 600, 0x12C0 data buffer)
98
- pub coord_list: *mut u8,
99
  /// 0x510: Weapon table pointer
100
  pub weapon_table: *mut u8,
101
  /// 0x514: Unknown pointer (populated at runtime)
@@ -279,8 +279,26 @@ pub struct DDGame {
279
  /// Cleared by DSSound_LoadAllSpeechBanks (0x571A70), filled by DDGameWrapper__LoadSpeechWAV (0x571530).
280
  pub speech_slot_table: SpeechSlotTable,
281
 
282
- /// 0x7D84-0x7E9F: Unknown
283
- pub _unknown_7d84: [u8; 0x7EA0 - 0x7D84],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
 
285
  /// 0x7EA0: Flag/counter (value 4 at runtime — likely team count).
286
  pub field_7ea0: u32,
@@ -1094,39 +1112,39 @@ unsafe fn init_graphics_and_resources(
1094
 
1095
  // ── CoordList at DDGame+0x50C (capacity 600, 0x12C0 buffer) ──
1096
  {
1097
- let cl = wa_malloc(12) as *mut u32;
1098
- *cl = 0; // count
1099
- *cl.add(1) = 600; // capacity
1100
- let data = wa_malloc_zeroed(0x12C0);
1101
- *cl.add(2) = data as u32;
1102
- (*ddgame).coord_list = cl as *mut u8;
 
1103
 
1104
  // Populate coord_list from PCLandscape's coordinate output.
1105
  // landscape_temp[0] = coordinate count, landscape_coords_buf = pairs of (x, y).
1106
  // Original packs as: coord = x * 0x10000 + y (Fixed-point).
1107
- // Each coord_list entry is 8 bytes: [coord_value, 1]. Duplicates are skipped.
1108
  let coord_count = landscape_temp[0];
1109
- let data_ptr = data as *mut u32;
1110
  for j in 0..coord_count as usize {
1111
  let x = landscape_coords_buf[j * 2];
1112
  let y = landscape_coords_buf[j * 2 + 1];
1113
  let coord_val = x.wrapping_mul(0x10000).wrapping_add(y);
1114
- let cur_count = *cl as usize;
1115
  if cur_count >= 600 {
1116
  break;
1117
  }
1118
  // Check for duplicates
1119
  let mut dup = false;
1120
  for k in 0..cur_count {
1121
- if *data_ptr.add(k * 2) == coord_val {
1122
  dup = true;
1123
  break;
1124
  }
1125
  }
1126
  if !dup {
1127
- *data_ptr.add(cur_count * 2) = coord_val;
1128
- *data_ptr.add(cur_count * 2 + 1) = 1;
1129
- *cl = (cur_count + 1) as u32;
1130
  }
1131
  }
1132
  }
@@ -1542,6 +1560,38 @@ pub struct RenderEntry {
1542
  }
1543
  const _: () = assert!(core::mem::size_of::<RenderEntry>() == 0x14);
1544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1545
  // ============================================================
1546
  // Sound queue entry — 16 entries at DDGame + 0x7F00
1547
  // ============================================================
 
94
  pub sprite_regions: [*mut u8; 8],
95
  /// 0x48C-0x508: Arrow collision region pointers (32 entries)
96
  pub arrow_collision_regions: [*mut u8; 32],
97
+ /// 0x50C: Coordinate list — dynamic array of packed (x,y) terrain coords.
98
+ pub coord_list: *mut CoordList,
99
  /// 0x510: Weapon table pointer
100
  pub weapon_table: *mut u8,
101
  /// 0x514: Unknown pointer (populated at runtime)
 
279
  /// Cleared by DSSound_LoadAllSpeechBanks (0x571A70), filled by DDGameWrapper__LoadSpeechWAV (0x571530).
280
  pub speech_slot_table: SpeechSlotTable,
281
 
282
+ /// 0x7D84-0x7E24: Unknown
283
+ pub _unknown_7d84: [u8; 0x7E25 - 0x7D84],
284
+ /// 0x7E25: Weapon restriction active flag (byte).
285
+ /// When nonzero, specific weapons (0x19 area) are disabled.
286
+ pub weapon_restriction_active: u8,
287
+ /// 0x7E26-0x7E2D: Unknown
288
+ pub _unknown_7e26: [u8; 0x7E2E - 0x7E26],
289
+ /// 0x7E2E: Version flag byte 1 (set by InitVersionFlags).
290
+ pub version_flag_1: u8,
291
+ /// 0x7E2F: Version flag byte 2 (set by InitVersionFlags).
292
+ pub version_flag_2: u8,
293
+ /// 0x7E30-0x7E3E: Unknown
294
+ pub _unknown_7e30: [u8; 0x7E3F - 0x7E30],
295
+ /// 0x7E3F: Version flag byte 3 (set by InitVersionFlags).
296
+ /// Passed to is_super_weapon as the version/mode parameter.
297
+ pub version_flag_3: u8,
298
+ /// 0x7E40: Fast-forward hurry flag (byte).
299
+ pub hurry_flag: u8,
300
+ /// 0x7E41-0x7E9F: Unknown
301
+ pub _unknown_7e41: [u8; 0x7EA0 - 0x7E41],
302
 
303
  /// 0x7EA0: Flag/counter (value 4 at runtime — likely team count).
304
  pub field_7ea0: u32,
 
1112
 
1113
  // ── CoordList at DDGame+0x50C (capacity 600, 0x12C0 buffer) ──
1114
  {
1115
+ let cl = wa_malloc(core::mem::size_of::<CoordList>() as u32) as *mut CoordList;
1116
+ (*cl).count = 0;
1117
+ (*cl).capacity = 600;
1118
+ let data = wa_malloc_zeroed(600 * core::mem::size_of::<CoordListEntry>() as u32)
1119
+ as *mut CoordListEntry;
1120
+ (*cl).data = data;
1121
+ (*ddgame).coord_list = cl;
1122
 
1123
  // Populate coord_list from PCLandscape's coordinate output.
1124
  // landscape_temp[0] = coordinate count, landscape_coords_buf = pairs of (x, y).
1125
  // Original packs as: coord = x * 0x10000 + y (Fixed-point).
1126
+ // Duplicates are skipped.
1127
  let coord_count = landscape_temp[0];
 
1128
  for j in 0..coord_count as usize {
1129
  let x = landscape_coords_buf[j * 2];
1130
  let y = landscape_coords_buf[j * 2 + 1];
1131
  let coord_val = x.wrapping_mul(0x10000).wrapping_add(y);
1132
+ let cur_count = (*cl).count as usize;
1133
  if cur_count >= 600 {
1134
  break;
1135
  }
1136
  // Check for duplicates
1137
  let mut dup = false;
1138
  for k in 0..cur_count {
1139
+ if (*data.add(k)).coord == coord_val {
1140
  dup = true;
1141
  break;
1142
  }
1143
  }
1144
  if !dup {
1145
+ (*data.add(cur_count)).coord = coord_val;
1146
+ (*data.add(cur_count)).flag = 1;
1147
+ (*cl).count = (cur_count + 1) as u32;
1148
  }
1149
  }
1150
  }
 
1560
  }
1561
  const _: () = assert!(core::mem::size_of::<RenderEntry>() == 0x14);
1562
 
1563
+ // ============================================================
1564
+ // CoordList — dynamic array of packed terrain coordinates
1565
+ // ============================================================
1566
+
1567
+ /// Packed terrain coordinate entry (8 bytes).
1568
+ ///
1569
+ /// `coord` packs x and y as `x * 0x10000 + y` (fixed-point).
1570
+ /// `flag` is always 1 for populated entries.
1571
+ #[repr(C)]
1572
+ #[derive(Clone, Copy)]
1573
+ pub struct CoordListEntry {
1574
+ pub coord: u32,
1575
+ pub flag: u32,
1576
+ }
1577
+ const _: () = assert!(core::mem::size_of::<CoordListEntry>() == 8);
1578
+
1579
+ /// Dynamic coordinate array header (12 bytes).
1580
+ ///
1581
+ /// Allocated at DDGame+0x50C during `init_graphics_and_resources`.
1582
+ /// Data buffer is a separate allocation of `capacity * 8` bytes.
1583
+ /// Used for terrain coordinate lookups (spawning, aiming, collision).
1584
+ #[repr(C)]
1585
+ pub struct CoordList {
1586
+ /// Number of entries currently stored.
1587
+ pub count: u32,
1588
+ /// Maximum number of entries (600).
1589
+ pub capacity: u32,
1590
+ /// Pointer to the data buffer (`capacity * sizeof(CoordListEntry)` bytes).
1591
+ pub data: *mut CoordListEntry,
1592
+ }
1593
+ const _: () = assert!(core::mem::size_of::<CoordList>() == 12);
1594
+
1595
  // ============================================================
1596
  // Sound queue entry — 16 entries at DDGame + 0x7F00
1597
  // ============================================================
crates/openwa-core/src/engine/game_info.rs CHANGED
@@ -9,8 +9,10 @@
9
  /// cover all known accesses.
10
  #[repr(C)]
11
  pub struct GameInfo {
12
- /// 0x0000-0x044B: Unknown
13
- pub _unknown_0000: [u8; 0x44C],
 
 
14
 
15
  // --- Speech configuration ---
16
  /// 0x044C: Number of teams with speech banks loaded (byte).
@@ -25,8 +27,18 @@ pub struct GameInfo {
25
  /// Used by DDGame constructor for conditional initialization.
26
  pub game_version: i32,
27
 
28
- /// 0xD77C-0xD943: Unknown
29
- pub _unknown_d77c: [u8; 0xD944 - 0xD77C],
 
 
 
 
 
 
 
 
 
 
30
 
31
  /// 0xD944: Network config byte 1 (copied to network object+0x28).
32
  pub net_config_1: u8,
@@ -35,12 +47,30 @@ pub struct GameInfo {
35
  /// 0xD946: Network config byte 2 (copied to network object+0x29).
36
  pub net_config_2: u8,
37
 
38
- /// 0xD947-0xD98A: Unknown
39
- pub _unknown_d947: [u8; 0xD98B - 0xD947],
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  /// 0xD98B: Terrain flag (set from map object during replay loading).
41
  pub terrain_flag: u8,
42
- /// 0xD98C-0xD9DF: Unknown
43
- pub _unknown_d98c: [u8; 0xD9E0 - 0xD98C],
 
 
 
 
 
44
 
45
  /// 0xD9E0: Streaming audio config data (path config passed to streaming audio ctor).
46
  /// Address of this field is passed as a pointer parameter.
@@ -65,8 +95,13 @@ pub struct GameInfo {
65
 
66
  // --- Replay configuration (populated by ReplayLoader) ---
67
 
68
- /// 0xDAFA-0xDB1B: Unknown
69
- pub _unknown_dafa: [u8; 0xDB1C - 0xDAFA],
 
 
 
 
 
70
  /// 0xDB1C: Replay map sub-type (first DWORD of first payload).
71
  /// >= 1: map stored in playback.thm. Negative: inline map data.
72
  pub replay_map_type: i32,
 
9
  /// cover all known accesses.
10
  #[repr(C)]
11
  pub struct GameInfo {
12
+ /// 0x0000: Number of teams (byte, read as first byte of struct).
13
+ pub num_teams: u8,
14
+ /// 0x0001-0x044B: Unknown
15
+ pub _unknown_0001: [u8; 0x44C - 1],
16
 
17
  // --- Speech configuration ---
18
  /// 0x044C: Number of teams with speech banks loaded (byte).
 
27
  /// Used by DDGame constructor for conditional initialization.
28
  pub game_version: i32,
29
 
30
+ /// 0xD77C-0xD931: Unknown
31
+ pub _unknown_d77c: [u8; 0xD932 - 0xD77C],
32
+ /// 0xD932: Weapon availability threshold (u16). For weapon 0x45:
33
+ /// if game_version > 0xD1 and this > 0x7FFF, weapon is disabled.
34
+ pub weapon_avail_threshold: u16,
35
+ /// 0xD934-0xD93B: Unknown
36
+ pub _unknown_d934: [u8; 0xD93C - 0xD934],
37
+ /// 0xD93C: Super weapon allowed flag. If 0, super weapons are
38
+ /// disabled (except when game_version < 0x2A).
39
+ pub super_weapon_allowed: u8,
40
+ /// 0xD93D-0xD943: Unknown
41
+ pub _unknown_d93d: [u8; 0xD944 - 0xD93D],
42
 
43
  /// 0xD944: Network config byte 1 (copied to network object+0x28).
44
  pub net_config_1: u8,
 
47
  /// 0xD946: Network config byte 2 (copied to network object+0x29).
48
  pub net_config_2: u8,
49
 
50
+ /// 0xD947-0xD94B: Unknown
51
+ pub _unknown_d947: [u8; 0xD94C - 0xD947],
52
+ /// 0xD94C: Weapon 0x36 (Indian Nuclear Test) disable flag.
53
+ pub weapon_36_disabled: u8,
54
+ /// 0xD94D-0xD955: Unknown
55
+ pub _unknown_d94d: [u8; 0xD956 - 0xD94D],
56
+ /// 0xD956: Weapon index offset flag. Affects weapon 0x19 availability check.
57
+ pub weapon_index_offset: u8,
58
+ /// 0xD957-0xD958: Unknown
59
+ pub _unknown_d957: [u8; 0xD959 - 0xD957],
60
+ /// 0xD959: Version-gated weapon restriction. If nonzero and game_version > 0x29,
61
+ /// returns -2 for unavailable weapons.
62
+ pub weapon_version_gate: u8,
63
+ /// 0xD95A-0xD98A: Unknown
64
+ pub _unknown_d95a: [u8; 0xD98B - 0xD95A],
65
  /// 0xD98B: Terrain flag (set from map object during replay loading).
66
  pub terrain_flag: u8,
67
+ /// 0xD98C-0xD9A1: Unknown
68
+ pub _unknown_d98c: [u8; 0xD9A2 - 0xD98C],
69
+ /// 0xD9A2: Network weapon exception flag. When net_config_2 != 0,
70
+ /// weapons 10/0x37/0x38 are only disabled if this is also 0.
71
+ pub net_weapon_exception: u8,
72
+ /// 0xD9A3-0xD9DF: Unknown
73
+ pub _unknown_d9a3: [u8; 0xD9E0 - 0xD9A3],
74
 
75
  /// 0xD9E0: Streaming audio config data (path config passed to streaming audio ctor).
76
  /// Address of this field is passed as a pointer parameter.
 
95
 
96
  // --- Replay configuration (populated by ReplayLoader) ---
97
 
98
+ /// 0xDAFA-0xDB07: Unknown
99
+ pub _unknown_dafa: [u8; 0xDB08 - 0xDAFA],
100
+ /// 0xDB08: Weapon 0x42 mode flag (u32). Controls team-count vs network_ecx
101
+ /// check for weapon availability.
102
+ pub weapon_42_mode: u32,
103
+ /// 0xDB0C-0xDB1B: Unknown
104
+ pub _unknown_db0c: [u8; 0xDB1C - 0xDB0C],
105
  /// 0xDB1C: Replay map sub-type (first DWORD of first payload).
106
  /// >= 1: map stored in playback.thm. Negative: inline map data.
107
  pub replay_map_type: i32,
crates/openwa-core/src/engine/game_state_init.rs CHANGED
@@ -3,6 +3,8 @@
3
  //! Each function is hooked individually so it works regardless of whether
4
  //! InitGameState itself is Rust or the original WA code.
5
 
 
 
6
  use crate::wa_alloc::wa_malloc;
7
 
8
  /// Pure Rust implementation of SpriteGfxTable__Init (0x541620).
@@ -343,29 +345,26 @@ pub unsafe fn is_super_weapon(weapon_index: u32, param_1: u8) -> u8 {
343
  ///
344
  /// Checks whether a weapon (1..0x46) is available given current game state.
345
  /// `ddgame` is the DDGame pointer directly (not wrapper).
346
- pub unsafe fn check_weapon_avail(ddgame: *mut u8, weapon_index: u32) -> i32 {
347
- let game_info = *(ddgame.add(0x24) as *const *mut u8);
348
- let game_version = *(game_info.add(0xD778) as *const i32);
349
- let num_teams = *game_info as u8;
350
 
351
  // Step 1: Special per-weapon disabling rules
352
  match weapon_index {
353
  10 | 0x37 | 0x38 => {
354
- if *(game_info.add(0xD946) as *const u8) != 0
355
- && *(game_info.add(0xD9A2) as *const u8) == 0
356
- {
357
  return 0;
358
  }
359
  }
360
  0x36 => {
361
- if *(game_info.add(0xD94C) as *const u8) != 0 {
362
  return 0;
363
  }
364
  }
365
  0x42 => {
366
- let db08 = *(game_info.add(0xDB08) as *const u32);
367
- if db08 == 0 {
368
- if *(ddgame.add(0x1C) as *const u32) == 0 {
369
  return 0;
370
  }
371
  } else if (num_teams as u32) < 2 {
@@ -374,8 +373,7 @@ pub unsafe fn check_weapon_avail(ddgame: *mut u8, weapon_index: u32) -> i32 {
374
  }
375
  0x45 => {
376
  if game_version > 0xD1 {
377
- let val = *(game_info.add(0xD932) as *const u16);
378
- if val > 0x7FFF {
379
  return 0;
380
  }
381
  }
@@ -383,35 +381,33 @@ pub unsafe fn check_weapon_avail(ddgame: *mut u8, weapon_index: u32) -> i32 {
383
  _ => {}
384
  }
385
 
386
- // Step 2: Branch on weapon table entry + DDGame+0x777C
387
- // DDGame+0x510 is a POINTER to the weapon table (double deref)
388
- let weapon_table = *(ddgame.add(0x510) as *const *const u8);
389
  let weapon_entry = *(weapon_table.add(0x10 + (weapon_index as usize) * 0x1D0) as *const i32);
390
 
391
- if *(ddgame.add(0x777C) as *const i32) == 0 || weapon_entry != 0 {
392
- // Main path: check super weapon, 7E25, etc.
393
- let super_result = is_super_weapon(weapon_index, *(ddgame.add(0x7E3F) as *const u8));
394
- // After is_super_weapon, re-read ddgame (ECX may change in original — doesn't matter in Rust)
395
- if super_result != 0 && *(game_info.add(0xD93C) as *const u8) == 0 {
396
- // (game_version < 0x2A) - 1: if < 0x2A → 1-1=0, else → 0-1=-1
397
  return (game_version < 0x2A) as i32 - 1;
398
  }
399
 
400
- if *(ddgame.add(0x7E25) as *const u8) == 0 {
401
  return 1;
402
  }
403
 
404
- // Check if weapon_index == (0x19 - (game_info+0xD956 != 0))
405
- let d956_nonzero = (*(game_info.add(0xD956) as *const u8) != 0) as u32;
406
- if weapon_index != 0x19 - d956_nonzero {
407
  return 1;
408
  }
409
 
410
  return 0;
411
  }
412
 
413
- // Else branch: 777C != 0 AND weapon_entry == 0
414
- if game_version > 0x29 && *(game_info.add(0xD959) as *const u8) != 0 {
415
  return -2;
416
  }
417
 
 
3
  //! Each function is hooked individually so it works regardless of whether
4
  //! InitGameState itself is Rust or the original WA code.
5
 
6
+ use crate::engine::ddgame::DDGame;
7
+ use crate::log::log_line;
8
  use crate::wa_alloc::wa_malloc;
9
 
10
  /// Pure Rust implementation of SpriteGfxTable__Init (0x541620).
 
345
  ///
346
  /// Checks whether a weapon (1..0x46) is available given current game state.
347
  /// `ddgame` is the DDGame pointer directly (not wrapper).
348
+ pub unsafe fn check_weapon_avail(ddgame: *mut DDGame, weapon_index: u32) -> i32 {
349
+ let gi = (*ddgame).game_info;
350
+ let game_version = (*gi).game_version;
351
+ let num_teams = (*gi).num_teams;
352
 
353
  // Step 1: Special per-weapon disabling rules
354
  match weapon_index {
355
  10 | 0x37 | 0x38 => {
356
+ if (*gi).net_config_2 != 0 && (*gi).net_weapon_exception == 0 {
 
 
357
  return 0;
358
  }
359
  }
360
  0x36 => {
361
+ if (*gi).weapon_36_disabled != 0 {
362
  return 0;
363
  }
364
  }
365
  0x42 => {
366
+ if (*gi).weapon_42_mode == 0 {
367
+ if (*ddgame).network_ecx == 0 {
 
368
  return 0;
369
  }
370
  } else if (num_teams as u32) < 2 {
 
373
  }
374
  0x45 => {
375
  if game_version > 0xD1 {
376
+ if (*gi).weapon_avail_threshold > 0x7FFF {
 
377
  return 0;
378
  }
379
  }
 
381
  _ => {}
382
  }
383
 
384
+ // Step 2: Branch on weapon table entry + level_width_raw
385
+ let weapon_table = (*ddgame).weapon_table as *const u8;
 
386
  let weapon_entry = *(weapon_table.add(0x10 + (weapon_index as usize) * 0x1D0) as *const i32);
387
 
388
+ if (*ddgame).level_width_raw == 0 || weapon_entry != 0 {
389
+ // Main path: check super weapon flag
390
+ let super_result = is_super_weapon(weapon_index, (*ddgame).version_flag_3);
391
+ if super_result != 0 && (*gi).super_weapon_allowed == 0 {
392
+ // (game_version < 0x2A) - 1: if < 0x2A → 0, else → -1
 
393
  return (game_version < 0x2A) as i32 - 1;
394
  }
395
 
396
+ if (*ddgame).weapon_restriction_active == 0 {
397
  return 1;
398
  }
399
 
400
+ // Check if weapon_index == (0x19 - (weapon_index_offset != 0))
401
+ let offset = ((*gi).weapon_index_offset != 0) as u32;
402
+ if weapon_index != 0x19 - offset {
403
  return 1;
404
  }
405
 
406
  return 0;
407
  }
408
 
409
+ // Else branch: level_width_raw != 0 AND weapon_entry == 0
410
+ if game_version > 0x29 && (*gi).weapon_version_gate != 0 {
411
  return -2;
412
  }
413
 
crates/openwa-wormkit/src/replacements/ddgame_init.rs CHANGED
@@ -339,7 +339,7 @@ unsafe extern "C" fn init_turn_state_trampoline() {
339
  // Returns i32 in EAX.
340
 
341
  extern "cdecl" fn impl_check_weapon_avail(ddgame: u32, weapon_index: u32) -> i32 {
342
- unsafe { check_weapon_avail(ddgame as *mut u8, weapon_index) }
343
  }
344
 
345
  #[unsafe(naked)]
 
339
  // Returns i32 in EAX.
340
 
341
  extern "cdecl" fn impl_check_weapon_avail(ddgame: u32, weapon_index: u32) -> i32 {
342
+ unsafe { check_weapon_avail(ddgame as *mut DDGame, weapon_index) }
343
  }
344
 
345
  #[unsafe(naked)]