Advertisement
nicoviale_

Untitled

Jan 26th, 2024
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. /**
  2.  * @brief Logica del processo del singolo coccodrillo
  3.  *
  4.  * @param arg puntatore alla struttura ThreadArgs di inizializzazione
  5.  */
  6. void* croc_logic(void* arg){
  7.     int steps=0, phase_steps=0;
  8.     bool next_phase=false;
  9.  
  10.     ThreadArgs thread_args = *((ThreadArgs*)arg);
  11.  
  12.     //spacchettamento argomento
  13.     WINDOW* win   = thread_args.win;
  14.     Obj croc      = thread_args.obj;
  15.     int delay     = thread_args.delay;
  16.     Direction direction = thread_args.direction;
  17.  
  18.     //id del thread
  19.     croc.tid = pthread_self();
  20.  
  21.     //prima inizializzazione passi
  22.     switch(croc.id){
  23.         case BAD_CROC_L:
  24.         case BAD_CROC_R:
  25.             phase_steps = rand_range(MIN_BAD_CROC_STEPS, MAX_BAD_CROC_STEPS);
  26.             break;
  27.         default:
  28.             phase_steps = EMERGING_STEPS;
  29.             break;
  30.     }
  31.  
  32.     //ciclo di generazione delle coordinate
  33.     while(true){
  34.        
  35.         //spostamento
  36.         croc.x += (direction==LEFT ? -1 : 1);
  37.        
  38.         //se il coccodrillo e' totalmente fuori dalla window
  39.         if(obj_out_of_win(win, croc, BOXED)){
  40.            
  41.             //randomizza il fatto di essere buono o cattivo
  42.             croc.id = rand_croc_id(direction);
  43.  
  44.             //se e' fuori riporta al bordo corretto
  45.             croc.x = direction==LEFT ? getmaxx(win)-2 : 2-CROC_W;
  46.  
  47.             //se e' cattivo prepara l'immersione
  48.             switch(croc.id){
  49.                 case BAD_CROC_L:
  50.                 case BAD_CROC_R:
  51.                     phase_steps = rand_range(MIN_BAD_CROC_STEPS, MAX_BAD_CROC_STEPS);
  52.             }
  53.         }
  54.  
  55.         //cammina sino alla prossima fase
  56.         if(steps < phase_steps){
  57.             steps++;
  58.         }else{
  59.             steps = 0;
  60.             next_phase = true;
  61.         }
  62.  
  63.         //controllo di prossima fase
  64.         if(next_phase){
  65.             next_phase = false;
  66.  
  67.             //fasi di immersione ed emersione del coccodrillo
  68.             switch(croc.id){
  69.                 //* Inizia l'immersione
  70.                 case BAD_CROC_L:
  71.                 case BAD_CROC_R:
  72.                     phase_steps = SUBMERGING_STEPS;
  73.                     croc.id = (direction==LEFT) ? BAD_CROC_L_SUBMERGING : BAD_CROC_R_SUBMERGING;
  74.                     break;
  75.                 //* Immerso
  76.                 case BAD_CROC_L_SUBMERGING:
  77.                 case BAD_CROC_R_SUBMERGING:
  78.                     phase_steps = UNDER_WATER_STEPS;
  79.                     croc.id = BAD_CROC_SUBMERGED;
  80.                     break;
  81.                 //* Inizia l'emersione
  82.                 case BAD_CROC_SUBMERGED:
  83.                     phase_steps = UNDER_WATER_STEPS;
  84.                     croc.id = (direction==LEFT) ? GOOD_CROC_L_EMERGING : GOOD_CROC_R_EMERGING;
  85.                     break;
  86.                 //* Emerso
  87.                 case GOOD_CROC_L_EMERGING:
  88.                 case GOOD_CROC_R_EMERGING:
  89.                     phase_steps = EMERGING_STEPS;
  90.                     croc.id = (direction==LEFT) ? GOOD_CROC_L : GOOD_CROC_R;
  91.                     break;
  92.             }
  93.         }
  94.  
  95.         write_buffer(croc);
  96.         usleep(delay);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement