Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ShnizelBatata
- class planet:
- def __init__(self, p):
- self.num_ships = p.num_ships()
- self.planet_id = p.planet_id()
- self.growth_rate = p.growth_rate()
- self.owner = p.owner()
- self.location = (p.x(), p.y())
- self.planet = p
- class fleet:
- def __init__(self, f):
- self.owner = f.owner()
- self.num_ships = f.num_ships()
- self.src = f.source_planet()
- self.dest = f.destination_planet()
- self.turns_remaining = f.turns_remaining()
- self.trip_length = f.total_trip_length()
- def init_planets(pw):
- friendly = list()
- enemy = list()
- neutral = list()
- if pw.my_planets():
- for i in pw.my_planets():
- friendly.append(planet(i))
- if pw.enemy_planets():
- for i in pw.enemy_planets():
- enemy.append(planet(i))
- if pw.neutral_planets():
- for i in pw.neutral_planets():
- neutral.append(planet(i))
- return (
- friendly, enemy, neutral)
- def init_fleets(pw):
- friendly = list()
- enemy = list()
- if pw.my_fleets():
- for i in pw.my_fleets():
- friendly.append(fleet(i))
- if pw.enemy_fleets():
- for i in pw.enemy_fleets():
- enemy.append(fleet(i))
- return (
- friendly, enemy)
- def calculate_worth_neutral(pw, src, dest):
- if dest.num_ships != 0:
- return float(dest.growth_rate) / (dest.num_ships ** 1.5 * pw.distance(src.planet, dest.planet) ** 3)
- return float(dest.growth_rate) / pw.distance(src.planet, dest.planet) ** 3
- def calculate_worth_enemy(pw, src, dest, my_fleets, enemy_fleets):
- dist = pw.distance(src.planet, dest.planet)
- ships = dest.num_ships + dest.growth_rate * dist
- if my_fleets:
- for i in my_fleets:
- if i.dest == dest.planet_id and i.turns_remaining <= dist:
- ships -= i.num_ships
- if enemy_fleets:
- for i in enemy_fleets:
- if i.dest == dest.planet_id and i.turns_remaining <= dist:
- ships += i.num_ships
- if dest.growth_rate != 0 and ships > 0:
- return (float(dest.growth_rate) / (ships ** 1.5 * pw.distance(src.planet, dest.planet) ** 2.5), ships)
- return (0.0, 0)
- def calculate_worth_friendly(pw, src, dest, my_fleets, enemy_fleets):
- dist = pw.distance(src.planet, dest.planet)
- ships = dest.num_ships
- takeover_turn = 0
- last_turn = 0
- enemy_fleets.sort(key=lambda x: x.turns_remaining)
- if my_fleets:
- for i in my_fleets:
- if i.dest == dest.planet_id and i.turns_remaining <= dist:
- ships += i.num_ships
- if enemy_fleets:
- for i in enemy_fleets:
- if i.dest == dest.planet_id and i.turns_remaining <= dist:
- ships -= i.num_ships
- if i.turns_remaining > last_turn and takeover_turn == 0:
- ships += (i.turns_remaining - last_turn) * dest.growth_rate
- if ships < 0 and takeover_turn == 0:
- takeover_turn = i.turns_remaining
- if ships < 0:
- ships -= dest.growth_rate * (dist - takeover_turn)
- ships = -ships
- return (
- float(dest.growth_rate) / (ships ** 1.5 * dist ** 3), ships)
- else:
- return (0.0, 0)
- def do_turn(pw):
- my_planets, enemy_planets, neutral_planets = init_planets(pw)
- my_fleets, enemy_fleets = init_fleets(pw)
- dont_attack = set()
- didnt_attack = list()
- max_growth_rate = 0
- if enemy_planets:
- max_growth_rate = max(enemy_planets, key=lambda x: x.growth_rate)
- max_growth_rate = max_growth_rate.growth_rate
- if my_fleets:
- for i in my_fleets:
- if pw.get_planet(i.dest).owner == 0:
- dont_attack.add(i.dest)
- for i in my_planets:
- worth_list = list()
- if neutral_planets:
- for j in neutral_planets:
- if j.growth_rate >= 2:
- worth_list.append((calculate_worth_neutral(pw, i, j), j, j.num_ships, j.planet_id))
- if enemy_planets:
- for j in enemy_planets:
- if j.growth_rate >= 2 or max_growth_rate < 2:
- worth, num_ships = calculate_worth_enemy(pw, i, j, my_fleets, enemy_fleets)
- if worth != 0.0 or max_growth_rate == 0:
- worth_list.append((worth * 2, j, num_ships))
- for j in my_planets:
- if i != j:
- worth, num_ships = calculate_worth_friendly(pw, i, j, my_fleets, enemy_fleets)
- if worth != 0.0:
- worth_list.append((worth * 2, j, num_ships))
- worth_list.sort(key=lambda x: x[0], reverse=True)
- for index, j in enumerate(worth_list):
- if index > len(worth_list) / 4:
- break
- if j[0] != 0.0 and i.num_ships > j[2] and j[1].planet_id not in dont_attack:
- pw.issue_order(i.planet, j[1].planet, j[2] + 1)
- i.num_ships -= j[2] + 1
- if j[1].owner == 0:
- dont_attack.add(j[1].planet_id)
- elif max_growth_rate == 0 and i.num_ships > j[2] and j[1].planet_id not in dont_attack:
- pw.issue_order(i.planet, j[1].planet, j[2] + 1)
- i.num_ships -= j[2] + 1
- else:
- didnt_attack.append(i)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement