// File: /workspace/github_cpp_makefile_star2_O1/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 0x4a75c
movsd 0x30e0(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0x4a75c
movsd (%r15), %xmm8
xorpd %xmm0, %xmm0
ucomisd %xmm8, %xmm0
jbe 0x2cff2
movsd 0x3f7f0(%rip), %xmm1    # 0x6c7e0
jmp 0x2d017
movsd 0x8(%r15), %xmm9
movapd 0x3e010(%rip), %xmm0    # 0x6b010
andpd %xmm9, %xmm0
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2d028
movsd 0x3e081(%rip), %xmm1    # 0x6b098
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 0x3f79a(%rip), %xmm2    # 0x6c7d8
movl $0x9, %r15d
xorpd %xmm11, %xmm11
leaq 0x3f570(%rip), %r12     # 0x6c5c0
movapd %xmm8, %xmm12
movapd %xmm8, %xmm0
movsd %xmm8, -0x30(%rbp)
movapd %xmm9, -0x70(%rbp)
movsd %xmm10, -0x28(%rbp)
movsd %xmm12, -0x50(%rbp)
movapd %xmm2, -0x80(%rbp)
movsd %xmm0, -0x60(%rbp)
subsd %xmm8, %xmm0
movsd %xmm0, -0x58(%rbp)
mulsd %xmm0, %xmm0
addsd %xmm9, %xmm0
ucomisd %xmm11, %xmm0
jb 0x2d09c
sqrtsd %xmm0, %xmm0
jmp 0x2d0a1
callq 0x93c0
movsd %xmm0, -0x48(%rbp)
subsd (%rbx), %xmm0
divsd (%r14), %xmm0
addsd -0x40(%rbp), %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
callq 0x98a0
movsd -0x48(%rbp), %xmm13
movsd -0x60(%rbp), %xmm12
movapd %xmm12, %xmm4
movsd -0x58(%rbp), %xmm7
mulsd %xmm7, %xmm4
movsd -0x28(%rbp), %xmm10
movapd %xmm10, %xmm1
mulsd %xmm4, %xmm1
movsd (%r14), %xmm5
movapd %xmm13, %xmm2
mulsd %xmm5, %xmm2
divsd %xmm2, %xmm1
movsd 0x3df4f(%rip), %xmm2    # 0x6b058
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
subsd %xmm3, %xmm2
movapd %xmm2, %xmm3
movapd 0x3deeb(%rip), %xmm1    # 0x6b010
andpd %xmm1, %xmm3
movapd -0x80(%rbp), %xmm6
andpd %xmm1, %xmm6
ucomisd %xmm6, %xmm3
movsd -0x50(%rbp), %xmm1
movsd -0x30(%rbp), %xmm8
movapd -0x70(%rbp), %xmm9
xorpd %xmm11, %xmm11
ja 0x2d017
movapd %xmm10, %xmm1
divsd %xmm5, %xmm1
movapd %xmm2, %xmm5
addsd 0x3df31(%rip), %xmm5    # 0x6b098
mulsd %xmm7, %xmm5
divsd %xmm13, %xmm5
movapd %xmm12, %xmm6
addsd %xmm12, %xmm6
subsd %xmm8, %xmm6
divsd %xmm13, %xmm6
mulsd %xmm7, %xmm4
movapd %xmm13, %xmm7
mulsd %xmm13, %xmm7
mulsd %xmm13, %xmm7
divsd %xmm7, %xmm4
subsd %xmm4, %xmm6
mulsd %xmm6, %xmm0
addsd %xmm5, %xmm0
mulsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm11, %xmm0
setae %al
movapd %xmm0, %xmm1
andpd 0x3de4f(%rip), %xmm1    # 0x6b010
movsd (%r12,%rax,8), %xmm4
cmpltsd 0x3de70(%rip), %xmm1    # 0x6b040
andpd %xmm1, %xmm4
andnpd %xmm0, %xmm1
orpd %xmm4, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
movapd %xmm12, %xmm1
subsd %xmm0, %xmm1
subl $0x1, %r15d
setb %al
ucomisd 0x3e544(%rip), %xmm3    # 0x6b740
jbe 0x2d017
movapd %xmm1, %xmm0
testb %al, %al
je 0x2d06c
jmp 0x2d017
nop