paavohtl Claude Opus 4.6 commited on
Commit
18cc044
·
1 Parent(s): 70e6ea1

feat: add GetAmmo and AddAmmo weapon hooks with TeamWeaponState struct

Browse files

Add Rust replacements for GetAmmo (0x5225E0) and AddAmmo (0x522640),
the first DDGame wrapper functions to be ported. Both use __usercall
conventions with implicit register params, handled by new multi-register
arms in the usercall_trampoline! macro.

- TeamWeaponState/TeamEntry structs in openwa-types with typed accessors
- 3 new usercall_trampoline! arms: 2-reg+1-stack, 2-reg+2-stack, 3-reg
- GetAmmo: alliance-based ammo lookup with delay/phase/SelectWorm checks
- AddAmmo: ammo increment with unlimited (-1) support
- SubtractAmmo (0x522680) address noted but unhooked — only 5 xrefs,
never observed during normal gameplay; likely not the main decrement path

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

crates/openwa-types/src/address.rs CHANGED
@@ -122,8 +122,12 @@ pub mod va {
122
  // === Weapon system ===
123
 
124
  pub const INIT_WEAPON_TABLE: u32 = 0x0053_CAB0;
 
125
  pub const GET_AMMO: u32 = 0x0052_25E0;
126
  pub const ADD_AMMO: u32 = 0x0052_2640;
 
 
 
127
  pub const SUBTRACT_AMMO: u32 = 0x0052_2680;
128
 
129
  // === Graphics / rendering ===
 
122
  // === Weapon system ===
123
 
124
  pub const INIT_WEAPON_TABLE: u32 = 0x0053_CAB0;
125
+ pub const COUNT_ALIVE_WORMS: u32 = 0x0052_25A0;
126
  pub const GET_AMMO: u32 = 0x0052_25E0;
127
  pub const ADD_AMMO: u32 = 0x0052_2640;
128
+ /// Not the main ammo decrement path — only 5 xrefs across 3 functions,
129
+ /// never observed firing during normal gameplay. The real decrement is
130
+ /// likely inlined at weapon-firing call sites. Unhooked until verified.
131
  pub const SUBTRACT_AMMO: u32 = 0x0052_2680;
132
 
133
  // === Graphics / rendering ===
crates/openwa-types/src/ddgame.rs CHANGED
@@ -113,6 +113,12 @@ pub mod offsets {
113
  pub const WEAPON_TABLE: usize = 0x510;
114
  pub const WEAPON_PANEL: usize = 0x548;
115
 
 
 
 
 
 
 
116
  // === FUN_00526120 init offsets (stride 0x194, 10 entries) ===
117
  pub const INIT_TABLE_BASE: usize = 0x379C;
118
  pub const INIT_TABLE_STRIDE: usize = 0x194;
@@ -126,3 +132,95 @@ pub mod offsets {
126
  pub const FLAG_7EF8: usize = 0x7EF8;
127
  pub const FIELD_7EFC: usize = 0x7EFC;
128
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  pub const WEAPON_TABLE: usize = 0x510;
114
  pub const WEAPON_PANEL: usize = 0x548;
115
 
116
+ // === Team weapon state (DDGame + 0x4628) ===
117
+ /// Base of TeamWeaponState sub-struct within DDGame.
118
+ /// Callers pass DDGame + TEAM_WEAPON_STATE as base pointer to
119
+ /// GetAmmo/AddAmmo/SubtractAmmo.
120
+ pub const TEAM_WEAPON_STATE: usize = 0x4628;
121
+
122
  // === FUN_00526120 init offsets (stride 0x194, 10 entries) ===
123
  pub const INIT_TABLE_BASE: usize = 0x379C;
124
  pub const INIT_TABLE_STRIDE: usize = 0x194;
 
132
  pub const FLAG_7EF8: usize = 0x7EF8;
133
  pub const FIELD_7EFC: usize = 0x7EFC;
134
  }
135
+
136
+ // ============================================================
137
+ // Team weapon state — sub-struct at DDGame + 0x4628
138
+ // ============================================================
139
+
140
+ /// Per-team entry within the TeamWeaponState area.
141
+ ///
142
+ /// Located at TeamWeaponState base + team_index * 0x51C.
143
+ /// Each team has an alliance_id that maps into shared ammo/delay tables.
144
+ ///
145
+ /// Worm data lives BEFORE this entry:
146
+ /// - worm_count at offset -0x4
147
+ /// - worm array at offset -0x4A0 (stride 0x9C, health at [0])
148
+ #[repr(C)]
149
+ pub struct TeamEntry {
150
+ pub _unknown_000: [u8; 4],
151
+ /// Alliance ID — teams with the same alliance share ammo pools.
152
+ /// Index into ammo/delay tables: alliance_id * 142 + weapon_id
153
+ pub alliance_id: i32,
154
+ pub _unknown_008: [u8; 0x514],
155
+ }
156
+
157
+ const _: () = assert!(core::mem::size_of::<TeamEntry>() == 0x51C);
158
+
159
+ /// Team weapon state area within DDGame (at DDGame + 0x4628).
160
+ ///
161
+ /// Contains per-team entries and an interleaved ammo/delay table.
162
+ /// Used by GetAmmo (0x5225E0), AddAmmo (0x522640), SubtractAmmo (0x522680).
163
+ ///
164
+ /// The ammo/delay table uses stride 142 (= 71 weapons * 2) per alliance.
165
+ /// Within each alliance block of 142 entries:
166
+ /// - Entries 0..70 are ammo counts (accessed at base + 0x1EB4 + index * 4)
167
+ /// - Entries 71..141 are delay flags (accessed at base + 0x1FD0 + index * 4)
168
+ ///
169
+ /// The original code accesses these as two arrays at different base offsets
170
+ /// (0x1EB4 and 0x1FD0) using the same index `alliance_id * 142 + weapon_id`.
171
+ /// Since 0x1FD0 - 0x1EB4 = 71 * 4, this is equivalent to accessing
172
+ /// `weapon_slots[alliance * 142 + weapon]` for ammo and
173
+ /// `weapon_slots[alliance * 142 + 71 + weapon]` for delay.
174
+ #[repr(C)]
175
+ pub struct TeamWeaponState {
176
+ /// 0x0000: Per-team entries (6 teams, stride 0x51C = 1308 bytes each)
177
+ pub teams: [TeamEntry; 6],
178
+ /// 0x1EA8: Padding
179
+ pub _pad_1ea8: [u8; 0xC],
180
+ /// 0x1EB4: Interleaved ammo/delay slots.
181
+ /// Per alliance: [ammo_0..ammo_70, delay_0..delay_70] = 142 i32 entries.
182
+ /// 6 alliances * 142 = 852 total entries.
183
+ /// Ammo: -1 = unlimited, 0 = none, >0 = count.
184
+ /// Delay: nonzero = weapon on delay.
185
+ pub weapon_slots: [i32; 852],
186
+ /// 0x2C04: Padding between weapon_slots end and game_mode_flag.
187
+ /// weapon_slots: 852 * 4 = 3408 = 0xD50. 0x1EB4 + 0xD50 = 0x2C04.
188
+ pub _pad_2c04: [u8; 8],
189
+ /// 0x2C0C: Game mode flag (nonzero = override weapon delays in certain conditions)
190
+ pub game_mode_flag: i32,
191
+ /// 0x2C10: Unknown padding
192
+ pub _pad_2c10: [u8; 0x18],
193
+ /// 0x2C28: Game phase counter (>=484 = sudden death, >=-2 = normal game)
194
+ pub game_phase: i32,
195
+ }
196
+
197
+ const _: () = assert!(core::mem::size_of::<TeamWeaponState>() == 0x2C2C);
198
+
199
+ impl TeamWeaponState {
200
+ /// Compute the flat index for ammo/delay table access.
201
+ ///
202
+ /// The weapon_slots array is interleaved: per alliance, 71 ammo slots
203
+ /// then 71 delay slots (stride 142 per alliance).
204
+ /// Ammo: `weapon_slots[alliance_id * 142 + weapon_id]`
205
+ /// Delay: `weapon_slots[alliance_id * 142 + 71 + weapon_id]`
206
+ pub unsafe fn ammo_index(&self, team_index: usize, weapon_id: u32) -> usize {
207
+ let alliance_id = self.teams[team_index].alliance_id as usize;
208
+ alliance_id * 142 + weapon_id as usize
209
+ }
210
+
211
+ /// Get ammo count for a weapon slot (by flat index).
212
+ pub fn get_ammo(&self, index: usize) -> i32 {
213
+ self.weapon_slots[index]
214
+ }
215
+
216
+ /// Get mutable reference to ammo count for a weapon slot.
217
+ pub fn ammo_mut(&mut self, index: usize) -> &mut i32 {
218
+ &mut self.weapon_slots[index]
219
+ }
220
+
221
+ /// Get delay flag for a weapon slot (by flat index).
222
+ /// Delay is at +71 offset from the ammo index within the same alliance block.
223
+ pub fn get_delay(&self, index: usize) -> i32 {
224
+ self.weapon_slots[index + 71]
225
+ }
226
+ }
crates/openwa-wormkit/src/hook.rs CHANGED
@@ -51,6 +51,62 @@ macro_rules! usercall_trampoline {
51
  );
52
  }
53
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  }
55
 
56
  pub(crate) use usercall_trampoline;
 
51
  );
52
  }
53
  };
54
+
55
+ // 2 register args, 1 stack param, ret N
56
+ // Stack layout at entry: [ESP+0]=retaddr, [ESP+4]=arg
57
+ // Push: stack_arg, reg2, reg1 → impl_fn(reg1, reg2, stack_arg)
58
+ (fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
59
+ stack_params = 1; ret_bytes = $ret:literal) => {
60
+ #[unsafe(naked)]
61
+ unsafe extern "C" fn $name() {
62
+ core::arch::naked_asm!(
63
+ "push [esp+4]",
64
+ concat!("push ", stringify!($r2)),
65
+ concat!("push ", stringify!($r1)),
66
+ "call {impl_fn}",
67
+ "add esp, 12",
68
+ concat!("ret ", $ret),
69
+ impl_fn = sym $impl,
70
+ );
71
+ }
72
+ };
73
+
74
+ // 2 register args, 2 stack params, ret N
75
+ // Stack layout at entry: [ESP+0]=retaddr, [ESP+4]=arg1, [ESP+8]=arg2
76
+ // Push: stack_arg2, stack_arg1, reg2, reg1 → impl_fn(reg1, reg2, stack_arg1, stack_arg2)
77
+ (fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
78
+ stack_params = 2; ret_bytes = $ret:literal) => {
79
+ #[unsafe(naked)]
80
+ unsafe extern "C" fn $name() {
81
+ core::arch::naked_asm!(
82
+ "push [esp+8]",
83
+ "push [esp+8]",
84
+ concat!("push ", stringify!($r2)),
85
+ concat!("push ", stringify!($r1)),
86
+ "call {impl_fn}",
87
+ "add esp, 16",
88
+ concat!("ret ", $ret),
89
+ impl_fn = sym $impl,
90
+ );
91
+ }
92
+ };
93
+
94
+ // 3 register args, 0 stack params, plain ret
95
+ // Push: reg3, reg2, reg1 → impl_fn(reg1, reg2, reg3)
96
+ (fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident, $r3:ident]) => {
97
+ #[unsafe(naked)]
98
+ unsafe extern "C" fn $name() {
99
+ core::arch::naked_asm!(
100
+ concat!("push ", stringify!($r3)),
101
+ concat!("push ", stringify!($r2)),
102
+ concat!("push ", stringify!($r1)),
103
+ "call {impl_fn}",
104
+ "add esp, 12",
105
+ "ret",
106
+ impl_fn = sym $impl,
107
+ );
108
+ }
109
+ };
110
  }
111
 
112
  pub(crate) use usercall_trampoline;
crates/openwa-wormkit/src/replacements/mod.rs CHANGED
@@ -1,10 +1,12 @@
1
  mod config;
2
  mod frontend;
3
  mod scheme;
 
4
 
5
  pub fn install_all() -> Result<(), String> {
6
  frontend::install()?;
7
  scheme::install()?;
8
  config::install()?;
 
9
  Ok(())
10
  }
 
1
  mod config;
2
  mod frontend;
3
  mod scheme;
4
+ mod weapon;
5
 
6
  pub fn install_all() -> Result<(), String> {
7
  frontend::install()?;
8
  scheme::install()?;
9
  config::install()?;
10
+ weapon::install()?;
11
  Ok(())
12
  }
crates/openwa-wormkit/src/replacements/weapon.rs ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //! Weapon ammo accessor hooks.
2
+ //!
3
+ //! Replaces WA.exe weapon state functions with Rust implementations:
4
+ //! - GetAmmo (0x5225E0): query ammo count with delay/phase checks
5
+ //! - AddAmmo (0x522640): add ammo to a weapon slot
6
+
7
+ use openwa_types::address::va;
8
+ use openwa_types::ddgame::TeamWeaponState;
9
+
10
+ use crate::hook::{self, usercall_trampoline};
11
+
12
+ // ============================================================
13
+ // AddAmmo replacement (0x522640)
14
+ // ============================================================
15
+ // __usercall: EAX = team_index, EDX = amount, [ESP+4] = team_info_base, [ESP+8] = weapon_id
16
+ // RET 0x8
17
+
18
+ unsafe extern "cdecl" fn add_ammo_impl(team_index: u32, amount: i32, base: u32, weapon_id: u32) {
19
+ let state = &mut *(base as *mut TeamWeaponState);
20
+ let idx = state.ammo_index(team_index as usize, weapon_id);
21
+ let ammo = state.get_ammo(idx);
22
+ if ammo >= 0 {
23
+ if amount < 0 {
24
+ *state.ammo_mut(idx) = -1; // set unlimited
25
+ } else {
26
+ *state.ammo_mut(idx) = ammo + amount;
27
+ }
28
+ }
29
+ }
30
+
31
+ usercall_trampoline!(fn trampoline_add_ammo; impl_fn = add_ammo_impl;
32
+ regs = [eax, edx]; stack_params = 2; ret_bytes = "0x8");
33
+
34
+ // ============================================================
35
+ // GetAmmo replacement (0x5225E0)
36
+ // ============================================================
37
+ // __usercall: EAX = team_index, ESI = team_info_base, EDX = weapon_id
38
+ // plain RET, returns EAX = ammo count
39
+
40
+ unsafe extern "cdecl" fn get_ammo_impl(team_index: u32, base: u32, weapon_id: u32) -> u32 {
41
+ let state = &*(base as *const TeamWeaponState);
42
+ let idx = state.ammo_index(team_index as usize, weapon_id);
43
+
44
+ // Check weapon delay
45
+ if state.get_delay(idx) != 0 {
46
+ if state.game_mode_flag == 0 {
47
+ return 0;
48
+ }
49
+ // In sudden death (phase >= 484), delayed weapons return 0
50
+ // unless it's Teleport (weapon 0x28)
51
+ if state.game_phase >= 0x1E4 && weapon_id != 0x28 {
52
+ return 0;
53
+ }
54
+ }
55
+
56
+ // SelectWorm (0x3B) requires >1 alive worm on the team
57
+ if state.game_phase >= -2 && weapon_id == 0x3B {
58
+ if !count_alive_worms(state, team_index as usize) {
59
+ return 0;
60
+ }
61
+ }
62
+
63
+ state.get_ammo(idx) as u32
64
+ }
65
+
66
+ /// Returns true if more than 1 worm is alive on the team.
67
+ ///
68
+ /// Worm data lives at negative offsets from the TeamEntry:
69
+ /// - worm_count at team_entry_addr - 0x4
70
+ /// - worm array at team_entry_addr - 0x4A0 (stride 0x9C, health at [0])
71
+ unsafe fn count_alive_worms(state: &TeamWeaponState, team_index: usize) -> bool {
72
+ let base = state as *const TeamWeaponState as *const u8;
73
+ let team_entry = base.add(team_index * 0x51C) as usize;
74
+ let worm_count = *((team_entry - 4) as *const i32);
75
+ let mut alive = 0i32;
76
+ if worm_count > 0 {
77
+ let mut ptr = (team_entry - 0x4A0) as *const u8;
78
+ for _ in 0..worm_count {
79
+ if *(ptr as *const i32) > 0 {
80
+ alive += 1;
81
+ }
82
+ ptr = ptr.add(0x9C);
83
+ }
84
+ }
85
+ alive > 1
86
+ }
87
+
88
+ usercall_trampoline!(fn trampoline_get_ammo; impl_fn = get_ammo_impl;
89
+ regs = [eax, esi, edx]);
90
+
91
+ // ============================================================
92
+ // Hook installation
93
+ // ============================================================
94
+
95
+ pub fn install() -> Result<(), String> {
96
+ unsafe {
97
+ let _ = hook::install(
98
+ "AddAmmo",
99
+ va::ADD_AMMO,
100
+ trampoline_add_ammo as *const (),
101
+ )?;
102
+
103
+ let _ = hook::install(
104
+ "GetAmmo",
105
+ va::GET_AMMO,
106
+ trampoline_get_ammo as *const (),
107
+ )?;
108
+ }
109
+
110
+ Ok(())
111
+ }