// File: /workspace/github_cpp_makefile_star2_O2/wrighteagle2d[P]wrighteaglebase/Debug/../src/Dasher.cpp
// Function: Dasher::CycleNeedToPointWithCertainPosture(PlayerState const&, Vector, bool, double*)
int Dasher::CycleNeedToPointWithCertainPosture(const PlayerState &player,
                                               Vector target,
                                               const bool inverse,
                                               double *buf) {
  int cycle = 0; //用的周期

  const double &decay = player.GetPlayerDecay();
  const double &speedmax = player.GetEffectiveSpeedMax();
  const double &stamina = player.GetStamina();
  const double &stamina_inc_max = player.GetStaminaIncMax();
  const double &dash_max = ServerParam::instance().maxDashPower();
  const Vector &pos = player.GetPos();
  const Vector &vel = player.GetVel();

  const double accrate = player.GetDashPowerRate() * player.GetEffort();
  double speed = vel.Mod();

  const Vector predict_pos_1 = pos + vel;
  const Vector predict_pos_2 = predict_pos_1 + vel * decay;
  const double dir = (target - pos).Dir();
  double dis = (target - predict_pos_1).Mod();

  const double kick_area = player.IsGoalie()
                               ? ServerParam::instance().catchAreaLength()
                               : (player.GetKickableArea() - GETBALL_BUFFER);

  if (dis <= kick_area) {
    dis = pos.Dist(target);
    if (buf)
      *buf = 0;
    return 1;
  }

  double facing;
  if (player.IsBodyDirValid()) {
    facing = player.GetBodyDir();
  } else if (speed > 0.26) {
    facing = vel.Dir();
  } else {
    facing = dir; //认为不用转身
  }

  double diffang = fabs(GetNormalizeAngleDeg(dir - facing));
  const double oneturnang = player.GetMaxTurnAngle();
  const double stamina_recovery_thr = ServerParam::instance().recoverDecThr() *
                                      ServerParam::instance().staminaMax();

  double angbuf = FLOAT_EPS;
  angbuf = ASin(kick_area / dis);
  angbuf = Max(angbuf, 15.0);

  if (inverse) {
    diffang = 180.0 - diffang;
    facing = GetNormalizeAngleDeg(180.0 + facing);
  }

  // I 调整阶段
  if (diffang <= angbuf) { //不需要转身
    target = (target - pos).Rotate(-facing);
    dis = fabs(target.X());
    double y = fabs(target.Y());
    if (y < kick_area) {
      dis -= sqrt(kick_area * kick_area - y * y);
    }
    speed *= Cos(vel.Dir() - facing); //身体方向上的投影
  } else if (diffang <= oneturnang) {
    cycle += 1;
    target -= predict_pos_1;
    speed *= Cos(vel.Dir() - dir); //取得目标方向的投影
    speed *=
        decay; //进行投影.垂直方向1个周期后衰减到10+厘米了,并且在1turn时可加入考虑修正掉
    dis = target.Mod();
    dis -= kick_area;
  } else { //认为转身两下（不细致）
    cycle += 2;
    target -= predict_pos_2;
    speed *= Cos(vel.Dir() - dir); //取得目标方向的投影
    speed *=
        decay *
        decay; //进行投影.垂直方向1个周期后衰减到10+厘米了,并且在1turn时可加入考虑修正掉
    dis = target.Mod();
    dis -= kick_area;
  }

  if (dis <= 0) {
    if (buf != NULL) {
      *buf = -dis / (speed / decay);
      *buf = Min(*buf, 0.99);
    }
    return Max(cycle, 0);
  }

  // II 加速 & 消耗体力阶段
  const double stamina_used_per_cycle = inverse ? dash_max * 2.0 : dash_max;
  const int full_cyc =
      int((stamina - stamina_recovery_thr) /
          (stamina_used_per_cycle - stamina_inc_max)); //满体力阶段
  int acc_cyc = 0;                                     //加速阶段
  const double speedmax_thr = speedmax * decay * 0.98;
  const double accmax = accrate * dash_max;

  while (acc_cyc < full_cyc && speed < speedmax_thr) {
    speed += accmax;
    if (speed > speedmax) {
      speed = speedmax;
    }
    dis -= speed;
    if (dis <= 0) { //还没加速到最大就跑到了...
      cycle += acc_cyc + 1;
      if (buf != NULL) {
        *buf = -dis / (speed / decay);
        *buf = Min(*buf, 0.99);
      }
      return Max(cycle, 0);
    }
    speed *= decay;
    ++acc_cyc;
  }

  cycle += acc_cyc;

  // III 满体匀速阶段
  int aver_cyc = full_cyc - acc_cyc;
  double aver_cyc_dis = aver_cyc * speedmax;
  if (aver_cyc_dis >= dis) {
    if (buf != NULL) {
      double realcyc = cycle + dis / speedmax;
      cycle = int(ceil(realcyc));
      *buf = cycle - realcyc;
      return Max(cycle, 0);

    } else {
      cycle = int(ceil(cycle + dis / speedmax));
      return Max(cycle, 0);
    }
  } else {
    cycle += aver_cyc;
    dis -= aver_cyc_dis;
  }

  // IV 没体(0消耗)减速阶段
  double acc_tired = stamina_inc_max * accrate;
  double speed_tired = acc_tired / (1 - decay);
  double speed_tired_thr = speed_tired * decay;
  speed *= decay;
  while (dis > 0 && fabs(speed - speed_tired_thr) > 0.004) {
    speed += acc_tired;
    dis -= speed;
    speed *= decay;
    ++cycle;
  }

  if (dis <= 0) {
    if (buf != NULL) {
      *buf = -dis / (speed / decay);
      *buf = Min(*buf, 0.99);
    }
    return Max(cycle, 0);
  }

  // V 没体(0消耗)匀速阶段

  if (buf != NULL) {
    double realcyc = cycle + dis / speed_tired;
    cycle = int(ceil(realcyc));
    *buf = cycle - realcyc;
    return Max(cycle, 0);
  } else {
    cycle = cycle + int(ceil(dis / speed_tired));
    return Max(cycle, 0);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rcx, %rbx
movl %edx, -0x74(%rbp)
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
movq %rsi, %r15
movq %rsi, %rdi
callq 0xd344
movq %rax, %r13
movq %r15, %rdi
callq 0x1cd18
movq %rax, %r12
callq 0x3db2c
movq %rax, -0xf8(%rbp)
movq %r15, %rdi
callq 0xd2d2
movsd (%rax), %xmm0
mulsd 0x80(%r15), %xmm0
movsd %xmm0, -0x100(%rbp)
movsd 0x30(%r15), %xmm3
movsd 0x38(%r15), %xmm2
movsd 0x8(%r15), %xmm0
movsd 0x10(%r15), %xmm1
movsd %xmm3, -0x98(%rbp)
addsd %xmm0, %xmm3
movsd %xmm3, -0xb0(%rbp)
movsd %xmm2, -0xa0(%rbp)
addsd %xmm1, %xmm2
movsd %xmm2, -0xa8(%rbp)
movsd (%r13), %xmm2
movsd %xmm2, -0xf0(%rbp)
movsd -0x30(%rbp), %xmm2
subsd %xmm0, %xmm2
movsd -0x38(%rbp), %xmm0
subsd %xmm1, %xmm0
leaq -0xd0(%rbp), %rdi
movsd %xmm2, (%rdi)
movsd %xmm0, 0x8(%rdi)
callq 0xcd30
movsd %xmm0, -0x60(%rbp)
movsd -0x30(%rbp), %xmm0
subsd -0xb0(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
subsd -0xa8(%rbp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
cmpb $0x1, 0x175(%r15)
jne 0x1aa9c
callq 0x3db2c
movsd 0x31b0(%rax), %xmm2
jmp 0x1aab1
movq (%r15), %rax
movq %r15, %rdi
callq *0x10(%rax)
movsd (%rax), %xmm2
subsd 0x5ca67(%rip), %xmm2    # 0x77518
ucomisd -0x50(%rbp), %xmm2
jae 0x1ab12
movq %r12, -0xe0(%rbp)
movq %rbx, -0xe8(%rbp)
movsd -0xa0(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd -0x98(%rbp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
leaq 0x30(%r15), %r12
sqrtsd %xmm1, %xmm1
movsd 0xc0(%r15), %xmm0
ucomisd 0x3d77d(%rip), %xmm0    # 0x58278
movsd %xmm1, -0x68(%rbp)
movsd %xmm2, -0x58(%rbp)
jbe 0x1ab27
movsd 0xb0(%r15), %xmm1
jmp 0x1ab42
pushq $0x1
popq %rax
testq %rbx, %rbx
je 0x1b076
andq $0x0, (%rbx)
jmp 0x1b076
ucomisd 0x3d169(%rip), %xmm1    # 0x57c98
movsd -0x60(%rbp), %xmm1
jbe 0x1ab42
movq %r12, %rdi
callq 0xcd30
movapd %xmm0, %xmm1
movapd %xmm1, -0x90(%rbp)
movsd -0x60(%rbp), %xmm0
subsd %xmm1, %xmm0
movabsq $-0x3f99800000000000, %r14 # imm = 0xC066800000000000
leaq -0xd0(%rbp), %rbx
movq %r14, (%rbx)
movq %rbx, %rdi
callq 0xd305
andpd 0x3c499(%rip), %xmm0    # 0x57010
movapd %xmm0, -0xc0(%rbp)
movsd 0x158(%r15), %xmm0
movsd %xmm0, -0xd8(%rbp)
callq 0x3db2c
movsd 0x3060(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
callq 0x3db2c
movsd -0x70(%rbp), %xmm0
mulsd 0x3048(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x58(%rbp), %xmm0
divsd -0x50(%rbp), %xmm0
movsd %xmm0, (%rbx)
leaq -0xd0(%rbp), %rdi
callq 0x1cba1
movsd 0x3cb75(%rip), %xmm2    # 0x57750
maxsd %xmm0, %xmm2
movl -0x74(%rbp), %ebx
testb %bl, %bl
je 0x1ac33
movsd 0x3c47a(%rip), %xmm1    # 0x57068
movapd -0x90(%rbp), %xmm0
addsd %xmm1, %xmm0
subsd -0xc0(%rbp), %xmm1
movsd %xmm1, -0xc0(%rbp)
leaq -0xd0(%rbp), %rdi
movq %r14, (%rdi)
movsd %xmm2, -0x90(%rbp)
callq 0xd305
movsd -0x90(%rbp), %xmm2
movsd -0xc0(%rbp), %xmm1
jmp 0x1ac43
movapd -0x90(%rbp), %xmm0
movapd -0xc0(%rbp), %xmm1
ucomisd %xmm1, %xmm2
jae 0x1ad17
movsd -0xd8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x1ad9c
movsd -0x98(%rbp), %xmm1
movsd -0xf0(%rbp), %xmm2
mulsd %xmm2, %xmm1
movsd -0xa0(%rbp), %xmm0
mulsd %xmm2, %xmm0
movsd -0xb0(%rbp), %xmm2
addsd %xmm1, %xmm2
movsd -0xa8(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
subsd %xmm2, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x38(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movq %r12, %rdi
callq 0xcd30
subsd -0x60(%rbp), %xmm0
mulsd 0x3c3e0(%rip), %xmm0    # 0x570a8
divsd 0x3c398(%rip), %xmm0    # 0x57068
callq 0x9940
movsd -0x68(%rbp), %xmm9
mulsd %xmm0, %xmm9
movsd (%r13), %xmm0
mulsd %xmm0, %xmm0
mulsd %xmm0, %xmm9
movsd -0x38(%rbp), %xmm1
mulsd %xmm1, %xmm1
movsd -0x30(%rbp), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm8
subsd -0x58(%rbp), %xmm8
pushq $0x2
jmp 0x1addd
movsd -0x30(%rbp), %xmm1
subsd 0x8(%r15), %xmm1
movsd -0x38(%rbp), %xmm2
subsd 0x10(%r15), %xmm2
leaq -0xd0(%rbp), %rdi
movsd %xmm1, (%rdi)
movsd %xmm2, 0x8(%rdi)
movapd 0x3c2db(%rip), %xmm1    # 0x57020
movapd %xmm0, -0x90(%rbp)
xorpd %xmm0, %xmm1
leaq -0x108(%rbp), %rsi
movlpd %xmm1, (%rsi)
callq 0xd02c
movapd %xmm0, %xmm2
movapd 0x3c2a3(%rip), %xmm3    # 0x57010
andpd %xmm3, %xmm2
andpd %xmm1, %xmm3
movsd -0x58(%rbp), %xmm0
ucomisd %xmm3, %xmm0
jbe 0x1adf3
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1ade0
sqrtsd %xmm0, %xmm0
jmp 0x1adef
movq %r12, %rdi
callq 0xcd30
subsd -0x60(%rbp), %xmm0
mulsd 0x3c2f7(%rip), %xmm0    # 0x570a8
divsd 0x3c2af(%rip), %xmm0    # 0x57068
callq 0x9940
movsd -0x68(%rbp), %xmm9
mulsd %xmm0, %xmm9
mulsd (%r13), %xmm9
movapd -0x50(%rbp), %xmm8
subsd -0x58(%rbp), %xmm8
pushq $0x1
popq %rax
jmp 0x1ae30
movapd %xmm2, -0x50(%rbp)
callq 0x93e0
movapd -0x50(%rbp), %xmm2
subsd %xmm0, %xmm2
movapd %xmm2, -0x50(%rbp)
movq %r12, %rdi
callq 0xcd30
subsd -0x90(%rbp), %xmm0
mulsd 0x3c298(%rip), %xmm0    # 0x570a8
divsd 0x3c250(%rip), %xmm0    # 0x57068
callq 0x9940
movapd -0x50(%rbp), %xmm8
movsd -0x68(%rbp), %xmm9
mulsd %xmm0, %xmm9
xorl %eax, %eax
xorpd %xmm0, %xmm0
ucomisd %xmm8, %xmm0
movq -0xe8(%rbp), %r12
movsd -0x100(%rbp), %xmm7
jae 0x1aee9
movq -0xf8(%rbp), %rcx
movsd 0x33c0(%rcx), %xmm2
movapd %xmm2, %xmm0
testb %bl, %bl
je 0x1ae69
addsd %xmm2, %xmm0
movsd 0x60(%r15), %xmm1
movsd 0x78(%r15), %xmm4
subsd -0x70(%rbp), %xmm4
movq -0xe0(%rbp), %rcx
movsd (%rcx), %xmm3
subsd %xmm3, %xmm0
divsd %xmm0, %xmm4
cvttsd2si %xmm4, %ecx
movsd (%r13), %xmm0
movapd %xmm1, %xmm4
mulsd %xmm0, %xmm4
mulsd 0x3d3e1(%rip), %xmm4    # 0x58288
mulsd %xmm7, %xmm2
xorl %edx, %edx
xorpd %xmm5, %xmm5
movl %ecx, %esi
subl %edx, %esi
jle 0x1af0b
ucomisd %xmm9, %xmm4
jbe 0x1af0b
addsd %xmm2, %xmm9
movapd %xmm1, %xmm6
minsd %xmm9, %xmm6
subsd %xmm6, %xmm8
ucomisd %xmm8, %xmm5
jae 0x1af8c
mulsd %xmm0, %xmm6
incl %edx
movapd %xmm6, %xmm9
jmp 0x1aeb1
testq %r12, %r12
je 0x1b076
xorpd 0x3c125(%rip), %xmm8    # 0x57020
divsd (%r13), %xmm9
divsd %xmm9, %xmm8
jmp 0x1afab
cvtsi2sd %esi, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm8, %xmm2
jae 0x1afc3
addl %ecx, %eax
subsd %xmm2, %xmm8
mulsd %xmm3, %xmm7
movsd 0x3c117(%rip), %xmm2    # 0x57048
subsd %xmm0, %xmm2
movapd %xmm7, %xmm1
divsd %xmm2, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
xorpd %xmm3, %xmm3
movapd 0x3c0bf(%rip), %xmm4    # 0x57010
movsd 0x3d337(%rip), %xmm5    # 0x58290
movl %eax, %ebx
mulsd %xmm0, %xmm9
ucomisd %xmm3, %xmm8
jbe 0x1aff2
movapd %xmm9, %xmm6
subsd %xmm2, %xmm6
andpd %xmm4, %xmm6
ucomisd %xmm5, %xmm6
jbe 0x1aff2
addsd %xmm7, %xmm9
subsd %xmm9, %xmm8
incl %ebx
jmp 0x1af5b
addl %edx, %eax
incl %eax
testq %r12, %r12
je 0x1b076
xorpd 0x3c07e(%rip), %xmm8    # 0x57020
divsd %xmm0, %xmm6
divsd %xmm6, %xmm8
movsd 0x3d2cd(%rip), %xmm0    # 0x58280
minsd %xmm8, %xmm0
movsd %xmm0, (%r12)
jmp 0x1b076
addl %edx, %eax
cvtsi2sd %eax, %xmm0
divsd %xmm1, %xmm8
addsd %xmm0, %xmm8
movapd %xmm8, -0x50(%rbp)
movapd %xmm8, %xmm0
callq 0x90f0
cvttsd2si %xmm0, %ecx
testq %r12, %r12
je 0x1b03b
cvtsi2sd %ecx, %xmm0
jmp 0x1b030
xorpd %xmm2, %xmm2
ucomisd %xmm8, %xmm2
jae 0x1b044
testq %r12, %r12
je 0x1b088
cvtsi2sd %ebx, %xmm0
divsd %xmm1, %xmm8
addsd %xmm0, %xmm8
movapd %xmm8, -0x50(%rbp)
movapd %xmm8, %xmm0
callq 0x90f0
cvttsd2si %xmm0, %ecx
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd -0x50(%rbp), %xmm0
movsd %xmm0, (%r12)
xorl %eax, %eax
testl %ecx, %ecx
cmovgl %ecx, %eax
jmp 0x1b076
testq %r12, %r12
je 0x1b06f
xorpd 0x3bfce(%rip), %xmm8    # 0x57020
divsd %xmm0, %xmm9
divsd %xmm9, %xmm8
movsd 0x3d21c(%rip), %xmm0    # 0x58280
minsd %xmm8, %xmm0
movsd %xmm0, (%r12)
xorl %eax, %eax
testl %ebx, %ebx
cmovgl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
divsd %xmm1, %xmm8
movapd %xmm8, %xmm0
callq 0x90f0
cvttsd2si %xmm0, %ecx
addl %ebx, %ecx
jmp 0x1b03b
nop