// File: /workspace/github_cpp_makefile_star2_O1/MichaelXanth[P]RPG_Game/Game/Game.cpp
// Function: Game::Play()
void Game::Play(void)
{
    bool gameover = false;

    while(gameover == false) {

        grid.DisplayGrid(heroes);

        char input = GetInput();

        // Enters inventory
        if (input == 'i') {

            int herochoice = 1;
            int arrow      = 0;

            cout << "Press 'e' to exit the inventory" << endl;

            if (heroes.size() != 1)
                cout << "Switch between heroes using numbers in range [1-" << heroes.size() << "]: ";

            cout << endl;

            while(true) {
                bool changeHero = false;
                char selection = ' ';

                cout << "\nSelected Hero: \033[38;5;220m" << heroes[herochoice - 1]->GetName() << "\033[0m" << endl << endl;

                while(true) {
                    if (selection == '\n')
                        cout << "\033[3A\033[J";

                    // Prints and handles the arrow menu
                    do {

                        DisplayActions(arrow,true);

                        selection = getch();

                        if (selection == 'w' && arrow)
                            arrow--;
                        else if (selection == 's' && arrow < 2)
                            arrow++;
                        else if (selection > '0' && selection <= '0' + heroes.size()) {
                            herochoice = selection - '0';
                            changeHero = true;
                            cout << "\033[3A";
                        }

                        if (selection != '\n')
                            cout << "\033[3A\033[J";

                    } while(selection != 'e' && selection != '\n' && changeHero != true);

                    if (selection == 'e' || changeHero == true)
                        break;

                    OpenInventory(*heroes[herochoice - 1] , arrow + 1);
                }

                if (selection == 'e')
                    break;

            }
        
        } else if (input == 'q') {
            char ch;
            cout << "Do you want to quit the game? [y/n]: " << endl;
            ch = getch();
            if (ch == 'y' || ch == 'Y' || ch == '\n')
                gameover = true;
        
        } else {
            bool moved = false;

            moved = grid.MoveHeroes(input);  

            // If the heroes are inside the store then the store menu opens
            if (grid.IsInStore() == true) {
                Trade();  
                grid.HeroExitedStore();
            
            // If the conditions are met a battle starts
            } else if (moved == true && grid.IsInStore() == false && Battle::BattleHappens() == true) {
                StartBattle(); 
            
            /* If a battle does not happen then the heroes restore some of their HP and some of their MP*/
            } else if (moved == true && grid.IsInStore() == false) { 
                for (int k = 0; k < heroes.size(); k++)
                    heroes[k]->EndTurnRefill();
            }
        }
    }
    cout << "Thanks for playing!" << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r12
xorl %r14d, %r14d
movl $0x440009, %ebp         # imm = 0x440009
movq 0x23840(%rip), %rbx     # 0x2efc8
leaq 0x10(%rsp), %r13
leaq 0x18bf5(%rip), %r15     # 0x24389
movq %rdi, 0x8(%rsp)
movq (%r12), %rdi
movq 0x10(%r12), %rsi
callq 0xf3aa
callq 0x10728
movzbl %al, %ecx
addl $-0x61, %ecx
cmpl $0x16, %ecx
ja 0xb7a7
btl %ecx, %ebp
jb 0xb827
cmpl $0x8, %ecx
je 0xb893
cmpl $0x10, %ecx
jne 0xb7a7
movl $0x25, %edx
movq %rbx, %rdi
leaq 0x18bbd(%rip), %rsi     # 0x24396
callq 0x31e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
callq 0x10728
cmpb $0xa, %al
je 0xb81f
movzbl %al, %eax
cmpl $0x79, %eax
je 0xb81f
cmpl $0x59, %eax
jne 0xbb90
movb $0x1, %r14b
jmp 0xbb90
movq (%r12), %rdi
movsbl %al, %esi
callq 0x10148
movl %eax, %ebp
movq (%r12), %rdi
callq 0x102d0
testb %al, %al
je 0xb85f
movq 0x8(%r12), %rdi
movq 0x10(%r12), %rsi
callq 0x22594
movq (%r12), %rdi
callq 0x102d4
jmp 0xbb8b
testb %bpl, %bpl
je 0xbb8b
movq (%r12), %rdi
callq 0x102d0
testb %al, %al
jne 0xbb39
callq 0xaed8
testb %al, %al
je 0xbb39
movq %r12, %rdi
callq 0xbeb8
jmp 0xbb8b
movl $0x1f, %edx
movq %rbx, %rdi
leaq 0x18a7a(%rip), %rsi     # 0x2431c
callq 0x31e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq 0x10(%r12), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
cmpq $0x8, %rcx
je 0xb920
movl $0x30, %edx
movq %rbx, %rdi
leaq 0x18a4d(%rip), %rsi     # 0x2433c
callq 0x31e0
movq 0x10(%r12), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x3, %rsi
movq %rbx, %rdi
callq 0x30f0
movl $0x3, %edx
movq %rax, %rdi
leaq 0x18a9d(%rip), %rsi     # 0x243b8
callq 0x31e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x1, 0x4(%rsp)
xorl %r12d, %r12d
movq %r14, 0x30(%rsp)
movl $0x1b, %edx
movq %rbx, %rdi
leaq 0x18a07(%rip), %rsi     # 0x2436d
callq 0x31e0
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movslq 0x4(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rsi
movq %r13, %rdi
callq 0x108c2
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x31e0
movq %rax, %rbp
movl $0x4, %edx
movq %rax, %rdi
leaq 0x1b437(%rip), %rsi     # 0x26de4
callq 0x31e0
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xba20
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x31b0
movb $0x20, %bpl
xorl %r14d, %r14d
cmpb $0xa, %bpl
jne 0xba3c
movl $0x7, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31e0
movl %r12d, 0x10(%rsp)
movq %r13, %rsi
movl $0x1, %edx
callq 0xbc0c
callq 0x10728
movl %eax, %ebp
cmpb $0x77, %al
sete %al
testl %r12d, %r12d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xba6c
decl %r12d
jmp 0xbace
cmpb $0x73, %bpl
sete %al
cmpl $0x2, %r12d
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xba86
incl %r12d
jmp 0xbace
cmpb $0x31, %bpl
jl 0xbace
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
movzbl %bpl, %eax
sarq $0x3, %rcx
addq $0x30, %rcx
cmpq %rax, %rcx
jb 0xbace
leal -0x30(%rbp), %eax
movsbl %al, %eax
movl %eax, 0x4(%rsp)
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x188cb(%rip), %rsi     # 0x24391
callq 0x31e0
movb $0x1, %r14b
cmpb $0xa, %bpl
je 0xbafb
movl $0x7, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31e0
cmpb $0x65, %bpl
je 0xbaf5
movl %r14d, %eax
andb $0x1, %al
je 0xba3c
cmpb $0x65, %bpl
je 0xbb2d
testb $0x1, %r14b
jne 0xb957
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movslq 0x4(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rsi
leal 0x1(%r12), %eax
movswl %ax, %edx
callq 0xbe72
jmp 0xba26
movq 0x8(%rsp), %r12
movq 0x30(%rsp), %r14
jmp 0xbb8b
testb %bpl, %bpl
je 0xbb8b
movq (%r12), %rdi
callq 0x102d0
testb %al, %al
movl $0x440009, %ebp         # imm = 0x440009
jne 0xbb90
movq 0x10(%r12), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0xbb90
movq %r14, %rbp
xorl %r14d, %r14d
movq (%rax,%r14,8), %rdi
callq 0x111da
incq %r14
movq 0x10(%r12), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
ja 0xbb64
movq %rbp, %r14
movl $0x440009, %ebp         # imm = 0x440009
testb $0x1, %r14b
je 0xb799
leaq 0x1881b(%rip), %rsi     # 0x243bc
movl $0x13, %edx
movq %rbx, %rdi
callq 0x31e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0xbc03
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x31b0
movq %rbx, %rdi
callq 0x32f0
nop