// File: /workspace/github_cpp_makefile_star2_O3/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
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
sqrtsd %xmm0, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
cmpl %ecx, 0x30(%rdi)
jge 0x3bb1
movq 0xb441(%rip), %rdi      # 0xefd0
leaq 0x7665(%rip), %rsi      # 0xb1fb
movl $0xc, %edx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230
movq %rsi, %rbx
movq %rdi, %r14
movl 0x2c(%rdi), %r15d
subl 0x34(%rsi), %r15d
movq 0xb40a(%rip), %r12      # 0xefd0
leaq 0x7573(%rip), %rsi      # 0xb140
movl $0x7, %edx
movq %r12, %rdi
callq 0x2230
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movl 0x20(%rbx), %eax
cmpl %r15d, %eax
jle 0x3d4a
leaq 0x7533(%rip), %rsi      # 0xb148
movl $0xf, %edx
movq %r12, %rdi
callq 0x2230
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
leaq 0x80(%r14), %r13
movq %r13, %rdi
callq 0x9c32
testb %al, %al
je 0x40c4
movq %r13, %rdi
callq 0x9e94
movq %rax, %r13
leaq 0x74e5(%rip), %rsi      # 0xb158
movl $0xe, %edx
movq %r12, %rdi
callq 0x2230
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movq %r13, %rdi
callq 0x5b58
testl %eax, %eax
jle 0x3eda
leaq 0x74a8(%rip), %rsi      # 0xb167
movl $0x25, %edx
movq %r12, %rdi
callq 0x2230
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
testl %r15d, %r15d
jle 0x3f61
leaq 0x7489(%rip), %rsi      # 0xb18d
movl $0xb, %edx
movq %r12, %rdi
callq 0x2230
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
subl %r15d, 0x20(%rbx)
movq %r13, %rdi
callq 0x5d54
jmp 0x4004
testl %eax, %eax
je 0x3e7f
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 0x347c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4208
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0x3d97
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2200
movl $0x0, 0x20(%rbx)
movl 0x40(%rbx), %eax
addl %eax, 0x40(%r14)
movq 0xb224(%rip), %r15      # 0xefd0
leaq 0x741a(%rip), %rsi      # 0xb1cd
movl $0xb, %edx
movq %r15, %rdi
callq 0x2230
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 0x347c
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r15, %rdi
callq 0x2230
movq %rax, %r15
leaq 0x73e0(%rip), %rsi      # 0xb1d9
movl $0xb, %edx
movq %rax, %rdi
callq 0x2230
movl 0x40(%rbx), %esi
movq %r15, %rdi
callq 0x2300
movq %rax, %rbx
leaq 0x73ca(%rip), %rsi      # 0xb1e5
movl $0xc, %edx
movq %rax, %rdi
callq 0x2230
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x3e64
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
movl 0x40(%r14), %eax
cmpl 0x3c(%r14), %eax
jl 0x410e
movq %r14, %rdi
callq 0x381c
jmp 0x410e
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 0x347c
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0xb125(%rip), %rdi      # 0xefd0
callq 0x2230
leaq 0x733b(%rip), %rsi      # 0xb1f2
movl $0x8, %edx
movq %rax, %rdi
callq 0x2230
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x410e
movq -0x38(%rbp), %rsi
jmp 0x4106
leaq -0x48(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x5ad6
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0xb0d9(%rip), %rdi      # 0xefd0
callq 0x2230
movq %rax, %r15
leaq 0x72a6(%rip), %rsi      # 0xb1ac
movl $0xc, %edx
movq %rax, %rdi
callq 0x2230
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21e0
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 0x3f53
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
decl 0x20(%rbx)
movl $0x1, %r15d
jmp 0x4004
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 0x347c
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0xb041(%rip), %rdi      # 0xefd0
callq 0x2230
movq %rax, %r12
leaq 0x71fb(%rip), %rsi      # 0xb199
movl $0x12, %edx
movq %rax, %rdi
callq 0x2230
leaq -0x58(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x347c
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %r12, %rdi
callq 0x2230
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x3feb
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x4004
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
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 0x347c
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0xaf9f(%rip), %rdi      # 0xefd0
callq 0x2230
movq %rax, %r14
leaq 0x7179(%rip), %rsi      # 0xb1b9
movl $0x7, %edx
movq %rax, %rdi
callq 0x2230
movq %r14, %rdi
movl %r15d, %esi
callq 0x2300
movq %rax, %r14
leaq 0x715f(%rip), %rsi      # 0xb1c1
movl $0xb, %edx
movq %rax, %rdi
callq 0x2230
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x347c
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %r14, %rdi
callq 0x2230
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x40af
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x40c4
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
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 0x347c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4208
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x410e
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2200
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 0x413f
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x413f
jmp 0x413c
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
jmp 0x4193
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x41b5
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x41b5
jmp 0x41b2
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0x41d8
movq -0x78(%rbp), %rsi
jmp 0x41d0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
jne 0x41cc
jmp 0x41d8
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x41cc
jmp 0x41d8
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r15, %rdi
je 0x41d8
movq -0x98(%rbp), %rsi
jmp 0x41d0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0x41cc
jmp 0x41d8
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r13, %rdi
je 0x41d8
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2310