// File: /workspace/github_cpp_makefile_star2_O2/Doug0855[P]TigerZone/Board.cpp
// Function: Board::buildTrail(Structure*, Tile*, std::vector<std::vector<Block, std::allocator<Block>>, std::allocator<std::vector<Block, std::allocator<Block>>>>&, std::pair<int, int>, std::vector<Tile*, std::allocator<Tile*>>&)
void Board::buildTrail(Structure* struc, Tile *tile, std::vector< std::vector<Block> >& tileBlocks, std::pair<int,int> blockSpot, std::vector<Tile*> &visitedTiles) {
	int row = blockSpot.first;
	int col = blockSpot.second;
	tileBlocks[row][col].visit();

	// We must check how many ways a trail exits/enters a block to know how many trails the block contains.
	// If its 2 or under then the block is 1 connected trail, if it's more than the trails all start from the center and are unique entities
	if(row == 1 && col == 1) {
		int divergingTrailCount = 0;
		if(tileBlocks[row+1][col].getType() == "trail")
			divergingTrailCount++;
		if(tileBlocks[row-1][col].getType() == "trail")
			divergingTrailCount++;
		if(tileBlocks[row][col+1].getType() == "trail")
			divergingTrailCount++;
		if(tileBlocks[row][col-1].getType() == "trail")
			divergingTrailCount++;
		if(divergingTrailCount > 2)
			return;
	}

	struc->structureBlocks.push_back(tileBlocks[row][col]);

	// Check block for meeple
	if(tileBlocks[row][col].hasMeeple())
		struc->hasMeeple = true;

	// Check to see if you've added this blocks animals
	bool addedTileAnimals;
	for(int i = 0; i < visitedTiles.size(); i++) {
			if(visitedTiles[i] == tile)
			{
				addedTileAnimals = true;
				//debugging std::cout<<"Added animals already for this tile"<<std::endl;
			}
	}
	if(!addedTileAnimals) {
		struc->checkAnimals(tile);
		// if(tile->hasCrocodile())
		// 	struc->crocodileCount++;
		// if(tile->hasDeer())
		// 	struc->deerCount++;
		// if(tile->hasBoar())
		// 	struc->boarCount++;
		// if(tile->hasBuffalo())
		// 	struc->buffaloCount++;
	}

	// Check tiles above, below, left, and right of the current block to see if trail structure continues
	if((row + 1) <= 2 && !(tileBlocks[row + 1][col].isVisited()) && tileBlocks[row+1][col].getType() == "trail") {
		buildTrail(struc, tile, tileBlocks, std::pair<int,int>(row+1,col), visitedTiles);
	}
	else if(row + 1 > 2 && tile->getDownTile() != NULL) {
		bool visitedNeighborTile = false;
		for(int i = 0; i < visitedTiles.size(); i++) {
			if(visitedTiles[i] == tile->getDownTile())
				visitedNeighborTile = true;
		}
		if(!visitedNeighborTile) {
			visitedTiles.push_back(tile->getDownTile());
			std::vector< std::vector<Block> > newBlocks = tile->getDownTile()->getInnerBlocks();
			buildTrail(struc, tile->getDownTile(), newBlocks, std::pair<int,int>(row - 2,col), visitedTiles);
		}
	}
	// std::cout<<"about to check row-1"<<std::endl;
	if((row - 1) >= 0 && !(tileBlocks[row - 1][col].isVisited()) && tileBlocks[row - 1][col].getType() == "trail") {
		buildTrail(struc, tile, tileBlocks, std::pair<int,int>(row-1,col), visitedTiles);
	}
	else if(row-1 < 0 && tile->getUpTile() != NULL) {
		bool visitedNeighborTile = false;
		for(int i = 0; i < visitedTiles.size(); i++) {
			if(visitedTiles[i] == tile->getUpTile())
				visitedNeighborTile = true;
		}
		if(!visitedNeighborTile) {
			visitedTiles.push_back(tile->getUpTile());
			std::vector< std::vector<Block> > newBlocks = tile->getUpTile()->getInnerBlocks();
			buildTrail(struc, tile->getUpTile(), newBlocks, std::pair<int,int>(row+2,col), visitedTiles);
		}
	}
	// std::cout<<"about to check col+1"<<std::endl;
	if((col + 1) <= 2 && !(tileBlocks[row][col + 1].isVisited()) && tileBlocks[row][col + 1].getType() == "trail") {
		buildTrail(struc, tile, tileBlocks, std::pair<int,int>(row,col+1), visitedTiles);
	}
	else if(col+1 > 2 && tile->getRightTile() != NULL) {
		bool visitedNeighborTile = false;
		for(int i = 0; i < visitedTiles.size(); i++) {
			if(visitedTiles[i] == tile->getRightTile())
				visitedNeighborTile = true;
		}
		if(!visitedNeighborTile) {
			visitedTiles.push_back(tile->getRightTile());
			std::vector< std::vector<Block> > newBlocks = tile->getRightTile()->getInnerBlocks();
			buildTrail(struc, tile->getRightTile(), newBlocks, std::pair<int,int>(row,col-2), visitedTiles);
		}
	}
	// std::cout<<"about to check col-1"<<std::endl;
	if((col - 1) >= 0 && !(tileBlocks[row][col-1].isVisited()) && tileBlocks[row][col - 1].getType() == "trail") {
		// std::cout<<"passing col-1"<<std::endl;
		buildTrail(struc, tile, tileBlocks, std::pair<int,int>(row,col-1), visitedTiles);
	}
	else if(col-1 < 0 && tile->getLeftTile() != NULL) {
		bool visitedNeighborTile = false;
		for(int i = 0; i < visitedTiles.size(); i++) {
			if(visitedTiles[i] == tile->getLeftTile())
				visitedNeighborTile = true;
		}
		if(!visitedNeighborTile) {
			visitedTiles.push_back(tile->getLeftTile());
			std::vector< std::vector<Block> > newBlocks = tile->getLeftTile()->getInnerBlocks();
			buildTrail(struc, tile->getLeftTile(), newBlocks, std::pair<int,int>(row,col+2), visitedTiles);
		}
	}
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, -0x30(%rbp)
movq %r8, %rbx
movq %rdx, -0x38(%rbp)
movq %rsi, %r14
movq %rdi, -0x48(%rbp)
movq %r8, %r15
shrq $0x20, %r15
movslq %ebx, %rdi
movq %rcx, -0x40(%rbp)
movq (%rcx), %rax
imulq $0x18, %rdi, %r13
movq %r8, %rdx
sarq $0x20, %rdx
movq (%rax,%r13), %rsi
imulq $0x28, %rdx, %r8
movb $0x1, 0x1(%rsi,%r8)
cmpl $0x1, %edi
setne %cl
cmpq $0x1, %r15
setne %dl
orb %cl, %dl
movq %rbx, -0x50(%rbp)
movq %r8, -0x88(%rbp)
jne 0xe584
movq %r13, -0x58(%rbp)
movq %r15, -0x60(%rbp)
movq 0x30(%rax), %rax
leaq (%rax,%r8), %rsi
addq $0x8, %rsi
movq -0x40(%rbp), %r15
leaq -0x80(%rbp), %r12
movq %r12, %rdi
movq %r8, %r13
callq 0x32c0
leaq 0x42a4(%rip), %rsi      # 0x12737
movq %r12, %rdi
callq 0x6261
movl %eax, %ebx
movq %r12, %rdi
callq 0x3120
movq %r14, -0x90(%rbp)
movzbl %bl, %r14d
movq (%r15), %rax
movq (%rax), %rax
leaq (%rax,%r13), %rsi
addq $0x8, %rsi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x32c0
movq %rbx, %rdi
leaq 0x4263(%rip), %rsi      # 0x12737
callq 0x6261
movl %eax, %r12d
movq %rbx, %rdi
callq 0x3120
leal 0x1(%r14), %r13d
testb %r12b, %r12b
cmovel %r14d, %r13d
movq (%r15), %rax
movq 0x18(%rax), %rsi
addq $0x58, %rsi
movq %rbx, %rdi
callq 0x32c0
movq %rbx, %rdi
leaq 0x422b(%rip), %r14      # 0x12737
movq %r14, %rsi
callq 0x6261
movl %eax, %r12d
movq %rbx, %rdi
callq 0x3120
movzbl %r12b, %r12d
movq (%r15), %rax
movq 0x18(%rax), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x32c0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6261
movl %eax, %r14d
movq %rbx, %rdi
movq -0x50(%rbp), %rbx
callq 0x3120
movzbl %r14b, %eax
movq -0x90(%rbp), %r14
addl %r12d, %eax
addl %r13d, %eax
cmpl $0x2, %eax
ja 0xeba0
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %r13
movq (%rax,%r13), %rsi
movq -0x60(%rbp), %r15
movq -0x88(%rbp), %r8
leaq 0x28(%r14), %rdi
addq %r8, %rsi
callq 0xec76
movq -0x88(%rbp), %r9
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq (%rcx,%r13), %rax
cmpb $0x1, (%rax,%r9)
jne 0xe5ae
movb $0x1, 0x50(%r14)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x1, %ebx
jg 0xe665
movq %rbx, %rax
shlq $0x20, %rax
addq %r12, %rax
incq %rax
sarq $0x20, %rax
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %rax
cmpb $0x0, 0x1(%rax,%r9)
jne 0xe73b
leaq (%rax,%r9), %rsi
addq $0x8, %rsi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x32c0
leaq 0x413a(%rip), %rsi      # 0x12737
movq %rbx, %rdi
callq 0x6261
movq %r14, %r12
movl %eax, %r14d
leaq -0x80(%rbp), %rdi
callq 0x3120
movq -0x88(%rbp), %r9
testb %r14b, %r14b
movq %r12, %r14
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq -0x50(%rbp), %rbx
je 0xe73b
leal 0x1(%rbx), %r8d
movq %rbx, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rax
orq %rax, %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r9
callq 0xe3f8
jmp 0xe734
movq -0x38(%rbp), %rax
movq 0xb8(%rax), %rax
testq %rax, %rax
je 0xe74a
movq -0x30(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0xe6a4
cmpq %rax, (%rcx,%rsi,8)
sete %r8b
orb %r8b, %dil
incq %rsi
jmp 0xe68f
testb $0x1, %dil
jne 0xe73b
movq %r13, -0x58(%rbp)
movq %r15, -0x60(%rbp)
leaq -0x80(%rbp), %rsi
movq %rax, (%rsi)
movq -0x38(%rbp), %r13
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq -0x30(%rbp), %r12
movq %r12, %rdi
callq 0xf488
movl $0xd0, %esi
addq 0xb8(%r13), %rsi
leaq -0x80(%rbp), %rdi
callq 0x65ba
movq 0xb8(%r13), %rdx
leal (%r15,%rbx), %r8d
decl %r8d
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rbx
orq %rbx, %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
leaq -0x80(%rbp), %rcx
movq %r12, %r9
callq 0xe3f8
leaq -0x80(%rbp), %rdi
callq 0xecb2
movq -0x50(%rbp), %rbx
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq -0x88(%rbp), %r9
testl %ebx, %ebx
jle 0xe7e9
movq -0x40(%rbp), %rax
movq (%rax), %rcx
leal (%rbx,%r12), %eax
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %rax
cmpb $0x0, 0x1(%rax,%r9)
jne 0xe8b8
leaq (%rax,%r9), %rsi
addq $0x8, %rsi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x32c0
leaq 0x3fba(%rip), %rsi      # 0x12737
movq %rbx, %rdi
callq 0x6261
movq %r13, %r12
movq %r15, %r13
movq %r14, %r15
movl %eax, %r14d
movq %rbx, %rdi
movq -0x50(%rbp), %rbx
callq 0x3120
testb %r14b, %r14b
movq %r15, %r14
movq %r13, %r15
movq %r12, %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
je 0xe8b8
leal (%rbx,%r12), %r8d
movq %rbx, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rax
orq %rax, %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r9
callq 0xe3f8
jmp 0xe8b8
movq -0x38(%rbp), %rax
movq 0xb0(%rax), %rax
testq %rax, %rax
je 0xe8b8
movq -0x30(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0xe828
cmpq %rax, (%rcx,%rsi,8)
sete %r8b
orb %r8b, %dil
incq %rsi
jmp 0xe813
testb $0x1, %dil
jne 0xe8b8
movq %r13, -0x58(%rbp)
movq %r15, -0x60(%rbp)
leaq -0x80(%rbp), %rsi
movq %rax, (%rsi)
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0xf488
movl $0xd0, %esi
movq -0x38(%rbp), %r13
addq 0xb0(%r13), %rsi
movq %rbx, %r12
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x65ba
movq 0xb0(%r13), %rdx
leal 0x2(%r12), %r8d
movq %r12, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rax
orq %rax, %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r15, %r9
callq 0xe3f8
leaq -0x80(%rbp), %rdi
callq 0xecb2
movq -0x50(%rbp), %rbx
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x1, %r15d
jg 0xe973
movq -0x40(%rbp), %rax
movq (%rax), %rax
leaq (%r12,%rbx), %rcx
incq %rcx
sarq $0x20, %rcx
movq (%rax,%r13), %rax
imulq $0x28, %rcx, %rcx
cmpb $0x0, 0x1(%rax,%rcx)
jne 0xea4d
leaq (%rax,%rcx), %rsi
addq $0x8, %rsi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x32c0
leaq 0x3e35(%rip), %rsi      # 0x12737
movq %rbx, %rdi
callq 0x6261
movq %r13, %r12
movq %r15, %r13
movq %r14, %r15
movl %eax, %r14d
movq %rbx, %rdi
movq -0x50(%rbp), %rbx
callq 0x3120
testb %r14b, %r14b
movq %r15, %r14
movq %r13, %r15
movq %r12, %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
je 0xea4d
movq %rbx, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rax
addq %r12, %rax
movl %ebx, %ecx
leaq (%rcx,%rax), %r8
incq %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r9
callq 0xe3f8
jmp 0xea4d
movq -0x38(%rbp), %rax
movq 0xc8(%rax), %rax
testq %rax, %rax
je 0xea56
movq -0x30(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0xe9b2
cmpq %rax, (%rcx,%rsi,8)
sete %r8b
orb %r8b, %dil
incq %rsi
jmp 0xe99d
testb $0x1, %dil
jne 0xea4d
movq %r13, -0x58(%rbp)
movq %r15, -0x60(%rbp)
leaq -0x80(%rbp), %rsi
movq %rax, (%rsi)
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0xf488
movl $0xd0, %esi
movq -0x38(%rbp), %r13
addq 0xc8(%r13), %rsi
movq %rbx, %r12
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x65ba
movq 0xc8(%r13), %rdx
movq %r12, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rax
movl %r12d, %ecx
addq %rax, %rcx
movabsq $-0x200000000, %r8      # imm = 0xFFFFFFFE00000000
addq %rcx, %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r15, %r9
callq 0xe3f8
leaq -0x80(%rbp), %rdi
callq 0xecb2
movq -0x50(%rbp), %rbx
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %r15d, %r15d
jle 0xeae7
movq -0x40(%rbp), %rax
movq (%rax), %rax
addl %r12d, %r15d
movq (%rax,%r13), %rax
imulq $0x28, %r15, %rcx
cmpb $0x0, 0x1(%rax,%rcx)
jne 0xeba0
leaq (%rax,%rcx), %rsi
addq $0x8, %rsi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x32c0
leaq 0x3ca9(%rip), %rsi      # 0x12737
movq %rbx, %rdi
callq 0x6261
movq %r14, %r15
movl %eax, %r14d
movq %rbx, %rdi
movq -0x50(%rbp), %rbx
callq 0x3120
testb %r14b, %r14b
je 0xeba0
movq %r15, %rsi
movq %rbx, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rcx, %rax
addq %rcx, %rax
movl %ebx, %r8d
orq %rax, %r8
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %r9
callq 0xe3f8
jmp 0xeba0
movq -0x38(%rbp), %rax
movq 0xc0(%rax), %rax
testq %rax, %rax
je 0xeba0
movq -0x30(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0xeb26
cmpq %rax, (%rcx,%rsi,8)
sete %r8b
orb %r8b, %dil
incq %rsi
jmp 0xeb11
testb $0x1, %dil
jne 0xeba0
leaq -0x80(%rbp), %rsi
movq %rax, (%rsi)
movq -0x30(%rbp), %r15
movq %r15, %rdi
callq 0xf488
movl $0xd0, %esi
movq -0x38(%rbp), %r13
addq 0xc0(%r13), %rsi
movq %rbx, %r12
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x65ba
movq 0xc0(%r13), %rdx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %r12, %rcx
movl %r12d, %eax
addq %rcx, %rax
movabsq $0x200000000, %r8       # imm = 0x200000000
addq %rax, %r8
movq -0x48(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rcx
movq %r15, %r9
callq 0xe3f8
leaq -0x80(%rbp), %rdi
callq 0xecb2
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xebb5
jmp 0xebb5
jmp 0xebb5
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0xecb2
movq %rbx, %rdi
callq 0x33c0
nop