Skip to content

Commit

Permalink
Fix temporary packets being send as permanent and vice versa
Browse files Browse the repository at this point in the history
A bunch of things got changed in a desparate attempt to fix my shit but
the crux of the issue was this:

	rpc_unreliable("sync_permanent_vehicle_data", i, pt[0]);
	rpc("sync_temporary_vehicle_data", i, pt[1]);

Needless to say, I'm fucking pissed now.
  • Loading branch information
Demindiro committed Jul 12, 2021
1 parent 80f66b4 commit 470cddd
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 73 deletions.
33 changes: 17 additions & 16 deletions gd/blocks/turrets/turret.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func _ready_deferred():
for body in anchor_mounts_bodies:
_create_joint(get_parent(), body)

if _body_a != null:
if _body_a != null && _body_b != null:
_body_b_mount = Spatial.new()
_body_b.add_child(_body_b_mount)
_body_b_mount.transform.basis = transform.basis
Expand Down Expand Up @@ -57,21 +57,22 @@ func aim_at(position: Vector3):
func _create_joint(body_a: PhysicsBody, body_b: PhysicsBody) -> void:
_body_a = body_a
_body_b = body_b
var vh = body_a.get_meta("ownwar_vehicle_list")[body_a.get_meta("ownwar_vehicle_index")]
var bd_a = body_a.get_meta("ownwar_body_index")
var bd_b = body_b.get_meta("ownwar_body_index")
var z_up = transform.basis * Basis(Vector3(0, 0, 1), Vector3(1, 0, 0), Vector3(0, 1, 0))
_joint_rid = PhysicsServer.joint_create_hinge(
body_a.get_rid(),
Transform(z_up, vh.voxel_to_translation(bd_a, base_position)),
body_b.get_rid(),
Transform(z_up, vh.voxel_to_translation(bd_b, base_position))
)
PhysicsServer.hinge_joint_set_param(
_joint_rid,
PhysicsServer.HINGE_JOINT_MOTOR_MAX_IMPULSE,
max_impulse
)
if body_b != null && body_a != null:
var vh = body_a.get_meta("ownwar_vehicle_list")[body_a.get_meta("ownwar_vehicle_index")]
var bd_a = body_a.get_meta("ownwar_body_index")
var bd_b = body_b.get_meta("ownwar_body_index")
var z_up = transform.basis * Basis(Vector3(0, 0, 1), Vector3(1, 0, 0), Vector3(0, 1, 0))
_joint_rid = PhysicsServer.joint_create_hinge(
body_a.get_rid(),
Transform(z_up, vh.voxel_to_translation(bd_a, base_position)),
body_b.get_rid(),
Transform(z_up, vh.voxel_to_translation(bd_b, base_position))
)
PhysicsServer.hinge_joint_set_param(
_joint_rid,
PhysicsServer.HINGE_JOINT_MOTOR_MAX_IMPULSE,
max_impulse
)


func _remove_joint() -> void:
Expand Down
4 changes: 2 additions & 2 deletions gd/blocks/weapons/plasma/cannon.tres
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ surfaces/1 = {

[resource]
script = ExtResource( 1 )
human_name = "Plasma Cannon"
revision = 1
human_category = "Weapons"
mass = 1.0
revision = 1
human_name = "Plasma Cannon"
aabb = AABB( 0, 0, 0, 1, 1, 3 )
id = 90
health = 100
Expand Down
7 changes: 5 additions & 2 deletions gd/maps/the_pit/the_pit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var pending_temporary_data := []
# Permanent vehicle data to be applied
var pending_permanent_data := []

# Counter to reduce the amount of temporary packets send
var temp_packet_counter = 0


func _ready() -> void:
get_tree().multiplayer_poll = false
Expand Down Expand Up @@ -129,8 +132,8 @@ func _physics_process(delta: float) -> void:
var v = vehicles[i]
if v != null:
var pt = v.create_packet()
rpc_unreliable("sync_permanent_vehicle_data", i, pt[0]);
rpc("sync_temporary_vehicle_data", i, pt[1]);
rpc("sync_permanent_vehicle_data", i, pt[0]);
rpc_unreliable("sync_temporary_vehicle_data", i, pt[1]);

# Process damage events
if !server_mode:
Expand Down
2 changes: 1 addition & 1 deletion gd/start_menu/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ bus = "Music"
[connection signal="vehicle_renamed" from="." to="Vehicle info" method="on_vehicle_renamed"]
[connection signal="loaded_vehicle" from="Vehicle preview" to="Vehicle info" method="set_vehicle"]
[connection signal="timeout" from="Vehicle preview/InputTimer" to="Vehicle preview" method="input_timeout"]
[connection signal="select_vehicle" from="Vehicle list" to="Vehicle preview" method="set_preview"]
[connection signal="select_vehicle" from="Vehicle list" to="." method="select_vehicle"]
[connection signal="select_vehicle" from="Vehicle list" to="Vehicle preview" method="set_preview"]
[connection signal="create_vehicle" from="New vehicle" to="." method="goto_editor"]
[connection signal="focus_exited" from="New vehicle/Box/Name" to="New vehicle" method="focus_lost"]
[connection signal="gui_input" from="New vehicle/Box/Name" to="New vehicle" method="name_gui_input"]
Expand Down
13 changes: 6 additions & 7 deletions gdn/ownwar/src/vehicle/body/damage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,11 @@ impl super::Body {
/// Returns `true` if the body is destroyed.
#[must_use]
pub(super) fn apply_damage_events(&mut self, shared: &mut Shared) -> bool {
if self.is_destroyed() {
return true;
}

let mut destroyed = Vec::new();
let mut destroy_disconnected = false;
let mut evts = mem::take(&mut self.damage_events);
let mut body_destroyed = false;
let mut body_destroyed = self.is_destroyed();

let old_mass = self.mass;

Expand Down Expand Up @@ -332,7 +329,7 @@ impl super::Body {

pub(super) fn correct_mass(&mut self) {
self.calculate_mass();
assert_ne!(self.mass, 0.0, "Mass is zero!");
//assert_ne!(self.mass, 0.0, "Mass is zero!");

let center = (self.center_of_mass() + Vector3::new(0.5, 0.5, 0.5)) * block::SCALE;

Expand All @@ -352,7 +349,9 @@ impl super::Body {
}
}

self.update_node_mass()
if !self.is_destroyed() {// TODO
self.update_node_mass()
}
}

/// Attempt to destroy a block.
Expand All @@ -368,7 +367,7 @@ impl super::Body {
destroyed_blocks: &mut Vec<Voxel>,
) -> Result<bool, ()> {
if let Ok(result) = self.try_damage_block(voxel, *damage) {
let node = unsafe { self.node().assume_safe() };
let node = unsafe { self.node().unwrap().assume_safe() };
let center_of_mass = self.center_of_mass();
*damage = result.damage();
match result {
Expand Down
20 changes: 12 additions & 8 deletions gdn/ownwar/src/vehicle/body/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,14 @@ impl super::Body {
}

// Setup node tree
for body in self.children.iter_mut() {
for body in self.children.iter() {
unsafe {
self.node
.unwrap()
.assume_safe()
.add_child(body.node.unwrap(), false);
if let Some(bn) = body.node.as_ref() {
self.node
.unwrap()
.assume_safe()
.add_child(bn, false);
}
}
}

Expand All @@ -329,11 +331,13 @@ impl super::Body {
pub(in super::super) fn create_collision_exceptions(&mut self) {
// TODO find a way to avoid a temporary buffer
let mut nodes = Vec::new();
self.iter_all_bodies(&mut |b| nodes.push(b.node().clone()));
self.iter_all_bodies(&mut |b| { b.node.clone().map(|bn| nodes.push(bn)); });
self.iter_all_bodies(&mut |b| {
nodes.iter().for_each(|a| unsafe {
if a != b.node() {
b.node().assume_safe().add_collision_exception_with(a);
if let Some(b) = b.node.as_ref() {
if a != b {
b.assume_safe().add_collision_exception_with(a);
}
}
})
});
Expand Down
27 changes: 14 additions & 13 deletions gdn/ownwar/src/vehicle/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const COLLISION_LAYER: u32 = 2;
const COLLISION_MASK: u32 = 1 | 2 | (1 << 7);

pub(super) struct Body {
// TODO don't make this public
node: Option<Ref<VehicleBody>>,
#[cfg(not(feature = "server"))]
voxel_mesh: Option<Instance<VoxelMesh, Shared>>,
Expand Down Expand Up @@ -373,9 +374,8 @@ impl Body {
self.children.iter_mut().for_each(Self::step);
}

#[track_caller]
pub fn node(&self) -> &Ref<VehicleBody> {
self.node.as_ref().expect("Body is already destroyed")
pub fn node(&self) -> Option<&Ref<VehicleBody>> {
self.node.as_ref()
}

pub fn add_block(
Expand Down Expand Up @@ -550,8 +550,10 @@ impl Body {

#[track_caller]
pub fn position(&self) -> (Vector3, Quat) {
let trf = unsafe { self.node().assume_safe().transform() };
(trf.origin, trf.basis.to_quat())
self.node().map(|n| {
let trf = unsafe { n.assume_safe().transform() };
(trf.origin, trf.basis.to_quat())
}).unwrap_or((Vector3::zero(), Quat::identity()))
}

pub fn set_position(&self, translation: Vector3, rotation: Quat) {
Expand All @@ -560,25 +562,24 @@ impl Body {
let trf = rotation.to_transform().then_translate(translation);
unsafe {
self.node()
.assume_safe()
.set_transform(Transform::from_transform(&trf))
};
.map(|b| b.assume_safe().set_transform(Transform::from_transform(&trf)));
}
}

pub fn linear_velocity(&self) -> Vector3 {
unsafe { self.node().assume_safe().linear_velocity() }
unsafe { self.node().map(|b| b.assume_safe().linear_velocity()).unwrap_or(Vector3::zero()) }
}

pub fn set_linear_velocity(&self, velocity: Vector3) {
unsafe { self.node().assume_safe().set_linear_velocity(velocity) };
unsafe { self.node().map(|b| b.assume_safe().set_linear_velocity(velocity)); }
}

pub fn angular_velocity(&self) -> Vector3 {
unsafe { self.node().assume_safe().angular_velocity() }
unsafe { self.node().map(|b| b.assume_safe().angular_velocity()).unwrap_or(Vector3::zero()) }
}

pub fn set_angular_velocity(&self, velocity: Vector3) {
unsafe { self.node().assume_safe().set_angular_velocity(velocity) };
unsafe { self.node().map(|b| b.assume_safe().set_angular_velocity(velocity)); }
}

pub fn aabb(&self) -> AABB<u8> {
Expand Down Expand Up @@ -616,7 +617,7 @@ impl Body {

/// Update the mass of the rigidbody with the current `mass`.
fn update_node_mass(&mut self) {
unsafe { self.node().assume_safe().set_mass(self.mass.into()) }
unsafe { self.node().map(|b| b.assume_safe().set_mass(self.mass.into())); }
}
}

Expand Down
8 changes: 6 additions & 2 deletions gdn/ownwar/src/vehicle/body/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,8 @@ impl super::Body {
parent_anchors: Vec::new(),
};

// FIXME this leaks memory if the body is dead.
slf.create_godot_nodes();
slf.correct_mass();
slf.correct_mass(); // TODO should be done afterwards

for z in 0..=size.z {
for y in 0..=size.y {
Expand All @@ -223,6 +222,9 @@ impl super::Body {
}

if !slf.is_destroyed() {
//slf.correct_mass();
slf.update_node_mass();

// If alive, get & apply position & velocity
let tr = Self::deserialize_vector3(in_)?;
let rot = Self::deserialize_vector3(in_)?;
Expand All @@ -234,6 +236,8 @@ impl super::Body {
slf.set_position(tr, rot);
slf.set_linear_velocity(lv);
slf.set_angular_velocity(av);
} else {
unsafe { slf.node.take().map(|n| n.assume_unique().queue_free()) };
}

Ok(slf)
Expand Down
4 changes: 2 additions & 2 deletions gdn/ownwar/src/vehicle/body/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl super::Body {
origin: Vector3,
direction: Vector3,
) -> (Vector3, Vector3) {
let node = unsafe { self.node().assume_safe() };
let node = unsafe { self.node().unwrap().assume_safe() };
let local_unscaled_origin = node.to_local(origin);
let local_origin = local_unscaled_origin / block::SCALE + self.center_of_mass();
let local_direction = node.to_local(origin + direction) - local_unscaled_origin;
Expand All @@ -24,7 +24,7 @@ impl super::Body {
origin: Vector3,
direction: Vector3,
) -> (Vector3, Vector3) {
let node = unsafe { self.node().assume_safe() };
let node = unsafe { self.node().unwrap().assume_safe() };
let local_unscaled_origin = (origin - self.center_of_mass()) * block::SCALE;
let global_origin = node.to_global(local_unscaled_origin);
let global_direction = node.to_global(origin + direction) - global_origin;
Expand Down
44 changes: 24 additions & 20 deletions gdn/ownwar/src/vehicle/vehicle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ pub mod gd {
let body = self.vehicle.body(&[]).unwrap();
let (tr, pos) = body.position();
body.iter_all_bodies(&mut |b| {
b.node()
.assume_safe()
.set_meta("ownwar_vehicle_list", vehicles.clone());
if let Some(b) = b.node() {
b.assume_safe().set_meta("ownwar_vehicle_list", vehicles.clone());
}
});
scene
.assume_safe()
.add_child(self.vehicle.body(&[]).unwrap().node(), true);
.add_child(self.vehicle.body(&[]).unwrap().node().unwrap(), true);
if reset_position {
body.iter_all_bodies(&mut |b| {
// TODO use proper offsets so that bodies don't "fly" into position.
Expand Down Expand Up @@ -337,7 +337,7 @@ pub mod gd {

#[export]
fn get_node(&self, _: TRef<Reference>) -> Ref<VehicleBody> {
self.vehicle.body(&[]).unwrap().node().clone()
self.vehicle.body(&[]).unwrap().node().unwrap().clone()
}

/// Map a global voxel coordinate to a local translation, accounting for center of mass &
Expand Down Expand Up @@ -487,19 +487,21 @@ pub mod gd {
vehicle_id: u16,
team: super::Team,
) {
let node = unsafe { body.node().assume_safe() };
node.set_meta("ownwar_body_index", Variant::from_byte_array(&index_stack));
node.set_meta("ownwar_vehicle_index", Variant::from_u64(vehicle_id.into()));
node.set_meta("ownwar_vehicle_team", Variant::from_u64(team.into()));
node.set_name(format!(
"OwnWar VehicleBody {}.{:?}",
vehicle_id,
&index_stack.read()[..]
));
for (i, body) in body.children_mut().enumerate() {
let mut index_stack = index_stack.clone();
index_stack.push(i.try_into().unwrap());
Self::set_meta(body, index_stack, vehicle_id, team);
if let Some(node) = body.node() {
let node = unsafe { node.assume_safe() };
node.set_meta("ownwar_body_index", Variant::from_byte_array(&index_stack));
node.set_meta("ownwar_vehicle_index", Variant::from_u64(vehicle_id.into()));
node.set_meta("ownwar_vehicle_team", Variant::from_u64(team.into()));
node.set_name(format!(
"OwnWar VehicleBody {}.{:?}",
vehicle_id,
&index_stack.read()[..]
));
for (i, body) in body.children_mut().enumerate() {
let mut index_stack = index_stack.clone();
index_stack.push(i.try_into().unwrap());
Self::set_meta(body, index_stack, vehicle_id, team);
}
}
}
}
Expand Down Expand Up @@ -761,6 +763,7 @@ impl Vehicle {
let mb = self.main_body.as_ref().unwrap();
let space = unsafe {
mb.node()
.unwrap()
.assume_safe()
.get_world()
.expect("No world")
Expand Down Expand Up @@ -799,6 +802,7 @@ impl Vehicle {
let mb = self.main_body.as_ref().unwrap();
let space = unsafe {
mb.node()
.unwrap()
.assume_safe()
.get_world()
.expect("No world")
Expand Down Expand Up @@ -832,9 +836,9 @@ impl Vehicle {

// Move the bodies
mb.iter_all_bodies(&mut |b| {
if !b.is_destroyed() {
if let Some(node) = b.node() {
//let (tr, rot) = b.position();
let trf = unsafe { b.node().assume_safe().global_transform() };
let trf = unsafe { node.assume_safe().global_transform() };

let xz = trf.basis.z();
let xz = Vector3::new(xz.x, 0.0, xz.z).normalize() * 2.0;
Expand Down
8 changes: 8 additions & 0 deletions install-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ case $1 in
done
done
;;
lib-only)
for SERVER in lu0 lv0; do
echo Installing PCK \& libraries on $SERVER
for file in *.so *.pck; do
scp "$file" "ownwar@$SERVER:$file"
done
done
;;
*)
for SERVER in lu0 lv0; do
echo Installing on $SERVER
Expand Down

0 comments on commit 470cddd

Please sign in to comment.