// File: 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
subq $0x1b0, %rsp            # imm = 0x1B0
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x188(%rbp)
movq 0x50(%rax), %rdi
callq 0x3510
movl %eax, %ecx
movq -0x188(%rbp), %rax
movl %ecx, -0x2c(%rbp)
movq 0x50(%rax), %rdi
callq 0x3530
movl %eax, -0x30(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x22c0
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x8ff6
movl -0x5c(%rbp), %eax
cmpl -0x20(%rbp), %eax
je 0x8fe6
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x8fe6
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rdi
callq 0x3510
movl %eax, -0x18c(%rbp)
jmp 0x8f4a
movl -0x18c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x8fe4
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x50(%rax), %rdi
callq 0x3530
movl %eax, -0x190(%rbp)
jmp 0x8f76
movl -0x190(%rbp), %eax
cmpl -0x30(%rbp), %eax
jne 0x8fe4
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x8930
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x90(%rbp), %rdi
callq 0x88d0
jmp 0x8faf
leaq -0x58(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2260
leaq -0x90(%rbp), %rdi
callq 0x2100
movl -0x5c(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x8ff6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x964e
jmp 0x8fe6
jmp 0x8fe8
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x8f02
movq -0x188(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x8900
jmp 0x900b
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x8900
jmp 0x9025
cmpl $0x3, -0x34(%rbp)
jle 0x91ad
movq -0x188(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0x88d0
jmp 0x9044
leaq 0x79f9(%rip), %rsi      # 0x10a44
leaq -0xf0(%rbp), %rdi
callq 0x97e0
movb %al, -0x191(%rbp)
jmp 0x905f
leaq -0xf0(%rbp), %rdi
callq 0x2100
movb -0x191(%rbp), %al
testb $0x1, %al
jne 0x9077
jmp 0x90c4
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x91ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x9642
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x9636
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x2100
jmp 0x9636
movq -0x188(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x88d0
jmp 0x90d9
leaq 0x796b(%rip), %rsi      # 0x10a4b
leaq -0x110(%rbp), %rdi
callq 0x97e0
movb %al, -0x192(%rbp)
jmp 0x90f4
leaq -0x110(%rbp), %rdi
callq 0x2100
movb -0x192(%rbp), %al
testb $0x1, %al
jne 0x910c
jmp 0x9137
movl -0x34(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x34(%rbp)
jmp 0x91a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x2100
jmp 0x9636
movq -0x188(%rbp), %rsi
leaq -0x130(%rbp), %rdi
callq 0x88d0
jmp 0x914c
leaq 0x78ff(%rip), %rsi      # 0x10a52
leaq -0x130(%rbp), %rdi
callq 0x97e0
movb %al, -0x193(%rbp)
jmp 0x9167
leaq -0x130(%rbp), %rdi
callq 0x2100
movb -0x193(%rbp), %al
testb $0x1, %al
jne 0x917f
jmp 0x91a7
movl -0x34(%rbp), %eax
subl $0x3, %eax
movl %eax, -0x34(%rbp)
jmp 0x91a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x2100
jmp 0x9636
jmp 0x91a9
jmp 0x91ab
jmp 0x91b4
movl $0x0, -0x34(%rbp)
movq -0x188(%rbp), %rax
cmpl $0x3, 0x4(%rax)
jle 0x9279
leaq 0x7878(%rip), %rsi      # 0x10a44
leaq -0x58(%rbp), %rdi
callq 0x97e0
movb %al, -0x194(%rbp)
jmp 0x91dd
movb -0x194(%rbp), %al
testb $0x1, %al
jne 0x91e9
jmp 0x91fb
movq -0x188(%rbp), %rax
movl 0x4(%rax), %ecx
subl $0x1, %ecx
movl %ecx, 0x4(%rax)
jmp 0x9277
movq -0x188(%rbp), %rdi
addq $0x30, %rdi
leaq 0x783e(%rip), %rsi      # 0x10a4b
callq 0x97e0
movb %al, -0x195(%rbp)
jmp 0x921a
movb -0x195(%rbp), %al
testb $0x1, %al
jne 0x9226
jmp 0x9238
movq -0x188(%rbp), %rax
movl 0x4(%rax), %ecx
subl $0x2, %ecx
movl %ecx, 0x4(%rax)
jmp 0x9275
movq -0x188(%rbp), %rdi
addq $0x30, %rdi
leaq 0x7808(%rip), %rsi      # 0x10a52
callq 0x97e0
movb %al, -0x196(%rbp)
jmp 0x9257
movb -0x196(%rbp), %al
testb $0x1, %al
jne 0x9263
jmp 0x9273
movq -0x188(%rbp), %rax
movl 0x4(%rax), %ecx
subl $0x3, %ecx
movl %ecx, 0x4(%rax)
jmp 0x9275
jmp 0x9277
jmp 0x9287
movq -0x188(%rbp), %rax
movl $0x0, 0x4(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x2180
movl %eax, %edi
callq 0x2190
callq 0x2070
movl %eax, %ecx
movslq %ecx, %rax
imulq $0x66666667, %rax, %rdx # imm = 0x66666667
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x22, %rdx
addl %eax, %edx
addl %edx, %edx
movl %edx, %eax
leal (%rax,%rax,4), %edx
movl %ecx, %eax
negl %edx
movl %edx, %ecx
leal 0x1(%rax,%rcx), %eax
movl %eax, -0x134(%rbp)
movl -0x134(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x1a0(%rbp)
subl $0x5, %eax
ja 0x934e
movq -0x1a0(%rbp), %rax
leaq 0x76ec(%rip), %rcx      # 0x109dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x188(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x4, %ecx
movl %ecx, 0x4(%rax)
jmp 0x934e
movq -0x188(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x3, %ecx
movl %ecx, 0x4(%rax)
jmp 0x934e
movq -0x188(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x4(%rax)
jmp 0x934e
movl -0x34(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x34(%rbp)
jmp 0x934e
movl -0x34(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x34(%rbp)
jmp 0x934e
movl -0x34(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1a8(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x8950
movq -0x1a8(%rbp), %rdi
movl %eax, %esi
movq -0x188(%rbp), %rax
subl 0x4(%rax), %esi
callq 0x8980
movq -0x188(%rbp), %rdi
movl (%rdi), %esi
subl -0x34(%rbp), %esi
callq 0x8980
movl $0x0, -0x138(%rbp)
cmpl $0x28, -0x138(%rbp)
jge 0x93d6
movq 0xac1b(%rip), %rdi      # 0x13fd0
leaq 0x75bd(%rip), %rsi      # 0x10979
callq 0x21a0
jmp 0x93c3
jmp 0x93c5
movl -0x138(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x138(%rbp)
jmp 0x93a5
movl $0xffffffff, -0x13c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x140(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x89a0
testb $0x1, %al
jne 0x94cd
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x89a0
testb $0x1, %al
jne 0x94cd
callq 0x2070
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %edx
movl %edx, -0x134(%rbp)
cmpl $0x1, -0x134(%rbp)
jne 0x949e
movq -0x188(%rbp), %rdi
movl $0x1, %esi
callq 0x8980
movl $0x1, -0x13c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
callq 0x8e20
jmp 0x946d
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, 0x50(%rax)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x94c8
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x1, %esi
callq 0x8980
movl $0x2, -0x13c(%rbp)
movl $0x2, -0x140(%rbp)
jmp 0x955d
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x89a0
testb $0x1, %al
jne 0x94f6
movl $0x0, -0x13c(%rbp)
movl $0x1, -0x140(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x89a0
testb $0x1, %al
jne 0x955b
movl $0x3, -0x13c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
callq 0x8e20
jmp 0x952c
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, 0x50(%rax)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x955d
leaq -0x160(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x20b0
jmp 0x9572
leaq -0x180(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x20b0
jmp 0x9587
movq -0x188(%rbp), %rax
movl 0x4(%rax), %edx
movl -0x34(%rbp), %ecx
movl -0x13c(%rbp), %r8d
leaq -0x160(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x79b0
jmp 0x95b0
leaq -0x180(%rbp), %rdi
callq 0x2100
leaq -0x160(%rbp), %rdi
callq 0x2100
movl -0x140(%rbp), %eax
movl %eax, -0x1ac(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x2100
leaq -0xb0(%rbp), %rdi
callq 0x2100
leaq -0x58(%rbp), %rdi
callq 0x2100
movl -0x1ac(%rbp), %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x962a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x2100
leaq -0x160(%rbp), %rdi
callq 0x2100
leaq -0xd0(%rbp), %rdi
callq 0x2100
leaq -0xb0(%rbp), %rdi
callq 0x2100
leaq -0x58(%rbp), %rdi
callq 0x2100
movq -0x68(%rbp), %rdi
callq 0x2320