2024-04-03 23:29:43 +02:00
import pygame
2024-05-26 11:50:42 +02:00
from classes . cell import Cell
import prefs
2024-04-11 13:20:29 +02:00
import random
2024-05-26 11:50:42 +02:00
import heapq
from collections import deque
2024-04-11 13:20:29 +02:00
2024-04-03 23:29:43 +02:00
class Klient :
2024-05-26 11:50:42 +02:00
def __init__ ( self , x , y , cells ) :
self . sprite = pygame . image . load ( " sprites/klient.png " ) . convert_alpha ( )
self . sprite = pygame . transform . scale ( self . sprite , ( prefs . CELL_SIZE , prefs . CELL_SIZE ) )
self . current_cell = cells [ x ] [ y ]
self . current_x = x
self . current_y = y
# self.imie = imie
# self.nazwisko = nazwisko
# self.wiek = wiek
przyStoliku = False
2024-04-03 23:29:43 +02:00
self . stolik = None
2024-05-26 11:50:42 +02:00
# self.rachunek = Rachunek(random.randint(1,1000))
# self.ulubiony_posilek = ulubiony_posilek
# self.restrykcje_dietowe = restrykcje_dietowe
self . cells = cells
self . X = x
self . Y = y
self . last_move_time = pygame . time . get_ticks ( )
self . last_interact_time = pygame . time . get_ticks ( )
self . last_update_time = pygame . time . get_ticks ( )
self . direction = 0
self . directionPOM = 0
self . xPOM = x
self . yPOM = y
self . g_scores = { }
self . textures = [
pygame . image . load ( " sprites/klient.png " ) . convert_alpha ( ) ,
pygame . image . load ( " sprites/klient.png " ) . convert_alpha ( ) ,
pygame . image . load ( " sprites/klient.png " ) . convert_alpha ( ) ,
pygame . image . load ( " sprites/klient.png " ) . convert_alpha ( )
]
def update ( self , surface ) :
surface . blit ( self . sprite , ( self . current_cell . X * prefs . CELL_SIZE ,
self . current_cell . Y * prefs . CELL_SIZE ) )
def moveto ( self , x , y ) :
if not self . cells [ x ] [ y ] . blocking_movement :
self . current_cell = self . cells [ x ] [ y ]
self . moved = True
self . last_move_time = pygame . time . get_ticks ( )
print ( " Agent moved to x,y: " , x , y )
else :
print ( " Agent cannot move to this direction " )
def move_direction ( self ) :
if self . direction == 0 and pygame . time . get_ticks ( ) - self . last_move_time > 125 and self . current_cell . Y < prefs . GRID_SIZE - 1 and not self . cells [ self . current_cell . X ] [ self . current_cell . Y + 1 ] . blocking_movement :
self . current_cell = self . cells [ self . current_cell . X ] [ self . current_cell . Y + 1 ]
self . moved = True
self . last_move_time = pygame . time . get_ticks ( )
if self . direction == 1 and pygame . time . get_ticks ( ) - self . last_move_time > 125 and self . current_cell . X > 0 and not self . cells [ self . current_cell . X - 1 ] [ self . current_cell . Y ] . blocking_movement :
self . current_cell = self . cells [ self . current_cell . X - 1 ] [ self . current_cell . Y ]
self . moved = True
self . last_move_time = pygame . time . get_ticks ( )
if self . direction == 2 and pygame . time . get_ticks ( ) - self . last_move_time > 125 and self . current_cell . Y > 0 and not self . cells [ self . current_cell . X ] [ self . current_cell . Y - 1 ] . blocking_movement :
self . current_cell = self . cells [ self . current_cell . X ] [ self . current_cell . Y - 1 ]
self . moved = True
self . last_move_time = pygame . time . get_ticks ( )
if self . direction == 3 and pygame . time . get_ticks ( ) - self . last_move_time > 125 and self . current_cell . X < prefs . GRID_SIZE - 1 and not self . cells [ self . current_cell . X + 1 ] [ self . current_cell . Y ] . blocking_movement :
self . current_cell = self . cells [ self . current_cell . X + 1 ] [ self . current_cell . Y ]
self . moved = True
self . last_move_time = pygame . time . get_ticks ( )
def rotate_left ( self ) :
if pygame . time . get_ticks ( ) - self . last_move_time > 125 :
self . direction + = 1
if self . direction == 4 :
self . direction = 0
self . sprite = self . textures [ self . direction ]
self . sprite = pygame . transform . scale ( self . sprite , ( prefs . CELL_SIZE , prefs . CELL_SIZE ) )
self . last_move_time = pygame . time . get_ticks ( )
print ( self . direction )
def rotate_right ( self ) :
if pygame . time . get_ticks ( ) - self . last_move_time > 125 :
self . direction - = 1
if self . direction == - 1 :
self . direction = 3
self . sprite = self . textures [ self . direction ]
self . sprite = pygame . transform . scale ( self . sprite , ( prefs . CELL_SIZE , prefs . CELL_SIZE ) )
self . last_move_time = pygame . time . get_ticks ( )
print ( self . direction )
2024-04-03 23:42:53 +02:00
def zloz_zamowienie ( self , zamowienie , stolik ) :
2024-04-03 23:29:43 +02:00
if self . stolik is None :
self . stolik = stolik
2024-04-11 13:20:29 +02:00
stolik . przypisz_kelner ( stolik . kelner )
self . rachunek . dodaj_zamowienie ( zamowienie )
2024-04-03 23:42:53 +02:00
print ( f " Klinet { self . imie } { self . nazwisko } zlozyl zamowienie przy stoliku { stolik . numer_stolika } i przyjal je kelner { stolik . kelner . numer_pracowniczy } . " )
else :
2024-04-03 23:29:43 +02:00
print ( " Klient ma juz przypisany stolik. " )
2024-04-11 13:20:29 +02:00
def __str__ ( self ) :
2024-04-11 15:09:27 +02:00
return f " Klient: { self . imie } { self . nazwisko } { self . wiek } , ulubione Danie: { self . ulubiony_posilek } , restrykcje diet: { self . restrykcje_dietowe } "
2024-05-26 11:50:42 +02:00
def get_possible_moves ( self ) :
possible_moves = [ ]
if self . directionPOM == 0 : # Patrzy w dół
possible_moves . append ( ( 0 , ' left ' ) )
possible_moves . append ( ( 0 , ' right ' ) )
if self . yPOM < prefs . GRID_SIZE - 1 and not self . cells [ self . xPOM ] [ self . yPOM + 1 ] . blocking_movement :
possible_moves . append ( ( self . directionPOM , ' forward ' ) )
elif self . directionPOM == 1 : # Patrzy w lewo
possible_moves . append ( ( 1 , ' left ' ) )
possible_moves . append ( ( 1 , ' right ' ) )
if self . xPOM > 0 and not self . cells [ self . xPOM - 1 ] [ self . yPOM ] . blocking_movement :
possible_moves . append ( ( self . directionPOM , ' forward ' ) )
elif self . directionPOM == 2 : # Patrzy w górę
possible_moves . append ( ( 2 , ' left ' ) )
possible_moves . append ( ( 2 , ' right ' ) )
if self . yPOM > 0 and not self . cells [ self . xPOM ] [ self . yPOM - 1 ] . blocking_movement :
possible_moves . append ( ( self . directionPOM , ' forward ' ) )
elif self . directionPOM == 3 : # Patrzy w prawo
possible_moves . append ( ( 3 , ' left ' ) )
possible_moves . append ( ( 3 , ' right ' ) )
if self . xPOM < prefs . GRID_SIZE - 1 and not self . cells [ self . xPOM + 1 ] [ self . yPOM ] . blocking_movement :
possible_moves . append ( ( self . directionPOM , ' forward ' ) )
return possible_moves
def calculate_priority ( self , el ) :
return el [ 0 ]
def bfs2 ( self , target_x , target_y ) :
visited = set ( )
self . directionPOM = self . direction
start_state = ( self . current_cell . X , self . current_cell . Y , self . directionPOM )
#print(start_state)
queue = [ ]
heapq . heappush ( queue , ( 0 , ( start_state , [ ] , 0 ) ) )
while queue :
_ , que = heapq . heappop ( queue )
state , actions , gscore = que
self . xPOM , self . yPOM , self . directionPOM = state
if self . xPOM == target_x and self . yPOM == target_y :
return actions
if ( self . xPOM , self . yPOM , self . directionPOM ) in visited :
continue
visited . add ( ( self . xPOM , self . yPOM , self . directionPOM ) )
possible_moves = self . get_possible_moves ( )
for new_direction , action in possible_moves :
new_x , new_y = self . xPOM , self . yPOM
new_actions = actions + [ action ]
if action == ' left ' :
new_direction = ( self . directionPOM + 1 ) % 4
elif action == ' right ' :
new_direction = ( self . directionPOM - 1 ) % 4
else : # forward
if self . directionPOM == 0 :
new_y + = 1
elif self . directionPOM == 1 :
new_x - = 1
elif self . directionPOM == 2 :
new_y - = 1
else : # direction == 3
new_x + = 1
if 0 < = new_x < prefs . GRID_SIZE and 0 < = new_y < prefs . GRID_SIZE \
and not self . cells [ new_x ] [ new_y ] . blocking_movement :
new_state = ( new_x , new_y , new_direction )
if ( action == ' left ' or action == ' right ' ) :
gscore = gscore + 1
else :
gscore = gscore + self . cells [ new_x ] [ new_y ] . waga
f_score = gscore + self . heuristic ( ( new_x , new_y ) , ( target_x , target_y ) )
heapq . heappush ( queue , ( f_score , ( new_state , new_actions , gscore ) ) )
return [ ]
def heuristic ( self , current , target ) :
# Manhattan distance heuristic
dx = abs ( current [ 0 ] - target [ 0 ] )
dy = abs ( current [ 1 ] - target [ 1 ] )
return dx + dy
2024-04-11 13:30:21 +02:00