Advertisement
Guest User

Skillz 2019 17.1 16:16

a guest
Jan 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.42 KB | None | 0 0
  1. from elf_kingdom import *
  2. import random
  3.  
  4. def do_turn(game):
  5.     handle_elves(game)
  6.     handle_portals(game)
  7.  
  8. def elf_attack_near_castle(game, elf, elf_state):
  9.     my_castle = game.get_my_castle()
  10.     entities = game.get_enemy_living_elves() + game.get_enemy_lava_giants()
  11.    
  12.     for entity in entities:
  13.         if entity.in_range(my_castle, 1000) and elf_state == "":
  14.             if elf.in_attack_range(entity):
  15.                 elf.attack(entity)
  16.                 elf_state = "attack"
  17.                 break
  18.             else:
  19.                 elf.move_to(entity)
  20.                 elf_state = "move"
  21.                 break
  22.     return elf_state
  23.  
  24.  
  25. def elf_attack_near_castle_portals(game, elf, elf_state):
  26.     my_castle = game.get_my_castle()
  27.     enemy_portals = game.get_enemy_portals()
  28.     closest_portal = -1
  29.    
  30.     for i, portal in enumerate(enemy_portals):
  31.         if elf.in_attack_range(portal) and elf_state == "":
  32.             elf.attack(portal)
  33.             elf_state = "attack"
  34.         elif portal.in_range(my_castle, 2000):
  35.             if closest_portal > -1 and closest_portal < len(enemy_portals):
  36.                 if elf.distance(enemy_portals[i]) < elf.distance(enemy_portals[closest_portal]):
  37.                     closest_portal = i
  38.             else:
  39.                 closest_portal = i
  40.     if closest_portal > -1 and closest_portal < len(enemy_portals) and elf_state == "":
  41.         elf.move_to(enemy_portals[closest_portal])
  42.         elf_state = "move"
  43.     return elf_state
  44.  
  45. def elf_attack_nearby_portals(game, elf, elf_state):
  46.     enemy_portals = game.get_enemy_portals()
  47.     closest_portal = -1
  48.    
  49.     for i, portal in enumerate(enemy_portals):
  50.         if elf.in_attack_range(portal) and elf_state == "":
  51.             elf.attack(portal)
  52.             elf_state = "attack"
  53.         elif elf.in_range(portal, 2000):
  54.             if closest_portal > -1 and closest_portal < len(enemy_portals):
  55.                 if elf.distance(enemy_portals[i]) < elf.distance(enemy_portals[closest_portal]):
  56.                     closest_portal = i
  57.             else:
  58.                 closest_portal = i
  59.     if closest_portal > -1 and closest_portal < len(enemy_portals) and elf_state == "":
  60.         elf.move_to(enemy_portals[closest_portal])
  61.         elf_state = "move"
  62.     return elf_state
  63.  
  64. def elf_attack_near_castle_elves(game, elf, elf_state):
  65.     my_castle = game.get_my_castle()
  66.     entities = game.get_enemy_living_elves()
  67.    
  68.     for entity in entities:
  69.         if elf_state == "":
  70.             if entity.in_range(my_castle, 3000):
  71.                 if elf.in_attack_range(entity):
  72.                     elf.attack(entity)
  73.                     elf_state = "attack"
  74.                 else:
  75.                     elf.move_to(entity)
  76.                     elf_state = "move"
  77.         else:
  78.             break
  79.     return elf_state
  80.  
  81. def elf_run_away_nearby_mobs(game, elf, elf_state):
  82.     my_castle = game.get_my_castle()
  83.     entities = game.get_enemy_ice_trolls() + game.get_enemy_living_elves()
  84.    
  85.     for entity in entities:
  86.         if elf_state == "":
  87.             if elf.in_range(entity, 500):
  88.                 if not entity.in_range(my_castle, entity.attack_range + 50):
  89.                     new_loc = elf.get_location().towards(entity, -1000)
  90.                     if new_loc.in_map():
  91.                         elf.move_to(new_loc)
  92.                         elf_state = "move"
  93.                     # else:
  94.                     #     if elf.in_attack_range(entity):
  95.                     #         elf.attack(entity)
  96.                     #         elf_state = "attack"
  97.                     #     else:
  98.                     #         elf.move_to(entity)
  99.                     #         elf_state = "move"
  100.         else:
  101.             break
  102.     return elf_state
  103.  
  104. def handle_elves(game):
  105.     enemy_castle = game.get_enemy_castle()
  106.     my_castle = game.get_my_castle()
  107.    
  108.     for elf in game.get_my_living_elves():
  109.         elf_state = ""
  110.        
  111.         if elf_state == "":
  112.             if elf.can_build_portal():
  113.                 elf.build_portal()
  114.                 elf_state = "build"
  115.             # elif elf.in_attack_range(enemy_castle):
  116.             #     elf.attack(enemy_castle)
  117.             #     elf_state = "attack"
  118.             else:
  119.                 elf_state = elf_attack_near_castle_portals(game, elf, elf_state)
  120.                 elf_state = elf_attack_near_castle_elves(game, elf, elf_state)
  121.                 elf_state = elf_run_away_nearby_mobs(game, elf, elf_state)
  122.                 elf_state = elf_attack_nearby_portals(game, elf, elf_state)
  123.                 elf_state = elf_attack_near_castle(game, elf, elf_state)
  124.                
  125.                 if elf_state == "":
  126.                     new_loc = elf.get_location().towards(enemy_castle, elf.distance(enemy_castle) - 2000)
  127.                     elf.move_to(new_loc)
  128.                     elf_state = "move"
  129.            
  130.             for portal in game.get_my_portals():
  131.                 if elf_state == "":
  132.                     if portal.current_health <= game.portal_max_health * 0.7:
  133.                         if portal.in_range(my_castle, 2000) and elf.in_range(portal, 2000) and elf.can_build_portal():
  134.                             elf.build_portal()
  135.                         elif elf.in_range(portal, 500) and elf.can_build_portal():
  136.                             elf.build_portal()
  137.        
  138. def get_closest_portal_to_castle(game, castle):
  139.     portals = game.get_my_portals()
  140.     shortest_distance = 999999
  141.     closest_portal = -1
  142.    
  143.     for i, portal in enumerate(portals):
  144.         if portal.distance(castle) < shortest_distance:
  145.             shortest_distance = portal.distance(castle)
  146.             closest_portal = i
  147.            
  148.     return portals[closest_portal]
  149.            
  150. def summon_creatures(game, portal, is_closest=False):
  151.     my_castle = game.get_my_castle()
  152.     nearby_mobs = 0
  153.     nearby_my_ice_trolls = 0
  154.     to_spawn = random.randint(1, int((nearby_mobs ** -1) * 10)) if nearby_mobs > 0 else random.randint(1, 3)
  155.    
  156.     for entity in game.get_enemy_creatures() + game.get_enemy_living_elves():
  157.         if portal.in_range(entity, 3000):
  158.             nearby_mobs += 1
  159.    
  160.     for entity in game.get_my_ice_trolls():
  161.         if portal.in_range(entity, 1000):
  162.             nearby_my_ice_trolls += 1
  163.            
  164.     print to_spawn, portal, nearby_mobs >= 1, portal.in_range(my_castle, 1500), portal.can_summon_ice_troll()
  165.     if to_spawn == 1 or is_closest:
  166.         if nearby_mobs >= 1 and nearby_my_ice_trolls < 6 and portal.in_range(my_castle, 1500) and portal.can_summon_ice_troll():
  167.             portal.summon_ice_troll()
  168.         elif not portal.in_range(my_castle, 1500) and portal.can_summon_lava_giant():
  169.             portal.summon_lava_giant()
  170.             print 'portal ', portal, ' summons an ice troll'
  171.    
  172.  
  173. def handle_portals(game):
  174.     portals = game.get_my_portals()
  175.     my_castle = game.get_my_castle()
  176.     enemy_castle = game.get_enemy_castle()
  177.    
  178.     if len(portals) > 1:
  179.         closest_portal_to_my_castle = get_closest_portal_to_castle(game, my_castle)
  180.         closest_portal_to_enemy_castle = get_closest_portal_to_castle(game, enemy_castle)
  181.        
  182.         summon_creatures(game, closest_portal_to_my_castle, True)
  183.         summon_creatures(game, closest_portal_to_enemy_castle, True)
  184.        
  185.         for portal in portals:
  186.             if portal != closest_portal_to_enemy_castle and portal != closest_portal_to_my_castle:
  187.                 summon_creatures(game, portal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement