2023-04-22 17:46:08 +02:00
|
|
|
from agentActionType import AgentActionType
|
|
|
|
import time
|
|
|
|
from turnCar import turn_left_orientation, turn_right_orientation
|
|
|
|
from garbageTruck import GarbageTruck
|
|
|
|
from typing import Tuple, Dict
|
|
|
|
from gridCellType import GridCellType
|
|
|
|
from gameContext import GameContext
|
|
|
|
from agentOrientation import AgentOrientation
|
|
|
|
import pygame
|
2023-04-23 16:42:45 +02:00
|
|
|
from bfs import find_path_to_nearest_can
|
|
|
|
from agentState import AgentState
|
|
|
|
|
2023-05-25 18:18:11 +02:00
|
|
|
|
2023-04-23 16:42:45 +02:00
|
|
|
def collect_garbage(game_context: GameContext) -> None:
|
|
|
|
while True:
|
|
|
|
start_agent_state = AgentState(game_context.dust_car.position, game_context.dust_car.orientation)
|
2023-05-25 18:18:11 +02:00
|
|
|
path = find_path_to_nearest_can(start_agent_state, game_context.grid, game_context.city)
|
2023-04-23 16:42:45 +02:00
|
|
|
if path == None or len(path) == 0:
|
|
|
|
break
|
|
|
|
move_dust_car(path, game_context)
|
|
|
|
next_position = calculate_next_position(game_context.dust_car)
|
|
|
|
game_context.grid[next_position] = GridCellType.VISITED_GARBAGE_CAN
|
2023-05-13 23:06:42 +02:00
|
|
|
game_context.city.cans_dict[next_position].is_visited = True
|
2023-04-23 16:42:45 +02:00
|
|
|
pass
|
2023-04-22 17:46:08 +02:00
|
|
|
|
2023-05-25 18:18:11 +02:00
|
|
|
|
2023-04-22 17:46:08 +02:00
|
|
|
def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None:
|
|
|
|
for action in actions:
|
|
|
|
street_position = game_context.dust_car.position
|
|
|
|
has_to_render_street = False
|
|
|
|
if action == AgentActionType.TURN_LEFT:
|
|
|
|
game_context.dust_car.orientation = turn_left_orientation(game_context.dust_car.orientation)
|
|
|
|
elif action == AgentActionType.TURN_RIGHT:
|
|
|
|
game_context.dust_car.orientation = turn_right_orientation(game_context.dust_car.orientation)
|
|
|
|
elif action == AgentActionType.MOVE_FORWARD:
|
|
|
|
game_context.dust_car.position = calculate_next_position(game_context.dust_car)
|
|
|
|
has_to_render_street = True
|
|
|
|
game_context.dust_car.render(game_context)
|
|
|
|
if has_to_render_street:
|
|
|
|
if game_context.grid[street_position] == GridCellType.STREET_HORIZONTAL:
|
|
|
|
game_context.render_in_cell(street_position, "imgs/street_horizontal.png")
|
|
|
|
elif game_context.grid[street_position] == GridCellType.STREET_VERTICAL:
|
|
|
|
game_context.render_in_cell(street_position, "imgs/street_vertical.png")
|
2023-05-13 19:30:00 +02:00
|
|
|
elif game_context.grid[street_position] == GridCellType.SPEED_BUMP:
|
|
|
|
game_context.render_in_cell(street_position, "imgs/speed_bump.png")
|
2023-04-22 17:46:08 +02:00
|
|
|
pygame.display.update()
|
2023-05-11 20:14:09 +02:00
|
|
|
time.sleep(0.15)
|
2023-04-22 17:46:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
def calculate_next_position(car: GarbageTruck) -> Tuple[int, int]:
|
|
|
|
if car.orientation == AgentOrientation.UP:
|
|
|
|
if car.position[1] - 1 < 1:
|
|
|
|
return None
|
|
|
|
return (car.position[0], car.position[1] - 1)
|
|
|
|
if car.orientation == AgentOrientation.DOWN:
|
|
|
|
if car.position[1] + 1 > 27:
|
|
|
|
return None
|
|
|
|
return (car.position[0], car.position[1] + 1)
|
|
|
|
if car.orientation == AgentOrientation.LEFT:
|
|
|
|
if car.position[0] - 1 < 1:
|
|
|
|
return None
|
|
|
|
return (car.position[0] - 1, car.position[1])
|
|
|
|
if car.position[0] + 1 > 27:
|
|
|
|
return None
|
|
|
|
return (car.position[0] + 1, car.position[1])
|