1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-06-21 07:27:45 +00:00

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.).
This commit is contained in:
The Dax 2025-06-19 07:19:28 -04:00 committed by Joshua Goins
parent 9567c8f38e
commit 749b499db6

View file

@ -38,25 +38,36 @@ impl LuaPlayer {
self.queued_segments.push(segment); 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 { let ipc = ServerZoneIpcSegment {
op_code: ServerZoneIpcType::ServerChatMessage, op_code,
timestamp: timestamp_secs(), timestamp: timestamp_secs(),
data: ServerZoneIpcData::ServerChatMessage { data,
message: message.to_string(),
unk: 0,
},
..Default::default() ..Default::default()
}; };
self.queue_segment(PacketSegment { self.queue_segment(PacketSegment {
source_actor: self.player_data.actor_id, source_actor,
target_actor: self.player_data.actor_id, target_actor,
segment_type: SegmentType::Ipc, segment_type: SegmentType::Ipc,
data: SegmentData::Ipc { data: 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) { fn give_status_effect(&mut self, effect_id: u16, duration: f32) {
self.status_effects.add(effect_id, duration); self.status_effects.add(effect_id, duration);
} }
@ -69,46 +80,28 @@ impl LuaPlayer {
scene_flags: u32, scene_flags: u32,
param: u8, param: u8,
) { ) {
let ipc = ServerZoneIpcSegment { let op_code = ServerZoneIpcType::EventScene;
op_code: ServerZoneIpcType::EventScene, let data = ServerZoneIpcData::EventScene(EventScene {
timestamp: timestamp_secs(), actor_id: target,
data: ServerZoneIpcData::EventScene(EventScene { event_id,
actor_id: target, scene,
event_id, scene_flags,
scene, unk2: param,
scene_flags,
unk2: param,
..Default::default()
}),
..Default::default() ..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) { fn set_position(&mut self, position: Position, rotation: f32) {
let ipc = ServerZoneIpcSegment { let op_code = ServerZoneIpcType::Warp;
op_code: ServerZoneIpcType::Warp, let data = ServerZoneIpcData::Warp(Warp {
timestamp: timestamp_secs(), dir: write_quantized_rotation(&rotation),
data: ServerZoneIpcData::Warp(Warp { position,
dir: write_quantized_rotation(&rotation),
position,
..Default::default()
}),
..Default::default() ..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) { fn change_territory(&mut self, zone_id: u16) {