示例#1
0
    def start(self):
        super().start()
        # Find a spawn location
        ped_pos = h.Pathfind.save_coord_for_ped(self.x, self.y, 0, False, 0)
        if ped_pos is None:
            pos, heading = h.Pathfind.closest_vehicle_node_with_heading(
                self.x, self.y, 0, 0, 0, 0)
            ped_pos = h.Pathfind.save_coord_for_ped(pos.x, pos.y, 0, False, 0)
            if ped_pos is None:
                ped_pos = pos

        # Teleport player
        ped = h.Player.ped_id()
        while not ped.does_exist():
            h.wait(0)
            ped = h.Player.ped_id()
        ped.set_coords(ped_pos.x, ped_pos.y, ped_pos.z, 0, 0, 0, 1)
        ped.heading = heading
        for it in range(45):
            h.wait(0)
        ped.set_keep_task(False)
        h.Ai.clear_ped_tasks(ped)
        h.Ai.task_wander_in_area(ped, pos.x, pos.y, pos.z, 15, 0.01, 1)
        ped.set_keep_task(True)
        self.safety_check()

        self.setup_cam()

        # Reset the start time
        self.start_time = time()
示例#2
0
    def start(self, autopilot=None):
        logging.info('##################################################')
        logging.info('Starting scenario.\nParams:\n%s' %
                     utils.pretty_print_dict(self.params()))

        while h_utils.bad_stuff():
            h.wait(0)

        # Basic map setup.
        h.Gameplay.set_random_seed(self.seed)

        while not h.Pathfind.load_all_path_nodes():
            h.wait(0)

        # Setup weather and time.
        h.Gameplay.set_weather_type_now_persist(self.weather)
        h.Time.set_clock_time(self.in_game_time // 60, self.in_game_time % 60,
                              0)

        self.timeout_timer.reset()
        self.make_safe_timer.reset()

        # NOTE: method should be overriden.
        self.setup(autopilot)

        self._make_player_safe()
        self._remove_cameras()
        self._set_camera_view()

        # set_camera_zeroed()
        h_utils.toggle_controls(False)
示例#3
0
def pause(ticks):
    """
    No user inputs during pause.
    """
    for _ in range(ticks):
        toggle_controls(False)
        h.wait(0)
示例#4
0
def get_player_ped_id(n=100):
    for _ in range(n):
        ped = h.Player.ped_id()

        if ped.does_exist():
            return ped

        h.wait(0)

    logging.warn('Cannot get pyhookv Player ped_id.')

    return None
示例#5
0
 def ph_loop(self):
     if self.running:
         self.scenario = Scenario(**self.scenario_params)
         self.scenario.start()
         self.first_frame = True
         self.last_capture_time = 0
         try:
             while self.running:
                 h.wait(0)
                 if not self.scenario.update():
                     break
         finally:
             self.scenario.stop()
             self.scenario = None
示例#6
0
    def _update(self, first_call=False):
        h.wait(0)

        self._maybe_init_mmap()

        if first_call and self.can_start:
            self._is_env_running = True
            self._is_agent_running = True
            self.can_start = False

            return True

        self._read_from_client()
        self._write_to_client()

        return self._is_env_running
示例#7
0
    def _ready_vehicle(self):
        player_ped_id = h_utils.get_player_ped_id()
        pos = player_ped_id.get_coords(1)
        heading = player_ped_id.heading

        h_utils.set_ped_position(player_ped_id, pos)
        h_utils.set_ped_heading(player_ped_id, heading)

        h.wait(500)
        self.vehicle = h_utils.spawn_entity(
            self.vehicle_metadata.code,
            lambda: h_utils.spawn_vehicle(self.vehicle_metadata, pos, heading))

        h.wait(500)
        player_ped_id.set_into_vehicle(self.vehicle, -1)

        if INVISIBLE:
            self.vehicle.set_alpha(0, 0)
            player_ped_id.set_alpha(0, 0)
示例#8
0
def spawn_vehicle(vehicle_metadata, pos, heading, set_invincible=True, n_retry=100):
    for _ in range(n_retry):
        vehicle = h.Vehicle.create_vehicle(
                vehicle_metadata.hash,
                pos.x, pos.y, pos.z, heading, False, False)

        if vehicle.does_exist():
            if set_invincible:
                set_vehicle_invincible(vehicle)

            vehicle.set_on_ground_properly()

            return vehicle

        h.wait(100)

    logging.warn(
            'Cannot spawn vehicle. Type: %s, Code: %s' % (
                vehicle_metadata.code, vehicle_metadata.type))

    return None
示例#9
0
    def start(self):
        api.info("Starting new scenario", self.params())
        while bad_stuff():
            h.wait(0)
        # Basic map setup
        h.Gameplay.set_random_seed(self.seed)
        while not h.Pathfind.load_all_path_nodes():
            h.wait(0)
        api.info("Starting")

        # Start the timeout
        self.start_time = time()
        self.last_safety = time()

        # Setup the weather
        h.Gameplay.set_weather_type_now_persist(self.weather)

        # Set the time
        h.Time.set_clock_time(self.time // 60, self.time % 60, 0)

        # Disable the control
        h.Controls.disable_control_action(0, h.eControl.control_next_camera,
                                          True)
        h.Controls.disable_control_action(0,
                                          h.eControl.control_look_left_right,
                                          True)
        h.Controls.disable_control_action(0, h.eControl.control_look_up_down,
                                          True)
        h.Controls.disable_control_action(0, h.eControl.control_look_up_only,
                                          True)
        h.Controls.disable_control_action(0, h.eControl.control_look_down_only,
                                          True)
        h.Controls.disable_control_action(0, h.eControl.control_look_left_only,
                                          True)
        h.Controls.disable_control_action(0,
                                          h.eControl.control_look_right_only,
                                          True)
示例#10
0
    def start(self):
        super().start()
        # Find a spawn location
        pos, heading = h.Pathfind.closest_vehicle_node_with_heading(
            self.x, self.y, 0, 0, 0, 0)

        # Remove nearby vehicles (3m radius)
        # self._remove_vehicles_in_radius(pos.x, pos.y, pos.z, 3)
        self._remove_all_vehicles()

        # Teleport player
        ped = h.Player.ped_id()
        while not ped.does_exist():
            h.wait(0)
            ped = h.Player.ped_id()
        ped.set_coords(pos.x, pos.y, pos.z, 0, 0, 0, 1)
        ped.heading = heading
        h.wait(500)
        # Create the vehicle
        h.Streaming.request_model(self.vehicle.hash)
        while not h.Streaming.has_model_loaded(self.vehicle.hash):
            h.wait(100)

        for it in range(10):
            v = h.Vehicle.create_vehicle(self.vehicle.hash, pos.x, pos.y,
                                         pos.z, heading, False, False)
            h.wait(100)
            if v.does_exist(): break
        h.Streaming.set_model_as_no_longer_needed(self.vehicle.hash)
        v.set_on_ground_properly()
        h.wait(500)
        ped.set_into_vehicle(v, -1)

        # TODO: Setup the camera
        #		if h.Cam.get_follow_ped_view_mode() != 4:
        #			h.Cam.set_follow_ped_view_mode(4)
        #		if h.Cam.get_follow_vehicle_view_mode() != 4:
        #			h.Cam.set_follow_vehicle_view_mode(4)
        #		h.Cam.set_gameplay_relative_pitch(0., 0.)
        #		h.Cam.set_gameplay_relative_heading(0.)
        h.wait(100)

        # Setup the autopilot
        ped.set_driver_ability(100)
        ped.set_driver_aggressiveness(50)
        #ped.set_keep_task(False)
        h.Ai.clear_ped_tasks(ped)
        h.Ai.task_vehicle_drive_wander(ped, v, self.max_speed,
                                       self.driving_style)
        #ped.set_keep_task(True)

        # Make the vehicle invincible
        ped.set_config_flag(32, False)
        v.tyres_can_burst = False
        v.set_wheels_can_break(False)
        v.set_has_strong_axles(True)
        v.set_can_be_visibly_damaged(False)
        v.set_invincible(True)
        v.set_proofs(1, 1, 1, 1, 1, 1, 1, 1)

        self.v = v
        self.ped = ped

        self.safety_check()
        h.wait(500)

        self.setup_cam()

        # Reset the start time
        self.start_time = time()
        self.last_control = self.v.control