Esempio n. 1
0
 def resetBallClosest(self):
     all_keys = ("midSpot", "topSpot", "botSpot")
     available_keys = [
         key for key in all_keys if not World.instance.space.point_query(
             [
                 float(v)
                 for v in ScriptLoader.instance.object_map[key].position
             ],
             0.0,
             pymunk.ShapeFilter(mask=DYNAMIC_CATEGORY),
         )
     ]
     best_key = sorted([(
         magnitude_sq([
             a - b for a, b in zip(
                 ScriptLoader.instance.object_map["IR_BALL"].body.position,
                 ScriptLoader.instance.object_map[key].position,
             )
         ]),
         key,
     ) for key in (available_keys if available_keys else all_keys)])[0][1]
     ScriptLoader.instance.object_map["IR_BALL"].body.position = [
         float(v)
         for v in ScriptLoader.instance.object_map[best_key].position
     ]
     ScriptLoader.instance.object_map["IR_BALL"].body.velocity = (0.0, 0.0)
Esempio n. 2
0
 def tick(self, tick):
     ball_pos = self.tracking_ball.position
     sensor = ScriptLoader.instance.object_map[self.getPrefix() + 'light_up_2']
     distance = np.sqrt(magnitude_sq(ball_pos - sensor.position))
     vector = ball_pos - sensor.position
     relative_bearing = np.arctan2(vector[1], vector[0]) - sensor.rotation
     self.device_class._calc(relative_bearing, distance)
     for x in range(5):
         ScriptLoader.instance.object_map[self.getPrefix() + f'light_up_{x}'].visual.fill = (max(min(255 * self.device_class.value(x+1) / 9, 255), 0), 0, 0)
     return False
Esempio n. 3
0
 def tick(self, tick):
     # Make sure the water tower is in the tile, and has small velocity
     if magnitude_sq(
             self.waterTower.body.velocity) > self.MAX_WATER_VELOCITY_SQ:
         # Bad! Infer which bot based on in the tile.
         self.rescue.lackOfProgress(0)
         return
     pos1 = self.waterTower.position
     pos2 = self.rescue.tiles[self.index]["all_elems"][0].position
     if abs(pos1[0] - pos2[0]) > self.rescue.TILE_LENGTH / 2 or abs(
             pos1[1] - pos2[1]) > self.rescue.TILE_LENGTH / 2:
         # Bad! Infer which bot based on in the tile.
         self.rescue.lackOfProgress(0)
Esempio n. 4
0
 def resetBallClosest(self):
     all_keys = ("midSpot", "topSpot", "botSpot")
     available_keys = [
         key for key in all_keys if not World.instance.space.point_query(
             ScriptLoader.instance.object_map[key].position, 0.0,
             pymunk.ShapeFilter(mask=DYNAMIC_CATEGORY))
     ]
     best_key = sorted([(
         magnitude_sq(
             ScriptLoader.instance.object_map["IR_BALL"].body.position -
             ScriptLoader.instance.object_map[key].position),
         key,
     ) for key in (available_keys if available_keys else all_keys)])[0][1]
     ScriptLoader.instance.object_map[
         "IR_BALL"].body.position = ScriptLoader.instance.object_map[
             best_key].position
     ScriptLoader.instance.object_map["IR_BALL"].body.velocity = np.array(
         [0.0, 0.0])
 def handleEvent(self, event):
     super().handleEvent(event)
     if (
         event.type == EV3SIM_BOT_COMMAND and 
         event.command_type == CommandSystem.TYPE_CUSTOM and
         isinstance(event.payload, str) and
         event.payload.startswith("Position: ")
     ):
         data = event.payload[10:]
         lines = data.split(", ")
         if len(lines) == 2 and lines[0].startswith("x: ") and lines[1].startswith("y: "):
             x = float(lines[0][3:])
             y = float(lines[1][3:])
             if magnitude_sq([x - self.robot.position[0], y - self.robot.position[1]]) < 20:
                 ScriptLoader.instance.object_map["positionText"].text = f"{x}, {y}" + " - Correct!"
                 ScriptLoader.instance.object_map["positionBG"].fill = "#22aa22"
             else:
                 ScriptLoader.instance.object_map["positionText"].text = f"{x}, {y}" + " - Wrong!"
                 ScriptLoader.instance.object_map["positionBG"].fill = "#aa2222"
Esempio n. 6
0
    def tick(self, tick):
        super().tick(tick)
        self.cur_tick = tick
        for i in range(len(self.robots)):
            self.bot_follows[i].body.position = [
                float(v)
                for v in local_space_to_world_space(
                    ScriptLoader.instance.robots[f"Robot-{i}"]._follow_collider_offset,
                    self.robots[i].body.angle,
                    (self.robots[i].body.position.x, self.robots[i].body.position.y),
                )
            ]
            # Ensure visual is not 1 frame behind.
            self.bot_follows[i].position = np.array(self.bot_follows[i].body.position)
            if self.current_follow is not None and not World.instance.paused:
                distance = magnitude_sq(
                    self.bot_follows[i].position
                    - self._recurseObj(self.tiles[self.current_follow[0]]["follows"], self.current_follow[1:])
                )
                if distance > self.MAX_FOLLOW_DIST * self.MAX_FOLLOW_DIST and not self.roaming[i]:
                    self.lackOfProgress(i)
            if self.roaming[i] and not World.instance.paused:
                # Check we are still in the tile. Kinda bad.
                if (
                    abs(self.bot_follows[i].position[0] - self.tiles[self.roam_tile[i]]["world_pos"][0])
                    > self.TILE_LENGTH / 2
                    or abs(self.bot_follows[i].position[1] - self.tiles[self.roam_tile[i]]["world_pos"][1])
                    > self.TILE_LENGTH / 2
                ) and magnitude_sq(
                    self.bot_follows[i].position
                    - self._recurseObj(self.tiles[self.roam_tile[i]]["follows"], self.roaming[i][1])
                ) > (
                    self.ROBOT_CENTRE_RADIUS + self.FOLLOW_POINT_RADIUS + 0.05
                ) * (
                    self.ROBOT_CENTRE_RADIUS + self.FOLLOW_POINT_RADIUS + 0.05
                ):
                    self.lackOfProgress(i)
            if not World.instance.paused:
                x = (self.bot_follows[i].position[0] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                y = (self.bot_follows[i].position[1] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                for tile in self.tiles:
                    tx = (tile["world_pos"][0] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                    ty = (tile["world_pos"][1] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                    if tx == x and ty == y:
                        if tile["type"] == "rescue" and self.modes[i] == self.MODE_FOLLOW:
                            self.modes[i] = self.MODE_RESCUE
                            # For reentry.
                            self.resetFollows()
                            self.current_follow = None
                        elif tile["type"] == "follow" and self.modes[i] == self.MODE_RESCUE:
                            self.modes[i] = self.MODE_FOLLOW
                            if self.can_scored_this_spawn:
                                self.scoreReEntry()
                        break
                else:
                    self.lackOfProgress(i)
                if not self.can_scored_this_spawn:
                    cx = (self.can_obj.position[0] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                    cy = (self.can_obj.position[1] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                    for tile in self.tiles:
                        tx = (tile["world_pos"][0] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                        ty = (tile["world_pos"][1] + self.TILE_LENGTH / 2) // self.TILE_LENGTH
                        if tx == cx and ty == cy:
                            if tile["type"] != "rescue":
                                self.scoreCan()
                            rel_pos_x = self.can_obj.position[0] - tile["world_pos"][0]
                            rel_pos_y = self.can_obj.position[1] - tile["world_pos"][1]
                            sx = 1
                            sy = 1
                            if rel_pos_x < 0:
                                rel_pos_x = -rel_pos_x
                                sx = -1
                            if rel_pos_y < 0:
                                rel_pos_y = -rel_pos_y
                                sy = -1
                            if rel_pos_y < self.TILE_LENGTH / 3 and rel_pos_x < self.TILE_LENGTH / 3:
                                break
                            under, right, under_right = self._checkConnectingRescueTiles(tile["world_pos"], sx, sy)
                            if under_right and under and right:
                                # We can't be outside of the area.
                                break
                            if under:
                                if rel_pos_x < self.TILE_LENGTH / 3:
                                    break
                            if right:
                                if rel_pos_y < self.TILE_LENGTH / 3:
                                    break
                            self.scoreCan()
                            break
                    else:
                        self.scoreCan()

        for i in range(len(self.tiles)):
            if self.tiles[i]["type"] == "follow":
                self.tiles[i]["checker"].tick(tick)
        # UI Tick
        if self._pressed:
            ScriptLoader.instance.object_map["controlsReset"].visual.image_path = "ui/controls_reset_pressed.png"
        else:
            ScriptLoader.instance.object_map["controlsReset"].visual.image_path = "ui/controls_reset_released.png"
        self.update_time()
Esempio n. 7
0
 def handleEvent(self, event):
     if self.mode == self.MODE_NORMAL or self.mode == self.MODE_CAN_DRAGGING:
         button_filter = lambda x: True
     else:
         button_filter = lambda x: False
     super().handleEvent(event, button_filter=button_filter)
     if self.mode == self.MODE_NORMAL:
         if event.type == pygame.MOUSEMOTION:
             self.current_mpos = event.pos
         elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
             world_mpos = utils.screenspace_to_worldspace(
                 self.current_mpos, self.customMap)
             vec = [
                 self.can_obj.position[0] - world_mpos[0],
                 self.can_obj.position[1] - world_mpos[1],
             ]
             d = magnitude_sq(vec)
             if d <= pow(2.5, 2):
                 self.mode = self.MODE_CAN_DRAGGING
                 self.rel_pos = vec
                 return
             if self.current_mpos[0] > self.side_width:
                 self.selectTile(self.toTilePos(self.current_mpos))
         elif event.type == pygame.MOUSEWHEEL:
             for attr, conv, inc in [
                 ("rotation_entry", int, 90),
             ]:
                 if hasattr(self, attr):
                     rect = getattr(self, attr).get_relative_rect()
                     if (rect.left <= self.current_mpos[0] <= rect.right
                             and rect.top <= self.current_mpos[1] <=
                             rect.bottom):
                         try:
                             val = conv(getattr(self, attr).text)
                             val += event.y * inc
                             getattr(self, attr).set_text(str(val))
                         except:
                             pass
         elif event.type == pygame.KEYDOWN:
             if event.key == pygame.K_BACKSPACE or event.key == pygame.K_DELETE:
                 # Check that no entry is focused.
                 good = True
                 for attr in [
                         "rotation_entry",
                 ]:
                     if hasattr(self, attr) and getattr(self,
                                                        attr).is_focused:
                         good = False
                 if good:
                     self.removeSelected()
     elif self.mode == self.MODE_CAN_DRAGGING:
         if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
             self.mode = self.MODE_NORMAL
             return
         elif event.type == pygame.MOUSEMOTION:
             self.current_mpos = event.pos
             world_mpos = utils.screenspace_to_worldspace(
                 self.current_mpos, self.customMap)
             self.can_obj.position = [
                 self.rel_pos[0] + world_mpos[0],
                 self.rel_pos[1] + world_mpos[1],
             ]
             self.previous_info["settings"]["rescue"][
                 "CAN_SPAWN_POSITION"] = [
                     float(self.can_obj.position[0] - self.tile_offset[0]),
                     float(self.can_obj.position[1] - self.tile_offset[1]),
                 ]