Robot Rumble ALPHA
discord
try it!
boards
tutorial
docs
login
/
signup
import math buddy_dist = 3 health_threshold = 1 #1 | 2 #--|-- #3 | 4 def get_quadrant(x,y): size = 19 if (0 < x and x <= math.floor(size/2)): if (0 < y and y <= math.floor(size/2)): return 1 else: return 3 else: if (0 < y and y <= math.floor(size/2)): return 2 else: return 4 def go_around(state, unit, direction): #found = state.obj_by_coords(Coords(unit.coords.x + direction.to_coords.x, unit.coords.y + direction.to_coords.y)) # dumb function, optimize later # if going in a certain direction, keep trying until an unobstructed cardinal direction is found, if none found, attack an enemy attack_dir = None if (direction.to_coords.y == 1): # going south found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y+1)) print(f"unit {unit.coords} looking {direction} at {Coords(unit.coords.x, unit.coords.y + 1)}, which has {found}") if (found is None): # empty space, so free to move print(f"{unit.coords} moving south") return Action.move(direction) else: print(f"{unit.coords} south blocked, trying east") # non-empty space, so go around if (found.team == state.other_team): # an enemy, mark it for potential attack attack_dir = direction.South found = state.obj_by_coords(Coords(unit.coords.x+1, unit.coords.y)) # try left if (found is None): print(f"{unit.coords} moving east") return Action.move(direction.East) else: print(f"{unit.coords} east blocked, trying west") if (found.team == state.other_team): attack_dir = direction.East found = state.obj_by_coords(Coords(unit.coords.x-1, unit.coords.y)) if (found is None): print(f"{unit.coords} moving west") return Action.move(direction.West) else: print(f"{unit.coords} west blocked, trying north") if (found.team == state.other_team): attack_dir = direction.West found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y-1)) if (found is None): print(f"{unit.coords} moving north") return Action.move(direction.North) else: print(f"{unit.coords} is trapped") if (found.team == state.other_team): attack_dir = direction.North # we are blocked on all directions if (attack_dir is not None): # so attack if there was an enemy return Action.attack(attack_dir) elif (direction.to_coords.y == -1): found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y-1)) print(f"unit {unit.coords} looking {direction} at {Coords(unit.coords.x, unit.coords.y - 1)}, which has {found}") if (found is None): print(f"unit {unit.coords} moving north") return Action.move(direction) else: print(f"unit {unit.coords} north blocked, trying east") if (found.team == state.other_team): attack_dir = direction.North found = state.obj_by_coords(Coords(unit.coords.x+1, unit.coords.y)) if (found is None): print(f"unit {unit.coords} moving east") return Action.move(direction.East) else: print(f"unit {unit.coords} east blocked, trying west") if (found.team == state.other_team): attack_dir = direction.East found = state.obj_by_coords(Coords(unit.coords.x-1, unit.coords.y)) if (found is None): print(f"unit {unit.coords} moving west") return Action.move(direction.West) else: print(f"unit {unit.coords} west blocked, trying south") if (found.team == state.other_team): attack_dir = direction.West found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y+1)) if (found is None): print(f"unit {unit.coords} moving south") return Action.move(direction.South) else: print(f"unit {unit.coords} is trapped") if (found.team == state.other_team): attack_dir = direction.South if (attack_dir is not None): return Action.attack(attack_dir) elif (direction.to_coords.x == 1): found = state.obj_by_coords(Coords(unit.coords.x+1, unit.coords.y)) print(f"unit {unit.coords} looking {direction} at {Coords(unit.coords.x + 1, unit.coords.y)}, which has {found}") if (found is None): print(f"unit {unit.coords} moving east") return Action.move(direction) else: print(f"unit {unit.coords} east blocked, trying south") if (found.team == state.other_team): attack_dir = direction.East found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y+1)) if (found is None): print(f"unit {unit.coords} moving south") return Action.move(direction.South) else: print(f"unit {unit.coords} south blocked, trying north") if (found.team == state.other_team): attack_dir = direction.South found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y-1)) if (found is None): print(f"unit {unit.coords} moving north") return Action.move(direction.North) else: print(f"unit {unit.coords} north blocked, trying west") if (found.team == state.other_team): attack_dir = direction.North found = state.obj_by_coords(Coords(unit.coords.x-1, unit.coords.y)) if (found is None): print(f"unit {unit.coords} moving west") return Action.move(direction.West) else: print(f"unit {unit.coords} is trapped") if (found.team == state.other_team): attack_dir = direction.West if (attack_dir is not None): return Action.attack(attack_dir) elif (direction.to_coords.x == -1): found = state.obj_by_coords(Coords(unit.coords.x-1, unit.coords.y)) print(f"unit {unit.coords} looking {direction} at {Coords(unit.coords.x - 1, unit.coords.y)}, which has {found}") if (found is None): print(f"unit {unit.coords} moving west") return Action.move(direction) else: print(f"unit {unit.coords} west blocked, trying south") if (found.team == state.other_team): attack_dir = direction.West found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y+1)) if (found is None): print(f"unit {unit.coords} moving south") return Action.move(direction.South) else: print(f"unit {unit.coords} south blocked, trying north") if (found.team == state.other_team): attack_dir = direction.South found = state.obj_by_coords(Coords(unit.coords.x, unit.coords.y-1)) if (found is None): print(f"unit {unit.coords} moving north") return Action.move(direction.North) else: print(f"unit {unit.coords} north blocked, trying east") if (found.team == state.other_team): attack_dir = direction.North found = state.obj_by_coords(Coords(unit.coords.x+1, unit.coords.y)) if (found is None): print(f"unit {unit.coords} moving east") return Action.move(direction.East) else: print(f"unit {unit.coords} is trapped") if (found.team == state.other_team): attack_dir = direction.East if (attack_dir is not None): return Action.attack(attack_dir) def robot(state, unit): global debug posx = unit.coords.x posy = unit.coords.y quad = get_quadrant(posx, posy) # basic variables enemies = state.objs_by_team(state.other_team) quadrant_enemies = [enemy for enemy in enemies if get_quadrant(enemy.coords.x, enemy.coords.y) == quad] allies = state.objs_by_team(state.our_team) # get a list of allies quadrant_allies = [ally for ally in allies if get_quadrant(ally.coords.x, ally.coords.y) == quad] # get a list of allies in the same quadrant if (len(quadrant_allies) >= 2): buddy = min(quadrant_allies, key=lambda e: e.coords.distance_to(unit.coords) if e.coords.distance_to(unit.coords) > 0 and e.health > health_threshold else 100 ) else: buddy = None # get the closest ally that is not this unit, and also has sufficient health debug.log(key="buddy", val=buddy) if (unit.health <= health_threshold): closest_enemy = min(enemies, key=lambda e: e.coords.distance_to(unit.coords)) direction = unit.coords.direction_to(closest_enemy.coords) debug.log(key="option", val="low hp") return go_around(state, unit, direction.opposite) else: if (buddy is not None): # if we have a buddy avg_coords = Coords(round((unit.coords.x + buddy.coords.x)/2),round((unit.coords.y + buddy.coords.y)/2)) closest_enemy = min(enemies, key=lambda e: e.coords.distance_to(avg_coords)) # find the closest enemy to BOTH of the buddies if (unit.coords.distance_to(closest_enemy.coords) == 1 and buddy.coords.distance_to(closest_enemy.coords) > buddy_dist): # if we are touching an enemy and our buddy cannot help us soon direction = unit.coords.direction_to(closest_enemy.coords) debug.log(key="option", val="no buddy nearby") return go_around(state, unit, direction.opposite) # run away from the enemy elif (unit.coords.distance_to(closest_enemy.coords) == 1 and buddy.coords.distance_to(closest_enemy.coords) < buddy_dist): # touching an enemy and our buddy can help direction = unit.coords.direction_to(closest_enemy.coords) debug.log(key="option", val="attacking with buddy") return Action.attack(direction) # attack the enemy debug.log(key="dist", val=unit.coords.distance_to(buddy.coords)) if (unit.coords.distance_to(buddy.coords) < buddy_dist): # buddy is nearby, so attack! direction = unit.coords.direction_to(closest_enemy.coords) debug.log(key="option", val="hunting with buddy") return go_around(state, unit, direction) else: # buddy is far away, so move towards him direction = unit.coords.direction_to(buddy.coords) debug.log(key="option", val="running to buddy") return go_around(state, unit, direction) else: # no buddy, so run away! closest_enemy = min(enemies, key=lambda e: e.coords.distance_to(unit.coords)) direction = unit.coords.direction_to(closest_enemy.coords) debug.log(key="option", val="buddy does not exist") return go_around(state, unit, direction.opposite)
Made with <3 by Anton and Noa
github org