// File: /workspace/github_cpp_makefile_star2_O2/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 0x3db2c
movsd 0x30e0(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x3db2c
movsd (%r15), %xmm8
xorpd %xmm0, %xmm0
ucomisd %xmm8, %xmm0
jbe 0x2634f
movsd 0x32456(%rip), %xmm1    # 0x587a0
jmp 0x2654b
movsd 0x8(%r15), %xmm2
movapd 0x30cb3(%rip), %xmm0    # 0x57010
andpd %xmm2, %xmm0
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x26378
movsd 0x30cfd(%rip), %xmm1    # 0x57070
jmp 0x2654b
movsd 0x3468(%rax), %xmm9
mulsd %xmm2, %xmm2
movapd %xmm2, -0x70(%rbp)
movsd 0x32406(%rip), %xmm0    # 0x58798
pushq $0xb
popq %r15
leaq 0x321d3(%rip), %r12     # 0x58570
movapd %xmm8, %xmm1
movapd %xmm8, %xmm10
movsd %xmm8, -0x30(%rbp)
movsd %xmm9, -0x28(%rbp)
movapd %xmm1, %xmm2
decl %r15d
je 0x26547
movapd %xmm2, %xmm1
subsd %xmm8, %xmm1
movsd %xmm1, -0x50(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
addsd -0x70(%rbp), %xmm0
sqrtsd %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
subsd (%rbx), %xmm0
divsd (%r14), %xmm0
addsd -0x40(%rbp), %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
movsd %xmm2, -0x60(%rbp)
movsd %xmm10, -0x58(%rbp)
callq 0x98a0
movsd -0x48(%rbp), %xmm11
movsd -0x50(%rbp), %xmm7
movsd -0x60(%rbp), %xmm10
movsd -0x28(%rbp), %xmm9
movsd -0x30(%rbp), %xmm8
movapd %xmm10, %xmm4
mulsd %xmm7, %xmm4
movapd %xmm9, %xmm1
mulsd %xmm4, %xmm1
movsd (%r14), %xmm5
movapd %xmm11, %xmm2
mulsd %xmm5, %xmm2
divsd %xmm2, %xmm1
movsd 0x30beb(%rip), %xmm2    # 0x57048
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
subsd %xmm3, %xmm2
movapd %xmm2, %xmm3
movapd 0x30b97(%rip), %xmm1    # 0x57010
andpd %xmm1, %xmm3
movapd -0x80(%rbp), %xmm6
andpd %xmm1, %xmm6
ucomisd %xmm6, %xmm3
movsd -0x58(%rbp), %xmm1
ja 0x2654b
movapd %xmm9, %xmm1
divsd %xmm5, %xmm1
movapd %xmm2, %xmm5
addsd 0x30bc6(%rip), %xmm5    # 0x57070
mulsd %xmm7, %xmm5
divsd %xmm11, %xmm5
movapd %xmm10, %xmm6
addsd %xmm10, %xmm6
subsd %xmm8, %xmm6
divsd %xmm11, %xmm6
mulsd %xmm7, %xmm4
movapd %xmm11, %xmm7
mulsd %xmm11, %xmm7
mulsd %xmm11, %xmm7
divsd %xmm7, %xmm4
subsd %xmm4, %xmm6
mulsd %xmm6, %xmm0
addsd %xmm5, %xmm0
mulsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd 0x31de8(%rip), %xmm0    # 0x582e0
setae %al
movapd %xmm0, %xmm1
andpd 0x30b09(%rip), %xmm1    # 0x57010
movsd (%r12,%rax,8), %xmm4
cmpltsd 0x30b2a(%rip), %xmm1    # 0x57040
andpd %xmm1, %xmm4
andnpd %xmm0, %xmm1
orpd %xmm4, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
movapd %xmm10, %xmm1
subsd %xmm0, %xmm1
ucomisd 0x311dd(%rip), %xmm3    # 0x57718
movapd %xmm2, %xmm0
ja 0x263b3
jmp 0x2654b
movapd %xmm2, %xmm1
movapd %xmm1, %xmm0
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq