update network management

This commit is contained in:
AleaJactaEst 2023-11-25 15:16:15 +01:00
parent 3c510cbcc5
commit ad622360f5
3 changed files with 73 additions and 38 deletions

View file

@ -54,34 +54,34 @@ func is_local_authority() -> bool:
#sync player on clients #sync player on clients
func _physics_process(_delta): func _physics_process(_delta):
return return
if !is_local_authority(): # if !is_local_authority():
if not processed_position: # if not processed_position:
PlayerRef.character_node.position = sync_position # PlayerRef.character_node.position = sync_position
processed_position = true # processed_position = true
PlayerRef.mesh_ref.rotation = sync_mesh_rotation # PlayerRef.mesh_ref.rotation = sync_mesh_rotation
PlayerRef.input_direction = sync_direction # PlayerRef.input_direction = sync_direction
PlayerRef.gait = sync_gait # PlayerRef.gait = sync_gait
PlayerRef.stance = sync_stance # PlayerRef.stance = sync_stance
PlayerRef.rotation_mode = sync_rotation_mode # PlayerRef.rotation_mode = sync_rotation_mode
PlayerRef.camera_root.VObject.transform = sync_camera_v_transform # PlayerRef.camera_root.VObject.transform = sync_camera_v_transform
PlayerRef.camera_root.HObject.transform = sync_camera_h_transform # PlayerRef.camera_root.HObject.transform = sync_camera_h_transform
PlayerRef.camera_root.view_mode = sync_CameraHOffset # PlayerRef.camera_root.view_mode = sync_CameraHOffset
PlayerRef.camera_root.CameraHOffset = sync_CameraHOffset # PlayerRef.camera_root.CameraHOffset = sync_CameraHOffset
PlayerRef.movement_state = sync_movement_state # PlayerRef.movement_state = sync_movement_state
PlayerRef.movement_action = sync_movement_action # PlayerRef.movement_action = sync_movement_action
# PlayerRef.velocity = sync_velocity ## PlayerRef.velocity = sync_velocity
PlayerRef.input_is_moving = sync_input_is_moving # PlayerRef.input_is_moving = sync_input_is_moving
return # return
sync_position = PlayerRef.character_node.position # sync_position = PlayerRef.character_node.position
sync_mesh_rotation = PlayerRef.mesh_ref.rotation # sync_mesh_rotation = PlayerRef.mesh_ref.rotation
sync_direction = PlayerRef.input_direction # sync_direction = PlayerRef.input_direction
sync_gait = PlayerRef.gait # sync_gait = PlayerRef.gait
sync_stance = PlayerRef.stance # sync_stance = PlayerRef.stance
sync_rotation_mode = PlayerRef.rotation_mode # sync_rotation_mode = PlayerRef.rotation_mode
sync_camera_h_transform = PlayerRef.camera_root.HObject.transform # sync_camera_h_transform = PlayerRef.camera_root.HObject.transform
sync_camera_v_transform = PlayerRef.camera_root.VObject.transform # sync_camera_v_transform = PlayerRef.camera_root.VObject.transform
sync_movement_state = PlayerRef.movement_state # sync_movement_state = PlayerRef.movement_state
sync_movement_action = PlayerRef.movement_action # sync_movement_action = PlayerRef.movement_action
sync_input_is_moving = PlayerRef.input_is_moving # sync_input_is_moving = PlayerRef.input_is_moving
sync_view_mode = PlayerRef.camera_root.view_mode # sync_view_mode = PlayerRef.camera_root.view_mode
sync_CameraHOffset = PlayerRef.camera_root.CameraHOffset # sync_CameraHOffset = PlayerRef.camera_root.CameraHOffset

View file

@ -134,6 +134,7 @@ func get_event_received():
var rx:float = data.decode_double(2+32) var rx:float = data.decode_double(2+32)
var ry:float = data.decode_double(2+40) var ry:float = data.decode_double(2+40)
var rz:float = data.decode_double(2+48) var rz:float = data.decode_double(2+48)
var _tick:int = data.decode_double(2+1)
print("MyID:", id) print("MyID:", id)
#print("id:", id, " x:", x, " y:", y, " z:", z) #print("id:", id, " x:", x, " y:", y, " z:", z)
update_my_position.emit(Vector3(x, y, z), Vector3(rx, ry, rz)) update_my_position.emit(Vector3(x, y, z), Vector3(rx, ry, rz))
@ -154,11 +155,12 @@ func get_event_received():
var rx:float = data.decode_double(pos+32) var rx:float = data.decode_double(pos+32)
var ry:float = data.decode_double(pos+40) var ry:float = data.decode_double(pos+40)
var rz:float = data.decode_double(pos+48) var rz:float = data.decode_double(pos+48)
pos += 56 var tick:int = data.decode_u8(pos+56)
pos += 57
if mid == id: if mid == id:
#print("Me id:", mid, " x:", mx, " y:", my, " z:", mz) #print("Me id:", mid, " x:", mx, " y:", my, " z:", mz)
continue continue
print("pos:", pos, " id:", mid, " x:", mx, " y:", my, " z:", mz, " rx:", rx, " ry:", ry, " rz:", rz) print("pos:", pos, " id:", mid, " tick:", tick, " x:", mx, " y:", my, " z:", mz, " rx:", rx, " ry:", ry, " rz:", rz)
update_player_position.emit(mid, Vector3(mx, my, mz), Vector3(rx, ry, rz)) update_player_position.emit(mid, Vector3(mx, my, mz), Vector3(rx, ry, rz))
var nbuserremove:int = data[pos] var nbuserremove:int = data[pos]
pos += 1 pos += 1

View file

@ -62,6 +62,7 @@ fn push_u64(data:&mut Vec<u8>, value:u64) {
data.push(buf[6]); data.push(buf[6]);
data.push(buf[7]); data.push(buf[7]);
} }
fn push_f64(data:&mut Vec<u8>, value:f64) { fn push_f64(data:&mut Vec<u8>, value:f64) {
let mut buf = [0; 8]; let mut buf = [0; 8];
LittleEndian::write_f64(&mut buf, value); LittleEndian::write_f64(&mut buf, value);
@ -75,6 +76,10 @@ fn push_f64(data:&mut Vec<u8>, value:f64) {
data.push(buf[7]); data.push(buf[7]);
} }
fn push_u8(data:&mut Vec<u8>, value:u8) {
data.push(value);
}
/* /*
enum Simple { enum Simple {
Error(String), Error(String),
@ -125,6 +130,7 @@ struct User {
position_updated: bool, position_updated: bool,
lasttime: Instant, lasttime: Instant,
sendfull: u8, sendfull: u8,
lastupdate: u8,
} }
impl PartialEq for User { impl PartialEq for User {
@ -143,6 +149,18 @@ impl User {
} }
} }
*/ */
pub fn clear_player(&mut self) {
self.x = 0.0;
self.y = 0.0;
self.z = 0.0;
self.rx = 0.0;
self.ry = 0.0;
self.rz = 0.0;
self.position_updated = false;
self.lasttime = Instant::now();
self.sendfull = 0;
self.lastupdate = 0;
}
pub fn set_inactive(&mut self) { pub fn set_inactive(&mut self) {
self.active = false; self.active = false;
self.lasttime = Instant::now(); self.lasttime = Instant::now();
@ -160,6 +178,7 @@ impl User {
} }
pub fn clear_username(&mut self) { pub fn clear_username(&mut self) {
self.username = "".to_string(); self.username = "".to_string();
self.clear_player();
} }
pub fn clear_address(&mut self) { pub fn clear_address(&mut self) {
self.address = Address::new(Ipv4Addr::new(0, 0, 0, 0), 0); self.address = Address::new(Ipv4Addr::new(0, 0, 0, 0), 0);
@ -167,30 +186,36 @@ impl User {
pub fn clear_position_updated(&mut self) { pub fn clear_position_updated(&mut self) {
self.position_updated = false; self.position_updated = false;
} }
pub fn update_pos_rot(&mut self, x:f64, y:f64, z:f64, rx:f64, ry:f64, rz:f64) { pub fn update_pos_rot(&mut self, x:f64, y:f64, z:f64, rx:f64, ry:f64, rz:f64, lastupdate:u8) {
if self.x != x { if self.x != x {
self.x = x; self.x = x;
self.position_updated = true; self.position_updated = true;
self.lastupdate = lastupdate;
} }
if self.y != y { if self.y != y {
self.y = y; self.y = y;
self.position_updated = true; self.position_updated = true;
self.lastupdate = lastupdate;
} }
if self.z != z { if self.z != z {
self.z = z; self.z = z;
self.position_updated = true; self.position_updated = true;
self.lastupdate = lastupdate;
} }
if self.rx != rx { if self.rx != rx {
self.rx = rx; self.rx = rx;
self.position_updated = true; self.position_updated = true;
self.lastupdate = lastupdate;
} }
if self.ry != ry { if self.ry != ry {
self.ry = ry; self.ry = ry;
self.position_updated = true; self.position_updated = true;
self.lastupdate = lastupdate;
} }
if self.rz != rz { if self.rz != rz {
self.rz = rz; self.rz = rz;
self.position_updated = true; self.position_updated = true;
self.lastupdate = lastupdate;
} }
} }
/* /*
@ -207,6 +232,7 @@ impl User {
push_f64(data, self.rx); push_f64(data, self.rx);
push_f64(data, self.ry); push_f64(data, self.ry);
push_f64(data, self.rz); push_f64(data, self.rz);
push_u8(data, self.lastupdate);
} }
pub fn push_packet_id(&self, data:&mut Vec<u8>) { pub fn push_packet_id(&self, data:&mut Vec<u8>) {
push_u64(data, self.id); push_u64(data, self.id);
@ -335,13 +361,13 @@ impl Users {
pub fn add(&mut self, username:String, address: Address) -> u64 { pub fn add(&mut self, username:String, address: Address) -> u64 {
let id = self.get_new_id(); let id = self.get_new_id();
self.player_active += 1; self.player_active += 1;
self.users.push( User { active: true, username:username, address: address, id: id, x: 0.0, y: 10.0, z:0.0, rx: 0.0, ry: 0.0, rz:0.0, position_updated:true, lasttime: Instant::now(), sendfull:10} ); self.users.push( User { active: true, username:username, address: address, id: id, x: 0.0, y: 10.0, z:0.0, rx: 0.0, ry: 0.0, rz:0.0, position_updated:true, lasttime: Instant::now(), sendfull:10, lastupdate:0} );
id id
} }
pub fn update_pos_rot(&mut self, address: Address, x:f64, y:f64, z:f64, rx:f64, ry:f64, rz:f64) { pub fn update_pos_rot(&mut self, address: Address, x:f64, y:f64, z:f64, rx:f64, ry:f64, rz:f64, tick:u8) {
for user in self.users.iter_mut() { for user in self.users.iter_mut() {
if user.address == address { if user.address == address {
user.update_pos_rot(x, y, z, rx, ry, rz); user.update_pos_rot(x, y, z, rx, ry, rz, tick);
} }
} }
} }
@ -717,6 +743,7 @@ fn main() -> anyhow::Result<()> {
let mut launch_cleanup : u8 = 60; let mut launch_cleanup : u8 = 60;
let mut last_update:Instant = Instant::now(); let mut last_update:Instant = Instant::now();
let mut stat:Statistics = Statistics::new(); let mut stat:Statistics = Statistics::new();
let mut tick : u8 = 0;
if cli.trace { if cli.trace {
loginit(LevelFilter::Trace).unwrap(); loginit(LevelFilter::Trace).unwrap();
@ -748,6 +775,12 @@ fn main() -> anyhow::Result<()> {
info!("Started"); info!("Started");
loop { loop {
trace!("users: {}", users); trace!("users: {}", users);
if tick == 255 {
tick += 1;
} else {
tick = 1;
}
stat.inc_loop(); stat.inc_loop();
match host.service(1000).context("service failed")? { match host.service(1000).context("service failed")? {
Some(Event::Connect(_)) => { Some(Event::Connect(_)) => {
@ -902,7 +935,7 @@ fn main() -> anyhow::Result<()> {
let ry = rybytes.read_f64::<LittleEndian>().unwrap(); let ry = rybytes.read_f64::<LittleEndian>().unwrap();
let mut rzbytes: &[u8] = &packet.data()[40..48]; let mut rzbytes: &[u8] = &packet.data()[40..48];
let rz = rzbytes.read_f64::<LittleEndian>().unwrap(); let rz = rzbytes.read_f64::<LittleEndian>().unwrap();
users.update_pos_rot(sender.address(), x, y, z, rx, ry, rz); users.update_pos_rot(sender.address(), x, y, z, rx, ry, rz, tick);
} }
_ => { _ => {
send_message_connect_ko(sender.clone()).unwrap(); send_message_connect_ko(sender.clone()).unwrap();