#include "COL.h"

#include "CBall.h"
#include "CPlayer.h"
#include "GM.h"
#include "XMATH.h"
#include "GU.h"
#include "GL.h"
#include "SNDGAME.h"
void COL_PlayerBallCollideEvent(CPlayer *pPlayer_0, int i_1, bool &b_2) {
  TAnimData *tAnimData_r7;  // r7
  CPlayer *pPlarer_r0;      // r0
  CPlayer *pPlayer_r8 = 0;  // r8
  int iStance_r0;           // r0
  int i_r1;                 // r1
  TPoint v18;               // r2
  TPoint v20;               // r2
  TPoint tPoint_r0;         // r0
  int i_v23_54;             // [sp+Ch] [bp-54h]
  TPoint tPoint_4C;         // [sp+14h] [bp-4Ch] BYREF
  int i_mag_44;             // [sp+1Ch] [bp-44h] BYREF
  TPoint3D t_Point3D_40;    // [sp+20h] [bp-40h] BYREF
  TPoint3D tPoint_30;       // [sp+30h] [bp-30h] BYREF
  // 00171E98
  tAnimData_r7 = pPlayer_0->GetAnimData();  // call 001704DC
  pPlarer_r0 = tGame.allplay_14[tGame.mTLogic_6678.team_40]
                   .teamlist_0[tGame.mTLogic_6678.player_3C];
  if (pPlarer_r0->uc_88)
    pPlayer_r8 = tGame.allplay_14[tGame.mTLogic_6678.team_40]
                     .teamlist_0[tGame.mTLogic_6678.player_3C];

  // 00171ED8
  if (i_1 != -1) {
    // 00171EDC
    i_v23_54 = pPlayer_0->tStr32_44.uc_0;
    if (XMATH_Distance3D(&cBall.ballPosPrev_4, &tGame.mTMath_6734.field_370) <
        tGame.mTMath_6734.field_388[0] +
            tGame.mTMath_6734.field_388[1])  // call 001F3490
    {
      // 00171F0E
      tPoint_30.point_0.dx_0 = cBall.ballPosPrev_4.point_0.dx_0 -
                               tGame.mTMath_6734.field_370.point_0.dx_0;
      tPoint_30.point_0.dy_4 = cBall.ballPosPrev_4.point_0.dy_4 -
                               tGame.mTMath_6734.field_370.point_0.dy_4;
      tPoint_30.dz_8 =
          cBall.ballPosPrev_4.dz_8 - tGame.mTMath_6734.field_370.dz_8;

      XMATH_Normalize(&tPoint_30,
                      tGame.mTMath_6734.field_388[0] +
                          tGame.mTMath_6734.field_388[1]);  // call 001F2586
      cBall.ballPosPrev_4.point_0.dx_0 =
          tGame.mTMath_6734.field_370.point_0.dx_0 + tPoint_30.point_0.dx_0;
      cBall.ballPosPrev_4.point_0.dy_4 =
          tGame.mTMath_6734.field_370.point_0.dy_4 + tPoint_30.point_0.dy_4;
      cBall.ballPosPrev_4.dz_8 =
          tPoint_30.dz_8 + tGame.mTMath_6734.field_370.dz_8;
    }

    /// 171F58
    if (tGame.field_6C34 <= 2 && tGame.mTLogic_6678.field_48 == 11 &&
        tGame.mTLogic_6678.field_44 == pPlayer_0->tStr32_44.uc_0 &&
        tGame.mTLogic_6678.field_50 == pPlayer_0->tStr32_44.uc_1  /// 00171F88
        && (tGame.mTMath_6734.field_370.point_0.dy_4 -
            tGame.mTMath_6734.field_37C.point_0.dy_4) /
                       1024 * cBall.ballMovePrev_10.point_0.dy_4 / 256 +
                   cBall.ballMovePrev_10.point_0.dx_0 *
                       ((tGame.mTMath_6734.field_370.point_0.dx_0 -
                         tGame.mTMath_6734.field_37C.point_0.dx_0) /
                        1024) /
                       256 +
                   cBall.ballMovePrev_10.dz_8 *
                       ((tGame.mTMath_6734.field_370.dz_8 -
                         tGame.mTMath_6734.field_37C.dz_8) /
                        1024) /
                       256 <=
               -1) {
      tGame.field_6C34 = 0;
    } else {
      // 0017200A
      pPlayer_0->field_14C = i_1;
      i_mag_44 = XMATH_Mag3D(&cBall.ballMovePrev_10);        // call 001F3274
      if ((pPlayer_0->tStr32_44.field_8 & 0xFFFFFFFE) == 8)  // 00172024
      {
        // 00172024
        iStance_r0 = (pPlayer_0->tStr32_44.field_14 -
                      tAnimData_r7->filed_20[0].filed_0) /
                     pPlayer_0->tStr32_44.field_1C;  // call 002A3CC0
        if (iStance_r0 < 0) {
          iStance_r0 = -iStance_r0;  // 00172036
        }

        // 00172038
        if (iStance_r0 > 3) {
          // 001720A4
          tPoint_4C =
              XMATH_Project(pPlayer_0->u_0.s_2_0.s_2, 0x8000);  // 001F32DC

          if (i_mag_44 <= 2136) i_mag_44 = 2136;

          // 001720C8
          t_Point3D_40.point_0 = tPoint_4C;
          t_Point3D_40.dz_8 = (tGame.mTMath_6734.field_37C.dz_8 -
                               tGame.mTMath_6734.field_370.dz_8) /
                              8;

          XMATH_Normalize(
              &t_Point3D_40,
              (4 * (uint)i_mag_44) / 5u);  // call 002A3C14 //call 001F2586
        } else {
          // 0017203C
          t_Point3D_40.dz_8 = pPlayer_0->aPoint3D_178[1].dz_8;
          t_Point3D_40.point_0 = pPlayer_0->aPoint3D_178[1].point_0;
        }

        // 001720EE
        cBall.SetVel(t_Point3D_40.point_0.dx_0, t_Point3D_40.point_0.dy_4,
                     t_Point3D_40.dz_8);  // 00157668
        b_2 = 1;
      } else {
        // 0017204C
        t_Point3D_40.point_0.dx_0 = tGame.mTMath_6734.field_37C.point_0.dx_0 -
                                    tGame.mTMath_6734.field_370.point_0.dx_0;
        t_Point3D_40.point_0.dy_4 = tGame.mTMath_6734.field_37C.point_0.dy_4 -
                                    tGame.mTMath_6734.field_370.point_0.dy_4;
        t_Point3D_40.dz_8 = (tGame.mTMath_6734.field_37C.dz_8 -
                             tGame.mTMath_6734.field_370.dz_8) /
                            8;
        XMATH_Normalize(&t_Point3D_40, i_mag_44);  // call 001F2586

        // 0017208C
        if ((pPlayer_r8 == 0 || pPlayer_r8 == pPlayer_0))  // call 001F328A
        {
          // 00172098
          cBall.SetVel(t_Point3D_40.point_0.dx_0, t_Point3D_40.point_0.dy_4,
                       t_Point3D_40.dz_8);  // call 00157668
        } else {
          // loc_1721CE
          if(XMATH_Mag(&pPlayer_r8->Point3D_14.point_0)>(i_mag_44/2)){
          GM_DeflectBallFromDribbler(pPlayer_0, &i_mag_44);  // 00171E50
          b_2 = 1;
          }else{
            // 00172098
          cBall.SetVel(t_Point3D_40.point_0.dx_0, t_Point3D_40.point_0.dy_4,
                       t_Point3D_40.dz_8);  // call 00157668
          }
        }
      }

      // 001720FE
      i_r1 = 0xFA5;
      if (i_mag_44 / 2 > 0xFA5) i_r1 = i_mag_44 / 2;

      // 0017210C
      XMATH_Normalize(&cBall.ballMovePrev_10, i_r1);
      if (pPlayer_r8 == 0 || pPlayer_r8 == pPlayer_0) {
        cBall.SetVelRel(pPlayer_0->point_20.dx_0 / 4,
                        pPlayer_0->point_20.dy_4 / 4, 0);  // call 00157678
      }

      // 0017213A
      if (pPlayer_0->tStr32_44.uc_0 == 2) {
        // 00172142
        tGame.field_6C34 = 0;
        tGame.mTLogic_6678.field_48 = 11;
      } else {
        // 00172154
       
        GL_SetTouch(i_v23_54, pPlayer_0->tStr32_44.uc_1,
                    (pPlayer_0->tStr32_44.field_8 | 1) == 9, EKickType_11,
                    "Deflection");
      }

      // 00172174
      if (pPlayer_0->tStr32_44.field_8 != 8 &&
          pPlayer_0->tStr32_44.field_8 != 15) {
        // 00172190
        if ((int)cBall.speedXY_28 < 16021) {
          // 001721F6
          if ((int)cBall.speedXY_28 < 10681) {
            // 00172230
            tPoint_r0.dx_0 = cBall.point3D_44.point_0.dx_0 / 8 +
                             pPlayer_0->Point3D_14.point_0.dx_0 / 2;
            tPoint_r0.dy_4 = cBall.point3D_44.point_0.dy_4 / 8 +
                             pPlayer_0->Point3D_14.point_0.dy_4 / 2;
            pPlayer_0->Point3D_14.point_0 = tPoint_r0;
          } else {
            // 001721FE
            v20 = pPlayer_0->point_20;
            tPoint_4C.dx_0 = v20.dx_0 + cBall.point3D_44.point_0.dx_0 / 8;
            tPoint_4C.dy_4 = v20.dy_4 + cBall.point3D_44.point_0.dy_4 / 8;
            pPlayer_0->Trip(0, GU_GetRot(&tPoint_4C));
          }
        } else {
          // 0017219C
          v18 = pPlayer_0->point_20;
          tPoint_4C.dx_0 = v18.dx_0 + cBall.point3D_44.point_0.dx_0 / 8;
          tPoint_4C.dy_4 = v18.dy_4 + cBall.point3D_44.point_0.dy_4 / 8;
          pPlayer_0->Fell(GU_GetRot(&tPoint_4C), 0);
        }
      }
      // 0017217E
      cBall.Deflect();
      SNDGAME_PlaySFX(0x14u);
    }
  }
}
// uint COL_PlayerBallCollision(CPlayer *player_0) {
//   unsigned int iIndex_r4;       // r4
//   int i_x_r1;                   // r1
//   TLimbInfo *pTLimbInfo_r10;    // r10
//   int i_y_r3;                   // r3
//   TPoint v8;                    // d16
//   int i_r9;                     // r12
//   int i_r7;                     // r7
//   int i_Distance_3D;            // [sp+3Ch] [bp-29Ch]
//   TPoint3D tTPoint3Ds_48[42];   // [sp+48h] [bp-290h] BYREF
//   TPoint3D tPoint_240;          // [sp+240h] [bp-98h] BYREF
//   TPoint3D tPoint_250;          // [sp+250h] [bp-88h] BYREF
//   TPoint3D tPoint_260;          // [sp+260h] [bp-78h] BYREF
//   TPoint3D tPoint_270;          // [sp+270h] [bp-68h] BYREF
//   uint8 b_flags_280[24] = {0};  // [sp+280h] [bp-58h]

//   // 001718B6
//   tPoint_270 = cBall.point3D_2C;
//   tPoint_260 = cBall.ballPosPrev_4;

//   i_Distance_3D = XMATH_Distance3D(&tPoint_270, &tPoint_260);  // call
//   001F3490 iIndex_r4 = 0;

//   // 001718DA
//   while (iIndex_r4 <= 0xD) {
//     // 001718DE
//     i_x_r1 = COL_tLimbInfo[iIndex_r4].field_0;
//     if (!b_flags_280[i_x_r1])  // 001718E6
//     {
//       // 001718EC
//       player_0->GetBonePosition(i_x_r1, &tTPoint3Ds_48[i_x_r1]);  // call
//       001715EC i_x_r1 = COL_tLimbInfo[iIndex_r4].field_0; b_flags_280[i_x_r1]
//       = 1;
//     }

//     // 00171904
//     pTLimbInfo_r10 = &COL_tLimbInfo[iIndex_r4];
//     i_y_r3 = pTLimbInfo_r10->field_4;
//     if (!b_flags_280[i_y_r3])  // 0017190E
//     {
//       // 00171910
//       player_0->GetBonePosition(pTLimbInfo_r10->field_4,
//       &tTPoint3Ds_48[i_y_r3]);  // call 001715EC i_x_r1 =
//       COL_tLimbInfo[iIndex_r4].field_0; i_y_r3 = pTLimbInfo_r10->field_4;
//       b_flags_280[i_y_r3] = 1;
//     }

//     // 0017192C
//     tPoint_240 = tTPoint3Ds_48[i_y_r3];
//     tPoint_250 = tTPoint3Ds_48[i_x_r1];

//     // 00171958
//     i_r9 = XMATH_Distance3D(&tPoint_250, &tPoint_240);  // call 001F3490
//     i_r7 = COL_tBoneNodeInfo[COL_tLimbInfo[iIndex_r4].field_0].i_4;

//     // 0017198A
//     if (i_r7 < COL_tBoneNodeInfo[pTLimbInfo_r10->field_4].i_4)
//       i_r7 = COL_tBoneNodeInfo[pTLimbInfo_r10->field_4].i_4;

//     // 001719C4
//     ++iIndex_r4;
//     if (UTILCOL_CapsuleCapsuleCheck(tPoint_250, tPoint_240, tPoint_270,
//     tPoint_260, i_r7, 0x1000, i_r9, i_Distance_3D))  // call 001787A0
//       return iIndex_r4 - 1;
//   }
//   return -1;
// }