// File: /workspace/github_cpp_makefile_star2_O1/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 0x2daa
movl %eax, %r15d
movq %rbx, -0x68(%rbp)
movq 0x50(%rbx), %rdi
callq 0x2db4
movl %eax, %r13d
leaq -0xc8(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testl %r12d, %r12d
movl %r14d, -0x6c(%rbp)
jle 0x7abf
movl %r14d, %ebx
movl %r12d, %r14d
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0x7ab1
movq -0x30(%rbp), %rax
movq (%rax,%r12,8), %rax
testq %rax, %rax
je 0x7ab1
movq 0x50(%rax), %rdi
callq 0x2daa
cmpl %r15d, %eax
jne 0x7ab1
movq -0x30(%rbp), %rax
movq (%rax,%r12,8), %rax
movq 0x50(%rax), %rdi
callq 0x2db4
cmpl %r13d, %eax
je 0x7f1b
incq %r12
movq %rax, -0x38(%rbp)
cmpq %r12, %r14
jne 0x7a77
jmp 0x7ac6
xorl %r12d, %r12d
movq %rax, -0x38(%rbp)
leaq -0x50(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x68(%rbp), %rbx
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
leaq -0x60(%rbp), %rdi
callq 0x2c60
movl %r12d, %eax
movq -0x30(%rbp), %r15
movq %rax, -0x40(%rbp)
movq (%r15,%rax,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 0x2c60
xorl %r13d, %r13d
movl $0x0, %r12d
cmpl $0x4, -0x38(%rbp)
jl 0x7c3e
leaq -0x88(%rbp), %r14
movq %r14, -0x10(%r14)
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
addq %rsi, %rdx
leaq -0x98(%rbp), %rdi
callq 0x2c60
leaq 0x6dbd(%rip), %rsi      # 0xe910
leaq -0x98(%rbp), %r12
movq %r12, %rdi
callq 0x2050
movl %eax, %r15d
movq (%r12), %rdi
cmpq %r14, %rdi
je 0x7b7d
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2120
testl %r15d, %r15d
je 0x7c24
leaq -0x98(%rbp), %rdi
movq %r14, (%rdi)
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
addq %rsi, %rdx
callq 0x2c60
leaq 0x6d70(%rip), %rsi      # 0xe917
movq %r12, %rdi
callq 0x2050
movl %eax, %r15d
movq (%r12), %rdi
cmpq %r14, %rdi
je 0x7bca
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2120
testl %r15d, %r15d
je 0x7c2c
leaq -0x98(%rbp), %rdi
movq %r14, (%rdi)
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
addq %rsi, %rdx
callq 0x2c60
leaq 0x6d2e(%rip), %rsi      # 0xe91e
movq %r12, %rdi
callq 0x2050
movl %eax, %r15d
movq (%r12), %rdi
cmpq %r14, %rdi
je 0x7c13
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x38(%rbp), %rax
leal -0x3(%rax), %r12d
testl %r15d, %r15d
cmovnel %eax, %r12d
jmp 0x7c36
movq -0x38(%rbp), %rax
decl %eax
jmp 0x7c33
movq -0x38(%rbp), %rax
addl $-0x2, %eax
movl %eax, %r12d
movq -0x30(%rbp), %r15
leaq -0x50(%rbp), %r14
cmpl $0x4, 0x4(%rbx)
jl 0x7ca8
leaq 0x6cc5(%rip), %rsi      # 0xe910
leaq -0xd8(%rbp), %rdi
callq 0x2050
testl %eax, %eax
je 0x7c93
leaq 0x30(%rbx), %r15
leaq 0x6cb1(%rip), %rsi      # 0xe917
movq %r15, %rdi
callq 0x2050
testl %eax, %eax
je 0x7c9c
leaq 0x6ca5(%rip), %rsi      # 0xe91e
movq %r15, %rdi
callq 0x2050
testl %eax, %eax
movq -0x30(%rbp), %r15
jne 0x7cac
movl 0x4(%rbx), %r13d
addl $-0x3, %r13d
jmp 0x7ca8
movl 0x4(%rbx), %r13d
decl %r13d
jmp 0x7ca8
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 0x7d13
leaq 0x6bc0(%rip), %rcx      # 0xe8a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addl $0x4, 0x4(%rbx)
jmp 0x7d13
addl $0x3, %r12d
jmp 0x7d13
addl $0x2, 0x4(%rbx)
jmp 0x7d13
addl $0x4, %r12d
jmp 0x7d13
addl $0x3, 0x4(%rbx)
jmp 0x7d13
addl $0x2, %r12d
movq -0x40(%rbp), %rax
movq (%r15,%rax,8), %rax
movl 0x4(%rbx), %ecx
subl %ecx, (%rax)
movl %r12d, -0x38(%rbp)
subl %r12d, (%rbx)
movq 0x10(%rbp), %r13
movl $0x28, %ebx
movq 0xa299(%rip), %r15      # 0x11fd0
leaq 0x6b17(%rip), %r12      # 0xe855
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2140
decl %ebx
jne 0x7d3e
movq -0x30(%rbp), %r15
movq -0x40(%rbp), %rax
movq (%r15,%rax,8), %rdi
movl (%rdi), %eax
movslq -0x6c(%rbp), %rcx
testl %eax, %eax
jg 0x7d75
movq (%r15,%rcx,8), %rdx
cmpl $0x0, (%rdx)
jle 0x7ece
movq %r14, %rbx
movq (%r15,%rcx,8), %rcx
xorl %r12d, %r12d
movq %r15, %r14
xorl %r15d, %r15d
cmpl $0x0, (%rcx)
setle %r12b
setg %cl
testl %eax, %eax
jle 0x7d9b
movb %cl, %r15b
negl %r15d
jmp 0x7dc0
movq -0x78(%rbp), %rsi
callq 0x79b6
movq -0x40(%rbp), %rdx
movq (%r14,%rdx,8), %rax
xorl %ecx, %ecx
movq %rcx, 0x50(%rax)
movq %rcx, (%r14,%rdx,8)
decl (%r13)
movl $0x3, %r15d
leaq -0x108(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x118(%rbp), %rdi
callq 0x2c60
leaq -0xe8(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf8(%rbp), %rdi
callq 0x2c60
movq -0x68(%rbp), %rax
movl 0x4(%rax), %edx
leaq -0x118(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
movl -0x38(%rbp), %ecx
movl %r15d, %r8d
callq 0x63b2
movq -0xf8(%rbp), %rdi
cmpq %r13, %rdi
je 0x7e45
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x118(%rbp), %rdi
cmpq %r14, %rdi
je 0x7e60
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x7e82
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x60(%rbp), %rdi
cmpq %rbx, %rdi
je 0x7e97
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x7eb9
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x2120
movl %r12d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rbx
callq 0x2040
testb $0x1, %al
jne 0x7f76
movq -0x68(%rbp), %rax
movl $0x1, (%rax)
movq -0x40(%rbp), %rax
movq (%r15,%rax,8), %rdi
movq -0x78(%rbp), %rsi
callq 0x79b6
movq -0x40(%rbp), %rcx
movq (%r15,%rcx,8), %rax
xorl %r12d, %r12d
movq %r12, 0x50(%rax)
movq %r12, (%r15,%rcx,8)
decl (%r13)
movl $0x1, %r15d
jmp 0x7dc0
movq -0x30(%rbp), %rax
movq (%rax,%r12,8), %rax
movl 0x4(%rax), %ecx
movq %rcx, -0x38(%rbp)
leaq -0x50(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x30(%rax), %rsi
movq 0x38(%rax), %rdx
addq %rsi, %rdx
leaq -0x60(%rbp), %rdi
callq 0x2c60
leaq -0xd8(%rbp), %rdi
leaq -0x60(%rbp), %r15
movq %r15, %rsi
callq 0x2170
movq (%r15), %rdi
cmpq %rbx, %rdi
je 0x7ac6
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2120
jmp 0x7ac6
movq -0x40(%rbp), %rax
movq (%r15,%rax,8), %rax
movl $0x1, (%rax)
movl $0x2, %r15d
movl $0x2, %r12d
jmp 0x7dc0
jmp 0x7fed
jmp 0x7f9b
jmp 0x7f9b
leaq -0x50(%rbp), %rbx
jmp 0x7ff5
movq %rax, %r15
movq -0xf8(%rbp), %rdi
cmpq %r13, %rdi
je 0x7fc4
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x2120
jmp 0x7fc4
movq %rax, %r15
movq -0x118(%rbp), %rdi
cmpq %r14, %rdi
je 0x7ff8
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x2120
jmp 0x7ff8
movq %r14, %rbx
movq %rax, %r15
jmp 0x801a
jmp 0x7fed
jmp 0x7ff5
movq %rax, %r15
jmp 0x802f
movq %r14, %rbx
movq %rax, %r15
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x801a
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x60(%rbp), %rdi
cmpq %rbx, %rdi
je 0x802f
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x8051
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x2120
movq %r15, %rdi
callq 0x2210
nop