diff options
Diffstat (limited to 'strategy.py')
-rw-r--r-- | strategy.py | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/strategy.py b/strategy.py index 1f1249f..5b8ca5a 100644 --- a/strategy.py +++ b/strategy.py @@ -4,23 +4,33 @@ import gui import random class Strategy: - def __init__(self): + def __init__(self, c): self.target = (0,0) self.has_target = False self.target_cell = None self.color = (0,0,0) - - def process_frame(self,c): + self.c = c + + def dist(self, cell): + return math.sqrt((cell.pos[0]-self.c.player.center[0])**2 + (cell.pos[1]-self.c.player.center[1])**2) + + def edible(self, cell): + return (cell.is_food) or (cell.mass <= sorted(self.c.player.own_cells, key = lambda x: x.mass)[0].mass * 0.75) and not (cell.is_virus) + + def weight_cell(self, cell): + pass + + def process_frame(self): 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)) + my_smallest = min(map(lambda cell : cell.mass, self.c.player.own_cells)) + my_largest = max(map(lambda cell : cell.mass, self.c.player.own_cells)) # enemy/virus avoidance forbidden_intervals = [] - for cell in c.world.cells.values(): - relpos = ((cell.pos[0]-c.player.center[0]),(cell.pos[1]-c.player.center[1])) + for cell in self.c.world.cells.values(): + relpos = ((cell.pos[0]-self.c.player.center[0]),(cell.pos[1]-self.c.player.center[1])) dist = math.sqrt(relpos[0]**2+relpos[1]**2) if (not cell.is_virus and dist < ((500+2*cell.size) if cell.mass > 1.25*my_smallest*2 else (300+cell.size)) and cell.mass > 1.25 * my_smallest) or (cell.is_virus and dist < my_largest and cell.mass < my_largest): @@ -30,13 +40,13 @@ class Strategy: runaway = True # wall avoidance - if c.player.center[0] < c.world.top_left[1]+(c.player.total_size*2): + if self.c.player.center[0] < self.c.world.top_left[1]+(self.c.player.total_size*2): forbidden_intervals += [(0.5*pi, 1.5*pi)] - if c.player.center[0] > c.world.bottom_right[1]-(c.player.total_size*2): + if self.c.player.center[0] > self.c.world.bottom_right[1]-(self.c.player.total_size*2): forbidden_intervals += [(0,0.5*pi), (1.5*pi, 2*pi)] - if c.player.center[1] < c.world.top_left[0]+(c.player.total_size*2): + if self.c.player.center[1] < self.c.world.top_left[0]+(self.c.player.total_size*2): forbidden_intervals += [(pi, 2*pi)] - if c.player.center[1] > c.world.bottom_right[0]-(c.player.total_size*2): + if self.c.player.center[1] > self.c.world.bottom_right[0]-(self.c.player.total_size*2): forbidden_intervals += [(0, pi)] # if there's actually an enemy to avoid: @@ -49,25 +59,24 @@ class Strategy: (a,b) = find_largest_angle_interval(allowed_intervals) 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))) + runaway_x, runaway_y = (self.c.player.center[0]+int(100*math.cos(runaway_angle))), (self.c.player.center[1]+int(100*math.sin(runaway_angle))) self.target = (runaway_x, runaway_y) self.has_target = False self.target_cell = None self.color = (255,0,0) - print ("Running away: " + str((runaway_x-c.player.center[0], runaway_y-c.player.center[1]))) + print ("Running away: " + str((runaway_x-self.c.player.center[0], runaway_y-self.c.player.center[1]))) # a bit of debugging information for i in forbidden_intervals: - gui.draw_arc(c.player.center, c.player.total_size+10, i, (255,0,255)) + gui.draw_arc(self.c.player.center, self.c.player.total_size+10, i, (255,0,255)) # if however there's no enemy to avoid, chase food or jizz randomly around else: - def edible(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) def rival(cell, food): if cell.is_virus or cell.is_food: return False - if cell.cid in c.player.own_ids: return False + if cell.cid in self.c.player.own_ids: return False if cell.mass < 1.25*my_smallest: return food.is_food or cell.size > 1.25*food.size @@ -81,25 +90,25 @@ class Strategy: if self.target_cell != None: self.target = tuple(self.target_cell.pos) - if self.target_cell not in c.world.cells.values() or not edible(self.target_cell): + if self.target_cell not in self.c.world.cells.values() or not self.edible(self.target_cell): self.target_cell = None self.has_target = False print("target_cell does not exist any more") - elif self.target == tuple(c.player.center): + elif self.target == tuple(self.c.player.center): self.has_target = False print("Reached random destination") if not self.has_target: - food = list(filter(edible, c.world.cells.values())) + food = list(filter(self.edible, self.c.world.cells.values())) def quality(cell): - dd_sq = max((cell.pos[0]-c.player.center[0])**2 + (cell.pos[1]-c.player.center[1])**2,0.001) + dd_sq = max((cell.pos[0]-self.c.player.center[0])**2 + (cell.pos[1]-self.c.player.center[1])**2,0.001) sigma = 500 dist_score = -math.exp(-dd_sq/(2*sigma**2)) - rivals = filter(lambda r : rival(r,cell), c.world.cells.values()) - splitkillers = filter(splitkiller, c.world.cells.values()) - nonsplitkillers = filter(nonsplitkiller, c.world.cells.values()) + rivals = filter(lambda r : rival(r,cell), self.c.world.cells.values()) + splitkillers = filter(splitkiller, self.c.world.cells.values()) + nonsplitkillers = filter(nonsplitkiller, self.c.world.cells.values()) rival_score = 0 for r in rivals: @@ -121,12 +130,12 @@ class Strategy: density_score = 0 sigma = 300 - for f in filter(lambda c : c.is_food and c!=cell, c.world.cells.values()): + for f in filter(lambda c : c.is_food and c!=cell, self.c.world.cells.values()): dd_sq = (f.pos[0]-cell.pos[0])**2 + (f.pos[1]-cell.pos[1])**2 density_score -= math.exp(-dd_sq/(2*sigma**2)) wall_score = 0 - wall_dist = min( cell.pos[0]-c.world.top_left[1], c.world.bottom_right[1]-cell.pos[0], cell.pos[1]-c.world.top_left[0], c.world.bottom_right[0]-cell.pos[1] ) + wall_dist = min( cell.pos[0]-self.c.world.top_left[1], self.c.world.bottom_right[1]-cell.pos[0], cell.pos[1]-self.c.world.top_left[0], self.c.world.bottom_right[0]-cell.pos[1] ) sigma = 100 wall_score = math.exp(-wall_dist**2/(2*sigma**2)) @@ -143,8 +152,8 @@ class Strategy: self.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) + rx = self.c.player.center[0] + random.randrange(-400, 401) + ry = self.c.player.center[1] + random.randrange(-400, 401) self.target = (rx, ry) self.has_target = True self.color = (0,255,0) @@ -152,6 +161,6 @@ class Strategy: # more debugging - gui.draw_line(c.player.center, self.target, self.color) + gui.draw_line(self.c.player.center, self.target, self.color) return self.target |