Advertisement
Guest User

Untitled

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