// File: /workspace/github_cpp_makefile_star2_O2/AngelPn[P]L5R-card_game/src/Player.cpp
// Function: Player::checkChains(Holding*)
void Player::checkChains(Holding* holding)
{
	cout << "Checking for possible chains ... " << endl << endl;

	Holding* upper = NULL;
	Holding* sub = NULL;
	if (holding->getHoldingType() == MINE) //if holding is mine you search for possible upper holding connections
	{
		for (unsigned int i = 1; i < holdings.size(); i++)  //you go through all players holdings other than stronghold obviously
		{
			if (holdings[i]->getHoldingType() == GOLD_MINE && upper == NULL && holdings[i]->hasSub() == false ) //chain goes mine --> gold mine
				upper = holdings[i];
			else if (holdings[i]->getHoldingType() == GOLD_MINE && upper != NULL &&
					holdings[i]->hasSub() == false && rand()%2 == 1) //if another gold mine is found choose in random
				upper = holdings[i];
		}

	}

	else if (holding->getHoldingType() == CRYSTAL_MINE) //if holding is crystal mine you search for possible sub holding connections
	{
		for (unsigned int i = 1; i < holdings.size(); i++)
		{
			if (holdings[i]->getHoldingType() == GOLD_MINE && sub == NULL && holdings[i]->hasUpper() == false) //chain goes crystal mine <-- gold mine
				sub = holdings[i];
			else if (holdings[i]->getHoldingType() == GOLD_MINE && sub != NULL &&
					holdings[i]->hasUpper() == false && rand()%2 == 1) //if another gold mine is found choose in random
				sub = holdings[i];
		}

	}

	else if (holding->getHoldingType() == GOLD_MINE) //if holding is gold mine you search for possible upper AND sub holding connections
	{
		for (unsigned int i = 1; i < holdings.size(); i++)
		{
			if (holdings[i]->getHoldingType() == MINE && sub == NULL && holdings[i]->hasUpper() == false) //chain goes gold mine <-- mine
				sub = holdings[i];
			else if (holdings[i]->getHoldingType() == MINE && sub != NULL &&
					holdings[i]->hasUpper() == false && rand()%2 == 1) //if another mine is found choose in random
				sub = holdings[i];

			if (holdings[i]->getHoldingType() == CRYSTAL_MINE && upper == NULL && holdings[i]->hasSub() == false) //chaing goes mine --> gold mine --> crystal mine
				upper = holdings[i];
			else if (holdings[i]->getHoldingType() == CRYSTAL_MINE && upper != NULL &&
					holdings[i]->hasSub() == false && rand()%2 == 1) //if another mine is found choose in random
				upper = holdings[i];
		}

	}

	if (upper != NULL){ //if a possible upper holding connection is decided
		holding->setUpperHolding(upper);  //make connection
		upper->setSubHolding(holding);	  //connection is two-way
	}
	if (sub != NULL){ //if a possible sub holding connection is decided
		holding->setSubHolding(sub); 	  //make connection
		sub->setUpperHolding(holding);	  //connection is two-way	
	}

	if (upper != NULL || sub !=NULL) //if at least one connection was made
	{
		holding->equipBonus();  //update harvest value of holding with bonus
		if(upper!=NULL)
			upper->equipBonus(); //update harvest value of upper with bonus, if it exists 
		if(sub!=NULL)
			sub->equipBonus();   //update harvest value of sub with bonus, if it exists
		cout << "Connections made , bonuses equipped " << endl << endl;
	}

	else
		cout << "No possible connections found :( " << endl << endl;
 //obviously if given holding is not mine or gold mine or crystal mine nothing happens and we just move on with our day


}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %rbx
movq %rdi, %r15
movq 0x8680(%rip), %rdi      # 0x10fd0
leaq 0x2c5e(%rip), %rsi      # 0xb5b5
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
callq 0x30d0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
cmpl $0x1, %eax
jne 0x8a06
pushq $0x1
popq %r13
xorl %r14d, %r14d
xorl %r12d, %r12d
movl %r13d, %r13d
movq 0x38(%r15), %rax
movq 0x40(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x8b98
movq (%rax,%r13,8), %rdi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x38(%r15), %rcx
movq (%rcx,%r13,8), %rdi
cmpl $0x2, %eax
jne 0x89c6
testq %r12, %r12
jne 0x89c6
cmpq $0x0, 0x38(%rdi)
je 0x89fe
movq (%rdi), %rax
callq *0x28(%rax)
cmpl $0x2, %eax
jne 0x8a01
testq %r12, %r12
je 0x8a01
movq 0x38(%r15), %rax
movq (%rax,%r13,8), %rax
cmpq $0x0, 0x38(%rax)
jne 0x8a01
callq 0x3070
andl $0x80000001, %eax       # imm = 0x80000001
cmpl $0x1, %eax
jne 0x8a01
movq 0x38(%r15), %rax
movq (%rax,%r13,8), %r12
jmp 0x8a01
movq %rdi, %r12
incl %r13d
jmp 0x8988
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
cmpl $0x3, %eax
jne 0x8a9d
pushq $0x1
popq %r12
xorl %r14d, %r14d
movl %r12d, %r12d
movq 0x38(%r15), %rax
movq 0x40(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0x8bab
movq (%rax,%r12,8), %rdi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x38(%r15), %rcx
movq (%rcx,%r12,8), %rdi
cmpl $0x2, %eax
jne 0x8a5d
testq %r14, %r14
jne 0x8a5d
cmpq $0x0, 0x30(%rdi)
je 0x8a95
movq (%rdi), %rax
callq *0x28(%rax)
cmpl $0x2, %eax
jne 0x8a98
testq %r14, %r14
je 0x8a98
movq 0x38(%r15), %rax
movq (%rax,%r12,8), %rax
cmpq $0x0, 0x30(%rax)
jne 0x8a98
callq 0x3070
andl $0x80000001, %eax       # imm = 0x80000001
cmpl $0x1, %eax
jne 0x8a98
movq 0x38(%r15), %rax
movq (%rax,%r12,8), %r14
jmp 0x8a98
movq %rdi, %r14
incl %r12d
jmp 0x8a1f
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x28(%rax)
cmpl $0x2, %eax
jne 0x8bf8
pushq $0x1
popq %r13
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %r13d, %r13d
movq 0x38(%r15), %rax
movq 0x40(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x8b98
movq (%rax,%r13,8), %rdi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x38(%r15), %rcx
movq (%rcx,%r13,8), %rdi
cmpl $0x1, %eax
jne 0x8af7
testq %r14, %r14
jne 0x8af7
cmpq $0x0, 0x30(%rdi)
je 0x8b2f
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x38(%r15), %rcx
cmpl $0x1, %eax
jne 0x8b32
testq %r14, %r14
je 0x8b32
movq (%rcx,%r13,8), %rax
cmpq $0x0, 0x30(%rax)
jne 0x8b32
callq 0x3070
andl $0x80000001, %eax       # imm = 0x80000001
movq 0x38(%r15), %rcx
cmpl $0x1, %eax
jne 0x8b32
movq (%rcx,%r13,8), %r14
jmp 0x8b32
movq %rdi, %r14
movq (%rcx,%r13,8), %rdi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x38(%r15), %rcx
movq (%rcx,%r13,8), %rdi
cmpl $0x3, %eax
jne 0x8b55
testq %r12, %r12
jne 0x8b55
cmpq $0x0, 0x38(%rdi)
je 0x8b8d
movq (%rdi), %rax
callq *0x28(%rax)
cmpl $0x3, %eax
jne 0x8b90
testq %r12, %r12
je 0x8b90
movq 0x38(%r15), %rax
movq (%rax,%r13,8), %rax
cmpq $0x0, 0x38(%rax)
jne 0x8b90
callq 0x3070
andl $0x80000001, %eax       # imm = 0x80000001
cmpl $0x1, %eax
jne 0x8b90
movq 0x38(%r15), %rax
movq (%rax,%r13,8), %r12
jmp 0x8b90
movq %rdi, %r12
incl %r13d
jmp 0x8ab9
testq %r12, %r12
je 0x8bab
movq %r12, 0x30(%rbx)
movq %rbx, 0x38(%r12)
movb $0x1, %r15b
jmp 0x8bb1
xorl %r15d, %r15d
xorl %r12d, %r12d
testq %r14, %r14
je 0x8bcd
movq %r14, 0x38(%rbx)
movq %rbx, 0x30(%r14)
movq %rbx, %rdi
callq 0x90b2
testb %r15b, %r15b
jne 0x8bda
jmp 0x8be2
testb %r15b, %r15b
je 0x8bf8
movq %rbx, %rdi
callq 0x90b2
movq %r12, %rdi
callq 0x90b2
leaq 0x29ee(%rip), %rbx      # 0xb5d7
testq %r14, %r14
je 0x8bff
movq %r14, %rdi
callq 0x90b2
jmp 0x8bff
leaq 0x29fd(%rip), %rbx      # 0xb5fc
movq 0x83ca(%rip), %rdi      # 0x10fd0
movq %rbx, %rsi
callq 0x31a0
movq %rax, %rdi
callq 0x30d0
movq %rax, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x30d0
nop