// File: /workspace/github_cpp_makefile_star2_O2/wrighteagle2d[P]wrighteaglebase/Debug/../src/Dasher.cpp
// Function: Dasher::CyclePredictedToPoint(PlayerState const&, Vector, double)
int Dasher::CyclePredictedToPoint(const PlayerState &player, Vector target,
                                  double dash_power) {
  double corrected_dash_power = dash_power;
  double effective_power;
  double predicted_stamina = player.GetStamina();
  double predicted_effort = player.GetEffort();
  double predicted_recovery = player.GetRecovery();
  double predicted_capacity = player.GetCapacity();
  double myang = player.GetBodyDir();
  Vector position = player.GetPos();
  Vector velocity;

  if (player.GetVelConf() < FLOAT_EPS) {
    velocity = Vector(0, 0);
  } else {
    velocity = player.GetVel();
  }

  Vector last_position = position;
  int max_cyc =
      (int)(position.Dist(target) / player.GetEffectiveSpeedMax() + 100);
  int last_action = 0; /* 0 -- turn , 1 -- dash*/

  for (int i = 0; i < max_cyc; i++) {
    if (position.Dist(target) < PlayerParam::instance().AtPointBuffer()) {
      return i;
    }

    if (position.Dist(target) > last_position.Dist(target) + FLOAT_EPS &&
        last_action == 1) {
      return i;
    }

    last_position = position;

    /*decide if we should turn*/
    double targ_ang;
    if (dash_power > 0) {
      targ_ang = (target - position).Dir() - myang;
    } else {
      targ_ang = (target - position).Dir() - GetNormalizeAngleDeg(myang + 180);
    }

    double max_go_to_point_angle_err = 4; //见08CP_max_go_to_point_angle_err
    if (fabs(GetNormalizeAngleDeg(targ_ang)) > max_go_to_point_angle_err) {
      /*turnint*/
      double this_turn =
          MinMax(-player.GetMaxTurnAngle(), GetNormalizeAngleDeg(targ_ang),
                 player.GetMaxTurnAngle());

      myang += this_turn;
      corrected_dash_power = 0;
      last_action = 0;
    } else {
      corrected_dash_power = player.CorrectDashPowerForStamina(dash_power);
      if (fabs(corrected_dash_power) > predicted_stamina) {
        effective_power = Sign(corrected_dash_power) * predicted_stamina;
      } else {
        effective_power = corrected_dash_power;
      }

      effective_power *= predicted_effort;
      effective_power *= player.GetDashPowerRate();
      velocity += Polar2Vector(effective_power, myang);
      last_action = 1;
    }

    if (velocity.Mod() > player.GetEffectiveSpeedMax()) {
      velocity *= (player.GetEffectiveSpeedMax() / velocity.Mod());
    }

    position += velocity;
    velocity *= player.GetPlayerDecay();

    // 08 内UpdatePredictStaminaWithDash函数 直接写入此函数
    if (dash_power > 0) {
      predicted_stamina -= dash_power;
    } else {
      predicted_stamina -= 2 * dash_power;
    }

    if (predicted_stamina < 0) {
      predicted_stamina = 0;
    }

    if (predicted_stamina < ServerParam::instance().recoverDecStamina() &&
        predicted_recovery > ServerParam::instance().recoverMin()) {
      predicted_recovery -= ServerParam::instance().recoverDec();
    }

    if (predicted_stamina < ServerParam::instance().effortDecStamina() &&
        predicted_effort > player.GetEffortMin()) {
      predicted_effort -= ServerParam::instance().effortDec();
    }

    if (predicted_stamina > ServerParam::instance().effortIncStamina() &&
        predicted_effort < player.GetEffortMax()) {
      predicted_effort += ServerParam::instance().effortDec();
    }

    //增加capacity改进
    double stamina_inc =
        Min(predicted_recovery * player.GetStaminaIncMax(), predicted_capacity);
    predicted_stamina += stamina_inc;
    if (predicted_stamina > ServerParam::instance().staminaMax()) {
      predicted_stamina = ServerParam::instance().staminaMax();
    }

    predicted_capacity -= stamina_inc;
    if (predicted_capacity < 0) {
      predicted_capacity = 0;
    }
  }

  return max_cyc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movaps %xmm2, -0x100(%rbp)
movq %rsi, %rbx
movsd 0x78(%rsi), %xmm2
movaps %xmm2, -0x60(%rbp)
movsd 0x80(%rsi), %xmm4
movsd 0x90(%rsi), %xmm5
movsd 0x88(%rsi), %xmm2
movsd %xmm2, -0x78(%rbp)
movsd 0xb0(%rsi), %xmm2
movsd %xmm2, -0x48(%rbp)
movsd 0x8(%rsi), %xmm9
movsd 0x10(%rsi), %xmm10
movsd 0x48(%rsi), %xmm6
movsd 0x3aab0(%rip), %xmm2    # 0x57040
ucomisd %xmm6, %xmm2
cmpltsd %xmm2, %xmm6
movsd 0x30(%rsi), %xmm2
andnpd %xmm2, %xmm6
xorpd %xmm7, %xmm7
ja 0x1c5ad
movsd 0x38(%rbx), %xmm7
movapd %xmm9, %xmm3
subsd %xmm0, %xmm3
movapd %xmm10, %xmm2
subsd %xmm1, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm3, %xmm3
addsd %xmm2, %xmm3
sqrtsd %xmm3, %xmm2
divsd 0x60(%rbx), %xmm2
addsd 0x3b12c(%rip), %xmm2    # 0x57708
cvttsd2si %xmm2, %eax
xorl %r12d, %r12d
testl %eax, %eax
movl $0x0, %r14d
movl %eax, -0x34(%rbp)
cmovgl %eax, %r14d
leaq -0x90(%rbp), %r15
movapd %xmm9, %xmm3
movapd %xmm10, %xmm8
xorl %r13d, %r13d
movsd %xmm1, -0xc0(%rbp)
movsd %xmm0, -0xb8(%rbp)
cmpl %r13d, %r14d
je 0x1cb34
movsd %xmm10, -0xf0(%rbp)
movsd %xmm9, -0xa0(%rbp)
movapd %xmm7, -0xe0(%rbp)
movapd %xmm6, -0xb0(%rbp)
movsd %xmm5, -0x40(%rbp)
movsd %xmm4, -0x30(%rbp)
movsd %xmm3, -0xc8(%rbp)
subsd %xmm0, %xmm3
movsd %xmm8, -0xd0(%rbp)
movapd %xmm8, %xmm2
subsd %xmm1, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm3, %xmm3
addsd %xmm2, %xmm3
sqrtsd %xmm3, %xmm2
movsd %xmm2, -0x70(%rbp)
callq 0x38d94
movsd -0x70(%rbp), %xmm3
movsd 0x5c68(%rax), %xmm0
ucomisd %xmm3, %xmm0
ja 0x1cb31
movsd -0xa0(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm4
subsd %xmm4, %xmm0
movsd -0xf0(%rbp), %xmm1
movsd -0xc0(%rbp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm0
addsd 0x3a966(%rip), %xmm0    # 0x57040
cmpl $0x1, %r12d
movapd %xmm2, %xmm1
movapd %xmm4, %xmm2
jne 0x1c6f2
ucomisd %xmm0, %xmm3
ja 0x1cb31
movapd -0x100(%rbp), %xmm0
ucomisd 0x3bbde(%rip), %xmm0    # 0x582e0
movapd %xmm2, %xmm0
subsd -0xc8(%rbp), %xmm0
subsd -0xd0(%rbp), %xmm1
jbe 0x1c741
movsd %xmm0, -0x90(%rbp)
movsd %xmm1, -0x88(%rbp)
movq %r15, %rdi
callq 0xcd30
subsd -0x48(%rbp), %xmm0
movabsq $-0x3f99800000000000, %r12 # imm = 0xC066800000000000
jmp 0x1c795
movsd %xmm0, -0x90(%rbp)
movsd %xmm1, -0x88(%rbp)
movq %r15, %rdi
callq 0xcd30
movsd %xmm0, -0x70(%rbp)
movsd -0x48(%rbp), %xmm0
addsd 0x3a8fd(%rip), %xmm0    # 0x57068
movabsq $-0x3f99800000000000, %r12 # imm = 0xC066800000000000
movq %r12, -0x118(%rbp)
leaq -0x118(%rbp), %rdi
callq 0xd305
movsd -0x70(%rbp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movq %r12, -0x90(%rbp)
movsd %xmm0, -0x70(%rbp)
movq %r15, %rdi
callq 0xd305
movapd 0x3a85f(%rip), %xmm1    # 0x57010
andpd %xmm1, %xmm0
ucomisd 0x3bb1b(%rip), %xmm0    # 0x582d8
jbe 0x1c81f
movsd 0x158(%rbx), %xmm0
xorps 0x3a852(%rip), %xmm0    # 0x57020
movaps %xmm0, -0xa0(%rbp)
movq %r12, -0x90(%rbp)
movsd -0x70(%rbp), %xmm0
movq %r15, %rdi
callq 0xd305
movsd 0x158(%rbx), %xmm1
maxsd -0xa0(%rbp), %xmm0
minsd %xmm0, %xmm1
addsd -0x48(%rbp), %xmm1
movsd %xmm1, -0x48(%rbp)
xorl %r12d, %r12d
movapd -0xb0(%rbp), %xmm2
movapd -0xe0(%rbp), %xmm3
jmp 0x1c8c7
movq %rbx, %rdi
movapd -0x100(%rbp), %xmm0
callq 0x3a8ce
movapd %xmm0, %xmm1
andpd 0x3a7d5(%rip), %xmm1    # 0x57010
movapd -0x60(%rbp), %xmm5
movapd %xmm5, %xmm2
xorpd 0x3a7d4(%rip), %xmm2    # 0x57020
xorpd %xmm3, %xmm3
cmplesd %xmm0, %xmm3
movapd %xmm3, %xmm4
andpd %xmm5, %xmm4
andnpd %xmm2, %xmm3
orpd %xmm4, %xmm3
movapd %xmm5, %xmm2
cmpltsd %xmm1, %xmm2
movapd %xmm2, %xmm1
andnpd %xmm0, %xmm1
andpd %xmm3, %xmm2
orpd %xmm1, %xmm2
mulsd -0x30(%rbp), %xmm2
movsd %xmm2, -0x80(%rbp)
movq %rbx, %rdi
callq 0xd2d2
movsd (%rax), %xmm0
mulsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0xd15b
movapd -0xb0(%rbp), %xmm2
addsd %xmm0, %xmm2
movapd -0xe0(%rbp), %xmm3
addsd %xmm1, %xmm3
pushq $0x1
popq %r12
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
sqrtsd %xmm1, %xmm1
movsd 0x60(%rbx), %xmm0
ucomisd %xmm0, %xmm1
movapd %xmm0, -0x70(%rbp)
movsd %xmm1, -0x110(%rbp)
divsd %xmm1, %xmm0
movapd %xmm3, -0xe0(%rbp)
movapd %xmm0, -0xf0(%rbp)
jbe 0x1c90f
mulsd %xmm0, %xmm2
movapd %xmm2, -0xb0(%rbp)
movq %rbx, %rdi
callq 0xd344
movsd (%rax), %xmm0
movsd %xmm0, -0xa0(%rbp)
movapd -0x100(%rbp), %xmm3
movapd %xmm3, %xmm0
addsd %xmm3, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm3, %xmm1
movapd %xmm1, %xmm2
andpd %xmm3, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movapd -0x60(%rbp), %xmm0
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd %xmm1, -0x60(%rbp)
callq 0x3db2c
movsd 0x3438(%rax), %xmm0
ucomisd -0x60(%rbp), %xmm0
jbe 0x1c9a9
callq 0x3db2c
movsd -0x40(%rbp), %xmm0
ucomisd 0x3068(%rax), %xmm0
jbe 0x1c9a9
callq 0x3db2c
movsd -0x40(%rbp), %xmm0
subsd 0x3070(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
callq 0x3db2c
movsd 0x3430(%rax), %xmm0
ucomisd -0x60(%rbp), %xmm0
jbe 0x1c9e7
movq %rbx, %rdi
callq 0x1cd6e
movsd -0x30(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x1c9e7
callq 0x3db2c
movsd -0x30(%rbp), %xmm0
subsd 0x3090(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x3db2c
movsd -0x60(%rbp), %xmm0
ucomisd 0x3440(%rax), %xmm0
jbe 0x1ca2e
movq %rbx, %rdi
callq 0x1cda2
movsd (%rax), %xmm0
movsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1ca2e
movsd %xmm1, -0x30(%rbp)
callq 0x3db2c
movsd -0x30(%rbp), %xmm0
addsd 0x3090(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x1cd18
movsd (%rax), %xmm0
mulsd -0x40(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, -0x108(%rbp)
movsd -0x60(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0x3db2c
movsd -0x60(%rbp), %xmm4
ucomisd 0x3048(%rax), %xmm4
jbe 0x1ca7f
callq 0x3db2c
movsd 0x3048(%rax), %xmm4
movapd -0xe0(%rbp), %xmm0
movapd -0xf0(%rbp), %xmm1
mulsd %xmm0, %xmm1
movapd -0x70(%rbp), %xmm7
cmpltsd -0x110(%rbp), %xmm7
andpd %xmm7, %xmm1
andnpd %xmm0, %xmm7
orpd %xmm1, %xmm7
movsd -0xc8(%rbp), %xmm0
movapd %xmm0, %xmm9
movapd -0xb0(%rbp), %xmm6
addsd %xmm6, %xmm0
movsd -0xd0(%rbp), %xmm1
movapd %xmm1, %xmm10
addsd %xmm7, %xmm1
movsd -0xa0(%rbp), %xmm2
mulsd %xmm2, %xmm6
mulsd %xmm2, %xmm7
movsd -0x78(%rbp), %xmm3
subsd -0x108(%rbp), %xmm3
xorpd %xmm2, %xmm2
maxsd %xmm3, %xmm2
incl %r13d
movapd %xmm0, %xmm3
movapd %xmm1, %xmm8
movsd %xmm2, -0x78(%rbp)
movapd %xmm4, -0x60(%rbp)
movsd -0xc0(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm0
movsd -0x30(%rbp), %xmm4
movsd -0x40(%rbp), %xmm5
jmp 0x1c616
movl %r13d, %r14d
movl -0x34(%rbp), %eax
cmpl %eax, %r14d
cmovll %r14d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq