// File: /workspace/github_cpp_makefile_star2_O2/arisKoutsou[P]oopproj/./src/livings/Hero.cpp
// Function: Hero::buy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void Hero::buy(const string& itemName) {
  Market* currentMarket = this->getTile().getMarket();

  if (currentMarket == NULL) {
    cout << "You can't buy on a tile that has no market!" << endl << endl;
    return;
  }

  Item* itemToBuy = currentMarket->getItemByName(itemName);

  if (itemToBuy != NULL) {    
    string kind = itemToBuy->kindOf();
    Item* itemToAdd;
    bool equiped = false;

    cout << endl << "Bought " << itemName
	 << " (" << kind << ")";
    if (kind == "Weapon") {
      itemToAdd = new Weapon(*static_cast<Weapon*>(itemToBuy));
      Weapon* aux = static_cast<Weapon*>(itemToAdd);
      if (aux->needsBothHands()) {
        if (this->usesLeftHand() == false && this->usesRightHand() == false) {
	  this->equipWeapon(aux);
	  equiped = true;
	}
      } else {
	if (this->usesLeftHand() == false || this->usesRightHand() == false) {
	  this->equipWeapon(aux);
	  equiped = true;
	}
      }
    } else if (kind == "Armor") {
      itemToAdd = new Armor(*static_cast<Armor*>(itemToBuy));
      if (this->shield == NULL) {
	this->shield = static_cast<Armor*>(itemToAdd);
	equiped = true;
      }
    } else {
      itemToAdd = new Potion(*static_cast<Potion*>(itemToBuy));
    }

    if(!equiped) inventory.addItem(itemToAdd);
    setMoney(getMoney() - itemToBuy->buyFor());
    return;
  }

  Spell* spellToBuy = currentMarket->getSpellByName(itemName);
  
  if (spellToBuy != NULL) {
    string kind = spellToBuy->kindOf();
    Spell* spellToAdd;
    bool equiped = false;

    cout << endl << "Bought " << itemName
	 << " (" << kind << ")";
    if (kind == "IceSpell") {
      spellToAdd = new IceSpell(*static_cast<IceSpell*>(spellToBuy));
    } else if (kind == "FireSpell") {
      spellToAdd = new FireSpell(*static_cast<FireSpell*>(spellToBuy));
    } else {
      spellToAdd = new LightningSpell(*static_cast<LightningSpell*>(spellToBuy));
    }
    if (this->spells.size() == 0) {
      this->spells.push_back(spellToAdd);
      equiped = true;
    }

    if (!equiped) inventory.addSpell(spellToAdd);
    setMoney(getMoney() - spellToBuy->getValue());
    return;
  }

  cout << endl << "There's no item/spell with this name" << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rsi, %r15
movq %rdi, %rbx
callq 0xf07e
movq %rax, %rdi
callq 0x5bb2
testq %rax, %rax
je 0x1090a
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0xde90
testq %rax, %rax
je 0x10939
movq %rax, %r14
movq (%rax), %rax
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq *0x18(%rax)
movq 0x10755(%rip), %rdi     # 0x20fc0
callq 0x4120
leaq 0x580c(%rip), %rsi      # 0x16083
movq %rax, %rdi
callq 0x4250
movq %rax, %rdi
movq %r15, %rsi
callq 0x4220
leaq 0x57fa(%rip), %rsi      # 0x1608b
movq %rax, %rdi
callq 0x4250
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x4220
leaq 0x4d0e(%rip), %rsi      # 0x155bb
movq %rax, %rdi
callq 0x4250
leaq 0x52e6(%rip), %rsi      # 0x15ba2
leaq 0x10(%rsp), %rdi
callq 0x5b02
testb %al, %al
je 0x109e3
pushq $0x48
popq %rdi
callq 0x4260
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x126a8
movq %r15, %rdi
callq 0x13d6e
movq 0x48(%rbx), %rcx
movq 0x50(%rbx), %rdx
testb %al, %al
je 0x10a33
orq %rdx, %rcx
jne 0x10a43
jmp 0x10a54
movq 0x106af(%rip), %rdi     # 0x20fc0
leaq 0x573f(%rip), %rsi      # 0x16057
callq 0x4250
movq %rax, %rdi
callq 0x4120
movq %rax, %rdi
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4120
movq %r12, %rdi
movq %r15, %rsi
callq 0xdef8
testq %rax, %rax
je 0x10a72
movq %rax, %r14
movq (%rax), %rax
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq *0x18(%rax)
movq 0x1065b(%rip), %rdi     # 0x20fc0
callq 0x4120
leaq 0x5712(%rip), %rsi      # 0x16083
movq %rax, %rdi
callq 0x4250
movq %rax, %rdi
movq %r15, %rsi
callq 0x4220
leaq 0x5700(%rip), %rsi      # 0x1608b
movq %rax, %rdi
callq 0x4250
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x4220
leaq 0x4c14(%rip), %rsi      # 0x155bb
movq %rax, %rdi
callq 0x4250
leaq 0x527c(%rip), %rsi      # 0x15c32
leaq 0x10(%rsp), %rdi
callq 0x5b02
testb %al, %al
je 0x10a92
pushq $0x58
popq %rdi
callq 0x4260
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x12744
jmp 0x10ad5
leaq 0x51bf(%rip), %rsi      # 0x15ba9
leaq 0x10(%rsp), %rdi
callq 0x5b02
testb %al, %al
je 0x10a1b
pushq $0x48
popq %rdi
callq 0x4260
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x126dc
cmpq $0x0, 0x58(%rbx)
jne 0x10a43
movq %r15, 0x58(%rbx)
jmp 0x10a5f
pushq $0x60
popq %rdi
callq 0x4260
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x1270c
jmp 0x10a43
testq %rdx, %rdx
setne %al
testq %rcx, %rcx
setne %cl
testb %al, %cl
je 0x10a54
leaq 0xc0(%rbx), %rdi
movq %r15, %rsi
callq 0xb74a
jmp 0x10a5f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1018a
movl 0xac(%rbx), %ebp
movq %r14, %rdi
callq 0x1459a
jmp 0x10b0e
movq 0x10547(%rip), %rdi     # 0x20fc0
callq 0x4120
leaq 0x5609(%rip), %rsi      # 0x1608e
movq %rax, %rdi
callq 0x4250
jmp 0x10925
leaq 0x51a2(%rip), %rsi      # 0x15c3b
leaq 0x10(%rsp), %rdi
callq 0x5b02
testb %al, %al
je 0x10abf
pushq $0x58
popq %rdi
callq 0x4260
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x12778
jmp 0x10ad5
pushq $0x58
popq %rdi
callq 0x4260
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x127ac
movq %r15, 0x8(%rsp)
cmpq $0x0, 0x70(%rbx)
je 0x10af2
leaq 0xc0(%rbx), %rdi
movq %r15, %rsi
callq 0xb784
jmp 0x10b00
leaq 0x60(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xbc90
movl 0xac(%rbx), %ebp
movq %r14, %rdi
callq 0xed68
subl %eax, %ebp
movl %ebp, 0xac(%rbx)
leaq 0x10(%rsp), %rdi
callq 0x4470
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x10b31
jmp 0x10b31
movq %rax, %rbx
pushq $0x58
jmp 0x10b46
movq %rax, %rbx
pushq $0x60
jmp 0x10b46
jmp 0x10b41
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x4270
jmp 0x10b58
jmp 0x10b55
jmp 0x10b55
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4470
movq %rbx, %rdi
callq 0x4410