// File: /workspace/github_cpp_makefile_star2_O2/wrighteagle2d[P]wrighteaglebase/Debug/../src/Kicker.cpp
// Function: Kicker::MultiCycleKick(Agent const&, int)
ActionPlan Kicker::MultiCycleKick(const Agent &agent, int cycle) {
  Assert(cycle > 1);

  UpdateKickData(agent);

  ActionPlan plan;
  AtomicAction act;

  /** 首先考虑能否先转身再踢，可以就不进行后面的搜索了 */
  act = GetTurnAction(agent);
  if (act.mSucceed == true) {
    plan.mCycle = 2;
    plan.mSucceed = true;
    plan.mActionQueue.clear();
    plan.mActionQueue.push_back(act);
    return plan;
  }

  /** 其次考虑能否先停再转身，再踢 */
  double speed_buf = 0.1;
  int index = -1;
  double turn_max_speed = 0.0;
  double turn_poss = 0.0;
  if (cycle >= 3) {
    act = TurnPlan(agent, index, turn_max_speed);
    if (act.mSucceed == true && index >= 0 &&
        turn_max_speed > mKickSpeed + 0.1) {
      turn_poss =
          mPointEva[index] *
          mSpeedCurve.GetOutput(turn_max_speed - mKickSpeed + speed_buf);
      turn_poss *= 1.2; /** 这里要仔细调下 */
    }
  }

  /** 下面进行多脚踢球的规划 */
  // target及mPoint都是在踢球的最后一个周期的坐标系中
  Vector target =
      mKickTarget -
      mInput.mPlayerVel * ((1 - pow(PlayerParam::instance()
                                        .HeteroPlayer(mInput.mPlayerType)
                                        .playerDecay(),
                                    cycle - 1)) /
                           (1 - PlayerParam::instance()
                                    .HeteroPlayer(mInput.mPlayerType)
                                    .playerDecay()));
  int best = -1;
  double poss = 0.0;
  double max_poss = 0.0;

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

  if (cycle < 4 && mMaxSpeedFlag[idx][cycle - 1] == mAgentID) {
    best = NearestPoint(mMaxSpeedOutFirstPos[idx][cycle - 1]);

    // std::cerr << agent.GetSelfUnum() << "@" <<
    // agent.GetWorldState().CurrentTime() << ": good" << std::endl;

    if (best >= 0) {
      poss = mPointEva[best] * mSpeedCurve.GetOutput(mMaxSpeed[idx][cycle - 1] -
                                                     mKickSpeed + speed_buf);
    }
  }

  if (poss < 0.001) {
    // std::cerr << agent.GetSelfUnum() << "@" <<
    // agent.GetWorldState().CurrentTime() << ": no good" << std::endl;

    for (int k = 0, v = cycle - 2, j = NearestPoint(mInput.mBallPos);
         k < POINTS_NUM; ++k) {
      int i =
          (int)(GetNormalizeAngleDeg((target - mPoint[k]).Dir(), -FLOAT_EPS) /
                STEP_KICK_ANGLE);
      if (mPointEva[k] > 0.001 &&
          (double)mKickerValue[v][i][j][k] >
              mKickSpeed - speed_buf) // 要大于期望的出球速度
      {
        // mPoint[k]+mInput.mPlayerVel是mPoint[k]在踢球的第一个周期的坐标系中的位置
        Vector ball_vel = (mPoint[k] + mInput.mPlayerVel - mInput.mBallPos) *
                          ServerParam::instance().ballDecay(); // 踢到k后的球速
        double speed = 0.0;

        if (cycle == 2) // 前向搜索1步
        {
          speed = GetOneKickMaxSpeed(ball_vel, (target - mPoint[k]).Dir(),
                                     mMaxAccel[k]);
        } 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
            {
              // mPoint[l]+PlayerVel*Decay是mPoint[l]在踢球的第二个周期的坐标系中的位置
              Vector ball_vel_next =
                  (mPoint[l] +
                   mInput.mPlayerVel * PlayerParam::instance()
                                           .HeteroPlayer(mInput.mPlayerType)
                                           .playerDecay() -
                   mPoint[k]) *
                  ServerParam::instance().ballDecay();
              if (cycle == 3) {
                speed =
                    Max(speed, GetOneKickMaxSpeed(ball_vel_next,
                                                  (target - mPoint[l]).Dir(),
                                                  mMaxAccel[l]));
              } else if (cycle ==
                         4) // 利用mKickerValue[1]的信息，没必要再前向了
              {
                int t = (int)(GetNormalizeAngleDeg((target - mPoint[l]).Dir(),
                                                   -FLOAT_EPS) /
                              STEP_KICK_ANGLE);
                speed = Max(speed, (double)mKickerValue[1][t][k][l]);
              }
            }
          }
        }

        if (speed > mKickSpeed - speed_buf) {
          poss = mPointEva[k] *
                 mSpeedCurve.GetOutput(speed - mKickSpeed + speed_buf);
        } else {
          poss = 0.0;
        }

        if (poss > max_poss) // 找到poss最大的点
        {
          best = k;
          max_poss = poss;
          // max_speed   = speed;
        }
      }
    }
  }

  if (turn_poss > max_poss) // 转身规划的收益较高
  {
    plan.mCycle = 3;
    plan.mSucceed = true;
    plan.mActionQueue.clear();
    plan.mActionQueue.push_back(act);
    return plan;
  }

  // 不进行转身的规划，进行多脚踢球
  plan.mCycle = cycle;
  plan.mSucceed = false;
  if (best >= 0) {
    act = GetOneKickAction(agent, mInput.mBallPos, mInput.mBallVel,
                           mInput.mPlayerVel + mPoint[best]);
    if (act.mSucceed == true) {
      plan.mSucceed = true;
      plan.mActionQueue.clear();
      plan.mActionQueue.push_back(act);
    }
  }
  return plan;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rcx, -0x48(%rbp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x2703e
movq %r14, %rdi
callq 0x2a916
leaq -0xe0(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x29cde
movupd -0xe0(%rbp), %xmm0
movupd -0xd0(%rbp), %xmm1
movupd -0xc0(%rbp), %xmm2
movapd %xmm2, -0x110(%rbp)
movapd %xmm1, -0x120(%rbp)
movapd %xmm0, -0x130(%rbp)
cmpb $0x1, -0x130(%rbp)
jne 0x2863b
movl $0x2, 0x4(%r14)
movb $0x1, (%r14)
leaq 0x8(%r14), %rbx
movq %rbx, %rdi
callq 0x2a952
leaq -0x130(%rbp), %rsi
movq %rbx, %rdi
callq 0x2a988
jmp 0x28ea4
cmpl $0x3, -0x48(%rbp)
movq %r14, -0x50(%rbp)
jl 0x28687
leaq -0xe0(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2a02e
movupd -0xe0(%rbp), %xmm0
movupd -0xd0(%rbp), %xmm1
movupd -0xc0(%rbp), %xmm2
movapd %xmm2, -0x110(%rbp)
movapd %xmm1, -0x120(%rbp)
movapd %xmm0, -0x130(%rbp)
callq 0x38d94
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r15), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3020(%rax,%rcx), %xmm0
movq -0x48(%rbp), %rax
leal -0x1(%rax), %ebx
cvtsi2sd %ebx, %xmm1
callq 0x98a0
movsd %xmm0, -0x30(%rbp)
callq 0x38d94
movq %r12, -0x138(%rbp)
movsd 0x2e975(%rip), %xmm0    # 0x57048
movapd %xmm0, %xmm1
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r15), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
subsd 0x3020(%rax,%rcx), %xmm0
subsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd 0x2b4598(%r15), %xmm0
mulsd %xmm1, %xmm0
mulsd 0x2b45a0(%r15), %xmm1
movsd 0x2b45d8(%r15), %xmm2
subsd %xmm0, %xmm2
movsd 0x2b45e0(%r15), %xmm3
movsd %xmm2, -0x70(%rbp)
movapd %xmm2, %xmm0
subsd 0x2b45b0(%r15), %xmm0
subsd %xmm1, %xmm3
movsd %xmm3, -0x68(%rbp)
movapd %xmm3, %xmm1
subsd 0x2b45b8(%r15), %xmm1
leaq -0xe0(%rbp), %rdi
movsd %xmm0, (%rdi)
movsd %xmm1, 0x8(%rdi)
callq 0xcd30
leaq 0x2b45b0(%r15), %rax
movq %rax, -0xe8(%rbp)
movabsq $-0x4126d58c8eef1bac, %rax # imm = 0xBED92A737110E454
leaq -0x60(%rbp), %rdi
movq %rax, (%rdi)
callq 0xd305
pushq $-0x1
popq %rax
movl %eax, -0x34(%rbp)
cmpl $0x3, -0x48(%rbp)
jg 0x28848
cvttsd2si %xmm0, %eax
movslq %eax, %r14
imulq $0x30, %r14, %r13
addq %r15, %r13
movslq %ebx, %r12
movq %r12, %rbx
shlq $0x4, %rbx
leaq (%rbx,%r13), %rdi
addq $0x2b5010, %rdi         # imm = 0x2B5010
movq %r15, %rsi
callq 0x2a9d6
testb %al, %al
je 0x28848
leaq (%rbx,%r13), %rsi
addq $0x2bf8d0, %rsi         # imm = 0x2BF8D0
movq %r15, %rdi
callq 0x2ac8a
testl %eax, %eax
movl %eax, -0x34(%rbp)
js 0x28848
movl %eax, %ebx
movsd 0x2b4d80(%r15,%rbx,8), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0x2b4558(%r15), %rdi
imulq $0x18, %r14, %rax
addq %r15, %rax
movsd 0x2b9390(%rax,%r12,8), %xmm0
subsd 0x2b45d0(%r15), %xmm0
addsd 0x2fd72(%rip), %xmm0    # 0x58590
leaq -0xe0(%rbp), %rsi
movsd %xmm0, (%rsi)
pushq $0x1
popq %rdx
callq 0x2aa02
mulsd -0x30(%rbp), %xmm0
movsd 0x2f8ba(%rip), %xmm1    # 0x580f8
ucomisd %xmm0, %xmm1
jbe 0x28dde
movq %r15, %rdi
movq -0xe8(%rbp), %rsi
callq 0x2ac8a
movq -0x48(%rbp), %r12
leal -0x2(%r12), %ecx
leaq 0x48(%r15), %rdx
movq %rdx, -0xf8(%rbp)
leaq 0x558(%r15), %rdx
movslq %ecx, %rcx
cltq
leaq 0x2b4870(%r15), %rsi
movq %rsi, -0x148(%rbp)
leaq 0xe6fe8(%r15), %rsi
movq %rsi, -0xf0(%rbp)
leaq 0x2b4558(%r15), %rsi
movq %rsi, -0x140(%rbp)
xorpd %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
xorl %r13d, %r13d
imulq $0xe6a90, %rcx, %rcx    # imm = 0xE6A90
addq %rdx, %rcx
imulq $0x144, %rax, %rax      # imm = 0x144
addq %rcx, %rax
movq %rax, -0x150(%rbp)
movabsq $-0x4126d58c8eef1bac, %r14 # imm = 0xBED92A737110E454
cmpq $0x51, %r13
je 0x28da7
movq %r13, %rbx
shlq $0x4, %rbx
movsd -0x70(%rbp), %xmm0
movq -0xf8(%rbp), %rax
subsd (%rax,%rbx), %xmm0
movsd -0x68(%rbp), %xmm1
subsd 0x8(%rax,%rbx), %xmm1
movsd %xmm0, -0xe0(%rbp)
movsd %xmm1, -0xd8(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xcd30
movq %r14, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xd305
movsd 0x2b4d80(%r15,%r13,8), %xmm1
ucomisd 0x2f7be(%rip), %xmm1    # 0x580f8
jbe 0x28d94
divsd 0x2ede0(%rip), %xmm0    # 0x57728
cvttsd2si %xmm0, %eax
cltq
imulq $0x6684, %rax, %rax     # imm = 0x6684
addq -0x150(%rbp), %rax
cvtss2sd (%rax,%r13,4), %xmm0
movsd 0x2b45d0(%r15), %xmm1
addsd 0x2fc25(%rip), %xmm1    # 0x58598
ucomisd %xmm1, %xmm0
jbe 0x28d94
addq -0xf8(%rbp), %rbx
movsd (%rbx), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0x8(%rbx), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd 0x2b4598(%r15), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0x2b45a0(%r15), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd 0x2b45b0(%r15), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd 0x2b45b8(%r15), %xmm0
movsd %xmm0, -0x88(%rbp)
callq 0x3db2c
movsd -0x78(%rbp), %xmm2
addsd -0x90(%rbp), %xmm2
subsd -0x88(%rbp), %xmm2
movsd -0x40(%rbp), %xmm1
addsd -0x30(%rbp), %xmm1
subsd -0x98(%rbp), %xmm1
movsd 0x30e0(%rax), %xmm0
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm2
movsd %xmm1, -0xe0(%rbp)
movsd %xmm2, -0xd8(%rbp)
cmpl $0x2, %r12d
jne 0x28a83
movsd -0x70(%rbp), %xmm0
subsd (%rbx), %xmm0
movsd -0x68(%rbp), %xmm1
subsd 0x8(%rbx), %xmm1
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x58(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xcd30
movsd %xmm0, -0xa8(%rbp)
movq -0x148(%rbp), %rax
leaq (%rax,%r13,8), %rcx
movq %r15, %rdi
leaq -0xe0(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
callq 0x2a844
movsd %xmm0, -0x30(%rbp)
jmp 0x28d18
movapd %xmm2, %xmm0
addsd (%rbx), %xmm1
addsd 0x8(%rbx), %xmm0
xorpd %xmm2, %xmm2
movl $0x2b4870, %r14d        # imm = 0x2B4870
movq -0xf0(%rbp), %r12
movsd %xmm1, -0x40(%rbp)
movsd %xmm0, -0x78(%rbp)
movsd %xmm2, -0x30(%rbp)
cmpq $0x2b4af8, %r14         # imm = 0x2B4AF8
je 0x28d0a
movapd %xmm0, %xmm2
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
sqrtsd %xmm0, %xmm0
movsd 0x2b4870(%r15,%r13,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x28c64
callq 0x38d94
movq 0x5c38(%rax), %rax
movslq 0x2b4580(%r15), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x2b4598(%r15), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd 0x2b45a0(%r15), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd 0x3020(%rax,%rcx), %xmm0
movsd %xmm0, -0x168(%rbp)
movsd -0x569098(%r15,%r14,2), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x569090(%r15,%r14,2), %xmm0
movsd %xmm0, -0x160(%rbp)
movsd (%rbx), %xmm0
movsd %xmm0, -0x170(%rbp)
movsd 0x8(%rbx), %xmm0
movsd %xmm0, -0x158(%rbp)
callq 0x3db2c
movsd -0x98(%rbp), %xmm1
movsd -0x168(%rbp), %xmm0
mulsd %xmm0, %xmm1
addsd -0x160(%rbp), %xmm1
subsd -0x158(%rbp), %xmm1
movsd -0x90(%rbp), %xmm2
mulsd %xmm0, %xmm2
addsd -0x88(%rbp), %xmm2
subsd -0x170(%rbp), %xmm2
movsd 0x30e0(%rax), %xmm0
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm1
movsd %xmm2, -0x60(%rbp)
movsd %xmm1, -0x58(%rbp)
movq -0x48(%rbp), %rax
cmpl $0x4, %eax
movsd -0x40(%rbp), %xmm1
movsd -0x30(%rbp), %xmm2
je 0x28c78
cmpl $0x3, %eax
jne 0x28cf8
movsd -0x70(%rbp), %xmm0
subsd -0x569098(%r15,%r14,2), %xmm0
movsd -0x68(%rbp), %xmm1
subsd -0x569090(%r15,%r14,2), %xmm1
movsd %xmm0, -0xa8(%rbp)
movsd %xmm1, -0xa0(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xcd30
movsd %xmm0, -0xb0(%rbp)
leaq (%r15,%r14), %rcx
movq %r15, %rdi
leaq -0x60(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x2a844
jmp 0x28cea
movsd -0x40(%rbp), %xmm1
movsd -0x78(%rbp), %xmm0
movsd -0x30(%rbp), %xmm2
jmp 0x28cfd
movsd -0x70(%rbp), %xmm0
subsd -0x569098(%r15,%r14,2), %xmm0
movsd -0x68(%rbp), %xmm1
subsd -0x569090(%r15,%r14,2), %xmm1
movsd %xmm0, -0xa8(%rbp)
movsd %xmm1, -0xa0(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xcd30
movabsq $-0x4126d58c8eef1bac, %rax # imm = 0xBED92A737110E454
movq %rax, -0xb0(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xd305
divsd 0x2ea51(%rip), %xmm0    # 0x57728
cvttsd2si %xmm0, %eax
cltq
imulq $0x6684, %rax, %rax     # imm = 0x6684
cvtss2sd (%r12,%rax), %xmm0
maxsd -0x30(%rbp), %xmm0
movapd %xmm0, %xmm2
movsd -0x40(%rbp), %xmm1
movsd -0x78(%rbp), %xmm0
addq $0x8, %r14
addq $0x4, %r12
jmp 0x28aab
movq -0x48(%rbp), %r12
movabsq $-0x4126d58c8eef1bac, %r14 # imm = 0xBED92A737110E454
movsd 0x2b45d0(%r15), %xmm1
movapd %xmm1, %xmm2
addsd 0x2f86b(%rip), %xmm2    # 0x58598
xorpd %xmm0, %xmm0
movsd -0x30(%rbp), %xmm3
ucomisd %xmm2, %xmm3
jbe 0x28d78
movapd %xmm3, %xmm2
movsd 0x2b4d80(%r15,%r13,8), %xmm0
movsd %xmm0, -0x40(%rbp)
subsd %xmm1, %xmm2
addsd 0x2f835(%rip), %xmm2    # 0x58590
movsd %xmm2, -0x60(%rbp)
movq -0x140(%rbp), %rdi
leaq -0x60(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x2aa02
mulsd -0x40(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
ucomisd %xmm1, %xmm0
movl -0x34(%rbp), %eax
cmoval %r13d, %eax
movl %eax, -0x34(%rbp)
maxsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
incq %r13
addq $0x144, -0xf0(%rbp) # imm = 0x144
jmp 0x288d2
xorpd %xmm0, %xmm0
ucomisd -0x80(%rbp), %xmm0
jbe 0x28dc3
movq -0x50(%rbp), %r14
movl $0x3, 0x4(%r14)
jmp 0x28e85
movq -0x50(%rbp), %r14
movl %r12d, 0x4(%r14)
movb $0x0, (%r14)
movl -0x34(%rbp), %eax
testl %eax, %eax
js 0x28ea4
movl %eax, %ebx
jmp 0x28dee
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %rax
movl %eax, 0x4(%r14)
movb $0x0, (%r14)
shlq $0x4, %rbx
movsd 0x2b4598(%r15), %xmm0
movsd 0x2b45a0(%r15), %xmm1
addsd 0x48(%r15,%rbx), %xmm0
addsd 0x50(%r15,%rbx), %xmm1
leaq 0x2b45c0(%r15), %r8
leaq -0x60(%rbp), %r9
movsd %xmm0, (%r9)
movsd %xmm1, 0x8(%r9)
movl $0x1, (%rsp)
leaq -0xe0(%rbp), %rdi
movq %r15, %rsi
movq -0x138(%rbp), %rdx
movq -0xe8(%rbp), %rcx
callq 0x2972c
movupd -0xe0(%rbp), %xmm0
movupd -0xd0(%rbp), %xmm1
movupd -0xc0(%rbp), %xmm2
movapd %xmm2, -0x110(%rbp)
movapd %xmm1, -0x120(%rbp)
movapd %xmm0, -0x130(%rbp)
cmpb $0x1, -0x130(%rbp)
jne 0x28ea4
movb $0x1, (%r14)
leaq 0x8(%r14), %rbx
movq %rbx, %rdi
callq 0x2a952
leaq -0x130(%rbp), %rsi
movq %rbx, %rdi
callq 0x2a988
movq %r14, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ecb
jmp 0x28ee1
movq %r14, -0x50(%rbp)
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
jmp 0x28ee1
movq %rax, %rbx
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
callq 0x2b0c6
movq %rbx, %rdi
callq 0x98b0
nop