home · contact · privacy
Flatten game->world hierarchy.
[plomrogue2-experiments] / new / plomrogue / things.py
index ed3b5474d6a64bf978c86ef8b41436043171565b..2e97541a8cb2416eaa3ead7bad023c4b9d75ce3d 100644 (file)
@@ -1,15 +1,15 @@
 from plomrogue.errors import GameError
 from plomrogue.errors import GameError
-from plomrogue.mapping import YX
+from plomrogue.mapping import YX, Map, FovMapHex
 
 
 
 class ThingBase:
     type_ = '?'
 
 
 
 
 class ThingBase:
     type_ = '?'
 
-    def __init__(self, world, id_=None, position=(YX(0,0), YX(0,0))):
-        self.world = world
+    def __init__(self, game, id_=None, position=(YX(0,0), YX(0,0))):
+        self.game = game
         if id_ is None:
         if id_ is None:
-            self.id_ = self.world.new_thing_id()
+            self.id_ = self.game.new_thing_id()
         else:
             self.id_ = id_
         self.position = position
         else:
             self.id_ = id_
         self.position = position
@@ -51,30 +51,44 @@ class Thing(ThingBase):
     def _position_set(self, pos):
         super()._position_set(pos)
         for t_id in self.inventory:
     def _position_set(self, pos):
         super()._position_set(pos)
         for t_id in self.inventory:
-            t = self.world.get_thing(t_id)
+            t = self.game.get_thing(t_id)
             t.position = self.position
             t.position = self.position
-        if not self.id_ == self.world.player_id:
+        if not self.id_ == self.game.player_id:
             return
         edge_left = self.position[1].x - self._radius
         edge_right = self.position[1].x + self._radius
         edge_up = self.position[1].y - self._radius
         edge_down = self.position[1].y + self._radius
         if edge_left < 0:
             return
         edge_left = self.position[1].x - self._radius
         edge_right = self.position[1].x + self._radius
         edge_up = self.position[1].y - self._radius
         edge_down = self.position[1].y + self._radius
         if edge_left < 0:
-            self.world.get_map(self.position[0] - YX(1,-1))
-            self.world.get_map(self.position[0] - YX(0,-1))
-            self.world.get_map(self.position[0] - YX(-1,-1))
-        if edge_right >= self.world.map_size.x:
-            self.world.get_map(self.position[0] + YX(1,1))
-            self.world.get_map(self.position[0] + YX(0,1))
-            self.world.get_map(self.position[0] + YX(-1,1))
+            self.game.get_map(self.position[0] + YX(1,-1))
+            self.game.get_map(self.position[0] + YX(0,-1))
+            self.game.get_map(self.position[0] + YX(-1,-1))
+        if edge_right >= self.game.map_size.x:
+            self.game.get_map(self.position[0] + YX(1,1))
+            self.game.get_map(self.position[0] + YX(0,1))
+            self.game.get_map(self.position[0] + YX(-1,1))
         if edge_up < 0:
         if edge_up < 0:
-            self.world.get_map(self.position[0] - YX(-1,1))
-            self.world.get_map(self.position[0] - YX(-1,0))
-            self.world.get_map(self.position[0] - YX(-1,-1))
-        if edge_down >= self.world.map_size.y:
-            self.world.get_map(self.position[0] + YX(1,1))
-            self.world.get_map(self.position[0] + YX(1,0))
-            self.world.get_map(self.position[0] + YX(1,-1))
+            self.game.get_map(self.position[0] + YX(-1,1))
+            self.game.get_map(self.position[0] + YX(-1,0))
+            self.game.get_map(self.position[0] + YX(-1,-1))
+        if edge_down >= self.game.map_size.y:
+            self.game.get_map(self.position[0] + YX(1,1))
+            self.game.get_map(self.position[0] + YX(1,0))
+            self.game.get_map(self.position[0] + YX(1,-1))
+        #alternative
+        #if self.position[1].x < self._radius:
+        #    self.game.get_map(self.position[0] - YX(0,1))
+        #if self.position[1].y < self._radius:
+        #    self.game.get_map(self.position[0] - YX(1,0))
+        #if self.position[1].x > self.game.map_size.x - self._radius:
+        #    self.game.get_map(self.position[0] + YX(0,1))
+        #if self.position[1].y > self.game.map_size.y - self._radius:
+        #    self.game.get_map(self.position[0] + YX(1,0))
+        #if self.position[1].y < self._radius and \
+        #   self.position[1].x <= [pos for pos in
+        #                          diagonal_distance_edge
+        #                          if pos.y == self.position[1].y][0].x:
+        #    self.game.get_map(self.position[0] - YX(1,1))
 
 
 
 
 
 
@@ -99,7 +113,7 @@ class ThingAnimate(Thing):
 
     def move_on_dijkstra_map(self, own_pos, targets):
         visible_map = self.get_visible_map()
 
     def move_on_dijkstra_map(self, own_pos, targets):
         visible_map = self.get_visible_map()
-        dijkstra_map = self.world.game.map_type(visible_map.size)
+        dijkstra_map = Map(visible_map.size)
         n_max = 256
         dijkstra_map.terrain = [n_max for i in range(dijkstra_map.size_i)]
         for target in targets:
         n_max = 256
         dijkstra_map.terrain = [n_max for i in range(dijkstra_map.size_i)]
         for target in targets:
@@ -110,19 +124,22 @@ class ThingAnimate(Thing):
             for pos in dijkstra_map:
                 if visible_map[pos] != '.':
                     continue
             for pos in dijkstra_map:
                 if visible_map[pos] != '.':
                     continue
-                neighbors = dijkstra_map.get_neighbors(pos)
+                neighbors = self.game.map_geometry.get_neighbors((YX(0,0), pos),
+                                                                 dijkstra_map.size)
                 for direction in neighbors:
                 for direction in neighbors:
-                    yx = neighbors[direction]
-                    if yx is not None and dijkstra_map[yx] < dijkstra_map[pos] - 1:
-                        dijkstra_map[pos] = dijkstra_map[yx] + 1
+                    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
                         shrunk = True
-        neighbors = dijkstra_map.get_neighbors(own_pos)
+        neighbors = self.game.map_geometry.get_neighbors((YX(0,0), own_pos),
+                                                         dijkstra_map.size)
         n = n_max
         target_direction = None
         for direction in sorted(neighbors.keys()):
         n = n_max
         target_direction = None
         for direction in sorted(neighbors.keys()):
-            yx = neighbors[direction]
-            if yx is not None:
-                n_new = dijkstra_map[yx]
+            big_yx, small_yx = neighbors[direction]
+            if big_yx == (0,0):
+                n_new = dijkstra_map[small_yx]
                 if n_new < n:
                     n = n_new
                     target_direction = direction
                 if n_new < n:
                     n = n_new
                     target_direction = direction
@@ -150,12 +167,12 @@ class ThingAnimate(Thing):
 
     def hunt_food_satisfaction(self):
         for id_ in self.inventory:
 
     def hunt_food_satisfaction(self):
         for id_ in self.inventory:
-            t = self.world.get_thing(id_)
+            t = self.game.get_thing(id_)
             if t.type_ == 'food':
                 self.set_task('EAT', (id_,))
                 return True
         for id_ in self.get_pickable_items():
             if t.type_ == 'food':
                 self.set_task('EAT', (id_,))
                 return True
         for id_ in self.get_pickable_items():
-            t = self.world.get_thing(id_)
+            t = self.game.get_thing(id_)
             if t.type_ == 'food':
                 self.set_task('PICKUP', (id_,))
                 return True
             if t.type_ == 'food':
                 self.set_task('PICKUP', (id_,))
                 return True
@@ -182,7 +199,7 @@ class ThingAnimate(Thing):
             self.set_task('WAIT')
 
     def set_task(self, task_name, args=()):
             self.set_task('WAIT')
 
     def set_task(self, task_name, args=()):
-        task_class = self.world.game.tasks[task_name]
+        task_class = self.game.tasks[task_name]
         self.task = task_class(self, args)
         self.task.check()  # will throw GameError if necessary
 
         self.task = task_class(self, args)
         self.task.check()  # will throw GameError if necessary
 
@@ -190,8 +207,8 @@ class ThingAnimate(Thing):
         """Further the thing in its tasks, decrease its health.
 
         First, ensures an empty map, decrements .health and kills
         """Further the thing in its tasks, decrease its health.
 
         First, ensures an empty map, decrements .health and kills
-        thing if crossing zero (removes from self.world.things for AI
-        thing, or unsets self.world.player_is_alive for player thing);
+        thing if crossing zero (removes from self.game.things for AI
+        thing, or unsets self.game.player_is_alive for player thing);
         then checks that self.task is still possible and aborts if
         otherwise (for AI things, decides a new task).
 
         then checks that self.task is still possible and aborts if
         otherwise (for AI things, decides a new task).
 
@@ -203,10 +220,10 @@ class ThingAnimate(Thing):
         self.unset_surroundings()
         self.health -= 1
         if self.health <= 0:
         self.unset_surroundings()
         self.health -= 1
         if self.health <= 0:
-            if self is self.world.player:
-                self.world.player_is_alive = False
+            if self is self.game.player:
+                self.game.player_is_alive = False
             else:
             else:
-                del self.world.things[self.world.things.index(self)]
+                del self.game.things[self.game.things.index(self)]
             return
         try:
             self.task.check()
             return
         try:
             self.task.check()
@@ -234,14 +251,12 @@ class ThingAnimate(Thing):
         self._surrounding_map = None
         self._surroundings_offset = None
 
         self._surrounding_map = None
         self._surroundings_offset = None
 
-    def must_fix_indentation(self):
-        return self._radius % 2 != self.position[1].y % 2
-
     def get_surroundings_offset(self):
         if self._surroundings_offset is not None:
             return self._surroundings_offset
     def get_surroundings_offset(self):
         if self._surroundings_offset is not None:
             return self._surroundings_offset
-        add_line = self.must_fix_indentation()
-        offset = YX(self.position[1].y - self._radius - int(add_line),
+        offset = YX(self.position[0].y * self.game.map_size.y +
+                    self.position[1].y - self._radius,
+                    self.position[0].x * self.game.map_size.x +
                     self.position[1].x - self._radius)
         self._surroundings_offset = offset
         return self._surroundings_offset
                     self.position[1].x - self._radius)
         self._surroundings_offset = offset
         return self._surroundings_offset
@@ -249,32 +264,15 @@ class ThingAnimate(Thing):
     def get_surrounding_map(self):
         if self._surrounding_map is not None:
             return self._surrounding_map
     def get_surrounding_map(self):
         if self._surrounding_map is not None:
             return self._surrounding_map
-
-        def pan_and_scan(size_of_axis, pos, offset):
-            big_pos = 0
-            small_pos = pos + offset
-            if small_pos < 0:
-                big_pos = -1
-                small_pos = size_of_axis + small_pos
-            elif small_pos >= size_of_axis:
-                big_pos = 1
-                small_pos = small_pos - size_of_axis
-            return big_pos, small_pos
-
-        add_line = self.must_fix_indentation()
-        self._surrounding_map = self.world.game.\
-                                map_type(size=YX(self._radius*2+1+int(add_line),
-                                                 self._radius*2+1))
-        size = self.world.map_size
+        self._surrounding_map = Map(size=YX(self._radius*2+1, self._radius*2+1))
         offset = self.get_surroundings_offset()
         for pos in self._surrounding_map:
         offset = self.get_surroundings_offset()
         for pos in self._surrounding_map:
-            big_y, small_y = pan_and_scan(size.y, pos.y, offset.y)
-            big_x, small_x = pan_and_scan(size.x, pos.x, offset.x)
-            big_yx = YX(big_y, big_x)
-            small_yx = YX(small_y, small_x)
-            map_ = self.world.get_map(big_yx, False)
+            offset_pos = pos + offset
+            absolutize = self.game.map_geometry.absolutize_coordinate
+            big_yx, small_yx = absolutize(self.game.map_size, (0,0), offset_pos)
+            map_ = self.game.get_map(big_yx, False)
             if map_ is None:
             if map_ is None:
-                map_ = self.world.game.map_type(size=self.world.map_size)
+                map_ = Map(size=self.game.map_size)
             self._surrounding_map[pos] = map_[small_yx]
         return self._surrounding_map
 
             self._surrounding_map[pos] = map_[small_yx]
         return self._surrounding_map
 
@@ -282,58 +280,46 @@ class ThingAnimate(Thing):
         if self._stencil is not None:
             return self._stencil
         surrounding_map = self.get_surrounding_map()
         if self._stencil is not None:
             return self._stencil
         surrounding_map = self.get_surrounding_map()
-        m = surrounding_map.new_from_shape(' ')
+        m = Map(surrounding_map.size, ' ')
         for pos in surrounding_map:
             if surrounding_map[pos] in {'.', '~'}:
                 m[pos] = '.'
         for pos in surrounding_map:
             if surrounding_map[pos] in {'.', '~'}:
                 m[pos] = '.'
-        offset = self.get_surroundings_offset()
-        fov_center = self.position[1] - offset
-        self._stencil = m.get_fov_map(fov_center)
+        fov_center = YX((m.size.y) // 2, m.size.x // 2)
+        self._stencil = FovMapHex(m, fov_center)
         return self._stencil
 
     def get_visible_map(self):
         stencil = self.get_stencil()
         return self._stencil
 
     def get_visible_map(self):
         stencil = self.get_stencil()
-        m = self.get_surrounding_map().new_from_shape(' ')
+        m = Map(self.get_surrounding_map().size, ' ')
         for pos in m:
             if stencil[pos] == '.':
                 m[pos] = self._surrounding_map[pos]
         return m
 
     def get_visible_things(self):
         for pos in m:
             if stencil[pos] == '.':
                 m[pos] = self._surrounding_map[pos]
         return m
 
     def get_visible_things(self):
-
-        def calc_pos_in_fov(big_pos, small_pos, offset, size_of_axis):
-            pos = small_pos - offset
-            if big_pos == -1:
-                pos = small_pos - size_of_axis - offset
-            elif big_pos == 1:
-                pos = small_pos + size_of_axis - offset
-            return pos
-
         stencil = self.get_stencil()
         offset = self.get_surroundings_offset()
         visible_things = []
         stencil = self.get_stencil()
         offset = self.get_surroundings_offset()
         visible_things = []
-        size = self.world.map_size
-        fov_size = self.get_surrounding_map().size
-        for thing in self.world.things:
-            big_pos = thing.position[0]
-            small_pos = thing.position[1]
-            pos_y = calc_pos_in_fov(big_pos.y, small_pos.y, offset.y, size.y)
-            pos_x = calc_pos_in_fov(big_pos.x, small_pos.x, offset.x, size.x)
-            if pos_y < 0 or pos_x < 0 or\
-               pos_y >= fov_size.y or pos_x >= fov_size.x:
+        for thing in self.game.things:
+            pos = self.game.map_geometry.pos_in_projection(thing.position,
+                                                           offset,
+                                                           self.game.map_size)
+            if pos.y < 0 or pos.x < 0 or\
+               pos.y >= stencil.size.y or pos.x >= stencil.size.x:
                 continue
                 continue
-            if (not thing.in_inventory) and stencil[YX(pos_y, pos_x)] == '.':
+            if (not thing.in_inventory) and stencil[pos] == '.':
                 visible_things += [thing]
         return visible_things
 
     def get_pickable_items(self):
         pickable_ids = []
         visible_things = self.get_visible_things()
                 visible_things += [thing]
         return visible_things
 
     def get_pickable_items(self):
         pickable_ids = []
         visible_things = self.get_visible_things()
-        for t in [t for t in visible_things if
-                  isinstance(t, ThingItem) and
+        neighbor_fields = self.game.map_geometry.get_neighbors(self.position,
+                                                               self.game.map_size)
+        for t in [t for t in visible_things
+                  if isinstance(t, ThingItem) and
                   (t.position == self.position or
                   (t.position == self.position or
-                   t.position[1] in
-                   self.world.maps[YX(0,0)].get_neighbors(self.position[1]).values())]:
+                   t.position in neighbor_fields.values())]:
             pickable_ids += [t.id_]
         return pickable_ids
 
             pickable_ids += [t.id_]
         return pickable_ids