remove crash from collision
authoralfadur
Wed, 11 Nov 2020 22:40:33 +0300
changeset 15757 c7332c7f64cd
parent 15756 1d652bca512e
child 15758 6c939e6de981
remove crash from collision
rust/hwphysics/src/grid.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/hwphysics/src/grid.rs	Wed Nov 11 21:18:31 2020 +0300
+++ b/rust/hwphysics/src/grid.rs	Wed Nov 11 22:40:33 2020 +0300
@@ -55,6 +55,11 @@
         &mut self.bins[index.y as usize * self.bins_count.width + index.x as usize]
     }
 
+    fn try_get_bin(&mut self, index: Point) -> Option<&mut GridBin> {
+        self.bins
+            .get_mut(index.y as usize * self.bins_count.width + index.x as usize)
+    }
+
     fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
         self.get_bin(self.bin_index(position))
     }
@@ -82,35 +87,38 @@
         let old_bin_index = self.bin_index(old_position);
         let new_bin_index = self.bin_index(new_position);
 
-        let old_bin = self.lookup_bin(old_position);
-        if let Some(index) = old_bin.dynamic_refs.iter().position(|id| *id == gear_id) {
-            if old_bin_index == new_bin_index {
-                old_bin.dynamic_entries[index].center = *new_position
+        if let Some(old_bin) = self.try_get_bin(old_bin_index) {
+            let bounds = if let Some(index) =
+                old_bin.dynamic_refs.iter().position(|id| *id == gear_id)
+            {
+                if old_bin_index == new_bin_index {
+                    old_bin.dynamic_entries[index].center = *new_position;
+                    None
+                } else {
+                    Some(old_bin.dynamic_entries.swap_remove(index))
+                }
+            } else if let Some(index) = old_bin.static_refs.iter().position(|id| *id == gear_id) {
+                old_bin.static_refs.swap_remove(index);
+                Some(old_bin.static_entries.swap_remove(index))
             } else {
-                let bounds = old_bin.dynamic_entries.swap_remove(index);
-                let new_bin = self.get_bin(new_bin_index);
-
-                new_bin.dynamic_refs.push(gear_id);
-                new_bin.dynamic_entries.push(CircleBounds {
-                    center: *new_position,
-                    ..bounds
-                });
-            }
-        } else if let Some(index) = old_bin.static_refs.iter().position(|id| *id == gear_id) {
-            let bounds = old_bin.static_entries.swap_remove(index);
-            old_bin.static_refs.swap_remove(index);
-
-            let new_bin = if old_bin_index == new_bin_index {
-                old_bin
-            } else {
-                self.get_bin(new_bin_index)
+                None
             };
 
-            new_bin.dynamic_refs.push(gear_id);
-            new_bin.dynamic_entries.push(CircleBounds {
-                center: *new_position,
-                ..bounds
-            });
+            if let Some(bounds) = bounds {
+                let new_bin = if old_bin_index == new_bin_index {
+                    Some(old_bin)
+                } else {
+                    self.try_get_bin(new_bin_index)
+                };
+
+                if let Some(new_bin) = new_bin {
+                    new_bin.dynamic_refs.push(gear_id);
+                    new_bin.dynamic_entries.push(CircleBounds {
+                        center: *new_position,
+                        ..bounds
+                    });
+                }
+            }
         }
     }
 
--- a/rust/lib-hedgewars-engine/src/world.rs	Wed Nov 11 21:18:31 2020 +0300
+++ b/rust/lib-hedgewars-engine/src/world.rs	Wed Nov 11 22:40:33 2020 +0300
@@ -106,6 +106,14 @@
         let land = landgen.generate_land(&params, &mut self.random_numbers_gen);
 
         self.game_state = Some(GameState::new(land, physics));
+
+        if let Some(ref mut state) = self.game_state {
+            let position = Point::new(
+                (self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32,
+                0,
+            );
+            self.create_gear(position);
+        }
     }
 
     pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) {
@@ -148,13 +156,13 @@
     pub fn step(&mut self) {
         if let Some(ref mut state) = self.game_state {
             let next = self.random_numbers_gen.next().unwrap();
-            if next % 32 == 0 {
+            /*if next % 32 == 0 {
                 let position = Point::new(
                     (self.random_numbers_gen.next().unwrap() % state.land.width() as u32) as i32,
                     0,
                 );
                 self.create_gear(position);
-            }
+            }*/
         }
 
         if let Some(ref mut state) = self.game_state {