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 = []
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 = []
def __init__(self, new_location, new_type=STANDARD_CAR, new_length_covered=0): """ Constructor method. """ # Check the vehicle type if new_type not in VEHICLE_TYPES: raise Exception( 'ERROR (in car.__init__()) : unknown type of vehicle !') # Initialization of the parameters self.path = [] self.is_waiting = False self.width = VEHICLE[new_type][DEFAULT_WIDTH] self.speed = VEHICLE[new_type][DEFAULT_SPEED] self.headway = VEHICLE[new_type][DEFAULT_HEADWAY] self.length = VEHICLE[new_type][DEFAULT_LENGTH] self.force = VEHICLE[new_type][DEFAULT_FORCE] self.mass = VEHICLE[new_type][DEFAULT_MASS] self.color = VEHICLE[new_type][DEFAULT_COLOR] self.acceleration = 0 self.sight_distance = 5 * VEHICLE[STANDARD_CAR][DEFAULT_LENGTH] self.total_waiting_time = 0 self.last_waiting_time = 0 self.dead = False # Several sub-categories : truck, long truck, bus if new_type == TRUCK: possible_sizes = [(2, 25), (3, 60), (4, 15)] self.length *= lib.proba_poll(possible_sizes) self.mass *= self.length # Cars must be created on a lane if isinstance(new_location, __road__.Lane): self.location = new_location self.location.cars.insert(0, self) self.length_covered = new_length_covered else: raise ValueError( 'ERROR (in car.__init__()) : new cars must be created on a lane !' ) self.generate_path()
def __init__(self, new_location, new_type = STANDARD_CAR, new_length_covered = 0): """ Constructor method. """ # Check the vehicle type if new_type not in VEHICLE_TYPES: raise Exception('ERROR (in car.__init__()) : unknown type of vehicle !') # Initialization of the parameters self.path = [] self.is_waiting = False self.width = VEHICLE[new_type][DEFAULT_WIDTH] self.speed = VEHICLE[new_type][DEFAULT_SPEED] self.headway = VEHICLE[new_type][DEFAULT_HEADWAY] self.length = VEHICLE[new_type][DEFAULT_LENGTH] self.force = VEHICLE[new_type][DEFAULT_FORCE] self.mass = VEHICLE[new_type][DEFAULT_MASS] self.color = VEHICLE[new_type][DEFAULT_COLOR] self.acceleration = 0 self.sight_distance = 5 * VEHICLE[STANDARD_CAR][DEFAULT_LENGTH] self.total_waiting_time = 0 self.last_waiting_time = 0 self.dead = False # Several sub-categories : truck, long truck, bus if new_type == TRUCK: possible_sizes = [(2, 25), (3, 60), (4, 15)] self.length *= lib.proba_poll(possible_sizes) self.mass *= self.length # Cars must be created on a lane if isinstance(new_location, __road__.Lane): self.location = new_location self.location.cars.insert(0, self) self.length_covered = new_length_covered else: raise ValueError('ERROR (in car.__init__()) : new cars must be created on a lane !') self.generate_path()