paavohtl Claude Opus 4.6 commited on
Commit
69c1c2b
·
1 Parent(s): 747a75c

refactor: split team/worm accessors from weapon.rs into team.rs

Browse files

Move CountTeamsByAlliance, GetTeamTotalHealth, IsWormInSpecialState,
GetWormPosition, HasFullHealthWorm to a new team.rs module. weapon.rs
now only contains AddAmmo and GetAmmo.

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

crates/openwa-wormkit/src/replacements/mod.rs CHANGED
@@ -1,6 +1,7 @@
1
  mod config;
2
  mod frontend;
3
  mod scheme;
 
4
  mod weapon;
5
 
6
  pub fn install_all() -> Result<(), String> {
@@ -8,5 +9,6 @@ pub fn install_all() -> Result<(), String> {
8
  scheme::install()?;
9
  config::install()?;
10
  weapon::install()?;
 
11
  Ok(())
12
  }
 
1
  mod config;
2
  mod frontend;
3
  mod scheme;
4
+ mod team;
5
  mod weapon;
6
 
7
  pub fn install_all() -> Result<(), String> {
 
9
  scheme::install()?;
10
  config::install()?;
11
  weapon::install()?;
12
+ team::install()?;
13
  Ok(())
14
  }
crates/openwa-wormkit/src/replacements/team.rs ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //! Team and worm state accessor hooks.
2
+ //!
3
+ //! Replaces WA.exe functions that access the TeamWeaponState area (DDGame + 0x4628):
4
+ //! - CountTeamsByAlliance (0x522030): count teams by alliance membership
5
+ //! - GetTeamTotalHealth (0x5224D0): sum worm health for a team
6
+ //! - IsWormInSpecialState (0x5226B0): check worm state flag
7
+ //! - GetWormPosition (0x522700): read worm X,Y coordinates
8
+ //! - HasFullHealthWorm (0x5228D0): check if any worm has full health
9
+
10
+ use openwa_types::address::va;
11
+ use openwa_types::ddgame::TeamWeaponState;
12
+
13
+ use crate::hook::{self, usercall_trampoline};
14
+
15
+ // ============================================================
16
+ // CountTeamsByAlliance replacement (0x522030)
17
+ // ============================================================
18
+ // __usercall: EAX = base, EDI = alliance_id
19
+ // plain RET
20
+
21
+ unsafe extern "cdecl" fn count_teams_by_alliance_impl(base: u32, alliance_id: i32) {
22
+ let state = &mut *(base as *mut TeamWeaponState);
23
+ state.current_alliance = alliance_id;
24
+ state.active_team_count = 0;
25
+ state.same_alliance_count = 0;
26
+ state.enemy_team_count = 0;
27
+
28
+ // Teams are 1-indexed; iterate 1..=team_count
29
+ // Per-team data: base + 0x510 + (team_1based - 1) * 0x51C
30
+ // At [+0]: alliance field, [+4]: alive flag
31
+ let base_ptr = base as *const u8;
32
+ for i in 0..state.team_count {
33
+ let team_data = base_ptr.add(0x510 + i as usize * 0x51C);
34
+ let team_alliance = *(team_data as *const i32);
35
+ let alive_flag = *(team_data.add(4) as *const i32);
36
+ if alive_flag != 0 && team_alliance >= 0 {
37
+ state.active_team_count += 1;
38
+ if team_alliance == alliance_id {
39
+ state.same_alliance_count += 1;
40
+ } else {
41
+ state.enemy_team_count += 1;
42
+ }
43
+ }
44
+ }
45
+ }
46
+
47
+ usercall_trampoline!(fn trampoline_count_teams_by_alliance; impl_fn = count_teams_by_alliance_impl;
48
+ regs = [eax, edi]);
49
+
50
+ // ============================================================
51
+ // GetTeamTotalHealth replacement (0x5224D0)
52
+ // ============================================================
53
+ // __fastcall: ECX = team_index, EDX = base
54
+ // plain RET, returns EAX = total health
55
+
56
+ unsafe extern "cdecl" fn get_team_total_health_impl(team_index: u32, base: u32) -> i32 {
57
+ let base_ptr = base as *const u8;
58
+ let team_entry = base_ptr.add(team_index as usize * 0x51C) as usize;
59
+
60
+ // Check eliminated flag at team_entry - 0x10
61
+ if *((team_entry - 0x10) as *const i32) != 0 {
62
+ return 0;
63
+ }
64
+
65
+ let worm_count = *((team_entry - 4) as *const i32);
66
+ let mut total = 0i32;
67
+ if worm_count > 0 {
68
+ let mut ptr = (team_entry - 0x4A0) as *const u8;
69
+ for _ in 0..worm_count {
70
+ total += *(ptr as *const i32);
71
+ ptr = ptr.add(0x9C);
72
+ }
73
+ }
74
+ total
75
+ }
76
+
77
+ usercall_trampoline!(fn trampoline_get_team_total_health; impl_fn = get_team_total_health_impl;
78
+ regs = [ecx, edx]);
79
+
80
+ // ============================================================
81
+ // IsWormInSpecialState replacement (0x5226B0)
82
+ // ============================================================
83
+ // __usercall: EAX = team_index, ECX = worm_index, [ESP+4] = base
84
+ // RET 0x4, returns EAX = bool (1 if special state)
85
+
86
+ unsafe extern "cdecl" fn is_worm_in_special_state_impl(
87
+ team_index: u32, worm_index: u32, base: u32,
88
+ ) -> u32 {
89
+ let addr = base as usize
90
+ + team_index as usize * 0x51C
91
+ + worm_index as usize * 0x9C;
92
+ // Worm state at offset -0x598 from the computed team+worm address
93
+ let state_val = *((addr.wrapping_sub(0x598)) as *const u32);
94
+ match state_val {
95
+ 0x80 | 0x81 | 0x82 | 0x83 | 0x85 | 0x89 => 1,
96
+ _ => 0,
97
+ }
98
+ }
99
+
100
+ usercall_trampoline!(fn trampoline_is_worm_in_special_state; impl_fn = is_worm_in_special_state_impl;
101
+ regs = [eax, ecx]; stack_params = 1; ret_bytes = "0x4");
102
+
103
+ // ============================================================
104
+ // GetWormPosition replacement (0x522700)
105
+ // ============================================================
106
+ // __usercall: EAX = team_index, ECX = worm_index,
107
+ // [ESP+4] = base, [ESP+8] = out_x, [ESP+C] = out_y
108
+ // RET 0xC
109
+
110
+ unsafe extern "cdecl" fn get_worm_position_impl(
111
+ team_index: u32, worm_index: u32, base: u32, out_x: *mut i32, out_y: *mut i32,
112
+ ) {
113
+ let addr = base as usize
114
+ + team_index as usize * 0x51C
115
+ + worm_index as usize * 0x9C;
116
+ // Position X at offset -0x508, Y at -0x504
117
+ *out_x = *((addr.wrapping_sub(0x508)) as *const i32);
118
+ *out_y = *((addr.wrapping_sub(0x504)) as *const i32);
119
+ }
120
+
121
+ usercall_trampoline!(fn trampoline_get_worm_position; impl_fn = get_worm_position_impl;
122
+ regs = [eax, ecx]; stack_params = 3; ret_bytes = "0xC");
123
+
124
+ // ============================================================
125
+ // HasFullHealthWorm replacement (0x5228D0)
126
+ // ============================================================
127
+ // __usercall: EAX = base
128
+ // plain RET, returns EAX = bool (1 if any team has a worm at full health)
129
+
130
+ unsafe extern "cdecl" fn has_full_health_worm_impl(base: u32) -> u32 {
131
+ let state = &*(base as *const TeamWeaponState);
132
+ let base_ptr = base as *const u8;
133
+
134
+ // Iterate teams 1..=team_count
135
+ // Team data pointer: base + 0x518 + (i-1)*0x51C = base + 0x518 for team 1
136
+ for i in 0..state.team_count {
137
+ let team_ptr = base_ptr.add(0x518 + i as usize * 0x51C);
138
+ // Eliminated flag at team_ptr - 0xC
139
+ if *(team_ptr.sub(0xC) as *const i32) != 0 {
140
+ continue;
141
+ }
142
+ // Worm count at team_ptr[0]
143
+ let worm_count = *(team_ptr as *const i32);
144
+ if worm_count > 0 {
145
+ // Worm data at team_ptr - 0x4F8 (= ESI + -0x13E DWORDs)
146
+ let mut worm_ptr = team_ptr.sub(0x4F8);
147
+ for _ in 0..worm_count {
148
+ if *(worm_ptr as *const i32) == 100 {
149
+ return 1;
150
+ }
151
+ worm_ptr = worm_ptr.add(0x9C);
152
+ }
153
+ }
154
+ }
155
+ 0
156
+ }
157
+
158
+ usercall_trampoline!(fn trampoline_has_full_health_worm; impl_fn = has_full_health_worm_impl;
159
+ reg = eax);
160
+
161
+ // ============================================================
162
+ // Hook installation
163
+ // ============================================================
164
+
165
+ pub fn install() -> Result<(), String> {
166
+ unsafe {
167
+ let _ = hook::install(
168
+ "CountTeamsByAlliance",
169
+ va::COUNT_TEAMS_BY_ALLIANCE,
170
+ trampoline_count_teams_by_alliance as *const (),
171
+ )?;
172
+
173
+ let _ = hook::install(
174
+ "GetTeamTotalHealth",
175
+ va::GET_TEAM_TOTAL_HEALTH,
176
+ trampoline_get_team_total_health as *const (),
177
+ )?;
178
+
179
+ let _ = hook::install(
180
+ "IsWormInSpecialState",
181
+ va::IS_WORM_IN_SPECIAL_STATE,
182
+ trampoline_is_worm_in_special_state as *const (),
183
+ )?;
184
+
185
+ let _ = hook::install(
186
+ "GetWormPosition",
187
+ va::GET_WORM_POSITION,
188
+ trampoline_get_worm_position as *const (),
189
+ )?;
190
+
191
+ let _ = hook::install(
192
+ "HasFullHealthWorm",
193
+ va::HAS_FULL_HEALTH_WORM,
194
+ trampoline_has_full_health_worm as *const (),
195
+ )?;
196
+ }
197
+
198
+ Ok(())
199
+ }
crates/openwa-wormkit/src/replacements/weapon.rs CHANGED
@@ -1,13 +1,8 @@
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,152 +88,6 @@ unsafe fn count_alive_worms(state: &TeamWeaponState, team_index: usize) -> bool
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,36 +105,6 @@ pub fn install() -> Result<(), String> {
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(())
 
1
+ //! Weapon ammo hooks.
2
  //!
3
+ //! Replaces WA.exe functions that manage weapon ammo in 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
 
7
  use openwa_types::address::va;
8
  use openwa_types::ddgame::TeamWeaponState;
 
88
  usercall_trampoline!(fn trampoline_get_ammo; impl_fn = get_ammo_impl;
89
  regs = [eax, esi, edx]);
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  // ============================================================
92
  // Hook installation
93
  // ============================================================
 
105
  va::GET_AMMO,
106
  trampoline_get_ammo as *const (),
107
  )?;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  }
109
 
110
  Ok(())