Exemplo n.º 1
0
    def get_move(self, gameboard, player, opponent):
        if not self.init_with_data:
            self.turrets = {"x": {}, "y": {}}
            for turret in gameboard.turrets:
                bounds = [turret.x-4, turret.x+4]
                for coeff in [-1, 1]:
                    for j in range(1, 5):
                        if gameboard.wall_at_tile[(turret.x+j*coeff) % gameboard.width][turret.y]:
                            bounds[int((coeff+1)/2)] = turret.x+(j-1)*coeff
                            break
                
                if(turret.y not in self.turrets['x']):
                    if bounds[1] > gameboard.height:
                        self.turrets['x'][turret.y] = [(0, bounds[1] % gameboard.width, turret), (bounds[0], gameboard.width-1, turret)]
                    elif bounds[0] < 0:
                        self.turrets['x'][turret.y] = [(bounds[0] % gameboard.width, gameboard.width-1, turret), (0, bounds[1], turret)]
                    else:
                        self.turrets['x'][turret.y] = [(bounds[0], bounds[1], turret)]
                else:
                    if bounds[1] > gameboard.height:
                        self.turrets['x'][turret.y] += [(0, bounds[1] % gameboard.width, turret), (bounds[0], gameboard.width-1, turret)]
                    elif bounds[0] < 0:
                        self.turrets['x'][turret.y] += [(bounds[0] % gameboard.width, gameboard.width-1, turret), (0, bounds[1], turret)]
                    else:
                        self.turrets['x'][turret.y] += [(bounds[0], bounds[1], turret)]
                            
            for turret in gameboard.turrets:
                bounds = [turret.y-4, turret.y+4]
                for coeff in [-1, 1]:
                    for j in range(1, 5):
                        if gameboard.wall_at_tile[turret.x][(turret.y+j*coeff) % gameboard.width]:
                            bounds[int((coeff+1)/2)] = turret.y+(j-1)*coeff
                            break
                
                print(bounds)
                if(turret.x not in self.turrets['y']):
                    if bounds[1] >= gameboard.width:
                        self.turrets['y'][turret.x] = [(0, bounds[1] % gameboard.height, turret), (bounds[0], gameboard.height-1, turret)]
                    elif bounds[0] < 0:
                        self.turrets['y'][turret.x] = [(bounds[0] % gameboard.height, gameboard.height-1, turret), (0, bounds[1], turret)]
                    else:
                        self.turrets['y'][turret.x] = [(bounds[0], bounds[1], turret)]
                else:
                    if bounds[1] >= gameboard.width:
                        self.turrets['y'][turret.x] += [(0, bounds[1] % gameboard.height, turret), (bounds[0], gameboard.height-1, turret)]
                    elif bounds[0] < 0:
                        self.turrets['y'][turret.x] += [(bounds[0] % gameboard.height, gameboard.height-1, turret), (0, bounds[1], turret)]
                    else:
                        self.turrets['y'][turret.x] += [(bounds[0], bounds[1], turret)]
            
            grid = Grid(gameboard)
            astar = AStar(grid, player, gameboard, self.turrets)
            objectives = astar.process()
            self.objectives = objectives;
            #print(objectives)
            self.init_with_data = True

            #CHOOSE THE PATH Using indexMax
            
            #If turn is 1:
                #decrement all the turns and pop and return foward 
            #Else:
                #decrement all the turns and change the face
        for turret in gameboard.turrets:
            print("~~~", (turret.x, turret.y), turret.is_dead, "~~~")
        
        if len(self.force_stack) > 0:
            print("SHIELDING")
            return self.force_stack.pop()
        else:
            if self.last_index != None and not self.objectives[self.last_index]['path']:
                grid = Grid(gameboard)
                astar = AStar(grid, player, gameboard, self.turrets)
                objectives = astar.process()
                self.objectives = objectives;
                print("\n")
                
            new_list = []
            x = -1
            for pair in self.objectives:
                x += 1
                if(pair['path'] == None or pair['path'][0]['turns'] == 0):
                    del(self.objectives[x])
                    continue
                distance = pair['path'][0]['turns'];
                new_list.append(distance)
            index = new_list.index(min(new_list));
            #index = index_max(objectives, gameboard.get_turns_remaining());
            Path = self.objectives[index];
            self.last_index = index
            
            print(Path)
            
            for step in Path['path']:
                step['turns'] -= 1
            
            if (Path['path'][-1]['turns'] == 0):
                if self.wait_move(gameboard, player, opponent, Path['path'][-1]['coord']):
                    return Move.NONE;
                if self.away(opponent, player, gameboard, 1):
                    if opponent.shield_active:
                        step['turns'] += 1;
                        return Move.SHOOT;
                if self.away(opponent, player, gameboard, 2):
                    if (opponent.shield_count == 0):
                        step['turns'] += 1;
                        return Move.SHOOT;
                        
                if len(Path['path']) == 1 and isinstance(Path['objective'], Turret) and "_need_powerup" in Path['path'][-1] and Path['path'][-1]['_need_powerup']:
                    #this is the last step of a turret instruction, and a powerup is needed
                    if player.laser_count > 0:
                        self.force_stack.append(Move.LASER)
                    elif player.shield_count > 0:
                        self.force_stack.append(Move.SHIELD)
                    else:
                        step['turns'] += 1;
                        Path['path'].pop()
                        return Move.NONE
                    # else:
                    #     # we can shoot the turret!
                    #     dist = abs(Path['objective'].x - Path['path'][-1]['coord'][0]) + abs(Path['objective'].y - Path['path'][-1]['coord'][1])
                    #     # we have to push onto stack backwards
                    #     self.force_stack.append( Move.FORWARD )
                    #     self.force_stack.append( player.direction )
                    #     self.force_stack.append( Move.SHOOT )
                    #     self.force_stack.append(\
                    #         self.normalized_directions[(ceil((Path['objective'].x - Path['path'][-1]['coord'][0])/dist), ceil((Path['objective'].y - Path['path'][-1]['coord'][1])/dist))][1] \
                    #         )
                            
                motion = (Move.FORWARD if Path['path'][-1]['move'] == self.direction_to_move[player.direction] else Path['path'][-1]['move'])
                Path['path'].pop();
                # print("~~~~", motion, "~~~~")
                return motion
            else:
                # print(Path['path'][-1])
                return Path['path'][-1]['move']