Beispiel #1
0
def dict_to_game_state(state_dict):
    gs = GameState()
    if 'ball' in state_dict:
        gs.ball = BallState()
        if 'physics' in state_dict['ball']:
            gs.ball.physics = dict_to_physics(state_dict['ball']['physics'])
    if 'cars' in state_dict:
        gs.cars = {}
        for index, car in state_dict['cars'].items():
            car_state = CarState()
            if 'physics' in car:
                car_state.physics = dict_to_physics(car['physics'])
            if 'boost_amount' in car:
                car_state.boost_amount = car['boost_amount']
            gs.cars[int(index)] = car_state
    if 'game_info' in state_dict:
        gs.game_info = GameInfoState()
        if 'paused' in state_dict['game_info']:
            gs.game_info.paused = state_dict['game_info']['paused']
        if 'world_gravity_z' in state_dict['game_info']:
            gs.game_info.world_gravity_z = state_dict['game_info']['world_gravity_z']
        if 'game_speed' in state_dict['game_info']:
            gs.game_info.game_speed = state_dict['game_info']['game_speed']
    if 'console_commands' in state_dict:
        gs.console_commands = state_dict['console_commands']
    return gs
Beispiel #2
0
    def main(self):
        self.set_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), "SuperchargedBots.cfg"))

        self.teams = []

        if self.get_bool_from_config("Options", "help_blue_team"):
            self.teams.append(0)
        print(f"SuperchargedBots: help_blue_team = {0 in self.teams}")

        if self.get_bool_from_config("Options", "help_orange_team"):
            self.teams.append(1)
        print(f"SuperchargedBots: help_orange_team = {1 in self.teams}")

        self.bots_only = self.get_bool_from_config("Options", "bots_only")
        print(f"SuperchargedBots: bots_only = {self.bots_only}")

        self.bonus_boost_accel_percent = self.get_float_from_config("Options", "bonus_boost_accel_percent") / 100
        print(f"SuperchargedBots: bonus_boost_accel_percent = {self.bonus_boost_accel_percent * 100}%")

        self.bonus_boost_tank = self.get_int_from_config("Options", "bonus_boost_tank")
        print(f"SuperchargedBots: bonus_boost_tank = {self.bonus_boost_tank}")

        self.minimum_boost = self.get_int_from_config("Options", "minimum_boost")
        print(f"SuperchargedBots: minimum_boost = {self.minimum_boost}")

        self.bonus_hit_percent = self.get_int_from_config("Options", "bonus_hit_percent")
        print(f"SuperchargedBots: bonus_hit_percent = {self.bonus_hit_percent}")

        self.demo_helper = self.get_bool_from_config("Options", "demo_helper")
        print(f"SuperchargedBots: demo_helper = {self.demo_helper}")

        self.socket_relay = SocketRelay()
        self.socket_relay.player_input_change_handlers.append(self.input_change)

        self.non_blocking_socket_relay = Thread(target=self.socket_relay.connect_and_run, args=(False, True, False))
        self.non_blocking_socket_relay.start()

        while 1:
            try:
                self.packet: GameTickPacket = self.wait_game_tick_packet()
                
                time = self.packet.game_info.seconds_elapsed
                self.delta_time = time - self.time
                self.time = time

                supercharged_bots = []
                cars = dict()

                for car_index in range(self.packet.num_cars):
                    car = self.packet.game_cars[car_index]

                    if (self.bots_only and not car.is_bot) or car.team not in self.teams:
                        continue

                    if car.name not in self.tracker:
                        self.tracker[car.name] = DEFAULT_CAR.copy()

                    supercharged_bots.append(car.name)

                    if not self.packet.game_info.is_round_active:
                        continue

                    if self.packet.game_info.is_kickoff_pause:
                        self.tracker[car.name]['total_boost'] = BOOST_CONSUMPTION
                        self.tracker[car.name]['last_boost'] = BOOST_CONSUMPTION
                        continue

                    velocity = None

                    if self.demo_helper:
                        for other_car_index in range(self.packet.num_cars):
                            other_car = self.packet.game_cars[other_car_index]

                            if car.team == other_car.team:
                                continue

                            car_location = Vector.from_vector(car.physics.location)
                            other_car_location = Vector.from_vector(other_car.physics.location)

                            if car_location.flat_dist(other_car_location) < 200 and abs(Vector.from_vector(car.physics.velocity).angle(other_car_location - car_location)) < 0.5:
                                velocity = Vector.from_vector(car.physics.velocity).flatten().scale(2300)

                    if self.tracker[car.name]['boosting']:
                        if not self.tracker[car.name]['steering'] and (car.boost > self.minimum_boost):
                            CP = math.cos(car.physics.rotation.pitch)
                            SP = math.sin(car.physics.rotation.pitch)
                            CY = math.cos(car.physics.rotation.yaw)
                            SY = math.sin(car.physics.rotation.yaw)
                            forward = Vector(CP*CY, CP*SY, SP)
                            if velocity is None:
                                velocity = Vector.from_vector(car.physics.velocity) + forward * (BOOST_ACCEL * self.delta_time * self.bonus_boost_accel_percent)

                        self.tracker[car.name]['total_boost'] -= BOOST_CONSUMPTION * self.delta_time * (100 / self.bonus_boost_tank)

                    boost_amount = None

                    if car.boost > self.minimum_boost and car.boost > self.tracker[car.name]['last_boost']:
                        self.tracker[car.name]['total_boost'] += car.boost - self.tracker[car.name]['last_boost']
                    elif car.boost < self.minimum_boost:
                        self.tracker[car.name]['total_boost'] = self.minimum_boost
                    
                    self.tracker[car.name]['total_boost'] = cap(self.tracker[car.name]['total_boost'], 0, 100)
                    floored_boost = math.floor(self.tracker[car.name]['total_boost'])
                    if floored_boost != car.boost:
                        boost_amount = floored_boost
                    self.tracker[car.name]['last_boost'] = car.boost if boost_amount is None else boost_amount

                    if velocity is None and boost_amount is None:
                        continue

                    cars[car_index] = CarState(
                        Physics(
                            velocity=None if velocity is None else Vector3(*velocity)
                        ),
                        boost_amount=boost_amount
                    )

                last_ball_touch = self.packet.game_ball.latest_touch
                ball = None

                if last_ball_touch.time_seconds > self.last_ball_touch_time:
                    if (last_ball_touch.time_seconds - self.last_ball_touch_time) > 0.5:
                        if not self.bots_only or self.packet.game_cars[last_ball_touch.player_index].is_bot:
                            if last_ball_touch.team in self.teams:
                                bonus_hit_multiplier = self.bonus_hit_percent / 100 + 1
                                ball_velocity = Vector.from_vector(self.packet.game_ball.physics.velocity) * Vector(bonus_hit_multiplier, bonus_hit_multiplier, 1 / bonus_hit_multiplier)
                                ball = BallState(physics=Physics(
                                    velocity=Vector3(*ball_velocity)
                                ))

                    self.last_ball_touch_time = last_ball_touch.time_seconds

                game_state = GameState()

                if cars:
                    game_state.cars = cars

                if ball is not None:
                    game_state.ball = ball    

                self.set_game_state(game_state)

                if self.last_packet_time == -1 or self.time - self.last_packet_time >= 0.1:
                    self.matchcomms.outgoing_broadcast.put_nowait({
                        "supercharged_bots": supercharged_bots,
                        "supercharged_config": {
                            "bonus_boost_accel_percent": self.bonus_boost_accel_percent,
                            "bonus_boost_tank": self.bonus_boost_tank,
                            "minimum_boost": self.minimum_boost,
                            "bonus_hit_percent": self.bonus_hit_percent,
                            "demo_helper": self.demo_helper,
                        }
                    })
            except Exception:
                print_exc()