home · contact · privacy
Enforce sane create_unfound decisions.
[plomrogue2-experiments] / new / plomrogue / things.py
index db37a04bd11e09d14c912ae79e4aee0f5b05623e..8900d9a983d751490b7b0d84f0802d9b2efae1ec 100644 (file)
@@ -51,7 +51,7 @@ class Thing(ThingBase):
     def _position_set(self, pos):
         super()._position_set(pos)
         for t_id in self.inventory:
-            t = self.game.get_thing(t_id)
+            t = self.game.get_thing(t_id, create_unfound=False)
             t.position = self.position
         if not self.id_ == self.game.player_id:
             return
@@ -110,30 +110,51 @@ class ThingAnimate(Thing):
         self.set_task('WAIT')
         self._last_task_result = None
         self.unset_surroundings()
+        self.close_maps = ()
+
+    def _position_set(self, pos):
+        """For player we need to update .close_maps on every move via the
+           self.surroundings property method, to keep their reality
+           bubble in sync with their movement.
+
+        """
+        super()._position_set(pos)
+        if self.id_ == self.game.player_id:
+            if not hasattr(self, '_surroundings'):
+                self._surroundings = None
+            self.surroundings
 
     def move_on_dijkstra_map(self, own_pos, targets):
         visible_map = self.get_visible_map()
-        dijkstra_map = Map(visible_map.size)
+        dijkstra_map = Map(visible_map.size,
+                           start_indented=visible_map.start_indented)
         n_max = 256
         dijkstra_map.terrain = [n_max for i in range(dijkstra_map.size_i)]
         for target in targets:
             dijkstra_map[target] = 0
         shrunk = True
+        get_neighbors = self.game.map_geometry.get_neighbors
         while shrunk:
             shrunk = False
             for pos in dijkstra_map:
                 if visible_map[pos] != '.':
                     continue
-                neighbors = self.game.map_geometry.get_neighbors((YX(0,0), pos),
-                                                                 dijkstra_map.size)
+                neighbors = get_neighbors((YX(0,0), pos), dijkstra_map.size,
+                                          dijkstra_map.start_indented)
                 for direction in neighbors:
                     big_yx, small_yx = neighbors[direction]
                     if big_yx == YX(0,0) and \
                        dijkstra_map[small_yx] < dijkstra_map[pos] - 1:
                         dijkstra_map[pos] = dijkstra_map[small_yx] + 1
                         shrunk = True
-        neighbors = self.game.map_geometry.get_neighbors((YX(0,0), own_pos),
-                                                         dijkstra_map.size)
+        #print('DEBUG DIJKSTRA ---------------------', self.id_, self.position)
+        #for y, line in dijkstra_map.lines():
+        #    line_to_print = []
+        #    for x in line:
+        #        line_to_print += ['%3s' % x]
+        #    print(' '.join(line_to_print))
+        neighbors = get_neighbors((YX(0,0), own_pos), dijkstra_map.size,
+                                  dijkstra_map.start_indented)
         n = n_max
         target_direction = None
         for direction in sorted(neighbors.keys()):
@@ -145,33 +166,33 @@ class ThingAnimate(Thing):
                     target_direction = direction
         return target_direction
 
-    def hunt_player(self):
-        visible_things = self.get_visible_things()
-        target = None
-        for t in visible_things:
-            if t.type_ == 'human':
-                target = t.position[1] - self.view_offset
-                break
-        if target is not None:
-            try:
-                offset_self_pos = self.position[1] - self.view_offset
-                target_dir = self.move_on_dijkstra_map(offset_self_pos,
-                                                       [target])
-                if target_dir is not None:
-                    self.set_task('MOVE', (target_dir,))
-                    return True
-            except GameError:
-                pass
-        return False
+    #def hunt_player(self):
+    #    visible_things = self.get_visible_things()
+    #    target = None
+    #    for t in visible_things:
+    #        if t.type_ == 'human':
+    #            target = t.position[1] - self.view_offset
+    #            break
+    #    if target is not None:
+    #        try:
+    #            offset_self_pos = self.position[1] - self.view_offset
+    #            target_dir = self.move_on_dijkstra_map(offset_self_pos,
+    #                                                   [target])
+    #            if target_dir is not None:
+    #                self.set_task('MOVE', (target_dir,))
+    #                return True
+    #        except GameError:
+    #            pass
+    #    return False
 
     def hunt_food_satisfaction(self):
         for id_ in self.inventory:
-            t = self.game.get_thing(id_)
+            t = self.game.get_thing(id_, create_unfound=False)
             if t.type_ == 'food':
                 self.set_task('EAT', (id_,))
                 return True
         for id_ in self.get_pickable_items():
-            t = self.game.get_thing(id_)
+            t = self.game.get_thing(id_, create_unfound=False)
             if t.type_ == 'food':
                 self.set_task('PICKUP', (id_,))
                 return True
@@ -179,8 +200,12 @@ class ThingAnimate(Thing):
         food_targets = []
         for t in visible_things:
             if t.type_ == 'food':
-                food_targets += [t.position[1] - self.view_offset]
-        offset_self_pos = self.position[1] - self.view_offset
+                food_targets += [self.game.map_geometry.pos_in_view(t.position,
+                                                                    self.view_offset,
+                                                                    self.game.map_size)]
+        offset_self_pos = self.game.map_geometry.pos_in_view(self.position,
+                                                             self.view_offset,
+                                                             self.game.map_size)
         target_dir = self.move_on_dijkstra_map(offset_self_pos,
                                                food_targets)
         if target_dir:
@@ -221,6 +246,7 @@ class ThingAnimate(Thing):
             if self is self.game.player:
                 self.game.player_is_alive = False
             else:
+                # TODO: Handle inventory.
                 del self.game.things[self.game.things.index(self)]
             return
         try:
@@ -246,7 +272,7 @@ class ThingAnimate(Thing):
 
     def unset_surroundings(self):
         self._stencil = None
-        self._surrounding_map = None
+        self._surroundings = None
 
     @property
     def view_offset(self):
@@ -254,27 +280,25 @@ class ThingAnimate(Thing):
                                                       self.position,
                                                       self._radius)
 
-    def get_surrounding_map(self):
-        if self._surrounding_map is not None:
-            return self._surrounding_map
-        self._surrounding_map = Map(size=YX(self._radius*2+1, self._radius*2+1))
-        for pos in self._surrounding_map:
-            correct = self.game.map_geometry.correct_double_coordinate
-            big_yx, small_yx = correct(self.game.map_size, (0,0),
-                                       pos + self.view_offset)
-            map_ = self.game.get_map(big_yx, False)
-            if map_ is None:
-                map_ = Map(size=self.game.map_size)
-            self._surrounding_map[pos] = map_[small_yx]
-        return self._surrounding_map
+    @property
+    def surroundings(self):
+        if self._surroundings is not None:
+            return self._surroundings
+        s, close_maps = self.\
+            game.map_geometry.get_view_and_seen_maps(self.game.map_size,
+                                                     self.game.get_map,
+                                                     self._radius,
+                                                     self.view_offset)
+        self.close_maps = close_maps
+        self._surroundings = s
+        return self._surroundings
 
     def get_stencil(self):
         if self._stencil is not None:
             return self._stencil
-        surrounding_map = self.get_surrounding_map()
-        m = Map(surrounding_map.size, ' ')
-        for pos in surrounding_map:
-            if surrounding_map[pos] in {'.', '~'}:
+        m = Map(self.surroundings.size, ' ', self.surroundings.start_indented)
+        for pos in self.surroundings:
+            if self.surroundings[pos] in {'.', '~'}:
                 m[pos] = '.'
         fov_center = YX((m.size.y) // 2, m.size.x // 2)
         self._stencil = FovMapHex(m, fov_center)
@@ -282,10 +306,10 @@ class ThingAnimate(Thing):
 
     def get_visible_map(self):
         stencil = self.get_stencil()
-        m = Map(self.get_surrounding_map().size, ' ')
+        m = Map(self.surroundings.size, ' ', self.surroundings.start_indented)
         for pos in m:
             if stencil[pos] == '.':
-                m[pos] = self._surrounding_map[pos]
+                m[pos] = self.surroundings[pos]
         return m
 
     def get_visible_things(self):