// File: /workspace/github_cpp_makefile_star2_O3/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 0x2e200
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, %r15
movq %rdi, %r14
movq %rsi, -0x58(%rbp)
callq 0x2d494
leaq 0x2b4598(%r14), %rdi
leaq 0x2b45a8(%r14), %rsi
movq %rsi, -0xa0(%rbp)
callq 0x3268c
movsd %xmm0, -0x50(%rbp)
movsd %xmm1, -0x30(%rbp)
callq 0x43dce
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r14), %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 0x3af1c(%rip), %xmm1    # 0x69058
subsd %xmm0, %xmm1
movsd %xmm1, -0x48(%rbp)
callq 0x43dce
movsd -0x48(%rbp), %xmm1
movsd -0x50(%rbp), %xmm0
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r14), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3aee7(%rip), %xmm2    # 0x69058
subsd 0x3020(%rax,%rcx), %xmm2
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
mulsd -0x30(%rbp), %xmm1
addsd 0x2b4588(%r14), %xmm0
addsd 0x2b4590(%r14), %xmm1
cmpl $0x4, (%r13)
jl 0x2e204
testq %r12, %r12
je 0x2e1e1
addq $0x48, %r14
movq %r14, %rdi
movq -0xa0(%rbp), %rsi
movsd %xmm0, -0x50(%rbp)
movsd %xmm1, -0x48(%rbp)
callq 0x3268c
movsd -0x50(%rbp), %xmm2
addsd %xmm0, %xmm2
movsd -0x48(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm2, (%r12)
movsd %xmm0, 0x8(%r12)
testq %rbx, %rbx
je 0x2e1ee
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
callq 0x49360
movsd 0x30f8(%rax), %xmm0
jmp 0x2e9e3
xorps %xmm0, %xmm0
retq
movsd (%r15), %xmm3
movsd 0x8(%r15), %xmm2
movsd %xmm0, -0x50(%rbp)
subsd %xmm0, %xmm3
movsd %xmm3, -0x30(%rbp)
movsd %xmm1, -0x48(%rbp)
subsd %xmm1, %xmm2
movapd %xmm2, -0x90(%rbp)
movsd 0x2b45a8(%r14), %xmm0
mulsd 0x3ae61(%rip), %xmm0    # 0x690a0
divsd 0x3ae01(%rip), %xmm0    # 0x69048
leaq -0x40(%rbp), %r15
movq %r13, -0x60(%rbp)
leaq -0xc0(%rbp), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x92d0
movsd (%r15), %xmm0
movsd (%r13), %xmm1
movq -0x60(%rbp), %r15
movsd -0x30(%rbp), %xmm3
movapd %xmm3, %xmm2
mulsd %xmm0, %xmm3
movapd %xmm3, %xmm4
movapd -0x90(%rbp), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm1, %xmm2
subsd %xmm0, %xmm2
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
movapd %xmm2, -0xd0(%rbp)
movapd %xmm2, %xmm1
subsd 0x2b45b0(%r14), %xmm1
movapd %xmm3, -0x90(%rbp)
movapd %xmm3, %xmm2
subsd 0x2b45b8(%r14), %xmm2
movapd %xmm1, %xmm0
andpd 0x3ad3d(%rip), %xmm0    # 0x69010
movsd 0x3ad65(%rip), %xmm3    # 0x69040
ucomisd %xmm0, %xmm3
movq -0x58(%rbp), %r13
jbe 0x2e303
movapd %xmm2, %xmm3
andpd 0x3ad1f(%rip), %xmm3    # 0x69010
xorpd %xmm0, %xmm0
movsd 0x3ad43(%rip), %xmm4    # 0x69040
ucomisd %xmm3, %xmm4
ja 0x2e362
movapd %xmm2, %xmm0
callq 0x90d0
mulsd 0x3ad34(%rip), %xmm0    # 0x69048
divsd 0x3ad34(%rip), %xmm0    # 0x69050
movsd 0x3ad54(%rip), %xmm1    # 0x69078
ucomisd %xmm0, %xmm1
jbe 0x2e33e
movsd 0x3ad3e(%rip), %xmm2    # 0x69070
addsd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x2e332
jmp 0x2e362
ucomisd 0x3c5b2(%rip), %xmm0    # 0x6a8f8
jb 0x2e362
movsd 0x3ad18(%rip), %xmm1    # 0x69068
movsd 0x3c5a0(%rip), %xmm2    # 0x6a8f8
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jae 0x2e358
cvttsd2si %xmm0, %eax
cltq
leaq (%rax,%rax,2), %rcx
movq %rcx, -0x98(%rbp)
shlq $0x4, %rcx
leaq (%r14,%rcx), %r9
movslq (%r15), %rax
movq %rax, %rdx
shlq $0x4, %rdx
movl 0x8(%r13), %esi
movq 0x18(%r13), %rdi
movq 0x10(%rdi), %rdi
movb 0xc(%r13), %r8b
movq %r9, %r13
movl %esi, -0x40(%rbp)
movq %rdi, -0x3c(%rbp)
movb %r8b, -0x34(%rbp)
movq -0x38(%rbp), %rsi
cmpl %esi, 0x2b5008(%rdx,%r9)
jne 0x2e3e2
movq -0x40(%rbp), %rdi
movq %rdi, %r8
shrq $0x20, %r8
cmpl %r8d, 0x2b5004(%rdx,%r13)
jne 0x2e3e2
addq %r13, %rdx
addq $0x2b5000, %rdx         # imm = 0x2B5000
cmpl %edi, (%rdx)
jne 0x2e3e2
shrq $0x20, %rsi
cmpb %sil, 0xc(%rdx)
je 0x2e984
leaq 0x2b45b0(%r14), %rdx
addq %r14, %rcx
addq $0x2b5010, %rcx         # imm = 0x2B5010
movq %rcx, -0x108(%rbp)
movups (%rdx), %xmm0
movaps %xmm0, -0xc0(%rbp)
movups (%rdx), %xmm0
movaps %xmm0, -0xf0(%rbp)
movupd 0x2b45c0(%r14), %xmm0
movapd %xmm0, -0xe0(%rbp)
cmpl $0x1, %eax
movq %r13, -0x110(%rbp)
jne 0x2e4f9
leaq 0x2b45c0(%r14), %rax
movq %rax, -0x30(%rbp)
movapd -0xd0(%rbp), %xmm0
subsd 0x2b45b0(%r14), %xmm0
movapd -0x90(%rbp), %xmm2
subsd 0x2b45b8(%r14), %xmm2
movapd %xmm0, %xmm1
movapd 0x3aba8(%rip), %xmm3    # 0x69010
andpd %xmm3, %xmm0
movsd 0x3abcc(%rip), %xmm4    # 0x69040
ucomisd %xmm0, %xmm4
jbe 0x2e488
andpd %xmm2, %xmm3
xorpd %xmm0, %xmm0
ucomisd %xmm3, %xmm4
ja 0x2e4a1
movapd %xmm2, %xmm0
callq 0x90d0
mulsd 0x3abaf(%rip), %xmm0    # 0x69048
divsd 0x3abaf(%rip), %xmm0    # 0x69050
leaq -0x40(%rbp), %r13
movsd %xmm0, (%r13)
movq -0x58(%rbp), %r15
movq %r15, %rdi
callq 0xe75a
movsd 0x130(%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
callq 0x49360
movsd -0x78(%rbp), %xmm0
mulsd 0x3130(%rax), %xmm0
leaq -0xb0(%rbp), %rcx
movsd %xmm0, (%rcx)
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x326fe
movapd %xmm0, %xmm2
jmp 0x2e89f
movq %r12, -0xf8(%rbp)
movq %rbx, -0x100(%rbp)
leaq 0x48(%r14), %rax
movq %rax, -0x120(%rbp)
leaq 0x2b4870(%r14), %rax
movq %rax, -0x118(%rbp)
xorpd %xmm0, %xmm0
leaq -0x40(%rbp), %rax
movapd %xmm0, (%rax)
xorpd %xmm2, %xmm2
xorl %r12d, %r12d
leaq -0xb0(%rbp), %r13
movsd 0x2b4d80(%r14,%r12,8), %xmm0
ucomisd 0x3bbec(%rip), %xmm0    # 0x6a138
jbe 0x2e880
movsd %xmm2, -0x30(%rbp)
movq %r12, %rax
shlq $0x4, %rax
movq -0x120(%rbp), %rcx
leaq (%rcx,%rax), %rbx
movsd (%rcx,%rax), %xmm0
movsd 0x8(%rcx,%rax), %xmm1
addsd 0x2b4598(%r14), %xmm0
addsd 0x2b45a0(%r14), %xmm1
subsd 0x2b45b0(%r14), %xmm0
movsd %xmm0, -0x70(%rbp)
subsd 0x2b45b8(%r14), %xmm1
movsd %xmm1, -0x68(%rbp)
callq 0x49360
movsd -0x68(%rbp), %xmm4
movsd -0x70(%rbp), %xmm3
movsd 0x30e0(%rax), %xmm0
mulsd %xmm0, %xmm3
mulsd %xmm0, %xmm4
movsd %xmm3, -0x40(%rbp)
movsd %xmm4, -0x38(%rbp)
movq -0x60(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x2e69d
movapd -0xd0(%rbp), %xmm1
subsd (%rbx), %xmm1
movapd -0x90(%rbp), %xmm0
subsd 0x8(%rbx), %xmm0
movapd %xmm1, %xmm2
andpd 0x3aa13(%rip), %xmm2    # 0x69010
movsd 0x3aa3b(%rip), %xmm3    # 0x69040
ucomisd %xmm2, %xmm3
jbe 0x2e629
movapd %xmm0, %xmm3
andpd 0x3a9f9(%rip), %xmm3    # 0x69010
xorpd %xmm2, %xmm2
movsd 0x3aa1d(%rip), %xmm4    # 0x69040
ucomisd %xmm3, %xmm4
ja 0x2e642
callq 0x90d0
movapd %xmm0, %xmm2
mulsd 0x3aa0e(%rip), %xmm2    # 0x69048
divsd 0x3aa0e(%rip), %xmm2    # 0x69050
movsd %xmm2, -0xb0(%rbp)
movq -0x118(%rbp), %rax
leaq (%rax,%r12,8), %rcx
movq %r14, %rdi
leaq -0x40(%rbp), %rsi
movq %r13, %rdx
callq 0x326fe
movsd -0x30(%rbp), %xmm2
ucomisd %xmm2, %xmm0
jbe 0x2e880
movups (%rbx), %xmm1
movaps %xmm1, -0xc0(%rbp)
movups (%rbx), %xmm1
movaps %xmm1, -0xf0(%rbp)
movapd -0x40(%rbp), %xmm1
movapd %xmm1, -0xe0(%rbp)
movapd %xmm0, %xmm2
jmp 0x2e880
addsd (%rbx), %xmm3
addsd 0x8(%rbx), %xmm4
movl $0x2b4870, %r15d        # imm = 0x2B4870
movsd -0x30(%rbp), %xmm2
movsd %xmm3, -0x70(%rbp)
movsd %xmm4, -0x68(%rbp)
movsd -0x569098(%r14,%r15,2), %xmm0
subsd %xmm3, %xmm0
movsd -0x569090(%r14,%r15,2), %xmm1
subsd %xmm4, %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
movsd 0x2b4870(%r14,%r12,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2e86f
movsd %xmm2, -0x30(%rbp)
callq 0x43dce
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r14), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3020(%rax,%rcx), %xmm0
movsd 0x2b4598(%r14), %xmm1
mulsd %xmm0, %xmm1
mulsd 0x2b45a0(%r14), %xmm0
addsd -0x569098(%r14,%r15,2), %xmm1
addsd -0x569090(%r14,%r15,2), %xmm0
subsd (%rbx), %xmm1
movsd %xmm1, -0x78(%rbp)
subsd 0x8(%rbx), %xmm0
movsd %xmm0, -0x128(%rbp)
callq 0x49360
movsd 0x30e0(%rax), %xmm0
movsd -0x78(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd -0x128(%rbp), %xmm2
mulsd %xmm0, %xmm2
movsd %xmm1, -0xb0(%rbp)
movsd %xmm2, -0xa8(%rbp)
movapd -0xd0(%rbp), %xmm1
subsd -0x569098(%r14,%r15,2), %xmm1
movapd -0x90(%rbp), %xmm0
subsd -0x569090(%r14,%r15,2), %xmm0
movapd %xmm1, %xmm2
andpd 0x3a84b(%rip), %xmm2    # 0x69010
movsd 0x3a873(%rip), %xmm3    # 0x69040
ucomisd %xmm2, %xmm3
jbe 0x2e7f1
movapd %xmm0, %xmm3
andpd 0x3a831(%rip), %xmm3    # 0x69010
xorpd %xmm2, %xmm2
movsd 0x3a855(%rip), %xmm4    # 0x69040
ucomisd %xmm3, %xmm4
ja 0x2e80a
callq 0x90d0
movapd %xmm0, %xmm2
mulsd 0x3a846(%rip), %xmm2    # 0x69048
divsd 0x3a846(%rip), %xmm2    # 0x69050
movsd %xmm2, -0x130(%rbp)
leaq (%r14,%r15), %rcx
movq %r14, %rdi
movq %r13, %rsi
leaq -0x130(%rbp), %rdx
callq 0x326fe
movsd -0x30(%rbp), %xmm2
ucomisd %xmm2, %xmm0
jbe 0x2e865
leaq (%r14,%r15,2), %rax
addq $-0x569098, %rax        # imm = 0xFFA96F68
movups (%rax), %xmm1
movaps %xmm1, -0xc0(%rbp)
movups (%rbx), %xmm1
movaps %xmm1, -0xf0(%rbp)
movupd -0xb0(%rbp), %xmm1
movapd %xmm1, -0xe0(%rbp)
movapd %xmm0, %xmm2
movsd -0x70(%rbp), %xmm3
movsd -0x68(%rbp), %xmm4
addq $0x8, %r15
cmpq $0x2b4af8, %r15         # imm = 0x2B4AF8
jne 0x2e6bb
incq %r12
cmpq $0x51, %r12
jne 0x2e53a
movq -0x100(%rbp), %rbx
movq -0xf8(%rbp), %r12
movq -0x58(%rbp), %r15
movl 0x8(%r15), %eax
movq 0x18(%r15), %rcx
movq 0x10(%rcx), %rcx
movb 0xc(%r15), %dl
movl %eax, -0x40(%rbp)
movq %rcx, -0x3c(%rbp)
movb %dl, -0x34(%rbp)
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x60(%rbp), %r15
movslq (%r15), %rdx
shlq $0x4, %rdx
movq -0x108(%rbp), %rsi
movq %rax, -0x10(%rdx,%rsi)
movl %ecx, -0x8(%rdx,%rsi)
shrq $0x20, %rcx
movb %cl, -0x4(%rdx,%rsi)
movq -0x98(%rbp), %rax
leaq (%r14,%rax,8), %rax
movslq (%r15), %rcx
movsd %xmm2, 0x2b9388(%rax,%rcx,8)
leaq -0xc0(%rbp), %rdi
movq -0xa0(%rbp), %r13
movq %r13, %rsi
callq 0x3268c
movsd -0x50(%rbp), %xmm2
addsd %xmm0, %xmm2
movsd -0x48(%rbp), %xmm0
addsd %xmm1, %xmm0
movslq (%r15), %rax
shlq $0x4, %rax
movq -0x110(%rbp), %rcx
movsd %xmm2, 0x2bb540(%rax,%rcx)
movsd %xmm0, 0x2bb548(%rax,%rcx)
movapd -0xf0(%rbp), %xmm0
movupd %xmm0, 0x2bf8c0(%rax,%rcx)
leaq -0xe0(%rbp), %rdi
movq %r13, %rsi
movq %rcx, %r13
callq 0x3268c
movslq (%r15), %rax
movq %rax, %rcx
shlq $0x4, %rcx
movsd %xmm0, 0x2c3c40(%rcx,%r13)
movsd %xmm1, 0x2c3c48(%rcx,%r13)
testq %r12, %r12
je 0x2e9a2
cltq
shlq $0x4, %rax
movupd 0x2bb540(%rax,%r13), %xmm0
movupd %xmm0, (%r12)
movl (%r15), %eax
testq %rbx, %rbx
je 0x2e9be
cltq
shlq $0x4, %rax
movupd 0x2c3c40(%rax,%r13), %xmm0
movupd %xmm0, (%rbx)
movl (%r15), %eax
movq -0x98(%rbp), %rcx
leaq (%r14,%rcx,8), %rbx
movslq %eax, %r14
callq 0x49360
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
nop