// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %rcx, %r15
movl %edx, -0x54(%rbp)
movaps %xmm1, -0x70(%rbp)
movsd %xmm0, -0x30(%rbp)
movq %rsi, %r14
callq 0x43dce
movq 0x5c38(%rax), %rbx
movslq 0x114(%r14), %rax
imulq $0x3700, %rax, %r12     # imm = 0x3700
callq 0x43dce
movq 0x5c38(%rax), %r13
movslq 0x114(%r14), %rax
movq %rax, -0xd0(%rbp)
callq 0x49360
movq %rax, -0xc8(%rbp)
callq 0x43dce
movq 0x5c38(%rax), %rax
movslq 0x114(%r14), %rcx
imulq $0x3700, %rcx, %rcx     # imm = 0x3700
movsd 0x3030(%rax,%rcx), %xmm0
mulsd 0x80(%r14), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd 0x30(%r14), %xmm3
movsd 0x38(%r14), %xmm1
movsd 0x8(%r14), %xmm0
movsd 0x10(%r14), %xmm2
movsd %xmm3, -0x90(%rbp)
addsd %xmm0, %xmm3
movsd %xmm3, -0xa8(%rbp)
movsd %xmm1, -0x98(%rbp)
addsd %xmm2, %xmm1
movsd %xmm1, -0xa0(%rbp)
movsd 0x3020(%rbx,%r12), %xmm1
movsd %xmm1, -0xc0(%rbp)
movsd -0x30(%rbp), %xmm1
subsd %xmm0, %xmm1
movapd -0x70(%rbp), %xmm0
subsd %xmm2, %xmm0
movapd %xmm1, %xmm3
andpd 0x49b24(%rip), %xmm3    # 0x69010
movsd 0x49b4c(%rip), %xmm2    # 0x69040
ucomisd %xmm3, %xmm2
jbe 0x1f515
movapd %xmm0, %xmm3
andpd 0x49b0a(%rip), %xmm3    # 0x69010
xorpd %xmm4, %xmm4
movsd %xmm4, -0x50(%rbp)
ucomisd %xmm3, %xmm2
ja 0x1f52f
callq 0x90d0
mulsd 0x49b26(%rip), %xmm0    # 0x69048
divsd 0x49b26(%rip), %xmm0    # 0x69050
movsd %xmm0, -0x50(%rbp)
movsd -0x30(%rbp), %xmm0
subsd -0xa8(%rbp), %xmm0
movapd -0x70(%rbp), %xmm1
subsd -0xa0(%rbp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
movapd %xmm0, -0x40(%rbp)
cmpb $0x1, 0x175(%r14)
jne 0x1f577
callq 0x49360
movsd 0x31b0(%rax), %xmm0
jmp 0x1f58c
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movsd (%rax), %xmm0
subsd 0x64f8c(%rip), %xmm0    # 0x84518
ucomisd -0x40(%rbp), %xmm0
jae 0x1f5e6
movsd %xmm0, -0x60(%rbp)
movq %r13, -0xb8(%rbp)
movsd -0x98(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd -0x90(%rbp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
leaq 0x30(%r14), %r13
sqrtsd %xmm1, %xmm1
movsd 0xc0(%r14), %xmm0
ucomisd 0x4ace4(%rip), %xmm0    # 0x6a2b8
movsd %xmm1, -0x78(%rbp)
jbe 0x1f601
movsd 0xb0(%r14), %xmm0
jmp 0x1f618
movl $0x1, %r13d
testq %r15, %r15
je 0x1fc7e
movq $0x0, (%r15)
jmp 0x1fc7e
ucomisd 0x4a6c7(%rip), %xmm1    # 0x69cd0
movsd -0x50(%rbp), %xmm0
jbe 0x1f618
movq %r13, %rdi
callq 0xe702
movsd -0x50(%rbp), %xmm2
subsd %xmm0, %xmm2
movsd 0x49a37(%rip), %xmm1    # 0x69060
ucomisd %xmm2, %xmm1
movsd %xmm0, -0x48(%rbp)
jbe 0x1f648
movsd 0x49a34(%rip), %xmm0    # 0x69070
addsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x1f63c
jmp 0x1f66c
ucomisd 0x499f8(%rip), %xmm2    # 0x69048
jb 0x1f66c
movsd 0x49a0e(%rip), %xmm0    # 0x69068
movsd 0x499e6(%rip), %xmm1    # 0x69048
addsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x1f662
andpd 0x4999c(%rip), %xmm2    # 0x69010
movapd %xmm2, -0xf0(%rbp)
movsd 0x158(%r14), %xmm0
movsd %xmm0, -0xb0(%rbp)
callq 0x49360
movsd 0x3060(%rax), %xmm0
movsd %xmm0, -0x88(%rbp)
callq 0x49360
movsd -0x88(%rbp), %xmm0
mulsd 0x3048(%rax), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x60(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
ucomisd 0x4abef(%rip), %xmm0    # 0x6a2c0
jae 0x1f6f8
movsd 0x4abed(%rip), %xmm1    # 0x6a2c8
ucomisd %xmm0, %xmm1
jae 0x1f702
callq 0x9840
mulsd 0x4995a(%rip), %xmm0    # 0x69048
divsd 0x4995a(%rip), %xmm0    # 0x69050
jmp 0x1f70a
movsd 0x499b0(%rip), %xmm0    # 0x690b0
jmp 0x1f70a
movsd 0x499ae(%rip), %xmm0    # 0x690b8
movsd -0x48(%rbp), %xmm4
movsd 0x49949(%rip), %xmm3    # 0x69060
movsd 0x4a039(%rip), %xmm1    # 0x69758
maxsd %xmm0, %xmm1
cmpb $0x0, -0x54(%rbp)
movq %r15, -0x80(%rbp)
je 0x1f763
movsd 0x49913(%rip), %xmm2    # 0x69048
movapd %xmm2, %xmm0
subsd -0xf0(%rbp), %xmm0
addsd %xmm2, %xmm4
ucomisd %xmm4, %xmm3
jbe 0x1f90e
movsd 0x49919(%rip), %xmm2    # 0x69070
addsd %xmm2, %xmm4
ucomisd %xmm4, %xmm3
ja 0x1f757
jmp 0x1f76b
movapd -0xf0(%rbp), %xmm0
leaq (%rbx,%r12), %r15
addq $0x3020, %r15           # imm = 0x3020
ucomisd %xmm0, %xmm1
jae 0x1f84d
movsd -0xb0(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x1f933
movsd -0x90(%rbp), %xmm2
movsd -0xc0(%rbp), %xmm1
mulsd %xmm1, %xmm2
movsd -0x98(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd -0xa8(%rbp), %xmm1
addsd %xmm2, %xmm1
movsd -0xa0(%rbp), %xmm2
addsd %xmm0, %xmm2
movsd -0x30(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movapd -0x70(%rbp), %xmm0
subsd %xmm2, %xmm0
movapd %xmm0, -0x70(%rbp)
movq %r13, %rdi
callq 0xe702
subsd -0x50(%rbp), %xmm0
mulsd 0x49855(%rip), %xmm0    # 0x69050
divsd 0x49845(%rip), %xmm0    # 0x69048
callq 0x9950
movsd -0x78(%rbp), %xmm9
mulsd %xmm0, %xmm9
movsd (%r15), %xmm0
mulsd %xmm0, %xmm0
mulsd %xmm0, %xmm9
movapd -0x70(%rbp), %xmm1
mulsd %xmm1, %xmm1
movsd -0x30(%rbp), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm8
subsd -0x60(%rbp), %xmm8
movl $0x2, %r13d
jmp 0x1f9c7
movsd -0x30(%rbp), %xmm0
subsd 0x8(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
movapd -0x70(%rbp), %xmm0
subsd 0x10(%r14), %xmm0
movapd %xmm0, -0x70(%rbp)
movsd 0x4982b(%rip), %xmm0    # 0x690a0
movsd %xmm4, -0x48(%rbp)
mulsd %xmm4, %xmm0
divsd 0x497c2(%rip), %xmm0    # 0x69048
leaq -0x100(%rbp), %r12
leaq -0xf8(%rbp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x92d0
movsd (%r12), %xmm0
movsd (%rbx), %xmm1
movsd -0x30(%rbp), %xmm3
movapd %xmm3, %xmm2
mulsd %xmm0, %xmm3
movapd %xmm3, %xmm4
movapd -0x70(%rbp), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm1, %xmm2
subsd %xmm0, %xmm2
mulsd %xmm1, %xmm3
addsd %xmm4, %xmm3
movapd 0x49735(%rip), %xmm1    # 0x69010
andpd %xmm1, %xmm2
andpd %xmm3, %xmm1
movsd -0x60(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1f98c
mulsd %xmm3, %xmm3
mulsd %xmm0, %xmm0
subsd %xmm3, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1f979
sqrtsd %xmm0, %xmm0
jmp 0x1f988
ucomisd 0x49732(%rip), %xmm4    # 0x69048
jb 0x1f76b
movsd 0x49744(%rip), %xmm3    # 0x69068
addsd %xmm3, %xmm4
ucomisd %xmm2, %xmm4
jae 0x1f924
jmp 0x1f76b
movq %r13, %rdi
callq 0xe702
subsd -0x50(%rbp), %xmm0
mulsd 0x49708(%rip), %xmm0    # 0x69050
divsd 0x496f8(%rip), %xmm0    # 0x69048
callq 0x9950
movapd -0x40(%rbp), %xmm8
movsd -0x78(%rbp), %xmm9
mulsd %xmm0, %xmm9
mulsd (%r15), %xmm9
subsd -0x60(%rbp), %xmm8
movl $0x1, %r13d
jmp 0x1f9c7
movapd %xmm2, -0x40(%rbp)
callq 0x93c0
movapd -0x40(%rbp), %xmm2
subsd %xmm0, %xmm2
movapd %xmm2, -0x40(%rbp)
movq %r13, %rdi
callq 0xe702
subsd -0x48(%rbp), %xmm0
mulsd 0x496aa(%rip), %xmm0    # 0x69050
divsd 0x4969a(%rip), %xmm0    # 0x69048
callq 0x9950
movapd -0x40(%rbp), %xmm8
movsd -0x78(%rbp), %xmm9
mulsd %xmm0, %xmm9
xorl %r13d, %r13d
xorpd %xmm0, %xmm0
ucomisd %xmm8, %xmm0
movsd -0xd8(%rbp), %xmm7
jae 0x1fa10
imulq $0x3700, -0xd0(%rbp), %rax # imm = 0x3700
movq -0xb8(%rbp), %rcx
addq %rcx, %rax
addq $0x3018, %rax           # imm = 0x3018
movq -0xc8(%rbp), %rcx
movsd 0x33c0(%rcx), %xmm3
cmpb $0x0, -0x54(%rbp)
jne 0x1fa46
movapd %xmm3, %xmm0
jmp 0x1fa4e
movq -0x80(%rbp), %rax
testq %rax, %rax
je 0x1fc7e
xorpd 0x495fa(%rip), %xmm8    # 0x69020
divsd (%r15), %xmm9
divsd %xmm9, %xmm8
movsd 0x4a898(%rip), %xmm0    # 0x6a2d0
minsd %xmm8, %xmm0
movsd %xmm0, (%rax)
jmp 0x1fc7e
movapd %xmm3, %xmm0
addsd %xmm3, %xmm0
movsd 0x78(%r14), %xmm2
subsd -0x88(%rbp), %xmm2
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
divsd %xmm0, %xmm2
cvttsd2si %xmm2, %eax
movsd 0x60(%r14), %xmm2
movsd (%r15), %xmm0
xorl %ecx, %ecx
testl %eax, %eax
jle 0x1fb16
movapd %xmm2, %xmm4
mulsd %xmm0, %xmm4
mulsd 0x4a847(%rip), %xmm4    # 0x6a2d8
ucomisd %xmm9, %xmm4
movq -0x80(%rbp), %rbx
jbe 0x1fb1a
mulsd %xmm7, %xmm3
movl $0x1, %edx
xorpd %xmm5, %xmm5
movapd %xmm9, %xmm6
addsd %xmm3, %xmm6
movapd %xmm2, %xmm9
minsd %xmm6, %xmm9
subsd %xmm9, %xmm8
ucomisd %xmm8, %xmm5
jae 0x1fae1
mulsd %xmm0, %xmm9
leal 0x1(%rdx), %ecx
cmpl %eax, %edx
jge 0x1fadd
ucomisd %xmm9, %xmm4
movl %ecx, %edx
ja 0x1faa9
decl %ecx
jmp 0x1fb1a
addl %edx, %r13d
testq %rbx, %rbx
je 0x1fc7e
xorpd 0x4952a(%rip), %xmm8    # 0x69020
divsd %xmm0, %xmm9
divsd %xmm9, %xmm8
movsd 0x4a7c8(%rip), %xmm0    # 0x6a2d0
minsd %xmm8, %xmm0
movsd %xmm0, (%rbx)
jmp 0x1fc7e
movq -0x80(%rbp), %rbx
movl %eax, %edx
subl %ecx, %edx
xorps %xmm3, %xmm3
cvtsi2sd %edx, %xmm3
mulsd %xmm2, %xmm3
ucomisd %xmm8, %xmm3
jae 0x1fbec
addl %eax, %r13d
subsd %xmm3, %xmm8
mulsd %xmm1, %xmm7
movsd 0x49510(%rip), %xmm2    # 0x69058
subsd %xmm0, %xmm2
movapd %xmm7, %xmm1
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm9
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm8
jbe 0x1fba8
movapd %xmm0, %xmm3
mulsd %xmm1, %xmm3
movapd 0x4949c(%rip), %xmm4    # 0x69010
movsd 0x4a764(%rip), %xmm5    # 0x6a2e0
movapd %xmm9, %xmm6
subsd %xmm3, %xmm6
andpd %xmm4, %xmm6
ucomisd %xmm5, %xmm6
jbe 0x1fba8
addsd %xmm7, %xmm9
subsd %xmm9, %xmm8
incl %r13d
mulsd %xmm0, %xmm9
ucomisd %xmm2, %xmm8
ja 0x1fb7c
ucomisd %xmm8, %xmm2
jae 0x1fc34
testq %rbx, %rbx
je 0x1fc5f
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
divsd %xmm1, %xmm8
addsd %xmm0, %xmm8
movapd %xmm8, -0x40(%rbp)
movapd %xmm8, %xmm0
callq 0x9100
cvttsd2si %xmm0, %eax
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
jmp 0x1fc20
addl %r13d, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd %xmm2, %xmm8
addsd %xmm0, %xmm8
movapd %xmm8, -0x40(%rbp)
movapd %xmm8, %xmm0
callq 0x9100
cvttsd2si %xmm0, %eax
testq %rbx, %rbx
je 0x1fc29
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
subsd -0x40(%rbp), %xmm0
movsd %xmm0, (%rbx)
xorl %r13d, %r13d
testl %eax, %eax
cmovgl %eax, %r13d
jmp 0x1fc7e
testq %rbx, %rbx
je 0x1fc75
xorpd 0x493de(%rip), %xmm8    # 0x69020
divsd %xmm0, %xmm9
divsd %xmm9, %xmm8
movsd 0x4a67c(%rip), %xmm0    # 0x6a2d0
minsd %xmm8, %xmm0
movsd %xmm0, (%rbx)
jmp 0x1fc75
divsd %xmm1, %xmm8
movapd %xmm8, %xmm0
callq 0x9100
cvttsd2si %xmm0, %eax
addl %eax, %r13d
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
movl %r13d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop