// File: EstherJin[P]TetrisRemake/basicboard.cc
// Function: BasicBoard::dropBlock()
int BasicBoard::dropBlock() {
	// keep dropping
	for (int i = 0; i < gridRows; ++i) {
		bool valid = validMove(currentBlock->downPos(1));
		if (valid) {
			currentBlock->down(1);
		}
		else break;
	}

	// change to block 2
	currentBlock->detach();
	if (!textOnly) currentBlock->detach();
	Block2 b2(currentBlock->getNum(),lvl->getLevel());
	activeBlocks.emplace_back(b2);
	vector<Coordinates> blockCoords = currentBlock->getPos();
  	for (auto coord:blockCoords) {
		grid[coord.row].changeCell(coord.col, currentBlock->getType());
		grid[coord.row].attachObserver(coord.col, &b2);
	}

	// check lines cleared
	int linesCleared = 0;
	for (int i = gridRows - 1; i >= 0; --i) {
		if (grid[i].checkFull()) {
			++linesCleared;
			grid.erase(grid.begin() + i);
			Row newRow {0, &sg, &gd};
			grid.insert(grid.begin(), newRow);
			for (int j = i; j >= 0; --j) {
				grid[j].changeRowNum(j);
			}
			grid[i].notifAbout();
			++i;
		}
	}

	// calculate score
	if (linesCleared > 0)  score += (lvl->getLevel() + linesCleared) * (lvl->getLevel() + linesCleared);
	for (int i = 0; i < activeBlocks.size(); ++i) {
		if (activeBlocks[i].checkDeleted()) {
			score += (activeBlocks[i].getLevelDropped() + 1) * (activeBlocks[i].getLevelDropped() + 1);
			activeBlocks.erase(activeBlocks.begin() + i);
		}
	}
	if (!textOnly) gd.printScore(score);

	// switch blocks
	currentBlock = nullptr;
	std::swap(currentBlock, nextBlock);

	// check game over
	if (!validMove(currentBlock->getPos())) {
		currentBlock = nullptr;
		throw "game over";
	}

	currentBlock->attach(&sg);
	if (!textOnly) currentBlock->attach(&gd);
	currentBlock->notifCurrPos();

	return linesCleared;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x128(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x128(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0xc0(%rcx), %eax
jge 0x8c69
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rsi
leaq -0x28(%rbp), %rdi
movq %rdi, -0x138(%rbp)
movl $0x1, %edx
callq 0x134a0
movq -0x128(%rbp), %rdi
movq -0x138(%rbp), %rsi
movq (%rdi), %rax
movq 0x50(%rax), %rax
callq *%rax
movb %al, -0x129(%rbp)
jmp 0x8c01
leaq -0x28(%rbp), %rdi
callq 0xa880
movb -0x129(%rbp), %al
andb $0x1, %al
movb %al, -0xd(%rbp)
testb $0x1, -0xd(%rbp)
je 0x8c57
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rdi
movl $0x1, %esi
callq 0x13350
jmp 0x8c59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xa880
jmp 0x9471
jmp 0x8c69
jmp 0x8c5b
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x8ba1
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rdi
addq $0x8, %rdi
callq 0x290a0
movq -0x128(%rbp), %rax
testb $0x1, 0xa8(%rax)
jne 0x8cb7
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rdi
addq $0x8, %rdi
callq 0x290a0
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rdi
callq 0x13600
movq -0x128(%rbp), %rdi
movl %eax, -0x144(%rbp)
addq $0x90, %rdi
callq 0xa7c0
movq %rax, %rdi
callq 0x23610
movl -0x144(%rbp), %esi
movl %eax, %edx
leaq -0x48(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x13e80
movq -0x128(%rbp), %rdi
movq -0x140(%rbp), %rsi
addq $0x58, %rdi
callq 0xa8e0
jmp 0x8d24
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x130f0
jmp 0x8d45
leaq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0xa960
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0xa990
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa9c0
testb $0x1, %al
jne 0x8d7d
jmp 0x8e48
leaq -0x70(%rbp), %rdi
callq 0xaa00
movq -0x128(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x80(%rbp)
addq $0x70, %rdi
movslq -0x80(%rbp), %rsi
callq 0xaa20
movq -0x128(%rbp), %rdi
movq %rax, -0x158(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x14c(%rbp)
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rdi
callq 0x130d0
movb %al, -0x145(%rbp)
jmp 0x8dd4
movl -0x14c(%rbp), %esi
movq -0x158(%rbp), %rdi
movb -0x145(%rbp), %al
movsbl %al, %edx
callq 0x241f0
jmp 0x8df1
movq -0x128(%rbp), %rdi
addq $0x70, %rdi
movslq -0x80(%rbp), %rsi
callq 0xaa20
movq %rax, %rdi
movl -0x7c(%rbp), %esi
leaq -0x48(%rbp), %rdx
callq 0x24230
jmp 0x8e16
jmp 0x8e18
leaq -0x70(%rbp), %rdi
callq 0xaa40
jmp 0x8d67
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x9468
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x945f
movq -0x128(%rbp), %rax
movl $0x0, -0x84(%rbp)
movl 0xc0(%rax), %eax
subl $0x1, %eax
movl %eax, -0x88(%rbp)
cmpl $0x0, -0x88(%rbp)
jl 0x9070
movq -0x128(%rbp), %rdi
addq $0x70, %rdi
movslq -0x88(%rbp), %rsi
callq 0xaa20
movq %rax, %rdi
callq 0x24140
movb %al, -0x159(%rbp)
jmp 0x8e9c
movb -0x159(%rbp), %al
testb $0x1, %al
jne 0x8eab
jmp 0x905a
movq -0x128(%rbp), %rdi
movl -0x84(%rbp), %eax
incl %eax
movl %eax, -0x84(%rbp)
addq $0x70, %rdi
movq %rdi, -0x170(%rbp)
callq 0xaad0
movq %rax, -0xa0(%rbp)
movslq -0x88(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0xab00
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xab40
movq -0x170(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0xaa60
movq %rax, -0x168(%rbp)
jmp 0x8f20
movq -0x128(%rbp), %rcx
movq -0x168(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq %rcx, %rdx
addq $0x8, %rdx
addq $0x30, %rcx
leaq -0xc8(%rbp), %rdi
xorl %esi, %esi
callq 0x24060
jmp 0x8f50
movq -0x128(%rbp), %rdi
addq $0x70, %rdi
movq %rdi, -0x180(%rbp)
callq 0xaad0
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0xab40
movq -0x180(%rbp), %rdi
movq -0xd0(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
callq 0xab80
movq %rax, -0x178(%rbp)
jmp 0x8fa4
movq -0x178(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0xe4(%rbp)
cmpl $0x0, -0xe4(%rbp)
jl 0x901e
movq -0x128(%rbp), %rdi
addq $0x70, %rdi
movslq -0xe4(%rbp), %rsi
callq 0xaa20
movq %rax, %rdi
movl -0xe4(%rbp), %esi
callq 0x241a0
jmp 0x8fee
jmp 0x8ff0
movl -0xe4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x8fbe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x9e50
jmp 0x945f
movq -0x128(%rbp), %rdi
addq $0x70, %rdi
movslq -0x88(%rbp), %rsi
callq 0xaa20
movq %rax, %rdi
callq 0x242d0
jmp 0x903f
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x9e50
jmp 0x905c
movl -0x88(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x8e68
cmpl $0x0, -0x84(%rbp)
jle 0x90ff
movq -0x128(%rbp), %rdi
addq $0x90, %rdi
callq 0xa7c0
movq %rax, %rdi
callq 0x23610
movl %eax, -0x184(%rbp)
jmp 0x90a0
movq -0x128(%rbp), %rdi
movl -0x184(%rbp), %eax
movl -0x84(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x18c(%rbp)
addq $0x90, %rdi
callq 0xa7c0
movq %rax, %rdi
callq 0x23610
movl %eax, -0x188(%rbp)
jmp 0x90d7
movq -0x128(%rbp), %rax
movl -0x18c(%rbp), %ecx
movl -0x188(%rbp), %edx
addl -0x84(%rbp), %edx
imull %edx, %ecx
addl 0x88(%rax), %ecx
movl %ecx, 0x88(%rax)
movl $0x0, -0xe8(%rbp)
movq -0x128(%rbp), %rdi
movslq -0xe8(%rbp), %rax
movq %rax, -0x198(%rbp)
addq $0x58, %rdi
callq 0xad90
movq %rax, %rcx
movq -0x198(%rbp), %rax
cmpq %rcx, %rax
jae 0x9276
movq -0x128(%rbp), %rdi
addq $0x58, %rdi
movslq -0xe8(%rbp), %rsi
callq 0xadb0
movq %rax, %rdi
callq 0x13f20
movb %al, -0x199(%rbp)
jmp 0x9161
movb -0x199(%rbp), %al
testb $0x1, %al
jne 0x9170
jmp 0x9260
movq -0x128(%rbp), %rdi
addq $0x58, %rdi
movslq -0xe8(%rbp), %rsi
callq 0xadb0
movq %rax, %rdi
callq 0x13f40
movl %eax, -0x1a0(%rbp)
jmp 0x9197
movq -0x128(%rbp), %rdi
movl -0x1a0(%rbp), %eax
incl %eax
movl %eax, -0x1a8(%rbp)
addq $0x58, %rdi
movslq -0xe8(%rbp), %rsi
callq 0xadb0
movq %rax, %rdi
callq 0x13f40
movl %eax, -0x1a4(%rbp)
jmp 0x91cc
movq -0x128(%rbp), %rdi
movl -0x1a8(%rbp), %ecx
movl -0x1a4(%rbp), %eax
incl %eax
imull %eax, %ecx
movl 0x88(%rdi), %eax
addl %ecx, %eax
movl %eax, 0x88(%rdi)
addq $0x58, %rdi
movq %rdi, -0x1b8(%rbp)
callq 0xae40
movq %rax, -0x100(%rbp)
movslq -0xe8(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0xae70
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xaeb0
movq -0x1b8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0xadd0
movq %rax, -0x1b0(%rbp)
jmp 0x9252
movq -0x1b0(%rbp), %rax
movq %rax, -0x108(%rbp)
jmp 0x9262
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x9109
movq -0x128(%rbp), %rax
testb $0x1, 0xa8(%rax)
jne 0x92a3
movq -0x128(%rbp), %rax
movq %rax, %rdi
addq $0x30, %rdi
movl 0x88(%rax), %esi
callq 0x2b8c0
jmp 0x92a1
jmp 0x92a3
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
movq %rdi, -0x1c0(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0xa4f0
movq -0x128(%rbp), %rsi
movq -0x1c0(%rbp), %rdi
addq $0x98, %rsi
callq 0xa520
movq -0x1c0(%rbp), %rdi
callq 0xa550
movq %rax, %rsi
leaq -0x120(%rbp), %rdi
callq 0x130f0
jmp 0x92f8
movq -0x128(%rbp), %rdi
movq (%rdi), %rax
movq 0x50(%rax), %rax
leaq -0x120(%rbp), %rsi
callq *%rax
movb %al, -0x1c1(%rbp)
jmp 0x9317
movb -0x1c1(%rbp), %al
xorb $-0x1, %al
movb %al, -0x1c2(%rbp)
leaq -0x120(%rbp), %rdi
callq 0xa880
movb -0x1c2(%rbp), %al
testb $0x1, %al
jne 0x933d
jmp 0x93a7
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x1d0(%rbp)
callq 0xa4f0
movl $0x8, %edi
callq 0x61a0
movq -0x1d0(%rbp), %rdx
movq %rax, %rdi
leaq 0x23d1f(%rip), %rax     # 0x2d095
movq %rax, (%rdi)
movq 0x38c28(%rip), %rsi     # 0x41fa8
callq 0x65b0
jmp 0x947a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x120(%rbp), %rdi
callq 0xa880
jmp 0x945f
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq -0x128(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rdi
addq $0x8, %rsi
callq 0x29070
jmp 0x93d3
movq -0x128(%rbp), %rax
testb $0x1, 0xa8(%rax)
jne 0x9411
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq -0x128(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rdi
addq $0x30, %rsi
callq 0x29070
jmp 0x940f
jmp 0x9411
movq -0x128(%rbp), %rdi
addq $0xb8, %rdi
callq 0xa550
movq %rax, %rdi
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
jmp 0x9432
movl -0x84(%rbp), %eax
movl %eax, -0x1d4(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xa880
leaq -0x48(%rbp), %rdi
callq 0x13ed0
movl -0x1d4(%rbp), %eax
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0xa880
leaq -0x48(%rbp), %rdi
callq 0x13ed0
movq -0x30(%rbp), %rdi
callq 0x65c0
nopw (%rax,%rax)