// File: /workspace/github_cpp_makefile_star2_O2/kaiming[P]Chamber-Crawler-3k/Board/board.cc
// Function: Board::validDest(std::shared_ptr<WalkableTile>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
std::shared_ptr<WalkableTile> Board::validDest(std::shared_ptr<WalkableTile> centre, std::string direction) {
    
    // Set up variable to hold destination
    std::shared_ptr<Tile> destination;

    // Coordinate system with (0,0) in the top left
    //   first int is x value, increases left to right
    //   second int is y value, increases top to bottom

    // std::pair<int x, int y>
    if (direction == "no") {
        // no = North/Up
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.second - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second - 1][current.first];
    } else if (direction == "so") {
        // so = South/Down
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.second + 1 >= static_cast<int>(floors[floorNum-1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second + 1][current.first];
    } else if (direction == "ea") {
        // ea = East/Right
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first + 1 >= static_cast<int>(floors[floorNum-1][1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second][current.first + 1];
    } else if (direction == "we") {
        // we = West/Left
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second][current.first - 1];
    } else if (direction == "ne") { // ----------------------------------------------------------------------
        // ne = North East/Right and Up
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first + 1 >= static_cast<int>(floors[floorNum-1][1].size()) || current.second - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second - 1][current.first + 1];
    } else if (direction == "nw") {
        // nw = North West/Left and Up 
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first - 1 < 0 || current.second - 1 < 0) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second - 1][current.first - 1];
    } else if (direction == "se") {
        // se = South East/Right and Down
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first + 1 >= static_cast<int>(floors[floorNum-1][1].size()) || current.second + 1 >= static_cast<int>(floors[floorNum-1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second + 1][current.first + 1];
    } else if (direction == "sw") {
        // sw = South West/Left and Down
        std::pair<int, int> current = centre->getCoord();

        // Check if in bounds
        if (current.first - 1 < 0 || current.second + 1 >= static_cast<int>(floors[floorNum-1].size())) {
            return nullptr;
        }

        destination = floors[floorNum-1][current.second + 1][current.first - 1];
    } else {
        // Invalid direction
        return nullptr;
    }

    // Check destination tile to see if it's a WalkableTile
    std::shared_ptr<WalkableTile> typeCheck = std::dynamic_pointer_cast<WalkableTile>(destination);
    if (typeCheck) {
        // Destination is a valid, WalkableTile 
        return typeCheck;        
    }

    return nullptr;

}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0xa7a3(%rip), %rsi      # 0x18301
movq %rcx, %rdi
callq 0x6801
testb %al, %al
je 0xdbaf
movq (%r15), %rdi
callq 0x173bc
movq %rax, %rcx
shrq $0x20, %rcx
testl %ecx, %ecx
jle 0xdeaa
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addl %edx, %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
imulq $0x18, %rdx, %rdx
movq -0x18(%rsi,%rdx), %rdx
imulq $0x18, %rcx, %rcx
movslq %eax, %rsi
shlq $0x4, %rsi
addq (%rdx,%rcx), %rsi
jmp 0xdcc4
leaq 0xa74e(%rip), %rsi      # 0x18304
movq %r12, %rdi
callq 0x6801
testb %al, %al
je 0xdc19
movq (%r15), %rdi
callq 0x173bc
movq %rax, %rcx
movq %rax, %rsi
shrq $0x20, %rsi
incl %esi
movslq 0x108(%r14), %rax
movq (%r14), %rdx
imulq $0x18, %rax, %rax
movq -0x18(%rdx,%rax), %rdi
movq -0x10(%rdx,%rax), %rax
subq %rdi, %rax
pushq $0x18
popq %r8
cqto
idivq %r8
cmpl %eax, %esi
jge 0xdeaa
movslq %esi, %rax
imulq $0x18, %rax, %rax
movslq %ecx, %rsi
shlq $0x4, %rsi
addq (%rdi,%rax), %rsi
jmp 0xdcc4
leaq 0xa6e7(%rip), %rsi      # 0x18307
movq %r12, %rdi
callq 0x6801
testb %al, %al
je 0xdc73
movq (%r15), %rdi
callq 0x173bc
leal 0x1(%rax), %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
imulq $0x18, %rdx, %rdx
movq -0x18(%rsi,%rdx), %rdx
movq 0x20(%rdx), %rsi
subq 0x18(%rdx), %rsi
shrq $0x4, %rsi
cmpl %esi, %ecx
jge 0xdeaa
sarq $0x20, %rax
imulq $0x18, %rax, %rax
movslq %ecx, %rsi
shlq $0x4, %rsi
addq (%rdx,%rax), %rsi
jmp 0xdcc4
leaq 0xa690(%rip), %rsi      # 0x1830a
movq %r12, %rdi
callq 0x6801
testb %al, %al
je 0xdd15
movq (%r15), %rdi
callq 0x173bc
testl %eax, %eax
jle 0xdeaa
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addl %eax, %esi
movslq 0x108(%r14), %rcx
movq (%r14), %rdx
imulq $0x18, %rcx, %rcx
sarq $0x20, %rax
movq -0x18(%rdx,%rcx), %rcx
imulq $0x18, %rax, %rax
shlq $0x4, %rsi
addq (%rcx,%rax), %rsi
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x12742
movq %rsp, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x12363
movq (%r15), %rax
testq %rax, %rax
je 0xdd00
movq %rax, (%rbx)
movq 0x8(%rsp), %rax
andq $0x0, 0x8(%rsp)
movq %rax, 0x8(%rbx)
andq $0x0, (%rsp)
jmp 0xdd06
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
leaq 0x8(%rsp), %rdi
callq 0x6c00
jmp 0xdeb0
leaq 0xa5f1(%rip), %rsi      # 0x1830d
movq %r12, %rdi
callq 0x6801
testb %al, %al
je 0xdd72
movq (%r15), %rdi
callq 0x173bc
leal 0x1(%rax), %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
imulq $0x18, %rdx, %rdx
movq -0x18(%rsi,%rdx), %rdx
movq 0x20(%rdx), %rsi
subq 0x18(%rdx), %rsi
shrq $0x4, %rsi
cmpl %esi, %ecx
jge 0xdeaa
shrq $0x20, %rax
testl %eax, %eax
jle 0xdeaa
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addl %esi, %eax
jmp 0xdc62
leaq 0xa597(%rip), %rsi      # 0x18310
movq %r12, %rdi
callq 0x6801
testb %al, %al
je 0xddcc
movq (%r15), %rdi
callq 0x173bc
testl %eax, %eax
jle 0xdeaa
movq %rax, %rcx
shrq $0x20, %rcx
testl %ecx, %ecx
jle 0xdeaa
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addq %rdx, %rax
addl %edx, %ecx
movslq 0x108(%r14), %rdx
movq (%r14), %rsi
imulq $0x18, %rdx, %rdx
movq -0x18(%rsi,%rdx), %rdx
imulq $0x18, %rcx, %rcx
movl %eax, %esi
jmp 0xdba2
leaq 0xa540(%rip), %rsi      # 0x18313
movq %r12, %rdi
callq 0x6801
testb %al, %al
je 0xde41
movq (%r15), %rdi
callq 0x173bc
movq %rax, %rcx
leal 0x1(%rax), %esi
movslq 0x108(%r14), %rdx
movq (%r14), %rax
imulq $0x18, %rdx, %rdx
movq -0x18(%rax,%rdx), %rdi
movq 0x20(%rdi), %r8
subq 0x18(%rdi), %r8
shrq $0x4, %r8
cmpl %r8d, %esi
jge 0xdeaa
addq %rdx, %rax
shrq $0x20, %rcx
incl %ecx
movq -0x10(%rax), %rax
subq %rdi, %rax
pushq $0x18
popq %r8
cqto
idivq %r8
cmpl %eax, %ecx
jge 0xdeaa
movslq %ecx, %rax
imulq $0x18, %rax, %rax
movslq %esi, %rsi
jmp 0xdc0c
leaq 0xa4ce(%rip), %rsi      # 0x18316
movq %r12, %rdi
callq 0x6801
testb %al, %al
je 0xdeaa
movq (%r15), %rdi
callq 0x173bc
movq %rax, %rcx
testl %ecx, %ecx
jle 0xdeaa
movq %rcx, %rsi
shrq $0x20, %rsi
incl %esi
movslq 0x108(%r14), %rax
movq (%r14), %rdx
imulq $0x18, %rax, %rax
movq -0x18(%rdx,%rax), %rdi
movq -0x10(%rdx,%rax), %rax
subq %rdi, %rax
pushq $0x18
popq %r8
cqto
idivq %r8
cmpl %eax, %esi
jge 0xdeaa
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq %rax, %rcx
movslq %esi, %rax
imulq $0x18, %rax, %rax
movl %ecx, %esi
jmp 0xdc0c
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
leaq 0x18(%rsp), %rdi
callq 0x6c00
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0xded7
jmp 0xded7
jmp 0xded7
jmp 0xded7
jmp 0xded7
jmp 0xded7
jmp 0xded7
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x6c00
movq %rbx, %rdi
callq 0x5390