2021-04-24 00:04:19 +02:00
import astar
2021-05-31 17:10:14 +02:00
import cart
2021-03-21 12:40:13 +01:00
import definitions
2021-04-09 22:49:58 +02:00
import graph
2021-06-01 02:00:55 +02:00
import image_slicer
2021-04-03 00:16:53 +02:00
import map
2021-05-31 17:10:14 +02:00
import neuralnetwork
2021-06-01 02:00:55 +02:00
import os
2021-03-21 12:40:13 +01:00
import plant
2021-03-13 22:30:18 +01:00
import pygame
2021-04-03 00:16:53 +02:00
import station
2021-05-15 22:41:07 +02:00
import treelearn
2021-05-31 17:10:14 +02:00
pygame . display . set_caption ( " Smart Cart " )
2021-03-13 22:30:18 +01:00
def main ( ) :
2021-04-03 11:24:39 +02:00
#tworzenie podstawowych obiektów
2021-04-03 00:16:53 +02:00
map1 = map . Map ( [ ] )
map1 . create_base_map ( )
2021-04-10 18:24:13 +02:00
move_list = [ ]
2021-05-31 17:10:14 +02:00
amount_of_seeds_dict = { " beetroot " : definitions . CART_AMOUNT_OF_SEEDS_EACH_TYPE , " carrot " : definitions . CART_AMOUNT_OF_SEEDS_EACH_TYPE , " potato " : definitions . CART_AMOUNT_OF_SEEDS_EACH_TYPE , " wheat " : definitions . CART_AMOUNT_OF_SEEDS_EACH_TYPE }
2021-03-31 12:56:29 +02:00
collected_plants_dict = { " beetroot " : 0 , " carrot " : 0 , " potato " : 0 , " wheat " : 0 }
2021-05-31 17:10:14 +02:00
fertilizer_dict = { " beetroot " : definitions . CART_FERTILIZER , " carrot " : definitions . CART_FERTILIZER , " potato " : definitions . CART_FERTILIZER , " wheat " : definitions . CART_FERTILIZER }
2021-04-03 00:16:53 +02:00
station1 = station . Station ( collected_plants_dict )
2021-05-31 17:10:14 +02:00
cart1 = cart . Cart ( amount_of_seeds_dict , collected_plants_dict , definitions . CART_DIRECTION_NORTH , fertilizer_dict , definitions . CART_FUEL , definitions . CART_WATER_LEVEL , 0 , 0 )
cart1_rect = pygame . Rect ( cart1 . get_x ( ) , cart1 . get_y ( ) , definitions . BLOCK_SIZE , definitions . BLOCK_SIZE )
2021-03-13 22:30:18 +01:00
clock = pygame . time . Clock ( )
2021-05-16 15:36:53 +02:00
tree = treelearn . treelearn ( ) #tworzenie drzewa decyzyjnego
decision = [ 0 ] #początkowa decyzja o braku powrotu do stacji (0)
2021-06-01 02:00:55 +02:00
classes , model = neuralnetwork . create_neural_network ( ) #uczenie sieci neuronowej
2021-03-13 22:30:18 +01:00
run = True
2021-04-03 11:24:39 +02:00
while run : #pętla główna programu
2021-03-21 12:40:13 +01:00
clock . tick ( definitions . FPS )
2021-03-13 22:30:18 +01:00
for event in pygame . event . get ( ) :
if event . type == pygame . QUIT :
run = False
2021-05-31 17:10:14 +02:00
map1 . draw_window ( cart1 , cart1_rect )
2021-06-01 02:00:55 +02:00
pygame . image . save ( pygame . display . get_surface ( ) , os . path . join ( ' resources/neural_network/sliced/ ' , ' screen.jpg ' ) ) #zrzut obecnego ekranu
image_slicer . slice ( os . path . join ( ' resources/neural_network/sliced/ ' , ' screen.jpg ' ) , 100 ) #pocięcie ekranu na sto części
os . remove ( ' resources/neural_network/sliced/screen.jpg ' )
if not move_list and neuralnetwork . predfield ( classes , model ) is not False : #jeżeli są jakieś ruchy do wykonania w move_list oraz istnieje jakaś dojrzała roślina
istate = graph . Istate ( cart1 . get_direction ( ) , cart1 . get_x ( ) / definitions . BLOCK_SIZE , cart1 . get_y ( ) / definitions . BLOCK_SIZE ) #stan początkowy wózka (jego orientacja oraz jego aktualne miejsce)
2021-05-16 15:36:53 +02:00
if decision == [ 0 ] : #jeżeli decyzja jest 0 (brak powrotu do stacji) to uprawiaj pole
2021-06-01 02:00:55 +02:00
move_list = ( astar . graphsearch ( [ ] , astar . f , [ ] , neuralnetwork . predfield ( classes , model ) , istate , map1 , graph . succ ) ) #lista z ruchami, które należy po kolei wykonać, astar
2021-05-16 15:36:53 +02:00
else : #jeżeli decyzja jest 1 (powrót do stacji) to wróć do stacji uzupełnić zapasy
2021-05-31 18:16:14 +02:00
move_list = ( graph . graphsearch ( [ ] , [ ] , ( 0 , 0 ) , istate , graph . succ ) ) #lista z ruchami, które należy po kolei wykonać, graphsearch
2021-04-10 19:38:24 +02:00
elif move_list : #jeżeli move_list nie jest pusta
2021-05-31 18:16:14 +02:00
cart1 . handle_movement ( cart1_rect , move_list . pop ( 0 ) ) #wykonaj kolejny ruch oraz zdejmij ten ruch z początku listy
2021-04-10 18:24:13 +02:00
else :
2021-05-31 17:10:14 +02:00
cart1 . handle_movement_random ( cart1_rect ) #wykonuj losowe ruchy
2021-05-31 18:16:14 +02:00
cart1 . do_work ( cart1_rect , map1 , station1 ) #wykonaj pracę na danym polu
decision = treelearn . make_decision ( cart1 . get_all_amount_of_seeds ( ) , cart1 . get_all_collected_plants ( ) , cart1 . get_all_fertilizer ( ) , cart1 . get_fuel ( ) , tree , cart1 . get_water_level ( ) ) #podejmij decyzję czy wracać do stacji (0 : NIE, 1 : TAK)
2021-04-10 19:38:24 +02:00
plant . Plant . grow_plants ( map1 ) #zwiększ poziom dojrzałości roślin
2021-03-13 22:30:18 +01:00
pygame . quit ( )
if __name__ == " __main__ " :
main ( )