// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x2c(%rdi), %r15d
movl 0x30(%rdi), %r12d
movl 0x78(%rsi), %eax
movl 0x7c(%rsi), %ecx
subl 0x78(%rdi), %eax
imull %eax, %eax
subl 0x7c(%rdi), %ecx
imull %ecx, %ecx
addl %eax, %ecx
cvtsi2sd %ecx, %xmm0
xorpd %xmm1, %xmm1
subl 0x34(%rsi), %r15d
ucomisd %xmm1, %xmm0
jb 0x3b70
sqrtsd %xmm0, %xmm0
jmp 0x3b75
callq 0x21a0
cvttsd2si %xmm0, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
cmpl %ecx, %r12d
jge 0x3bae
movq 0xb444(%rip), %rdi      # 0xefd0
leaq 0x7668(%rip), %rsi      # 0xb1fb
movl $0xc, %edx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2240
movq 0xb41b(%rip), %r12      # 0xefd0
leaq 0x7584(%rip), %rsi      # 0xb140
movl $0x7, %edx
movq %r12, %rdi
callq 0x2240
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movl 0x20(%rbx), %eax
cmpl %r15d, %eax
jle 0x3d39
leaq 0x7544(%rip), %rsi      # 0xb148
movl $0xf, %edx
movq %r12, %rdi
callq 0x2240
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
leaq 0x80(%r14), %r13
movq %r13, %rdi
callq 0x9bf2
testb %al, %al
je 0x40b3
movq %r13, %rdi
callq 0x9e62
movq %rax, %r13
leaq 0x74f6(%rip), %rsi      # 0xb158
movl $0xe, %edx
movq %r12, %rdi
callq 0x2240
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movq %r13, %rdi
callq 0x5b40
testl %eax, %eax
jle 0x3ec9
leaq 0x74b9(%rip), %rsi      # 0xb167
movl $0x25, %edx
movq %r12, %rdi
callq 0x2240
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
testl %r15d, %r15d
jle 0x3f50
leaq 0x749a(%rip), %rsi      # 0xb18d
movl $0xb, %edx
movq %r12, %rdi
callq 0x2240
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
subl %r15d, 0x20(%rbx)
movq %r13, %rdi
callq 0x5d3c
jmp 0x3ff3
testl %eax, %eax
je 0x3e6e
leaq -0x78(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x88(%rbp), %r15
movq %r15, %rdi
callq 0x344a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4208
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0x3d86
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2210
movl $0x0, 0x20(%rbx)
movl 0x40(%rbx), %eax
addl %eax, 0x40(%r14)
movq 0xb235(%rip), %r15      # 0xefd0
leaq 0x742b(%rip), %rsi      # 0xb1cd
movl $0xb, %edx
movq %r15, %rdi
callq 0x2240
leaq -0x38(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0x344a
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r15, %rdi
callq 0x2240
movq %rax, %r15
leaq 0x73f1(%rip), %rsi      # 0xb1d9
movl $0xb, %edx
movq %rax, %rdi
callq 0x2240
movl 0x40(%rbx), %esi
movq %r15, %rdi
callq 0x2310
movq %rax, %rbx
leaq 0x73db(%rip), %rsi      # 0xb1e5
movl $0xc, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x3e53
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2210
movl 0x40(%r14), %eax
cmpl 0x3c(%r14), %eax
jl 0x40fd
movq %r14, %rdi
callq 0x37e4
jmp 0x40fd
leaq -0x38(%rbp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x344a
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0xb136(%rip), %rdi      # 0xefd0
callq 0x2240
leaq 0x734c(%rip), %rsi      # 0xb1f2
movl $0x8, %edx
movq %rax, %rdi
callq 0x2240
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x40fd
movq -0x38(%rbp), %rsi
jmp 0x40f5
leaq -0x48(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x5abe
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0xb0ea(%rip), %rdi      # 0xefd0
callq 0x2240
movq %rax, %r15
leaq 0x72b7(%rip), %rsi      # 0xb1ac
movl $0xc, %edx
movq %rax, %rdi
callq 0x2240
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3f42
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2210
decl 0x20(%rbx)
movl $0x1, %r15d
jmp 0x3ff3
leaq -0x38(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0x344a
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0xb052(%rip), %rdi      # 0xefd0
callq 0x2240
movq %rax, %r12
leaq 0x720c(%rip), %rsi      # 0xb199
movl $0x12, %edx
movq %rax, %rdi
callq 0x2240
leaq -0x58(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x344a
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %r12, %rdi
callq 0x2240
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x3fda
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2210
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x3ff3
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x344a
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0xafb0(%rip), %rdi      # 0xefd0
callq 0x2240
movq %rax, %r14
leaq 0x718a(%rip), %rsi      # 0xb1b9
movl $0x7, %edx
movq %rax, %rdi
callq 0x2240
movq %r14, %rdi
movl %r15d, %esi
callq 0x2310
movq %rax, %r14
leaq 0x7170(%rip), %rsi      # 0xb1c1
movl $0xb, %edx
movq %rax, %rdi
callq 0x2240
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x344a
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %r14, %rdi
callq 0x2240
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x409e
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2210
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x40b3
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0x98(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
callq 0x344a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4208
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x40fd
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2210
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x412e
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2210
jmp 0x412e
jmp 0x412b
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
jmp 0x4182
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x41a4
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2210
jmp 0x41a4
jmp 0x41a1
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0x41c7
movq -0x78(%rbp), %rsi
jmp 0x41bf
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
jne 0x41bb
jmp 0x41c7
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x41bb
jmp 0x41c7
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x41c7
movq -0x98(%rbp), %rsi
jmp 0x41bf
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0x41bb
jmp 0x41c7
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x41c7
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x2320
nop