home · contact · privacy
Don't generate objects at illegal positions. Plus, refactor.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 21 Apr 2019 19:00:50 +0000 (21:00 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 21 Apr 2019 19:00:50 +0000 (21:00 +0200)
new/example_client.py
new/plomrogue/game.py
new/plomrogue/tasks.py

index 45f5be0861239a68d9287ff547a323af5d83a20a..675d0a20ed01f4e1eb0d1a3a6aa9c9259c3167b2 100755 (executable)
@@ -320,9 +320,8 @@ class DescriptorWidget(TextLinesWidget):
                 get_position_index(self.tui.examiner_position)
         terrain = self.tui.game.world.map_.terrain[pos_i]
         lines = [terrain]
-        for t in self.tui.game.world.things:
-            if t.position == self.tui.examiner_position:
-                lines += [t.type_]
+        for t in self.tui.game.world.things_at_pos(self.tui.examiner_position):
+            lines += [t.type_]
         return lines
 
 
index 25cb3e74fa72b5693c167af0fcee31a14eab2fcf..da8fe1a49962928d10fec5424baab8669db8a2ba 100755 (executable)
@@ -33,6 +33,13 @@ class WorldBase:
             return t
         return None
 
+    def things_at_pos(self, yx):
+        things = []
+        for t in self.things:
+            if t.position == yx:
+                things += [t]
+        return things
+
 
 
 class World(WorldBase):
@@ -84,8 +91,15 @@ class World(WorldBase):
 
         def add_thing(type_):
             t = self.game.thing_types[type_](self)
-            t.position = (random.randint(0, yx[0] -1),
-                          random.randint(0, yx[1] - 1))
+            while True:
+                new_pos = (random.randint(0, yx[0] -1),
+                           random.randint(0, yx[1] - 1))
+                if self.map_[new_pos] != '.':
+                    continue
+                if len(self.things_at_pos(new_pos)) > 0:
+                    continue
+                break
+            t.position = new_pos
             self.things += [t]
             return t
 
index c9ce45ed654f1aff64848c65f6b43167db3037c7..dfd22f7d50094d3732a327e370080e1085ef81ca 100644 (file)
@@ -41,8 +41,8 @@ class Task_MOVE(Task):
             raise GameError('would move outside map bounds')
         if self.thing.world.map_[test_pos] != '.':
             raise GameError('%s would move into illegal terrain' % self.thing.id_)
-        for t in self.thing.world.things:
-            if t.blocking and t.position == test_pos:
+        for t in self.thing.world.things_at_pos(test_pos):
+            if t.blocking:
                 raise GameError('%s would move into other thing' % self.thing.id_)
 
     def do(self):