From 94755002835bfdf9b4990b797b8ead3acacaad71 Mon Sep 17 00:00:00 2001 From: SpitfireX Date: Tue, 11 Aug 2015 03:50:09 +0200 Subject: Fixed the jerky target selection The cell now locks onto a target and tries to reach it. This target can either be a source of food or a random point (in case no food is around). The locked target only gets reset when the cell has to flee. --- main.py | 59 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 7bc1d1e..583581b 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,10 @@ from interval_utils import * sub = DummySubscriber() c = client.Client(sub) stats = stats.Stats() +target = (0,0) +has_target = False +target_cell = None +color = (0,0,0) # find out server and token to connect try: @@ -42,7 +46,7 @@ while True: gui.draw_frame() if len(list(c.player.own_cells)) > 0: - runaway=False + runaway = False my_smallest = min(map(lambda cell : cell.mass, c.player.own_cells)) my_largest = max(map(lambda cell : cell.mass, c.player.own_cells)) @@ -52,11 +56,11 @@ while True: relpos = ((cell.pos[0]-c.player.center[0]),(cell.pos[1]-c.player.center[1])) dist = math.sqrt(relpos[0]**2+relpos[1]**2) - if (not cell.is_virus and dist < 300+2*cell.size and cell.mass > 1.1 * my_smallest) or (cell.is_virus and dist < cell.size and cell.mass < my_largest): + if (not cell.is_virus and dist < 300+2*cell.size and cell.mass > 1.25 * my_smallest) or (cell.is_virus and dist < cell.size and cell.mass < my_largest): angle = math.atan2(relpos[1],relpos[0]) corridor_width = 2 * math.asin(cell.size / dist) forbidden_intervals += canonicalize_angle_interval((angle-corridor_width, angle+corridor_width)) - runaway=True + runaway = True if c.player.center[0] < c.world.top_left[1]+c.player.total_size: forbidden_intervals += [(0.5*pi, 1.5*pi)] @@ -79,25 +83,44 @@ while True: runaway_angle = (a+b)/2 runaway_x, runaway_y = (c.player.center[0]+int(100*math.cos(runaway_angle))), (c.player.center[1]+int(100*math.sin(runaway_angle))) - c.send_target(runaway_x, runaway_y) + target = (runaway_x, runaway_y) + has_target = False + + color = (255,0,0) print ("Running away: " + str((runaway_x-c.player.center[0], runaway_y-c.player.center[1]))) - gui.draw_line(c.player.center, (runaway_x,runaway_y),(255,0,0)) else: - food = list(filter(lambda x: x.is_food or x.mass <= sorted(c.player.own_cells, key = lambda x: x.mass)[0].mass * 0.75 and not x.is_virus, c.world.cells.values())) - def dist(cell): return math.sqrt((cell.pos[0]-c.player.center[0])**2 + (cell.pos[1]-c.player.center[1])**2) - food = sorted(food, key = dist) + if target_cell != None: + if target_cell not in c.world.cells.values(): + target_cell = None + has_target = False + print("target_cell does not exist any more") + elif target == tuple(c.player.center): + has_target = False + print("Reached random destination") - if len(food) > 0: - c.send_target(food[0].pos[0], food[0].pos[1]) - gui.draw_line(c.player.center, food[0].pos,(0,0,255)) - print("Found food at: " + str(food[0].pos)) - else: - rx = c.player.center[0] + random.randrange(-400, 401) - ry = c.player.center[1] + random.randrange(-400, 401) - c.send_target(rx, ry) - gui.draw_line(c.player.center, (rx, ry),(0,255,0)) - print("Nothing to do, heading to random destination: " + str((rx, ry))) + if not has_target: + def eatable(cell): return (cell.is_food) or (cell.mass <= sorted(c.player.own_cells, key = lambda x: x.mass)[0].mass * 0.75) and not (cell.is_virus) + food = list(filter(eatable, c.world.cells.values())) + + def dist(cell): return math.sqrt((cell.pos[0]-c.player.center[0])**2 + (cell.pos[1]-c.player.center[1])**2) + food = sorted(food, key = dist) + + if len(food) > 0: + target = (food[0].pos[0], food[0].pos[1]) + target_cell = food[0] + has_target = True + color = (0,0,255) + print("Found food at: " + str(food[0].pos)) + else: + rx = c.player.center[0] + random.randrange(-400, 401) + ry = c.player.center[1] + random.randrange(-400, 401) + target = (rx, ry) + has_target = True + color = (0,255,0) + print("Nothing to do, heading to random targetination: " + str((rx, ry))) + c.send_target(target[0], target[1]) + gui.draw_line(c.player.center, target, color) gui.update() stats.log_pos(c.player.center) stats.log_mass(c.player.total_mass) -- cgit v1.2.1