2024-04-12 21:50:11 +02:00
|
|
|
import random
|
2024-03-05 14:36:03 +01:00
|
|
|
import pygame
|
|
|
|
import sys
|
2024-05-10 22:29:08 +02:00
|
|
|
|
2024-05-08 11:54:49 +02:00
|
|
|
sys.path.append('./Animals')
|
|
|
|
from animals import create_animals, draw_Animals
|
2024-03-22 16:59:39 +01:00
|
|
|
from agent import Agent
|
2024-05-08 11:54:49 +02:00
|
|
|
from enclosure import create_enclosures, draw_enclosures, draw_gates
|
2024-03-24 17:33:58 +01:00
|
|
|
from spawner import Spawner
|
2024-05-08 11:54:49 +02:00
|
|
|
from state_space_search import graphsearch, generate_cost_map
|
|
|
|
from terrain_obstacle import create_obstacles, draw_Terrain_Obstacles
|
|
|
|
from constants import Constants, init_pygame
|
2024-05-08 21:18:55 +02:00
|
|
|
from draw import draw_goal, draw_grid, draw_house
|
2024-05-10 22:29:08 +02:00
|
|
|
from season import draw_background
|
|
|
|
from night import change_time
|
2024-03-05 14:36:03 +01:00
|
|
|
|
2024-05-08 11:54:49 +02:00
|
|
|
const = Constants()
|
|
|
|
init_pygame(const)
|
2024-03-05 14:36:03 +01:00
|
|
|
pygame.display.set_caption("Mini Zoo")
|
|
|
|
|
2024-04-16 14:50:06 +02:00
|
|
|
obstacles = set()
|
2024-03-24 17:46:13 +01:00
|
|
|
animals_position = set()
|
2024-04-26 23:45:06 +02:00
|
|
|
terrain_obstacles_position = set()
|
2024-03-23 13:55:16 +01:00
|
|
|
|
2024-04-27 19:50:18 +02:00
|
|
|
Animals = create_animals()
|
|
|
|
Enclosures = create_enclosures()
|
2024-05-08 11:54:49 +02:00
|
|
|
Terrain_Obstacles = create_obstacles()
|
2024-03-05 14:36:03 +01:00
|
|
|
|
2024-03-24 17:33:58 +01:00
|
|
|
def spawn_all_animals():
|
|
|
|
for Animal in Animals:
|
2024-04-26 23:45:06 +02:00
|
|
|
spawner1 = Spawner(Animal)
|
|
|
|
spawner1.spawn_animal(obstacles, animals_position, Enclosures)
|
|
|
|
|
|
|
|
def spawn_obstacles():
|
|
|
|
for terrain_obstacle in Terrain_Obstacles:
|
2024-05-08 11:54:49 +02:00
|
|
|
spawner2 = Spawner(terrain_obstacle)
|
|
|
|
spawner2.spawn_terrain_obstacles(obstacles, animals_position, terrain_obstacles_position, const.GRID_WIDTH, const.GRID_HEIGHT)
|
2024-03-24 17:33:58 +01:00
|
|
|
|
2024-04-12 18:17:30 +02:00
|
|
|
def generate_obstacles():
|
|
|
|
for en in Enclosures:
|
|
|
|
# Pobierz współrzędne bramy
|
2024-04-28 22:40:13 +02:00
|
|
|
gate_x, gate_y = en.gate1
|
2024-04-12 18:17:30 +02:00
|
|
|
gate_x -= 1
|
|
|
|
gate_y -= 1
|
2024-04-28 22:40:13 +02:00
|
|
|
|
|
|
|
gate_x2, gate_y2 = en.gate2
|
|
|
|
gate_x2 -= 1
|
|
|
|
gate_y2 -= 1
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
# Dodaj lewy brzeg prostokąta
|
|
|
|
for y in range(en.y1, en.y2 + 1):
|
2024-04-28 22:40:13 +02:00
|
|
|
if (en.x1, y) != (gate_x, gate_y) and (en.x1, y) != (gate_x2, gate_y2):
|
2024-04-16 14:50:06 +02:00
|
|
|
obstacles.add((en.x1, y))
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
# Dodaj prawy brzeg prostokąta
|
|
|
|
for y in range(en.y1, en.y2 + 1):
|
2024-04-28 22:40:13 +02:00
|
|
|
if (en.x2, y) != (gate_x, gate_y) and (en.x2, y) != (gate_x2, gate_y2):
|
2024-04-16 14:50:06 +02:00
|
|
|
obstacles.add((en.x2, y))
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
# Dodaj górny brzeg prostokąta
|
|
|
|
for x in range(en.x1+1, en.x2):
|
2024-04-28 22:40:13 +02:00
|
|
|
if (x, en.y1) != (gate_x, gate_y) and (x, en.y1) != (gate_x2, gate_y2):
|
2024-04-16 14:50:06 +02:00
|
|
|
obstacles.add((x, en.y1))
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
# Dodaj dolny brzeg prostokąta
|
|
|
|
for x in range(en.x1+1, en.x2):
|
2024-04-28 22:40:13 +02:00
|
|
|
if (x, en.y2) != (gate_x, gate_y) and (x, en.y2) != (gate_x2, gate_y2):
|
2024-04-16 14:50:06 +02:00
|
|
|
obstacles.add((x, en.y2))
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
return obstacles
|
|
|
|
|
2024-03-05 14:36:03 +01:00
|
|
|
def main():
|
2024-05-08 11:54:49 +02:00
|
|
|
initial_state = (0, 0, 'S')
|
|
|
|
agent = Agent(initial_state, 'images/agent1.png', const.GRID_SIZE)
|
2024-04-12 18:17:30 +02:00
|
|
|
|
|
|
|
obstacles = generate_obstacles()
|
|
|
|
actions = []
|
2024-03-05 14:36:03 +01:00
|
|
|
clock = pygame.time.Clock()
|
2024-04-12 21:50:11 +02:00
|
|
|
spawned = False
|
2024-05-10 22:29:08 +02:00
|
|
|
|
2024-05-12 23:05:28 +02:00
|
|
|
# Lista zawierająca klatki do odwiedzenia
|
|
|
|
enclosures_to_visit = Enclosures.copy()
|
|
|
|
current_enclosure_index = -1 # Indeks bieżącej klatki
|
|
|
|
actions_to_compare_list = [] # Lista zawierająca ścieżki do porównania
|
|
|
|
goals_to_compare_list = list() # Lista zawierająca cele do porównania
|
|
|
|
|
2024-03-05 14:36:03 +01:00
|
|
|
while True:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
2024-05-12 16:25:09 +02:00
|
|
|
agent.handle_event(event, const.GRID_WIDTH, const.GRID_HEIGHT, Animals, obstacles,const)
|
2024-05-08 11:54:49 +02:00
|
|
|
|
2024-05-10 22:29:08 +02:00
|
|
|
change_time(const)
|
|
|
|
draw_background(const)
|
2024-05-08 11:54:49 +02:00
|
|
|
draw_grid(const)
|
|
|
|
draw_enclosures(Enclosures, const)
|
|
|
|
draw_gates(Enclosures, const)
|
2024-05-08 21:18:55 +02:00
|
|
|
draw_house(const)
|
2024-05-12 16:25:09 +02:00
|
|
|
|
2024-04-12 21:50:11 +02:00
|
|
|
if not spawned:
|
|
|
|
spawn_all_animals()
|
2024-04-26 23:45:06 +02:00
|
|
|
spawn_obstacles()
|
2024-05-08 11:54:49 +02:00
|
|
|
cost_map = generate_cost_map(Animals, Terrain_Obstacles)
|
2024-04-12 23:11:46 +02:00
|
|
|
for animal in Animals:
|
2024-05-12 23:05:28 +02:00
|
|
|
# animal._feed = 0
|
|
|
|
animal._feed = random.randint(0, 10)
|
2024-04-12 21:50:11 +02:00
|
|
|
spawned = True
|
2024-05-08 11:54:49 +02:00
|
|
|
|
|
|
|
draw_Animals(Animals, const)
|
|
|
|
draw_Terrain_Obstacles(Terrain_Obstacles, const)
|
2024-05-10 22:29:08 +02:00
|
|
|
agent.draw(const)
|
2024-03-05 14:36:03 +01:00
|
|
|
pygame.display.flip()
|
|
|
|
clock.tick(10)
|
2024-04-16 14:50:06 +02:00
|
|
|
|
2024-04-12 18:17:30 +02:00
|
|
|
if actions:
|
|
|
|
action = actions.pop(0)
|
2024-05-12 16:25:09 +02:00
|
|
|
agent.move(action, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, Animals, goal,const)
|
2024-04-12 18:17:30 +02:00
|
|
|
pygame.time.wait(200)
|
2024-04-16 14:50:06 +02:00
|
|
|
else:
|
2024-05-12 23:05:28 +02:00
|
|
|
if agent._dryfood > 1 and agent._wetfood > 1 :
|
|
|
|
if not goals_to_compare_list:
|
|
|
|
current_enclosure_index = (current_enclosure_index + 1) % len(enclosures_to_visit)
|
|
|
|
current_enclosure = enclosures_to_visit[current_enclosure_index]
|
|
|
|
|
|
|
|
for animal in current_enclosure.animals:
|
|
|
|
goal = (animal.x, animal.y)
|
|
|
|
goals_to_compare_list.append(goal)
|
|
|
|
|
|
|
|
actions_to_compare = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
|
|
|
|
actions_to_compare_list.append((actions_to_compare, goal))
|
|
|
|
|
|
|
|
chosen_path_and_goal = min(actions_to_compare_list, key=lambda x: len(x[0]))
|
|
|
|
goal = chosen_path_and_goal[1]
|
2024-05-08 21:18:55 +02:00
|
|
|
draw_goal(const, goal)
|
2024-05-12 23:05:28 +02:00
|
|
|
|
|
|
|
# Usuń wybrany element z listy
|
|
|
|
actions_to_compare_list.remove(chosen_path_and_goal)
|
|
|
|
goals_to_compare_list.remove(goal)
|
|
|
|
|
2024-05-08 21:18:55 +02:00
|
|
|
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
|
2024-05-12 23:05:28 +02:00
|
|
|
|
2024-05-08 21:18:55 +02:00
|
|
|
else:
|
|
|
|
goal = (3,1)
|
|
|
|
draw_goal(const, goal)
|
|
|
|
actions = graphsearch(agent.istate, goal, const.GRID_WIDTH, const.GRID_HEIGHT, obstacles, cost_map)
|
2024-04-27 19:50:18 +02:00
|
|
|
|
2024-03-05 14:36:03 +01:00
|
|
|
if __name__ == "__main__":
|
2024-05-08 11:54:49 +02:00
|
|
|
main()
|