|
|
|
@ -21,6 +21,11 @@ class Knight: |
|
|
|
self.direction = 2 |
|
|
|
self.direction = 2 |
|
|
|
self.visited = [self.position] |
|
|
|
self.visited = [self.position] |
|
|
|
self.starting_position = (position) |
|
|
|
self.starting_position = (position) |
|
|
|
|
|
|
|
self.already_tried = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def move(self, target): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ai(self): |
|
|
|
def ai(self): |
|
|
|
screen_x, screen_y = self.engine.iso_transform(*self.position) |
|
|
|
screen_x, screen_y = self.engine.iso_transform(*self.position) |
|
|
|
@ -33,66 +38,15 @@ class Knight: |
|
|
|
screen_y += (screen_y_dest - screen_y) * self.partial_move |
|
|
|
screen_y += (screen_y_dest - screen_y) * self.partial_move |
|
|
|
else: |
|
|
|
else: |
|
|
|
screen_x, screen_y = self.engine.iso_transform(*self.destination) |
|
|
|
screen_x, screen_y = self.engine.iso_transform(*self.destination) |
|
|
|
|
|
|
|
|
|
|
|
neighbors_list = self.engine.find_neighbors(self.destination) # Get the neighbors of the destination cell |
|
|
|
neighbors_list = self.engine.find_neighbors(self.destination) # Get the neighbors of the destination cell |
|
|
|
|
|
|
|
|
|
|
|
# Remove any neighbors that are not walkable |
|
|
|
# Remove any neighbors that are not walkable |
|
|
|
neighbors_list = [cell for cell in neighbors_list if self.engine.battlefield[cell[1]][cell[0]].walkable] |
|
|
|
neighbors_list = [cell for cell in neighbors_list if self.engine.battlefield[cell[1]][cell[0]].walkable] |
|
|
|
|
|
|
|
# Update position and visited list |
|
|
|
old_distance_from_target = self.engine.get_distance(self.destination,self.target) |
|
|
|
|
|
|
|
old_distance_from_start = self.engine.get_distance(self.destination,self.starting_position) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for cell_visited in self.visited: |
|
|
|
|
|
|
|
if cell_visited in neighbors_list: |
|
|
|
|
|
|
|
neighbors_list.remove(cell_visited) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#self.visited = self.visited[-2:] |
|
|
|
|
|
|
|
self.position = self.destination |
|
|
|
self.position = self.destination |
|
|
|
|
|
|
|
self.destination = None |
|
|
|
self.visited.append(self.position) |
|
|
|
self.visited.append(self.position) |
|
|
|
|
|
|
|
self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target) |
|
|
|
for cell in neighbors_list: |
|
|
|
self.partial_move = 0 |
|
|
|
self.engine.effects.append(OrderClick(self.engine.iso_transform(*cell),color="lightblue")) |
|
|
|
|
|
|
|
for cell in self.visited: |
|
|
|
|
|
|
|
self.engine.effects.append(OrderClick(self.engine.iso_transform(*cell),color="red")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not neighbors_list: |
|
|
|
|
|
|
|
print("No neighbors") |
|
|
|
|
|
|
|
#neighbors_list.append(self.visited) |
|
|
|
|
|
|
|
#self.target = self.position |
|
|
|
|
|
|
|
self.destination = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target) |
|
|
|
|
|
|
|
new_distance_from_target = self.engine.get_distance(self.destination,self.target) |
|
|
|
|
|
|
|
new_distance_from_start = self.engine.get_distance(self.destination,self.starting_position) |
|
|
|
|
|
|
|
if new_distance_from_target >= old_distance_from_target or new_distance_from_start < old_distance_from_start: |
|
|
|
|
|
|
|
print(f"{time.time()} - No progress") |
|
|
|
|
|
|
|
new_neighbors_list = [] |
|
|
|
|
|
|
|
for neighbor in neighbors_list: |
|
|
|
|
|
|
|
neighbor_neighbors_list = self.engine.find_neighbors(neighbor) |
|
|
|
|
|
|
|
for neighbor_neighbor in neighbor_neighbors_list: |
|
|
|
|
|
|
|
# consider only the neighbors of the neighbors that are walkable |
|
|
|
|
|
|
|
if not self.engine.battlefield[neighbor_neighbor[1]][neighbor_neighbor[0]].walkable: |
|
|
|
|
|
|
|
if neighbor_neighbor not in self.visited: |
|
|
|
|
|
|
|
# if the neighbor of the neighbor is not walkable and has not been visited, add it to the list of visited cells |
|
|
|
|
|
|
|
new_neighbors_list.append(neighbor) |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
# scegli uhn vicino casualmente come destinaziione |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.destination = new_neighbors_list[random.randint(0,len(new_neighbors_list)-1)] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.destination: |
|
|
|
|
|
|
|
self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="purple")) |
|
|
|
|
|
|
|
self.partial_move = 0 |
|
|
|
|
|
|
|
if self.position != self.target: |
|
|
|
|
|
|
|
self.direction = self.engine.get_direction(self.position,self.destination) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
print("No destination") |
|
|
|
|
|
|
|
self.visited = [self.position] |
|
|
|
|
|
|
|
self.destination = self.position |
|
|
|
|
|
|
|
#self.target = self.position |
|
|
|
|
|
|
|
self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="brown")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
|
else: |
|
|
|
self.visited = [self.target] |
|
|
|
self.visited = [self.target] |
|
|
|
self.position = self.target |
|
|
|
self.position = self.target |
|
|
|
@ -101,4 +55,8 @@ class Knight: |
|
|
|
self.state = "Idle" |
|
|
|
self.state = "Idle" |
|
|
|
self.partial_move = 1 |
|
|
|
self.partial_move = 1 |
|
|
|
gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif') |
|
|
|
gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif') |
|
|
|
return gif, screen_x, screen_y |
|
|
|
return gif, screen_x, screen_y |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def move_to(self, target): |
|
|
|
|
|
|
|
self.target = target |
|
|
|
|
|
|
|
self.state = "Walk" |
|
|
|
|