// File: /workspace/github_cpp_makefile_star2_O3/wrighteagle2d[P]wrighteaglebase/Debug/../src/InterceptModel.cpp
// Function: InterceptModel::CalcPeakPoint(Vector const&, double const&, double const&, double)
double InterceptModel::CalcPeakPoint(const Vector &relpos, const double &vp,
                                     const double &ka, const double fix) {
  static const double MINERROR = 0.01;

  const double alpha = ServerParam::instance().ballDecay();
  const double ln_alpha = ServerParam::instance().logBallDecay();
  const double x0 = relpos.X();
  const double y0 = relpos.Y();

  if (x0 < 0.0) {
    return 150.0;
  }

  if (fabs(y0) < ka) {
    return -1.0; //不可能穿越
  }

  double s, p, alpha_p, f, dfdx, last_f = 1000.0, x, last_x = x0;
  int iteration_cycle = 0;

  x = x0;
  do {
    iteration_cycle += 1;
    if (iteration_cycle > 10) {
      break;
    }
    s = Sqrt((x - x0) * (x - x0) + y0 * y0);
    p = (s - ka) / vp + fix;
    if (p < 0.0)
      p = 0.0;
    alpha_p = pow(alpha, (p));
    f = 1.0 - alpha_p * (1 - x * (x - x0) * ln_alpha / (s * vp));

    if (fabs(f) > fabs(last_f)) {
      return last_x; //没有切点
    } else {
      last_f = f;
    }
    last_x = x;
    dfdx =
        ln_alpha / vp *
        ((x - x0) * (f - 1.0) / s +
         alpha_p * ((x + x - x0) / s - x * (x - x0) * (x - x0) / (s * s * s)));
    dfdx = fabs(dfdx) < FLOAT_EPS ? (Sign(dfdx) * FLOAT_EPS) : dfdx;
    x = x - f / dfdx;
  } while (fabs(f) > MINERROR);

  return x;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movsd %xmm0, -0x40(%rbp)
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
callq 0x49360
movsd 0x30e0(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x49360
movsd (%r15), %xmm8
xorpd %xmm0, %xmm0
ucomisd %xmm8, %xmm0
jbe 0x2c640
movsd 0x3e192(%rip), %xmm1    # 0x6a7d0
jmp 0x2c665
movsd 0x8(%r15), %xmm9
movapd 0x3c9c2(%rip), %xmm0    # 0x69010
andpd %xmm9, %xmm0
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2c676
movsd 0x3ca33(%rip), %xmm1    # 0x69098
movapd %xmm1, %xmm0
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movsd 0x3468(%rax), %xmm10
mulsd %xmm9, %xmm9
movsd (%r14), %xmm2
movsd 0x3e137(%rip), %xmm3    # 0x6a7c8
movl $0x9, %r15d
leaq 0x3df12(%rip), %r12     # 0x6a5b0
movapd %xmm8, %xmm11
movapd %xmm8, %xmm0
movsd %xmm8, -0x30(%rbp)
movapd %xmm9, -0x70(%rbp)
movsd %xmm10, -0x28(%rbp)
movsd %xmm11, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movapd %xmm3, -0x80(%rbp)
subsd %xmm8, %xmm0
movsd %xmm0, -0x50(%rbp)
mulsd %xmm0, %xmm0
addsd %xmm9, %xmm0
sqrtsd %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
subsd (%rbx), %xmm0
divsd %xmm2, %xmm0
addsd -0x40(%rbp), %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
callq 0x98a0
movsd -0x48(%rbp), %xmm12
movsd -0x50(%rbp), %xmm7
movsd -0x60(%rbp), %xmm11
movsd -0x28(%rbp), %xmm10
movapd -0x70(%rbp), %xmm9
movsd -0x30(%rbp), %xmm8
movapd %xmm11, %xmm5
mulsd %xmm7, %xmm5
movapd %xmm10, %xmm1
mulsd %xmm5, %xmm1
movsd (%r14), %xmm2
movapd %xmm12, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm3, %xmm1
movsd 0x3c904(%rip), %xmm3    # 0x69058
movapd %xmm3, %xmm4
subsd %xmm1, %xmm4
mulsd %xmm0, %xmm4
subsd %xmm4, %xmm3
movapd %xmm3, %xmm4
movapd 0x3c8a0(%rip), %xmm1    # 0x69010
andpd %xmm1, %xmm4
movapd -0x80(%rbp), %xmm6
andpd %xmm1, %xmm6
ucomisd %xmm6, %xmm4
movsd -0x58(%rbp), %xmm1
ja 0x2c665
movapd %xmm3, %xmm1
addsd 0x3c900(%rip), %xmm1    # 0x69098
mulsd %xmm7, %xmm1
divsd %xmm12, %xmm1
movapd %xmm11, %xmm6
addsd %xmm11, %xmm6
subsd %xmm8, %xmm6
divsd %xmm12, %xmm6
mulsd %xmm7, %xmm5
movapd %xmm12, %xmm7
mulsd %xmm12, %xmm7
mulsd %xmm12, %xmm7
divsd %xmm7, %xmm5
subsd %xmm5, %xmm6
mulsd %xmm6, %xmm0
movapd %xmm10, %xmm5
divsd %xmm2, %xmm5
addsd %xmm1, %xmm0
mulsd %xmm5, %xmm0
xorl %eax, %eax
ucomisd 0x3c8a1(%rip), %xmm0    # 0x69090
setae %al
movapd %xmm0, %xmm1
andpd 0x3c812(%rip), %xmm1    # 0x69010
movsd (%r12,%rax,8), %xmm5
cmpltsd 0x3c833(%rip), %xmm1    # 0x69040
andpd %xmm1, %xmm5
andnpd %xmm0, %xmm1
orpd %xmm5, %xmm1
movapd %xmm3, %xmm0
divsd %xmm1, %xmm0
movapd %xmm11, %xmm1
subsd %xmm0, %xmm1
subl $0x1, %r15d
setb %al
ucomisd 0x3cf07(%rip), %xmm4    # 0x69740
jbe 0x2c665
movapd %xmm1, %xmm0
testb %al, %al
je 0x2c6ba
jmp 0x2c665