// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movb $0x0, -0x9(%rbp)
movb -0x9(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1816f
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rax), %rsi
callq 0x1d1a0
movq -0x58(%rbp), %rdi
callq 0x17c50
movb %al, -0xa(%rbp)
movsbl -0xa(%rbp), %eax
cmpl $0x69, %eax
jne 0x1800b
movl $0x1, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq 0x3628e(%rip), %rdi     # 0x4dfc8
leaq 0x255db(%rip), %rsi     # 0x3d31c
callq 0x4260
movq %rax, %rdi
movq 0x36268(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x5ad0
cmpq $0x1, %rax
je 0x17daa
movq 0x36259(%rip), %rdi     # 0x4dfc8
leaq 0x255c6(%rip), %rsi     # 0x3d33c
callq 0x4260
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x60(%rbp)
movq 0x10(%rax), %rdi
callq 0x5ad0
movq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x4080
movq %rax, %rdi
leaq 0x25613(%rip), %rsi     # 0x3d3b8
callq 0x4260
movq 0x36217(%rip), %rdi     # 0x4dfc8
movq 0x36200(%rip), %rsi     # 0x4dfb8
callq 0x4290
movb $0x0, -0x15(%rbp)
movb $0x20, -0x16(%rbp)
movq 0x361fc(%rip), %rdi     # 0x4dfc8
leaq 0x2559a(%rip), %rsi     # 0x3d36d
callq 0x4260
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x78(%rbp)
movq 0x10(%rax), %rdi
movl -0x10(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x5030
movq (%rax), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x1ea60
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x4210
movq %rax, -0x68(%rbp)
jmp 0x17e17
movq -0x68(%rbp), %rdi
leaq 0x28052(%rip), %rsi     # 0x3fe74
callq 0x4260
movq %rax, -0x80(%rbp)
jmp 0x17e2d
movq -0x80(%rbp), %rdi
movq 0x36180(%rip), %rsi     # 0x4dfb8
callq 0x4290
movq %rax, -0x88(%rbp)
jmp 0x17e46
movq -0x88(%rbp), %rdi
movq 0x36164(%rip), %rsi     # 0x4dfb8
callq 0x4290
jmp 0x17e5b
leaq -0x38(%rbp), %rdi
callq 0x4160
movsbl -0x16(%rbp), %eax
cmpl $0xa, %eax
jne 0x17e9c
movq 0x36154(%rip), %rdi     # 0x4dfc8
leaq 0x2550e(%rip), %rsi     # 0x3d389
callq 0x4260
jmp 0x17e9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4160
jmp 0x1819a
jmp 0x17e9e
movq -0x58(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x48(%rbp)
leaq -0x48(%rbp), %rsi
movl $0x1, %edx
callq 0x181b0
callq 0x1e820
movb %al, -0x16(%rbp)
movsbl -0x16(%rbp), %eax
cmpl $0x77, %eax
jne 0x17ed8
cmpl $0x0, -0x14(%rbp)
je 0x17ed8
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x17f4c
movsbl -0x16(%rbp), %eax
cmpl $0x73, %eax
jne 0x17ef2
cmpl $0x2, -0x14(%rbp)
jge 0x17ef2
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x17f4a
movsbl -0x16(%rbp), %eax
cmpl $0x30, %eax
jle 0x17f48
movq -0x58(%rbp), %rax
movsbq -0x16(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movq 0x10(%rax), %rdi
callq 0x5ad0
movq %rax, %rcx
movq -0x90(%rbp), %rax
addq $0x30, %rcx
cmpq %rcx, %rax
ja 0x17f48
movsbl -0x16(%rbp), %eax
subl $0x30, %eax
movl %eax, -0x10(%rbp)
movb $0x1, -0x15(%rbp)
movq 0x3608c(%rip), %rdi     # 0x4dfc8
leaq 0x2544e(%rip), %rsi     # 0x3d391
callq 0x4260
jmp 0x17f4a
jmp 0x17f4c
movsbl -0x16(%rbp), %eax
cmpl $0xa, %eax
je 0x17f68
movq 0x3606c(%rip), %rdi     # 0x4dfc8
leaq 0x25426(%rip), %rsi     # 0x3d389
callq 0x4260
jmp 0x17f6a
movsbl -0x16(%rbp), %ecx
xorl %eax, %eax
cmpl $0x65, %ecx
movb %al, -0x91(%rbp)
je 0x17fa0
movsbl -0x16(%rbp), %ecx
xorl %eax, %eax
cmpl $0xa, %ecx
movb %al, -0x91(%rbp)
je 0x17fa0
movb -0x15(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
setne %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0x17e9e
movsbl -0x16(%rbp), %eax
cmpl $0x65, %eax
je 0x17fc4
movb -0x15(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x17fc6
jmp 0x17ff6
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x10(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x5030
movq -0x58(%rbp), %rdi
movq (%rax), %rsi
movl -0x14(%rbp), %eax
addl $0x1, %eax
movswl %ax, %edx
callq 0x183f0
jmp 0x17e64
movsbl -0x16(%rbp), %eax
cmpl $0x65, %eax
jne 0x18001
jmp 0x18006
jmp 0x17dbd
jmp 0x1816a
movsbl -0xa(%rbp), %eax
cmpl $0x71, %eax
jne 0x18062
movq 0x35fad(%rip), %rdi     # 0x4dfc8
leaq 0x25374(%rip), %rsi     # 0x3d396
callq 0x4260
movq %rax, %rdi
movq 0x35f87(%rip), %rsi     # 0x4dfb8
callq 0x4290
callq 0x1e820
movb %al, -0x49(%rbp)
movsbl -0x49(%rbp), %eax
cmpl $0x79, %eax
je 0x18059
movsbl -0x49(%rbp), %eax
cmpl $0x59, %eax
je 0x18059
movsbl -0x49(%rbp), %eax
cmpl $0xa, %eax
jne 0x1805d
movb $0x1, -0x9(%rbp)
jmp 0x18168
movq -0x58(%rbp), %rax
movb $0x0, -0x4a(%rbp)
movq (%rax), %rdi
movsbl -0xa(%rbp), %esi
callq 0x1df00
movb %al, %cl
movq -0x58(%rbp), %rax
andb $0x1, %cl
movb %cl, -0x4a(%rbp)
movq (%rax), %rdi
callq 0x1e200
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x180ae
movq -0x58(%rbp), %rdi
callq 0x184a0
movq -0x58(%rbp), %rax
movq (%rax), %rdi
callq 0x1e220
jmp 0x18166
movb -0x4a(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x180eb
movq -0x58(%rbp), %rax
movq (%rax), %rdi
callq 0x1e200
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x180eb
callq 0x16fd0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x180eb
movq -0x58(%rbp), %rdi
callq 0x184e0
jmp 0x18164
movb -0x4a(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x18162
movq -0x58(%rbp), %rax
movq (%rax), %rdi
callq 0x1e200
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x18162
movl $0x0, -0x50(%rbp)
movq -0x58(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq %rcx, -0xa0(%rbp)
movq 0x10(%rax), %rdi
callq 0x5ad0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
cmpq %rcx, %rax
jae 0x18160
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x50(%rbp), %rsi
callq 0x5030
movq (%rax), %rdi
callq 0x1fb30
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x18115
jmp 0x18162
jmp 0x18164
jmp 0x18166
jmp 0x18168
jmp 0x1816a
jmp 0x17ceb
movq 0x35e52(%rip), %rdi     # 0x4dfc8
leaq 0x2523f(%rip), %rsi     # 0x3d3bc
callq 0x4260
movq %rax, %rdi
movq 0x35e2c(%rip), %rsi     # 0x4dfb8
callq 0x4290
addq $0xa0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x4440
nopw %cs:(%rax,%rax)