⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multi.c

📁 小型游戏引擎
💻 C
📖 第 1 页 / 共 3 页
字号:
			for(i=0;i<messleng;i++) bufptr[i] = syncbuf[i+1];			if ((comrateoption&15) > 0)			{				i = getcrc(bufptr,messleng);				updatecrc16(i,syncbuf[0]);				if (((unsigned short)i) != ((long)syncbuf[syncbufleng-2])+((long)syncbuf[syncbufleng-1]<<8))					bad |= 2;   //CRC error			}			syncbufleng = 0;			if (bad != 0)			{					//Don't send reset again if outbufplc is not before incnt!				if ((bad == 1) && ((((syncbuf[0]&127)-(*incnt))&127) >= 124))				{					bad = 0;					continue;				}				bad = 0;				if (comreset != 0) comreset--;				if (((*comerror)|comreset) == 0)				{					*comerror = 2; comreset = 2;					startcom();				}				continue;			}			*incnt = (((*incnt)+1)&127);			if ((messleng > 0) && (bufptr[0] >= 200))  //200-255 are messages for engine's use only				processreservedmessage(messleng,bufptr);			comescape = 0; comreset = 0; *comerror = 0;			return(messleng);		}		return(0);	}	else	{		if (*netinbufplc == *netinbufend) return(0);		messleng = (short)netinbuf[*netinbufplc] + (((short)netinbuf[((*netinbufplc)+1)&(COMBUFSIZ-1)])<<8);		for(i=0;i<messleng;i++)			getmess[i] = netinbuf[((*netinbufplc)+i+2)&(COMBUFSIZ-1)];		k = 0; *otherconnectindex = getmess[k++];		for(totbad=0;totbad<NETBACKPACKETS;totbad++)   //Number of sub-packets per packet		{			toindex = getmess[k++];			if (toindex == 0xfe)			{				netincnt[*otherconnectindex] = 0;  // (>= 200) && ( <= 254)				submessleng = messleng-2;          // Submessleng not necessary			}			else			{				if (toindex == 0xff)				{					for(i=connecthead;i>=0;i=connectpoint2[i])						if (i != *otherconnectindex)						{							if (i == myconnectindex) j = getmess[k];							k++;						}				}				else					j = getmess[k++];				if (j != netincnt[*otherconnectindex])				{					submessleng = (short)getmess[k]+(((short)getmess[k+1])<<8);					k += submessleng+2;					continue;				}				netincnt[*otherconnectindex]++;				submessleng = (short)getmess[k]+(((short)getmess[k+1])<<8); k += 2;			}			for(i=0;i<submessleng;i++) bufptr[i] = getmess[k++];			if (totbad == 0)			{					//Increment inbufplc only if first sub-message is read				*netinbufplc = (((*netinbufplc)+messleng+2)&(COMBUFSIZ-1));				if ((submessleng > 0) && (bufptr[0] >= 200)) //200-255 are messages for engine's use only				{					if (bufptr[0] >= 253)					{						processreservedmessage(submessleng,bufptr);						if ((bufptr[0] == 253) || (bufptr[0] == 254)) return(0);					}					return(submessleng);				}			}			if (*otherconnectindex == myconnectindex) return(0);			return(submessleng);   //Got good packet		}		syncstate++;   //DON'T WANT TO GET HERE!!!			//Increment inbufplc to make it not continuously screw up!		*netinbufplc = (((*netinbufplc)+messleng+2)&(COMBUFSIZ-1));	}	return(0);}void initcrc(void){	long i, j, k, a;	for(j=0;j<256;j++)      //Calculate CRC table	{		k = (j<<8); a = 0;		for(i=7;i>=0;i--)		{			if (((k^a)&0x8000) > 0)				a = ((a<<1)&65535) ^ 0x1021;   //0x1021 = genpoly			else				a = ((a<<1)&65535);			k = ((k<<1)&65535);		}		crctable[j] = (a&65535);	}}long getcrc(char *buffer, short bufleng){	long i, j;	j = 0;	for(i=bufleng-1;i>=0;i--) updatecrc16(j,buffer[i]);	return(j&65535);}void installbicomhandlers(void){#ifdef PLATFORM_DOS		union REGS r;	struct SREGS sr;	long lowp;	void far *fh;		//Get old protected mode handler	r.x.eax = 0x3500+comvect;   /* DOS get vector (INT 0Ch) */	sr.ds = sr.es = 0;	int386x(0x21,&r,&r,&sr);	orig_pm_sel = (unsigned short)sr.es;	orig_pm_off = r.x.ebx;		//Get old real mode handler	r.x.eax = 0x0200;   /* DPMI get real mode vector */	r.h.bl = comvect;	int386(0x31,&r,&r);	orig_rm_seg = (unsigned short)r.x.ecx;	orig_rm_off = (unsigned short)r.x.edx;		//Allocate memory in low memory to store real mode handler	if ((lowp = convalloc32(COMCODEBYTES+(COMBUFSIZ<<1))) == 0)		{ printf("Can't allocate conventional memory.\n"); exit; }	inbufplc = (short *)(lowp+0);	inbufend = (short *)(lowp+2);	outbufplc = (short *)(lowp+4);	outbufend = (short *)(lowp+6);	comport = (short *)(lowp+8);	comtype = (char *)(lowp+10);	comerror = (char *)(lowp+11);	comresend = (char *)(lowp+12);	incnt = (char *)(lowp+13);	inbuf = (char *)(lowp+COMCODEBYTES);	outbuf = (char *)(lowp+COMCODEBYTES+COMBUFSIZ);	memcpy((void *)lowp,(void *)rmbuffer,COMCODEBYTES);		//Set new protected mode handler	r.x.eax = 0x2500+comvect;   /* DOS set vector (INT 0Ch) */	fh = (void far *)comhandler;	r.x.edx = FP_OFF(fh);	sr.ds = FP_SEG(fh);      //DS:EDX == &handler	sr.es = 0;	int386x(0x21,&r,&r,&sr);		//Set new real mode handler (must be after setting protected mode)	r.x.eax = 0x0201;	r.h.bl = comvect;              //CX:DX == real mode &handler	r.x.ecx = ((lowp>>4)&0xffff);  //D32realseg	r.x.edx = COMCODEOFFS;         //D32realoff	int386(0x31,&r,&r);#else	fprintf (stderr,"%s, line %d; installbicomhandlers() called\n",		__FILE__, __LINE__);#endif	}void uninstallbicomhandlers(void){#ifdef PLATFORM_DOS		union REGS r;	struct SREGS sr;		//restore old protected mode handler	r.x.eax = 0x2500+comvect;   /* DOS set vector (INT 0Ch) */	r.x.edx = orig_pm_off;	sr.ds = orig_pm_sel;    /* DS:EDX == &handler */	sr.es = 0;	int386x(0x21,&r,&r,&sr);		//restore old real mode handler	r.x.eax = 0x0201;   /* DPMI set real mode vector */	r.h.bl = comvect;	r.x.ecx = (unsigned long)orig_rm_seg;     //CX:DX == real mode &handler	r.x.edx = (unsigned long)orig_rm_off;	int386(0x31,&r,&r);#else	fprintf (stderr, "%s line %d; uninstallbicomhandlers() called\n",		__FILE__, __LINE__);	#endif	}void processreservedmessage(short tempbufleng, char *datempbuf){	long i, j, k, daotherconnectnum, templong;	switch(datempbuf[0])	{		//[253] (login, if myconnectnum's lowest, then respond with packet type 254)		case 253:			if (multioption < 5)			{				otherconnectnum = ((long)datempbuf[1])+(((long)datempbuf[2])<<8)+(((long)datempbuf[3])<<16)+(((long)datempbuf[4])<<24);				datempbuf[0] = 254;				sendpacket(-1,datempbuf,1);				myconnectindex = 0;				connecthead = 0; connectpoint2[0] = 1; connectpoint2[1] = -1;				numplayers = 2;			}			else if (multioption >= 5)			{				daotherconnectnum = ((long)datempbuf[1])+((long)(datempbuf[2]<<8))+((long)(datempbuf[3]<<16))+((long)(datempbuf[4]<<24));				if (daotherconnectnum != myconnectnum)				{					netinitconnection(daotherconnectnum,&datempbuf[5]);					if ((myconnectindex == connecthead) || ((connectnum[connecthead] == daotherconnectnum) && (myconnectindex == connectpoint2[connecthead])))					{						datempbuf[0] = 254;						j = 1;						for(i=0;i<MAXPLAYERS;i++)							if ((connectnum[i] != 0x7fffffff) && (connectnum[i] != daotherconnectnum))							{								datempbuf[j++] = (connectnum[i]&255);								datempbuf[j++] = ((connectnum[i]>>8)&255);								datempbuf[j++] = ((connectnum[i]>>16)&255);								datempbuf[j++] = ((connectnum[i]>>24)&255);								for(k=0;k<10;k++)									datempbuf[j++] = compaddr[i][k];							}							//While this doesn't have to be a broadcast, sending							//this info again makes good error correction						sendpacket(-1,datempbuf,j);						for(i=0;i<MAXPLAYERS;i++)							if (connectnum[i] == daotherconnectnum)							{								sendpacket((short)i,datempbuf,j);								break;							}					}				}			}			break;		case 254:  //[254][connectnum][connectnum]...(Packet type 253 response)			if (multioption < 5)			{				myconnectindex = 1;				connecthead = 0; connectpoint2[0] = 1; connectpoint2[1] = -1;				numplayers = 2;			}			else if (multioption >= 5)			{				j = 1;				while (j < tempbufleng)				{					templong = ((long)datempbuf[j])+((long)(datempbuf[j+1]<<8))+((long)(datempbuf[j+2]<<16))+((long)(datempbuf[j+3]<<24));					netinitconnection(templong,&datempbuf[j+4]);					j += 14;				}			}			break;		case 255:			if (multioption >= 5)				netuninitconnection(datempbuf[1]);			break;	}}void sendlogon(void){	long i;	char tempbuf[16];	if (multioption <= 0)		return;	tempbuf[0] = 253;	if (multioption < 5)	{		tempbuf[1] = kinp(0x40);		tempbuf[2] = kinp(0x40);		tempbuf[3] = kinp(0x40);		tempbuf[4] = mypriority;		myconnectnum = ((long)tempbuf[1])+(((long)tempbuf[2])<<8)+(((long)tempbuf[3])<<16)+(((long)mypriority)<<24);		sendpacket(-1,tempbuf,5);	}	else	{		tempbuf[1] = (myconnectnum&255);		tempbuf[2] = ((myconnectnum>>8)&255);		tempbuf[3] = ((myconnectnum>>16)&255);		tempbuf[4] = ((myconnectnum>>24)&255);		for(i=0;i<10;i++)			tempbuf[i+5] = mycompaddr[i];		sendpacket(-1,tempbuf,15);	}}void sendlogoff(void){	char tempbuf[16];	long i;	if ((numplayers <= 1) || (multioption <= 0)) return;	tempbuf[0] = 255;	if (multioption < 5)	{		sendpacket(-1,tempbuf,1);	}	else	{		tempbuf[1] = myconnectindex;		for(i=connecthead;i>=0;i=connectpoint2[i])			if (i != myconnectindex)				sendpacket(i,tempbuf,2);	}}int getoutputcirclesize(void){	if ((multioption >= 1) && (multioption <= 4))	{		startcom();		return(((*outbufend)-(*outbufplc)+COMBUFSIZ)&(COMBUFSIZ-1));	}	return(0);}int setsocket(short newsocket){	long i;	if (multioption < 5)	{		socket = newsocket;		return(0);	}	simulateint(0x7a,0L,(long)0x1,0L,(long)socket,0L,0L);                             //Closesocket	socket = newsocket;	simulateint(0x7a,0L,(long)0x1,0L,(long)socket,0L,0L);                             //Closesocket	if ((simulateint(0x7a,(long)0xff,0L,0L,(long)socket,0L,0L)&255) != 0) return(-2); //Opensocket	mycompaddr[10] = (socket&255);	mycompaddr[11] = (socket>>8);	ecbget[10] = (socket&255);	ecbget[11] = (socket>>8);	for(i=0;i<MAXPLAYERS;i++)	{		compaddr[i][10] = (socket&255);		compaddr[i][11] = (socket>>8);	}	return(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -