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

📄 server.cc

📁 国外一套很好的游戏代码,款经典的小游戏 希望大家提出宝贵意见 让我们一起进步
💻 CC
📖 第 1 页 / 共 2 页
字号:
		if(ply->in->enem && ply->in->enem->ply)			txtp+=sprintf(txtp,"[1] Chat with target player\n");		txtp+=sprintf(txtp,"[2] Chat with team\n");		txtp+=sprintf(txtp,"[3] Chat with all\n");		printtocons(txt);		break;		case CMOD_CHATPRIVATE:		printtocons("Chat with target player\n");		requestline(false);		break;		case CMOD_CHATTEAM:		printtocons("Chat with team");		requestline(false);		break;		case CMOD_CHATALL:		printtocons("Chat with all");		requestline(false);		break;		case CMOD_WHOIS:		txtp+=sprintf(txtp,"WHOIS\n\n");		if(ply->in->enem)		{			spr=ply->in->enem->interact(txtp,CMOD_WHOIS,-1,ply->in);			txtp=txt+strlen(txt);		}		else			txtp+=sprintf(txtp,"No target\n");		txtp+=sprintf(txtp,"\n[1] Cycle to next player");		printtocons(txt);		spritetocons(spr);		break;		case CMOD_HACK:		if(ply->op && !locg)		{			printtocons("Admin\n\n[1] Set account password\n[2] Kick user\n[3] Delete user\n[4] Shutdown server");		}		else		{			printtocons("Admin\n\n[1] Set account password");		}		break;				case CMOD_PASS1:		printtocons("Choose your password");		requestline(true);		break;		case CMOD_PASS2:		printtocons("Confirm your password");		requestline(true);		break;		case CMOD_KICK:		printtocons("Input username to kick");		requestline(false);		break;		case CMOD_DELETE:		printtocons("Input username to delete");		requestline(false);		break;	}}void server::cons(int opr){	char txt[1024]; //Text buffer for constructing output	alliance* tali; //Alliance to choose	ship* tshp; //Temporary ship scratchpad	if(opr<0)		return;	if(cmod!=CMOD_CHOOSE && !(ply && ply->in))		return;	txt[0]='\0';	switch(cmod)	{		case CMOD_CHOOSE:		tali=alliance::get(opr);		if(tali)		{			ply->spawn(tali);			log("Spawned as %s(%s)",ply->in->cls,ply->in->all->nam);			changecmod(CMOD_HACK);		}		else		{			printtomesg("No such alliance");			changecmod(CMOD_CHOOSE);		}		break;		case CMOD_EQUIP:		ply->in->interact(txt,CMOD_EQUIP,opr,ply->in);		printtomesg(txt);		changecmod(CMOD_EQUIP);		break;		case CMOD_SCAN:		if(ply->in->enem)			ply->in->enem->interact(txt,CMOD_SCAN,opr,ply->in);		else			if(ply->in->plnt)				ply->in->plnt->interact(txt,CMOD_SCAN,opr,ply->in);		break;		case CMOD_HAIL:		if(ply->in->enem)		{			ply->in->enem->interact(txt,CMOD_HAIL,opr,ply->in);			printtomesg(txt);			changecmod(CMOD_HAIL);		}		else		{			if(ply->in->plnt)			{				if(opr==5)				{					if(ply->in->see(ply->in->plnt))					{						if(ply->in->plnt->all==ply->in->all)						{							ply->in->transport(ply->in->plnt);							ply->commit();							printtomesg("Restore position saved");						}						else							throw error("Cannot save with a different allegiance");					}					else					{						throw error("Out of range");					}				}				else if(opr==4)				{					changecmod(CMOD_REFIT);				}				else				{					ply->in->plnt->interact(txt,CMOD_HAIL,opr,ply->in);					printtomesg(txt);					changecmod(CMOD_HAIL);				}			}		}		break;		case CMOD_REFIT:		if(ply->in->plnt)		{			ply->in->plnt->interact(txt,CMOD_REFIT,opr,ply->in);			printtomesg(txt);			changecmod(CMOD_REFIT);		}		break;		case CMOD_CHAT:		if(opr==1 && ply->in->enem && ply->in->enem->ply)			changecmod(CMOD_CHATPRIVATE);		if(opr==2)			changecmod(CMOD_CHATTEAM);		if(opr==3)			changecmod(CMOD_CHATALL);		break;		case CMOD_WHOIS:		if(opr==1)		{			for(int i=0,j=(ply->in->enem ? ply->in->enem->self+1 : 0);i<ship::ISIZE;i++,j++)			{				if(j>=ship::ISIZE)					j=0;				tshp=ship::get(j);				if(tshp && tshp->ply && tshp!=ply->in)				{					ply->in->enem=tshp;					ply->in->frnd=NULL;					ply->in->plnt=NULL;					break;				}			}			changecmod(CMOD_WHOIS);		}		break;		case CMOD_HACK:		if(opr==1)		{			changecmod(CMOD_PASS1);		}		if(ply->op && !locg)		{			if(opr==2)				changecmod(CMOD_KICK);			if(opr==3)				changecmod(CMOD_DELETE);			if(opr==4)			{				log("Server shutdown requested");				qsig=true;			}		}		break;	}}void server::requestline(bool hide){	unsigned char buf[SERV_READLN_SZ]; //For sending the request byte	buf[0]=SERV_READLN;	buf[1]=(unsigned char)hide;	hlpr->send(buf,SERV_READLN_SZ);}void server::input(){	player* delp; //Player to delete	switch(cmod)	{		case CMOD_NAME:		if(inpb[0]=='\0')			throw error("Aborted name entry");		try		{			if(strlen(inpb)<2)			{				inpb[0]='\0';				throw error("Name too short");			}			ply=player::get(inpb);			if(ply)			{				log("Attempting login");				if(locg)				{					ply->login(NULL);					log("Local game login");					changecmod(CMOD_STAT);				}				else				{					changecmod(CMOD_PASS);				}			}			else			{				ply=new player(inpb);				log("New player created");				changecmod(CMOD_CHOOSE);			}		}		catch(error it)		{			ply=NULL;				log(it.str);			printtomesg(it.str);			changecmod(CMOD_NAME);		}		break;		case CMOD_PASS:		try		{			ply->login(inpb);			log("Login succeeded");			changecmod(CMOD_STAT);		}		catch(error it)		{			ply=NULL;				log(it.str);			printtomesg(it.str);			changecmod(CMOD_NAME);		}		break;		case CMOD_PASS1:		inpb[32]='\0';		strcpy(tpas,inpb);		changecmod(CMOD_PASS2);		break;		case CMOD_PASS2:		inpb[32]='\0';		if(strcmp(inpb,tpas)==0)		{			ply->setpass(inpb);			printtomesg("Password set successfully");			log("Set password");		}		else		{			printtomesg("Passwords don't match!");		}		changecmod(CMOD_HACK);		break;		case CMOD_CHATPRIVATE:		if(ply && ply->in && ply->in->enem && ply->in->enem->ply)		{			hail(ply,ply->in->enem->ply,inpb);			changecmod(CMOD_CHATPRIVATE);		}		break;				case CMOD_CHATTEAM:		if(ply && ply->in)		{			for(int i=0;i<ISIZE;i++)				if(connections[i] && connections[i]->ply && connections[i]->ply->in && connections[i]->ply->in->all==ply->in->all)					hail(ply,connections[i]->ply,inpb);			changecmod(CMOD_CHATTEAM);		}		break;				case CMOD_CHATALL:		if(ply->in)		{			for(int i=0;i<ISIZE;i++)				if(connections[i] && connections[i]->ply)					hail(ply,connections[i]->ply,inpb);			changecmod(CMOD_CHATALL);		}		break;		case CMOD_KICK:		log("Attemped kick of %s",inpb);		for(int i=0;i<ISIZE;i++)		{			if(connections[i] && connections[i]!=this && connections[i]->ply && strcmp(inpb,connections[i]->ply->nam)==0)			{				bulletin("%s was kicked from the server",inpb);				delete connections[i];				break;			}		}		changecmod(CMOD_HACK);		break;				case CMOD_DELETE:		log("Attemped deletion of %s",inpb); 		for(int i=0;i<ISIZE;i++)		{			if(connections[i] && connections[i]!=this && connections[i]->ply && strcmp(inpb,connections[i]->ply->nam)==0)			{				bulletin("%s was kicked from the server",inpb);				delete connections[i];				break;			}		}		delp=player::get(inpb);		if(delp && delp!=ply)			delete delp;		changecmod(CMOD_HACK);		break; 	}}void server::printtocons(char* fmt,...){	unsigned char buf[1028]; //Outgoing buffer	va_list fmts;	va_start(fmts,fmt);	vsprintf((char*)buf+3,fmt,fmts);	va_end(fmts);	buf[0]=SERV_CONS;	calc::inttodat(strlen((char*)buf+3),buf+1);	hlpr->send(buf,strlen((char*)buf+3)+3);}void server::spritetocons(int indx){	unsigned char buf[SERV_CSPR_SZ]; //Outgoing buffer	if(indx>=0)	{		buf[0]=SERV_CSPR;		calc::inttodat(indx,buf+1);		hlpr->send(buf,SERV_CSPR_SZ);	}}void server::printtomesg(char* fmt,...){	unsigned char buf[132]; //Outgoing buffer	va_list fmts;	if(fmt[0]!='\0')	{		va_start(fmts,fmt);		vsprintf((char*)buf+3,fmt,fmts);		va_end(fmts);		buf[0]=SERV_MESG;		calc::inttodat(strlen((char*)buf+3),buf+1);		hlpr->send(buf,strlen((char*)buf+3)+3);	}}void server::uploads(){	unsigned char buf[256]; //Buffer for outgoing data	if(ply && ply->in)	{		ply->in->netout(SERV_SELF,buf);		hlpr->send(buf,SERV_SELF_SZ);		calc::inttodat(foc,buf+21);		/*if(!shpu[ply->in->self])		{			ply->in->netout(SERV_NEW,buf);			hlpr->send(buf,SERV_NEW_SZ);			ply->in->netout(SERV_NAME,buf);			hlpr->send(buf,SERV_NAME_SZ);			shpu[ply->in->self]=true;		}		ply->in->netout(SERV_UPD,buf);		buf[21]=0;		hlpr->send(buf,SERV_UPD_SZ);*/				if(tcks%(urat/10)==0)		{			if(tcks%((urat*4)/10)==0)				uploadplanets();			uploadships();			uploadfrags();		}		hlpr->pump();	}}void server::uploadplanets(){	unsigned char buf[256]; //Outgoing scratchpad buffer to use	planet* tpln; //Concerned planet	for(int i=0;i<planet::ISIZE;i++)	{		tpln=planet::get(i);		if(tpln)		{			if(ply->in->see(tpln))			{				if(!plnu[i])				{					tpln->netout(SERV_NEW,buf);					hlpr->send(buf,SERV_NEW_SZ);					tpln->netout(SERV_NAME,buf);					hlpr->send(buf,SERV_NAME_SZ);					plnu[i]=true;				}				tpln->netout(SERV_UPD,buf);				hlpr->send(buf,SERV_UPD_SZ);			}			else			{				if(plnu[i])				{					buf[0]=SERV_DEL;					calc::inttodat(planet2pres(i),buf+1);					hlpr->send(buf,SERV_DEL_SZ);					plnu[i]=false;				}			}		}		else		{			if(plnu[i])			{				buf[0]=SERV_DEL;				calc::inttodat(planet2pres(i),buf+1);				hlpr->send(buf,SERV_DEL_SZ);				plnu[i]=false;			}		}	}}void server::uploadships(){	unsigned char buf[256]; //Outgoing scratchpad buffer to use	ship* tshp; //Concerned ship	for(int i=0;i<ship::ISIZE;i++)	{		tshp=ship::get(i);		//if(tshp!=ply->in)		{			if(tshp)			{				if(ply->in->see(tshp))				{					if(!shpu[i])					{						tshp->netout(SERV_NEW,buf);						hlpr->send(buf,SERV_NEW_SZ);						tshp->netout(SERV_NAME,buf);						hlpr->send(buf,SERV_NAME_SZ);						shpu[i]=true;					}					tshp->netout(SERV_UPD,buf);					if(ply->in->all->opposes(tshp->all))						buf[21]=1;					hlpr->send(buf,SERV_UPD_SZ);				}				else				{					if(shpu[i])					{						buf[0]=SERV_DEL;						calc::inttodat(ship2pres(i),buf+1);						hlpr->send(buf,SERV_DEL_SZ);						shpu[i]=false;					}				}			}			else			{				if(shpu[i])				{					buf[0]=SERV_DEL;					calc::inttodat(ship2pres(i),buf+1);					hlpr->send(buf,SERV_DEL_SZ);					shpu[i]=false;				}			}		}	}}void server::uploadfrags(){	unsigned char buf[256]; //Outgoing scratchpad buffer to use	frag* tfrg; //Concerned frag	for(int i=0;i<frag::ISIZE;i++)	{		tfrg=frag::get(i);		if(tfrg)		{			if(ply->in->see(tfrg))			{				if(frgu[i])				{					tfrg->netout(SERV_UPD,buf);					hlpr->send(buf,SERV_UPD_SZ);				}				else				{					tfrg->netout(SERV_NEW,buf);					hlpr->send(buf,SERV_NEW_SZ);					tfrg->netout(SERV_UPD,buf);					hlpr->send(buf,SERV_UPD_SZ);					frgu[i]=true;				}			}			else			{				if(frgu[i])				{					buf[0]=SERV_DEL;					calc::inttodat(frag2pres(i),buf+1);					hlpr->send(buf,SERV_DEL_SZ);					frgu[i]=false;				}			}		}		else		{			if(frgu[i])			{				buf[0]=SERV_DEL;				calc::inttodat(frag2pres(i),buf+1);				hlpr->send(buf,SERV_DEL_SZ);					frgu[i]=false;			}		}	}}void server::kill(){	unsigned char buf[SERV_DEL_SZ]; //Buffer for deleting player's ship clientside	try	{		uploadplanets();		uploadships();		uploadfrags();		buf[0]=SERV_DEL;		calc::inttodat(ship2pres(ply->in->self),buf+1);		hlpr->send(buf,SERV_DEL_SZ);		printtomesg("You have been destroyed: Game Over");		printtocons("Game over");	}	catch(error it)	{	}}void server::hilight(ship* tshp){	unsigned char buf[SERV_HILIGHT_SZ]; //Buffer for sending hilight information	if(tshp)	{		buf[0]=SERV_HILIGHT;		calc::inttodat(ship2pres(tshp->self),buf+1);		hlpr->send(buf,SERV_HILIGHT_SZ);	}}server* server::connections[ISIZE];long server::tcks;TCPsocket server::lstn;bool server::qsig;FILE* server::logf;bool server::locg;bool server::locl;

⌨️ 快捷键说明

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