// File: /workspace/github_cpp_makefile_star2_O2/miklhh[P]tetris/src/game.cpp
// Function: iterate_game()
bool iterate_game()
{
    /* Test if the game already set to game-over. */
    if (game_over)
    {
        delete current_block;
        delete next_block;
        return false;
    }

    /* Test if the game just ended. */
    //TODO: Implement later. Don't forget to remove (delete) all blocks.

    /* If the block hits the ground. */
    if (test_movable(current_block, DIRECTION_DOWN) != NO_COLLISION)
    {
        std::cout << "CRACH" << std::endl;
        for (int y = 0; y < MAX_BLOCK_HEIGHT; y++)
        {
            for (int x = 0; x < MAX_BLOCK_WIDTH; x++)
            {
                if (current_block->structure.tile[y][x] == FILLED)
                {
                    field[y + current_block->position.y][x + current_block->position.x].tile = FILLED;
                    field[y + current_block->position.y][x + current_block->position.x].color_b = COLOR_BLOCK_BACKGROUND;
                    field[y + current_block->position.y][x + current_block->position.x].color_f = current_block->color;
                }
            }
        }

        /* Test and remove posible full rows */
        for (int y1 = 0; y1 < GRID_HEIGHT; y1++)
        {
            /* Test if the row 'y1' is full. If it is, test how many row under 'y1'
            * which also is full. */
            if (test_row_full(y1))
            {
                int   row = y1;
                int   n = 1;
                bool  one_found = false;
                for (int y2 = y1; y2 < GRID_HEIGHT; y2++)
                {
                    bool row_full = test_row_full(y2);
                    if (row_full)
                    {
                        n = y2 - y1 + 1;
                        one_found = true;
                    }
                    if (one_found && !row_full)
                        break;
                }

                /* The appropriate rows to remove and the amount of rows under it
                * has been found. Remove accordingly and move field down. */
                for (int y = y1; y < y1 + n; y++)
                {
                    remove_row(y);
                }

                /* Move down the pieces above it and break out of loop. */
                move_field_down(y1, n);
                break;
            }
        }

        /* Remove the old block and prepare a new one. */
        delete current_block;
        current_block   = next_block;
        next_block      = block_spawn();
        return false;
    }

    /* The block hasn't landed. Move it downward a step and continue the game. */
    block_move(current_block, DIRECTION_DOWN);
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq 0x3c38(%rip), %rdi      # 0x6ab0
pushq $0x3
popq %rsi
callq 0x2b63
testl %eax, %eax
je 0x2f27
movq 0x3149(%rip), %rdi      # 0x5fd8
leaq 0x147d(%rip), %rsi      # 0x4313
callq 0x2190
movq %rax, %rdi
callq 0x20b0
movq 0x3c06(%rip), %r14      # 0x6ab0
leaq 0x10(%r14), %rax
xorl %ecx, %ecx
leaq 0x3299(%rip), %rdx      # 0x6150
movabsq $0x100000008, %rsi      # imm = 0x100000008
cmpq $0x4, %rcx
je 0x2f0d
movslq %ecx, %rdi
xorl %r8d, %r8d
cmpq $0x4, %r8
je 0x2f04
cmpl $0x1, (%rax,%r8,4)
jne 0x2eff
movslq 0xc(%r14), %r9
addq %rdi, %r9
movslq 0x8(%r14), %r10
addq %r8, %r10
imulq $0x78, %r9, %r9
addq %rdx, %r9
imulq $0xc, %r10, %r10
movq %rsi, 0x4(%r10,%r9)
movl (%r14), %r11d
movl %r11d, (%r10,%r9)
incq %r8
jmp 0x2ecd
incq %rcx
addq $0x10, %rax
jmp 0x2ec1
xorl %ebx, %ebx
cmpl $0x14, %ebx
je 0x2f9b
movl %ebx, %edi
callq 0x2d9a
testb %al, %al
jne 0x2f36
incl %ebx
jmp 0x2f0f
movq 0x3b82(%rip), %rax      # 0x6ab0
incl 0xc(%rax)
jmp 0x2fcb
pushq $0x1
popq %r15
xorl %r12d, %r12d
movl %r15d, %r14d
movl %ebx, %ebp
cmpl $0x14, %ebp
je 0x2f6d
movl %ebp, %edi
callq 0x2d9a
testb %al, %al
cmovnel %r15d, %r14d
movl %eax, %ecx
orb %r12b, %cl
notb %r12b
orb %al, %r12b
incl %ebp
incl %r15d
testb $0x1, %r12b
movl %ecx, %r12d
jne 0x2f42
leal (%r14,%rbx), %r15d
cmpl %r15d, %ebx
cmovgl %ebx, %r15d
movl %ebx, %ebp
cmpl %ebp, %r15d
je 0x2f8a
movl %ebp, %edi
callq 0x2d00
incl %ebp
jmp 0x2f7a
movl %ebx, %edi
movl %r14d, %esi
callq 0x2d37
movq 0x3b15(%rip), %r14      # 0x6ab0
pushq $0x50
popq %rsi
movq %r14, %rdi
callq 0x21b0
movq 0x3b0b(%rip), %rax      # 0x6ab8
movq %rax, 0x3afc(%rip)      # 0x6ab0
pushq $0x7
popq %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rcx
xorl %edx, %edx
callq 0x2746
movq %rax, 0x3aed(%rip)      # 0x6ab8
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq