// File: /workspace/github_cpp_makefile_star2_O2/MichaelXanth[P]RPG_Game/Market/Market.cpp
// Function: Market::Buy(std::vector<Hero*, std::allocator<Hero*>>&)
void Market::Buy(vector<Hero *>& heroes)
{
    unsigned int i;

    int action;
    string testAction;
    bool infoState     = false;
    Hero* selectedHero = NULL;

    if (heroes.size() == 1 && isAbleToBuy(heroes[0]) == -1) {
        usleep(2500000);
        DisplayMarket();
        return;
    
    } else {
        for (i = 0; i < heroes.size() && isAbleToBuy(heroes[i],false) == -1; i++)
            ;

        if (i == heroes.size()) {
            cerr << endl << "\033[31mError:\033[0m No one of the heroes are able to buy any Items or Spells from the Market.";
            usleep(2500000);
            DisplayMarket();
            return;
        }
    }

    while(true) {
        
        int buyState;

        if (heroes.size() == 1) {
            selectedHero = heroes[0];
        
        } else {
        
            for (i = 0; i < heroes.size() && isAbleToBuy(heroes[i],false) == -1; i++)
                ;

            if (i == heroes.size()) {
                cerr << endl << "-------------------------------" << endl << endl;
                cerr << "\033[35mWarning:\033[0m There are no other heroes able to buy any Items or Spells from the Market.";
                usleep(3000000);
                DisplayMarket();
                return;
            }

            while(selectedHero == NULL) {
                //bool errorFlag = false;
                cout << endl;
                cout << "-------------------------------" << endl << endl;
                cout << "(Type 0 to cancel)" << endl;
                cout << "Choose a Hero (1 - " << heroes.size() << "): ";

                cout << "\n\n\n\n\n";
                cout << "\033[5A\033[23C";

                while(true) {

                    bool errFlag = false;
                    cout << "\033[s";

                    do {
                        if (errFlag)
                            usleep(1800000);

                        cout << "\033[u";
                        cout << "\033[J";
                        cout << "\033[s";

                        cin >> testAction;

                        errFlag = true;
                    } while(!isNumeric(testAction.c_str(),action));

                    if (action < 0 || action > heroes.size()) {
                        cerr << endl << "\033[31mError:\033[0m Please choose a number in the given range." << endl;

                        usleep(1800000);

                        cout << "\033[u";
                        cout << "\033[J";

                        //cout << "\033[K";
                        //cout << "\033[1A\033[K";
                        //cout << "\033[1A\033[K";

                        //if (errorFlag)
                        //    cout << "\033[1A\033[K";

                        //errorFlag = true;
                    } else {
                        if (!action) {
                            DisplayMarket();
                            return;
                        }
                        selectedHero = heroes[action - 1];
                        break;
                    }
                }
            }
        }

        if ((buyState = isAbleToBuy(selectedHero)) != -1) {
            bool errFlag = false;

            DisplayMarket(infoState,selectedHero,buyState);

            cout << endl;
            cout << "-------------------------------" << endl << endl;
            cout << "Choose an action: ";

            cout << "\n\n\n\n\n";
            cout << "\033[5A\033[18C";
            cout << "\033[s";

            do {
                if (errFlag)
                    usleep(1800000);

                cout << "\033[u";
                cout << "\033[J";
                cout << "\033[s";

                cin >> testAction;

                errFlag = true;
            } while(!isValid(testAction.c_str(),action,1,3));

            switch(action)
            {
                case 1:
                    Purchase(*selectedHero,buyState);
                    break;
                case 2:
                    infoState = !infoState;
                    // DisplayMarket(infoState,selectedHero,buyState);
                    break;
                case 3:
                    DisplayMarket();
                    return;
            }
        
        } else if (heroes.size() == 1) {
            usleep(2500000);
            DisplayMarket();
            return;
        }

        if (buyState == -1) {
            DisplayMarket();
            return;
        }
    //        selectedHero = NULL;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, %r15
xorl %ecx, %ecx
movb %cl, (%rax)
movb %cl, -0x29(%rbp)
movq (%rsi), %rax
movq %rsi, -0x48(%rbp)
movq 0x8(%rsi), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x17c93
movq (%rax), %rsi
leaq -0x34(%rbp), %rdx
movb $0x1, (%rdx)
movq %r15, %rdi
callq 0x141aa
cmpl $-0x1, %eax
je 0x180a9
xorl %ecx, %ecx
leaq -0x34(%rbp), %rbx
movl %ecx, %r14d
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x17ce7
movq (%rax,%r14,8), %rsi
movb $0x0, -0x34(%rbp)
movq %r15, %rdi
movq %rbx, %rdx
callq 0x141aa
leal 0x1(%r14), %ecx
cmpl $-0x1, %eax
je 0x17c99
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jne 0x17d2a
movq 0xf2fd(%rip), %rdi      # 0x26ff0
callq 0x30d0
leaq 0x6a04(%rip), %rsi      # 0x1e703
movq %rax, %rdi
callq 0x31d0
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x32a0
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq %r15, %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x180c7
movq %r15, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq 0xf28b(%rip), %r13      # 0x26fc8
leaq 0x3b3a(%rip), %r12      # 0x1b87e
leaq 0x3b5d(%rip), %r15      # 0x1b8a8
leaq 0x5a51(%rip), %rbx      # 0x1d7a3
pushq $0x1
popq %r14
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x17e6a
movq (%rax), %rsi
movb $0x1, -0x30(%rbp)
movq -0x40(%rbp), %rdi
movq %rsi, -0x58(%rbp)
leaq -0x30(%rbp), %rdx
callq 0x141aa
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x1804f
movq -0x40(%rbp), %rdi
leaq -0x29(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl %eax, %ecx
callq 0x1496c
movq %r13, %rdi
callq 0x30d0
movq %r13, %rdi
leaq 0x64a6(%rip), %rsi      # 0x1e252
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq %r13, %rdi
leaq 0x6a48(%rip), %rsi      # 0x1e813
callq 0x31d0
movq %r13, %rdi
leaq 0x664e(%rip), %rsi      # 0x1e428
callq 0x31d0
movq %r13, %rdi
leaq 0x6a3d(%rip), %rsi      # 0x1e826
callq 0x31d0
movq %r13, %rdi
movq %r12, %rsi
callq 0x31d0
movq %r13, %rdi
movq %r15, %rsi
callq 0x31d0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x31d0
movq %r13, %rdi
movq %r12, %rsi
callq 0x31d0
movq 0xf1b7(%rip), %rdi      # 0x26fd8
leaq -0x88(%rbp), %rsi
callq 0x3250
movq -0x88(%rbp), %rdi
movl $0x1, -0x30(%rbp)
movl $0x3, -0x5c(%rbp)
leaq -0x4c(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x5c(%rbp), %rcx
movl %r14d, %r8d
callq 0xcb5c
testb %al, %al
jne 0x18038
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x32a0
jmp 0x17df9
xorl %ecx, %ecx
movl %ecx, %edx
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %rdx, %rax
jbe 0x17eb9
movq (%rcx,%rdx,8), %rsi
movb $0x0, -0x30(%rbp)
movq -0x40(%rbp), %rdi
movq %rdx, -0x68(%rbp)
leaq -0x30(%rbp), %rdx
callq 0x141aa
movq -0x68(%rbp), %rdx
leal 0x1(%rdx), %ecx
cmpl $-0x1, %eax
je 0x17e6c
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
cmpq %rdx, %rax
movq -0x58(%rbp), %rsi
je 0x180f9
testq %rsi, %rsi
jne 0x17d66
movq %r13, %rdi
callq 0x30d0
movq %r13, %rdi
leaq 0x6371(%rip), %rsi      # 0x1e252
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq %r13, %rdi
leaq 0x649d(%rip), %rsi      # 0x1e39d
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %r13, %rdi
leaq 0x68a3(%rip), %rsi      # 0x1e7ba
callq 0x31d0
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rsi
subq (%rcx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x64e4(%rip), %rsi      # 0x1e421
callq 0x31d0
movq %r13, %rdi
leaq 0x64dc(%rip), %rsi      # 0x1e428
callq 0x31d0
movq %r13, %rdi
leaq 0x6873(%rip), %rsi      # 0x1e7ce
callq 0x31d0
movq %r13, %rdi
movq %r12, %rsi
callq 0x31d0
movq %r13, %rdi
movq %r15, %rsi
callq 0x31d0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x31d0
movq %r13, %rdi
movq %r12, %rsi
callq 0x31d0
movq 0xf045(%rip), %rdi      # 0x26fd8
leaq -0x88(%rbp), %rsi
callq 0x3250
movq -0x88(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl %r14d, %edx
callq 0xcac0
testb %al, %al
jne 0x17fc2
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x32a0
jmp 0x17f6b
movslq -0x4c(%rbp), %rax
testq %rax, %rax
js 0x17fe2
movq -0x48(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jae 0x1802a
movq 0xf007(%rip), %rdi      # 0x26ff0
callq 0x30d0
movq %rax, %rdi
leaq 0x67e0(%rip), %rsi      # 0x1e7d8
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x32a0
movq %r13, %rdi
movq %r15, %rsi
callq 0x31d0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x31d0
jmp 0x17f60
testl %eax, %eax
je 0x18092
movq -0x8(%rcx,%rax,8), %rsi
jmp 0x17ec6
movl -0x4c(%rbp), %eax
cmpl $0x1, %eax
je 0x1806b
cmpl $0x2, %eax
je 0x18065
cmpl $0x3, %eax
jne 0x1807c
jmp 0x1817f
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cmpq $0x8, %rax
jne 0x1807c
jmp 0x1815b
xorb $0x1, -0x29(%rbp)
jmp 0x1807c
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x17246
cmpl $-0x1, -0x34(%rbp)
je 0x180e2
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
jmp 0x17d56
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x180c7
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x32a0
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq %r15, %rdi
xorl %edx, %edx
callq 0x1496c
leaq -0x88(%rbp), %rdi
callq 0x30c0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x180c7
movq 0xeef0(%rip), %rdi      # 0x26ff0
callq 0x30d0
leaq 0x6146(%rip), %rsi      # 0x1e252
movq %rax, %rdi
callq 0x31d0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq 0xeec5(%rip), %rdi      # 0x26ff0
leaq 0x662b(%rip), %rsi      # 0x1e75d
callq 0x31d0
movl $0x2dc6c0, %edi         # imm = 0x2DC6C0
callq 0x32a0
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x180c7
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x32a0
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x180c7
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
pushq $-0x1
popq %rcx
movq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0x1496c
jmp 0x180c7
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
jmp 0x181bd
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x30c0
movq %rbx, %rdi
callq 0x3330