Пример #1
0
    def update_car(self, car):
        """
        Updates a given car on the roundabout 
        """

        if not (car in self.car_spiraling_time):
            self.car_spiraling_time[car] = [
                car.total_waiting_time, lib.clock()
            ]

        car.total_waiting_time = self.car_spiraling_time[car][0] + lib.clock(
        ) - self.car_spiraling_time[car][1]

        if not (car.path is None) and self.leaving_roads:
            next_way = car.next_way(
                True)  # Just read the next_way unless you really go there
            car_slot = lib.find_key(self.slots_cars, car)

            #   The car has lost its slot
            if car_slot is None:
                raise Exception(
                    "ERROR (in Roundabout.update_car()) : a car has no slot !")

            #   The car's slot is in front of a leaving road
            if self.slots_roads[car_slot] in self.leaving_roads:
                if (self.leaving_roads[next_way].is_free) and self.slots_roads[
                        car_slot] == self.leaving_roads[next_way]:
                    #la route sur laquelle on veut aller est vidée et surtout _en face_  du slot de la voiture
                    car.join(self.leaving_roads[car.next_way(False) % len(
                        self.leaving_roads)].get_free_lane()
                             )  # cette fois on fait une lecture destructive

        #la voiture n'a pas d'endroit où aller : on la met dans le couloir de la mort
        else:
            self.to_kill.append(car)
Пример #2
0
    def update_car(self, car):
        """
        Updates a given car on the roundabout 
        """
        
        if not (car in self.car_spiraling_time):
            self.car_spiraling_time[car] = [car.total_waiting_time, lib.clock()]
            
        car.total_waiting_time = self.car_spiraling_time[car][0] + lib.clock() - self.car_spiraling_time[car][1]
        
        if not(car.path is None) and self.leaving_roads:
            next_way = car.next_way(True) # Just read the next_way unless you really go there
            car_slot = lib.find_key(self.slots_cars, car)

            #   The car has lost its slot   
            if car_slot is None:
                raise Exception("ERROR (in Roundabout.update_car()) : a car has no slot !")
            
            #   The car's slot is in front of a leaving road
            if self.slots_roads[car_slot] in self.leaving_roads:            
                if (self.leaving_roads[next_way].is_free) and self.slots_roads[car_slot] == self.leaving_roads[next_way]:
                #la route sur laquelle on veut aller est vidée et surtout _en face_  du slot de la voiture
                    car.join(self.leaving_roads[car.next_way(False) % len(self.leaving_roads)].get_free_lane()) # cette fois on fait une lecture destructive

        #la voiture n'a pas d'endroit où aller : on la met dans le couloir de la mort
        else:
            self.to_kill.append(car)
Пример #3
0
    def join(self, new_location):
        """
        Allocate the car to the given location. The concept of location makes it possible to use this code for either a lane or a roundabout.
            new_location    (road or lane or Roundabout)  :   lane or roundabout that will host, if possible, the car
            new_position    (list)          :   position in the lane or in the roundabout (note that the meaning of the position depends on the kind of location)
        """
        #   Leave the current location
        if (self.location is not None) and (self in self.location.cars):
            self.location.cars.remove(self)
        else:
            raise Exception(
                "WARNING (in car.join()) : a car had no location !")

        #   Roundabout -> Lane
        if isinstance(self.location, __roundabout__.Roundabout) and isinstance(
                new_location, __road__.Lane):
            car_slot = lib.find_key(self.roundabout.slots_cars, self)

            #   Remove car from its slot
            if car_slot is None:
                raise Exception(
                    "WARNING (in car.join()) : a car leaving from a roundabout had no slot !"
                )
            else:
                self.roundabout.slots_cars[car_slot] = None

            self.acceleration = self.force / self.mass

        #   Lane -> roundabout
        elif isinstance(new_location,
                        __roundabout__.Roundabout) and isinstance(
                            self.location, __road__.Lane):
            self.catch_slot(new_location)

        #   Lane -> lane OR roundabout -> roundabout : ERROR
        else:
            raise Exception(
                'ERROR (in car.join()) : road to road OR roundabout to roundabout junction is forbidden !'
            )

        #   Update data
        self.length_covered = 0
        self.location = new_location
        self.speed = 0
        self.location.cars.insert(0, self)  # CONVENTION SENSITIVE
Пример #4
0
    def update(self):
        """
        Updates the roundabout : rotate the cars, dispatch them...
        """
        #self.incoming_roads = sorted(self.incoming_roads, compa)
        #   Make the cars rotate
        if lib.clock() - self.last_shift > ROUNDABOUT_ROTATION_RATE:
            self.last_shift = lib.clock()
            self.slots_roads = lib.shift_list(self.slots_roads)

        #   Spawning mode
        if self.spawning and len(self.leaving_roads) and (
                lib.clock() - self.spawn_timer > self.spawn_time):
            self.spawn_timer = lib.clock()
            num_possible_roads = len(self.leaving_roads)
            # Possible ways out. NB : the "1000/ " thing ensures *integer* probabilities.
            possible_roads_events = [(self.leaving_roads[i],
                                      1000 / num_possible_roads)
                                     for i in range(num_possible_roads)]

            chosen_road = lib.proba_poll(possible_roads_events)
            if chosen_road.is_free:
                car_type_events = [(STANDARD_CAR, 80), (TRUCK, 15),
                                   (SPEED_CAR, 5)]

                new_car = __car__.Car(chosen_road.get_free_lane(),
                                      lib.proba_poll(car_type_events))

        #   Update traffic lights
        self._update_traffic_lights()

        #   Update cars
        for car in self.cars:
            self.update_car(car)

        #   Kill cars that have reached their destination
        for car in self.to_kill:
            car_slot = lib.find_key(self.slots_cars, car)
            self.slots_cars[car_slot] = None
            car.die()

        self.to_kill = []
Пример #5
0
    def update(self):
        """
        Updates the roundabout : rotate the cars, dispatch them...
        """
        #self.incoming_roads = sorted(self.incoming_roads, compa)
        #   Make the cars rotate
        if lib.clock() - self.last_shift > ROUNDABOUT_ROTATION_RATE:
            self.last_shift = lib.clock()
            self.slots_roads = lib.shift_list(self.slots_roads)

        #   Spawning mode
        if self.spawning and len(self.leaving_roads) and (lib.clock() - self.spawn_timer > self.spawn_time):
            self.spawn_timer = lib.clock() 
            num_possible_roads    = len(self.leaving_roads)
            # Possible ways out. NB : the "1000/ " thing ensures *integer* probabilities.
            possible_roads_events = [(self.leaving_roads[i], 1000/num_possible_roads) for i in range(num_possible_roads)]
        
            chosen_road = lib.proba_poll(possible_roads_events)
            if chosen_road.is_free:
                car_type_events = [(STANDARD_CAR, 80), 
                                   (TRUCK       , 15), 
                                   (SPEED_CAR   ,  5)]
                                   
                new_car = __car__.Car(chosen_road.get_free_lane(), lib.proba_poll(car_type_events))

        #   Update traffic lights
        self._update_traffic_lights()
                
        #   Update cars
        for car in self.cars:
            self.update_car(car)
        
        #   Kill cars that have reached their destination
        for car in self.to_kill:
            car_slot = lib.find_key(self.slots_cars, car)
            self.slots_cars[car_slot] = None
            car.die()

        self.to_kill = []
Пример #6
0
    def join(self, new_location):
        """
        Allocate the car to the given location. The concept of location makes it possible to use this code for either a lane or a roundabout.
            new_location    (road or lane or Roundabout)  :   lane or roundabout that will host, if possible, the car
            new_position    (list)          :   position in the lane or in the roundabout (note that the meaning of the position depends on the kind of location)
        """
        #   Leave the current location
        if (self.location is not None) and (self in self.location.cars):
            self.location.cars.remove(self)
        else:
            raise Exception("WARNING (in car.join()) : a car had no location !")

        #   Roundabout -> Lane
        if isinstance(self.location, __roundabout__.Roundabout) and isinstance(new_location, __road__.Lane):
            car_slot = lib.find_key(self.roundabout.slots_cars, self)
            
            #   Remove car from its slot
            if car_slot is None:
                raise Exception("WARNING (in car.join()) : a car leaving from a roundabout had no slot !")
            else:
                self.roundabout.slots_cars[car_slot] = None

            self.acceleration = self.force/self.mass

        #   Lane -> roundabout
        elif isinstance(new_location, __roundabout__.Roundabout) and isinstance(self.location, __road__.Lane):
            self.catch_slot(new_location)

        #   Lane -> lane OR roundabout -> roundabout : ERROR
        else:
            raise Exception('ERROR (in car.join()) : road to road OR roundabout to roundabout junction is forbidden !')
        
        #   Update data
        self.length_covered = 0
        self.location       = new_location
        self.speed          = 0
        self.location.cars.insert(0, self)  # CONVENTION SENSITIVE