Beispiel #1
0
def run_simulation(ticks,
                   algorithm_flag=True,
                   graphic_display=True,
                   info_display=False,
                   limit=150,
                   car_limit=15):
    from utils.utils import init_graphic_environment, do_round
    from models.channel import Channel
    import pygame
    channel = Channel()
    cars = read_simulation_file(ticks)
    created_cars = {}
    car_queue = []
    if graphic_display:
        screen, background, intersection_background, font = init_graphic_environment(
        )

    for tick in range(ticks):
        if tick == limit:
            break
        # print(tick)
        if car_limit >= len(list(created_cars.keys())):
            for index in range(len(cars['cars'])):
                if car_queue:
                    new_queue = []
                    for ind in range(len(car_queue)):
                        car = car_queue[ind]
                        lane = int(car['lane'])
                        length = 1
                        if channel.get_recent_entering()[lane] is not None:
                            length = channel.get_recent_entering(
                            )[lane].get_car_length()
                        coordinates = car['initial_coordinates']
                        rect = pygame.Rect(float(coordinates['x_coordinate']),
                                           float(coordinates['y_coordinate']),
                                           length, length)
                        rect.center = (float(coordinates['x_coordinate']),
                                       float(coordinates['y_coordinate']))
                        collides = collides_at_spawn(
                            rect,
                            channel.get_recent_entering()[lane])
                        if collides:
                            new_queue.append(car)
                        else:
                            created_cars[int(car['name'])] = recreate_car(
                                car, channel, algorithm_flag=algorithm_flag)
                    car_queue = new_queue
                lane = int(cars['cars'][index]['lane'])
                length = 1
                if channel.get_recent_entering()[lane] is not None:
                    length = channel.get_recent_entering(
                    )[lane].get_car_length()
                car = cars['cars'][index]
                if int(car['creation_time']) <= tick:
                    coordinates = car['initial_coordinates']
                    rect = pygame.Rect(float(coordinates['x_coordinate']),
                                       float(coordinates['y_coordinate']),
                                       length, length)
                    rect.center = (float(coordinates['x_coordinate']),
                                   float(coordinates['y_coordinate']))
                    collides = collides_at_spawn(
                        rect,
                        channel.get_recent_entering()[lane])
                    #print(channel.get_recent_entering()[lane].get_acceleration())
                    #print(created_cars)
                    if collides:
                        car_queue.append(car)
                    else:
                        created_cars[int(car['name'])] = recreate_car(
                            car, channel, algorithm_flag=algorithm_flag)
                else:
                    cars['cars'] = cars['cars'][index:]
                    break
        if graphic_display:
            screen.blit(background, (0, 0))
            screen.blit(intersection_background, (0, 0))
            for car in created_cars.values():
                screen.blit(car.rotated_image, car.screen_car)
            pygame.display.update(screen.get_rect())

        do_round(list(created_cars.values()), channel)

        if info_display:
            for car in created_cars.values():
                print('name: ' + str(car.get_name()) + ' controller: ' +
                      car.get_controller().__name__ + ' left: ' +
                      ' left_inner: ' + str(car.left_inner_rectangle) +
                      ' tick: ' + str(tick) + ' is_supervisor: ' +
                      str(car.is_supervisor()) + ' is_second: ' +
                      str(car.is_second_at_charge()) + ' inside_full: ' +
                      str(car.inside_full_rectangle))

    pygame.display.quit()