// File: /workspace/github_cpp_makefile_star2_O2/wrighteagle2d[P]wrighteaglebase/Debug/../src/Dasher.cpp
// Function: Dasher::TurnDashPlaning(Agent&, AtomicAction&, Vector, double, double, bool, bool)
void Dasher::TurnDashPlaning(Agent &agent, AtomicAction &act, Vector target,
                             double buffer, double power, bool inverse,
                             bool turn_first) {
  act.Clear();

  power = inverse ? -fabs(power) : fabs(power);
  power = agent.GetSelf().CorrectDashPowerForStamina(power);

  PlayerState &self = agent.Self();
  const Vector &my_pre_pos = self.GetPredictedPos(1);
  double target_dist = my_pre_pos.Dist(target);
  AngleDeg target_ang =
      inverse
          ? GetNormalizeAngleDeg((my_pre_pos - target).Dir() -
                                 agent.GetSelf().GetBodyDir())
          : GetNormalizeAngleDeg((target - my_pre_pos).Dir() -
                                 agent.GetSelf().GetBodyDir()); //需要转身的角度

  // DT_none
  //处理转身问题,可能1dash + 1turn更好
  double bufang;
  if (buffer > FLOAT_EPS) {
    bufang = ASin(buffer / target_dist / 2) * 2;
    bufang = Max(10.0, bufang);
  } else {
    bufang = 0.0;
  }

  const double &effort = self.GetEffort();
  const double &accrate = self.GetDashPowerRate();
  const double &inertia_moment = self.GetInertiaMoment();
  const double &decay = self.GetPlayerDecay();
  const double &speedmax = self.GetEffectiveSpeedMax();
  const Vector &vel = self.GetVel();

  const double &facing = self.GetBodyDir();
  double speed = vel.Mod();

  double diffang = fabs(target_ang);
  double oneturnang = self.GetMaxTurnAngle();
  if (buffer < FLOAT_EPS && target_dist > 2.6) {
    oneturnang += 2.6;
  }

  if (turn_first || diffang <= oneturnang ||
      speed <
          0.04) { //由于噪声的存在，在非身体方向也有速度，调也调不过来，太小时不如直接转,0.04是一般队员的误差极值
    act.mType = CT_Turn;
    act.mTurnAngle = target_ang;
    act.mSucceed = true;
    return;
  } else {
    //计算1 dash + 1turn;
    //下面都是算dash到什么速度最好
    double mspd1 = (180.0 / (diffang + 10) - 1.0) /
                   inertia_moment; // 10是buf，不然容易调整后正好还转不过来
    double dash2spd = Min(mspd1 / decay, speedmax) /
                      (1 + ServerParam::instance().playerRand());

    if (fabs(GetNormalizeAngleDeg(vel.Dir() - facing)) > 90.0) {
      speed = -speed;
    }

    double spd_inf = Max(
        speed - accrate * ServerParam::instance().maxDashPower(), -dash2spd);
    double spd_sup =
        Min(speed + accrate * ServerParam::instance().maxDashPower(), dash2spd);
    Ray cur_r(self.GetPos(), facing);
    Vector pt_inf = cur_r.GetPoint(spd_inf * (1.0 + decay));
    Vector pt_sup = cur_r.GetPoint(spd_sup * (1.0 + decay));
    Vector pt;     //要取得点
    bool bnegtive; //要跑的点是不是在身后
    Line pdl = Line(cur_r).GetPerpendicular(target); //垂线
    if (pdl.IsPointInSameSide(pt_inf, pt_sup)) {
      if (fabs(GetNormalizeAngleDeg(facing - target_ang)) < 90.0) {
        pt = pt_sup;
        bnegtive = (spd_sup < 0);
      } else {
        pt = pt_inf;
        bnegtive = (spd_inf < 0);
      }
    } else {
      double dist;
      bnegtive = !cur_r.Intersection(pdl, dist); //垂点
      pt = cur_r.GetPoint(dist);
    }
    double dis =
        pt.Dist(target); //两个周期后离point的距离,用来compare with 2 turn
    double twoturnang =
        oneturnang + 180.0 / (1.0 + inertia_moment * fabs(speed) * decay);
    if (twoturnang > diffang) {
      const Vector &pt2 = self.GetPredictedPos(2);
      double dis2 = pt2.Dist(target);
      if (dis2 < dis || diffang > 102.6) { //一次转太大，误差也大不如直接2turn
        act.mType = CT_Turn;
        act.mTurnAngle = target_ang;
        act.mSucceed = true;
        return;
      }
    }
    double spd_need;
    if (bnegtive) {
      spd_need = -(pt - self.GetPos()).Mod() / (1.0 + decay);
    } else {
      spd_need = (pt - self.GetPos()).Mod() / (1.0 + decay);
    }
    //会总是dash而dash后又转不过来...,看着就像不拿球
    double turnang_after_dash =
        180.0 / (1.0 + inertia_moment * fabs(spd_need) * decay) - 5.0;
    if (GetAngleDegDiffer((target - pt).Dir(), facing) > turnang_after_dash) {
      act.mType = CT_Turn;
      act.mTurnAngle = target_ang;
      act.mSucceed = true;
      return;
    }
    double power = (spd_need - speed) / accrate / effort;

    power = MinMax(-ServerParam::instance().maxDashPower(), power,
                   ServerParam::instance().maxDashPower());

    // I over
    act.mType = CT_Dash;
    act.mDashPower = AdjustPowerForDash(self, pt, FLOAT_EPS, power);
    act.mDashPower = agent.GetSelf().CorrectDashPowerForStamina(act.mDashPower);
    if (fabs(act.mDashPower) > FLOAT_EPS) {
      act.mSucceed = true;
    } else {
      act.mType = CT_Turn; // power为零时就不要执行了，没意义
      act.mTurnAngle = 0.0;
      act.mSucceed = true;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %r8d, -0x70(%rbp)
movl %ecx, %r12d
movaps %xmm2, -0x80(%rbp)
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x58(%rbp)
movq %rdx, %rbx
movq %rsi, %r14
movb $0x0, (%rdx)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rdx)
movups %xmm0, 0x14(%rdx)
movups %xmm0, 0x20(%rdx)
testl %ecx, %ecx
jne 0x1b36e
andps 0x3bca4(%rip), %xmm3    # 0x57010
jmp 0x1b375
orps 0x3bcab(%rip), %xmm3    # 0x57020
movaps %xmm3, -0x50(%rbp)
movq %r14, %rdi
callq 0xcd42
movq %rax, %rdi
movaps -0x50(%rbp), %xmm0
callq 0x3a8ce
movq %r14, %rdi
callq 0x10d5e
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0xd4c0
movsd (%rax), %xmm1
movsd 0x8(%rax), %xmm0
movapd %xmm1, %xmm2
movsd -0x58(%rbp), %xmm6
subsd %xmm6, %xmm2
movapd %xmm0, %xmm3
movsd -0x38(%rbp), %xmm5
subsd %xmm5, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
movapd %xmm2, %xmm7
mulsd %xmm2, %xmm7
addsd %xmm4, %xmm7
movsd %xmm7, -0x90(%rbp)
movabsq $-0x3f99800000000000, %r13 # imm = 0xC066800000000000
testl %r12d, %r12d
je 0x1b403
leaq -0xf0(%rbp), %rdi
movsd %xmm2, (%rdi)
movsd %xmm3, 0x8(%rdi)
jmp 0x1b423
movapd %xmm6, %xmm2
subsd %xmm1, %xmm2
movapd %xmm5, %xmm1
subsd %xmm0, %xmm1
leaq -0xf0(%rbp), %rdi
movsd %xmm2, (%rdi)
movsd %xmm1, 0x8(%rdi)
callq 0xcd30
movsd %xmm0, -0x50(%rbp)
movq %r14, %rdi
callq 0xcd42
movsd -0x50(%rbp), %xmm0
subsd 0xb0(%rax), %xmm0
leaq -0x108(%rbp), %rdi
movq %r13, (%rdi)
callq 0xd305
movapd %xmm0, -0x50(%rbp)
sqrtsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movapd -0x80(%rbp), %xmm0
ucomisd 0x3bbcd(%rip), %xmm0    # 0x57040
jbe 0x1b49a
movapd -0x80(%rbp), %xmm0
divsd -0x90(%rbp), %xmm0
mulsd 0x3c3be(%rip), %xmm0    # 0x57848
leaq -0xf0(%rbp), %rdi
movsd %xmm0, (%rdi)
callq 0x1cba1
movq %r15, %rdi
callq 0xd2d2
movq %rax, %r12
movq %r15, %rdi
callq 0x1cbea
movq %rax, -0xc0(%rbp)
movq %r15, %rdi
callq 0xd344
cmpb $0x0, -0x70(%rbp)
je 0x1b4e8
movl $0x1, 0x4(%rbx)
movapd -0x50(%rbp), %xmm0
movsd %xmm0, 0x18(%rbx)
movb $0x1, (%rbx)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movapd 0x3bb20(%rip), %xmm3    # 0x57010
andpd -0x50(%rbp), %xmm3
movsd 0x158(%r15), %xmm0
movsd 0x3cdaa(%rip), %xmm1    # 0x582b0
movapd %xmm0, %xmm2
addsd %xmm1, %xmm2
cmpltsd -0x90(%rbp), %xmm1
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movapd -0x80(%rbp), %xmm2
cmpltsd 0x3bb0f(%rip), %xmm2    # 0x57040
andpd %xmm2, %xmm1
andnpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movapd %xmm2, -0x80(%rbp)
ucomisd %xmm3, %xmm2
jae 0x1b4c2
movsd 0x30(%r15), %xmm0
movsd 0x38(%r15), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
sqrtsd %xmm0, %xmm1
movsd 0x3cd48(%rip), %xmm0    # 0x582b8
ucomisd %xmm1, %xmm0
ja 0x1b4c2
movq %rax, %r13
movq %r14, -0xb0(%rbp)
leaq 0x30(%r15), %r14
movq %r15, %rax
addq $0xb0, %rax
movq %rax, -0xb8(%rbp)
movsd 0x3c188(%rip), %xmm0    # 0x57728
movapd %xmm3, -0xd0(%rbp)
addsd %xmm3, %xmm0
movapd %xmm1, -0xa0(%rbp)
movsd 0x3baac(%rip), %xmm1    # 0x57068
divsd %xmm0, %xmm1
addsd 0x3baa8(%rip), %xmm1    # 0x57070
movq -0xc0(%rbp), %rax
divsd (%rax), %xmm1
divsd (%r13), %xmm1
movsd 0x60(%r15), %xmm0
minsd %xmm1, %xmm0
movapd %xmm0, -0x70(%rbp)
callq 0x3db2c
movsd 0x3028(%rax), %xmm0
addsd 0x3ba4b(%rip), %xmm0    # 0x57048
movapd -0x70(%rbp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, -0x70(%rbp)
movq %r14, %rdi
callq 0xcd30
subsd 0xb0(%r15), %xmm0
leaq -0xf0(%rbp), %r14
movabsq $-0x3f99800000000000, %rax # imm = 0xC066800000000000
movq %rax, (%r14)
movq %r14, %rdi
callq 0xd305
andpd 0x3b9d0(%rip), %xmm0    # 0x57010
movapd -0xa0(%rbp), %xmm3
movapd %xmm3, %xmm1
xorpd 0x3b9cc(%rip), %xmm1    # 0x57020
movsd 0x3ba2c(%rip), %xmm2    # 0x57088
cmpltsd %xmm0, %xmm2
andpd %xmm2, %xmm1
andnpd %xmm3, %xmm2
orpd %xmm1, %xmm2
movapd %xmm2, -0x90(%rbp)
movsd (%r12), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x3db2c
movsd -0x30(%rbp), %xmm1
mulsd 0x33c0(%rax), %xmm1
movapd -0x90(%rbp), %xmm0
subsd %xmm1, %xmm0
movapd -0x70(%rbp), %xmm1
xorpd 0x3b975(%rip), %xmm1    # 0x57020
maxsd %xmm0, %xmm1
movapd %xmm1, -0x130(%rbp)
movq %r12, -0xa8(%rbp)
movsd (%r12), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0x3db2c
movsd -0x30(%rbp), %xmm0
mulsd 0x33c0(%rax), %xmm0
addsd -0x90(%rbp), %xmm0
movapd %xmm0, %xmm1
movapd -0x70(%rbp), %xmm0
minsd %xmm1, %xmm0
movapd %xmm0, -0x70(%rbp)
movups 0x8(%r15), %xmm0
movaps %xmm0, (%r14)
movsd 0xb0(%r15), %xmm0
movsd %xmm0, 0x10(%r14)
movsd (%r13), %xmm0
addsd 0x3b92d(%rip), %xmm0    # 0x57048
mulsd -0x130(%rbp), %xmm0
leaq -0x108(%rbp), %r12
movsd %xmm0, (%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x1cc1e
leaq -0x118(%rbp), %rax
movsd %xmm0, (%rax)
movsd %xmm1, 0x8(%rax)
movq %r13, -0x30(%rbp)
movsd (%r13), %xmm0
addsd 0x3b8eb(%rip), %xmm0    # 0x57048
mulsd -0x70(%rbp), %xmm0
movsd %xmm0, (%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x1cc1e
leaq -0x140(%rbp), %r13
movsd %xmm0, (%r13)
movsd %xmm1, 0x8(%r13)
leaq -0x158(%rbp), %r14
movq %r14, %rdi
leaq -0xf0(%rbp), %rsi
callq 0x1cc46
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movapd 0x3b871(%rip), %xmm2    # 0x57020
xorpd %xmm0, %xmm2
movsd %xmm1, (%r12)
mulsd -0x58(%rbp), %xmm1
mulsd -0x38(%rbp), %xmm0
subsd %xmm1, %xmm0
movlpd %xmm2, 0x8(%r12)
movsd %xmm0, 0x10(%r12)
leaq -0x108(%rbp), %rdi
leaq -0x118(%rbp), %rsi
movq %r13, %rdx
callq 0x12f1c
testb %al, %al
je 0x1b849
movq -0xb8(%rbp), %rax
movsd (%rax), %xmm0
subsd -0x50(%rbp), %xmm0
leaq -0x158(%rbp), %rdi
movabsq $-0x3f99800000000000, %rax # imm = 0xC066800000000000
movq %rax, (%rdi)
callq 0xd305
andpd 0x3b7f0(%rip), %xmm0    # 0x57010
movsd 0x3b860(%rip), %xmm1    # 0x57088
ucomisd %xmm0, %xmm1
jbe 0x1b881
movsd -0x140(%rbp), %xmm5
movsd -0x138(%rbp), %xmm1
xorpd %xmm0, %xmm0
ucomisd -0x70(%rbp), %xmm0
jmp 0x1b89d
leaq -0xf0(%rbp), %r14
leaq -0x108(%rbp), %rsi
leaq -0x158(%rbp), %r12
movq %r14, %rdi
movq %r12, %rdx
callq 0x234b0
movl %eax, %r13d
xorb $0x1, %r13b
movq %r14, %rdi
movq %r12, %rsi
callq 0x1cc1e
movapd %xmm0, %xmm5
jmp 0x1b8a1
movsd -0x118(%rbp), %xmm5
movsd -0x110(%rbp), %xmm1
xorpd %xmm0, %xmm0
ucomisd -0x130(%rbp), %xmm0
seta %r13b
movq -0xb0(%rbp), %r14
movsd -0x38(%rbp), %xmm3
movsd -0x58(%rbp), %xmm4
movq -0xa8(%rbp), %r12
movq -0x30(%rbp), %rcx
movapd -0xd0(%rbp), %xmm2
movapd -0xa0(%rbp), %xmm6
andpd 0x3b73b(%rip), %xmm6    # 0x57010
movq -0xc0(%rbp), %rax
mulsd (%rax), %xmm6
mulsd (%rcx), %xmm6
addsd 0x3b75c(%rip), %xmm6    # 0x57048
movsd 0x3b774(%rip), %xmm0    # 0x57068
divsd %xmm6, %xmm0
movapd -0x80(%rbp), %xmm6
addsd %xmm0, %xmm6
ucomisd %xmm2, %xmm6
movsd %xmm5, -0x70(%rbp)
movsd %xmm1, -0xa0(%rbp)
jbe 0x1b99f
pushq $0x2
popq %rsi
movq %r15, %rdi
callq 0xd4c0
movapd -0xd0(%rbp), %xmm0
ucomisd 0x3c98d(%rip), %xmm0    # 0x582c0
ja 0x1b4c2
movsd -0x70(%rbp), %xmm5
movapd %xmm5, %xmm0
movsd -0x58(%rbp), %xmm4
subsd %xmm4, %xmm0
movsd -0xa0(%rbp), %xmm1
movapd %xmm1, %xmm2
movsd -0x38(%rbp), %xmm3
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm0, %xmm0
addsd %xmm2, %xmm0
sqrtsd %xmm0, %xmm0
movsd (%rax), %xmm6
movsd 0x8(%rax), %xmm2
subsd %xmm4, %xmm6
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm6, %xmm6
addsd %xmm2, %xmm6
sqrtsd %xmm6, %xmm2
ucomisd %xmm2, %xmm0
movq -0x30(%rbp), %rcx
ja 0x1b4c2
movapd %xmm5, %xmm0
subsd 0x8(%r15), %xmm0
movapd %xmm1, %xmm2
subsd 0x10(%r15), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm0, %xmm0
addsd %xmm2, %xmm0
sqrtsd %xmm0, %xmm7
testb %r13b, %r13b
je 0x1b9d0
xorpd 0x3b650(%rip), %xmm7    # 0x57020
movq -0xb8(%rbp), %r13
movsd (%rcx), %xmm0
movapd %xmm0, %xmm6
movsd 0x3b661(%rip), %xmm2    # 0x57048
addsd %xmm2, %xmm6
divsd %xmm6, %xmm7
movapd %xmm7, -0x80(%rbp)
movapd 0x3b614(%rip), %xmm6    # 0x57010
andpd %xmm7, %xmm6
movq -0xc0(%rbp), %rax
mulsd (%rax), %xmm6
mulsd %xmm0, %xmm6
addsd %xmm2, %xmm6
movsd 0x3b64d(%rip), %xmm0    # 0x57068
divsd %xmm6, %xmm0
addsd 0x3c8a1(%rip), %xmm0    # 0x582c8
movsd %xmm0, -0x38(%rbp)
subsd %xmm5, %xmm4
subsd %xmm1, %xmm3
leaq -0x158(%rbp), %rdi
movsd %xmm4, (%rdi)
movsd %xmm3, 0x8(%rdi)
callq 0xcd30
leaq -0x160(%rbp), %rdi
movsd %xmm0, (%rdi)
movq %r13, %rsi
callq 0x124e1
ucomisd -0x38(%rbp), %xmm0
ja 0x1b4c2
movapd -0x80(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
divsd (%r12), %xmm0
divsd 0x80(%r15), %xmm0
movapd %xmm0, -0x80(%rbp)
callq 0x3db2c
movsd 0x33c0(%rax), %xmm0
xorps 0x3b584(%rip), %xmm0    # 0x57020
movaps %xmm0, -0x50(%rbp)
callq 0x3db2c
movapd -0x80(%rbp), %xmm0
maxsd -0x50(%rbp), %xmm0
movsd 0x33c0(%rax), %xmm3
minsd %xmm0, %xmm3
movl $0x2, 0x4(%rbx)
movsd 0x3b576(%rip), %xmm2    # 0x57040
movq %r15, %rsi
movsd -0x70(%rbp), %xmm0
movsd -0xa0(%rbp), %xmm1
callq 0x1bb22
movsd %xmm0, 0x20(%rbx)
movq %r14, %rdi
callq 0xcd42
movsd 0x20(%rbx), %xmm0
movq %rax, %rdi
callq 0x3a8ce
movsd %xmm0, 0x20(%rbx)
andpd 0x3b50a(%rip), %xmm0    # 0x57010
xorpd %xmm1, %xmm1
movapd %xmm1, -0x50(%rbp)
ucomisd 0x3b529(%rip), %xmm0    # 0x57040
jbe 0x1b4c2
jmp 0x1b4d3