#include "CFTTNavMesh.h"

#include <arm_neon.h>

#include "FREE.h"

#include "TTDList.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "sub.h"

//#include "TNMNeighbour.h"
const uint dword_2FED08[0xE] = {0x3243F, 0x1DAC6, 0xFADC, 0x7F57, 0x3FEB,
                                   0x1FFD, 0x1000, 0x800, 0x400, 0x200,
                                   0x100, 0x80, 0x40, 0x20};


int Combine(TTDList<TTSPtr<TNMVert>> *a1, TTDList<TTSPtr<TNMVert>> *a2, TTDList<TTSPtr<TNMVert>> *a3)
{
  LOGE("Combine=%x%x%x", (unsigned)a1, (unsigned)a2, (unsigned)a3);
  return 0;
}

//00167760 ^_^
//单元测试等级: 目测游戏表现
//单元测试内容: 1-10
//单元测试结果: 正常
//已测试分支 1 5
void sub_167760(fpoint2<PointType_14> *r0_pfpoint, fpoint2<PointType_14> *r1_pfpoint)
{
  LOGE("sub_167760");
  int i_sp0;
  sub_166CB2(&i_sp0, r1_pfpoint);
  long long r7_ull = r1_pfpoint->x_0;
  long long r0_ull = r1_pfpoint->y_4;
  long long r5_ull = i_sp0;
  r0_pfpoint->x_0 = (r7_ull * 0x4000) / (r5_ull);
  r0_pfpoint->y_4 = (r0_ull * 0x4000) / (r5_ull);
}

// 00166D24 ^_-
//单元测试等级: 目测游戏表现
//单元测试内容: 1-10
//单元测试结果: 正常
//已测试分支  1 5

void EarTest(TTDListNode<TTSPtr<TNMVert>> *r0_pdlist_node)
{
  //TTSPtr<TNMVert> size=0x8
  //LOGE("%x%x", (unsigned)a1, (unsigned)a1->next);
  //sp34=r0_pdlist_node->next->m.pT_0
  //sp38=
  
  TTSPtr<TNMVert> ptr_vert_next_sp34=r0_pdlist_node->next->m;
  TTSPtr<TNMVert> ptr_vert_cur_sp2c=r0_pdlist_node->m;
  TTSPtr<TNMVert> ptr_vert_prev_sp24=r0_pdlist_node->prev->m;
  //r2 r1 r7 =ptr_vert_cur_sp2c ptr_vert_next_sp34 ptr_vert_prev_sp24  .0 
  //00166D5E
  //r3 r0  r0_pdlist_node->next  prev
  
  //r8 r1  ptr_vert_next_sp34.pT_0->field_0 ptr_vert_next_sp34.pT_0->field_4
  //r12 lr ptr_vert_next_sp24.pT_0->field_0 ptr_vert_next_sp24.pT_0->field_4
  //r7 r2 =ptr_vert_cur_sp2c.pT_0->field_0 ptr_vert_cur_sp2c.pT_0->field_4
 int r3_i= ptr_vert_prev_sp24.pT_0->field_4-ptr_vert_next_sp34.pT_0->field_4;
 int i_sp8=ptr_vert_prev_sp24.pT_0->field_0-ptr_vert_cur_sp2c.pT_0->field_0;
 int i_sp18=ptr_vert_next_sp34.pT_0->field_4-ptr_vert_cur_sp2c.pT_0->field_4;
 int r5_i=ptr_vert_cur_sp2c.pT_0->field_4-ptr_vert_prev_sp24.pT_0->field_4;
 int r4_i=ptr_vert_next_sp34.pT_0->field_0-ptr_vert_prev_sp24.pT_0->field_0;
 int r7_i=ptr_vert_cur_sp2c.pT_0->field_0-ptr_vert_next_sp34.pT_0->field_0;
 //sp4 =ptr_vert_cur_sp2c.0
 //sp1c=ptr_vert_next_sp34.0
 //spc=r5
 //sp8=r6
 //sp10= ptr_vert_cur_sp2c.pT_0->field_4
 //sp20= ptr_vert_next_sp34.pT_0->field_0  
 //sp14 18=ptr_vert_cur_sp2c.pT_0->field_0 r1_i

 
 TTDListNode<TTSPtr<TNMVert>>* r3_pnode = r0_pdlist_node->next;
 TTDListNode<TTSPtr<TNMVert>>* r0_pnode = r0_pdlist_node->prev;
 //loc_166D9A
 while(r0_pnode->prev!=r3_pnode){ 
   LOGE("EarTest_1"); //ok
   //loc_166D9A
   r0_pnode=r0_pnode->prev;
   // r8 r10 r0_pnode->m.pT_0->field_0 r0_pnode->m.pT_0->field_4
  int r6_i= r0_pnode->m.pT_0->field_4-ptr_vert_prev_sp24.pT_0->field_4;
  int r9_i=r0_pnode->m.pT_0->field_0-ptr_vert_prev_sp24.pT_0->field_0;
  int r1_i= ((uint64)r9_i)*r3_i/0x4000 + ((uint64)r6_i)*r4_i/0x4000;
  if(r1_i>=1){
    LOGE("EarTest_2");
    //00166DC8
    r1_i = r0_pnode->m.pT_0->field_0 -ptr_vert_next_sp34.pT_0->field_0;
    int r2_i= r0_pnode->m.pT_0->field_4 - ptr_vert_next_sp34.pT_0->field_4;
    r1_i= ((uint64)r1_i)*i_sp18/0x4000 + ((uint64)r2_i)*r7_i/0x4000;
    if(r1_i>=1){
      //00166DF0
      LOGE("EarTest_3");
      r1_i= r0_pnode->m.pT_0->field_4-ptr_vert_cur_sp2c.pT_0->field_4;
      r6_i= r0_pnode->m.pT_0->field_4 -ptr_vert_cur_sp2c.pT_0->field_0;
      r1_i= ((uint64)r1_i)*i_sp8/0x4000 + ((uint64)r6_i)*r5_i/0x4000;
      if(r1_i>0){
        LOGE("EarTest_4");
        return ;
      }

    }
  }
  

 }
//loc_166E2C  ok
LOGE("EarTest_5");
ptr_vert_cur_sp2c.pT_0->field_9=true;
  
}

void Test(){
  TNMTri tri_sp;

  fpoint2<PointType_14> parm1;
  parm1.x_0=0x1542;
  parm1.y_4=0x5487;
 // fpoint2<PointType_14> parm1;
 fpoint2<PointType_14> ret= tri_sp.Inside(parm1,false);
 LOGE("%x",ret.x_0);
}
//---------------------------------------------------------------------------------------
// 00167BD0  ^_-
//已测试分支  34 35 36 37 38 40 41 43 44 46 47 48 49 50 53 54
//单元测试等级: 目测游戏表现
//单元测试内容: 1-10
//单元测试结果: 正常
//已测试分支 
void CFTTNavMesh::Init()
{
  // 00167BD0
  // sp68=this
  LOGE("CFTTNavMesh::Init");
  TNMVert *vert_r1 = new TNMVert();              //这里要加上默认构造函数
  TTSPtr<TNMVert> TNMVert_TTSPtr_sp210(vert_r1); //智能指针构造函数要禁止内联
  vert_r1 = new TNMVert();
  TTSPtr<TNMVert> TNMVert_TTSPtr_sp208(vert_r1);
  vert_r1 = new TNMVert();
  TTSPtr<TNMVert> TNMVert_TTSPtr_sp200(vert_r1);
  vert_r1 = new TNMVert();
  TTSPtr<TNMVert> TNMVert_TTSPtr_sp1f8(vert_r1);
  TNMVert_TTSPtr_sp210->field_0 = 0xFFF1C000;
  TNMVert_TTSPtr_sp210->field_4 = 0x12C000;
  TNMVert_TTSPtr_sp208->field_0 = 0xE4000;
  TNMVert_TTSPtr_sp208->field_4 = 0x12C000;
  TNMVert_TTSPtr_sp200->field_0 = -0xE4000;
  TNMVert_TTSPtr_sp200->field_4 = -0x12C000;
  TNMVert_TTSPtr_sp1f8->field_0 = 0xE4000;
  TNMVert_TTSPtr_sp1f8->field_4 = 0xFFED4000;
  TNMVert_TTSPtr_sp208->field_A = 0;
  TNMVert_TTSPtr_sp210->field_A = 1;
  TNMVert_TTSPtr_sp200->field_A = 2;
  TNMVert_TTSPtr_sp1f8->field_A = 3;

  TTDList<TTSPtr<TNMVert>> list_sp1f0;
  // TTSPtr<TNMVert> TNMVert_TTSPtr_sp1e8=TNMVert_TTSPtr_sp208; 临时变量
  list_sp1f0.Insert(TNMVert_TTSPtr_sp208); //临时变量使用的是sp1e8

  //TTDList<TTSPtr<TNMVert>> list_sp108;
  // sp1d8  sp1e0  临时变量
  list_sp1f0.Insert(TNMVert_TTSPtr_sp210,
                    TNMVert_TTSPtr_sp208); // 00167CE0
  // sp1d0  sp1c8
  list_sp1f0.Insert(TNMVert_TTSPtr_sp200,
                    TNMVert_TTSPtr_sp210); // 00167D12
  // sp1c0 sp1b8
  list_sp1f0.Insert(TNMVert_TTSPtr_sp1f8,
                    TNMVert_TTSPtr_sp200); // 00167D48
  TNMVert_TTSPtr_sp210.Release();
  TNMVert_TTSPtr_sp208.Release();
  TNMVert_TTSPtr_sp200.Release();
  TNMVert_TTSPtr_sp1f8.Release();
  // sp5c=0x198
  //
  //[sp5c]=sp198
  uint r3_u = 0;

  TTList<TTDList<TTSPtr<TNMVert>> *> ttlist_sp1A8;

  int arr_sp40[4] = {-0x3f, 0, -0x3f, 0};
  uint d_sp30[4] = {0x7000, 0x3000, 0x7000, 0xFFFFD000};
  uint d_d14d15[4] = {0xFFFF9000, 0xFFFFD000, 0xFFFF9000, 0x3000};
  uint d_d10d11[4] = {0x3000, 0x2000, 0x3000, 0xFFFFE000};
  uint d_d12d13[4] = {0xFFFFD000, 0xFFFFE000, 0xFFFFD000, 0x2000};

  // float32x4_t  d_sp40;// = {-0x3f, 0, -0x3f, 0};  // 应该都是临时变量
  // d_sp40.n128_u64

  // int arr_sp30[4] = {0x7000, 0x3000, 0x7000, 0xFFFFD000};  //
  // 应该都是临时变量
  long long sp6csp60_ll;
  LOGE("CFTTNavMesh::Init loc_1682E0");
  // loc_1682E0
  LOGE("FREE_tInfo.filed_0.pFREE_tGoal_1818->m_iPropCount_74F0=%d", FREE_tInfo.filed_0.pFREE_tGoal_1818->m_iPropCount_74F0);
  while (r3_u < (uint)FREE_tInfo.filed_0.pFREE_tGoal_1818->m_iPropCount_74F0)
  {
    // loc_167DB6
    LOGE("CFTTNavMesh::Init loc_167DB6");
    TFreeProp *r10_pfreeProp =
        FREE_tInfo.filed_0.pFREE_tGoal_1818
            ->m_tFreeProp_74F4; //这里还原的时候提前使用了r10 不是还原错误
    if (r10_pfreeProp[r3_u].m_iType_C != 0)
    {
      // 00167DCE
      TPoint point_sp108[2];
      memset(&point_sp108, 0, sizeof(point_sp108));
      TPoint point_sp188[2];
      memset(&point_sp188, 0, sizeof(point_sp188));
      int r5_i =r10_pfreeProp[r3_u].m_tPoint3D_0.point_0.dx_0 / 2- r10_pfreeProp[r3_u].m_tPoint3D_0.point_0.dx_0 / 2 % 10;
      int r4_i = r10_pfreeProp[r3_u].m_tPoint3D_0.point_0.dy_4 / 2;
      // loc_167E24
      for (int r7_i = 0; r7_i < 4; r7_i++)
      {
        LOGE("branch_1");
        // loc_167E0E
        point_sp108[r7_i].dx_0 = r5_i;
        point_sp108[r7_i].dy_4 = r4_i - r4_i % 10;
      }
      // 00167E28
      uint u_r0;
      if (r10_pfreeProp[r3_u].m_iType_C == 2)
      {
        u_r0 = 1;
      }
      else
        u_r0 = 0;
      uint64x2_t d_q9 = vld1q_u32((const unsigned int *)arr_sp40);
      uint64x1_t d_d16 = vdup_n_s32(u_r0);
      uint64x2_t d_q8 = vmovl_u32(d_d16);

      d_q8 = vshlq_n_s64(d_q8, 63);
      d_q8 = vshlq_s64(d_q9, d_q8);
      d_q9 = d_q8;
      uint64x2_t d_q6 = vld1q_u32((const unsigned int *)d_d12d13);
      uint64x2_t d_q7 = vld1q_u32((const unsigned int *)d_d14d15);
      uint64x2_t d_q5 = vld1q_u32((const unsigned int *)d_d10d11);
      uint64x2_t d_q10 = vld1q_u32((const unsigned int *)d_sp30);
      vbslq_s64(d_q8, d_q6, d_q7);
      vbslq_s64(d_q9, d_q5, d_q10);
      uint64x2_t d_sp188 = d_q9; //这里的代码应该是采用了优化 看不懂
      uint64x2_t d_sp198 = d_q8;
      r4_i = ((long long)(r10_pfreeProp->m_iRot_10 * 0x4000 / 0xb4)) * 0x3243F /
             0x4000; 
      int r9_i = (r4_i % 10 - r4_i) * 16;
      int r10_i;
      // int i_sp60, i_sp6c;

      if (r9_i < 0xFFF9B781)
      {
        LOGE("branch_2");
        // 00167E9A
        r10_i = r9_i + 0xC90FE;
        // i_sp60 = 0;
        // i_sp6c = -1;
        sp6csp60_ll = 0xffffffff00000000;
      }
      else
      { // loc_167EAC
        r10_i = r9_i;
        if (r9_i > 0x6487F)
        {
          LOGE("branch_3");
          // i_sp6c = -1;
          sp6csp60_ll = 0xffffffff00000000;
          r10_i += 0xFFF36F02;
        }
        else
          //   i_sp6c = 1;
          // i_sp60 = 0;
          sp6csp60_ll = 0x100000000;
      }
      // loc_167EC8
      // int r11_i = 0;
      LOGE("branch_4");
      r4_i = 0x40000;
      long long r11r8_ll = 0x40000;
      int i_sp168;
      int i_sp160;
      for (int r5_i = 0xE; r5_i != 0; r5_i--)
      {
        LOGE("branch_5");
        // loc_167EDA
        i_sp160 = r4_i + 0x40000;
        sub_16A818(&i_sp168, &i_sp160);
        long ll_r2r1 = r11r8_ll * i_sp168 / 4096;
        if (ll_r2r1 < 0)
          r11r8_ll = -4294967296;
        // else
        //   r11_i = 0;
        r4_i = r4_i / 4;
      }
      // 00167F10
      int r12_i = 0;
      int r1_i = 0x40000;
      int r2_i = 0x40000;
      // loc_167F82
      for (int r3_i = 0x0; r3_i != 0xE; r3_i++)
      {
        LOGE("branch_6");
        // loc_167F20
        // 0xFFF80000&(r10_i>>31)
        int r4_i;
        if (r10_i > 0)
          r4_i = 0;
        else
          r4_i = 0xFFF80000;
        r4_i |= 0x40000;
        long long r0r7_ll = r2_i * r4_i;
        long long r6r5_ll = dword_2FED08[r3_i] * r4_i;
        long long r7r0_ll = r0r7_ll / 0x4000 * r1_i; 
        r10_i -= (r6r5_ll / 0x40000);
        long long r6r7_ll = r12_i * r4_i;
        r12_i += r7r0_ll / 0x40000;
        r6r7_ll = r6r7_ll / 0x4000 * r1_i;
        r2_i -= r6r7_ll / 0x40000;
        r1_i /= 2;
      }
      // 00167F86
      int r0_i =
          sp6csp60_ll * r12_i / 0x4000 / r11r8_ll; // check 有符号64位除法
      if (r9_i <= 0xFFF9B780)
      {
        LOGE("branch_7");
        // 00167FB0
        r9_i += 0xC90FE;
        sp6csp60_ll = -4294967296;
      }
      else
      { // loc_168000

        if (r9_i > 0x6487F)
        {
          LOGE("branch_8");
          r9_i += 0xFFF36F02;
          sp6csp60_ll = -4294967296;
        }
        else
          sp6csp60_ll = 4294967296;
      }
      // loc_16801A
      r4_i = r0_i >> 4;

      r5_i = 0x40000;
      long long r11r10_ll = 0x40000;
      // loc_168062
      for (int r8_i = 0; r8_i < 0xe; r8_i--)
      {
        LOGE("branch_9");
        // loc_16802E
        i_sp160 = r5_i + 0x40000;
        sub_16A818(&i_sp168, &i_sp160);
        r11r10_ll = i_sp168 * r11r10_ll / 0x40000;
        r5_i /= 4;
      }
      // 00168068
      r12_i = 0;
      r2_i = 0x40000;
      r1_i = 0x40000;
      // r8=r4 缓存
      // loc_1680DA
      for (int r3_i = 0; r3_i != 0xE; r3_i++)
      {
        LOGE("branch_10");
        // loc_16807A
        int r7_i;
        if (r9_i < 0)
          r7_i = 0xFFFC0000;
        else
          r7_i = 0x40000;
        // 0016808E
        r0_i = (long long)r1_i * r7_i / 0x4000; 
        r9_i -= (long long)dword_2FED08[r3_i] * r7_i / 0x40000;
        r7_i = r12_i * r7_i / 0x4000;
        r12_i += ((long long)r0_i) * r2_i / 0x40000;
        r1_i -= r7_i * r2_i / 0x40000;
        r2_i /= 2;
      }
      // 001680DE
      r0_i = sp6csp60_ll * r1_i / 0x4000 / r11r10_ll >> 4; //有符号64div
      r12_i = 0 - r4_i;
      r9_i = 0;
      // loc_168158
      TPoint *ppoint = (TPoint *)&d_sp188;
      for (int r2_i = 0; r2_i != 4; r2_i++)
      {
        LOGE("branch_11");
        // loc_16811A
        //(long long*)&d_sp188[r2_i]
        int r7_i = (long long)ppoint[r2_i].dx_0 * r0_i / 0x4000; // check
        ppoint[r2_i].dx_0 = (long long)ppoint[r2_i].dy_4 * r4_i / 0x4000 + r7_i;
        ppoint[r2_i].dy_4 = (long long)ppoint[r2_i].dy_4 * r0_i / 0x4000 + (long long)ppoint[r2_i].dx_0 * r12_i / 0x4000;
      }
      // 0016815C
      // loc_168180
      for (r0_i = 0; r0_i != 4; r0_i++)
      {
        LOGE("branch_12");
        // loc_168160
        // point_sp108
        point_sp108[r0_i].dx_0 += ppoint[r0_i].dx_0;
        point_sp108[r0_i].dy_4 += ppoint[r0_i].dy_4;
      }
      // 00168184
      // loc_168188
      TTSPtr<TNMVert> pvert_sp168[4];
      for (int r6_i = 0; r6_i < 4; r6_i++)
      {
        LOGE("branch_13");
        // loc_16819E
        TTSPtr<TNMVert> pvert_sp160;
        pvert_sp160.pT_0 = new TNMVert();
        pvert_sp168[r6_i] = pvert_sp160;
        pvert_sp168[r6_i]->field_0 = point_sp108[r6_i].dx_0; //
        pvert_sp168[r6_i]->field_4 = point_sp108[r6_i].dy_4;
        pvert_sp168[r6_i]->byte_8 = 0;
      }
      // 001681E2
      TTDList<TTSPtr<TNMVert>> *r6_plist = new TTDList<TTSPtr<TNMVert>>;
      r6_plist->Insert(pvert_sp168[0]); //产生临时变量sp158
      // loc_168254
    //  TTDList<TTSPtr<TNMVert>> list_sp160; //构造函数内联优化到pvert_sp160这里
      for (int r4_i = 0; r4_i != 3; r4_i++)
      {
        LOGE("branch_14");
        // loc_168218
        LOGE("loc_168218");
        
        r6_plist->Insert(pvert_sp168[r4_i], pvert_sp168[r4_i]);
      }
      // 00168258
      // loc_16825A
      TTDList<TTSPtr<TNMVert>> *r4_plist = r6_plist;
      TTDList<TTSPtr<TNMVert>> *r5_plist = r6_plist; 
      // loc_16826E
      TTList<TTDList<TTSPtr<TNMVert>> *>::TTListNode *r7_pnode = ttlist_sp1A8.pBegin_0;
      while (r7_pnode)
      {
        // 00168270
        r6_plist = new TTDList<TTSPtr<TNMVert>>();
        if (Combine(r6_plist, r7_pnode->m, r4_plist))
        {
          LOGE("branch_15");
          // 00168294
          if (r7_pnode->m)
            delete r7_pnode->m;
          // loc_1682A6
          ttlist_sp1A8.Remove(r7_pnode->m);
          if (r4_plist)
          {
            LOGE("branch_16");
            delete r4_plist; 
          }
        }
        else if (r6_plist)
        {
          LOGE("branch_17");
          // loc_168260
          delete r6_plist;
        }
        r7_pnode = r7_pnode->next;
        
      }
      // loc_1682C0
      ttlist_sp1A8.Insert(r4_plist);
      // loc_1682CC出作用域释放sp168
    }
    // loc_1682DE

    r3_u++;
  }
  // 001682F8
  // r8=sp108
  // sp14=sp128
  // sp10=sp120
  // sp18=sp118
  // r0=4

 

  // loc_1686DC
  LOGE("CFTTNavMesh::Init loc_1686DC ttlist_sp1A8.dCount_8=%x", ttlist_sp1A8.dCount_8);
  TTList<TTDList<TTSPtr<TNMVert>> *>::TTListNode *r5_pnode = ttlist_sp1A8.pBegin_0; 
  int i_sp6c=4;
  while (ttlist_sp1A8.dCount_8)
  {
     // loc_168318
  TTDListNode<TTSPtr<TNMVert>> *r11_pnode = 0;
  TTDList<TTSPtr<TNMVert>> *r10_plist = 0;
  TTDList<TTSPtr<TNMVert>> *r9_plist = 0;
  int r6_i = -2147467264;
    // loc_16838A
    LOGE("CFTTNavMesh::Init loc_16838A");
   
    
    while (r5_pnode)
    {
      // loc_16832E
      TTDList<TTSPtr<TNMVert>> *r7_plist = r5_pnode->m;
      TTDListNode<TTSPtr<TNMVert>> *r4_pnode = r7_plist->pLast_0;
      int r0_i = 0;
      // loc_16837C
      while (r0_i == 0 || r7_plist->pLast_0 != r4_pnode)
      {
        LOGE("branch_18");
        // loc_168350
        TTSPtr<TNMVert> pvert_sp108 = r4_pnode->m;
        if (pvert_sp108.pT_0->field_0 > r6_i)
        {
          LOGE("branch_19");
          r10_plist =r5_pnode->m;
          r11_pnode = r4_pnode;
          r6_i = pvert_sp108.pT_0->field_0;
          r9_plist = r10_plist;
        }
        r4_pnode = r4_pnode->prev;

        r0_i = 1;
      }
      LOGE("branch_20");
      // 00168382
      r5_pnode = r5_pnode->next;
    }
    // 00168390
    TNMTri nmtri_sp108;
    int i_sp2c=0x7FFFC000;
    int i_sp28 = 0;
    int i_sp24 = 0;
    int i_sp20 = 0;
    int i_sp1c = 0;
    //int i_sp30 = r11_pnode->m.pT_0->field_4;

    // sp5c= r11_plist->m.pT_0->field_0
    // sp30= r11_plist->m.pT_0->field_4
    // sp40=list_sp1f0.pLast_0
    int r1_i = 0;

    // loc_168488
     //TTDListNode<TTSPtr<TNMVert>> *r4_pnode;
    TTDListNode<TTSPtr<TNMVert>> *r8_pnode = list_sp1f0.pLast_0;
    // loc_168488
    while (r1_i == 0 || list_sp1f0.pLast_0 != r8_pnode)
    {
       LOGE("branch_21");
      // loc_1683E0
      // sp60=r8_pnode
      r1_i = 1;
      // r8_pnode->prev->m.pT_0 =r7
      int r6_i = r8_pnode->prev->m.pT_0->field_4 - r8_pnode->m.pT_0->field_4;
      if (r6_i >= 0xA4)
      {
         LOGE("branch_22");
        // 001683FC
         int r0_i=((long long)(r11_pnode->m.pT_0->field_4-r8_pnode->m.pT_0->field_4)*0x4000)/r6_i;
        //uint r0_u = (long long)r6_i * 0x4000 / r6_i;
        if ((uint)r0_i <= 0x4000)
        { // check
          // 0016841C
           LOGE("branch_23");
          int r2_i = r8_pnode->prev->m.pT_0->field_0 - r8_pnode->m.pT_0->field_0;

          r2_i = (long long)r0_i * r2_i / 0x4000 + r8_pnode->m.pT_0->field_0;
          int r3_i=r2_i - r11_pnode->m.pT_0->field_0;
          if (r2_i > r11_pnode->m.pT_0->field_0 && r3_i < i_sp2c)
          {
             LOGE("branch_24");
            // 00168440
            int r0_i = (long long)r0_i * r6_i / 0x4000;
            int r12_i = r8_pnode->m.pT_0->field_4 + r0_i;
            r1_i = r12_i;
            r6_i=r2_i;
            int r5_i=r8_pnode->m.pT_0->field_4;
            int lr_i=r8_pnode->m.pT_0->field_0;
            if (r0_i > 0){
               LOGE("branch_25");
              // 0016845E
              r6_i=lr_i;
              r1_i = r8_pnode->m.pT_0->field_4;
              r5_i=r12_i;
              lr_i=r2_i;
            }
            i_sp28=r6_i;
            i_sp24=r1_i;
           // i_sp124=r1_i;
            nmtri_sp108.filed_10[1].y_4=r1_i;
            nmtri_sp108.filed_10[1].x_0=r6_i;
           // i_sp120=r6_i;
            // i_sp128=lr_i;
            // i_sp12c=r5_i;
            nmtri_sp108.filed_10[2].y_4=r5_i;
            nmtri_sp108.filed_10[2].x_0=lr_i;
            i_sp2c=r3_i;
            i_sp1c=r5_i;
            i_sp20=lr_i;
            //r4_pnode = r8_pnode;
          }
        }
      }
      r8_pnode=r8_pnode->prev;
    }
    // 00168492
    fpoint2<PointType_14> point_sp188;
    fpoint2<PointType_14> point_sp168;
    point_sp188.y_4 = i_sp24 - r11_pnode->m.pT_0->field_4;
    point_sp188.x_0 = i_sp28 - r11_pnode->m.pT_0->field_0;
    point_sp168.y_4 = i_sp1c - r11_pnode->m.pT_0->field_4;
    point_sp168.x_0 = i_sp20 - r11_pnode->m.pT_0->field_0;
    fpoint2<PointType_14> fpoint_sp160;
    sub_167760(&fpoint_sp160, &point_sp188);
    int r4_i = fpoint_sp160.y_4;
    point_sp188 = fpoint_sp160;
    // arr_sp188[1]= fpoint_sp160.y_4;

    sub_167760(&fpoint_sp160, &point_sp168);
    point_sp168 = fpoint_sp160;
    int r8_i = fpoint_sp160.y_4;

    if (r4_i < 0)
    {
       LOGE("branch_26");
      r4_i = -r4_i;
    }
    //loc_1684D6
    if (r8_i < 0)
      r8_i = -r8_i;
    if (r8_i > r4_i)
      r8_i = r4_i;
   // r4_i = 0;
    int r0_i = 0;
    // loc_168542
   

   
    fpoint2<PointType_14> fpoint_sp100;
    fpoint2<PointType_14> fpoint_spf8;
   // TTDListNode<TTSPtr<TNMVert>> *r4_pnode = r8_pnode;
    TTDListNode<TTSPtr<TNMVert>> *r6_pnode = list_sp1f0.pLast_0;
    r0_i = 0;

    while (r0_i == 0 || list_sp1f0.pLast_0 != r6_pnode)
    {
       LOGE("branch_27");
      // loc_1684EE
      if (r6_pnode->m.pT_0->byte_8 == 0)
      {
        // 001684F4
        fpoint_sp100.x_0 = r6_pnode->m.pT_0->field_0;
        fpoint_sp100.y_4 = r6_pnode->m.pT_0->field_4; //这里可能是fpoint
        fpoint_sp160 = nmtri_sp108.Inside(fpoint_sp100, false);
        if (fpoint_sp160.x_0 == 0)
        {
          // 0016850C
          LOGE("branch_28");
          fpoint_sp160.x_0 = r6_pnode->m.pT_0->field_0 -nmtri_sp108.filed_10[0].x_0;
          fpoint_sp160.y_4 = r6_pnode->m.pT_0->field_4 - nmtri_sp108.filed_10[0].y_4;
          sub_167760(&fpoint_spf8, &fpoint_sp160);
          r0_i = fpoint_spf8.y_4;
          if (r0_i >= 0)
          {
            r0_i = -r0_i;
          }
          if (r0_i <= r8_i)
           {//loc_168534
           LOGE("branch_29");
            r4_i = r8_i;
            r8_i = r0_i;
          }
        }
        // loc_16853C
        r0_i = 1;
        r6_pnode = r6_pnode->prev;
      }
      else
      {
        LOGE("branch_30");
        r0_i = 1;
        r6_pnode = r6_pnode->prev;
      }
    }
    // 0016854E
    TNMVert *vert_r0 = new TNMVert();
    TTSPtr<TNMVert> ptr_vert_sp160(vert_r0);
    *ptr_vert_sp160.pT_0 = *r11_pnode->m.pT_0;
    // ptr_vert_sp160.pT_0->byte_8=*r11_pnode->m.pT_0->byte_8;
    // ptr_vert_sp160.pT_0->field_9=*r11_pnode->m.pT_0->field_9;
    ptr_vert_sp160.pT_0->field_A = i_sp6c;
    
    vert_r0 = new TNMVert();
    TTSPtr<TNMVert> ptr_vert_spf8(vert_r0);
    
    *ptr_vert_sp160.pT_0 = *r6_pnode->m.pT_0;
    //ptr_vert_sp160.pT_0->field_A = 4;
    // sp70=r6_pnode
    // r8=sp108
    TTDListNode<TTSPtr<TNMVert>> *r5_dlistnode = r11_pnode;
    r0_i = 1;

    // loc_168614
   // TTDList<TTSPtr<TNMVert>> dlidt_spe8;
    // TTSPtr<TNMVert> ptr_vert_spe0;
    // TTSPtr<TNMVert> ptr_vert_spd8;
    InsertedTTDListNode<TTSPtr<TNMVert>> dlist_spe8;
    while (r0_i != 0 || r5_dlistnode != r11_pnode)
    {
      LOGE("branch_31");
      // loc_1685BA
      r5_dlistnode->m.pT_0->field_A = i_sp6c++;
      // ptr_vert_spe0 = r5_dlistnode->m;

      dlist_spe8= list_sp1f0.Insert(r5_dlistnode->m, r6_pnode->m);
      r5_dlistnode=r5_dlistnode->prev;
    }
    // 0016861C
   // TTDList<TTSPtr<TNMVert>> dlist_spf0;
    InsertedTTDListNode<TTSPtr<TNMVert>> dlist_spf0 = list_sp1f0.Insert( ptr_vert_sp160, dlist_spe8.inserted_node->m);

    list_sp1f0.Insert(ptr_vert_spf8, dlist_spf0.inserted_node->m);

    if (r9_plist)
    {
      LOGE("branch_32");
      delete r9_plist;
    }
    // loc_1686A2
    ttlist_sp1A8.Remove(r9_plist);
    r0_i = 0;
    // loc_1686BE

    r6_pnode = list_sp1f0.pLast_0;
    while (r0_i == 0 || list_sp1f0.pLast_0 != r6_pnode)
    {
      LOGE("branch_33");
      // loc_1686B2
      ConvexTest(r6_pnode);
      r6_pnode = r6_pnode->prev;
    }
  }
  // sp188 =0 len=0x9
  // 001686E6
  // loc_1686F8
  int r0_i = 0;
  TTDListNode<TTSPtr<TNMVert>> *r4_node = list_sp1f0.pLast_0;
  while (r0_i == 0 || r4_node != list_sp1f0.pLast_0)
  {
    LOGE("branch_34");
    LOGE("loc_1686EC");
    ConvexTest(r4_node);
    r4_node = r4_node->prev;
    r0_i = 1;
  }
  // 00168704
  // r4=sp108
  // r6=sp188

  TTList<TTDListNode<TTSPtr<TNMVert>> *> list_sp188;
  TTDListNode<TTSPtr<TNMVert>> *r5_node = list_sp1f0.pLast_0;
  r0_i = 0;
  // //loc_168750
  while (r0_i == 0 || r5_node != list_sp1f0.pLast_0)
  {
    LOGE("branch_35");
    // loc_16871C
    TTSPtr<TNMVert> ptr_vert_sp108 = r5_node->m;
    ptr_vert_sp108.pT_0->field_9 = 0;
    if (r5_node->m.pT_0->byte_8)
    {
      LOGE("branch_36");
      EarTest(r5_node);
      if (ptr_vert_sp108.pT_0->field_9)
      {
        LOGE("branch_37");
        list_sp188.Insert(r5_node);
      }
    }

    // loc_168746
    r5_node = r5_node->prev;
    r0_i = 1;
  }
  // 0016875A
  //[sp30]=sp130
  //[sp2c]=sp128
  //[sp28]=sp120
  //[sp24]=sp118
  //[sp20]= TTList<TTDListNode<TTSPtr<TNMVert>> *>::Remove(TTDListNode<TTSPtr<TNMVert>> *)
  int count_sp70 = 0;
  // loc_16890E
  LOGE("CFTTNavMesh::Init loc_16890E");
  while (list_sp1f0.dCount_4 > 2)
  {
    // loc_168786
  LOGE("branch_38");
    // LOGE("CFTTNavMesh::Init loc_16890E_1 list_sp1f0.dCount_4 =%x",list_sp1f0.dCount_4 );
    // LOGE(" list_sp188.count=%x", list_sp188.dCount_8);
    // LOGE(" list_sp188.pBegin_0=%x", (unsigned)list_sp188.pBegin_0);
    TTDListNode<TTSPtr<TNMVert>> *r10_pnode = list_sp188.pBegin_0->m;
    // r1 = r10_p->next;

    // sp6c = r10_p->prev;
    // sp60= r10_p->next;

    // LOGE("CFTTNavMesh::Init loc_16890E_1 next =%x prev=%x ",(unsigned)r10_pnode->next,(unsigned)r10_pnode->prev,(unsigned)r10_pnode );

    TTSPtr<TNMVert> ptr_vert_sp168 = r10_pnode->next->m;
    TTSPtr<TNMVert> ptr_vert_sp160 = r10_pnode->m;
    TTSPtr<TNMVert> ptr_vert_spf8 = r10_pnode->prev->m;
    // LOGE("CFTTNavMesh::Init loc_16890E_9");
    // 001687CC
    TNMTri nmtri_sp108;
    // memset(&nmtri_sp108, 0,sizeof(nmtri_sp108));
    nmtri_sp108.filed_C = 0;
    nmtri_sp108.filed_28 = 0;
    nmtri_sp108.filed_2C = 0;
    nmtri_sp108.filed_30[0] = 0;
    nmtri_sp108.filed_30[1] = 0;
    nmtri_sp108.filed_30[2] = 0;
    nmtri_sp108.filed_30[3] = 0;
    nmtri_sp108.filed_34 = 0;
    // r7=sp168.pt0
    nmtri_sp108.filed_10[0].x_0 = 0;
    nmtri_sp108.filed_10[0].y_4 = 0;
    nmtri_sp108.filed_10[1].x_0 = 0;
    nmtri_sp108.filed_10[1].y_4 = 0;
    // sp5c=sp13a
    //  sp40 = ptr_vert_sp168.pT_0->field_0
    nmtri_sp108.filed_10[0].x_0 = ptr_vert_sp168.pT_0->field_0;
    nmtri_sp108.filed_10[0].y_4 = ptr_vert_sp168.pT_0->field_4;
    nmtri_sp108.filed_10[1].x_0 = ptr_vert_sp160.pT_0->field_0;
    nmtri_sp108.filed_10[1].y_4 = ptr_vert_sp160.pT_0->field_4;
    nmtri_sp108.filed_10[2].x_0 = ptr_vert_spf8.pT_0->field_0;
    nmtri_sp108.filed_10[2].y_4 = ptr_vert_spf8.pT_0->field_4;
    nmtri_sp108.filed_10[3].y_4 = (ptr_vert_sp168.pT_0->field_4 + ptr_vert_sp160.pT_0->field_4 + ptr_vert_spf8.pT_0->field_4) / 3;
    nmtri_sp108.filed_10[3].x_0 = (ptr_vert_sp168.pT_0->field_0 + ptr_vert_sp160.pT_0->field_0 + ptr_vert_spf8.pT_0->field_0) / 3;
    // sp70=0
    // sp140 byte=0
    nmtri_sp108.filed_30[0] = ptr_vert_sp168.pT_0->field_A;
    nmtri_sp108.filed_30[1] = ptr_vert_sp160.pT_0->field_A;
    nmtri_sp108.filed_30[2] = ptr_vert_spf8.pT_0->field_A;
    // LOGE("CFTTNavMesh::Init loc_16890E_11");
    vector_nmtri_0.Insert(nmtri_sp108);
    //  LOGE("CFTTNavMesh::Init loc_16890E_10");
    //这里是0016886C
    // LOGE("CFTTNavMesh::Init loc_16890E_8_1");
    list_sp188.Remove(r10_pnode);
    // LOGE("CFTTNavMesh::Init loc_16890E_8_2");
    list_sp188.Remove(r10_pnode->next);
    // LOGE("CFTTNavMesh::Init loc_16890E_8_3");
    list_sp188.Remove(r10_pnode->prev);
    // LOGE("CFTTNavMesh::Init loc_16890E_8_4");
    list_sp1f0.Remove(ptr_vert_sp160);
    //  LOGE("CFTTNavMesh::Init loc_16890E_8_5");
    ptr_vert_sp168.pT_0->field_9 = 0;
    if (ptr_vert_sp168.pT_0->byte_8 == 0)
    {
      LOGE("branch_39");
      // 001688A6
      // LOGE("CFTTNavMesh::Init loc_16890E_2");
      ConvexTest(r10_pnode->next);
    }
    if (ptr_vert_sp168.pT_0->byte_8)
    {
      LOGE("branch_40");
      // loc_1688B2
      EarTest(r10_pnode->next);
      if (ptr_vert_sp168.pT_0->field_9)
      {
        LOGE("branch_41");
        //  LOGE("CFTTNavMesh::Init loc_16890E_3");
        list_sp188.Insert(r10_pnode->next);
      }
      //  LOGE("CFTTNavMesh::Init loc_16890E_4");
    }
    // loc_1688C6
    // r9=1
    ptr_vert_spf8.pT_0->field_9 = 0;
    if (ptr_vert_spf8.pT_0->byte_8 == 0)
    {
      LOGE("branch_42");
      // 001688DC
      // LOGE("CFTTNavMesh::Init loc_16890E_5");
      ConvexTest(r10_pnode->prev);
    }
    if (ptr_vert_spf8.pT_0->byte_8)
    {
      LOGE("branch_43");
      // loc_1688E8
      // LOGE("CFTTNavMesh::Init loc_16890E_7");
      EarTest(r10_pnode->prev);
      if (ptr_vert_spf8.pT_0->field_9)
      {
        LOGE("branch_44");
        //  LOGE("CFTTNavMesh::Init loc_16890E_6");
        list_sp188.Insert(r10_pnode->prev);
      }
    }
    // loc_1688FC

    count_sp70++;
    // LOGE("CFTTNavMesh::Init loc_16890E_1 list_sp1f0.dCount_4 =%x",list_sp1f0.dCount_4 );
  }
  // 00168918
  uint8 r9_u8 = 0;
  uint8 r10_u8 = 1;
  // loc_168A6C
 // LOGE("CFTTNavMesh::Init loc_168A6C");
  uint8 u8_sp108[10];
  // loc_168928
  TNMTri *r7_pnmtri = &vector_nmtri_0.pBuf_0[r9_u8];
  int sp6c = r10_u8;
  //LOGE("udInsertPos_8=%x",vector_nmtri_0.udInsertPos_8);
  while (vector_nmtri_0.udInsertPos_8 > r9_u8)
  { // check dInsertPos_8 是否是无符号数 dInsertPos_8改为数组最后一个下标更好一点
    // loc_168928
    LOGE("loc_168928");
    //LOGE("branch_45");
    //   loc_168A4E
    uint r11_u = r9_u8 + 1;
    while (vector_nmtri_0.udInsertPos_8 > r11_u)
    {
      LOGE("branch_46");
      // loc_16893E
      // loc_16897E
      // lr= &vector_nmtri_0.pBuf_0;
      uint8 r6_u8 = 0;
      for (int r5_i = 0; r5_i != 3; r5_i++)
      {
        LOGE("branch_47");
        // loc_16895A
        // loc_168978
        for (int r2_i = 0; r2_i != 3; r2_i++)
        {
          LOGE("branch_48");
          // loc_168964
          if (vector_nmtri_0.pBuf_0[r10_u8].filed_30[r2_i] == r7_pnmtri->filed_30[r5_i])
          {
            LOGE("branch_49");
            // 0016896A
            u8_sp108[r6_u8] = r5_i;
            u8_sp108[r6_u8 + 2] = r2_i;
            //LOGE("r6_u8=%xu8_sp108[r6_u8]=%xu8_sp108[r6_u8 + 2] = %x",r6_u8,u8_sp108[r6_u8],u8_sp108[r6_u8 + 2] );
            r6_u8++;
          }
        }
      }
      TNMTri *r5_pnmtri = &vector_nmtri_0.pBuf_0[r11_u];
     // LOGE("r6_u8=%xu8_sp108[r6_u8]=%x",r6_u8,u8_sp108[r6_u8]);
      if (r6_u8 == 2)
      {
        LOGE("branch_50");
        // 00168988
        if (u8_sp108[0] == 2)
        {
          LOGE("branch_51");
          // loc_1689AC
          if (u8_sp108[1] == 1)
            r11_u = 1;
          else
            r11_u = 2;
        }
        else if (u8_sp108[0] == 1)
        {
          LOGE("branch_52");
          // loc_1689C0
          r11_u = 2;
          if (u8_sp108[1] == 2)
            r11_u = 1;
          if (u8_sp108[1] == 0)
            r11_u = 0;
        }
        else if (u8_sp108[0] || u8_sp108[1] != 1)
        {
          LOGE("branch_53");
          // loc_1689BA
          r11_u = 2;
        }
        else
          r11_u = 0;
        // loc_1689D6
        if (u8_sp108[2] == 2)
        {
          LOGE("branch_54");
          // loc_1689F0
          if (u8_sp108[3] == 1)
            r6_u8 = 1;
          else // loc_1689FC
            r6_u8 = 2;
        }
        else if (u8_sp108[2] == 1)
        {
          LOGE("branch_55");
          // loc_168A00
          r6_u8 = 2;
          if (u8_sp108[3] == 2)
            r6_u8 = 1;
          if (u8_sp108[3] == 0)
            r6_u8 = 0;
        }
        else if (u8_sp108[2] || u8_sp108[3] != 1)
        {
          LOGE("branch_56");
          // loc_1689FC
          r6_u8 = 2;
        }
        else
          r6_u8 = 0;
        // loc_168A12
        count_sp70 = (((count_sp70 & 0xFFFF0000) | r11_u) | (r6_u8 * 256));
        TNMNeighbour r5r2_neighbour;
        r5r2_neighbour.field_0 = r5_pnmtri;
        r5r2_neighbour.field_4 = count_sp70;
        r7_pnmtri->vector_nmneighbour_0.Insert(r5r2_neighbour);
        // vector_nmtri_0.pBuf_0[r11_u]
        TNMNeighbour r7r2_neighbour;
        r7r2_neighbour.field_0 = r7_pnmtri;
        r7r2_neighbour.field_4 = (((((uint)&list_sp188) & 0xffff0000) | (r11_u * 256)) | r6_u8);
        r5_pnmtri->vector_nmneighbour_0.Insert(r7r2_neighbour);
       // LOGE("insert");
      }
      // loc_168A4A
      r10_u8++;
      r11_u++;
    }
    r10_u8 = sp6c;
    r10_u8++;
    r9_u8++;
  }
  //
 // list_sp108.pLast_0=0;
  LOGE("end");
}
//---------------------------------------------------------------------------------------
// 00168F60
void CFTTNavMesh::Pathfind(fpoint2<PointType_14>, fpoint2<PointType_14>,
                           TTList<fpoint2<PointType_14>> &) {}
//---------------------------------------------------------------------------------------

TNMTri::TNMTri()
{
}
//这里引用的数据太少  实际拷贝的数据一样测试1-3
// 00168e16 ^_-
//单元测试等级: 目测游戏表现
//单元测试内容: 1-3关
//单元测试结果: 正常
//已测试分支
TNMTri::TNMTri(TNMTri const &r1_other) : vector_nmneighbour_0(r1_other.vector_nmneighbour_0)
{
  LOGE("TNMTri");
  // vector_nmneighbour_0 = r1_other.vector_nmneighbour_0;
  filed_C = r1_other.filed_C;
  for (int r2_i = 0; r2_i != 3; r2_i++)
  {
    // loc_168E2A
    filed_10[r2_i] = r1_other.filed_10[r2_i];
  }
  filed_28 = r1_other.filed_28;
  filed_2C = r1_other.filed_2C;
  filed_30[0] = r1_other.filed_30[0];
  filed_30[1] = r1_other.filed_30[1];
  filed_30[2] = r1_other.filed_30[2];
  filed_30[3] = r1_other.filed_30[3];
  filed_34 = r1_other.filed_34;
  filed_38 = r1_other.filed_38;
}

fpoint2<PointType_14> TNMTri::Inside(fpoint2<PointType_14> a1, bool a2)
{
  fpoint2<PointType_14> test;
  test.x_0 = a1.x_0 + 1;
  LOGE("%x%x%x", a1.x_0, a2, a1.y_4);
  return test;
}
