#include "Momery.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <ctype.h>
//#include <cs.h>
#include "MemoryTools.h"

#define PI 3.141592653589793238
#include <ctype.h>
#include <pthread.h>			// for thread(多线程)
#include <arpa/inet.h>			// 通讯(2)
/*
#define SERVER_PORT 6666		// 通讯(1)
 int clientSocket = 0;			// 通讯辨识

void Connectcommunication()
{
	struct sockaddr_in serverAddr;
	if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("socket创建失败");
		exit(1);
	}
	else
	{
		serverAddr.sin_family = AF_INET;
		serverAddr.sin_port = htons(SERVER_PORT);
		serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	}
	if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
	{
		perror("未连接到服务端");
		exit(1);
	}
	else
	{
		printf("成功连接服务端\n");
	}
}
*/

float MatrixValue[16] = { 0 };
FILE * fw;
FILE *wz;
float fww, fwww, wzz, wzzz, tz;
float zjiaodu = 180;
float zhudu =180;
float camear_z, r_x, r_y, r_w;
float camear_zz, r_xx, r_yy, r_ww;
float hd, z_x, z_z, z_y, obj_x, obj_z, obj_y, zm_x, zm_z, zm_y, angley, zm, zm1, zm2, jio, jioo;
double M, jl, jll;
int kh, kj, yl;
int rs;
int zsteam, objteam, hp;

int main(int argc, char **argv)
{
	
	remoteSwitch();
	ipid = getPID("com.sofunny.Sausage");
	char RangeValue[10240];
	char AdjustH[10240];
	char mname[] = "libunity.so";
	long int libbase = get_module_base(ipid, mname);
	char nname[] = "libil2cpp.so";
	long int libbase2 = get_module_base(ipid, nname);//这里是填模块的地方
	FILE *F;
	while ((F = fopen("/sdcard/zmstop", "r")) == NULL)
	{
		fw = fopen("/sdcard/fw", "r");
		fscanf(fw, "%f", &fww);
		fwww = fww;
		fclose(fw);
		wz = fopen("/sdcard/wz", "r");
		fscanf(wz, "%f", &wzz);
		wzzz = wzz;
		fclose(wz);		
		memset(RangeValue, 0, 10240);
		memset(AdjustH, 0, 10240);
		char aaa[40960];
		char b[30720];
		/*
		long int onesz = getZZ(libbase + 0xFDDA94);//第一级指针
			//printf("矩阵地址=%xl\n", onesz);
			long int twosz = getZZ(onesz + 0x4);//第二级指针
			long int ssz = getZZ(twosz + 0x4);//第三级指针
			long int fsz = getZZ(ssz + 0x8);     //第四级指针0x18
			long int center = getZZ(getZZ(fsz + 0x18) +0x2C);	
			long int sz = getZZ(center + 0x8) + 0x10;//到数组头
		//	printf("数组列表=%xl\n", sz);		
			long int count = getDword(center + 0xC);//遍历数量
		//	printf("矩阵地址=%xl\n", count);
			long int oneself = getZZ(sz + 0x0);
		//	printf("矩阵地址=%xl\n", oneself);
			long int team = getDword(oneself + 0xF0);//遍历数量
			long int Matrix = getZZ(getZZ(getZZ(getZZ(getZZ(libcase +  0xFDB3A8) + 0x5C) + 0x4) + 0x1C) + 0x8) + 0xC4;
	il2cpp 0x7406B00+0x5C+0x128+4C

Zxx
long int object = getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(objaddrzz + 0xD8) + 0x10) + 0x8) + 0x1C) + 0x1C) + 0x14) + 0x28) + 0x14);//敌人坐标
				//敌人坐标
				float z_x = getFloat(object + 0x60);//队伍坐标
				float z_z = getFloat(object + 0x64);//队伍坐标
				float z_y = getFloat(object + 0x68);//队伍坐标
				float obj_x = getFloat(object + 0x60);
				float obj_z = getFloat(object + 0x64)+0.7;
				float obj_y = getFloat(object + 0x68);
		
			
					*/
         	long Matrix = getZZ(getZZ(getZZ(getZZ(getZZ(libbase + 0xFDB3A8)+ 0x5C)+ 0x4) + 0x1C)+0x8) + 0xC4;//矩阵
			long Uworld = getZZ(getZZ(getZZ(getZZ(getZZ(libbase +0xFDDA94) + 0x4) +0x4) +0x8) +0x18);//数组
	       long int Uleve1 = getZZ(Uworld + 0x2C);//center
	       long int Uleve = getZZ(Uleve1 + 0x8);
		   long int Arrayaddr = Uleve + 0x10;//到这里是数组地方，所以不用getZZ了
	   	long int count = getDword(Uleve + 0xC);
	   	long int oneself = getZZ(Arrayaddr + 0x0);
	
	  long int panduan = getZZ(getZZ(getZZ(libbase2+0x7406B00)+0x5C)+0x128);
	  long int zhunxing = getZZ(getZZ(getZZ(libbase2+0x7406B00)+0x5C)+0x128);//这个也一样

        if (panduan != 0)
		{
		long int ego_zx = zhunxing + 0x4C;//这里也一样
		long int ego_zx1 = zhunxing + 0x54;//这里就是x
		long int ego_zy = zhunxing + 0x58;//这里就是y
		int zsteam = getDword(oneself + 0x100);
   	   kj = getDword(ego_zx + 0x2E8);//这里是开镜偏移
		//kh = getDword(zhunxing + 0x2C4);
		
		memset(aaa, 0, 2048);
		rs = 0;
		for (int i = 0; i < 16; i++)
		{
			float matrixaddr = getFloat(Matrix + i * 4);
			matrix[i] = matrixaddr;
		}
		
		for (int i = 0; i < count; i++)
		{
			long int objaddrzz = getZZ(Arrayaddr + 4 * i);
			long int zszuobiao = getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(oneself + 0xD8) + 0x10) + 0x8) + 0x1C) + 0x1C) + 0x14) + 0x28) + 0x14);
			long int objzuobiao = getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(getZZ(objaddrzz + 0xD8) + 0x10) + 0x8) + 0x1C) + 0x1C) + 0x14) + 0x28) + 0x14);

     int dw = getDword(objaddrzz + 0x100);//自身队伍
 	int ai = getDword(objaddrzz + 0x100);//
	
			int hp = getFloat(objaddrzz + 0x284);//这里是血量偏移
	
	    //	int dw = getDword(objaddrzz + 0xFC);//自身队伍
		if (getDword(objaddrzz + 0xFC) != 0)	// 去队友和自身
      	   	{
			continue;
			}

	    if (getFloat(objaddrzz+0x284) == 0.0)	// 去除血量为0
	     	{
			continue;
        	}
        	
         if (hp > 100)
            {
			continue;
			}

			z_x = getFloat(zszuobiao + 0x60);//这里是zb
			z_z = getFloat(zszuobiao + 0x64);
			z_y = getFloat(zszuobiao + 0x68);
			
			obj_x = getFloat(objzuobiao + 0x60);
			obj_z = getFloat(objzuobiao + 0x64);
			obj_y = getFloat(objzuobiao + 0x68);

			zm_x = getFloat(objzuobiao + 0x60);
			zm_z = getFloat(objzuobiao + 0x64)+tz;
			zm_y = getFloat(objzuobiao + 0x68);

			if (wzz == 0)
			{

				tz = 0;

			}

			if (wzz == 1)
			{

				tz = -0.5;

			}

			if (wzz == 2)
			{

				tz = 1.0;

			}
			
			

		/*	objteam = getDword(objaddrzz + 0xE8);

			if (objteam == zsteam)
			{
				continue;
			}

			hp = getFloat(objaddrzz + 0x274);
			if (hp == 0)
			{
				continue;
			}
*/
		
			
				
					
			camear_z =
				matrix[3] * obj_x + matrix[7] * obj_z + matrix[11] * obj_y + matrix[15];
				
			M = camear_z / 1;
			
			
			
	
			r_x =
				px + (matrix[0] * obj_x + matrix[4] * obj_z + matrix[8] * obj_y +
					  matrix[12]) / camear_z * px;

			r_y =
				py - (matrix[1] * obj_x + matrix[5] * (obj_z) + matrix[9] * obj_y +
					  matrix[13]) / camear_z * py;

			r_w =
				py - (matrix[1] * obj_x + matrix[5] * (obj_z + 1.8) + matrix[9] * obj_y +
					  matrix[13]) / camear_z * py;

			camear_zz = matrix[3] * obj_x + matrix[7] * obj_z + matrix[11] * obj_y + matrix[15];

			if (camear_zz < 0)
			{
				continue;
			}

			jio = camear_zz / 1;

			if (jio > 400)
			{
				continue;
			}

			r_xx =
				px + (matrix[0] * obj_x + matrix[4] * (obj_z) + matrix[8] * obj_y +
					  matrix[12]) / camear_zz * px;

			r_yy =
				py - (matrix[1] * obj_x + matrix[5] * (obj_z) + matrix[9] * obj_y +
					  matrix[13]) / camear_zz * py;

			r_ww =
				py - (matrix[1] * obj_x + matrix[5] * (obj_z + 1.8) + matrix[9] * obj_y +
					  matrix[13]) / camear_zz * py;


           	if (zm_x > z_x && zm_y > z_y)
			{

				hd = (float)(PI / 2 - atan2(zm_y - z_y, zm_x - z_x));

			}
			else if (zm_x < z_x && zm_y > z_y)
			{

				hd = (float)(PI + atan2(zm_y - z_y, z_x - zm_x) + PI / 2);

			}
			else if (zm_x < z_x && zm_y < z_y)
			{

				hd = (float)(PI / 2 - atan2(z_y - zm_y, z_x - zm_x) + PI);

			}
			else
			{
				hd = (float)(atan2(z_y - zm_y, zm_x - z_x) + PI / 2);

			}
			if (zm_z > z_z)
			{

				angley = (float)atan2((z_z - zm_z), M);
			}
			else if (zm_z < z_z)
			{

				angley = (float)0 - atan2((zm_z - z_z), M);
			}
				
			float pmjl = sqrt(pow(r_x - px, 2) + pow(r_y - py, 2));

			if (pmjl > fwww)
			{
				continue;
			}
			
			rs++;
			if (rs == 1 || pmjl <= jll)
			{
				jll = pmjl;
				zm = sin(hd / 2);
				zm1 = cos(hd / 2);
				zm2 = sin(angley / 2);
			}

		}
		
		if (kj != yl)
		{
		if (rs>0)
		{
			writeFloat(ego_zx, zm);
			writeFloat(ego_zx1, zm1);
			writeFloat(ego_zy, zm2);
		}
		}
		yl = kj;
		
		
			if (kh != yl)
		{
		if (rs>0)
		{
			writeFloat(ego_zx, zm);
			writeFloat(ego_zx1, zm1);
			writeFloat(ego_zy, zm2);
		}
		}
		
		}
		
		
	}
	remove("/sdcard/zmstop");
	return 0;
	fclose(F);
}