// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x80, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x2c(%rdi), %r15d
movl 0x30(%rdi), %r12d
subl 0x34(%rsi), %r15d
movl 0x78(%rsi), %esi
movl 0x7c(%rbx), %edx
callq 0x37b0
movq 0xab9c(%rip), %rdi      # 0xdfd0
cmpl %eax, %r12d
jge 0x3454
leaq 0x5dbb(%rip), %rsi      # 0x91fb
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x21c0
leaq 0x5ce5(%rip), %rsi      # 0x9140
callq 0x21c0
movq %rax, %rdi
callq 0x2110
movl 0x20(%rbx), %eax
cmpl %r15d, %eax
jle 0x352c
movq 0xab55(%rip), %rdi      # 0xdfd0
leaq 0x5cc6(%rip), %rsi      # 0x9148
callq 0x21c0
movq %rax, %rdi
callq 0x2110
leaq 0x80(%r14), %r12
movq %r12, %rdi
callq 0x7960
testb %al, %al
je 0x3732
movq %r12, %rdi
callq 0x7acc
movq %rax, %r12
movq 0xab18(%rip), %rdi      # 0xdfd0
leaq 0x5c99(%rip), %rsi      # 0x9158
callq 0x21c0
movq %rax, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x49ee
testl %eax, %eax
jle 0x361d
movq 0xaaed(%rip), %rdi      # 0xdfd0
leaq 0x5c7d(%rip), %rsi      # 0x9167
callq 0x21c0
movq %rax, %rdi
callq 0x2110
testl %r15d, %r15d
jle 0x3664
movq 0xaac9(%rip), %rdi      # 0xdfd0
leaq 0x5c7f(%rip), %rsi      # 0x918d
callq 0x21c0
movq %rax, %rdi
callq 0x2110
subl %r15d, 0x20(%rbx)
movq %r12, %rdi
callq 0x4b70
jmp 0x36be
testl %eax, %eax
je 0x35e7
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2250
movq %rbx, %rdi
movq %r15, %rsi
callq 0x37d8
leaq -0x80(%rbp), %rdi
callq 0x2100
andl $0x0, 0x20(%rbx)
movl 0x40(%rbx), %eax
addl %eax, 0x40(%r14)
movq 0xaa67(%rip), %rdi      # 0xdfd0
leaq 0x5c5d(%rip), %rsi      # 0x91cd
callq 0x21c0
movq %rax, %r15
leaq -0x40(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2250
movq %r15, %rdi
movq %r12, %rsi
callq 0x21b0
leaq 0x5c40(%rip), %rsi      # 0x91d9
movq %rax, %rdi
callq 0x21c0
movl 0x40(%rbx), %esi
movq %rax, %rdi
callq 0x22f0
leaq 0x5c32(%rip), %rsi      # 0x91e5
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2110
leaq -0x40(%rbp), %rdi
callq 0x2100
movl 0x40(%r14), %eax
cmpl 0x3c(%r14), %eax
jl 0x375b
movq %r14, %rdi
callq 0x31f8
jmp 0x375b
leaq -0x40(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2250
movq 0xa9d3(%rip), %rdi      # 0xdfd0
movq %r14, %rsi
callq 0x21b0
leaq 0x5be6(%rip), %rsi      # 0x91f2
movq %rax, %rdi
callq 0x21c0
leaq -0x40(%rbp), %rdi
jmp 0x3756
leaq -0x40(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x4992
movq 0xa99d(%rip), %rdi      # 0xdfd0
movq %r15, %rsi
callq 0x21b0
leaq 0x5b6a(%rip), %rsi      # 0x91ac
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2110
leaq -0x40(%rbp), %rdi
callq 0x2100
decl 0x20(%rbx)
pushq $0x1
popq %r15
jmp 0x36be
leaq -0x40(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2250
movq 0xa956(%rip), %rdi      # 0xdfd0
movq %r12, %rsi
callq 0x21b0
leaq 0x5b10(%rip), %rsi      # 0x9199
movq %rax, %rdi
callq 0x21c0
movq %rax, %r12
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0x2250
leaq -0x60(%rbp), %rsi
movq %r12, %rdi
callq 0x21b0
leaq -0x60(%rbp), %rdi
callq 0x2100
leaq -0x40(%rbp), %rdi
callq 0x2100
leaq -0x40(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2250
movq 0xa8fc(%rip), %rdi      # 0xdfd0
movq %r12, %rsi
callq 0x21b0
leaq 0x5ad6(%rip), %rsi      # 0x91b9
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
movl %r15d, %esi
callq 0x22f0
leaq 0x5ac4(%rip), %rsi      # 0x91c1
movq %rax, %rdi
callq 0x21c0
movq %rax, %r14
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0x2250
leaq -0x60(%rbp), %rsi
movq %r14, %rdi
callq 0x21b0
leaq -0x60(%rbp), %rdi
callq 0x2100
leaq -0x40(%rbp), %rdi
callq 0x2100
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2250
movq %rbx, %rdi
movq %r14, %rsi
callq 0x37d8
leaq -0xa0(%rbp), %rdi
callq 0x2100
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3771
jmp 0x379c
jmp 0x379c
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2100
jmp 0x379f
jmp 0x379c
jmp 0x379c
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0x37a3
jmp 0x379c
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
jmp 0x37a3
jmp 0x379c
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2300