// File: /workspace/github_cpp_makefile_star2_O3/giovifazi[P]knossos-bloodbath/ClassPlayer.cpp
// Function: Player::engage_battle(Player**, Map&, int, int, Node&, int&)
int Player::engage_battle(Player* p[], Map& map, int np, int p1_index, Node& node, int& alive_players)
{
   int x1 = pos->get_x(), y1 = pos->get_y();    // Coordinate di dove avviene lo scontro
   
   int atk2, p2_index;
   std::string shield2;

    
   // Cerco il giocatore nelle stesse coordinate con cui combattere
   for (int i=0; i < np; i++)
      if (i != p1_index && p[i] != NULL)                             // Escludo il giocatore attaccante e i morti dal controllo
         if (p[i]->pos->get_x() == x1 and p[i]->pos->get_y() == y1)
         {
            // salvo le variabili del giocatore che viene attaccato ed esco dal ciclo
            atk2 = p[i]->getAtk();
            shield2 = p[i]->getShield();
            p2_index = i;
            break;
         }

   // prendo i nomi dei giocatori per la stampa dell HUD
   std::string name_p1=getName();
   std::string name_p2=p[p2_index]->getName();		


   // Calcolo la formula per il combattimento (atk1-scudo2 e atk2-scudo1)
   // Sottraggo all attacco di un giocatore i valori dello scudo dell altro
   if (atk2 > 3)
   {
      if (getShield() == "bronze")
         atk2 -= 1;
      else
      if (getShield() == "silver")
         atk2 -= 2;
      else
      if (getShield() == "gold")
         atk2 -= 3;
   }
   else
      atk2 = 0;

   if (atk > 3)
   {
      if (shield2 == "bronze")
         atk -= 1;
      else
      if (shield == "silver")
         atk -= 2;
      else
      if (shield == "gold")
         atk -= 3;
   }
   else
      atk = 0;


   // Tiro il dado per randomizzare lo scontro, aumentanto a caso atk e atk2
   srand(time(0));
   int roll = (rand()%10) +1;
   
   switch (roll)
   {
      case 1:
         atk += 4;
         break;

      case 2:
         atk += 3;
         break;

      case 3:
         atk += 2;
         break;

      case 4:
         atk2 += 4;
         break;

      case 5:
         atk2 += 3;
         break;

      case 6:
         atk2 += 2;
         break;
   }
   

   // Infliggo i rispettivi danni
   p[p2_index]->set_hp(p[p2_index]->getHp() - atk);

   set_hp(hp - atk2);

   // Stampo su schermo i danni inflitti
   for(int j=0; j<40; j++)
      std::cout << "\n";

   // Variabile per stampare nell hud la morte di entrambi i giocatori
   int j=-1;	

   // nel caso muoiano entrambi i giocatori ritorna 1 se il p1 resuscita, 
   // 2 se resuscita p2
   int morte=0;

   // Controlla giocatori uccisi, impedisce la morte di entrambi
   if (!p[p2_index]->is_alive() and !p[p1_index]->is_alive())
   {
      // In caso siano entrambi morti, sceglie a caso chi riportare in vita
      // (con hp = 1)
      roll = (rand()%2) +1;

      if (roll == 1)
      {
         set_hp(1);
         j=1;

         p[p2_index]->remove_from_map(map);
         p[p2_index]->pos = NULL;
         p[p2_index] = NULL;
         alive_players--;  
 
      }
      else
      {
         p[p2_index]->set_hp(1);
         j=2;
         morte=2;
        
      }
   }
   else {
   // Visto che se muore il giocatore due, (cioe` il giocatore che viene
   // attaccato) non essendo il suo turno, non viene effettuato il controllo 
   // sulla sua morte nel main (essendo il turno del giocatore 1),
   // e se non viene rimosso, rimane sulla mappa con gli hp negativi

                if (!p[p1_index]->is_alive()){ j= 0; morte=1;}  //controllo se e' morto il giocatore che attacca e stampa

                if (!p[p2_index]->is_alive())
                {
                        j=3;

                        p[p2_index]->remove_from_map(map);
                        p[p2_index]->pos = NULL;
                        p[p2_index] = NULL;
                        alive_players--;    
                }
        }

   // Stampa grafica
  hud_battle(name_p1, name_p2, atk, atk2, j);
  
  return morte;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %r8d, %r14d
movl %ecx, %r12d
movq %rdx, -0x78(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdi, %rbx
movq 0x50(%rdi), %rdi
callq 0x2dc2
movl %eax, %r13d
movq 0x50(%rbx), %rdi
callq 0x2dcc
movl %eax, %r15d
leaq -0xc8(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testl %r12d, %r12d
movq %rbx, -0x40(%rbp)
movl %r14d, -0x6c(%rbp)
jle 0x7af2
movl %r12d, %ebx
movl %r14d, %r14d
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x7add
movq -0x30(%rbp), %rax
movq (%rax,%r12,8), %rax
testq %rax, %rax
je 0x7add
movq 0x50(%rax), %rdi
callq 0x2dc2
cmpl %r13d, %eax
jne 0x7add
movq -0x30(%rbp), %rax
movq (%rax,%r12,8), %rax
movq 0x50(%rax), %rdi
callq 0x2dcc
cmpl %r15d, %eax
je 0x7e46
incq %r12
cmpq %r12, %rbx
jne 0x7aa3
movl %ebx, %r12d
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rbx
jmp 0x7af9
xorl %r12d, %r12d
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %r15
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x2c70
movl %r12d, %r12d
movq (%r15,%r12,8), %rax
leaq -0xa8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
leaq -0xb8(%rbp), %rdi
callq 0x2c70
xorl %r13d, %r13d
movl $0x0, %r14d
cmpl $0x4, -0x38(%rbp)
movq %r12, -0x48(%rbp)
jl 0x7c87
leaq -0x88(%rbp), %r14
movq %r14, -0x10(%r14)
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
addq %rsi, %rdx
leaq -0x98(%rbp), %rdi
callq 0x2c70
leaq 0x6d8e(%rip), %rsi      # 0xe910
leaq -0x98(%rbp), %r12
movq %r12, %rdi
callq 0x2050
movl %eax, %r15d
movq (%r12), %rdi
cmpq %r14, %rdi
je 0x7bac
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2120
testl %r15d, %r15d
movq -0x48(%rbp), %r12
je 0x7c71
leaq -0x98(%rbp), %rdi
movq %r14, (%rdi)
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
addq %rsi, %rdx
callq 0x2c70
leaq 0x6d3d(%rip), %rsi      # 0xe917
leaq -0x98(%rbp), %r12
movq %r12, %rdi
callq 0x2050
movl %eax, %r15d
movq (%r12), %rdi
cmpq %r14, %rdi
je 0x7c04
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2120
testl %r15d, %r15d
movq -0x48(%rbp), %r12
je 0x7c79
leaq -0x98(%rbp), %rdi
movq %r14, (%rdi)
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
addq %rsi, %rdx
callq 0x2c70
leaq 0x6cf0(%rip), %rsi      # 0xe91e
leaq -0x98(%rbp), %r12
movq %r12, %rdi
callq 0x2050
movl %eax, %r15d
movq (%r12), %rdi
cmpq %r14, %rdi
je 0x7c58
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x38(%rbp), %rax
leal -0x3(%rax), %r14d
testl %r15d, %r15d
cmovnel %eax, %r14d
movq -0x30(%rbp), %r15
movq -0x48(%rbp), %r12
jmp 0x7c87
movq -0x38(%rbp), %rax
decl %eax
jmp 0x7c80
movq -0x38(%rbp), %rax
addl $-0x2, %eax
movl %eax, %r14d
movq -0x30(%rbp), %r15
cmpl $0x4, 0x4(%rbx)
jl 0x7cf1
leaq 0x6c7c(%rip), %rsi      # 0xe910
leaq -0xd8(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x7cdc
leaq 0x30(%rbx), %r15
leaq 0x6c68(%rip), %rsi      # 0xe917
movq %r15, %rdi
callq 0x2050
testl %eax, %eax
je 0x7ce5
leaq 0x6c5c(%rip), %rsi      # 0xe91e
movq %r15, %rdi
callq 0x2050
testl %eax, %eax
movq -0x30(%rbp), %r15
jne 0x7cf5
movl 0x4(%rbx), %r13d
addl $-0x3, %r13d
jmp 0x7cf1
movl 0x4(%rbx), %r13d
decl %r13d
jmp 0x7cf1
movl 0x4(%rbx), %r13d
addl $-0x2, %r13d
movq -0x30(%rbp), %r15
movl %r13d, 0x4(%rbx)
xorl %edi, %edi
callq 0x20e0
movl %eax, %edi
callq 0x20f0
callq 0x2040
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
cmpl $0x5, %eax
ja 0x7d5c
leaq 0x6b77(%rip), %rcx      # 0xe8a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addl $0x4, 0x4(%rbx)
jmp 0x7d5c
addl $0x3, %r14d
jmp 0x7d5c
addl $0x2, 0x4(%rbx)
jmp 0x7d5c
addl $0x4, %r14d
jmp 0x7d5c
addl $0x3, 0x4(%rbx)
jmp 0x7d5c
addl $0x2, %r14d
movq (%r15,%r12,8), %rax
movl 0x4(%rbx), %ecx
subl %ecx, (%rax)
movl %r14d, -0x38(%rbp)
subl %r14d, (%rbx)
movq 0x10(%rbp), %r13
movl $0x28, %ebx
movq 0xa254(%rip), %r15      # 0x11fd0
leaq 0x6ad2(%rip), %r12      # 0xe855
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2140
decl %ebx
jne 0x7d83
movq -0x30(%rbp), %r15
movq -0x48(%rbp), %r14
movq (%r15,%r14,8), %rdi
cmpl $0x0, (%rdi)
movslq -0x6c(%rbp), %rax
movq (%r15,%rax,8), %rax
movl (%rax), %eax
jle 0x7dce
leaq -0x58(%rbp), %rbx
xorl %r15d, %r15d
xorl %r14d, %r14d
testl %eax, %eax
setle %r15b
setg %r14b
negl %r14d
jmp 0x7eb7
testl %eax, %eax
jle 0x7e00
leaq -0x58(%rbp), %rbx
movq -0x78(%rbp), %rsi
callq 0x79e2
movq (%r15,%r14,8), %rax
movq %r15, %rcx
xorl %r15d, %r15d
movq %r15, 0x50(%rax)
movq %r15, (%rcx,%r14,8)
decl (%r13)
movl $0x3, %r14d
jmp 0x7eb7
leaq -0x58(%rbp), %rbx
callq 0x2040
testb $0x1, %al
jne 0x7ea1
movq -0x40(%rbp), %rax
movl $0x1, (%rax)
movq (%r15,%r14,8), %rdi
movq -0x78(%rbp), %rsi
callq 0x79e2
movq (%r15,%r14,8), %rax
movq %r15, %rcx
xorl %r15d, %r15d
movq %r15, 0x50(%rax)
movq %r15, (%rcx,%r14,8)
decl (%r13)
movl $0x1, %r14d
jmp 0x7eb7
movq -0x30(%rbp), %rax
movq (%rax,%r12,8), %rax
movl 0x4(%rax), %ecx
movq %rcx, -0x38(%rbp)
leaq -0x58(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x30(%rax), %rsi
movq 0x38(%rax), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x2c70
leaq -0xd8(%rbp), %rdi
leaq -0x68(%rbp), %r15
movq %r15, %rsi
callq 0x2170
movq (%r15), %rdi
cmpq %rbx, %rdi
je 0x7aec
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2120
jmp 0x7aec
movq (%r15,%r14,8), %rax
movl $0x1, (%rax)
movl $0x2, %r14d
movl $0x2, %r15d
leaq -0x108(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x118(%rbp), %rdi
callq 0x2c70
leaq -0xe8(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf8(%rbp), %rdi
callq 0x2c70
movq -0x40(%rbp), %rax
movl 0x4(%rax), %edx
leaq -0x118(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
movl -0x38(%rbp), %ecx
movl %r14d, %r8d
callq 0x63e0
movq -0xf8(%rbp), %rdi
cmpq %r13, %rdi
je 0x7f3d
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x118(%rbp), %rdi
cmpq %r12, %rdi
je 0x7f58
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x7f7a
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x7f8f
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x7fb1
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x2120
movl %r15d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x801b
jmp 0x8020
jmp 0x8020
jmp 0x8020
movq %rax, %r14
movq -0xf8(%rbp), %rdi
cmpq %r13, %rdi
je 0x7ff1
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x2120
jmp 0x7ff1
movq %rax, %r14
movq -0x118(%rbp), %rdi
cmpq %r12, %rdi
je 0x8027
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2120
jmp 0x8027
leaq -0x58(%rbp), %rbx
movq %rax, %r14
jmp 0x8049
jmp 0x801b
jmp 0x8024
movq %rax, %r14
jmp 0x805e
leaq -0x58(%rbp), %rbx
movq %rax, %r14
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x8049
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x68(%rbp), %rdi
cmpq %rbx, %rdi
je 0x805e
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x8080
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x2120
movq %r14, %rdi
callq 0x2210