def next_step(self, things, data): zombies = [ thing for thing in things.values() if isinstance(thing, Zombie) ] if zombies: target = closest(self, zombies) if distance(self, target) > self.weapon.max_range: best_move = closest(target, adjacent_positions(self)) obstacle = things.get(best_move) if obstacle: if isinstance(obstacle, Player): # zombie not in range. Player blocking path. Heal it. return 'heal', obstacle else: # zombie not in range. Obstacle in front. Shoot it. self.status = u'shooting obstacle to chase target' return 'attack', obstacle else: # zombie not in range. Not obstacle. Move. self.status = u'chasing target' return 'move', best_move else: # zombie in range. Shoot it. self.status = u'shooting target' return 'attack', target else: # no zombies. Heal. self.status = u'no targets, healing' return 'heal', self
def next_step(self, things, t): zombies = [thing for thing in things.values() if isinstance(thing, Zombie)] if zombies: target = closest(self, zombies) if distance(self, target) > self.weapon.max_range: best_move = closest(target, adjacent_positions(self)) obstacle = things.get(best_move) if obstacle: if isinstance(obstacle, Player): # zombie not in range. Player blocking path. Heal it. return 'heal', obstacle else: # zombie not in range. Obstacle in front. Shoot it. self.status = u'shooting obstacle to chase target' return 'attack', obstacle else: # zombie not in range. Not obstacle. Move. self.status = u'chasing target' return 'move', best_move else: # zombie in range. Shoot it. self.status = u'shooting target' return 'attack', target else: # no zombies. Heal. self.status = u'no targets, healing' return 'heal', self
def next_step(self, things): print('Which action?') print('w, a, s, d: movement (up, left down, right, like all games)') print('j: attack closest zombie') print('k: heal self') print('l: heal closest player') if sys.version_info > (3,): action = input() else: action = raw_input() if not action: self.status = 'sitting idle' action = None elif action in 'wasd': deltas = { 'w': (0, -1), 's': (0, 1), 'a': (-1, 0), 'd': (1, 0), } delta = deltas[action] self.status = u'walking' action = 'move' target = (self.position[0] + delta[0], self.position[1] + delta[1]) elif action == 'j': zombies = [thing for thing in things.values() if isinstance(thing, Zombie)] if zombies: self.status = u'shooting closest zombie' action = 'attack' target = closest(self, zombies) else: self.status = u'killing flies, because no zombies left' action = None elif action == 'k': self.status = u'healing self' return 'heal', self elif action == 'l': players = [thing for thing in things.values() if isinstance(thing, Player) and thing is not self] if players: self.status = u'healing closest friend' action = 'heal' target = closest(self, players) else: self.status = u'healing flies, because no players left' action = None else: action = None self.status = u'confused, trying something which is not a valid action' if action: return action, target
def reach_objective(self, things, zombies, players, walls, boxes, data): available = set(self.objectives) for player in players: available.discard(player.position) destination = closest(self, available) return self.go_to(destination, things, zombies, players, walls, boxes, data)
def next_step(self, things, t): if self.life < self.vida: if self.next_move == 'move': action = self.next_move target = tuple(random.choice(emptyPlace(self, things))) self.next_move = 'heal' elif self.next_move == 'heal': action = self.next_move target = self self.vida = self.life self.next_move = 'move' else: self.next_move = 'move' else: target = getClosestZombie(self, things) if (canAttack(self, target)): action = 'attack' else: action = 'move' target = closest(target, possible_moves(self, things)) try: name = target.name except: name = target self.status = str(action) #, str(name)#, self.weapon.max_range self.vida = self.life if action: return action, target
def next_step(self, things): '''Zombies attack if in range, else move in direction of players.''' action = None # possible targets for movement and attack humans = [thing for thing in things.values() if isinstance(thing, Player)] positions = possible_moves(self.position, things) if humans: # targets available target = closest(self, humans) if distance(self.position, target.position) < self.weapon.max_range: # target in range, attack action = 'attack', target else: # target not in range, _try_ to move if positions: by_distance = lambda position: distance(target.position, position) best_position = sorted(positions, key=by_distance)[0] action = 'move', best_position else: # no targets, just wander around if positions: action = 'move', random.choice(positions) return action
def next_step(self, things,t): if self.life < self.vida : if self.next_move == 'move': action = self.next_move target = tuple(random.choice(emptyPlace(self,things))) self.next_move = 'heal' elif self.next_move == 'heal': action = self.next_move target = self self.vida = self.life self.next_move = 'move' else: self.next_move = 'move' else: target = getClosestZombie(self,things) if (canAttack(self,target)): action = 'attack' else: action = 'move' target = closest(target,possible_moves(self,things)) try: name = target.name except: name = target self.status = str(action)#, str(name)#, self.weapon.max_range self.vida = self.life if action: return action, target
def drop_strikes(arr, num, exp): median = (min(arr) + max(arr)) / 2 xu = np.geomspace(median, max(arr), int(num / 2)) xl = min(arr) + median - np.geomspace(min(arr), median, int(num / 2)) x = np.sort(np.hstack([xl, xu[1:]])) x = [utils.closest(arr, i) for i in x] return [i if i in x else np.nan for i in arr], exp
def shoot_zombies(self, zombies): target = closest(self, zombies) in_range = distance(self, target) < self.weapon.max_range if in_range: self.status = u'need_support' return 'attack', target return None, None
def track_zombies(self, things, zombies, players, walls, boxes, data): closest_zombie = closest(self, zombies) if closest_zombie: self.status = u'tracking zombies' return self.go_to(closest_zombie.position, things, zombies, players, walls, boxes, data) return None, None
def get_next_move(self, player, things): if self.start_t is None: self.start_t = S.tick result = None g = self.map current = g[player.position] winner = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) #elif player.life < 40: # result = ('heal', player) else: #print "evaluating", self, self.position moves = utils.possible_moves(player, things) random.shuffle(moves) for pos in moves: #print pos, g[pos], current if g[pos] < current: winner = pos if winner: result = ('move', winner) else: target = closest( player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) # if result is None: # if random.random() < 0.25: # moves = utils.possible_moves(self, things) # if moves: # pos = random.choice(moves) # result = ('move', pos) if result is None: result = ('heal', player) if result[0] in ('attack', 'move'): S.last_action = S.tick if S.tick - S.last_action > self.wait: S.next_strategy = self.next_strategy S.last_action = S.tick if S.tick - self.start_t > self.timeout: S.next_strategy = self.next_strategy S.last_action = S.tick return result
def help_critical_teammates(self, things, players): critical = [ player for player in players if player.life > 0 and player.status == 'critical' ] if critical: closest_critical = closest(self, critical) if distance(self, closest_critical) < core.HEALING_RANGE: self.status = u'heal ' + closest_critical.name return 'heal', closest_critical return None, None
def next_step(self, things): zombies = [thing for thing in things.values() if isinstance(thing, Zombie)] if zombies: self.status = u'shooting stuff' target = closest(self, zombies) return 'attack', target else: self.status = u'waiting for targets' return None
def next_step(self, things, t): zombies = [ thing for thing in things.values() if isinstance(thing, Zombie) ] if zombies: self.status = u'shooting stuff' target = closest(self, zombies) return 'attack', target else: self.status = u'waiting for targets' return None
def alive_players_together(self): '''Are the alive players together (close to each other)?''' alive_players = self.get_alive_players() for player in alive_players: others = [other for other in alive_players if other is not player and other.life > 0] closest_other = closest(player, others) if closest_other: if distance(player.position, closest_other.position) > 2: return False return True
def get_next_move(self, player, things): if self.start_t is None: self.start_t = S.tick result = None g = self.map current = g[player.position] winner = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) #elif player.life < 40: # result = ('heal', player) else: #print "evaluating", self, self.position moves = utils.possible_moves(player, things) random.shuffle(moves) for pos in moves: #print pos, g[pos], current if g[pos] < current: winner = pos if winner: result = ('move', winner) else: target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) # if result is None: # if random.random() < 0.25: # moves = utils.possible_moves(self, things) # if moves: # pos = random.choice(moves) # result = ('move', pos) if result is None: result = ('heal', player) if result[0] in ('attack', 'move'): S.last_action = S.tick if S.tick - S.last_action > self.wait: S.next_strategy = self.next_strategy S.last_action = S.tick if S.tick - self.start_t > self.timeout: S.next_strategy = self.next_strategy S.last_action = S.tick return result
def analogy(w1, w2, w3, n=5, filter_given=True): print('%s : %s :: %s : ???' % (w1, w2, w3)) # w2 - w1 + w3 = w4 closest_ = closest(get_word_vec(w2) - get_word_vec(w1) + get_word_vec(w3)) # optionally filter out given words if filter_given: closest_words = [t for t in closest_ if t[0] not in [w1, w2, w3]] print_tuples(closest_words[:n]) return closest_words
def next_step(self, things, t): """Zombies attack if in range, else move in direction of players.""" action = None # possible targets for movement and attack humans = [ thing for thing in things.values() if isinstance(thing, Player) ] positions = possible_moves(self.position, things) if humans: # targets available target = closest(self, humans) if distance(self.position, target.position) < self.weapon.max_range: # target in range, attack action = 'attack', target else: # target not in range, _try_ to move if positions: # move best_position = closest(target, positions) action = 'move', best_position else: # if blocked by obstacles, try to break them adjacent = sort_by_distance(target, adjacent_positions(self)) for position in adjacent: thing = things.get(position) if isinstance(thing, (Box, Wall)): return 'attack', thing else: # no targets, just wander around if positions: action = 'move', random.choice(positions) return action
def next_step(self, things, t): '''Zombies attack if in range, else move in direction of players.''' action = None # possible targets for movement and attack humans = [thing for thing in things.values() if isinstance(thing, Player)] positions = possible_moves(self.position, things) if humans: # targets available target = closest(self, humans) if distance(self.position, target.position) < self.weapon.max_range: # target in range, attack action = 'attack', target else: # target not in range, _try_ to move if positions: # move best_position = closest(target, positions) action = 'move', best_position else: # if blocked by obstacles, try to break them adyacents = sort_by_distance(target, adyacent_positions(self)) for position in adyacents: thing = things.get(position) if isinstance(thing, (Box, Wall)): return 'attack', thing else: # no targets, just wander around if positions: action = 'move', random.choice(positions) return action
def get_next_move(self, player, things): result = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) else: target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) if result is None: result = ('heal', player) return result, False, "Waiting"
def get_next_move(self, player, things): result = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) else: target = closest( player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) if result is None: result = ('heal', player) return result, False, "Waiting"
def detect_rect(self, gray): """ Detects large rectangular shape on the image :param gray: :return: """ # get corners features = cv2.goodFeaturesToTrack(gray, 500, 0.01, 10) corners = features.squeeze() # get some number of corners closest to corresponding frame corners corner_candidates = list( map( lambda p: closest(corners, p[0], p[1], HoloDetector. NUM_CANDIDATES), ((0, 0), (0, gray.shape[0]), (gray.shape[1], gray.shape[0]), (gray.shape[1], 0)))) # check for rectangularity and get a maximum area rectangle combs = itertools.product(*corner_candidates) max_rect = None max_area = 0 for c1, c2, c3, c4 in combs: angles = [ angle(c1 - c2, c3 - c2), angle(c2 - c3, c4 - c3), angle(c1 - c4, c3 - c4) ] if np.allclose(angles, np.pi / 2, rtol=0.05): area = la.norm(c2 - c1) * la.norm(c3 - c2) if area > max_area: max_rect = [c1, c2, c3, c4] max_area = area if self.debug: self.dbg_images['corners'] = self.cur_img.copy() for c in range(4): # draw candidates if corner_candidates: list( map( lambda p: cv2.circle(self.dbg_images['corners'], tuple(p), 4, (0, 0, 255), 4), corner_candidates[c] [:HoloDetector.NUM_CANDIDATES])) # draw selected rect if max_rect: cv2.circle(self.dbg_images['corners'], tuple(max_rect[c]), 7, (0, 255, 0), 4) return max_rect, max_area
def get_next_move(self, player, things): result = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) else: target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) done = False if not self.location in things: done = True else: if result is None: result = ('attack', things[self.location]) return result, done, "Destroy(%s)" % (self.location,)
def get_next_move(self, player, things): result = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) else: target = closest( player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) done = False if not self.location in things: done = True else: if result is None: result = ('attack', things[self.location]) return result, done, "Destroy(%s)" % (self.location, )
def getClosestZombie(self, things): zombies = [] other = [thing for thing in things.values() if isinstance(thing, Zombie)] zombies = closest(self, other) return zombies
vrep.simx_opmode_oneshot_wait) _, temp = vrep.simxGetObjectPosition(clientID, temp, -1, vrep.simx_opmode_oneshot_wait) vert.insert(0, temp) _, temp = vrep.simxGetObjectHandle(clientID, 'End', vrep.simx_opmode_oneshot_wait) _, temp = vrep.simxGetObjectPosition(clientID, temp, -1, vrep.simx_opmode_oneshot_wait) vert.append(temp) # add edges for start and end vertices for ridge in ridgeVert: ridge[0] += 1 ridge[1] += 1 closest = ut.closest(vert[0], vert, connectStart) for i in range(connectStart): ridgeVert.insert(i, [0, closest[i]]) ridgeGaps.insert(i, None) closest = ut.closest(vert[-1], vert, connectEnd) for i in range(connectEnd): ridgeVert.append([len(vert) - 1, closest[i]]) ridgeGaps.append(None) # use already simulated costs # with open('data\\dense\\ec5.txt', 'rt', encoding='utf-8') as f: # ec = f.readline().split() # ec = [float(i) for i in ec] # evaluate edges using heuristics and find path for experimental evaluation
def set_target_to_kill(self, zombies): if ((self.target_to_kill == None) or (self.target_to_kill.life <= 0)): self.target_to_kill = closest(self, zombies)
def getClosestZombie(self,things): zombies = [] other = [thing for thing in things.values() if isinstance(thing, Zombie)] zombies = closest(self, other) return zombies
def get_next_move(self, player, things): if self.start_tick is None: self.start_tick = S.tick if self.map is None: self.map = GoalDistanceMap(self.goal, things) result = None done = False g = self.map current = g[player.position] winner = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) elif player.life < 50: result = ('heal', player) else: #print "evaluating", self, self.position moves = utils.possible_moves(player, things) random.shuffle(moves) for pos in moves: #print pos, g[pos], current if g[pos] < current: winner = pos if winner: result = ('move', winner) else: target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) # target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) # if target is not None: # if utils.distance(target, player) <= 1.5: # result = ('attack', target) # if result is not None: # moves = utils.possible_moves(player, things) # random.shuffle(moves) # for pos in moves: # #print pos, g[pos], current # if g[pos] < current: # winner = pos # if winner: # result = ('move', winner) # else: # target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) # if target is not None: # if utils.distance(target, player) <= player.weapon.max_range: # result = ('attack', target) # if result is None: # if random.random() < 0.25: # moves = utils.possible_moves(self, things) # if moves: # pos = random.choice(moves) # result = ('move', pos) if result is None: result = ('heal', player) if result[0] in ('attack', 'move'): S.last_action = S.tick if S.tick - S.last_action > self.wait: done = True S.last_action = S.tick if S.tick - self.start_tick > self.timeout: done = True return result, done, "Rush(%s)" % (self.goal,)
vrep.simx_opmode_oneshot_wait) _, temp = vrep.simxGetObjectPosition(clientID, temp, -1, vrep.simx_opmode_oneshot_wait) vertices.insert(0, temp) _, temp = vrep.simxGetObjectHandle(clientID, 'End', vrep.simx_opmode_oneshot_wait) _, temp = vrep.simxGetObjectPosition(clientID, temp, -1, vrep.simx_opmode_oneshot_wait) vertices.append(temp) # add edges for start and end vertices for ridge in edges: ridge[0] += 1 ridge[1] += 1 closest = ut.closest(vertices[0], vertices, connectStart) for i in range(connectStart): edges.insert(i, [0, closest[i]]) gaps.insert(i, None) closest = ut.closest(vertices[-1], vertices, connectEnd) for i in range(connectEnd): edges.append([len(vertices) - 1, closest[i]]) gaps.append(None) # load the paths paths = [] with open('data\\splitting\\maze_heuristika-15-200.txt', 'rt', encoding='utf-8') as f: for i in range(quadsNum):
def __init__(self, raw_data, profile, *, slot_number=0): self._raw_data = raw_data self.profile = profile tag = raw_data.get('tag', {}) extras = tag.get('ExtraAttributes', {}) self.slot_number = slot_number self.stack_size = self._raw_data.get('Count', 1) # Load item name and description self.name = re.sub('§.', '', tag.get('display', {}).get('Name', '')) self.internal_name = extras.get('id', '') self.description = tag.get('display', {}).get('Lore', []) self.description_clean = [re.sub('§.', '', line) for line in self.description] # Load item extra attributes self.hot_potatos = extras.get('hot_potato_count', 0) self.anvil_uses = extras.get('anvil_uses', 0) - self.hot_potatos self.collection_date = extras.get('timestamp', '') # ex: 'timestamp': '2/16/20 9:24 PM' self.runes = extras.get('runes', {}) # ex: 'runes': {'ZOMBIE_SLAYER': 3} self.enchantments = extras.get('enchantments', {}) self.reforge = extras.get('modifier', None) self.recombobulation = False if 'rarity_upgrades' in extras: if extras['rarity_upgrades'] == 1: self.recombobulation = True self.dungeon = 'dungeon_item_level' in extras self.dungeon_level = None if self.dungeon: self.dungeon_level = extras['dungeon_item_level'] # Load item rarity and item from last line of description clean self.rarity = None self.type = None if self.description_clean: last_line = self.description_clean[-1].split() # remove extra 'a' from recombobulated item description last line if self.recombobulation: last_line.pop(0) last_line.pop(-1) self.rarity = last_line[0].lower() if len(last_line) > 1: self.type = last_line[1].lower() if (self.dungeon or last_line[1].lower() == 'dungeon') and len(last_line) > 2: # In case some dungeon item doesnt have 'dungeon_item_level' attribute if not self.dungeon: self.dungeon = True self.dungeon_level = 0 self.type = last_line[2].lower() # Load item from backpacks self.contents = None if self.internal_name == 'NEW_YEAR_CAKE_BAG' or self.internal_name.endswith('_BACKPACK'): for key, value in extras.items(): if key == 'new_year_cake_bag_data' or key.endswith('_backpack_data'): self.contents = decode_inventory_data(value, profile, backpack=True) break # Load item stats stats, reforge_stat, dungeon_bonus = get_stats_from_description(self.description_clean, dungeon=self.dungeon) self.stats = ItemStats(stats, item=self, dungeon=self.dungeon) self.stats.reforge_stat = reforge_stat if self.dungeon: self.stats.dungeon_bonus += self.dungeon_level / 10 # Check and get profile's catacomb dungeon bonus + level based on dungeon bonus from description. # Only when the item is a dungeon item + there's a dungeon bonus from item and profile dungeon level hasn't been set. if self.dungeon and dungeon_bonus > 1.00 and self.profile.dungeon_skill == 0: relative_dungeon_bonus = (dungeon_bonus - (1 + self.dungeon_level / 10)) * 100 total_dungeon_bonus, dungeon_level = closest(ACCUMULATED_CATACOMB_LEVEL_REWARDS['dungeon bonus'], relative_dungeon_bonus) self.profile.dungeon_skill = dungeon_level self.profile.stats.dungeon_bonus += total_dungeon_bonus / 100 self.get_item_stats_extra()
changeSpeedTime = time.time() if (time.time() - radar > 30 + np.random.randint(10)): listCars = utils.checkSpeedPoint(listCars, df) listCars = utils.rewardPoint(listCars) listCars = utils.checkAccidentPoint(listCars, df) listToBlit = utils.toBlit(listCars) radar = time.time() for event in pygame.event.get(): if event.type == pygame.QUIT: crashed = True listCars.record_Start() elif event.type == pygame.MOUSEBUTTONDOWN: x0, x1 = pygame.mouse.get_pos() whichLane, corner = utils.closest(listLanes.get_listLanes(),\ [x0,x1], numberVertices) elif event.type == pygame.MOUSEBUTTONUP: DISPSURF.fill(WHITE) x0, x1 = pygame.mouse.get_pos() newListCar, flatten = listLanes.update(x0,x1, flatten, whichLane, \ corner, listCars.get_listCars(),\ listStartEnd) listCars.update_cars(newListCar) for l in range(len(listCars.get_listCars())): listStart, listCars, listLanes, flatten =\ utils.updatePosition(l, listStart, listCars, \ listLanes, distSecur, \ flatten, [listPassingLine, listPassingLine2,\ listPassingLine3, listPassingLine4],\
def get_next_move(self, player, things): if self.start_tick is None: self.start_tick = S.tick if self.map is None: self.map = GoalDistanceMap(self.goal, things) result = None done = False g = self.map current = g[player.position] winner = None if player.life < 70 and random.random() < 0.3: result = ('heal', player) elif player.life < 50: result = ('heal', player) else: #print "evaluating", self, self.position moves = utils.possible_moves(player, things) random.shuffle(moves) for pos in moves: #print pos, g[pos], current if g[pos] < current: winner = pos if winner: result = ('move', winner) else: target = closest( player, [x for x in things.values() if isinstance(x, Zombie)]) if target is not None: if utils.distance(target, player) <= player.weapon.max_range: result = ('attack', target) # target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) # if target is not None: # if utils.distance(target, player) <= 1.5: # result = ('attack', target) # if result is not None: # moves = utils.possible_moves(player, things) # random.shuffle(moves) # for pos in moves: # #print pos, g[pos], current # if g[pos] < current: # winner = pos # if winner: # result = ('move', winner) # else: # target = closest(player, [x for x in things.values() if isinstance(x, Zombie)]) # if target is not None: # if utils.distance(target, player) <= player.weapon.max_range: # result = ('attack', target) # if result is None: # if random.random() < 0.25: # moves = utils.possible_moves(self, things) # if moves: # pos = random.choice(moves) # result = ('move', pos) if result is None: result = ('heal', player) if result[0] in ('attack', 'move'): S.last_action = S.tick if S.tick - S.last_action > self.wait: done = True S.last_action = S.tick if S.tick - self.start_tick > self.timeout: done = True return result, done, "Rush(%s)" % (self.goal, )
def updateScale(self): self.unit = self.scale * self.pixelRatio self.axisStep = closest(self.scaleRange, 1 / self.scale) self.unitStep = self.unit * self.axisStep
def next_step(self, things, t): print('Which action?') print('w, a, s, d: movement (up, left down, right, like all games)') print('j: attack closest zombie') print('k: heal self') print('l: heal closest player') if sys.version_info > (3, ): action = input() else: action = raw_input() if not action: self.status = 'sitting idle' action = None elif action in 'wasd': deltas = { 'w': (0, -1), 's': (0, 1), 'a': (-1, 0), 'd': (1, 0), } delta = deltas[action] self.status = u'walking' action = 'move' target = (self.position[0] + delta[0], self.position[1] + delta[1]) elif action == 'j': zombies = [ thing for thing in things.values() if isinstance(thing, Zombie) ] if zombies: self.status = u'shooting closest zombie' action = 'attack' target = closest(self, zombies) else: self.status = u'killing flies, because no zombies left' action = None elif action == 'k': self.status = u'healing self' return 'heal', self elif action == 'l': players = [ thing for thing in things.values() if isinstance(thing, Player) and thing is not self ] if players: self.status = u'healing closest friend' action = 'heal' target = closest(self, players) else: self.status = u'healing flies, because no players left' action = None else: action = None self.status = u'confused, pressing random keys' if action: return action, target
def next_step(self, things, t): # action = random.choice(('move', 'attack', 'heal')) if self.life <= self.MAX_LIFE * .4: #max_heal_threshold(self): self.status = "I'm dieing!" return 'heal', self zombies = [thing for thing in things.values() if isinstance(thing, Zombie)] closest_zombie = closest(self, zombies) players = [thing for thing in things.values() if isinstance(thing, Player)] other_players = set(players) - set([self]) zombie_locations = [position for position, thing in things if isinstance(thing, Zombie)] moves = possible_moves(self, things) if moves and len([1 for move in moves if move in zombie_locations]) == 3: assert len(moves) == 1 self.status = "I'm surrounded!" return 'move', moves[0] #if closest_zombie and len(list(filter(lambda z: distance(self, z) <= 3, zombies))) > 5: # self.status = "I'm surrounded!" # return 'attack', closest_zombie if other_players and closest_zombie and len(list(filter(lambda z: distance(self, z) <= closest_zombie.weapon.max_range, zombies))) > 1: avgx = int(sum(player.position[0] for player in other_players) / len(other_players)) avgy = int(sum(player.position[0] for player in other_players) / len(other_players)) moves = astar(self.position, (avgx, avgy), closed=set(things.keys()) - set([(avgx, avgy)]), goal_met=distance_goal_fn(core.HEALING_RANGE)) if moves: self.status = 'This is a little overwhelming' return 'move', moves[0] players.sort(key=lambda x: x.life) if closest_zombie: moves_left_predicted = distance(self, closest_zombie) - closest_zombie.weapon.max_range #self.weapon.max_range else: moves_left_predicted = 9999 if moves_left_predicted - 1 > 0: if not closest_zombie: players_to_heal = players else: players_to_heal = list(filter(lambda p: (distance(self, p) - core.HEALING_RANGE) < moves_left_predicted, players)) if players_to_heal: player_to_heal = players_to_heal[0] if player_to_heal.life <= max_heal_threshold(self): self.status = 'healing ' + player_to_heal.name if distance(self, player_to_heal) < core.HEALING_RANGE: return 'heal', player_to_heal else: moves = astar(self.position, player_to_heal.position, closed=things.keys(), goal_met=distance_goal_fn(core.HEALING_RANGE)) if moves: return 'move', moves[0] else: self.status = "Healing (can't reach " + player_to_heal.name + ')' return 'heal', self else: if self.life < min_heal_threshold(self): self.status = "Nobody makes me bleed my own blood!" return 'heal', self else: self.status = "If it bleeds, we can kill it..." if closest_zombie and distance(self, closest_zombie) <= self.weapon.max_range: return 'attack', closest_zombie else: if closest_zombie: moves = astar(self.position, closest_zombie.position, closed=things.keys(), goal_met=distance_goal_fn(self.weapon.max_range)) else: moves = [] if moves: return 'move', moves[0] else: self.status = "Healing (can't attack)" return 'heal', self else: if self.life < min_heal_threshold(self): self.status = "Nobody makes me bleed my own blood!" return 'heal', self else: self.status = "WE'RE INVINCIBLE!" if closest_zombie and distance(self, closest_zombie) <= self.weapon.max_range: return 'attack', closest_zombie else: if closest_zombie: moves = astar(self.position, closest_zombie.position, closed=things.keys(), goal_met=distance_goal_fn(self.weapon.max_range)) else: moves = [] if moves: return 'move', moves[0] else: self.status = "Healing (can't attack)" return 'heal', self else: self.status = "DIE!" return 'attack', closest_zombie