def survivor_strategy(state): pid = state[2][1] actions = [] my_ships = [] enemy_ships = [] for obj in state[3][2]: if obj[0][0] == pid: print(obj) my_ships.append(obj) else: enemy_ships.append(obj) for my_ship in my_ships: my_pos = my_ship[0][2] thrust = (-sign(my_pos[0]), 0) if abs(my_pos[0]) > abs(my_pos[1]) else (0, -sign(my_pos[1])) actions.append([0, my_ship[0][1], thrust]) if enemy_ships: enemy_ship = random.choice(enemy_ships) enemy_pos = enemy_ship[0][2] enemy_speed = enemy_ship[0][3] actions.append([ 2, my_ship[0][1], (enemy_pos[0] + enemy_speed[0], enemy_pos[1] + enemy_speed[1]), 5 ]) return actions
def apply(self, state): st = State.parse(state) pid = state[2][1] actions = [] my_ships = [] enemy_ships = [] my_ships = [] enemy_ships = [] for some_ship in st.ships: if some_ship.player == st.me: my_ships.append(some_ship) else: enemy_ships.append(some_ship) print(f'Player test:' + '\n' + "\n".join(str(s) for s in my_ships)) for my_ship in my_ships: my_pos = [my_ship.x, my_ship.y] my_vel = [my_ship.vx, my_ship.vy] cur_closest = trace_orbit(my_pos[0], my_pos[1], my_vel[0], my_vel[1]) thrust = (0, 0) thrust = (-sign(my_pos[0]), 0) if abs(my_pos[0]) > abs(my_pos[1]) else ( 0, -sign(my_pos[1])) actions.append([0, my_ship.id, thrust]) if my_ship.heat == 0: self.k += 1 actions.append( my_ship.do_laser(-my_ship.x + self.k - 4, -my_ship.y, 64)) return actions
def apply(self, state): self.T += 1 st = State.parse(state) actions = [] my_ships = [] enemy_ships = [] for some_ship in st.ships: if some_ship.id not in self.birthday: self.birthday[some_ship.id] = self.T if some_ship.player == st.me: my_ships.append(some_ship) else: enemy_ships.append(some_ship) if self.printships: print( f'T:{self.T} Player {st.me}: {" ".join(str([s.fuel, s.laser, s.regen, s.lives]) for s in my_ships)}' ) for my_ship in my_ships: my_ship = my_ship birthday = self.birthday[my_ship.id] age = self.T - birthday if self.duplicate and my_ship.lives > 1 and self.T > 10: actions.append(my_ship.do_duplicate()) my_pos = [my_ship.x, my_ship.y] my_vel = [my_ship.vx, my_ship.vy] cur_closest = trace_orbit(my_pos[0], my_pos[1], my_vel[0], my_vel[1]) thrust = (0, 0) if cur_closest <= 17: thrust = (-sign(my_pos[0]), -sign(my_pos[0])) if abs( my_pos[0]) > abs(my_pos[1]) else (sign(my_pos[1]), -sign(my_pos[1])) # find closest friend - if too close randomize movement (include velocity in distance computation) closest_ship, dist = None, 1000 for other in my_ships: if other.id == my_ship.id: continue od = abs(other.x - my_ship.x) + abs(other.y - my_ship.y) + abs( other.vx - my_ship.vx) + abs(other.vy - my_ship.vy) if od < dist: dist = od closest_ship = other if closest_ship and dist < 4: thrust = (random.randint(-1, 1), random.randint(-1, 1)) actions.append([0, my_ship.id, thrust]) if enemy_ships: enemy_ship = random.choice(enemy_ships) if my_ship.laser and self.do_laser: ex, ey = enemy_ship.next_round_expected_location() actions.append(my_ship.do_laser(ex, ey)) return actions
def move_towards(x, vx, tx): """ x - where we are; vx - our speed; tx - where we want to be. Returns optimal do_thrust power. Speeds up only if we can later stop without overshoooting. Slows down if not slowing down would result in overdo_lasering. """ if x == tx: return sign(vx) s = sign(tx - x) if s == -1: x, vx, tx = -x, -vx, -tx def can_stop(x, vx): return x + vx * (vx - 1) // 2 <= tx if can_stop(x + vx + 1, vx + 1): return -s elif can_stop(x + vx, vx): return 0 else: return s
def get_mothership_actions(self, my_ship, st, enemy_ships): # we need to spawn swarm first thing actions = [] if my_ship.lives > 1: swarm_fuel = max(my_ship.fuel - self.laser_ship_stats[0], 0) for n in range(my_ship.lives - 1, 0, -1): f = (swarm_fuel * n) // (my_ship.lives - 1) if 2 * (f + n) >= my_ship.total_hp(): continue actions.append(my_ship.do_duplicate_from_mothership(f, n)) break # override all other actions with LaserShipStrategy if True: thrust = self.mothership_not_fall_on_planet(my_ship, st) if thrust.x != 0 or thrust.y != 0: actions.append(my_ship.do_thrust(thrust.x, thrust.y)) actions.extend( self.mothership_strategy.apply_laser(st, my_ship, enemy_ships, thrust, self.enemy_location)) return actions my_pos = [my_ship.x, my_ship.y] my_vel = [my_ship.vx, my_ship.vy] cur_closest, cur_farthest = trace_orbit(my_pos[0], my_pos[1], my_vel[0], my_vel[1], 384 - self.T) thrust = (0, 0) if cur_closest <= 24: thrust = (-sign(my_pos[0]), -sign(my_pos[0])) if abs( my_pos[0]) > abs(my_pos[1]) else (sign(my_pos[1]), -sign(my_pos[1])) if cur_farthest > st.field_size: thrust = (sign(my_vel[0]), sign(my_vel[1])) if my_ship.heat + THRUST_HEAT > my_ship.max_heat: thrust = 0, 0 actions.append([0, my_ship.id, thrust]) thrust_action = Thrust(*thrust) enemy_ship = self.choose_laser_target(my_ship, thrust_action, enemy_ships) if enemy_ship: predicted_thrust = self.enemy_thrust[enemy_ship.id] ex, ey = self.enemy_location[enemy_ship.id] next_dist = my_ship.next_dist(thrust_action, enemy_ship, predicted_thrust) if my_ship.laser: power = self.asses_laser_power(my_ship, thrust_action, enemy_ship) if power > 0: actions.append(my_ship.do_laser(ex, ey, power)) return actions
def apply(self, state): self.T += 1 st = State.parse(state) self.reset_precomputed() all_actions_of_all_ships = [] for ship in st.ships: if ship.id not in self.thrust_predictors: self.thrust_predictors[ship.id] = ThrustPredictor() self.thrust_predictors[ship.id].add(ship.last_actions) my_ships = [] enemy_ships = [] for some_ship in st.ships: if some_ship.id not in self.birthday: self.birthday[some_ship.id] = self.T if some_ship.player == st.me: my_ships.append(some_ship) else: enemy_ships.append(some_ship) self.precompute_enemy_stuff(some_ship) if self.printships: print(f'T:{self.T} Player {st.me}:' + '\n' + "\n".join(str(s) for s in my_ships)) for my_ship in my_ships: # TODO: beter logic if my_ship.laser > 0 and my_ship.lives == 1: all_actions_of_all_ships.extend( self.laser_ship.apply(st, my_ship, enemy_ships, self.enemy_location)) continue actions = [] my_ship = my_ship birthday = self.birthday[my_ship.id] age = self.T - birthday my_pos = [my_ship.x, my_ship.y] my_vel = [my_ship.vx, my_ship.vy] razduplyaemsya = True cur_closest, cur_farthest = trace_orbit(my_pos[0], my_pos[1], my_vel[0], my_vel[1], 265 - self.T) thrust = (0, 0) if cur_closest <= 24: thrust = (-sign(my_pos[0]), -sign(my_pos[0])) if abs( my_pos[0]) > abs(my_pos[1]) else (sign(my_pos[1]), -sign(my_pos[1])) razduplyaemsya = False if cur_farthest > st.field_size: thrust = (sign(my_vel[0]), sign(my_vel[1])) razduplyaemsya = False if self.duplicate and my_ship.lives > 1 and razduplyaemsya: actions.append(my_ship.do_duplicate()) # find closest friend - if too close randomize movement (include velocity in distance computation) closest_ship, dist = None, 1000 for other in my_ships: if other.id == my_ship.id: continue od = abs(other.x - my_ship.x) + abs(other.y - my_ship.y) + abs( other.vx - my_ship.vx) + abs(other.vy - my_ship.vy) if od < dist: dist = od closest_ship = other if closest_ship and dist < 2 and my_ship.vx == closest_ship.vx and my_ship.vy == closest_ship.vy: dx = random.randint(-1, 1) dy = random.randint(-1, 1) x = thrust[0] if thrust[0] == dx else thrust[0] + dx y = thrust[1] if thrust[1] == dy else thrust[0] + dy thrust = x, y # if len(enemy_ships) == 1 and self.T > 200 and st.me == ATACKER: # enemy_ship = enemy_ships[0] # predicted_thrust = self.thrust_predictors[enemy_ship.id].predict() # ex, ey = enemy_ship.next_round_expected_location(predicted_thrust) # x = move_towards(my_ship.x, my_ship.vx, ex) # y = move_towards(my_ship.y, my_ship.vy, ey) # thrust = x, y if my_ship.heat + THRUST_HEAT > my_ship.max_heat: thrust = 0, 0 actions.append([0, my_ship.id, thrust]) thrust_action = Thrust(*thrust) enemy_ship = self.choose_laser_target(my_ship, thrust_action, enemy_ships) if enemy_ship: predicted_thrust = self.enemy_thrust[enemy_ship.id] ex, ey = self.enemy_location[enemy_ship.id] next_dist = my_ship.next_dist(thrust_action, enemy_ship, predicted_thrust) if my_ship.laser and self.do_laser: power = self.asses_laser_power(my_ship, thrust_action, enemy_ship) if power > 0: actions.append(my_ship.do_laser(ex, ey, power)) enemy_ship = self.choose_explode_target(my_ship, thrust_action, enemy_ships) if enemy_ship: predicted_thrust = self.enemy_thrust[enemy_ship.id] next_dist = my_ship.next_dist(thrust_action, enemy_ship, predicted_thrust) if next_dist < 6 and st.me == ATACKER and self.T > 7 and len( my_ships) >= len(enemy_ships): actions = [my_ship.do_explode()] if next_dist < 6 and st.me == DEFENDER and self.T > 7 and len( my_ships) > len(enemy_ships): actions = [my_ship.do_explode()] all_actions_of_all_ships.extend(actions) return all_actions_of_all_ships