
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using nativeCom;
using zyxAdapter;
using Game.Data;

[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct strDataChange{
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=8)]
	public int[]  dowhat;//0
	public int askCnt;//1
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=1000)]
	public int[]  ask;//2
	public int rltCnt;//3
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=1000)]
	public int[]  rlt;//4
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=1000)]
	public sbyte[] ask_bts;//5
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=1000)]
	public byte[] rlt_bts;//6
}

[Serializable]
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct Tr_Paixing_z1{
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=5)]
	public Tr_Que pq;
	public 	 Tr_Dui dui;
}
[Serializable]
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct Tr_Paixing_z2{
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=7)]
	public Tr_Dui dui7;
}

[Serializable]
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct Tr_Paixing {
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=20)]
	public byte[] bak;
	public  byte UseJing;
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=22)]
	public Tr_Pai[]  Pai_StrAll; //全部牌，含杠第4张
	
	public   sbyte PaiCntAll; //全部牌数量
	
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=18)]
	public   sbyte[] str_Pai; //手牌+零牌   居然没用上???  1下标开始
	
	public    int TingCount;//听数量
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=17)]
	public  TTing[] TingArr;
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=3)]
	public    sbyte[] JingCnt; //正、副精，全部精

	public  int LX ;//-1不听 0听牌 >0胡牌[1平和 2七小队 3十三烂  4国士无双   8标准烂（147 258 369）]
	//      1: (pq: Tr_Ques; dui: Tr_Dui);
	//      2: (dui7: array[0..6] of Tr_Dui); //由大到小
	//      //3十三烂
	//      //4国士无双
	//      //8标准烂 147 258 369
}



[Serializable]
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct  tcls_mem_dll_agame{//麻将参数----每次开局
	
	public int a;
	public Tr_MJGZ gz;
	public sbyte isLibrary;//0exe测试 1网络DLL
	public sbyte isSifang;
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=50)]
	public byte[]  gzcs_big;	//前50个通用
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=50)]
	public byte[] gzcs_friend;//: array[0..49] of Byte;
}


[Serializable]
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct  tcls_mem_dll_main{//宿主麻将参数---初始化一次
	public int a;
	
	
	
	
	
	
}


[Serializable]
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct  tcls_mem_dll {//麻将参数---初始化一次
	public int huFanCnt;
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]
	public int[]  huFan_idx;
	public int hufanCnt_changeBase; //0..99 哪些被DLL替换了    ********新增
	[MarshalAs(UnmanagedType.ByValArray,SizeConst=40)]
	public int[] hupan_dll_changeBase;
	static	public void init(ref tcls_mem_dll t){
		t.huFan_idx=new int[100];
		t.hupan_dll_changeBase=new int[40];
	}
}


[Serializable]
[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi/*,Pack=1*/)]
public struct  tcls_mem_dll_main_agame{//宿主麻将参数---每次开局
	public int a;
	public Tr_MJGZ gz;
}

public   class mjbase{
	public tcls_mem_dll cs_dll;//DLL参数
	public tcls_mem_dll_main  cs_main;//主参数
	
	private tcls_mem_dll_agame cs_dll_agame;//DLL参数
	public tcls_mem_dll_main_agame  cs_main_agame;//主参数
	
	public	int[] fanidx;
	
	/// <summary>
	/// 所有牌
	/// </summary>
	public static readonly byte[] paiAll = new byte[]{
		11,12,13,14,15,16,17,18,19,
		31,32,33,34,35,36,37,38,39,
		51,52,53,54,55,56,57,58,59,
		71,72,73,74,77,78,79
	};
	
	/// <summary>
	/// 根据牌拿位置
	/// </summary>
	/// <param name="pai"></param>
	/// <returns></returns>
	public int getPaiIdx(int pai){
		int shi = pai / 10;
		if(shi < 7)
			return shi/2*9+pai%10-1;
		int len = paiAll.Length;
		for(int i=27;i<len;i++){
			if(paiAll[i] == pai)
				return i;
		}
		
		log("大错误" + pai);
		return 0;
	}
	
	strDataChange dc=new strDataChange();
	ABlock  ab_mjfun=new ABlock();
	unsafe int*[] intptr_dc=new int*[10];
	
	/// <summary>
	/// 胡牌修正
	/// </summary>
	/// <param name="pk"></param>
	/// <param name="fan"></param>
	/// <param name="hpr"></param>
	/// <param name="fangpaonew"></param>
	/// <param name="lingpai"></param>
	unsafe public   virtual void cal_hupai(ref tr_pack_dapai pk,ref Tr_faninfo fan,int hpr,int fangpaonew,int lingpai,int memoryAddress,int style/*0表示汇总前的修正 1表示汇总后的修正*/){
	}
	
	/// <summary>
	/// 计算胡番 奖励番 及胡的类型
	/// </summary>
	/// <param name="pk"></param>
	/// <param name="px"></param>
	/// <param name="addr_pxz"></param>
	/// <param name="fan"></param>
	/// <param name="hpr"></param>
	/// <param name="fangpaonew"></param>
	/// <param name="lingpai"></param>
	unsafe public   virtual void calFan_hupai(ref tr_pack_dapai pk,ref Tr_Paixing  px,IntPtr addr_pxz,ref Tr_faninfo fan,int hpr,int fangpaonew,int lingpai,int memoryAddress){
	}
	
	/// <summary>
	/// 补充小番种 100..199
	/// </summary>
	/// <param name="pk"></param>
	/// <param name="px"></param>
	/// <param name="fanIdx 番索引"></param>
	/// <param name="hpr 谁"></param>
	unsafe public   virtual int calFan_world(ref tr_pack_dapai pk,ref Tr_Paixing  px,int fanIdx,int hpr){
		int r= -4444;	//这是取底层
		return r;
		if (fanIdx>99)r=0;	//这是我要计算的
		
		else
			//这是覆盖底层
			switch(r){
				case 20:r=0;
				case 34:r=0;
				break;
		}
		return r;
	}
	
	public unsafe virtual void sendPackFunc(ref tr_pack_dapai pk,int address){}
	
	/// <summary>
	/// AI出牌
	/// </summary>
	/// <param name="pk"></param>
	/// <returns></returns>
	public virtual int chupai(ref tr_pack_dapai pk){
		return -1;
	}
	
	public void log(string str){
		dcEvent_zyx.Instance.log(str);
	}
	/// <summary>
	/// 设置番idx
	/// </summary>
	public virtual void setfanidx(){
	}
	/// <summary>
	/// 设置规则
	/// </summary>
	/// <param name="now"></param>
	public virtual void setGz(ref tcls_mem_dll_agame now,int memoryAddress){
	}
	
	unsafe public  int initDll_agame(ref tcls_mem_dll_main_agame  cs_main_,ref  tcls_mem_dll_agame cs_dll_,ref ABlock ab,int memoryAddress) {
		cs_main_agame=cs_main_;
		cs_dll_agame=cs_dll_;
		
		//cs_dll_agame.gz = cs_main_.gz;
		
		//log("设置西啊伤心啊上半场卡号");
		
		setGz(//ref cs_main_agame.gz,
		      ref cs_dll_agame,memoryAddress);
		
		log("xxxss "+cs_dll_agame.gz.dungaoCnt+"  "+ab.len+"  "+cs_main_agame.gz.dungaoCnt+"  "+cs_main_agame.gz.score_gang_jing[1]);
		funs.instance.memToNative_bytes(funs.instance.StructToBytes(cs_dll_agame),ref ab,0);
		//log("sdlkjf029384");
		return 1;
	}
	
	unsafe void fixpt(){
		
		fixed(int* ppp=&dc.dowhat[0]){
			intptr_dc[0]=ppp;
			
		}
		fixed(int* ppp=&dc.askCnt){
			intptr_dc[1]=ppp;
			
		}
		fixed(int* ppp=&dc.ask[0]){
			intptr_dc[2]=ppp;
			
		}
		fixed(int* ppp=&dc.rltCnt){
			intptr_dc[3]=ppp;
			
		}
		fixed(int* ppp=&dc.rlt[0]){
			intptr_dc[4]=ppp;
			
		}
		fixed(sbyte* ppp=&dc.ask_bts[0]){
			intptr_dc[5]=(int*)ppp;
			
		}
		fixed(byte* ppp=&dc.rlt_bts[0]){
			intptr_dc[6]=(int*)ppp;
			
		}
	}
	unsafe public  int initDll(ref tcls_mem_dll_main  cs_main_,ref  tcls_mem_dll cs_dll_,ref ABlock ab,int addr_mj) {
		addr_mjfun=addr_mj;
		
		dc.ask=new int[1000];
		dc.rlt=new int[1000];
		dc.dowhat=new int[8];
		dc.ask_bts=new sbyte[1000];
		dc.rlt_bts=new byte[1000];
		fixpt();
		ab_mjfun.pt=(int*)addr_mjfun;
		
		cs_main=cs_main_;
		cs_dll=cs_dll_;
		setfanidx();
		
		Buffer.BlockCopy(fanidx,0,cs_dll.huFan_idx,0,fanidx.Length<<2);
		cs_dll.huFanCnt=fanidx.Length;
		
		funs.instance.memToNative_bytes(funs.instance.StructToBytes(cs_dll),ref ab,0);
		
//		byte[] bt=funs.instance.StructToBytes(cs_dll);
//		int* addr=myCom_zyx.Instance.getPointerOfBytes(bt);
//		myCom_zyx.Instance.move2Process(addr,new IntPtr( ab.pt),bt.Length);
		
		
		mainDo("测试",new int[]{5,6},new sbyte[]{31,54});
		log("ceshi..."+getvalue_int(0)+"   "+getvalue_byte(3));
		return cs_dll.huFanCnt;
	}
	
	public int getvalue_int(int idx){
		return dc.rlt[idx];
		
	}
	public int[] getvalue_intArr(int idx,int len){
		if (idx==-1)
		return dc.rlt;else
		{
			int[] r=new int[len];
			Array.Copy(dc.rlt,idx,r,0,len);
			return r;
		}
		
	}
	public byte getvalue_byte(int idx){
		return dc.rlt_bts[idx];
		
	}
	public byte[] getvalue_byteArr(int idx,int len){
		if(idx==-1)return dc.rlt_bts;else
		{
			byte[] r=new byte[len];
			Array.Copy(dc.rlt_bts,idx,r,0,len);
			return r;
		}
		
	}
	
	
	protected int addr_mjfun;//宿主mjfun
	unsafe  protected void mainDo(string str, int[] ask,sbyte[] bts){
		
		int[] dow=new int[8];
		
		switch(str){
				case "测试": dow[0]=0;  break;
				case "门清": dow[0]=1;  break;
				case "零牌": dow[0]=2; break;
				case "零牌类型": dow[0]=3; break;//零牌类型 0自摸 1放炮
				case "垃圾牌":dow[0]=10;break;
				case "剩余牌":dow[0]=11;break;
				case "钱娄子": dow[0]=50;break;
				case "临时番": dow[0]=51;break;
				case "上把庄家":dow[0]=52;break;
				case "庄家座位":dow[0]=53;break;
				case "庄家":dow[0]=54;break;
				
				case "增加杠":dow[0]=55;break;
				case "删除杠":dow[0]=56;break;
				case "牌是否杠":dow[0]=57;break;
		}
		
		Array.Copy(dow,dc.dowhat,dow.Length);
		if(ask!=null)
			Array.Copy(ask,dc.ask,ask.Length);
		if( bts!=null)
			Array.Copy(bts,dc.ask_bts,bts.Length);
		//
		ab_mjfun.sty=56;
		ab_mjfun.len=678;
		ABlock xb=new ABlock();
		xb.pt=null;
		string sx="";
		string rlt=null;//!!!
		tr_rltpchar sdfsdf=new tr_rltpchar ();
		//fixpt();
		int len=intptr_dc.Length;
		for(int i=0;i<len;i++)sx+= ((int)(int*)intptr_dc[i]).ToString()+'^';
		dcEvent_zyx.Instance.TestGameFun(
//		myCom_zyx.Instance. doMsgNative_(
			"1",
			//new int*[]{ intptr_dc[0],intptr_dc[1],  	intptr_dc[2],intptr_dc[3],intptr_dc[4],intptr_dc[5]}
			sx //  intptr_dc
			,ref sdfsdf
			,ref ab_mjfun ,ref xb);
		
		rlt=null;//!!!!
		
		
	}
}