// File: /workspace/github_cpp_makefile_star2_O1/wrighteagle2d[P]wrighteaglebase/Debug/../src/Kicker.cpp
// Function: Kicker::GetMaxSpeed(Agent const&, Vector const&, int const&, Vector*, Vector*)
double Kicker::GetMaxSpeed(const Agent &agent, const Vector &kick_pos,
                           const int &cycle, Vector *kick_out_pos,
                           Vector *last_vel) {
  if (cycle < 1) {
    Assert(0);
    return 0.0;
  }

  UpdateKickData(agent);

  // absolute position of agent before the ball was kicked out
  Vector player_pos_final =
      mInput.mPlayerPos + mInput.mPlayerVel.Rotate(mInput.mPlayerBodyDir) *
                              ((1 - pow(PlayerParam::instance()
                                            .HeteroPlayer(mInput.mPlayerType)
                                            .playerDecay(),
                                        cycle - 1)) /
                               (1 - PlayerParam::instance()
                                        .HeteroPlayer(mInput.mPlayerType)
                                        .playerDecay()));

  if (cycle > 3) /** 4脚肯定能踢到最快球速了 */
  {
    // 不出意外，4脚踢球最后出球位置应该是mPoint[0]。 From Shi Ke.
    if (kick_out_pos)
      *kick_out_pos =
          mPoint[0].Rotate(mInput.mPlayerBodyDir) + player_pos_final;
    if (last_vel)
      *last_vel = Vector(0.0, 0.0); //认为球停在身前，速度为0
    return ServerParam::instance().ballSpeedMax();
  }

  Vector target = (kick_pos - player_pos_final).Rotate(-mInput.mPlayerBodyDir);

  int idx = (int)GetNormalizeAngleDeg((target - mInput.mBallPos).Dir(),
                                      -FLOAT_EPS); /** 得到0到359的一个整数 */

  if (mMaxSpeedFlag[idx][cycle - 1] != agent.GetAgentID()) {
    double max_speed = 0.0;
    Vector max_speed_pos = mInput.mBallPos;
    Vector max_speed_first_pos = mInput.mBallPos;
    Vector max_speed_last_vel = mInput.mBallVel;

    if (cycle == 1) {
      max_speed = GetOneKickMaxSpeed(
          mInput.mBallVel, (target - mInput.mBallPos).Dir(),
          agent.GetSelf().GetKickRate() * ServerParam::instance().maxPower());
    } else {
      Vector ball_vel = Vector(0.0, 0.0);

      for (int k = 0; k < POINTS_NUM; ++k) {
        if (mPointEva[k] > 0.001) {
          ball_vel = (mPoint[k] + mInput.mPlayerVel - mInput.mBallPos) *
                     ServerParam::instance().ballDecay(); //踢到k后的球速
          if (cycle == 2) /** 2周期踢球，重新算一下 */
          {
            double speed = GetOneKickMaxSpeed(
                ball_vel, (target - mPoint[k]).Dir(), mMaxAccel[k]);
            if (max_speed < speed) {
              max_speed = speed;
              max_speed_pos = mPoint[k];
              max_speed_first_pos = mPoint[k];
              max_speed_last_vel = ball_vel;
            }
          } else // 前向搜索2步
          {
            Vector ball_next = mPoint[k] + ball_vel;
            for (int l = 0; l < POINTS_NUM; ++l) // 从j踢到k，再踢到l
            {
              if (mPoint[l].Dist(ball_next) <
                  mMaxAccel[k]) // 保证理论上可以从k踢到l
              {
                Vector ball_vel_next =
                    (mPoint[l] +
                     mInput.mPlayerVel * PlayerParam::instance()
                                             .HeteroPlayer(mInput.mPlayerType)
                                             .playerDecay() -
                     mPoint[k]) *
                    ServerParam::instance().ballDecay();
                double speed = GetOneKickMaxSpeed(
                    ball_vel_next, (target - mPoint[l]).Dir(), mMaxAccel[l]);
                if (max_speed < speed) {
                  max_speed = speed;
                  max_speed_pos = mPoint[l];
                  max_speed_first_pos = mPoint[k];
                  max_speed_last_vel = ball_vel_next;
                }
              }
            }
          }
        }
      }
    }

    mMaxSpeedFlag[idx][cycle - 1] = agent.GetAgentID();
    mMaxSpeed[idx][cycle - 1] = max_speed;
    mMaxSpeedOutPos[idx][cycle - 1] =
        max_speed_pos.Rotate(mInput.mPlayerBodyDir) + player_pos_final;
    mMaxSpeedOutFirstPos[idx][cycle - 1] = max_speed_first_pos;
    mMaxSpeedOutLastVel[idx][cycle - 1] =
        max_speed_last_vel.Rotate(mInput.mPlayerBodyDir);
  }

  if (kick_out_pos)
    *kick_out_pos = mMaxSpeedOutPos[idx][cycle - 1];
  if (last_vel)
    *last_vel = mMaxSpeedOutLastVel[idx][cycle - 1];

  return Min(mMaxSpeed[idx][cycle - 1], ServerParam::instance().ballSpeedMax());
}

// Assembly:
cmpl $0x0, (%rcx)
jle 0x2ecb6
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %r9, %rbx
movq %r8, %r12
movq %rcx, %r13
movq %rdx, %r14
movq %rdi, %r15
movq %rsi, -0x68(%rbp)
callq 0x2de9c
leaq 0x2b4598(%r15), %rdi
leaq 0x2b45a8(%r15), %rsi
movq %rsi, -0x90(%rbp)
callq 0x33428
movsd %xmm0, -0x58(%rbp)
movsd %xmm1, -0x30(%rbp)
callq 0x450ba
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r15), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3020(%rax,%rcx), %xmm0
movl (%r13), %eax
decl %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
callq 0x98a0
movsd 0x3c466(%rip), %xmm1    # 0x6b058
subsd %xmm0, %xmm1
movsd %xmm1, -0x50(%rbp)
callq 0x450ba
movsd -0x50(%rbp), %xmm1
movsd -0x58(%rbp), %xmm0
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r15), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3c431(%rip), %xmm2    # 0x6b058
subsd 0x3020(%rax,%rcx), %xmm2
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
mulsd -0x30(%rbp), %xmm1
addsd 0x2b4588(%r15), %xmm0
addsd 0x2b4590(%r15), %xmm1
cmpl $0x4, (%r13)
jl 0x2ecba
testq %r12, %r12
je 0x2ec97
addq $0x48, %r15
movq %r15, %rdi
movq -0x90(%rbp), %rsi
movsd %xmm0, -0x58(%rbp)
movsd %xmm1, -0x50(%rbp)
callq 0x33428
movsd -0x58(%rbp), %xmm2
addsd %xmm0, %xmm2
movsd -0x50(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm2, (%r12)
movsd %xmm0, 0x8(%r12)
testq %rbx, %rbx
je 0x2eca4
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
callq 0x4a75c
movsd 0x30f8(%rax), %xmm0
jmp 0x2f48a
xorps %xmm0, %xmm0
retq
movq %r13, -0x70(%rbp)
movsd (%r14), %xmm3
movsd 0x8(%r14), %xmm2
movsd %xmm0, -0x58(%rbp)
subsd %xmm0, %xmm3
movsd %xmm3, -0x30(%rbp)
movsd %xmm1, -0x50(%rbp)
subsd %xmm1, %xmm2
movapd %xmm2, -0x80(%rbp)
movsd 0x2b45a8(%r15), %xmm0
mulsd 0x3c3aa(%rip), %xmm0    # 0x6b0a0
divsd 0x3c34a(%rip), %xmm0    # 0x6b048
leaq -0x40(%rbp), %r14
leaq -0xb0(%rbp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x92d0
movsd (%r14), %xmm0
movsd (%r13), %xmm1
movsd -0x30(%rbp), %xmm3
movapd %xmm3, %xmm2
mulsd %xmm0, %xmm3
movapd %xmm3, %xmm4
movapd -0x80(%rbp), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm1, %xmm2
subsd %xmm0, %xmm2
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
movapd %xmm2, -0xc0(%rbp)
movapd %xmm2, %xmm1
subsd 0x2b45b0(%r15), %xmm1
movapd %xmm3, -0x80(%rbp)
movapd %xmm3, %xmm2
subsd 0x2b45b8(%r15), %xmm2
movapd 0x3c298(%rip), %xmm4    # 0x6b010
movapd %xmm1, %xmm0
andpd %xmm4, %xmm0
movsd 0x3c2b8(%rip), %xmm3    # 0x6b040
ucomisd %xmm0, %xmm3
jbe 0x2eda8
movapd %xmm2, %xmm3
andpd %xmm4, %xmm3
xorpd %xmm0, %xmm0
movsd 0x3c29e(%rip), %xmm5    # 0x6b040
ucomisd %xmm3, %xmm5
ja 0x2edc9
movapd %xmm2, %xmm0
callq 0x90d0
movapd 0x3c257(%rip), %xmm4    # 0x6b010
mulsd 0x3c287(%rip), %xmm0    # 0x6b048
divsd 0x3c287(%rip), %xmm0    # 0x6b050
movsd 0x3c2a7(%rip), %xmm1    # 0x6b078
ucomisd %xmm0, %xmm1
movq -0x70(%rbp), %r14
movq -0x68(%rbp), %r8
jbe 0x2edf3
movsd 0x3c289(%rip), %xmm2    # 0x6b070
addsd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x2ede7
jmp 0x2ee17
ucomisd 0x3db0d(%rip), %xmm0    # 0x6c908
jb 0x2ee17
movsd 0x3c263(%rip), %xmm1    # 0x6b068
movsd 0x3dafb(%rip), %xmm2    # 0x6c908
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jae 0x2ee0d
cvttsd2si %xmm0, %eax
cltq
leaq (%rax,%rax,2), %rcx
movq %rcx, -0x88(%rbp)
shlq $0x4, %rcx
leaq (%r15,%rcx), %r13
movslq (%r14), %rax
movq %rax, %rdx
shlq $0x4, %rdx
movl 0x8(%r8), %esi
movq 0x18(%r8), %rdi
movq 0x10(%rdi), %rdi
movb 0xc(%r8), %r8b
movl %esi, -0x40(%rbp)
movq %rdi, -0x3c(%rbp)
movb %r8b, -0x34(%rbp)
movq -0x38(%rbp), %rsi
cmpl %esi, 0x2b5008(%rdx,%r13)
jne 0x2ee94
movq -0x40(%rbp), %rdi
movq %rdi, %r8
shrq $0x20, %r8
cmpl %r8d, 0x2b5004(%rdx,%r13)
jne 0x2ee94
addq %r13, %rdx
addq $0x2b5000, %rdx         # imm = 0x2B5000
cmpl %edi, (%rdx)
jne 0x2ee94
shrq $0x20, %rsi
cmpb %sil, 0xc(%rdx)
je 0x2f42f
leaq 0x2b45b0(%r15), %rdx
addq %r15, %rcx
addq $0x2b5010, %rcx         # imm = 0x2B5010
movq %rcx, -0xf8(%rbp)
movups (%rdx), %xmm0
movaps %xmm0, -0xb0(%rbp)
movups (%rdx), %xmm0
movaps %xmm0, -0xe0(%rbp)
movupd 0x2b45c0(%r15), %xmm0
movapd %xmm0, -0xd0(%rbp)
cmpl $0x1, %eax
movq %r13, -0x100(%rbp)
jne 0x2ef9c
leaq 0x2b45c0(%r15), %rax
movq %rax, -0x30(%rbp)
movapd -0xc0(%rbp), %xmm0
subsd 0x2b45b0(%r15), %xmm0
movapd -0x80(%rbp), %xmm2
subsd 0x2b45b8(%r15), %xmm2
movapd %xmm0, %xmm1
andpd %xmm4, %xmm0
movsd 0x3c125(%rip), %xmm3    # 0x6b040
ucomisd %xmm0, %xmm3
jbe 0x2ef2f
andpd %xmm2, %xmm4
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm3
ja 0x2ef48
movapd %xmm2, %xmm0
callq 0x90d0
mulsd 0x3c108(%rip), %xmm0    # 0x6b048
divsd 0x3c108(%rip), %xmm0    # 0x6b050
leaq -0x40(%rbp), %r13
movsd %xmm0, (%r13)
movq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0xe85a
movsd 0x130(%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
callq 0x4a75c
movsd -0x48(%rbp), %xmm0
mulsd 0x3130(%rax), %xmm0
leaq -0xa0(%rbp), %rcx
movsd %xmm0, (%rcx)
movq %r15, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x3349a
jmp 0x2f34d
movq %r12, -0xe8(%rbp)
movq %rbx, -0xf0(%rbp)
leaq 0x48(%r15), %rax
movq %rax, -0x110(%rbp)
leaq 0x2b4870(%r15), %rax
movq %rax, -0x108(%rbp)
xorpd %xmm0, %xmm0
leaq -0x40(%rbp), %rax
movapd %xmm0, (%rax)
xorl %r12d, %r12d
leaq -0xa0(%rbp), %r13
xorpd %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd 0x2b4d80(%r15,%r12,8), %xmm0
ucomisd 0x3d144(%rip), %xmm0    # 0x6c138
jbe 0x2f329
movq %r12, %rax
shlq $0x4, %rax
movq -0x110(%rbp), %rcx
leaq (%rcx,%rax), %rbx
movsd (%rcx,%rax), %xmm0
movsd 0x8(%rcx,%rax), %xmm1
addsd 0x2b4598(%r15), %xmm0
addsd 0x2b45a0(%r15), %xmm1
subsd 0x2b45b0(%r15), %xmm0
movsd %xmm0, -0x30(%rbp)
subsd 0x2b45b8(%r15), %xmm1
movsd %xmm1, -0x48(%rbp)
callq 0x4a75c
movsd -0x48(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
movsd 0x30e0(%rax), %xmm0
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm2
movsd %xmm1, -0x40(%rbp)
movsd %xmm2, -0x38(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x2f13a
movapd -0xc0(%rbp), %xmm1
subsd (%rbx), %xmm1
movapd -0x80(%rbp), %xmm0
subsd 0x8(%rbx), %xmm0
movapd %xmm1, %xmm2
movapd 0x3bf73(%rip), %xmm4    # 0x6b010
andpd %xmm4, %xmm2
movsd 0x3bf97(%rip), %xmm3    # 0x6b040
ucomisd %xmm2, %xmm3
jbe 0x2f0c9
movapd %xmm0, %xmm3
andpd %xmm4, %xmm3
xorpd %xmm2, %xmm2
movsd 0x3bf7d(%rip), %xmm4    # 0x6b040
ucomisd %xmm3, %xmm4
ja 0x2f0e2
callq 0x90d0
movapd %xmm0, %xmm2
mulsd 0x3bf6e(%rip), %xmm2    # 0x6b048
divsd 0x3bf6e(%rip), %xmm2    # 0x6b050
movsd %xmm2, -0xa0(%rbp)
movq -0x108(%rbp), %rax
leaq (%rax,%r12,8), %rcx
movq %r15, %rdi
leaq -0x40(%rbp), %rsi
movq %r13, %rdx
callq 0x3349a
ucomisd -0x60(%rbp), %xmm0
jbe 0x2f329
movups (%rbx), %xmm1
movaps %xmm1, -0xb0(%rbp)
movups (%rbx), %xmm1
movaps %xmm1, -0xe0(%rbp)
movapd -0x40(%rbp), %xmm1
movapd %xmm1, -0xd0(%rbp)
movsd %xmm0, -0x60(%rbp)
jmp 0x2f329
addsd (%rbx), %xmm1
addsd 0x8(%rbx), %xmm2
movl $0x2b4870, %r14d        # imm = 0x2B4870
movsd %xmm2, -0x48(%rbp)
movsd %xmm1, -0x30(%rbp)
movsd -0x569098(%r15,%r14,2), %xmm0
subsd %xmm1, %xmm0
movsd -0x569090(%r15,%r14,2), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd 0x3bf0d(%rip), %xmm0    # 0x6b090
jb 0x2f18b
sqrtsd %xmm0, %xmm0
jmp 0x2f195
callq 0x93c0
movsd -0x48(%rbp), %xmm2
movsd 0x2b4870(%r15,%r12,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2f313
callq 0x450ba
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r15), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3020(%rax,%rcx), %xmm0
movsd 0x2b4598(%r15), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x2b45a0(%r15), %xmm0
addsd -0x569098(%r15,%r14,2), %xmm1
addsd -0x569090(%r15,%r14,2), %xmm0
subsd (%rbx), %xmm1
movsd %xmm1, -0x120(%rbp)
subsd 0x8(%rbx), %xmm0
movsd %xmm0, -0x118(%rbp)
callq 0x4a75c
movsd 0x30e0(%rax), %xmm0
movsd -0x120(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd -0x118(%rbp), %xmm2
mulsd %xmm0, %xmm2
movsd %xmm1, -0xa0(%rbp)
movsd %xmm2, -0x98(%rbp)
movapd -0xc0(%rbp), %xmm1
subsd -0x569098(%r15,%r14,2), %xmm1
movapd -0x80(%rbp), %xmm0
subsd -0x569090(%r15,%r14,2), %xmm0
movapd %xmm1, %xmm2
movapd 0x3bd9f(%rip), %xmm4    # 0x6b010
andpd %xmm4, %xmm2
movsd 0x3bdc3(%rip), %xmm3    # 0x6b040
ucomisd %xmm2, %xmm3
jbe 0x2f29d
movapd %xmm0, %xmm3
andpd %xmm4, %xmm3
xorpd %xmm2, %xmm2
movsd 0x3bda9(%rip), %xmm4    # 0x6b040
ucomisd %xmm3, %xmm4
ja 0x2f2b6
callq 0x90d0
movapd %xmm0, %xmm2
mulsd 0x3bd9a(%rip), %xmm2    # 0x6b048
divsd 0x3bd9a(%rip), %xmm2    # 0x6b050
movsd %xmm2, -0x128(%rbp)
leaq (%r15,%r14), %rcx
movq %r15, %rdi
movq %r13, %rsi
leaq -0x128(%rbp), %rdx
callq 0x3349a
ucomisd -0x60(%rbp), %xmm0
jbe 0x2f30e
leaq (%r15,%r14,2), %rax
addq $-0x569098, %rax        # imm = 0xFFA96F68
movups (%rax), %xmm1
movaps %xmm1, -0xb0(%rbp)
movups (%rbx), %xmm1
movaps %xmm1, -0xe0(%rbp)
movupd -0xa0(%rbp), %xmm1
movapd %xmm1, -0xd0(%rbp)
movsd %xmm0, -0x60(%rbp)
movsd -0x48(%rbp), %xmm2
addq $0x8, %r14
cmpq $0x2b4af8, %r14         # imm = 0x2B4AF8
movsd -0x30(%rbp), %xmm1
jne 0x2f153
incq %r12
cmpq $0x51, %r12
jne 0x2efe2
movq -0xf0(%rbp), %rbx
movq -0xe8(%rbp), %r12
movq -0x68(%rbp), %r14
movsd -0x60(%rbp), %xmm0
movl 0x8(%r14), %eax
movq 0x18(%r14), %rcx
movq 0x10(%rcx), %rcx
movb 0xc(%r14), %dl
movl %eax, -0x40(%rbp)
movq %rcx, -0x3c(%rbp)
movb %dl, -0x34(%rbp)
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x70(%rbp), %r14
movslq (%r14), %rdx
shlq $0x4, %rdx
movq -0xf8(%rbp), %rsi
movq %rax, -0x10(%rdx,%rsi)
movl %ecx, -0x8(%rdx,%rsi)
shrq $0x20, %rcx
movb %cl, -0x4(%rdx,%rsi)
movq -0x88(%rbp), %rax
leaq (%r15,%rax,8), %rax
movslq (%r14), %rcx
movsd %xmm0, 0x2b9388(%rax,%rcx,8)
leaq -0xb0(%rbp), %rdi
movq -0x90(%rbp), %r13
movq %r13, %rsi
callq 0x33428
movsd -0x58(%rbp), %xmm2
addsd %xmm0, %xmm2
movsd -0x50(%rbp), %xmm0
addsd %xmm1, %xmm0
movslq (%r14), %rax
shlq $0x4, %rax
movq -0x100(%rbp), %rcx
movsd %xmm2, 0x2bb540(%rax,%rcx)
movsd %xmm0, 0x2bb548(%rax,%rcx)
movapd -0xe0(%rbp), %xmm0
movupd %xmm0, 0x2bf8c0(%rax,%rcx)
leaq -0xd0(%rbp), %rdi
movq %r13, %rsi
movq %rcx, %r13
callq 0x33428
movslq (%r14), %rax
shlq $0x4, %rax
movsd %xmm0, 0x2c3c40(%rax,%r13)
movsd %xmm1, 0x2c3c48(%rax,%r13)
testq %r12, %r12
je 0x2f44b
movslq (%r14), %rax
shlq $0x4, %rax
movupd 0x2bb540(%rax,%r13), %xmm0
movupd %xmm0, (%r12)
testq %rbx, %rbx
je 0x2f465
movslq (%r14), %rax
shlq $0x4, %rax
movupd 0x2c3c40(%rax,%r13), %xmm0
movupd %xmm0, (%rbx)
movq -0x88(%rbp), %rax
leaq (%r15,%rax,8), %rbx
movslq (%r14), %r14
callq 0x4a75c
movsd 0x30f8(%rax), %xmm0
minsd 0x2b9388(%rbx,%r14,8), %xmm0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq