Exemple #1
0
 def test_empirical_occlusion(self):
     from shapely.occlusion import occlusion
     mpos_x, mpos_y = 0, 0
     sight_triangle_width, sight_triangle_height = 100, 100
     sight_polygon_coords = [[mpos_x, mpos_y], [mpos_x - sight_triangle_width/2, mpos_y + sight_triangle_height], [mpos_x + sight_triangle_width/2, mpos_y + sight_triangle_height]]
     res1, _useless = occlusion(0, 0, sight_polygon_coords, [0, 10, 0, 18, 8, 18, 8, 10], 8)
     res2, _useless = occlusion(0, 0, sight_polygon_coords, [], 0)
     res3, _useless = occlusion(0, 0, sight_polygon_coords, [12, 0, 12, 8, 20, 8, 20, 0], 8)
     self.assertTrue(res1 != res2 and res1 != res3 and res2 == res3, "Those three executions of occlusion() should have returned different results for res1 and res2 but same for res2 and res3")
Exemple #2
0
 def test_empirical_occlusion(self):
     from shapely.occlusion import occlusion
     mpos_x, mpos_y = 0, 0
     sight_triangle_width, sight_triangle_height = 100, 100
     sight_polygon_coords = [[mpos_x, mpos_y],
                             [
                                 mpos_x - sight_triangle_width / 2,
                                 mpos_y + sight_triangle_height
                             ],
                             [
                                 mpos_x + sight_triangle_width / 2,
                                 mpos_y + sight_triangle_height
                             ]]
     res1, _useless = occlusion(0, 0, sight_polygon_coords,
                                [0, 10, 0, 18, 8, 18, 8, 10], 8)
     res2, _useless = occlusion(0, 0, sight_polygon_coords, [], 0)
     res3, _useless = occlusion(0, 0, sight_polygon_coords,
                                [12, 0, 12, 8, 20, 8, 20, 0], 8)
     self.assertTrue(
         res1 != res2 and res1 != res3 and res2 == res3,
         "Those three executions of occlusion() should have returned different results for res1 and res2 but same for res2 and res3"
     )
Exemple #3
0
    def step(self):
        # TODO: Maybe re-write the following lines, for a better handling of
        #       game termination.
        if self.auto_mode and self.__game_finished():
            self.end_of_game()
            return

        # Update players' positions and visions
        for p in self.__players:
            normalized_array = self.__get_normalized_direction_vector_from_angle(p.move_angle)
            self.__move_player(p, normalized_array[0] * p.speedx, normalized_array[1] * p.speedy)
            p.obstacles_in_sight = []
            p.obstacles_in_sight_n = 0
            # ------- Update player's sight -------
            # Parametrize things for occlusion (get obstacles that need to be taken into account by occlusion)
            sight_direction = self.__get_normalized_direction_vector_from_angle(p.sight_angle) * p.sight_range
            # A bit bruteforce here, let's use a circle instead of the real shaped vision
            # Just because there won't be many items to go through anyway
            # and for simplicity's and implementation speed's sakes
            y_start = max(0, p.posy - p.sight_range)
            y_end = min(self.slmap.max_y, p.posy + p.sight_range)
            x_start = max(0, p.posx - p.sight_range)
            x_end = min(self.slmap.max_x, p.posx + p.sight_range)
            row_start   = utils.norm_to_cell(y_start)
            row_end     = utils.norm_to_cell(y_end)
            col_start   = utils.norm_to_cell(x_start)
            col_end     = utils.norm_to_cell(x_end)
            vect = ((x_start, y_start), (x_end, y_end))
            self.__for_obstacle_in_range(vect, self.__occlusion_get_obstacle_in_range_callback, player=p)
            p.compute_sight_polygon_coords()
            # Launch occlusion
            p.sight_vertices, p.occlusion_polygon = occlusion(p.posx, p.posy, p.sight_polygon_coords, p.obstacles_in_sight, p.obstacles_in_sight_n)

            # ---------- Update player's visible objects list ----------
            # Note: Here we only go through the visible objects that are in a given range, not through all of them
            # We will go through the complete list, in order to update them, later in this method
            del p.visible_objects[:] # Empty the list

            for row in xrange(row_start, row_end+1):
                for col in xrange(col_start, col_end+1):
                    try:
                        for item in self.__actionable_items[self.__map_item_key_from_row_col(row, col)]:
                            if p.occlusion_polygon.intersects(item.geometric_point):
                                p.add_new_visible_object(item)
                    except KeyError:
                        pass # There was nothing at this (row,col) position...
                    try:
                        for item in self.__proximity_objects[self.__map_item_key_from_row_col(row, col)]:
                            if p.occlusion_polygon.intersects(item.geometric_point):
                                p.add_new_visible_object(item)
                    except KeyError:
                        pass # There was nothing at this (row,col) position...

            # ---------- Update player's visible players list ----------

            del p.visible_players[:] # Empty the list
            # Re-populate it
            for p2 in self.__players:
                if p2 is p:
                    continue # Do not include ourself in visible objects
                if p.occlusion_polygon.intersects(p2.hitbox):
                    p.visible_players.append((p2.player_id, p2.posx, p2.posy, p2.move_angle))
        # end of by player loop

        # Now go through all of the visible items to update them
        for row in xrange(0, self.slmap.height):
            for col in xrange(0, self.slmap.width):
                try:
                    for item in self.__actionable_items[self.__map_item_key_from_row_col(row, col)]:
                        item.update()
                except KeyError:
                    pass # There was nothing at this (row,col) position...
                try:
                    for item in self.__proximity_objects[self.__map_item_key_from_row_col(row, col)]:
                        item.update()
                        # Try to activate the proximity object on this player
                        for p_ in self.__players:
                            item.activate(p_)
                except KeyError:
                    pass # There was nothing at this (row,col) position...