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

feat: add 5 DDGame team/worm accessor function replacements

Browse files

Port CountTeamsByAlliance, GetTeamTotalHealth, IsWormInSpecialState,
GetWormPosition, and HasFullHealthWorm from the 0x5220-0x5260 DDGame
neighborhood. Extends TeamWeaponState with team_count and alliance
tracking fields. Adds 2 new usercall_trampoline! macro arms (2-reg
plain ret, 2-reg+3-stack ret N). Total hooks: 20 → 25.

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

crates/openwa-types/src/address.rs CHANGED
@@ -130,6 +130,24 @@ pub mod va {
130
  /// likely inlined at weapon-firing call sites. Unhooked until verified.
131
  pub const SUBTRACT_AMMO: u32 = 0x0052_2680;
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  // === Graphics / rendering ===
134
 
135
  pub const CONSTRUCT_DD_GAME: u32 = 0x0056_E220;
 
130
  /// likely inlined at weapon-firing call sites. Unhooked until verified.
131
  pub const SUBTRACT_AMMO: u32 = 0x0052_2680;
132
 
133
+ // === Team/worm accessor functions (DDGame + 0x4628 area) ===
134
+
135
+ /// Counts teams by alliance membership, sets current_alliance + counters.
136
+ /// usercall(EAX=base, EDI=alliance_id) → void, plain RET.
137
+ pub const COUNT_TEAMS_BY_ALLIANCE: u32 = 0x0052_2030;
138
+ /// Sums health of all worms on a team. Returns 0 if team eliminated.
139
+ /// fastcall(ECX=team_index, EDX=base) → EAX=total_health, plain RET.
140
+ pub const GET_TEAM_TOTAL_HEALTH: u32 = 0x0052_24D0;
141
+ /// Checks if a worm is in a "special" state (dying, drowning, etc.).
142
+ /// usercall(EAX=team_index, ECX=worm_index, [ESP+4]=base) → EAX=bool, RET 0x4.
143
+ pub const IS_WORM_IN_SPECIAL_STATE: u32 = 0x0052_26B0;
144
+ /// Reads worm X,Y position into output pointers.
145
+ /// usercall(EAX=team_index, ECX=worm_index, [ESP+4]=base, [ESP+8]=&x, [ESP+C]=&y), RET 0xC.
146
+ pub const GET_WORM_POSITION: u32 = 0x0052_2700;
147
+ /// Checks if any team has a worm at full health (100). 11 xrefs.
148
+ /// usercall(EAX=base) → EAX=bool, plain RET.
149
+ pub const HAS_FULL_HEALTH_WORM: u32 = 0x0052_28D0;
150
+
151
  // === Graphics / rendering ===
152
 
153
  pub const CONSTRUCT_DD_GAME: u32 = 0x0056_E220;
crates/openwa-types/src/ddgame.rs CHANGED
@@ -176,7 +176,9 @@ 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.
@@ -192,9 +194,22 @@ pub struct TeamWeaponState {
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.
 
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; 0x8],
180
+ /// 0x1EB0: Number of teams in the game (used by team iteration loops)
181
+ pub team_count: i32,
182
  /// 0x1EB4: Interleaved ammo/delay slots.
183
  /// Per alliance: [ammo_0..ammo_70, delay_0..delay_70] = 142 i32 entries.
184
  /// 6 alliances * 142 = 852 total entries.
 
194
  pub _pad_2c10: [u8; 0x18],
195
  /// 0x2C28: Game phase counter (>=484 = sudden death, >=-2 = normal game)
196
  pub game_phase: i32,
197
+
198
+ // === Alliance tracking (set by CountTeamsByAlliance, 0x522030) ===
199
+
200
+ /// 0x2C2C: Current alliance being evaluated
201
+ pub current_alliance: i32,
202
+ /// 0x2C30: Unknown padding
203
+ pub _pad_2c30: [u8; 4],
204
+ /// 0x2C34: Count of teams alive with valid alliance (>=0)
205
+ pub active_team_count: i32,
206
+ /// 0x2C38: Count of teams matching current_alliance
207
+ pub same_alliance_count: i32,
208
+ /// 0x2C3C: Count of teams not matching current_alliance
209
+ pub enemy_team_count: i32,
210
  }
211
 
212
+ const _: () = assert!(core::mem::size_of::<TeamWeaponState>() == 0x2C40);
213
 
214
  impl TeamWeaponState {
215
  /// Compute the flat index for ammo/delay table access.
crates/openwa-wormkit/src/hook.rs CHANGED
@@ -91,6 +91,43 @@ macro_rules! usercall_trampoline {
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]) => {
 
91
  }
92
  };
93
 
94
+ // 2 register args, 0 stack params, plain ret
95
+ // Push: reg2, reg1 → impl_fn(reg1, reg2)
96
+ (fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident]) => {
97
+ #[unsafe(naked)]
98
+ unsafe extern "C" fn $name() {
99
+ core::arch::naked_asm!(
100
+ concat!("push ", stringify!($r2)),
101
+ concat!("push ", stringify!($r1)),
102
+ "call {impl_fn}",
103
+ "add esp, 8",
104
+ "ret",
105
+ impl_fn = sym $impl,
106
+ );
107
+ }
108
+ };
109
+
110
+ // 2 register args, 3 stack params, ret N
111
+ // Stack layout at entry: [ESP+0]=retaddr, [ESP+4]=arg1, [ESP+8]=arg2, [ESP+C]=arg3
112
+ // Push: stack_arg3, stack_arg2, stack_arg1, reg2, reg1 → impl_fn(reg1, reg2, stack_arg1, stack_arg2, stack_arg3)
113
+ (fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident];
114
+ stack_params = 3; ret_bytes = $ret:literal) => {
115
+ #[unsafe(naked)]
116
+ unsafe extern "C" fn $name() {
117
+ core::arch::naked_asm!(
118
+ "push [esp+12]",
119
+ "push [esp+12]",
120
+ "push [esp+12]",
121
+ concat!("push ", stringify!($r2)),
122
+ concat!("push ", stringify!($r1)),
123
+ "call {impl_fn}",
124
+ "add esp, 20",
125
+ concat!("ret ", $ret),
126
+ impl_fn = sym $impl,
127
+ );
128
+ }
129
+ };
130
+
131
  // 3 register args, 0 stack params, plain ret
132
  // Push: reg3, reg2, reg1 → impl_fn(reg1, reg2, reg3)
133
  (fn $name:ident; impl_fn = $impl:path; regs = [$r1:ident, $r2:ident, $r3:ident]) => {
crates/openwa-wormkit/src/replacements/weapon.rs CHANGED
@@ -1,8 +1,13 @@
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;
@@ -88,6 +93,152 @@ unsafe fn count_alive_worms(state: &TeamWeaponState, team_index: usize) -> bool
88
  usercall_trampoline!(fn trampoline_get_ammo; impl_fn = get_ammo_impl;
89
  regs = [eax, esi, edx]);
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  // ============================================================
92
  // Hook installation
93
  // ============================================================
@@ -105,6 +256,36 @@ pub fn install() -> Result<(), String> {
105
  va::GET_AMMO,
106
  trampoline_get_ammo as *const (),
107
  )?;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  }
109
 
110
  Ok(())
 
1
+ //! Weapon and team state accessor hooks.
2
  //!
3
+ //! Replaces WA.exe functions that access the TeamWeaponState area (DDGame + 0x4628):
4
  //! - GetAmmo (0x5225E0): query ammo count with delay/phase checks
5
  //! - AddAmmo (0x522640): add ammo to a weapon slot
6
+ //! - CountTeamsByAlliance (0x522030): count teams by alliance membership
7
+ //! - GetTeamTotalHealth (0x5224D0): sum worm health for a team
8
+ //! - IsWormInSpecialState (0x5226B0): check worm state flag
9
+ //! - GetWormPosition (0x522700): read worm X,Y coordinates
10
+ //! - HasFullHealthWorm (0x5228D0): check if any worm has full health
11
 
12
  use openwa_types::address::va;
13
  use openwa_types::ddgame::TeamWeaponState;
 
93
  usercall_trampoline!(fn trampoline_get_ammo; impl_fn = get_ammo_impl;
94
  regs = [eax, esi, edx]);
95
 
96
+ // ============================================================
97
+ // CountTeamsByAlliance replacement (0x522030)
98
+ // ============================================================
99
+ // __usercall: EAX = base, EDI = alliance_id
100
+ // plain RET
101
+
102
+ unsafe extern "cdecl" fn count_teams_by_alliance_impl(base: u32, alliance_id: i32) {
103
+ let state = &mut *(base as *mut TeamWeaponState);
104
+ state.current_alliance = alliance_id;
105
+ state.active_team_count = 0;
106
+ state.same_alliance_count = 0;
107
+ state.enemy_team_count = 0;
108
+
109
+ // Teams are 1-indexed; iterate 1..=team_count
110
+ // Per-team data: base + 0x510 + (team_1based - 1) * 0x51C
111
+ // At [+0]: alliance field, [+4]: alive flag
112
+ let base_ptr = base as *const u8;
113
+ for i in 0..state.team_count {
114
+ let team_data = base_ptr.add(0x510 + i as usize * 0x51C);
115
+ let team_alliance = *(team_data as *const i32);
116
+ let alive_flag = *(team_data.add(4) as *const i32);
117
+ if alive_flag != 0 && team_alliance >= 0 {
118
+ state.active_team_count += 1;
119
+ if team_alliance == alliance_id {
120
+ state.same_alliance_count += 1;
121
+ } else {
122
+ state.enemy_team_count += 1;
123
+ }
124
+ }
125
+ }
126
+ }
127
+
128
+ usercall_trampoline!(fn trampoline_count_teams_by_alliance; impl_fn = count_teams_by_alliance_impl;
129
+ regs = [eax, edi]);
130
+
131
+ // ============================================================
132
+ // GetTeamTotalHealth replacement (0x5224D0)
133
+ // ============================================================
134
+ // __fastcall: ECX = team_index, EDX = base
135
+ // plain RET, returns EAX = total health
136
+
137
+ unsafe extern "cdecl" fn get_team_total_health_impl(team_index: u32, base: u32) -> i32 {
138
+ let base_ptr = base as *const u8;
139
+ let team_entry = base_ptr.add(team_index as usize * 0x51C) as usize;
140
+
141
+ // Check eliminated flag at team_entry - 0x10
142
+ if *((team_entry - 0x10) as *const i32) != 0 {
143
+ return 0;
144
+ }
145
+
146
+ let worm_count = *((team_entry - 4) as *const i32);
147
+ let mut total = 0i32;
148
+ if worm_count > 0 {
149
+ let mut ptr = (team_entry - 0x4A0) as *const u8;
150
+ for _ in 0..worm_count {
151
+ total += *(ptr as *const i32);
152
+ ptr = ptr.add(0x9C);
153
+ }
154
+ }
155
+ total
156
+ }
157
+
158
+ usercall_trampoline!(fn trampoline_get_team_total_health; impl_fn = get_team_total_health_impl;
159
+ regs = [ecx, edx]);
160
+
161
+ // ============================================================
162
+ // IsWormInSpecialState replacement (0x5226B0)
163
+ // ============================================================
164
+ // __usercall: EAX = team_index, ECX = worm_index, [ESP+4] = base
165
+ // RET 0x4, returns EAX = bool (1 if special state)
166
+
167
+ unsafe extern "cdecl" fn is_worm_in_special_state_impl(
168
+ team_index: u32, worm_index: u32, base: u32,
169
+ ) -> u32 {
170
+ let addr = base as usize
171
+ + team_index as usize * 0x51C
172
+ + worm_index as usize * 0x9C;
173
+ // Worm state at offset -0x598 from the computed team+worm address
174
+ let state_val = *((addr.wrapping_sub(0x598)) as *const u32);
175
+ match state_val {
176
+ 0x80 | 0x81 | 0x82 | 0x83 | 0x85 | 0x89 => 1,
177
+ _ => 0,
178
+ }
179
+ }
180
+
181
+ usercall_trampoline!(fn trampoline_is_worm_in_special_state; impl_fn = is_worm_in_special_state_impl;
182
+ regs = [eax, ecx]; stack_params = 1; ret_bytes = "0x4");
183
+
184
+ // ============================================================
185
+ // GetWormPosition replacement (0x522700)
186
+ // ============================================================
187
+ // __usercall: EAX = team_index, ECX = worm_index,
188
+ // [ESP+4] = base, [ESP+8] = out_x, [ESP+C] = out_y
189
+ // RET 0xC
190
+
191
+ unsafe extern "cdecl" fn get_worm_position_impl(
192
+ team_index: u32, worm_index: u32, base: u32, out_x: *mut i32, out_y: *mut i32,
193
+ ) {
194
+ let addr = base as usize
195
+ + team_index as usize * 0x51C
196
+ + worm_index as usize * 0x9C;
197
+ // Position X at offset -0x508, Y at -0x504
198
+ *out_x = *((addr.wrapping_sub(0x508)) as *const i32);
199
+ *out_y = *((addr.wrapping_sub(0x504)) as *const i32);
200
+ }
201
+
202
+ usercall_trampoline!(fn trampoline_get_worm_position; impl_fn = get_worm_position_impl;
203
+ regs = [eax, ecx]; stack_params = 3; ret_bytes = "0xC");
204
+
205
+ // ============================================================
206
+ // HasFullHealthWorm replacement (0x5228D0)
207
+ // ============================================================
208
+ // __usercall: EAX = base
209
+ // plain RET, returns EAX = bool (1 if any team has a worm at full health)
210
+
211
+ unsafe extern "cdecl" fn has_full_health_worm_impl(base: u32) -> u32 {
212
+ let state = &*(base as *const TeamWeaponState);
213
+ let base_ptr = base as *const u8;
214
+
215
+ // Iterate teams 1..=team_count
216
+ // Team data pointer: base + 0x518 + (i-1)*0x51C = base + 0x518 for team 1
217
+ for i in 0..state.team_count {
218
+ let team_ptr = base_ptr.add(0x518 + i as usize * 0x51C);
219
+ // Eliminated flag at team_ptr - 0xC
220
+ if *(team_ptr.sub(0xC) as *const i32) != 0 {
221
+ continue;
222
+ }
223
+ // Worm count at team_ptr[0]
224
+ let worm_count = *(team_ptr as *const i32);
225
+ if worm_count > 0 {
226
+ // Worm data at team_ptr - 0x4F8 (= ESI + -0x13E DWORDs)
227
+ let mut worm_ptr = team_ptr.sub(0x4F8);
228
+ for _ in 0..worm_count {
229
+ if *(worm_ptr as *const i32) == 100 {
230
+ return 1;
231
+ }
232
+ worm_ptr = worm_ptr.add(0x9C);
233
+ }
234
+ }
235
+ }
236
+ 0
237
+ }
238
+
239
+ usercall_trampoline!(fn trampoline_has_full_health_worm; impl_fn = has_full_health_worm_impl;
240
+ reg = eax);
241
+
242
  // ============================================================
243
  // Hook installation
244
  // ============================================================
 
256
  va::GET_AMMO,
257
  trampoline_get_ammo as *const (),
258
  )?;
259
+
260
+ let _ = hook::install(
261
+ "CountTeamsByAlliance",
262
+ va::COUNT_TEAMS_BY_ALLIANCE,
263
+ trampoline_count_teams_by_alliance as *const (),
264
+ )?;
265
+
266
+ let _ = hook::install(
267
+ "GetTeamTotalHealth",
268
+ va::GET_TEAM_TOTAL_HEALTH,
269
+ trampoline_get_team_total_health as *const (),
270
+ )?;
271
+
272
+ let _ = hook::install(
273
+ "IsWormInSpecialState",
274
+ va::IS_WORM_IN_SPECIAL_STATE,
275
+ trampoline_is_worm_in_special_state as *const (),
276
+ )?;
277
+
278
+ let _ = hook::install(
279
+ "GetWormPosition",
280
+ va::GET_WORM_POSITION,
281
+ trampoline_get_worm_position as *const (),
282
+ )?;
283
+
284
+ let _ = hook::install(
285
+ "HasFullHealthWorm",
286
+ va::HAS_FULL_HEALTH_WORM,
287
+ trampoline_has_full_health_worm as *const (),
288
+ )?;
289
  }
290
 
291
  Ok(())