Fix for invalid list assignment when popping the last cell from a level.
authorLukas Tönne <lukas.toenne@gmail.com>
Sat, 13 Dec 2014 11:41:57 +0000 (12:41 +0100)
committerLukas Tönne <lukas.toenne@gmail.com>
Sat, 13 Dec 2014 11:41:57 +0000 (12:41 +0100)
Also fixed a potential float rounding error giving invalid index.

object_physics_meadow/hierarchical_dart_throw.py

index f84fec9..a58ece2 100644 (file)
@@ -56,7 +56,10 @@ class GridLevel():
 
     def deactivate(self, index):
         c = self.cells[index]
-        self.cells[index] = self.cells.pop()
+        if index < len(self.cells)-1:
+            self.cells[index] = self.cells.pop()
+        else:
+            self.cells.pop()
         return c
 
     def cell_corners(self, cell):
@@ -93,7 +96,9 @@ def pop_cell(levels):
     for level in levels:
         level_totweight = len(level.cells) * level.weight
         if u < level_totweight:
-            cell_index = int(u / level.weight)
+            # Note: using int(u / level.weight) as cell index works in theory,
+            # but rounding errors can cause an invalid index >= len(level.cells)
+            cell_index = random.randrange(len(level.cells))
             cell = level.deactivate(cell_index)
             return level, cell
         else: