// File: /workspace/github_cpp_makefile_star2_O2/wrighteagle2d[P]wrighteaglebase/Debug/../src/Formation.cpp
// Function: FormationBase::GetExpectedGoaliePos(Vector const&, double, Vector const&, int)
Vector FormationBase::GetExpectedGoaliePos(const Vector &ball_pos, double run,
                                           const Vector &goalie_pos,
                                           int cycle_delay) {
  Vector p[2];
  p[0] = Vector(ServerParam::instance().PITCH_LENGTH / 2.0,
                -ServerParam::instance().goalWidth() / 2.0 + 0.1); // top
  p[1] = Vector(ServerParam::instance().PITCH_LENGTH / 2.0,
                ServerParam::instance().goalWidth() / 2.0 - 0.1); // down

  double tmp, distg;
  distg = ball_pos.Dist(goalie_pos);
  AngleDeg mid;
  mid = GetNormalizeAngleDeg(
      ((p[1] - ball_pos).Dir() + (p[0] - ball_pos).Dir()) / 2); //取角平分线
  tmp = ball_pos.Dist(Vector(ServerParam::instance().PITCH_LENGTH / 2.0, 0.0)) -
        2.0;
  Vector tgPos, jgPos;
  tgPos = ball_pos +
          Polar2Vector(distg, mid); // 估计守门员可能的位置,守门员向角平分线运动
  if (tgPos.Dist(ball_pos) > tmp) {
    tgPos = ball_pos + (tgPos - ball_pos).SetLength(tmp); // 调整到球场内
  }
  if (goalie_pos.Dist(tgPos) < run + cycle_delay * 0.8) {
    jgPos = tgPos; // 守门员向tgpos尽可能运动
  } else {
    jgPos = goalie_pos +
            Polar2Vector(run + cycle_delay * 0.8, (tgPos - goalie_pos).Dir());
  }
  return jgPos;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, -0x3c(%rbp)
movq %rdx, %rbx
movsd %xmm0, -0x50(%rbp)
movq %rsi, %r15
callq 0x3db2c
leaq 0x3a9e9(%rip), %rax     # 0x59ec8
movsd (%rax), %xmm0
mulsd 0x3835d(%rip), %xmm0    # 0x57848
movsd %xmm0, -0x30(%rbp)
callq 0x3db2c
movsd 0x3008(%rax), %xmm0
mulsd 0x38343(%rip), %xmm0    # 0x57848
movsd 0x39083(%rip), %xmm1    # 0x58590
subsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
callq 0x3db2c
callq 0x3db2c
movsd 0x3008(%rax), %xmm0
mulsd 0x38318(%rip), %xmm0    # 0x57848
addsd 0x39060(%rip), %xmm0    # 0x58598
movsd %xmm0, -0x48(%rbp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2198a
leaq -0x90(%rbp), %r12
movsd %xmm0, (%r12)
movsd -0x30(%rbp), %xmm0
subsd (%r15), %xmm0
movsd -0x48(%rbp), %xmm1
subsd 0x8(%r15), %xmm1
leaq -0x68(%rbp), %rdi
movsd %xmm0, (%rdi)
movsd %xmm1, 0x8(%rdi)
callq 0xcd30
movsd -0x30(%rbp), %xmm1
subsd (%r15), %xmm1
movsd -0x38(%rbp), %xmm2
subsd 0x8(%r15), %xmm2
movsd %xmm0, -0x38(%rbp)
leaq -0x80(%rbp), %r13
movsd %xmm1, (%r13)
movsd %xmm2, 0x8(%r13)
movq %r13, %rdi
callq 0xcd30
addsd -0x38(%rbp), %xmm0
mulsd 0x3828d(%rip), %xmm0    # 0x57848
movabsq $-0x3f99800000000000, %rax # imm = 0xC066800000000000
leaq -0x58(%rbp), %rdi
movq %rax, (%rdi)
callq 0xd305
leaq -0x88(%rbp), %r14
movsd %xmm0, (%r14)
callq 0x3db2c
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
subsd -0x30(%rbp), %xmm0
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
addsd 0x37a4e(%rip), %xmm0    # 0x57058
movsd %xmm0, -0x30(%rbp)
movsd %xmm0, (%r13)
movq %r12, %rdi
movq %r14, %rsi
callq 0xd15b
movsd (%r15), %xmm3
movsd 0x8(%r15), %xmm4
addsd %xmm3, %xmm0
addsd %xmm4, %xmm1
movapd %xmm0, %xmm2
subsd %xmm3, %xmm2
movapd %xmm1, %xmm3
subsd %xmm4, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
movapd %xmm2, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm4, %xmm5
sqrtsd %xmm5, %xmm4
ucomisd -0x30(%rbp), %xmm4
jbe 0x1f683
leaq -0x68(%rbp), %rdi
movsd %xmm2, (%rdi)
movsd %xmm3, 0x8(%rdi)
leaq -0x80(%rbp), %rsi
callq 0x219b2
addsd (%r15), %xmm0
addsd 0x8(%r15), %xmm1
movsd (%rbx), %xmm3
movsd 0x8(%rbx), %xmm2
movapd %xmm3, %xmm5
subsd %xmm0, %xmm5
movapd %xmm2, %xmm4
subsd %xmm1, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm5, %xmm5
addsd %xmm4, %xmm5
cvtsi2sdl -0x3c(%rbp), %xmm4
mulsd 0x38eeb(%rip), %xmm4    # 0x585a0
sqrtsd %xmm5, %xmm5
addsd -0x50(%rbp), %xmm4
ucomisd %xmm5, %xmm4
ja 0x1f700
leaq -0x58(%rbp), %r14
movsd %xmm4, (%r14)
subsd %xmm3, %xmm0
subsd %xmm2, %xmm1
leaq -0x68(%rbp), %rdi
movsd %xmm0, (%rdi)
movsd %xmm1, 0x8(%rdi)
callq 0xcd30
leaq -0x70(%rbp), %rsi
movsd %xmm0, (%rsi)
movq %r14, %rdi
callq 0xd15b
addsd (%rbx), %xmm0
addsd 0x8(%rbx), %xmm1
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop