// File: /workspace/github_cpp_makefile_star2_O2/czinn[P]homeworlds/game.cc
// Function: Game::perform_action(Action&)
bool Game::perform_action(Action& action) {
  if (action.player != cur_player_) {
    return false;
  }

  if (sacrifice_actions_ > 0) {
    sacrifice_actions_--;
  }

  switch(action.type) {
    case PASS:
      advance_cur_player();
      break;

    case ATTACK: {
        // Need to find player of attacked ship
        const System& system = get_system(action.system);
        const auto& ship = find_if(system.ships.begin(), system.ships.end(),
            [&action](const Ship& ship) {
              return ship.pyramid == action.ship && ship.player != action.player;
            });
        if (ship == system.ships.end()) {
          return false;
        }
        remove_ship(action.system, *ship);
        add_ship(action.system, Ship{action.player, ship->pyramid});
        done_main_action_ = true;
      }
      break;

    case DISCOVER: {
        int system_id = create_system({action.target});
        action.system_target = system_id;
        add_ship(system_id,
            remove_ship(action.system, Ship{action.player, action.ship}));
        if (get_system(action.system).ships.size() == 0) {
          destroy_system(action.system);
        }
        done_main_action_ = true;
      }
      break;

    case TRAVEL:
      add_ship(action.system_target,
          remove_ship(action.system, Ship{action.player, action.ship}));
      if (get_system(action.system).ships.size() == 0) {
        destroy_system(action.system);
      }
      done_main_action_ = true;
      break;

    case BUILD:
      add_ship(action.system, Ship{action.player, action.ship});
      done_main_action_ = true;
      break;

    case TRADE:
      remove_ship(action.system, Ship{action.player, action.ship});
      add_ship(action.system, Ship{action.player, action.target});
      done_main_action_ = true;
      break;

    case SACRIFICE:
      remove_ship(action.system, Ship{action.player, action.ship});
      if (get_system(action.system).ships.size() == 0) {
        destroy_system(action.system);
      }
      sacrifice_colour_ = action.ship.colour;
      sacrifice_actions_ = action.ship.size;
      done_main_action_ = true;
      break;

    case CATASTROPHE:
      apply_catastrophe(action.system, action.ship.colour);
      break;

  }

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl (%rsi), %eax
cmpl 0x4(%rdi), %eax
jne 0x3614
movq %rsi, %r15
movq %rdi, %r14
movl 0x14(%rdi), %ecx
testl %ecx, %ecx
jle 0x330c
decl %ecx
movl %ecx, 0x14(%r14)
movl 0x4(%r15), %ecx
movb $0x1, %bpl
cmpq $0x7, %rcx
ja 0x3616
leaq 0x6d48(%rip), %rdx      # 0xa06c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %r14, %rdi
callq 0x365c
jmp 0x3616
movl 0x8(%r15), %esi
leaq 0x10(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc(%r15), %rax
movq %rax, 0x4(%rdx)
jmp 0x3527
movq 0x18(%r15), %rax
leaq 0x38(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %rbx
pushq $0x1
popq %rdx
leaq 0xf(%rsp), %rcx
movq %rbx, %rdi
callq 0x3fb2
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x391a
movl %eax, %ebx
leaq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x4eb8
movl %ebx, 0x14(%r15)
movl 0x8(%r15), %esi
movl (%r15), %eax
leaq 0x38(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc(%r15), %rax
movq %rax, 0x4(%rdx)
movq %r14, %rdi
callq 0x367e
movq %rax, (%r12)
movl %edx, 0x8(%r12)
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
jmp 0x33f8
movl 0x14(%r15), %ebx
movl 0x8(%r15), %esi
leaq 0x38(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc(%r15), %rax
movq %rax, 0x4(%rdx)
movq %r14, %rdi
callq 0x367e
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
movq %r14, %rdi
movl %ebx, %esi
movq %rcx, %rdx
callq 0x384c
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x284a
movq 0x28(%rax), %rcx
cmpq 0x20(%rax), %rcx
jne 0x3576
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x39ba
jmp 0x3576
movl 0x8(%r15), %esi
movl 0x10(%r15), %edx
movq %r14, %rdi
callq 0x3ab8
jmp 0x3616
movl 0x8(%r15), %esi
movq %r14, 0x30(%rsp)
movq %r14, %rdi
callq 0x284a
movq 0x20(%rax), %r13
movq 0x28(%rax), %rbp
leaq 0x10(%rsp), %r14
movq %r15, 0x28(%rsp)
movq %r15, (%r14)
movq %rbp, (%rsp)
movq %rbp, %r15
subq %r13, %r15
pushq $0xc
popq %rcx
movq %r15, %rax
cqto
idivq %rcx
movq %rax, %rbx
sarq $0x2, %rbx
testq %rbx, %rbx
jle 0x3580
movq %r14, %rdi
movq %r13, %rsi
callq 0x3e5e
testb %al, %al
jne 0x35c7
movq %r14, %rbp
leaq 0xc(%r13), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3e5e
testb %al, %al
jne 0x35ca
leaq 0x18(%r13), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3e5e
testb %al, %al
jne 0x35ca
leaq 0x24(%r13), %r12
movq %rbp, %r14
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3e5e
testb %al, %al
jne 0x35ca
addq $0x30, %r13
decq %rbx
addq $-0x30, %r15
jmp 0x347f
movl 0x8(%r15), %esi
leaq 0x10(%rsp), %rbx
movl %eax, (%rbx)
movq 0xc(%r15), %rax
movq %rax, 0x4(%rbx)
movq %r14, %rdi
movq %rbx, %rdx
callq 0x367e
movl 0x8(%r15), %esi
movl (%r15), %eax
movl %eax, (%rbx)
movq 0x18(%r15), %rax
movq %rax, 0x4(%rbx)
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x384c
jmp 0x3576
movl 0x8(%r15), %esi
leaq 0x10(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc(%r15), %rax
movq %rax, 0x4(%rdx)
movq %r14, %rdi
callq 0x367e
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x284a
movq 0x28(%rax), %rcx
cmpq 0x20(%rax), %rcx
jne 0x356e
movl 0x8(%r15), %esi
movq %r14, %rdi
callq 0x39ba
movq 0xc(%r15), %rax
movq %rax, 0x14(%r14)
movb $0x1, 0x8(%r14)
jmp 0x3616
movq %r15, %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
cmpq $0x1, %rax
je 0x362b
cmpq $0x2, %rax
je 0x35b6
cmpq $0x3, %rax
jne 0x3614
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x3e5e
testb %al, %al
jne 0x35c7
addq $0xc, %r13
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x3e5e
testb %al, %al
je 0x3627
movq %r13, %r12
movq (%rsp), %rax
cmpq %rax, %r12
movb $0x1, %bpl
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r14
je 0x3614
movl 0x8(%r14), %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x367e
movl 0x8(%r14), %esi
movl (%r14), %eax
leaq 0x10(%rsp), %rdx
movl %eax, (%rdx)
movq 0x4(%r12), %rax
movq %rax, 0x4(%rdx)
movq %rbx, %rdi
callq 0x384c
movb $0x1, 0x8(%rbx)
jmp 0x3616
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0xc, %r13
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x3e5e
testb %al, %al
movq (%rsp), %rax
cmoveq %rax, %r13
movq %r13, %r12
jmp 0x35ce
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4eb8
movq %rbx, %rdi
callq 0x2300