// File: /workspace/github_cpp_makefile_star2_O2/AngelPn[P]L5R-card_game/src/Player.cpp
// Function: Player::equipPhase()
void Player::equipPhase()
{
	cout << "Player: " << this->name << " is playing " << endl;
	cout << "Equip Phase ..." << endl << endl;
	//player prints hand and army if has any
	if (!army.size())
	{
		cout << "No army found, phase is skipped :(" << endl << endl;
		return;
	}
	else if (!money && holdings.size() == 0)
	{
		cout << "No money and holdings available (you're broke dude), phase is skipped :(" << endl << endl;
		return;
	}

	else //proceed only if army and money are both available
	{
		cout << "Available money : " << money << endl << endl;
		this->printHand();
		this->printArmy();
	}

	this->tapHoldings(); //tap any holdings in case you need money

	char input;
	unsigned int soldier = 0, card = 0;

	while(money)  //while player has money and wants to
	{
		input = '0';
		soldier=0;
		card = 0;
		cout << "Available money : " << money << endl << endl;
		while (input != 'y' && input != 'n')
		{
			cout << this->name<< ", do you want to buy any cards from your hand (y:yes / n:no) ?" << endl;
			cin >> input;
		}

		if (input == 'n')
		{
			cout << "Well, okay then, get ready for battle phase" << endl << endl;
			break; //exit equip phase
		}
		while (soldier < 1 || soldier > army.size())
		{
			this->printArmy();
			cout << this->name<< ", choose a personality of your army to equip: (1-" << army.size() << ")" << endl;
			cin >> soldier;
		}

		//if the upper bound of green cards for the personality is  exceeded
		if (army[soldier-1]->belowBound() == false)
		{
			cout << "Personality already has max green cards, purchase cancelled" << endl;
			continue;
		}

		while (card < 1 || card > hand.size())
		{
			this->printHand();
			cout << this->name<< ", choose a card in your hand to equip selected personality with : (1-" << hand.size() << ")" << endl;
			cin >> card;
		}

		//if money is not enough for purchase
		if (hand[card-1]->get_cost() > money)
		{
			cout << "Selected card cannot be afforded, purchase cancelled" << endl;
			continue;
		}

		//if minimum honour of selected card is not matched by honour of selected personality
		if (hand[card-1]->get_honour() > army[soldier-1]->get_honour())
		{
			cout << "Selected card's honour is too high for your selected soldier, purchase cancelled" << endl;
			continue;
		}

		//at this point purchase is made
		money -= hand[card-1]->get_cost();   //make purchase
		//assign card (item or follower) to personality
		army[soldier-1]->equip(hand[card-1]);
		cout << "Purchase successfully made, card was assigned to your soldier " << endl << endl;
		//if player has available money to buy the effect cost of card
		if (hand[card-1]->get_effectCost() <= money)
		{
			input = '0';
			while (input != 'y' && input != 'n')
			{
				cout << this->name<< ", do you want buy the effect bonus of the card (y:yes / n:no) ?" << endl;
				cin >> input;
			}

			if (input == 'y')
			{
				money -= hand[card-1]->get_effectCost(); //make purchase
				//apply effectBonus to card
				hand[card-1]->applyBonus();
				cout << "Purchase successfully made,effect bonus of card applied " << endl << endl;
			}
		}

		//add attack/defense bonuses of card directly to soldier (this can be done either here or in battle phase)
		army[soldier-1]->applyBonus(hand[card-1]);
		hand.erase(hand.begin() + (card-1)); //erase card from hand

	}


	//HERE: money == roundMoney + the sum of all the harvest values of the tapped holdings - the sum of the cost of the cards bought
	//Thus if money - roundMoney > 0 => the sum of all the harvest values of the tapped holdings > the sum of the cost of the cards bought =>
	//some money (exactly money - roundMoney) from the tapping of the holdings was not used and it needs to be substracted
	//so in this case the new value of money would be money - (money - roundMoney) == roundMoney , hence the assignment money = roundMoney
	//else no substraction is made ,money remains intact and is assigned to roundMoney.

	cout << "Any remainder of the money from the tapped holdings is lost " << endl;
	money = (money > roundMoney) ? roundMoney : money ;  //if money  > roundMoney that means that some money from the tapping of the holdings was not used
	roundMoney = money;									 //and it needs to be substracted , thus money is set to be roundMoney
	cout << "Available money now is " << money << endl;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x9c90(%rip), %r15      # 0x10fd0
leaq 0x352c(%rip), %rsi      # 0xa873
movq %r15, %rdi
callq 0x31a0
leaq 0xa8(%rbx), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x3170
leaq 0x3514(%rip), %rsi      # 0xa87c
movq %rax, %rdi
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
leaq 0x37e7(%rip), %rsi      # 0xab66
movq %r15, %rdi
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq 0x58(%rbx), %rax
cmpq 0x50(%rbx), %rax
je 0x77ec
cmpl $0x0, 0xa0(%rbx)
jne 0x73bc
movq 0x40(%rbx), %rax
cmpq 0x38(%rbx), %rax
je 0x781d
movq 0x9c0d(%rip), %r15      # 0x10fd0
leaq 0x37f5(%rip), %rsi      # 0xabbf
movq %r15, %rdi
callq 0x31a0
movl 0xa0(%rbx), %esi
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x71f4
movq %rbx, %rdi
callq 0x782e
movq %rbx, %rdi
callq 0x78ac
leaq 0x8(%rsp), %r13
andl $0x0, (%r13)
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
leaq 0x20(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x4244(%rip), %rbp      # 0xb66e
movq 0x9baf(%rip), %r12      # 0x10fe0
cmpl $0x0, 0xa0(%rbx)
je 0x777d
andl $0x0, 0x8(%rsp)
movb $0x30, 0x3(%rsp)
andl $0x0, 0x4(%rsp)
movq %r15, %rdi
leaq 0x3768(%rip), %rsi      # 0xabbf
callq 0x31a0
movl 0xa0(%rbx), %esi
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movzbl 0x3(%rsp), %eax
cmpl $0x6e, %eax
je 0x775a
cmpl $0x79, %eax
je 0x7516
movq %r15, %rdi
movq %r14, %rsi
callq 0x3170
movq %rax, %rdi
leaq 0x372c(%rip), %rsi      # 0xabd2
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x3(%rsp), %rsi
callq 0x31f0
jmp 0x747a
movq %rbx, %rdi
callq 0x782e
movq %r15, %rdi
movq %r14, %rsi
callq 0x3170
movq %rax, %rdi
leaq 0x375e(%rip), %rsi      # 0xac3d
callq 0x31a0
movq 0x58(%rbx), %rsi
subq 0x50(%rbx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
movq %rbp, %rsi
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x3250
movl 0x8(%rsp), %eax
testq %rax, %rax
je 0x74c2
movq 0x50(%rbx), %rcx
movq 0x58(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jb 0x74c2
decl %eax
movq (%rcx,%rax,8), %rdi
callq 0x6922
testb %al, %al
je 0x75b7
movl 0x4(%rsp), %eax
testq %rax, %rax
je 0x755f
movq 0x20(%rbx), %rcx
movq 0x28(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jae 0x75c3
movq %rbx, %rdi
callq 0x71f4
movq %r15, %rdi
movq %r14, %rsi
callq 0x3170
movq %rax, %rdi
leaq 0x372f(%rip), %rsi      # 0xacab
callq 0x31a0
movq 0x28(%rbx), %rsi
subq 0x20(%rbx), %rsi
sarq $0x3, %rsi
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
movq %rbp, %rsi
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x4(%rsp), %rsi
callq 0x3250
jmp 0x7542
movq %r15, %rdi
leaq 0x36ae(%rip), %rsi      # 0xac6f
jmp 0x760a
decl %eax
movq (%rcx,%rax,8), %rax
movl 0x28(%rax), %ecx
cmpl 0xa0(%rbx), %ecx
jbe 0x75e0
movq %r15, %rdi
leaq 0x3713(%rip), %rsi      # 0xacf1
jmp 0x760a
movl 0x58(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0x8(%rsp), %eax
decl %eax
movq 0x50(%rbx), %rcx
movq (%rcx,%rax,8), %rdi
callq 0x688a
cmpl %eax, 0xc(%rsp)
jbe 0x761c
movq %r15, %rdi
leaq 0x371c(%rip), %rsi      # 0xad26
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
jmp 0x7431
movl 0x4(%rsp), %eax
decl %eax
movq 0x20(%rbx), %rcx
movq 0x50(%rbx), %rdx
movq (%rcx,%rax,8), %rsi
movl 0x28(%rsi), %eax
subl %eax, 0xa0(%rbx)
movl 0x8(%rsp), %eax
decl %eax
movq (%rdx,%rax,8), %rdi
callq 0x6a12
movq %r15, %rdi
leaq 0x3727(%rip), %rsi      # 0xad77
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movl 0x4(%rsp), %eax
decl %eax
movq 0x20(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movl 0x60(%rax), %eax
cmpl 0xa0(%rbx), %eax
ja 0x771c
movb $0x30, 0x3(%rsp)
movb $0x30, %al
cmpb $0x6e, %al
je 0x771c
movzbl %al, %eax
cmpl $0x79, %eax
je 0x76ce
movq %r15, %rdi
movq %r14, %rsi
callq 0x3170
movq %rax, %rdi
leaq 0x3708(%rip), %rsi      # 0xadb6
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
leaq 0x3(%rsp), %rsi
callq 0x31f0
movb 0x3(%rsp), %al
jmp 0x7689
movl 0x4(%rsp), %eax
decl %eax
movq 0x20(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movl 0x60(%rax), %ecx
subl %ecx, 0xa0(%rbx)
movq 0x50(%rax), %xmm0
movd 0x5c(%rax), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x50(%rax)
movq %r15, %rdi
leaq 0x36ef(%rip), %rsi      # 0xadf6
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movl 0x8(%rsp), %eax
decl %eax
movq 0x20(%rbx), %rcx
movq 0x50(%rbx), %rdx
movq (%rdx,%rax,8), %rdi
movl 0x4(%rsp), %eax
decl %eax
movq (%rcx,%rax,8), %rsi
callq 0x69fe
movl 0x4(%rsp), %esi
decl %esi
shlq $0x3, %rsi
addq 0x20(%rbx), %rsi
movq 0x10(%rsp), %rdi
callq 0x8f8e
jmp 0x7431
movq 0x986f(%rip), %rdi      # 0x10fd0
leaq 0x34a9(%rip), %rsi      # 0xac11
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq 0x984c(%rip), %r14      # 0x10fd0
leaq 0x36a4(%rip), %rsi      # 0xae2f
movq %r14, %rdi
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movl 0xa0(%rbx), %eax
movl 0xa4(%rbx), %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0xa0(%rbx)
movl %ecx, 0xa4(%rbx)
leaq 0x36ad(%rip), %rsi      # 0xae6c
movq %r14, %rdi
callq 0x31a0
movl 0xa0(%rbx), %esi
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
callq 0x30d0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x97dd(%rip), %rdi      # 0x10fd0
leaq 0x30a0(%rip), %rsi      # 0xa89a
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30d0
movq 0x97ac(%rip), %rdi      # 0x10fd0
leaq 0x334b(%rip), %rsi      # 0xab76
jmp 0x77fa
nop