def pick_fireangle(self, x, y, speedx, speedy, angle): origin = Point2(x, y) delta = Point2(speedx, speedy) leftangle = Matrix3.new_rotate(pi / 2) rightangle = Matrix3.new_rotate(-pi / 2) targetleft = origin + leftangle * delta * 100 targetright = origin + rightangle * delta * 100 left = LineSegment2(origin, targetleft) right = LineSegment2(origin, targetright) dleft = dright = 10000000 for w in self.walls: ileft = w.intersect(left) if ileft: dleft = min(dleft, ileft.distance(origin)) iright = w.intersect(right) if iright: dright = min(dright, iright.distance(origin)) if max(dleft, dright) < 35: return COMPENSATE = pi / 8 if dleft > dright: targetleft = Matrix3.new_rotate(COMPENSATE) * targetleft result = relative_angle(x, y, targetleft.x, targetleft.y, angle) else: targetright = Matrix3.new_rotate(-COMPENSATE) * targetright result = relative_angle(x, y, targetright.x, targetright.y, angle) return result
def messageReceived(self, msg): msg = bunchify(msg) if 'status' in msg: targets = [obj for obj in msg.proximity if obj.object_type == 'player'] if targets: my_pos= Point2(*msg.gps.position) # rotate aiming to the target t = targets[0] t_pos = Point2(t.position[0] + t.velocity[0] * POS_PER_VEL, t.position[1] + t.velocity[1] * POS_PER_VEL) turn = relative_angle(my_pos.x, my_pos.y, t_pos.x, t_pos.y, msg.gps.angle) else: # rotate a tick to the right turn = 0.1 # do the rotation and shoot self.command('turn', value=turn) self.command('fire')
def follow(self, my_position, enemy_position, my_angle): angle_relat = relative_angle(my_position[0], my_position[1], enemy_position[0], enemy_position[1], my_angle) self.command("turn", value=angle_relat)
def walk_the_park(self, gps): x, y = gps['position'] angle = gps['angle'] if self.steps % 20 == 0: self.dx, self.dy = self.get_new_destination(gps) turn = relative_angle(x, y, self.dx, self.dy, angle) return turn, 1
def messageReceived(self, message): if message['type'] == 'sensor' and message.get('gps', False): gps = message['gps'] x, y = message['gps']['position'] angle = message['gps']['angle'] fire = 0 # Butterfly if far_from_center(gps): if looking_center(gps): throttle = 1 else: throttle = 0 turn = relative_angle(x, y, 50, 50, angle) else: turn = 1 fire = 1 if random() < 0.3: throttle = 1 else: throttle = 0 # Speed control if too_fast(gps) and not looking_center(gps): throttle = 0 if too_slow(gps): throttle = 1 # Lock-on closer_player = get_closer_player(message) if closer_player: px, py = closer_player['position'] turn = relative_angle(x, y, px, py, angle) if should_fire(closer_player, turn): fire = 1 else: fire = 0 if point_defense(message): fire = 1 self.command("throttle", value=throttle) self.command("turn", value=turn) if fire: self.command("fire")
def get_new_destination(self, gps): x, y = gps['position'] angle = gps['angle'] shuffle(self.directions) for d in self.directions: pdx, pdy = x + d[0], y + d[1] if not self.collides(x, y, pdx, pdy) \ and math.fabs(relative_angle(x, y, pdx, pdy, angle)) < 4: return pdx, pdy return self.directions[0]
def point_defense(message): myxy = message['gps']['position'] angle = message['gps']['angle'] for obj in message.get('proximity', []): if obj['object_type'] in ['bullet']: pxy = obj['position'] d = distance(myxy, pxy) a = relative_angle(myxy[0], myxy[1], pxy[0], pxy[1], angle) if d < 2 and a < 2: return True return False
def smart_turn(self): if self.message['type'] == 'sensor': currentx = self.message['gps']['position'][0] currenty = self.message['gps']['position'][1] currentangle = self.message['gps']['angle'] for x in self.message['proximity']: if x['object_type'] == 'player': targetx = x['position'][0] targety = x['position'][1] break return relative_angle(currentx, currenty, targetx, targety, currentangle)
def point_and_shoot(self, target): # where to aim vel_modifier = 1 if target.object_type == 'player': vel_modifier = 0.1 elif target.object_type == 'bullet': vel_modifier = 0.2 aim_at = predict_pos(target.position, target.velocity, vel_modifier) turn = relative_angle(self.pos.x, self.pos.y, aim_at.x, aim_at.y, self.angle) # aim and shoot self.command('turn', value=turn) self.command('fire')
def butterfly(self, gps): if far_from_center(gps): if looking_center(gps): throttle = 1 else: throttle = 0 turn = relative_angle(x, y, 50, 50, angle) else: turn = 1 fire = 1 if random() < 0.3: throttle = 1 else: throttle = 0 return turn, throttle
def messageReceived(self, message): if message.get('type') == 'sensor': self.command("throttle", value=1) tracking = False x, y = message['gps']['position'] angle = message['gps']['angle'] for obj in message.get('proximity', []): if obj['object_type'] in ['powerup', 'player']: tracking = True trackx, tracky = obj['position'] turn = relative_angle(x, y, trackx, tracky, angle) self.command('turn', value=turn) if obj['object_type'] == 'player': self.command("fire") break if not tracking and random() < 0.2: self.command("fire") self.command("turn", value=1)
def messageReceived(self, message): if message.get("type") == "sensor": self.command("throttle", value=1) tracking = False x, y = message["gps"]["position"] angle = message["gps"]["angle"] for obj in message.get("proximity", []): if obj["object_type"] in ["powerup", "player"]: tracking = True trackx, tracky = obj["position"] turn = relative_angle(x, y, trackx, tracky, angle) self.command("turn", value=turn) if obj["object_type"] == "player": self.command("fire") break if not tracking and random() < 0.2: self.command("fire") self.command("turn", value=1)
def messageReceived(self, message): if message['type'] == 'map_description': self.map = message if message['type'] == 'sensor' and message.get('gps', False): self.steps += 1 gps = message['gps'] x, y = message['gps']['position'] angle = message['gps']['angle'] fire = 0 # Butterfly # turn, throttle = self.butterfly(gps) #turn, throttle = self.navigate(gps) turn, throttle = self.walk_the_park(gps) # Speed control if too_fast(gps) and not looking_center(gps): throttle = 0 if too_slow(gps): throttle = 1 # Lock-on closer_player = self.get_closer_player(message) if closer_player: px, py = closer_player['position'] turn = relative_angle(x, y, px, py, angle) if should_fire(closer_player, turn): fire = 1 else: fire = 0 if point_defense(message): fire = 1 self.command("throttle", value=throttle) self.command("turn", value=turn) if fire: self.command("fire")
def look_to(self, target): turn = relative_angle(self.pos.x, self.pos.y, target.x, target.y, self.angle) self.command('turn', value=turn)
def looking_center(gps): x, y = gps['position'] angle = gps['angle'] return math.fabs(relative_angle(x, y, 50, 50, angle)) < 5
def parse_sensor(self, message): if 'gps' not in message: return x, y = message['gps']['position'] angle = message['gps']['angle'] speedx, speedy = message['gps']['velocity'] # Increase visited tile count self.visit(x, y) objs = message.get('proximity', []) bullets = self.filter_visible(objs, x, y, 'bullet') bullets = [b for b in bullets if self.is_incoming(x, y, b)] players = self.filter_visible(objs, x, y, 'player') powerups = self.filter_visible(objs, x, y, 'powerup') if players: #~ print "Firing at player" obj = players[0] trackx, tracky = obj['position'] trackspeedx, trackspeedy = obj['velocity'] d = dist(x, y, trackx, tracky) turn = relative_angle(x, y, trackx, tracky, angle) self.command('turn', value=turn) self.guesstimate = Guesstimate(trackx, tracky, trackspeedx, trackspeedy) self.command("fire") elif bullets: #~ print "Firing at bullet" obj = bullets[0] trackx, tracky = obj['position'] trackspeedx, trackspeedy = obj['velocity'] d = sqrt((x - trackx)**2 + (y - tracky)**2) turn = relative_angle(x, y, trackx, tracky, angle) self.command('turn', value=turn) self.command("fire") elif powerups: #~ print "Grabbing powerup" obj = powerups[0] trackx, tracky = obj['position'] trackspeedx, trackspeedy = obj['velocity'] d = sqrt((x - trackx)**2 + (y - tracky)**2) # (x + speedx, y + speedy) here, to compensate the "orbit" effect turn = relative_angle( x + speedx * d / 70, y + speedy * d / 70, trackx, tracky, angle) self.command('turn', value=turn) self.command("throttle", value=1) #~ else: #~ print obj elif self.guesstimate and self.guesstimate.certainty > 0: #~ print "Firing at a guesstimate", self.guesstimate.certainty turn = relative_angle(x, y, self.guesstimate.x, self.guesstimate.y, angle) self.command('turn', value=turn) self.command("fire") self.guesstimate.step() else: #~ print "No guesstimate" # Pick a good next exploration tile if self.going is None or not self.has_line_of_fire(x, y, self.going.x, self.going.y): options = [t for t in self.tiles if self.has_line_of_fire(x, y, t.x, t.y)] if not options: return options.sort(key=lambda x: x.visits) options = [o for o in options if o.visits == options[0].visits] #~ print "Options:", options self.going = max(options, key=lambda t:t.distance_to(x, y)) speed = speedx**2 + speedy**2 speedangle = atan2(speedy, speedx) targetangle = atan2(self.going.y - y, self.going.x - x) #~ print speed, angle, speedangle divergence = (targetangle - speedangle) % TWO_PI divergence = min(divergence, TWO_PI - divergence) if speed > 500 and divergence < 0.1: #~ print "Firing at random" fireangle = self.pick_fireangle(x, y, speedx, speedy, angle) if fireangle is not None: self.command("turn", value=fireangle) if abs(fireangle) < 1: self.command('fire') # Take that! else: #~ if speed <= 1590: #~ print "Not bang because speed = %.3f" % speed #~ elif divergence >= 0.1: #~ print "Not bang because divergence = %.3f" % divergence #~ else: #~ print "EH!? OGG WANT BANG!!1! Y U NO BANG!??!?1!?" #~ print "Going to", self.going turn = relative_angle(x, y, self.going.x + speedx, self.going.y + speedy, angle) self.command("throttle", value=1) self.command("turn", value=turn)