// File: /workspace/github_cpp_makefile_star2_O2/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 0x279f9
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %r8, %r12
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq %r9, -0x58(%rbp)
callq 0x2703e
leaq 0x2b4598(%r14), %rdi
leaq 0x2b45a8(%r14), %rsi
movq %rsi, -0xd8(%rbp)
callq 0xd02c
movsd %xmm0, -0x40(%rbp)
movsd %xmm1, -0x30(%rbp)
callq 0x38d94
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r14), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3020(%rax,%rcx), %xmm0
movl (%r13), %eax
decl %eax
cvtsi2sd %eax, %xmm1
callq 0x98a0
movsd 0x2f717(%rip), %xmm1    # 0x57048
subsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
callq 0x38d94
movsd -0x38(%rbp), %xmm3
movsd -0x40(%rbp), %xmm2
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r14), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x2f6e2(%rip), %xmm0    # 0x57048
subsd 0x3020(%rax,%rcx), %xmm0
divsd %xmm0, %xmm3
mulsd %xmm3, %xmm2
mulsd -0x30(%rbp), %xmm3
addsd 0x2b4588(%r14), %xmm2
addsd 0x2b4590(%r14), %xmm3
cmpl $0x4, (%r13)
jl 0x279fd
testq %r12, %r12
je 0x279d6
addq $0x48, %r14
movq %r14, %rdi
movq -0xd8(%rbp), %rsi
movsd %xmm2, -0x40(%rbp)
movsd %xmm3, -0x38(%rbp)
callq 0xd02c
movsd -0x40(%rbp), %xmm2
addsd %xmm0, %xmm2
movsd -0x38(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm2, (%r12)
movsd %xmm0, 0x8(%r12)
movq -0x58(%rbp), %rax
testq %rax, %rax
je 0x279e7
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
callq 0x3db2c
movsd 0x30f8(%rax), %xmm0
jmp 0x2806a
xorps %xmm0, %xmm0
retq
movq %r12, -0xd0(%rbp)
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd %xmm2, -0x40(%rbp)
subsd %xmm2, %xmm0
movsd %xmm3, -0x38(%rbp)
subsd %xmm3, %xmm1
leaq -0x50(%rbp), %rbx
movsd %xmm0, (%rbx)
movsd %xmm1, 0x8(%rbx)
movsd 0x2b45a8(%r14), %xmm0
xorpd 0x2f5e3(%rip), %xmm0    # 0x57020
leaq -0xb0(%rbp), %r12
movlpd %xmm0, (%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd02c
movsd %xmm0, -0x68(%rbp)
subsd 0x2b45b0(%r14), %xmm0
movsd %xmm1, -0x60(%rbp)
subsd 0x2b45b8(%r14), %xmm1
movsd %xmm0, (%rbx)
movsd %xmm1, 0x8(%rbx)
leaq -0x50(%rbp), %rdi
callq 0xcd30
movabsq $-0x4126d58c8eef1bac, %rax # imm = 0xBED92A737110E454
movq %rax, (%r12)
leaq -0xb0(%rbp), %rdi
callq 0xd305
cvttsd2si %xmm0, %eax
cltq
movq %rax, -0xc8(%rbp)
imulq $0x30, %rax, %r12
leaq (%r14,%r12), %rcx
movslq (%r13), %rax
shlq $0x4, %rax
movq %rcx, -0xc0(%rbp)
leaq (%rax,%rcx), %rbx
addq $0x2b5000, %rbx         # imm = 0x2B5000
movq %r15, -0xb8(%rbp)
movq %r15, %rdi
callq 0x21bf6
leaq -0x50(%rbp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movb %dl, 0xc(%rcx)
leaq -0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0x2a836
movl (%r13), %ecx
testb %al, %al
je 0x27bdd
leaq 0x2b45b0(%r14), %rax
leaq (%r14,%r12), %rdx
addq $0x2b5010, %rdx         # imm = 0x2B5010
movq %rdx, -0x108(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x50(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0xb0(%rbp)
movupd 0x2b45c0(%r14), %xmm0
movapd %xmm0, -0x100(%rbp)
cmpl $0x1, %ecx
jne 0x27bfb
movsd -0x68(%rbp), %xmm0
subsd 0x2b45b0(%r14), %xmm0
movsd -0x60(%rbp), %xmm1
subsd 0x2b45b8(%r14), %xmm1
leaq 0x2b45c0(%r14), %rbx
leaq -0xa0(%rbp), %rdi
movsd %xmm0, (%rdi)
movsd %xmm1, 0x8(%rdi)
callq 0xcd30
leaq -0x88(%rbp), %r15
movsd %xmm0, (%r15)
movq -0xb8(%rbp), %r12
movq %r12, %rdi
callq 0xcd42
movsd 0x130(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x3db2c
movsd -0x30(%rbp), %xmm0
mulsd 0x3130(%rax), %xmm0
leaq -0xe8(%rbp), %rcx
movsd %xmm0, (%rcx)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2a844
movsd %xmm0, -0x30(%rbp)
jmp 0x27f32
movq -0x58(%rbp), %rdx
movq -0xd0(%rbp), %rsi
movq -0xc8(%rbp), %r15
movq -0xc0(%rbp), %r12
jmp 0x2800d
leaq 0x48(%r14), %rax
movq %rax, -0x120(%rbp)
leaq 0x2b4870(%r14), %rax
movq %rax, -0x110(%rbp)
xorpd %xmm0, %xmm0
leaq -0xa0(%rbp), %rax
movapd %xmm0, (%rax)
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
leaq -0xe8(%rbp), %r15
movq %r13, -0x118(%rbp)
cmpq $0x51, %r12
je 0x27f26
movsd 0x2b4d80(%r14,%r12,8), %xmm0
ucomisd 0x304a4(%rip), %xmm0    # 0x580f8
jbe 0x27f1e
movsd %xmm1, -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, -0x78(%rbp)
subsd 0x2b45b8(%r14), %xmm1
movsd %xmm1, -0x70(%rbp)
callq 0x3db2c
movsd -0x70(%rbp), %xmm3
movsd -0x78(%rbp), %xmm2
movsd 0x30e0(%rax), %xmm0
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm3
movsd %xmm2, -0xa0(%rbp)
movsd %xmm3, -0x98(%rbp)
cmpl $0x2, (%r13)
jne 0x27d6c
movsd -0x68(%rbp), %xmm0
subsd (%rbx), %xmm0
movsd -0x60(%rbp), %xmm1
subsd 0x8(%rbx), %xmm1
movsd %xmm0, -0x88(%rbp)
movsd %xmm1, -0x80(%rbp)
leaq -0x88(%rbp), %rdi
callq 0xcd30
movsd %xmm0, -0xe8(%rbp)
movq -0x110(%rbp), %rax
leaq (%rax,%r12,8), %rcx
movq %r14, %rdi
leaq -0xa0(%rbp), %rsi
movq %r15, %rdx
callq 0x2a844
movsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x27f1e
movups (%rbx), %xmm1
movaps %xmm1, -0x50(%rbp)
movups (%rbx), %xmm1
movaps %xmm1, -0xb0(%rbp)
movaps -0xa0(%rbp), %xmm1
movaps %xmm1, -0x100(%rbp)
movapd %xmm0, %xmm1
jmp 0x27f1e
addsd (%rbx), %xmm2
addsd 0x8(%rbx), %xmm3
movl $0x2b4870, %r13d        # imm = 0x2B4870
movsd %xmm2, -0x78(%rbp)
movsd %xmm3, -0x70(%rbp)
cmpq $0x2b4af8, %r13         # imm = 0x2B4AF8
je 0x27f12
movsd -0x569098(%r14,%r13,2), %xmm0
subsd %xmm2, %xmm0
movsd -0x569090(%r14,%r13,2), %xmm1
subsd %xmm3, %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
movsd 0x2b4870(%r14,%r12,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x27f09
callq 0x38d94
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,%r13,2), %xmm1
addsd -0x569090(%r14,%r13,2), %xmm0
subsd (%rbx), %xmm1
movsd %xmm1, -0x130(%rbp)
subsd 0x8(%rbx), %xmm0
movsd %xmm0, -0x128(%rbp)
callq 0x3db2c
movsd 0x30e0(%rax), %xmm0
movsd -0x130(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd -0x128(%rbp), %xmm2
mulsd %xmm0, %xmm2
movsd %xmm1, -0x88(%rbp)
movsd %xmm2, -0x80(%rbp)
movsd -0x68(%rbp), %xmm0
subsd -0x569098(%r14,%r13,2), %xmm0
movsd -0x60(%rbp), %xmm1
subsd -0x569090(%r14,%r13,2), %xmm1
movsd %xmm0, -0xe8(%rbp)
movsd %xmm1, -0xe0(%rbp)
movq %r15, %rdi
callq 0xcd30
movsd %xmm0, -0x138(%rbp)
leaq (%r14,%r13), %rcx
movq %r14, %rdi
leaq -0x88(%rbp), %rsi
leaq -0x138(%rbp), %rdx
callq 0x2a844
movsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x27efa
leaq (%r14,%r13,2), %rax
addq $-0x569098, %rax        # imm = 0xFFA96F68
movups (%rax), %xmm1
movaps %xmm1, -0x50(%rbp)
movups (%rbx), %xmm1
movaps %xmm1, -0xb0(%rbp)
movups -0x88(%rbp), %xmm1
movaps %xmm1, -0x100(%rbp)
movapd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movsd -0x78(%rbp), %xmm2
movsd -0x70(%rbp), %xmm3
addq $0x8, %r13
jmp 0x27d85
movq -0x118(%rbp), %r13
movsd -0x30(%rbp), %xmm1
incq %r12
jmp 0x27c38
movq -0xb8(%rbp), %r12
movsd %xmm1, -0x30(%rbp)
movq %r12, %rdi
callq 0x21bf6
movslq (%r13), %rcx
shlq $0x4, %rcx
movq -0x108(%rbp), %rsi
movq %rax, -0x10(%rcx,%rsi)
movl %edx, -0x8(%rcx,%rsi)
shrq $0x20, %rdx
movb %dl, -0x4(%rcx,%rsi)
movq -0xc8(%rbp), %r15
imulq $0x18, %r15, %rax
addq %r14, %rax
movslq (%r13), %rcx
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x2b9388(%rax,%rcx,8)
leaq -0x50(%rbp), %rdi
movq -0xd8(%rbp), %rbx
movq %rbx, %rsi
callq 0xd02c
movsd -0x40(%rbp), %xmm2
addsd %xmm0, %xmm2
movsd -0x38(%rbp), %xmm0
addsd %xmm1, %xmm0
movslq (%r13), %rax
shlq $0x4, %rax
movq -0xc0(%rbp), %r12
movsd %xmm2, 0x2bb540(%rax,%r12)
movsd %xmm0, 0x2bb548(%rax,%r12)
movapd -0xb0(%rbp), %xmm0
movupd %xmm0, 0x2bf8c0(%rax,%r12)
leaq -0x100(%rbp), %rdi
movq %rbx, %rsi
callq 0xd02c
movslq (%r13), %rcx
movq %rcx, %rax
shlq $0x4, %rax
movsd %xmm0, 0x2c3c40(%rax,%r12)
movsd %xmm1, 0x2c3c48(%rax,%r12)
movq -0x58(%rbp), %rdx
movq -0xd0(%rbp), %rsi
testq %rsi, %rsi
je 0x2802b
movslq %ecx, %rax
shlq $0x4, %rax
movupd 0x2bb540(%rax,%r12), %xmm0
movupd %xmm0, (%rsi)
movl (%r13), %ecx
testq %rdx, %rdx
je 0x28049
movslq %ecx, %rax
shlq $0x4, %rax
movupd 0x2c3c40(%rax,%r12), %xmm0
movupd %xmm0, (%rdx)
movl (%r13), %ecx
imulq $0x18, %r15, %rax
addq %rax, %r14
movslq %ecx, %rbx
callq 0x3db2c
movsd 0x30f8(%rax), %xmm0
minsd 0x2b9388(%r14,%rbx,8), %xmm0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq