// File: /workspace/github_c_makefile_star2_O2/kobolt[P]curses-collection/cgame/cgame.c
// Function: main
int main(void)
{
  int i, maxx, maxy, direction;
  char *done = NULL;

  initscr();
  if (has_colors()) {
    start_color();
    init_pair(PAIR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
    init_pair(PAIR_CYAN,   COLOR_CYAN,   COLOR_BLACK);
  }
  noecho();
  keypad(stdscr, TRUE);
  getmaxyx(stdscr, maxy, maxx);

  if (maxy != GAME_ROWS || maxx != GAME_COLS) {
    endwin();
    fprintf(stderr, "Terminal must be %dx%d!\n", GAME_ROWS, GAME_COLS);
    return 1;
  }

  init_game();

  while (done == NULL) {
    draw_screen();

    /* Get player input. */
    direction = DIRECTION_NONE;
    switch (getch()) {
    case KEY_LEFT:
      direction = DIRECTION_LEFT;
      break;

    case KEY_RIGHT:
      direction = DIRECTION_RIGHT;
      break;

    case KEY_UP:
      direction = DIRECTION_UP;
      break;

    case KEY_DOWN:
      direction = DIRECTION_DOWN;
      break;

    case KEY_RESIZE:
      done = "Window resize detected, aborted...";
      break;

    case 'q':
    case 'Q':
    case '\e':
      done = "Quitting...";
      break;

    default:
      break;
    }

    /* Move player, and check for collisions. */
    switch (move_object_check(&player, direction)) {
    case MOVE_OK:
      steps_taken++;
      break;

    case MOVE_BLOCKED:
      break;

    case MOVE_ON_MINE:
      done = "You were killed by a mine!";
      break;

    case MOVE_ON_MONEY:
      for (i = 0; i < AMOUNT_OF_MONEY; i++) {
        if (player.y == money[i].y && player.x == money[i].x) {
          money_collected++;
          money[i].taken = 1;
        }
      }
      break;

    case MOVE_ON_ENEMY:
      done = "You moved onto the enemy!";
      break;
    }

    draw_screen(); /* To update the positions of the objects, not visible. */

    /* Move enemy, and check for collision with player. */
    if (move_object_check(&enemy, direction_to_player()) == MOVE_BLOCKED) {
      enemy_idle_count++;
      if (enemy_idle_count > 3) {
        if (move_object_check(&enemy, random_direction()) == MOVE_OK)
          enemy_idle_count = 0;
      }
    }

    if (player.y == enemy.y && player.x == enemy.x) {
      done = "You were killed by the enemy!";
      break;
    }

    if (money_collected == AMOUNT_OF_MONEY)
      done = "Well done! all money collected!";
  }

  draw_screen();
  endwin();

  fprintf(stderr, "%s\n", done);
  fprintf(stderr, "You collected %d$ of %d$.\n",
    money_collected, AMOUNT_OF_MONEY);
  fprintf(stderr, "You walked %d step%s.\n", 
    steps_taken, (steps_taken == 1) ? "" : "s");

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
callq 0x10a0
callq 0x1030
testb %al, %al
je 0x1294
callq 0x10c0
pushq $0x1
popq %rdi
pushq $0x3
popq %rsi
xorl %edx, %edx
callq 0x1110
pushq $0x2
popq %rdi
pushq $0x6
popq %rsi
xorl %edx, %edx
callq 0x1110
callq 0x1090
movq 0x2d38(%rip), %rbx      # 0x3fd8
movq (%rbx), %rdi
pushq $0x1
popq %rsi
callq 0x10d0
movq (%rbx), %rax
testq %rax, %rax
jne 0x12de
callq 0x1150
movq 0x2d39(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd7f(%rip), %rsi       # 0x2048
pushq $0x18
popq %rdx
pushq $0x50
popq %rcx
xorl %eax, %eax
callq 0x10e0
pushq $0x1
popq %rbx
jmp 0x169b
cmpw $0x17, 0x4(%rax)
jne 0x12b3
cmpw $0x4f, 0x6(%rax)
jne 0x12b3
andl $0x0, 0x2f8d(%rip)      # 0x4280
andl $0x0, 0x2dea(%rip)      # 0x40e4
andl $0x0, 0x2f97(%rip)      # 0x4298
xorl %edi, %edi
callq 0x10f0
movl %eax, %edi
callq 0x1060
movb $0x40, 0x2dc2(%rip)     # 0x40d8
leaq 0x2db3(%rip), %r14      # 0x40d0
andl $0x0, 0x2db8(%rip)      # 0x40dc
leaq 0x2da9(%rip), %rsi      # 0x40d4
movq %r14, %rdi
callq 0x18c8
movq %r14, %rdi
callq 0x194b
movb $0x4d, 0x2f4a(%rip)     # 0x428c
leaq 0x2f3b(%rip), %r14      # 0x4284
movl $0x200100, 0x2f3d(%rip) # imm = 0x200100
leaq 0x2f2e(%rip), %rsi      # 0x4288
movq %r14, %rdi
callq 0x18c8
movq %r14, %rdi
callq 0x194b
leaq 0x2d7f(%rip), %r14      # 0x40f0
pushq $0x14
popq %rbx
subq $0x1, %rbx
jb 0x13a1
movb $0x24, 0x8(%r14)
movq $0x200000, 0xc(%r14)    # imm = 0x200000
leaq 0x4(%r14), %rsi
movq %r14, %rdi
callq 0x18c8
movq %r14, %rdi
callq 0x194b
addq $0x14, %r14
jmp 0x1374
leaq 0x2ef8(%rip), %r14      # 0x42a0
pushq $0xa
popq %rbx
subq $0x1, %rbx
jb 0x13d8
movb $0x2a, 0x8(%r14)
movl $0x200, 0xc(%r14)       # imm = 0x200
leaq 0x4(%r14), %rsi
movq %r14, %rdi
callq 0x18c8
movq %r14, %rdi
callq 0x194b
addq $0x14, %r14
jmp 0x13ab
leaq 0x2f91(%rip), %r14      # 0x4370
pushq $0x5
popq %rbx
subq $0x1, %rbx
jb 0x140f
movb $0x23, 0x8(%r14)
movl $0x100, 0xc(%r14)       # imm = 0x100
leaq 0x4(%r14), %rsi
movq %r14, %rdi
callq 0x18c8
movq %r14, %rdi
callq 0x194b
addq $0x14, %r14
jmp 0x13e2
leaq 0x2cba(%rip), %r14      # 0x40d0
leaq 0x2e67(%rip), %r15      # 0x4284
leaq 0xcbf(%rip), %rbx       # 0x20e3
leaq 0xbe9(%rip), %r13       # 0x2014
leaq 0x2cbe(%rip), %rbp      # 0x40f0
xorl %r12d, %r12d
testq %r12, %r12
jne 0x162e
callq 0x16ac
movq 0x2b8e(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1080
leal -0x102(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x147e
xorl %esi, %esi
cmpl $0x1b, %eax
je 0x1492
cmpl $0x51, %eax
je 0x1492
cmpl $0x71, %eax
je 0x1492
cmpl $0x19a, %eax            # imm = 0x19A
jne 0x14a8
leaq 0xbe5(%rip), %r12       # 0x2061
jmp 0x14ab
leaq 0xb7f(%rip), %rdx       # 0x2004
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
pushq $0x4
jmp 0x14a5
leaq 0xbeb(%rip), %r12       # 0x2084
jmp 0x14ab
pushq $0x1
jmp 0x14a5
pushq $0x2
jmp 0x14a5
pushq $0x3
popq %rax
movl %eax, %esi
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x1790
cmpl $0x4, %eax
ja 0x151b
movl %eax, %eax
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
incl 0x2c1a(%rip)            # 0x40e4
jmp 0x151b
movl 0x2bfe(%rip), %eax      # 0x40d0
movl 0x2da8(%rip), %ecx      # 0x4280
movl 0x2bf6(%rip), %edx      # 0x40d4
pushq $0x10
popq %rsi
cmpq $0x1a0, %rsi            # imm = 0x1A0
je 0x151b
cmpl -0x10(%rsi,%rbp), %eax
jne 0x1505
cmpl -0xc(%rsi,%rbp), %edx
jne 0x1505
incl %ecx
movl %ecx, 0x2d82(%rip)      # 0x4280
movl $0x1, (%rsi,%rbp)
addq $0x14, %rsi
jmp 0x14e1
leaq 0xb7e(%rip), %r12       # 0x2090
jmp 0x151b
leaq 0xb90(%rip), %r12       # 0x20ab
callq 0x16ac
movl 0x2d5e(%rip), %eax      # 0x4284
subl 0x2ba4(%rip), %eax      # 0x40d0
movl 0x2d56(%rip), %ecx      # 0x4288
subl 0x2b9c(%rip), %ecx      # 0x40d4
js 0x1567
je 0x1580
cmpl %eax, %ecx
setg %sil
movl %eax, %edx
negl %edx
cmpl %edx, %ecx
setl %dil
testb %dil, %sil
jne 0x1580
xorl %esi, %esi
cmpl %eax, %ecx
setl %sil
cmpl %edx, %ecx
leal 0x1(%rsi,%rsi), %esi
pushq $0x1
popq %rax
cmovlel %eax, %esi
jmp 0x158f
movl %eax, %edx
negl %edx
cmpl %edx, %ecx
setg %sil
cmpl %eax, %ecx
setl %dil
testb %dil, %sil
je 0x160b
pushq $0x3
popq %rsi
testl %eax, %eax
jg 0x158f
shrl $0x1d, %eax
andl $-0x4, %eax
movl %eax, %esi
movq %r15, %rdi
callq 0x1790
cmpl $0x1, %eax
jne 0x15df
movl 0x2cf6(%rip), %eax      # 0x4298
leal 0x1(%rax), %ecx
movl %ecx, 0x2ced(%rip)      # 0x4298
cmpl $0x3, %eax
jl 0x15df
callq 0x1100
cqto
pushq $0x4
popq %rcx
idivq %rcx
leal 0x1(%rdx), %esi
cmpq $0x4, %rdx
movl $0x0, %eax
cmovael %eax, %esi
movq %r15, %rdi
callq 0x1790
testl %eax, %eax
jne 0x15df
andl $0x0, 0x2cb9(%rip)      # 0x4298
movl 0x2aeb(%rip), %eax      # 0x40d0
cmpl 0x2c99(%rip), %eax      # 0x4284
jne 0x15fb
movl 0x2ae1(%rip), %eax      # 0x40d4
cmpl 0x2c8f(%rip), %eax      # 0x4288
je 0x1627
cmpl $0x14, 0x2c7e(%rip)     # 0x4280
cmoveq %rbx, %r12
jmp 0x1435
cmpl %eax, %ecx
setg %dil
cmpl %edx, %ecx
setl %cl
pushq $0x2
popq %rsi
testb %cl, %dil
jne 0x1587
jmp 0x158f
leaq 0xa97(%rip), %r12       # 0x20c5
callq 0x16ac
callq 0x1150
movq 0x29b9(%rip), %r14      # 0x3ff8
movq (%r14), %rdi
leaq 0xaba(%rip), %rsi       # 0x2103
xorl %ebx, %ebx
movq %r12, %rdx
xorl %eax, %eax
callq 0x10e0
movq (%r14), %rdi
movl 0x2c22(%rip), %edx      # 0x4280
leaq 0xaa2(%rip), %rsi       # 0x2107
pushq $0x14
popq %rcx
xorl %eax, %eax
callq 0x10e0
movq (%r14), %rdi
movl 0x2a6c(%rip), %edx      # 0x40e4
cmpl $0x1, %edx
leaq 0x9de(%rip), %rax       # 0x2060
leaq 0xab0(%rip), %rcx       # 0x2139
cmoveq %rax, %rcx
leaq 0xa8e(%rip), %rsi       # 0x2122
xorl %eax, %eax
callq 0x10e0
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq