Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. #ShnizelBatata
  2.  
  3. class planet:
  4.  
  5. def __init__(self, p):
  6. self.num_ships = p.num_ships()
  7. self.planet_id = p.planet_id()
  8. self.growth_rate = p.growth_rate()
  9. self.owner = p.owner()
  10. self.location = (p.x(), p.y())
  11. self.planet = p
  12.  
  13.  
  14. class fleet:
  15.  
  16. def __init__(self, f):
  17. self.owner = f.owner()
  18. self.num_ships = f.num_ships()
  19. self.src = f.source_planet()
  20. self.dest = f.destination_planet()
  21. self.turns_remaining = f.turns_remaining()
  22. self.trip_length = f.total_trip_length()
  23.  
  24.  
  25. def init_planets(pw):
  26. friendly = list()
  27. enemy = list()
  28. neutral = list()
  29. if pw.my_planets():
  30. for i in pw.my_planets():
  31. friendly.append(planet(i))
  32.  
  33. if pw.enemy_planets():
  34. for i in pw.enemy_planets():
  35. enemy.append(planet(i))
  36.  
  37. if pw.neutral_planets():
  38. for i in pw.neutral_planets():
  39. neutral.append(planet(i))
  40.  
  41. return (
  42. friendly, enemy, neutral)
  43.  
  44.  
  45. def init_fleets(pw):
  46. friendly = list()
  47. enemy = list()
  48. if pw.my_fleets():
  49. for i in pw.my_fleets():
  50. friendly.append(fleet(i))
  51.  
  52. if pw.enemy_fleets():
  53. for i in pw.enemy_fleets():
  54. enemy.append(fleet(i))
  55.  
  56. return (
  57. friendly, enemy)
  58.  
  59.  
  60. def calculate_worth_neutral(pw, src, dest):
  61. if dest.num_ships != 0:
  62. return float(dest.growth_rate) / (dest.num_ships ** 1.5 * pw.distance(src.planet, dest.planet) ** 3)
  63. return float(dest.growth_rate) / pw.distance(src.planet, dest.planet) ** 3
  64.  
  65.  
  66. def calculate_worth_enemy(pw, src, dest, my_fleets, enemy_fleets):
  67. dist = pw.distance(src.planet, dest.planet)
  68. ships = dest.num_ships + dest.growth_rate * dist
  69. if my_fleets:
  70. for i in my_fleets:
  71. if i.dest == dest.planet_id and i.turns_remaining <= dist:
  72. ships -= i.num_ships
  73.  
  74. if enemy_fleets:
  75. for i in enemy_fleets:
  76. if i.dest == dest.planet_id and i.turns_remaining <= dist:
  77. ships += i.num_ships
  78.  
  79. if dest.growth_rate != 0 and ships > 0:
  80. return (float(dest.growth_rate) / (ships ** 1.5 * pw.distance(src.planet, dest.planet) ** 2.5), ships)
  81. return (0.0, 0)
  82.  
  83.  
  84. def calculate_worth_friendly(pw, src, dest, my_fleets, enemy_fleets):
  85. dist = pw.distance(src.planet, dest.planet)
  86. ships = dest.num_ships
  87. takeover_turn = 0
  88. last_turn = 0
  89. enemy_fleets.sort(key=lambda x: x.turns_remaining)
  90. if my_fleets:
  91. for i in my_fleets:
  92. if i.dest == dest.planet_id and i.turns_remaining <= dist:
  93. ships += i.num_ships
  94.  
  95. if enemy_fleets:
  96. for i in enemy_fleets:
  97. if i.dest == dest.planet_id and i.turns_remaining <= dist:
  98. ships -= i.num_ships
  99. if i.turns_remaining > last_turn and takeover_turn == 0:
  100. ships += (i.turns_remaining - last_turn) * dest.growth_rate
  101. if ships < 0 and takeover_turn == 0:
  102. takeover_turn = i.turns_remaining
  103.  
  104. if ships < 0:
  105. ships -= dest.growth_rate * (dist - takeover_turn)
  106. ships = -ships
  107. return (
  108. float(dest.growth_rate) / (ships ** 1.5 * dist ** 3), ships)
  109. else:
  110. return (0.0, 0)
  111.  
  112.  
  113. def do_turn(pw):
  114. my_planets, enemy_planets, neutral_planets = init_planets(pw)
  115. my_fleets, enemy_fleets = init_fleets(pw)
  116. dont_attack = set()
  117. didnt_attack = list()
  118. max_growth_rate = 0
  119. if enemy_planets:
  120. max_growth_rate = max(enemy_planets, key=lambda x: x.growth_rate)
  121. max_growth_rate = max_growth_rate.growth_rate
  122. if my_fleets:
  123. for i in my_fleets:
  124. if pw.get_planet(i.dest).owner == 0:
  125. dont_attack.add(i.dest)
  126.  
  127. for i in my_planets:
  128. worth_list = list()
  129. if neutral_planets:
  130. for j in neutral_planets:
  131. if j.growth_rate >= 2:
  132. worth_list.append((calculate_worth_neutral(pw, i, j), j, j.num_ships, j.planet_id))
  133.  
  134. if enemy_planets:
  135. for j in enemy_planets:
  136. if j.growth_rate >= 2 or max_growth_rate < 2:
  137. worth, num_ships = calculate_worth_enemy(pw, i, j, my_fleets, enemy_fleets)
  138. if worth != 0.0 or max_growth_rate == 0:
  139. worth_list.append((worth * 2, j, num_ships))
  140.  
  141. for j in my_planets:
  142. if i != j:
  143. worth, num_ships = calculate_worth_friendly(pw, i, j, my_fleets, enemy_fleets)
  144. if worth != 0.0:
  145. worth_list.append((worth * 2, j, num_ships))
  146.  
  147. worth_list.sort(key=lambda x: x[0], reverse=True)
  148. for index, j in enumerate(worth_list):
  149. if index > len(worth_list) / 4:
  150. break
  151. if j[0] != 0.0 and i.num_ships > j[2] and j[1].planet_id not in dont_attack:
  152. pw.issue_order(i.planet, j[1].planet, j[2] + 1)
  153. i.num_ships -= j[2] + 1
  154. if j[1].owner == 0:
  155. dont_attack.add(j[1].planet_id)
  156. elif max_growth_rate == 0 and i.num_ships > j[2] and j[1].planet_id not in dont_attack:
  157. pw.issue_order(i.planet, j[1].planet, j[2] + 1)
  158. i.num_ships -= j[2] + 1
  159. else:
  160. didnt_attack.append(i)
  161. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement