Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from elf_kingdom import *
- import random
- def do_turn(game):
- handle_elves(game)
- handle_portals(game)
- def elf_attack_near_castle(game, elf, elf_state):
- my_castle = game.get_my_castle()
- entities = game.get_enemy_living_elves() + game.get_enemy_lava_giants()
- for entity in entities:
- if entity.in_range(my_castle, 1000) and elf_state == "":
- if elf.in_attack_range(entity):
- elf.attack(entity)
- elf_state = "attack"
- break
- else:
- elf.move_to(entity)
- elf_state = "move"
- break
- return elf_state
- def elf_attack_near_castle_portals(game, elf, elf_state):
- my_castle = game.get_my_castle()
- enemy_portals = game.get_enemy_portals()
- closest_portal = -1
- for i, portal in enumerate(enemy_portals):
- if elf.in_attack_range(portal) and elf_state == "":
- elf.attack(portal)
- elf_state = "attack"
- elif portal.in_range(my_castle, 2000):
- if closest_portal > -1 and closest_portal < len(enemy_portals):
- if elf.distance(enemy_portals[i]) < elf.distance(enemy_portals[closest_portal]):
- closest_portal = i
- else:
- closest_portal = i
- if closest_portal > -1 and closest_portal < len(enemy_portals) and elf_state == "":
- elf.move_to(enemy_portals[closest_portal])
- elf_state = "move"
- return elf_state
- def elf_attack_nearby_portals(game, elf, elf_state):
- enemy_portals = game.get_enemy_portals()
- closest_portal = -1
- for i, portal in enumerate(enemy_portals):
- if elf.in_attack_range(portal) and elf_state == "":
- elf.attack(portal)
- elf_state = "attack"
- elif elf.in_range(portal, 2000):
- if closest_portal > -1 and closest_portal < len(enemy_portals):
- if elf.distance(enemy_portals[i]) < elf.distance(enemy_portals[closest_portal]):
- closest_portal = i
- else:
- closest_portal = i
- if closest_portal > -1 and closest_portal < len(enemy_portals) and elf_state == "":
- elf.move_to(enemy_portals[closest_portal])
- elf_state = "move"
- return elf_state
- def elf_attack_near_castle_elves(game, elf, elf_state):
- my_castle = game.get_my_castle()
- entities = game.get_enemy_living_elves()
- for entity in entities:
- if elf_state == "":
- if entity.in_range(my_castle, 3000):
- if elf.in_attack_range(entity):
- elf.attack(entity)
- elf_state = "attack"
- else:
- elf.move_to(entity)
- elf_state = "move"
- else:
- break
- return elf_state
- def elf_run_away_nearby_mobs(game, elf, elf_state):
- my_castle = game.get_my_castle()
- entities = game.get_enemy_ice_trolls() + game.get_enemy_living_elves()
- for entity in entities:
- if elf_state == "":
- if elf.in_range(entity, 500):
- if not entity.in_range(my_castle, entity.attack_range + 50):
- new_loc = elf.get_location().towards(entity, -1000)
- if new_loc.in_map():
- elf.move_to(new_loc)
- elf_state = "move"
- # else:
- # if elf.in_attack_range(entity):
- # elf.attack(entity)
- # elf_state = "attack"
- # else:
- # elf.move_to(entity)
- # elf_state = "move"
- else:
- break
- return elf_state
- def handle_elves(game):
- enemy_castle = game.get_enemy_castle()
- my_castle = game.get_my_castle()
- for elf in game.get_my_living_elves():
- elf_state = ""
- if elf_state == "":
- if elf.can_build_portal():
- elf.build_portal()
- elf_state = "build"
- # elif elf.in_attack_range(enemy_castle):
- # elf.attack(enemy_castle)
- # elf_state = "attack"
- else:
- elf_state = elf_attack_near_castle_portals(game, elf, elf_state)
- elf_state = elf_attack_near_castle_elves(game, elf, elf_state)
- elf_state = elf_run_away_nearby_mobs(game, elf, elf_state)
- elf_state = elf_attack_nearby_portals(game, elf, elf_state)
- elf_state = elf_attack_near_castle(game, elf, elf_state)
- if elf_state == "":
- new_loc = elf.get_location().towards(enemy_castle, elf.distance(enemy_castle) - 2000)
- elf.move_to(new_loc)
- elf_state = "move"
- for portal in game.get_my_portals():
- if elf_state == "":
- if portal.current_health <= game.portal_max_health * 0.7:
- if portal.in_range(my_castle, 2000) and elf.in_range(portal, 2000) and elf.can_build_portal():
- elf.build_portal()
- elif elf.in_range(portal, 500) and elf.can_build_portal():
- elf.build_portal()
- def get_closest_portal_to_castle(game, castle):
- portals = game.get_my_portals()
- shortest_distance = 999999
- closest_portal = -1
- for i, portal in enumerate(portals):
- if portal.distance(castle) < shortest_distance:
- shortest_distance = portal.distance(castle)
- closest_portal = i
- return portals[closest_portal]
- def summon_creatures(game, portal, is_closest=False):
- my_castle = game.get_my_castle()
- nearby_mobs = 0
- nearby_my_ice_trolls = 0
- to_spawn = random.randint(1, int((nearby_mobs ** -1) * 10)) if nearby_mobs > 0 else random.randint(1, 3)
- for entity in game.get_enemy_creatures() + game.get_enemy_living_elves():
- if portal.in_range(entity, 3000):
- nearby_mobs += 1
- for entity in game.get_my_ice_trolls():
- if portal.in_range(entity, 1000):
- nearby_my_ice_trolls += 1
- print to_spawn, portal, nearby_mobs >= 1, portal.in_range(my_castle, 1500), portal.can_summon_ice_troll()
- if to_spawn == 1 or is_closest:
- if nearby_mobs >= 1 and nearby_my_ice_trolls < 6 and portal.in_range(my_castle, 1500) and portal.can_summon_ice_troll():
- portal.summon_ice_troll()
- elif not portal.in_range(my_castle, 1500) and portal.can_summon_lava_giant():
- portal.summon_lava_giant()
- print 'portal ', portal, ' summons an ice troll'
- def handle_portals(game):
- portals = game.get_my_portals()
- my_castle = game.get_my_castle()
- enemy_castle = game.get_enemy_castle()
- if len(portals) > 1:
- closest_portal_to_my_castle = get_closest_portal_to_castle(game, my_castle)
- closest_portal_to_enemy_castle = get_closest_portal_to_castle(game, enemy_castle)
- summon_creatures(game, closest_portal_to_my_castle, True)
- summon_creatures(game, closest_portal_to_enemy_castle, True)
- for portal in portals:
- if portal != closest_portal_to_enemy_castle and portal != closest_portal_to_my_castle:
- summon_creatures(game, portal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement