// File: MichaelXanth[P]RPG_Game/Battle/Battle.cpp
// Function: Battle::CastSpell(Hero&, Monster&)
bool Battle::CastSpell(Hero& hero, Monster& monster) 
{
    unsigned int totalDamage;
    unsigned int slotNumber;
    unsigned int typeOfSpell;
    Spell* chosenSpell;

    if (hero.DisplaySpells(chosenSpell , slotNumber , typeOfSpell) == false)
        return false;

    // Checks if the hero has enough magic power
    if (hero.UseSpell(*chosenSpell) == true) { 
        hero.ReduceMagicPower(chosenSpell->GetReqEnergy()); // Reduces the hero's magic power
        totalDamage = chosenSpell->HeroDamage(hero);        // Gets the total damage that will be caused
        monster.ChangeHealth(floor((double)(totalDamage * totalDamage) / (totalDamage + monster.MonsterGetDefence())));
        monster.HitBySpell(chosenSpell);                    // Applies the after attack effects of the spell
        hero.SpellUsed(slotNumber , typeOfSpell);           // Deletes the spell
        
        if (monster.Fainted()) {
            cout << "The hero " << hero.GetName() << " defeated the monster " << monster.GetName() << endl;
            return true;
        }
    }
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x20(%rbp), %rdx
leaq -0x24(%rbp), %rcx
callq 0x23cc0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1778b
movb $0x0, -0x1(%rbp)
jmp 0x17967
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x1f090
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x17963
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
movq -0xa8(%rbp), %rdi
movl %eax, -0x34(%rbp)
leaq -0x34(%rbp), %rsi
callq 0x1ed80
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3b040
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rdi
movq %rdi, -0x90(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x94(%rbp)
imull %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0xa0(%rbp)
callq 0x2e6f0
movsd -0xa0(%rbp), %xmm0
movl %eax, %ecx
movl -0x94(%rbp), %eax
addl %ecx, %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
callq 0x4410
movq -0x90(%rbp), %rdi
cvttsd2si %xmm0, %rax
movl %eax, %esi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x2e730
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
movl -0x24(%rbp), %edx
callq 0x20370
movq -0x18(%rbp), %rdi
callq 0x1ea40
testb $0x1, %al
jne 0x1786c
jmp 0x17961
movq 0x36755(%rip), %rdi     # 0x4dfc8
leaq 0x25a15(%rip), %rsi     # 0x3d28f
callq 0x4260
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x1ea60
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x4210
movq %rax, -0xb0(%rbp)
jmp 0x178b6
movq -0xb0(%rbp), %rdi
leaq 0x259d5(%rip), %rsi     # 0x3d299
callq 0x4260
movq %rax, -0xc8(%rbp)
jmp 0x178d2
movq -0x18(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x1ea60
jmp 0x178e4
movq -0xc8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x4210
movq %rax, -0xd0(%rbp)
jmp 0x17900
movq -0xd0(%rbp), %rdi
movq 0x366aa(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x17915
leaq -0x88(%rbp), %rdi
callq 0x4160
leaq -0x58(%rbp), %rdi
callq 0x4160
movb $0x1, -0x1(%rbp)
jmp 0x17967
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x17956
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x4160
leaq -0x58(%rbp), %rdi
callq 0x4160
jmp 0x17975
jmp 0x17963
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xd0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x4440
nop