def step(self, dt: float): self.speed = abs(self.speed) car_to_target = (self.target - self.car.location) local_target = dot(car_to_target, self.car.rotation) angle = atan2(local_target[1], local_target[0]) vel = norm(self.car.velocity) in_air = (not self.car.on_ground) on_wall = (self.car.location[2] > 250 and not in_air) reverse = (cos(angle) < 0 and not (on_wall or in_air or self.kickoff)) get_off_wall = (on_wall and local_target[2] > 450) if get_off_wall: car_to_target[2] = -self.car.location[2] local_target = dot(car_to_target, self.car.rotation) angle = atan2(local_target[1], local_target[0]) max_speed = self.determine_max_speed(local_target) self.update_rlu_drive(reverse, max_speed) self.rlu_drive.step(dt) self.finished = self.rlu_drive.finished self.controls = self.rlu_drive.controls self.controls.handbrake = False if reverse: angle = -invert_angle(angle) if self.power_turn and not on_wall: angle *= -1 self.controls.handbrake = (vel > 200) self.controls.steer = cap(angle * 3, -1, 1) self.controls.boost = False if not self.controls.handbrake: self.controls.handbrake = (abs(angle) > radians(70) and vel > 500 and not on_wall) if self.controls.handbrake: self.controls.handbrake = (dot(self.car.velocity, car_to_target) > -150) if in_air: self.aerial_turn.target = look_at(xy(car_to_target), vec3(0, 0, 1)) self.aerial_turn.step(dt) aerial_turn_controls = self.aerial_turn.controls self.controls.pitch = aerial_turn_controls.pitch self.controls.yaw = aerial_turn_controls.yaw self.controls.roll = aerial_turn_controls.roll self.controls.boost = False
def place_random_monsters(self): #todo - maybe we should move this to mapgenerator (check for theme, etc) #choose random number of monsters #3d(dlvl) + 3d2 + 7 monsters total - at least 11 monsters on d1 and up-to 40 on d27 num_monsters = util.roll(1, gl.__dlvl__, util.roll(3, 2, 7)) num_monsters -= len(self.critters) free_squares = -2 for x in xrange(self.width): for y in xrange(self.height): tile = self.tile_at(x, y) if tile.passable(): free_squares += 1 num_monsters = util.cap(num_monsters, free_squares) #if it was capped - then map is too small... if num_monsters == free_squares: num_monsters = randrange(1, free_squares / 2) passes = num_monsters * 5 for i in range(num_monsters): if passes <= 0: break passes -= 1 #choose random spot for this monster x, y = self.find_random_square(self.has_critter_at) room = self._find_room(x, y) if room and room.src: if room.src.no_mon_gen: continue #determining critter level. it may vary from XL - 3 to XL + 1. To let is scale better multiply by 100 #cap it to be no lower than 1 crit_hd = max(randrange(gl.__xl__ - 3, gl.__xl__ + 1), 1) self.place_random_critter(gl.__dlvl__, crit_hd, x, y) #check for OOD monster. let's create OOD 10% of the time for now if libtcod.random_get_int(0, 0, 100) >= 89: crit_level = libtcod.random_get_int(0, gl.__dlvl__ + 2, gl.__dlvl__ + 3) crit_hd = max(randrange(gl.__xl__ - 3, gl.__xl__ + 3), 1) x, y = self.find_random_square(self.has_critter_at) room = self._find_room(x, y) if room and room.src: if room.src.no_mon_gen: return self.place_random_critter(crit_level, crit_hd, x, y)
def place_random_monsters(self): #todo - maybe we should move this to mapgenerator (check for theme, etc) #choose random number of monsters #3d(dlvl) + 3d2 + 7 monsters total - at least 11 monsters on d1 and up-to 40 on d27 num_monsters = util.roll(1, gl.__dlvl__, util.roll(3, 2, 7)) num_monsters -= len(self.critters) free_squares = -2 for x in xrange(self.width): for y in xrange(self.height): tile = self.tile_at(x, y) if tile.passable(): free_squares += 1 num_monsters = util.cap(num_monsters, free_squares) #if it was capped - then map is too small... if num_monsters == free_squares: num_monsters = randrange(1 , free_squares / 2) passes = num_monsters * 5 for i in range(num_monsters): if passes <= 0: break passes -= 1 #choose random spot for this monster x, y = self.find_random_square(self.has_critter_at) room = self._find_room(x, y) if room and room.src: if room.src.no_mon_gen: continue #determining critter level. it may vary from XL - 3 to XL + 1. To let is scale better multiply by 100 #cap it to be no lower than 1 crit_hd = max(randrange(gl.__xl__ - 3, gl.__xl__ + 1), 1) self.place_random_critter(gl.__dlvl__, crit_hd, x, y) #check for OOD monster. let's create OOD 10% of the time for now if libtcod.random_get_int(0, 0, 100) >= 89: crit_level = libtcod.random_get_int(0, gl.__dlvl__ + 2, gl.__dlvl__ + 3) crit_hd = max(randrange(gl.__xl__ - 3, gl.__xl__ + 3), 1) x, y = self.find_random_square(self.has_critter_at) room = self._find_room(x, y) if room and room.src: if room.src.no_mon_gen: return self.place_random_critter(crit_level, crit_hd, x, y)
def shooting_target(agent): ball = agent.info.ball car = agent.info.my_car car_to_ball = ball.pos - car.pos backline_intersect = line_backline_intersect( agent.info.their_goal.center[1], vec2(car.pos), vec2(car_to_ball)) if -500 < backline_intersect < 500: goal_to_ball = normalize(car.pos - ball.pos) error = cap(distance_2d(ball.pos, car.pos) / 1000, 0, 1) else: # Right of the ball if -500 > backline_intersect: target = agent.info.their_goal.corners[3] + vec3(400, 0, 0) # Left of the ball elif 500 < backline_intersect: target = agent.info.their_goal.corners[2] - vec3(400, 0, 0) goal_to_ball = normalize(ball.pos - target) goal_to_car = normalize(car.pos - target) difference = goal_to_ball - goal_to_car error = cap(abs(difference[0]) + abs(difference[1]), 1, 10) goal_to_ball_2d = vec2(goal_to_ball[0], goal_to_ball[1]) test_vector_2d = dot(rotation(0.5 * math.pi), goal_to_ball_2d) test_vector = vec3(test_vector_2d[0], test_vector_2d[1], 0) distance = cap((40 + distance_2d(ball.pos, car.pos) * (error**2)) / 1.8, 0, 4000) location = ball.pos + vec3( (goal_to_ball[0] * distance), goal_to_ball[1] * distance, 0) # this adjusts the target based on the ball velocity perpendicular to the direction we're trying to hit it multiplier = cap(distance_2d(car.pos, location) / 1500, 0, 2) distance_modifier = cap( dot(test_vector, ball.vel) * multiplier, -1000, 1000) modified_vector = vec3(test_vector[0] * distance_modifier, test_vector[1] * distance_modifier, 0) location += modified_vector # another target adjustment that applies if the ball is close to the wall extra = 3850 - abs(location[0]) if extra < 0: location[0] = cap(location[0], -3850, 3850) location[1] = location[1] + (-sign(agent.team) * cap(extra, -800, 800)) return location
def place_monsters(self): #choose random number of monsters #3d(dlvl) + 3d2 + 7 monsters total - at least 11 monsters on d1 and up-to 40 on d27 num_monsters = util.roll(1, gl.__dlvl__, util.roll(3, 2, 7)) #cap monster generation for now num_monsters = util.cap(num_monsters, 30) for i in range(num_monsters): #choose random spot for this monster x, y = self.find_random_square(self.has_critter_at) #determining critter level. it may vary from XL - 3 to XL + 3. To let is scale better multiply by 100 #cap it to be no lower than 1 crit_hd = util.cap_lower(randrange(gl.__xl__ - 3, gl.__xl__ + 3), 1, 1) self.place_critter(gl.__dlvl__, crit_hd, x, y) #check for OOD monster. let's create OOD 10% of the time for now if libtcod.random_get_int(0, 0, 100) >= 89: crit_level = libtcod.random_get_int(0, gl.__dlvl__ + 2, gl.__dlvl__ + 3) crit_hd = util.cap_lower(randrange(gl.__xl__ - 3, gl.__xl__ + 3), 1, 1) x, y = self.find_random_square(self.has_critter_at) self.place_critter(crit_level, crit_hd, x, y)
def step(self, dt: float): self.update_rlu_drive() self.rlu_drive.step(dt) self.finished = self.rlu_drive.finished car_to_target = (self.target - self.car.position) local_target = dot(car_to_target, self.car.orientation) angle = atan2(local_target[1], local_target[0]) self.controls = self.rlu_drive.controls reverse = (cos(angle) < 0) if reverse: angle = -invert_angle(angle) if self.power_turn: self.controls.throttle = (-self.controls.throttle - 1) / 2 angle *= -1 else: self.controls.throttle = -1 self.controls.steer = cap(angle * 3, -1, 1) self.controls.boost = False self.controls.handbrake = (abs(angle) > radians(70))
def shooting_target(agent): """"Method that gives the target for the shooting strategy""" ball = agent.info.ball car = agent.info.my_car car_to_ball = ball.location - car.location backline_intersect = line_backline_intersect(agent.their_goal.center[1], vec2(car.location), vec2(car_to_ball)) if abs(backline_intersect) < 700: goal_to_ball = normalize(car.location - ball.location) error = 0 else: # Right of the ball if -500 > backline_intersect: target = agent.their_goal.corners[3] + vec3(400, 0, 0) # Left of the ball elif backline_intersect > 500: target = agent.their_goal.corners[2] - vec3(400, 0, 0) goal_to_ball = normalize(ball.location - target) # Subtract the goal to car vector difference = goal_to_ball - normalize(car.location - target) error = cap(abs(difference[0]) + abs(difference[1]), 0, 5) goal_to_ball_2d = vec2(goal_to_ball[0], goal_to_ball[1]) test_vector_2d = dot(rotation(0.5 * math.pi), goal_to_ball_2d) test_vector = vec3(test_vector_2d[0], test_vector_2d[1], 0) distance = cap( (40 + distance_2d(ball.location, car.location) * (error**2)) / 1.8, 0, 4000) location = ball.location + vec3( (goal_to_ball[0] * distance), goal_to_ball[1] * distance, 0) # this adjusts the target based on the ball velocity perpendicular # to the direction we're trying to hit it multiplier = cap(distance_2d(car.location, location) / 1500, 0, 2) distance_modifier = cap( dot(test_vector, ball.velocity) * multiplier, -1000, 1000) location += vec3(test_vector[0] * distance_modifier, test_vector[1] * distance_modifier, 0) # another target adjustment that applies if the ball is close to the wall extra = 3850 - abs(location[0]) if extra < 0: location[0] = cap(location[0], -3850, 3850) location[1] = location[1] + (-sign(agent.team) * cap(extra, -800, 800)) return location
def generate_road(self): ms_start = libtcod.sys_elapsed_milli() _h = self.height / 3 road_y = randrange(_h - util.roll(1, 10), _h + util.roll(1, 10)) len_no_break = randrange(self.break_road / 2, self.break_road) old_road_y = road_y delta = 0 if self.rank > self.RANK_CITY: delta = 1 for x in xrange(0, self.width): len_no_break -= 1 if len_no_break <= 0: len_no_break = randrange(self.break_road / 2, self.break_road) y_delta = util.roll(1, 3) #nope, try another turn if y_delta == 1: len_no_break = 1 elif y_delta == 2: old_road_y, road_y = road_y, util.cap_lower( road_y - 1, 0, 0) delta = -1 else: old_road_y, road_y = road_y, util.cap( road_y + 1, self.height) delta = 1 self.roadminy = min(self.roadminy, road_y) self.roadmaxy = max(self.roadmaxy, road_y) if old_road_y != road_y: self._map[old_road_y][x] = self.road() self._map[road_y][x] = self.road() old_road_y = road_y else: self._map[road_y][x] = self.road() if self.rank >= self.RANK_CITY: self._map[road_y + delta][x] = self.road() ms_end = libtcod.sys_elapsed_milli() print 'generated in ' + str(ms_end - ms_start) + ' ms'
def generate_road(self): ms_start = libtcod.sys_elapsed_milli() _h = self.height / 3 road_y = randrange(_h - util.roll(1, 10), _h + util.roll(1, 10)) len_no_break = randrange(self.break_road / 2, self.break_road) old_road_y = road_y delta = 0 if self.rank > self.RANK_CITY: delta = 1 for x in xrange(0, self.width): len_no_break -= 1 if len_no_break <= 0: len_no_break = randrange(self.break_road / 2, self.break_road) y_delta = util.roll(1, 3) #nope, try another turn if y_delta == 1: len_no_break = 1 elif y_delta == 2: old_road_y, road_y = road_y, util.cap_lower(road_y - 1, 0, 0) delta = -1 else: old_road_y, road_y = road_y, util.cap(road_y + 1, self.height) delta = 1 self.roadminy = min(self.roadminy, road_y) self.roadmaxy = max(self.roadmaxy, road_y) if old_road_y != road_y: self._map[old_road_y][x] = self.road() self._map[road_y][x] = self.road() old_road_y = road_y else: self._map[road_y][x] = self.road() if self.rank >= self.RANK_CITY: self._map[road_y + delta][x] = self.road() ms_end = libtcod.sys_elapsed_milli() print 'generated in ' + str(ms_end - ms_start) + ' ms'
def quaff(self, player): super(HealingPotion, self).quaff(player) player.hp = util.cap(player.hp + util.roll(2, 10, 1), player.base_hp) gl.message('You feel somewhat better')
def update_coords(self, playerx, playery): self.x = util.cap_lower(playerx - self.center[0], 0, 0) self.y = util.cap_lower(playery - self.center[1], 0, 0) self.x2 = util.cap(self.x + self.w - 1, self.map.width) self.y2 = util.cap(self.y + self.h - 1, self.map.height) self.playerxy = (playerx, playery)
def update_rlu_drive(self, reverse: bool = False, max_speed: float = 2200): self.target = self.target self.rlu_drive.target = self.target self.rlu_drive.speed = cap(self.speed * (-1 if reverse else 1), -max_speed, max_speed)