Example #1
0
File: road.py Project: ikn/latof
 def _jitter_clamp(self, x, lb, ub, lane):
     s = conf.TILE_SIZE[1]
     lb = s * lb + 1
     ub = s * (ub + 1) - 1
     inv_mean = 1. / conf.CRASH_POS_JITTER
     x = min(max(ir(x + randsgn() * expo(inv_mean)), lb), ub)
     tile_x = ((x - 1) if lane < 2 else x) / s
     return (tile_x, x)
Example #2
0
File: road.py Project: ikn/latof
 def _jitter_clamp (self, x, lb, ub, lane):
     s = conf.TILE_SIZE[1]
     lb = s * lb + 1
     ub = s * (ub + 1) - 1
     inv_mean = 1. / conf.CRASH_POS_JITTER
     x = min(max(ir(x + randsgn() * expo(inv_mean)), lb), ub)
     tile_x = ((x - 1) if lane < 2 else x) / s
     return (tile_x, x)
Example #3
0
  def generate( self ):
    '''Utilizes the Python standard library function for sampling from an
       exponential distribution.

       Returns:
         A random value governed by the exponential distribution with the user
         supplied rate.
    '''

    return expo( self.rate )
Example #4
0
File: road.py Project: ikn/latof
 def update(self):
     fps = self.level.game.scheduler.timer.fps
     speed = ir(float(conf.CAR_SPEED) / fps)
     speed_jitter = float(conf.CAR_SPEED_JITTER) / fps
     for lane, (lane_mode, stopping, (dirn, cars)) in \
         enumerate(zip(self._modes, self._stopping, self.cars)):
         # remove OoB cars
         if cars and self.oob(dirn, cars[0].rect):
             cars.pop(0)
         # add cars if needed
         if not cars or self.needs_car(dirn, cars[-1].rect):
             self.add_car(lane)
         # move cars
         prev = None
         new_mode = current_mode = 'moving'
         for car in cars:
             if lane_mode == 'moving':
                 car.start()
             if car.mode == 'moving':
                 r = car.rect
                 front = car.front(dirn)
                 v = speed + max(1 - speed,
                                 randsgn() * ir(expo(1 / speed_jitter)))
                 stop = front + dirn * v
                 # stop before stop marker
                 if lane_mode != 'moving' and current_mode == 'moving':
                     if dirn * front <= dirn * stopping:
                         stop = stopping
                     new_mode = lane_mode
                 # stop before next car
                 if prev is not None:
                     this_stop = prev.back(dirn) - \
                                 dirn * conf.CAR_GAP[current_mode]
                     stop = dirn * min(dirn * stop, dirn * this_stop)
                 new_v = dirn * (stop - front)
                 if new_v < v:
                     # velocity reduced
                     v = max(0, new_v)
                     if v == 0 and \
                        (current_mode == 'moving' or prev.mode != 'moving'):
                         # to make sure we only stop if every car in front
                         # has stopped - in case this car is faster than the
                         # one in front
                         car.set_mode(new_mode)
                     current_mode = new_mode
                 r.move_ip(v * dirn, 0)
             else:
                 new_mode = current_mode = car.mode
             prev = car
Example #5
0
File: road.py Project: ikn/latof
 def update (self):
     fps = self.level.game.scheduler.timer.fps
     speed = ir(float(conf.CAR_SPEED) / fps)
     speed_jitter = float(conf.CAR_SPEED_JITTER) / fps
     for lane, (lane_mode, stopping, (dirn, cars)) in \
         enumerate(zip(self._modes, self._stopping, self.cars)):
         # remove OoB cars
         if cars and self.oob(dirn, cars[0].rect):
             cars.pop(0)
         # add cars if needed
         if not cars or self.needs_car(dirn, cars[-1].rect):
             self.add_car(lane)
         # move cars
         prev = None
         new_mode = current_mode = 'moving'
         for car in cars:
             if lane_mode == 'moving':
                 car.start()
             if car.mode == 'moving':
                 r = car.rect
                 front = car.front(dirn)
                 v = speed + max(1 - speed,
                                 randsgn() * ir(expo(1 / speed_jitter)))
                 stop = front + dirn * v
                 # stop before stop marker
                 if lane_mode != 'moving' and current_mode == 'moving':
                     if dirn * front <= dirn * stopping:
                         stop = stopping
                     new_mode = lane_mode
                 # stop before next car
                 if prev is not None:
                     this_stop = prev.back(dirn) - \
                                 dirn * conf.CAR_GAP[current_mode]
                     stop = dirn * min(dirn * stop, dirn * this_stop)
                 new_v = dirn * (stop - front)
                 if new_v < v:
                     # velocity reduced
                     v = max(0, new_v)
                     if v == 0 and \
                        (current_mode == 'moving' or prev.mode != 'moving'):
                         # to make sure we only stop if every car in front
                         # has stopped - in case this car is faster than the
                         # one in front
                         car.set_mode(new_mode)
                     current_mode = new_mode
                 r.move_ip(v * dirn, 0)
             else:
                 new_mode = current_mode = car.mode
             prev = car