From: Christian Heller Date: Sun, 21 Apr 2019 19:00:50 +0000 (+0200) Subject: Don't generate objects at illegal positions. Plus, refactor. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/todos?a=commitdiff_plain;h=0f7c3e5559d61d352ae529239e667e9da5e284fd;p=plomrogue2-experiments Don't generate objects at illegal positions. Plus, refactor. --- diff --git a/new/example_client.py b/new/example_client.py index 45f5be0..675d0a2 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -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 diff --git a/new/plomrogue/game.py b/new/plomrogue/game.py index 25cb3e7..da8fe1a 100755 --- a/new/plomrogue/game.py +++ b/new/plomrogue/game.py @@ -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 diff --git a/new/plomrogue/tasks.py b/new/plomrogue/tasks.py index c9ce45e..dfd22f7 100644 --- a/new/plomrogue/tasks.py +++ b/new/plomrogue/tasks.py @@ -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):