def createTornado(self, team): with self.lock: if not self.testing: if tornado_cost > self.points[team]: return "Not enough points" if len(self.selected[team]) != 1: return "Must select one cell" self.points[team] -= tornado_cost row, col = iter(self.selected[team]).next() if self.cells[row][col].team != neutral_str: return "Illegal tornado placement" self.cells[row][col] = tornado() return self.selected[team] else: ret = self.selected[team] for (row,col) in self.selected[team]: self.points[team] -= tornado_cost old_team = self.cells[row][col].team self.cells[row][col] = tornado() if old_team not in neutral_teams: ret = ret.union(set(self.adj(row,col))) return ret
def reset(self, include_tornado = True, testing = False): with self.lock: self.testing = testing self.turn = 0 self.land = [[baseLand() for col in range(self.width)] for row in range(self.height)] self.selected = dict() for team in self.teams: self.selected[team] = set() self.points = collections.Counter() self.points[self.teams[0]] = 0 self.points[self.teams[1]] = 0 for row in range(self.height): for col in range((self.width + 1) / 2): result = self.rand.random() if result < .75 and col < self.width * 2 / 5: # < 1000 10000 # .25 .265 .2927 # .35 .385 # .50 .469 # .55 .487 .4802 # .60 .518 .4972 # .65 .522 .5022 # .66 .4956 # .67 .509 .5052 # .68 .4987 # .70 .522 .5001 # .75 .488 .5182 # .76 .5012 # .77 .4969 # .80 .5126 # .85 .4758 self.cells[row][col] = Cell(self.teams[0], 3, initDna()) self.cells[row][self.width - col - 1] = Cell(self.teams[1], 3, initDna()) else: self.cells[row][col] = neutral() self.cells[row][self.width - col - 1] = neutral() if include_tornado: self.cells[self.width / 2][self.height / 2] = tornado() self.warning_cells.clear() self.lava_cells.clear() self.volcanoes.clear()
def internal(self): step = [[neutral() for col in range(self.width)] for row in range(self.height)] with self.lock: for row in range(self.height): for col in range(self.width): counts = collections.Counter() strengths = collections.Counter() adjacents = self.adj(row, col) team = self.cells[row][col].team for (r,c) in adjacents: counts[self.cells[r][c].team] += 1 strengths[self.cells[r][c].team] += self.cells[r][c].strength if team not in neutral_teams: friendly = counts[team] if self.cells[row][col].isWarrior2(): friendly += self.cells[row][col].strength sum_enemy = 0 for str_team, strength in strengths.items(): if str_team != team: sum_enemy += strength is_dead = True if self.land[row][col].canSupport(friendly): if sum_enemy < strengths[team]: step[row][col] = self.cells[row][col] is_dead = False else: for r,c in self.friendlyAdj(row,col): if self.cells[r][c].isMedic(): for i in range(self.cells[r][c].medicLevel()): if self.rand.random() < .25: step[row][col] = self.cells[row][col] is_dead = False break else: # Medic did not save continue # Medic saved break else: # killed in combat for str_team, strength in strengths.items(): if str_team != team and str_team not in neutral_teams: self.points[str_team] += 1 if is_dead: for k_team in self.selected.iterkeys(): if (row,col) in self.selected[k_team]: self.selected[k_team].remove((row,col)) for r,c in adjacents: if self.cells[r][c].isHunter(): for i in range(self.cells[r][c].hunterLevel()): self.land[row][col].regen() self.land[row][col].regen() self.land[row][col].regen() self.land[row][col].regen() elif team == neutral_str: threes = [] for c_team, count in counts.items(): if count == 3 and c_team not in neutral_teams: threes.append(c_team) if len(threes) == 1: strength = strengths[threes[0]] + self.rand.randint(1,12) parents = [self.cells[loc[0]][loc[1]] for loc in adjacents if self.cells[loc[0]][loc[1]].team == threes[0]] step[row][col] = offspring(parents[0],parents[1],parents[2]) self.points[threes[0]] += 1 for steam in self.selected: if (row,col) in self.selected[steam]: self.selected[steam].remove((row,col)) elif len(threes) == 2: if strengths[threes[0]] > strengths[threes[1]]: winner = threes[0] elif strengths[threes[0]] < strengths[threes[1]]: winner = threes[1] else: winner = threes[self.rand.randint(0,1)] parents = [self.cells[loc[0]][loc[1]] for loc in adjacents if self.cells[loc[0]][loc[1]].team == winner] step[row][col] = offspring(parents[0],parents[1],parents[2]) for steam in self.selected: if (row,col) in self.selected[steam]: self.selected[steam].remove((row,col)) self.points[winner] += 1 for (new_r, new_c), (old_r, old_c) in zip(self.tornadoes, self.old_tornadoes): step[new_r][new_c] = tornado() for team in self.teams: if (old_r,old_c) in self.selected[team]: self.selected[team].remove((old_r,old_c)) self.selected[team].add((new_r,new_c)) self.cells = step