// File: hobbitsyfeet[P]Turn/unit.cc
// Function: Unit::attackUnit(Unit&)
void Unit::attackUnit(Unit& target){
  int totalDamage = (this->getAttack()-target.getDefence());
  Item* equipmentUsed;

  //if in range, engage
  if(this->getRange() >= distanceTo( target.getPosX(),target.getPosY() ) ){
		cout<<"inrange"<<endl;
    //if not dead, attack
    if(target.getHealth() > totalDamage){
			cout<<"target not dead"<<endl;
      if(inv.searchWeaponEquipped()){
        //getEquipped Weapon
        equipmentUsed = &inv.getItemEquipped();
				cout<<"i got a weapon"<<endl;
        //if weapon's durability
        if(equipmentUsed->itemGetDurability() > 0){
        //if the total damage is positive
				cout<<"my damage is greater than his defence"<<endl;
         if(totalDamage > 0){
          //do damage
					cout<<"target hit!"<<endl;
              target.health -= totalDamage;
              equipmentUsed->decrementDurability();
          //decrement item's durability
          }
          //no damage inflicted
          else{
          cout<<this->getName()<<" dealt 0 damage to"<< target.getName();
          }
        }
        //the item is broken
        else{
          totalDamage = 1;
          cout<<equipmentUsed->itemGetName()<<" is broken! "<<endl;
          target.health -= totalDamage;
       }
         //show results
        cout<<this->getName()<<" dealt "
	       <<totalDamage
	       <<" damage to "<<target.getName();
      }
       //save b/c stat change
      target.saveUnit(target.getName());
   }

    //they are dead, announce and do no action
    else{
      if(target.getHealth() != 0){
				target.saveUnit(target.getName());
        target.health = 0;
        experience += target.getExp();
        cout<<"you killed "<<target.getName()<<" and gained"<<target.getExp()<<" experience."<<endl;
        if(experience >= maxExperience)
          this->levelUp();
      }
      else
        cout<<target.getName()<<" is dead";
    }
  }
  else{
    cout<<"Not in range";
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x5180
movl %eax, -0x164(%rbp)
movq -0x10(%rbp), %rdi
callq 0x51c0
movq -0x160(%rbp), %rdi
movl %eax, %ecx
movl -0x164(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
callq 0x51a0
movl %eax, -0x154(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5260
movl %eax, -0x158(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5240
movq -0x160(%rbp), %rdi
movl -0x158(%rbp), %esi
movl %eax, %edx
callq 0x5c70
movl %eax, %ecx
movl -0x154(%rbp), %eax
cmpl %ecx, %eax
jl 0x5c48
movq 0x1396c(%rip), %rdi     # 0x18fd0
leaq 0xcaa1(%rip), %rsi      # 0x1210c
callq 0x3280
movq %rax, %rdi
movq 0x13946(%rip), %rsi     # 0x18fc0
callq 0x32b0
movq -0x10(%rbp), %rdi
callq 0x5140
cmpl -0x14(%rbp), %eax
jle 0x5a3f
movq 0x13938(%rip), %rdi     # 0x18fd0
leaq 0xca75(%rip), %rsi      # 0x12114
callq 0x3280
movq %rax, %rdi
movq 0x13912(%rip), %rsi     # 0x18fc0
callq 0x32b0
movq -0x160(%rbp), %rdi
addq $0x80, %rdi
callq 0x103e0
testb $0x1, %al
jne 0x56cf
jmp 0x59de
movq -0x160(%rbp), %rdi
addq $0x80, %rdi
callq 0x10790
movq %rax, -0x20(%rbp)
movq 0x138e3(%rip), %rdi     # 0x18fd0
leaq 0xca30(%rip), %rsi      # 0x12124
callq 0x3280
movq %rax, %rdi
movq 0x138bd(%rip), %rsi     # 0x18fc0
callq 0x32b0
movq -0x20(%rbp), %rdi
callq 0x8850
cmpl $0x0, %eax
jle 0x583b
movq 0x138af(%rip), %rdi     # 0x18fd0
leaq 0xca0b(%rip), %rsi      # 0x12133
callq 0x3280
movq %rax, %rdi
movq 0x13889(%rip), %rsi     # 0x18fc0
callq 0x32b0
cmpl $0x0, -0x14(%rbp)
jle 0x5781
movq 0x13887(%rip), %rdi     # 0x18fd0
leaq 0xca09(%rip), %rsi      # 0x12159
callq 0x3280
movq %rax, %rdi
movq 0x13861(%rip), %rsi     # 0x18fc0
callq 0x32b0
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movl 0x20(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x20(%rax)
movq -0x20(%rbp), %rdi
callq 0x8a40
jmp 0x5836
movq -0x160(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x50d0
movq -0x178(%rbp), %rsi
movq 0x1382a(%rip), %rdi     # 0x18fd0
callq 0x3270
movq %rax, -0x170(%rbp)
jmp 0x57b4
movq -0x170(%rbp), %rdi
leaq 0xc9a3(%rip), %rsi      # 0x12165
callq 0x3280
movq %rax, -0x180(%rbp)
jmp 0x57d0
movq -0x10(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x50d0
jmp 0x57df
movq -0x180(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x3270
jmp 0x57f1
leaq -0x70(%rbp), %rdi
callq 0x31c0
leaq -0x40(%rbp), %rdi
callq 0x31c0
jmp 0x5836
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x5828
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31c0
leaq -0x40(%rbp), %rdi
callq 0x31c0
jmp 0x5c64
jmp 0x58e0
movl $0x1, -0x14(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x8770
movq -0x190(%rbp), %rsi
movq 0x13769(%rip), %rdi     # 0x18fd0
callq 0x3270
movq %rax, -0x188(%rbp)
jmp 0x5875
movq -0x188(%rbp), %rdi
leaq 0xc8f5(%rip), %rsi      # 0x12178
callq 0x3280
movq %rax, -0x198(%rbp)
jmp 0x5891
movq -0x198(%rbp), %rdi
movq 0x13721(%rip), %rsi     # 0x18fc0
callq 0x32b0
jmp 0x58a6
leaq -0x90(%rbp), %rdi
callq 0x31c0
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movl 0x20(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x20(%rax)
jmp 0x58e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x31c0
jmp 0x5c64
movq -0x160(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x50d0
movq -0x1a8(%rbp), %rsi
movq 0x136c8(%rip), %rdi     # 0x18fd0
callq 0x3270
movq %rax, -0x1a0(%rbp)
jmp 0x5916
movq -0x1a0(%rbp), %rdi
leaq 0xc861(%rip), %rsi      # 0x12185
callq 0x3280
movq %rax, -0x1b0(%rbp)
jmp 0x5932
movq -0x1b0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x3430
movq %rax, -0x1b8(%rbp)
jmp 0x594a
movq -0x1b8(%rbp), %rdi
leaq 0xc835(%rip), %rsi      # 0x1218d
callq 0x3280
movq %rax, -0x1c0(%rbp)
jmp 0x5966
movq -0x10(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x50d0
jmp 0x5978
movq -0x1c0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3270
jmp 0x598d
leaq -0xd0(%rbp), %rdi
callq 0x31c0
leaq -0xb0(%rbp), %rdi
callq 0x31c0
jmp 0x59de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x59cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x31c0
leaq -0xb0(%rbp), %rdi
callq 0x31c0
jmp 0x5c64
movq -0x10(%rbp), %rsi
movq %rsi, -0x1d0(%rbp)
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x50d0
movq -0x1d0(%rbp), %rdi
movq -0x1c8(%rbp), %rsi
callq 0x5d30
jmp 0x5a11
leaq -0xf0(%rbp), %rdi
callq 0x31c0
jmp 0x5c46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x31c0
jmp 0x5c64
movq -0x10(%rbp), %rdi
callq 0x5140
cmpl $0x0, %eax
je 0x5bd4
movq -0x10(%rbp), %rsi
movq %rsi, -0x1e0(%rbp)
leaq -0x110(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x50d0
movq -0x1e0(%rbp), %rdi
movq -0x1d8(%rbp), %rsi
callq 0x5d30
jmp 0x5a84
leaq -0x110(%rbp), %rdi
callq 0x31c0
movq -0x10(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x10(%rbp), %rdi
callq 0x5220
movl %eax, %edx
movq -0x160(%rbp), %rax
movl 0x40(%rax), %ecx
addl %edx, %ecx
movl %ecx, 0x40(%rax)
movq 0x13514(%rip), %rdi     # 0x18fd0
leaq 0xc6d6(%rip), %rsi      # 0x12199
callq 0x3280
movq %rax, -0x1f8(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x130(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x50d0
movq -0x1f8(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x3270
movq %rax, -0x1e8(%rbp)
jmp 0x5b02
movq -0x1e8(%rbp), %rdi
leaq 0xc695(%rip), %rsi      # 0x121a5
callq 0x3280
movq %rax, -0x200(%rbp)
jmp 0x5b1e
movq -0x10(%rbp), %rdi
callq 0x5220
movq -0x200(%rbp), %rdi
movl %eax, %esi
callq 0x3430
movq %rax, -0x208(%rbp)
jmp 0x5b3e
movq -0x208(%rbp), %rdi
leaq 0xc665(%rip), %rsi      # 0x121b1
callq 0x3280
movq %rax, -0x210(%rbp)
jmp 0x5b5a
movq -0x210(%rbp), %rdi
movq 0x13458(%rip), %rsi     # 0x18fc0
callq 0x32b0
jmp 0x5b6f
leaq -0x130(%rbp), %rdi
callq 0x31c0
movq -0x160(%rbp), %rcx
movl 0x40(%rcx), %eax
cmpl 0x3c(%rcx), %eax
jl 0x5bd2
movq -0x160(%rbp), %rdi
callq 0x52e0
jmp 0x5bd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x31c0
jmp 0x5c64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x31c0
jmp 0x5c64
jmp 0x5c44
movq -0x10(%rbp), %rsi
leaq -0x150(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x50d0
movq -0x220(%rbp), %rsi
movq 0x133d7(%rip), %rdi     # 0x18fd0
callq 0x3270
movq %rax, -0x218(%rbp)
jmp 0x5c07
movq -0x218(%rbp), %rdi
leaq 0xc5a9(%rip), %rsi      # 0x121be
callq 0x3280
jmp 0x5c1c
leaq -0x150(%rbp), %rdi
callq 0x31c0
jmp 0x5c44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x31c0
jmp 0x5c64
jmp 0x5c46
jmp 0x5c5b
movq 0x13381(%rip), %rdi     # 0x18fd0
leaq 0xc571(%rip), %rsi      # 0x121c7
callq 0x3280
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3440
nopl (%rax)