From 749b499db668dfb8e471d7610f13a835dc547045 Mon Sep 17 00:00:00 2001 From: The Dax Date: Thu, 19 Jun 2025 07:19:28 -0400 Subject: [PATCH] Refactor portions of lua.rs to reduce boilerplate -Create common methods create_segment_self and create_segment_target. These reduce the amount of copy-paste boilerplate code since the IPC queueing stuff basically never changes as far as I can tell. Now we simply specify the opcode and the data that goes with it, and off we go. Create_segment_self retains the current behaviour of using the player's actor id as both the source and target, and create_segment_target allows us to change the source and target in case commands are written that require one or the other to be different (Sending targeted players to other coords/zones? Bringing a player to the GM/source user? Several possibilities.). --- src/world/lua.rs | 75 ++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/src/world/lua.rs b/src/world/lua.rs index e9a239d..f7d2c23 100644 --- a/src/world/lua.rs +++ b/src/world/lua.rs @@ -38,25 +38,36 @@ impl LuaPlayer { self.queued_segments.push(segment); } - fn send_message(&mut self, message: &str) { + fn create_segment_target(&mut self, op_code: ServerZoneIpcType, data: ServerZoneIpcData, source_actor: u32, target_actor: u32) { let ipc = ServerZoneIpcSegment { - op_code: ServerZoneIpcType::ServerChatMessage, + op_code, timestamp: timestamp_secs(), - data: ServerZoneIpcData::ServerChatMessage { - message: message.to_string(), - unk: 0, - }, + data, ..Default::default() }; self.queue_segment(PacketSegment { - source_actor: self.player_data.actor_id, - target_actor: self.player_data.actor_id, + source_actor, + target_actor, segment_type: SegmentType::Ipc, data: SegmentData::Ipc { data: ipc }, }); } + fn create_segment_self(&mut self, op_code: ServerZoneIpcType, data: ServerZoneIpcData) { + self.create_segment_target(op_code, data, self.player_data.actor_id, self.player_data.actor_id); + } + + fn send_message(&mut self, message: &str) { + let op_code = ServerZoneIpcType::ServerChatMessage; + let data = ServerZoneIpcData::ServerChatMessage { + message: message.to_string(), + unk: 0, + }; + + self.create_segment_self(op_code, data); + } + fn give_status_effect(&mut self, effect_id: u16, duration: f32) { self.status_effects.add(effect_id, duration); } @@ -69,46 +80,28 @@ impl LuaPlayer { scene_flags: u32, param: u8, ) { - let ipc = ServerZoneIpcSegment { - op_code: ServerZoneIpcType::EventScene, - timestamp: timestamp_secs(), - data: ServerZoneIpcData::EventScene(EventScene { - actor_id: target, - event_id, - scene, - scene_flags, - unk2: param, - ..Default::default() - }), + let op_code = ServerZoneIpcType::EventScene; + let data = ServerZoneIpcData::EventScene(EventScene { + actor_id: target, + event_id, + scene, + scene_flags, + unk2: param, ..Default::default() - }; - - self.queue_segment(PacketSegment { - source_actor: self.player_data.actor_id, - target_actor: self.player_data.actor_id, - segment_type: SegmentType::Ipc, - data: SegmentData::Ipc { data: ipc }, }); + + self.create_segment_self(op_code, data); } fn set_position(&mut self, position: Position, rotation: f32) { - let ipc = ServerZoneIpcSegment { - op_code: ServerZoneIpcType::Warp, - timestamp: timestamp_secs(), - data: ServerZoneIpcData::Warp(Warp { - dir: write_quantized_rotation(&rotation), - position, - ..Default::default() - }), + let op_code = ServerZoneIpcType::Warp; + let data = ServerZoneIpcData::Warp(Warp { + dir: write_quantized_rotation(&rotation), + position, ..Default::default() - }; - - self.queue_segment(PacketSegment { - source_actor: self.player_data.actor_id, - target_actor: self.player_data.actor_id, - segment_type: SegmentType::Ipc, - data: SegmentData::Ipc { data: ipc }, }); + + self.create_segment_self(op_code, data); } fn change_territory(&mut self, zone_id: u16) {