// File: /workspace/github_cpp_makefile_star2_O2/wrighteagle2d[P]wrighteaglebase/Debug/../src/InterceptModel.cpp
// Function: InterceptModel::CalcInterception(Vector const&, Vector const&, double, PlayerState const*, InterceptModel::InterceptSolution*)
void InterceptModel::CalcInterception(const Vector &ball_pos,
                                      const Vector &ball_vel,
                                      const double buffer,
                                      const PlayerState *player,
                                      InterceptSolution *sol) {
  const double &alpha = ServerParam::instance().ballDecay();
  const double &ln_alpha = ServerParam::instance().logBallDecay();

  Vector start_pt = (player->GetPos() - ball_pos).Rotate(-ball_vel.Dir());

  //取得模型输入
  const double x0 = start_pt.X();
  const double y0 = start_pt.Y();
  const double ball_spd = ball_vel.Mod();

  const double &player_spd = player->GetEffectiveSpeedMax();
  const double &kick_area = buffer;
  const double cycle_delay = double(player->GetPosDelay());

  const double max_x = ball_spd / (1.0 - alpha);

  //	if (Logger::instance().CurrentTime() == Time(205, 0) &&
  //player->GetUnum() == 4){ 		PlotInterceptCurve(x0, y0, ball_spd, player_spd,
  //kick_area, cycle_delay, max_x);
  //	}

  //特殊情况处理
  const double s = Sqrt(x0 * x0 + y0 * y0);
  const double self_fix = kick_area + cycle_delay * player_spd;

  if (s < self_fix) { //最好的情况已经可踢
    sol->tangc = 0;
    sol->interc = 1;
    sol->interp[0] = 0;
    sol->intert[0] = 0;
    return;
  }

  if (ball_spd < 0.1) {
    sol->tangc = 0;
    sol->interc = 1;
    sol->interp[0] = 0;

    double p = (s - kick_area) / player_spd - cycle_delay;
    if (p < 0.0)
      p = 0.0;
    sol->intert[0] = p;
    return;
  }

  //先判断切点个数 -- 根据切点个数得到解的个数，并依次选择迭代的初值
  int n = CalcTangPoint(x0, y0, player_spd, kick_area, cycle_delay, sol);

  if (n < 1) { //没有切点
    sol->interc = 1;

    sol->interp[0] = CalcInterPoint(max_x - 1.0, x0, y0, ball_spd, player_spd,
                                    kick_area, cycle_delay);
    sol->intert[0] =
        log(1.0 - sol->interp[0] * (1.0 - alpha) / ball_spd) / ln_alpha;
  } else if (n == 1) {
    /**
     * n = 1的情况对应只有一个切点，即外切的时候同时内切
     **/
    sol->interc = 1;

    if (x0 < 0.0) {
      sol->interp[0] = CalcInterPoint(max_x - 1.0, x0, y0, ball_spd, player_spd,
                                      kick_area, cycle_delay);
    } else {
      sol->interp[0] = CalcInterPoint(x0, x0, y0, ball_spd, player_spd,
                                      kick_area, cycle_delay);
    }

    sol->intert[0] =
        log(1.0 - sol->interp[0] * (1.0 - alpha) / ball_spd) / ln_alpha;
  } else {
    if (ball_spd < sol->tangv[1]) { //没有最佳截球区间，早期就可截
      sol->interc = 1;

      sol->interp[0] = CalcInterPoint(x0, x0, y0, ball_spd, player_spd,
                                      kick_area, cycle_delay);
      sol->intert[0] =
          log(1.0 - sol->interp[0] * (1.0 - alpha) / ball_spd) / ln_alpha;
    } else if (ball_spd < sol->tangv[0]) { //有最佳截球区间
      sol->interc = 3;

      sol->interp[0] = CalcInterPoint(x0, x0, y0, ball_spd, player_spd,
                                      kick_area, cycle_delay);
      sol->intert[0] =
          log(1.0 - sol->interp[0] * (1.0 - alpha) / ball_spd) / ln_alpha;

      sol->interp[1] =
          CalcInterPoint((sol->tangp[0] + sol->tangp[1]) * 0.5, x0, y0,
                         ball_spd, player_spd, kick_area, cycle_delay);
      sol->intert[1] =
          log(1.0 - sol->interp[1] * (1.0 - alpha) / ball_spd) / ln_alpha;

      sol->interp[2] =
          CalcInterPoint((sol->tangp[1] + max_x) * 0.5, x0, y0, ball_spd,
                         player_spd, kick_area, cycle_delay);
      sol->intert[2] =
          log(1.0 - sol->interp[2] * (1.0 - alpha) / ball_spd) / ln_alpha;
    } else { //没有最佳截球区间，只有后期才可截
      sol->interc = 1;

      sol->interp[0] = CalcInterPoint(max_x - 1.0, x0, y0, ball_spd, player_spd,
                                      kick_area, cycle_delay);
      sol->intert[0] =
          log(1.0 - sol->interp[0] * (1.0 - alpha) / ball_spd) / ln_alpha;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %rbx
movq %rcx, %r12
movsd %xmm0, -0x38(%rbp)
movq %rdx, %r13
movq %rsi, %r14
callq 0x3db2c
movq %rax, %r15
callq 0x3db2c
movsd 0x8(%r12), %xmm0
movsd 0x10(%r12), %xmm1
subsd (%r14), %xmm0
subsd 0x8(%r14), %xmm1
movq %rax, -0x50(%rbp)
leaq -0x88(%rbp), %r14
movsd %xmm0, (%r14)
movsd %xmm1, 0x8(%r14)
movq %r13, %rdi
callq 0xcd30
xorpd 0x31954(%rip), %xmm0    # 0x57020
leaq -0x78(%rbp), %rsi
movlpd %xmm0, (%rsi)
movq %r14, %rdi
callq 0xd02c
movsd -0x38(%rbp), %xmm3
movapd %xmm0, %xmm7
cvtsi2sdl 0x18(%r12), %xmm4
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movapd %xmm7, %xmm2
mulsd %xmm7, %xmm2
addsd %xmm0, %xmm2
sqrtsd %xmm2, %xmm6
movsd 0x60(%r12), %xmm2
movapd %xmm4, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm3, %xmm0
ucomisd %xmm6, %xmm0
jbe 0x25738
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, (%rbx)
andq $0x0, 0x28(%rbx)
xorpd %xmm0, %xmm0
jmp 0x25887
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm5
mulsd %xmm5, %xmm5
mulsd %xmm0, %xmm0
addsd %xmm5, %xmm0
sqrtsd %xmm0, %xmm5
movsd 0x32e34(%rip), %xmm0    # 0x58590
ucomisd %xmm5, %xmm0
jbe 0x25790
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, (%rbx)
andq $0x0, 0x28(%rbx)
subsd %xmm3, %xmm6
divsd 0x60(%r12), %xmm6
subsd %xmm4, %xmm6
xorpd %xmm0, %xmm0
maxsd %xmm6, %xmm0
jmp 0x25887
movsd 0x318b0(%rip), %xmm0    # 0x57048
subsd 0x30e0(%r15), %xmm0
movsd %xmm5, -0x30(%rbp)
divsd %xmm0, %xmm5
movapd %xmm5, -0x70(%rbp)
movapd %xmm7, -0x60(%rbp)
movapd %xmm7, %xmm0
movsd %xmm1, -0x48(%rbp)
movsd %xmm4, -0x40(%rbp)
movq %rbx, %rsi
callq 0x25a4c
cmpl $0x1, %eax
je 0x257f9
testl %eax, %eax
jne 0x2589e
movl $0x1, 0x4(%rbx)
movapd -0x70(%rbp), %xmm0
addsd 0x31885(%rip), %xmm0    # 0x57070
movsd 0x60(%r12), %xmm4
movapd -0x60(%rbp), %xmm1
jmp 0x25832
movapd -0x70(%rbp), %xmm3
addsd 0x3186a(%rip), %xmm3    # 0x57070
movl $0x1, 0x4(%rbx)
xorpd %xmm2, %xmm2
movapd -0x60(%rbp), %xmm1
movapd %xmm1, %xmm0
cmpltsd %xmm2, %xmm0
andpd %xmm0, %xmm3
andnpd %xmm1, %xmm0
orpd %xmm3, %xmm0
movsd 0x60(%r12), %xmm4
movsd -0x48(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
movsd -0x38(%rbp), %xmm5
movsd -0x40(%rbp), %xmm6
callq 0x2610e
movsd %xmm0, 0x28(%rbx)
movsd 0x317f0(%rip), %xmm2    # 0x57048
movapd %xmm2, %xmm1
subsd 0x30e0(%r15), %xmm1
mulsd %xmm0, %xmm1
divsd -0x30(%rbp), %xmm1
subsd %xmm1, %xmm2
movapd %xmm2, %xmm0
callq 0x93f0
movq -0x50(%rbp), %rax
divsd 0x3468(%rax), %xmm0
pushq $0x40
popq %rax
movsd %xmm0, (%rbx,%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x20(%rbx), %xmm0
movsd -0x30(%rbp), %xmm3
ucomisd %xmm3, %xmm0
movsd -0x48(%rbp), %xmm2
movapd -0x60(%rbp), %xmm1
jbe 0x258cf
movl $0x1, 0x4(%rbx)
movsd 0x60(%r12), %xmm4
movapd %xmm1, %xmm0
jmp 0x2583c
movsd 0x18(%rbx), %xmm0
ucomisd %xmm3, %xmm0
jbe 0x25a2b
movl $0x3, 0x4(%rbx)
movsd 0x60(%r12), %xmm4
movapd %xmm1, %xmm0
movsd -0x38(%rbp), %xmm5
movsd -0x40(%rbp), %xmm6
callq 0x2610e
movsd %xmm0, 0x28(%rbx)
movsd 0x3173c(%rip), %xmm2    # 0x57048
movapd %xmm2, %xmm1
subsd 0x30e0(%r15), %xmm1
mulsd %xmm0, %xmm1
divsd -0x30(%rbp), %xmm1
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
callq 0x93f0
movq -0x50(%rbp), %r14
divsd 0x3468(%r14), %xmm0
movsd %xmm0, 0x40(%rbx)
movsd 0x8(%rbx), %xmm0
addsd 0x10(%rbx), %xmm0
mulsd 0x31ef5(%rip), %xmm0    # 0x57848
movsd 0x60(%r12), %xmm4
movaps -0x60(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
movsd -0x38(%rbp), %xmm5
movsd -0x40(%rbp), %xmm6
callq 0x2610e
movsd %xmm0, 0x30(%rbx)
movsd 0x316c4(%rip), %xmm2    # 0x57048
movapd %xmm2, %xmm1
subsd 0x30e0(%r15), %xmm1
mulsd %xmm0, %xmm1
divsd -0x30(%rbp), %xmm1
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
callq 0x93f0
divsd 0x3468(%r14), %xmm0
movsd %xmm0, 0x48(%rbx)
movapd -0x70(%rbp), %xmm0
addsd 0x10(%rbx), %xmm0
mulsd 0x31e81(%rip), %xmm0    # 0x57848
movsd 0x60(%r12), %xmm4
movaps -0x60(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
movsd -0x38(%rbp), %xmm5
movsd -0x40(%rbp), %xmm6
callq 0x2610e
movsd %xmm0, 0x38(%rbx)
movsd 0x31650(%rip), %xmm2    # 0x57048
movapd %xmm2, %xmm1
subsd 0x30e0(%r15), %xmm1
mulsd %xmm0, %xmm1
divsd -0x30(%rbp), %xmm1
subsd %xmm1, %xmm2
movapd %xmm2, %xmm0
callq 0x93f0
divsd 0x3468(%r14), %xmm0
pushq $0x50
jmp 0x25889
movl $0x1, 0x4(%rbx)
movapd -0x70(%rbp), %xmm0
addsd 0x31631(%rip), %xmm0    # 0x57070
movsd 0x60(%r12), %xmm4
jmp 0x2583c
nop