paavohtl Claude Opus 4.6 (1M context) commited on
Commit
e98f3c6
·
1 Parent(s): 985fb50

refactor: fix CTaskCloud vtable (12→7 slots), add constructor and addresses

Browse files

The vtable was incorrectly sized at 12 slots — verified as 7 by gap to
CTaskCPU vtable (0x669D54 - 0x669D38 = 0x1C). CTaskCloud does not
override ProcessFrame; all logic runs in HandleMessage via FrameFinish.

- Fix vtable size 12→7, remove phantom process_frame slot 7
- Add write_replay_state (slot 0, 0x548430) to vtable definition
- Add ReadReplayState (0x548370) and WriteReplayState address constants
- Rename _unknown_44 → y_seed (EDI from constructor, seeds initial pos_y)
- Document _unknown_54 as unused padding (never touched by any code path)
- Add CloudType enum and CTaskCloud::init() Rust constructor
- Export new addresses and CloudType

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

crates/openwa-core/src/address.rs CHANGED
@@ -37,7 +37,10 @@ pub mod va {
37
  CTASK_VT1_FREE, CTASK_VT2_HANDLE_MESSAGE, CTASK_VT3, CTASK_VT5, CTASK_VT6,
38
  CTASK_VT7_PROCESS_FRAME, CTASK_VTABLE,
39
  };
40
- pub use crate::task::cloud::{CTASK_CLOUD_CTOR, CTASK_CLOUD_VTABLE};
 
 
 
41
  pub use crate::task::filter::{CTASK_FILTER_CTOR, CTASK_FILTER_VTABLE};
42
  pub use crate::task::fire::{CTASK_FIRE_CTOR, CTASK_FIRE_VTABLE};
43
  pub use crate::task::game_task::{
 
37
  CTASK_VT1_FREE, CTASK_VT2_HANDLE_MESSAGE, CTASK_VT3, CTASK_VT5, CTASK_VT6,
38
  CTASK_VT7_PROCESS_FRAME, CTASK_VTABLE,
39
  };
40
+ pub use crate::task::cloud::{
41
+ CTASK_CLOUD_CTOR, CTASK_CLOUD_READ_REPLAY_STATE, CTASK_CLOUD_VTABLE,
42
+ CTASK_CLOUD_WRITE_REPLAY_STATE,
43
+ };
44
  pub use crate::task::filter::{CTASK_FILTER_CTOR, CTASK_FILTER_VTABLE};
45
  pub use crate::task::fire::{CTASK_FIRE_CTOR, CTASK_FIRE_VTABLE};
46
  pub use crate::task::game_task::{
crates/openwa-core/src/task/cloud.rs CHANGED
@@ -1,29 +1,40 @@
1
  use super::base::CTask;
2
  use crate::fixed::Fixed;
 
3
  use crate::FieldRegistry;
4
 
5
  crate::define_addresses! {
6
  class "CTaskCloud" {
7
  /// CTaskCloud vtable - cloud/airstrike entity
8
  vtable CTASK_CLOUD_VTABLE = 0x0066_9D38;
 
 
9
  ctor CTASK_CLOUD_CTOR = 0x0054_82E0;
 
 
 
 
 
 
10
  }
11
  }
12
 
13
- /// CTaskCloud vtable — 12 slots. Extends CTask base (8 slots) with cloud behavior.
14
  ///
15
- /// Vtable at Ghidra 0x669D38.
16
- #[openwa_core::vtable(size = 12, va = 0x0066_9D38, class = "CTaskCloud")]
 
 
17
  pub struct CTaskCloudVTable {
 
 
 
 
18
  /// HandleMessage — processes cloud messages (wind updates, render).
19
  /// thiscall + 4 stack params, RET 0x10.
20
  #[slot(2)]
21
  pub handle_message:
22
  fn(this: *mut CTaskCloud, sender: *mut CTask, msg_type: u32, size: u32, data: *const u8),
23
- /// ProcessFrame — per-frame cloud update.
24
- /// thiscall + 1 stack param (flags), RET 0x4.
25
- #[slot(7)]
26
- pub process_frame: fn(this: *mut CTaskCloud, flags: u32),
27
  }
28
 
29
  /// Airstrike / weather cloud task.
@@ -31,26 +42,32 @@ pub struct CTaskCloudVTable {
31
  /// Extends CTask directly (not CGameTask). Clouds drift horizontally with wind,
32
  /// scroll on a parallax layer, and render as a single sprite.
33
  ///
34
- /// Allocation: 0x74 bytes (operator new in CTaskTeam__CreateWeatherFilter).
35
- /// Constructor: 0x5482E0 (usercall ESI=this, EAX=parent).
36
- /// Vtable: 0x669D38. Class type byte: 0x17.
37
  ///
38
  /// Three cloud sizes chosen by `cloud_type` param (0/1/2):
39
  /// - type 0: sprite 0x268 (large), vel_y 0x200
40
  /// - type 1: sprite 0x269 (medium), vel_y 0x166
41
  /// - type 2: sprite 0x26A (small), vel_y 0xCC
42
  ///
43
- /// Source: Ghidra decompilation of 0x5482E0 (constructor) and
44
- /// 0x5484C0 (HandleMessage update + render branches).
 
 
 
 
 
45
  #[derive(FieldRegistry)]
46
  #[repr(C)]
47
  pub struct CTaskCloud {
48
  /// 0x00–0x2F: CTask base
49
  pub base: CTask<*const CTaskCloudVTable>,
50
- /// 0x30: Parallax scroll layer depth (Fixed; 0x190000 = 25.0 at spawn,
51
  /// decrements by 1 each cloud spawned in a batch)
52
  pub layer_depth: Fixed,
53
- /// 0x34: Y position (Fixed 16.16); updated each frame: pos_y += vel_y
 
54
  pub pos_y: Fixed,
55
  /// 0x38: Y velocity (Fixed 16.16; set by cloud type: large=0x200, medium=0x166, small=0xCC)
56
  pub vel_y: Fixed,
@@ -58,21 +75,26 @@ pub struct CTaskCloud {
58
  pub sprite_id: u32,
59
  /// 0x40: X position (Fixed 16.16); wraps at landscape bounds each frame
60
  pub pos_x: Fixed,
61
- /// 0x44: Unknown (set from EDI at call site)
62
- pub _unknown_44: u32,
 
 
 
63
  /// 0x48: X velocity base (Fixed 16.16)
64
  pub vel_x: Fixed,
65
  /// 0x4C: Current wind acceleration (Fixed); converges toward wind_target each frame
66
  pub wind_accel: Fixed,
67
  /// 0x50: Target wind speed (Fixed); set by message 0x54 (wind-change event)
68
  pub wind_target: Fixed,
69
- /// 0x54–0x73: Unknown
 
 
70
  pub _unknown_54: [u8; 0x20],
71
  }
72
 
73
  const _: () = assert!(core::mem::size_of::<CTaskCloud>() == 0x74);
74
 
75
- // Generate typed vtable method wrappers: handle_message(), process_frame().
76
  bind_CTaskCloudVTable!(CTaskCloud, base.vtable);
77
 
78
  use crate::game::TaskMessage;
@@ -166,3 +188,69 @@ pub unsafe extern "thiscall" fn cloud_handle_message(
166
  // Broadcast to children — raw-pointer version avoids noalias UB
167
  CTask::broadcast_message_raw(this as *mut CTask, sender, msg_type, size, data);
168
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  use super::base::CTask;
2
  use crate::fixed::Fixed;
3
+ use crate::game::class_type::ClassType;
4
  use crate::FieldRegistry;
5
 
6
  crate::define_addresses! {
7
  class "CTaskCloud" {
8
  /// CTaskCloud vtable - cloud/airstrike entity
9
  vtable CTASK_CLOUD_VTABLE = 0x0066_9D38;
10
+ /// CTaskCloud constructor (usercall: ESI=this, EAX=parent, EDI=y_seed,
11
+ /// stack: cloud_type, layer_depth, pos_x, vel_x). RET 0x10.
12
  ctor CTASK_CLOUD_CTOR = 0x0054_82E0;
13
+ /// CTaskCloud::WriteReplayState — serializes cloud state to replay stream.
14
+ /// thiscall + 1 stack param (stream ptr), RET 0x4.
15
+ vmethod CTASK_CLOUD_WRITE_REPLAY_STATE = 0x0054_8430;
16
+ /// CTaskCloud::ReadReplayState — deserializes cloud state from replay stream.
17
+ /// usercall (ESI=stream, EDI=this). Not a vtable method.
18
+ fn CTASK_CLOUD_READ_REPLAY_STATE = 0x0054_8370;
19
  }
20
  }
21
 
22
+ /// CTaskCloud vtable — 7 slots. Same layout as CTask base; overrides slots 0 and 2.
23
  ///
24
+ /// Vtable at Ghidra 0x669D38. Size verified by gap to next vtable (CTaskCPU at 0x669D54).
25
+ /// CTaskCloud does NOT override ProcessFrame (slot 6) — all update logic is in
26
+ /// HandleMessage, responding to the FrameFinish message.
27
+ #[openwa_core::vtable(size = 7, va = 0x0066_9D38, class = "CTaskCloud")]
28
  pub struct CTaskCloudVTable {
29
+ /// WriteReplayState — serializes cloud state to replay stream.
30
+ /// thiscall + 1 stack param (stream ptr), RET 0x4.
31
+ #[slot(0)]
32
+ pub write_replay_state: fn(this: *mut CTaskCloud, stream: *mut u8),
33
  /// HandleMessage — processes cloud messages (wind updates, render).
34
  /// thiscall + 4 stack params, RET 0x10.
35
  #[slot(2)]
36
  pub handle_message:
37
  fn(this: *mut CTaskCloud, sender: *mut CTask, msg_type: u32, size: u32, data: *const u8),
 
 
 
 
38
  }
39
 
40
  /// Airstrike / weather cloud task.
 
42
  /// Extends CTask directly (not CGameTask). Clouds drift horizontally with wind,
43
  /// scroll on a parallax layer, and render as a single sprite.
44
  ///
45
+ /// Allocation: 0x74 bytes (operator new in CTaskTeam__CreateWeatherFilter 0x552960).
46
+ /// Constructor: 0x5482E0 (usercall ESI=this, EAX=parent, EDI=y_seed).
47
+ /// Vtable: 0x669D38. Class type byte: 0x17 (ClassType::Cloud).
48
  ///
49
  /// Three cloud sizes chosen by `cloud_type` param (0/1/2):
50
  /// - type 0: sprite 0x268 (large), vel_y 0x200
51
  /// - type 1: sprite 0x269 (medium), vel_y 0x166
52
  /// - type 2: sprite 0x26A (small), vel_y 0xCC
53
  ///
54
+ /// CreateWeatherFilter spawns clouds with a deterministic LCG (seed 0x12345678),
55
+ /// randomizing pos_x within level bounds and vel_x with random sign. The y_seed
56
+ /// (cloud count) contributes to the initial pos_y via `(pos_x + y_seed) & 0xFFFF`,
57
+ /// placing clouds near y ≈ 0 so they drift downward.
58
+ ///
59
+ /// Source: Ghidra decompilation of 0x5482E0 (constructor), 0x5484C0 (HandleMessage),
60
+ /// 0x548430 (WriteReplayState), 0x548370 (ReadReplayState).
61
  #[derive(FieldRegistry)]
62
  #[repr(C)]
63
  pub struct CTaskCloud {
64
  /// 0x00–0x2F: CTask base
65
  pub base: CTask<*const CTaskCloudVTable>,
66
+ /// 0x30: Parallax scroll layer depth (Fixed; starts at 0x190000 = 25.0,
67
  /// decrements by 1 each cloud spawned in a batch)
68
  pub layer_depth: Fixed,
69
+ /// 0x34: Y position (Fixed 16.16); updated each frame: pos_y += vel_y.
70
+ /// Initial value: (pos_x + y_seed) & 0xFFFF (near zero, clouds drift downward).
71
  pub pos_y: Fixed,
72
  /// 0x38: Y velocity (Fixed 16.16; set by cloud type: large=0x200, medium=0x166, small=0xCC)
73
  pub vel_y: Fixed,
 
75
  pub sprite_id: u32,
76
  /// 0x40: X position (Fixed 16.16); wraps at landscape bounds each frame
77
  pub pos_x: Fixed,
78
+ /// 0x44: Y seed value — EDI register from constructor call site.
79
+ /// In CreateWeatherFilter this is the total cloud count (e.g. 10).
80
+ /// Used to compute initial pos_y: (pos_x + y_seed) & 0xFFFF.
81
+ /// Serialized in replay state but not read during gameplay.
82
+ pub y_seed: u32,
83
  /// 0x48: X velocity base (Fixed 16.16)
84
  pub vel_x: Fixed,
85
  /// 0x4C: Current wind acceleration (Fixed); converges toward wind_target each frame
86
  pub wind_accel: Fixed,
87
  /// 0x50: Target wind speed (Fixed); set by message 0x54 (wind-change event)
88
  pub wind_target: Fixed,
89
+ /// 0x54–0x73: Unused padding. Allocation is 0x74 bytes but only 0x54 bytes are
90
+ /// initialized (memset in CreateWeatherFilter). Not touched by constructor,
91
+ /// HandleMessage, WriteReplayState, or ReadReplayState.
92
  pub _unknown_54: [u8; 0x20],
93
  }
94
 
95
  const _: () = assert!(core::mem::size_of::<CTaskCloud>() == 0x74);
96
 
97
+ // Generate typed vtable method wrappers: write_replay_state(), handle_message().
98
  bind_CTaskCloudVTable!(CTaskCloud, base.vtable);
99
 
100
  use crate::game::TaskMessage;
 
188
  // Broadcast to children — raw-pointer version avoids noalias UB
189
  CTask::broadcast_message_raw(this as *mut CTask, sender, msg_type, size, data);
190
  }
191
+
192
+ /// Cloud type determines sprite and vertical velocity.
193
+ #[repr(u32)]
194
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
195
+ pub enum CloudType {
196
+ /// Large cloud: sprite 0x268, vel_y 0x200
197
+ Large = 0,
198
+ /// Medium cloud: sprite 0x269, vel_y 0x166
199
+ Medium = 1,
200
+ /// Small cloud: sprite 0x26A, vel_y 0xCC
201
+ Small = 2,
202
+ }
203
+
204
+ impl CTaskCloud {
205
+ /// Initialize CTaskCloud fields on an already-constructed CTask base.
206
+ ///
207
+ /// Pure Rust equivalent of the original constructor at 0x5482E0.
208
+ /// The caller must have already:
209
+ /// 1. Allocated 0x74 bytes (e.g. via `wa_malloc(0x74)`)
210
+ /// 2. Zeroed the first 0x54 bytes (matches original `_memset(ptr, 0, 0x54)`)
211
+ /// 3. Called `CTask::Constructor` (0x5625A0) to set up the base task
212
+ ///
213
+ /// This function then sets the vtable, class type, and all cloud-specific fields.
214
+ ///
215
+ /// # Safety
216
+ /// `this` must point to a valid, allocated CTaskCloud with CTask base initialized.
217
+ pub unsafe fn init(
218
+ this: *mut CTaskCloud,
219
+ cloud_type: CloudType,
220
+ layer_depth: Fixed,
221
+ pos_x: Fixed,
222
+ vel_x: Fixed,
223
+ y_seed: u32,
224
+ ) {
225
+ use crate::rebase::rb;
226
+
227
+ // Set vtable pointer to the CTaskCloud vtable (rebased for ASLR)
228
+ (*this).base.vtable = rb(CTASK_CLOUD_VTABLE) as *const CTaskCloudVTable;
229
+ (*this).base.class_type = ClassType::Cloud;
230
+
231
+ // Position: x is the initial horizontal position, y is derived from x + seed
232
+ (*this).pos_x = pos_x;
233
+ (*this).pos_y = Fixed((pos_x.0.wrapping_add(y_seed as i32)) & 0xFFFF);
234
+ (*this).layer_depth = layer_depth;
235
+ (*this).y_seed = y_seed;
236
+ (*this).vel_x = vel_x;
237
+ (*this).wind_accel = Fixed(0);
238
+ (*this).wind_target = Fixed(0);
239
+
240
+ // Set type-dependent velocity and sprite
241
+ match cloud_type {
242
+ CloudType::Large => {
243
+ (*this).vel_y = Fixed(0x200);
244
+ (*this).sprite_id = 0x268;
245
+ }
246
+ CloudType::Medium => {
247
+ (*this).vel_y = Fixed(0x166);
248
+ (*this).sprite_id = 0x269;
249
+ }
250
+ CloudType::Small => {
251
+ (*this).vel_y = Fixed(0xCC);
252
+ (*this).sprite_id = 0x26A;
253
+ }
254
+ }
255
+ }
256
+ }
crates/openwa-core/src/task/mod.rs CHANGED
@@ -18,7 +18,7 @@ pub use arrow::{CTaskArrow, CTaskArrowVTable};
18
  pub use base::{
19
  CTask, CTaskBfsIter, CTaskVtable, SharedDataIter, SharedDataNode, SharedDataTable, Task, Vtable,
20
  };
21
- pub use cloud::{CTaskCloud, CTaskCloudVTable};
22
  pub use filter::{CTaskFilter, CTaskFilterVTable};
23
  pub use fire::{CTaskFire, CTaskFireVTable};
24
  pub use game_task::{CGameTask, SoundEmitter, SoundEmitterVTable};
 
18
  pub use base::{
19
  CTask, CTaskBfsIter, CTaskVtable, SharedDataIter, SharedDataNode, SharedDataTable, Task, Vtable,
20
  };
21
+ pub use cloud::{CTaskCloud, CTaskCloudVTable, CloudType};
22
  pub use filter::{CTaskFilter, CTaskFilterVTable};
23
  pub use fire::{CTaskFire, CTaskFireVTable};
24
  pub use game_task::{CGameTask, SoundEmitter, SoundEmitterVTable};