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

📄 strategy.cpp

📁 FIRA机器人足球比赛5V5仿真组的一般的防守策略
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	            
				    Kick(env,p->slowrobot,p->mainrobot);					
				 
			}
			
			Kick(env,p->slowrobot,p->preball);
			pos.x=85;
	        pos.y=43;
			PositionAndStop(env,p->cutrobot  ,pos,0);
			pos.x=33;
	        pos.y=48;
			PositionAndStop(env,p->defentrobot ,pos,0);
			break;
	   case 34:
		   		 		Order(env);
		oppOrder(env);
		if(Within(env,p->mainrobot ,5))
		{
			shoot(env,p->mainrobot );
		}
		else
		{
			Kick(env,p->mainrobot ,2,1);
			for(count=300;count>0;count--)
		     {
			while(count<1)
			{

			Kick(env,p->mainrobot ,p->cutrobot );
			} 
			}
			
		}
		if(Within(env,p->slowrobot ,5))
		{
			shoot(env,p->slowrobot );
		}
		else
		{
         Kick(env,p->slowrobot ,3,3);
		}
		if(Within(env,p->cutrobot ,5))
		{
		shoot(env,p->cutrobot );
		}
		else
		{
			if(p->opp [0].pos .y <=43)
			{
			pos.x=80;
			pos.y =52;
			PositionAndThrough(env,p->cutrobot ,pos,125);
			}
			else
			{
			pos.x=80;
			pos.y =33;
			PositionAndThrough(env,p->cutrobot ,pos,125);   
			}
		}
			pos.x=50;
			pos.y =43;
         if(fabs(p->opp[p->oppcutrobot].pos.x-pos.x)<10&&fabs(p->opp[p->cutrobot ].pos.y-pos.y)<10)
		 {
		  PositionAndThrough(env,p->defentrobot ,p->opp[p->cutrobot].pos,125);
		 }
		 else
		 {
			 PositionAndThrough(env,p->defentrobot ,pos ,0);
		 }
	    break;
	   case 35:
		   		    	Order(env);
		oppOrder(env);
		if(Within(env,p->mainrobot ,5))
		{
			shoot(env,p->mainrobot );
		}
		else
		{
			Kick(env,p->mainrobot ,2,1);
			for(count=300;count>0;count--)
		     {
			while(count<1)
			{
			Kick(env,p->mainrobot ,p->cutrobot );
			} 
			}
			
		}
		if(Within(env,p->slowrobot ,5))
		{
			shoot(env,p->slowrobot );
		}
		else
		{
         Kick(env,p->slowrobot ,3,3);
		}
		if(Within(env,p->cutrobot ,5))
		{
		shoot(env,p->cutrobot );
		}
		else
		{
			if(p->opp [0].pos .y <=43)
			{
			pos.x=83;
			pos.y =52;
			PositionAndThrough(env,p->cutrobot ,pos,125);
			}
			else
			{
			pos.x=83;
			pos.y =33;
			PositionAndThrough(env,p->cutrobot ,pos,125);
			}
		}
			pos.x=50;
			pos.y =50;
         if(fabs(p->opp[p->oppcutrobot].pos.x-pos.x)<10&&fabs(p->opp[p->cutrobot ].pos.y-pos.y)<10)
		 {
		  PositionAndThrough(env,p->defentrobot ,p->opp[p->cutrobot].pos,125);
		 }
		 else
		 {
			 PositionAndThrough(env,p->defentrobot ,pos ,0);
		 }
	    break;
	   case 36:
		  Order(env);
		oppOrder(env);
		if(Within(env,p->mainrobot ,5))
		{
		pos.x =80;
		pos.y =58;
		PositionAndThrough(env,p->cutrobot ,Meetball_p(env,p->cutrobot ),125);
		for(count=300;count>0;count--)
		{
			while(count<1)
			{

			Kick(env,p->mainrobot ,p->cutrobot );
			}
		}

		}
		else
		{
			Kick(env,p->mainrobot ,2,1);
			
		}
		if(Within(env,p->slowrobot ,5))
		{
		Kick(env,p->slowrobot ,p->cutrobot);
		}
		else
		{
         Kick(env,p->slowrobot ,3,3);
		}
		if(Within(env,p->cutrobot ,5))
		{
		shoot(env,p->cutrobot );
		}
		else
		{
			if(p->opp [0].pos .y <=43)
			{
			pos.x=80;
			pos.y =53;
			PositionAndThrough(env,p->cutrobot ,pos,125);
			}
			else
			{
			pos.x=80;
			pos.y =33;
			PositionAndThrough(env,p->cutrobot ,pos,125);  
			}
		}
			pos.x=33;
			pos.y =45;
         if(fabs(p->opp[p->oppcutrobot].pos.x-pos.x)<10&&fabs(p->opp[p->cutrobot ].pos.y-pos.y)<10)
		 {
		  PositionAndThrough(env,p->defentrobot ,p->opp[p->cutrobot].pos,125);
		 }
		 else
		 {
			 PositionAndThrough(env,p->defentrobot ,pos ,30);
		 }
	    break;
				}
			 }
			 }
	
////////////////////////////////////////////////////////////////////////////////////

void PlaceBallGame(Environment *env)
{
	Mydata * p;
	p=(Mydata *)env->userData;
	


}
/////////////**///////////////////////////////////////////////
void PenaltyBallGame(Environment *env)
{
	Mydata * p;
	p=(Mydata *)env->userData;	

	Order(env);
    if (p->oldball.x<50)//我方发点球
	{
		shoot(env,4);
		Meetball_p(env,3);
		shoot(env,3);
		Goalie(env,0);
		NormalGame(env);
	}
	else
	{
		Goalie(env,0);
		AuntPenalty(env);
		Meetball_p(env,3);
		NormalGame(env);
	}

}/////////////////////////////////////////////////


void FreeKickGame(Environment *env)
{
	Mydata * p;
	p=(Mydata *)env->userData;
	if(p->curball.x>50)//我们发 
    {
		Order(env);
		if (shoot1(env,0,2))
		{
		    shoot(env,4);
		    PredictBall(env,2);
		    Kick(env,3,p->preball);
			shoot(env,3);
			NormalGame(env);
		}
		else 
		{
			NormalGame(env);
		}
	}
	else
	{
		Goalie(env,0);
		AuntPenalty(env);
		NormalGame(env);
	}
}
void FreeGoalie( Environment* env )
{
	Mydata* p;
	p=(Mydata*)env->userData;

	Order(env);
	if (p->curball.x>50)
	{
		Goalie(env,0);
		PredictBall(env,2);
		Kick(env,3,p->preball);
		Kick(env,4,p->preball);
		NormalGame(env);
	}
	else
	{
		shoot(env,0);
	    Order(env);
        NormalGame(env);
	}
}


void GoalKickGame(Environment *env)
{
	Mydata * p;
	p=(Mydata *)env->userData;
	Order(env);
	if(p->curball.x<50)
	{
	    if (shoot1(env,0,2))
            shoot(env,0);
		Meetball_p(env,2);
		Kick(env,2,3);
	    NormalGame(env);
	}
	else
	{
		Goalie(env,0);
		PredictBall(env,2);
		Meetball_p(env,0);
		NormalGame(env);
	}
}


void FreeBallGame(Environment *env)
{
	Mydata * p;
	p=(Mydata *)env->userData;

	if(p->oldball.x > (FLEFT+FRIGHT)/2 )
	{//我们发自由球,球在前场
		if(p->oldball.y>(FTOP+FBOT)/2 )
		{//在上方
			Vector3D go ;
			go = Meetball_x(env,(FTOP+FBOT)/2-2);
			go.x -= 0.5;	//为了偏角准备
			if(go.z >0)
				PositionAndStop(env,2,go,-30);

			if(p->curball.y < (FTOP+FBOT)/2+5)
			{
				go =Meetball_x(env,GBOT);
				if(go.z >0)
					PositionAndStop(env,4,go,0.1);
			}
			PositionAndThrough(env,3,p->curball);
		}
		else
		{//在下方
			Vector3D go ;
			go = Meetball_x(env,(FTOP+FBOT)/2+2);
			go.x -= 0.5;	//为了偏角准备
			if(go.z >0)
				PositionAndStop(env,2,go,30);
			if(p->curball.y > (FTOP+FBOT)/2-5)
			{
				go =Meetball_x(env,GTOP);
				if(go.z >0)
					PositionAndStop(env,4,go,0.1);
			}
			PositionAndThrough(env,3,p->curball);
		}
	}
	else
	{//正常防守
		NormalGame(env);
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////




/*Action  对不同的GameState采用不同的策略
并且在各种状态之间切换的时候还用了计数器,作为从特殊状态改变到正常状态的判断标志
每种特殊状态都有计数器
*/

void Action( Environment *env )
{
	Mydata * p;
	p=(Mydata *)env->userData;

	switch(p->gameState)
	{
	case FREE_BALL:	//1:
// Free ball
		p->chooserobot = 0;
		p->npenaltyball =0;
		//NormalGame(env);
		if(p->nfreeball <70)
		{
			FreeBallGame(env);
			p->nfreeball++;
		}
		else
		{
			NormalGame(env);
			//连续发自由球怎么办????
			if(p->oldball.x >71-3 && p->oldball.x <71+3 )//限制坐标x
			{
				if(p->oldball.y >65-3 && p->oldball.y <65+3 )//限制坐标y
				{//上方
					if( (p->myoldpos[3].x >62 -3 && p->myoldpos[3].x <62 +3)
						&&(p->myoldpos[3].y >65 -3 && p->myoldpos[3].y <65 +3) )//检测robot坐标
						p->nfreeball =0;
				}
				else if(p->oldball.y >18-3 && p->oldball.y <18+3 )//限制坐标y
				{//上方
					if( (p->myoldpos[3].x >62 -3 && p->myoldpos[3].x <62 +3)
						&&(p->myoldpos[3].y >18 -3 && p->myoldpos[3].y <18 +3) )//检测robot坐标
						p->nfreeball =0;
				}
			}
		}
          Goalie(env,1);
		  Goalie(env,0);
		break;
	case PLACE_KICK:	//2:
//Place kick(Kickoff)
		p->chooserobot = 0;
		p->npenaltyball =0;
		p->nfreeball =0;
		NormalGame(env);

		break;
	case PENALTY_KICK:	//3:
    //Penatly kick
		p->nfreeball =0;
		if(p->npenaltyball < 46)
		{//还处于点球状态
			if(p->oldball.x > (FLEFT+FRIGHT)/2 )
			{
				Penalty(env);//我方发点球
				Goalie(env,1);
		        Goalie(env,0);
			}
			else
				AuntPenalty(env);

			p->npenaltyball++;	//自加计数
		}
		else
		{
			if( (p->oldball.x >77 && p->oldball.x < 80) //x
				&& (p->oldball.y > 40 && p->oldball.y < 43) //y
				&& (p->opp[1].pos.x < 52) && (p->opp[2].pos.x < 52)//用对方4个球员判断够了吧
				&& (p->opp[3].pos.x < 52)&& (p->opp[4].pos.x  < 52)
				)
			{//是否连续点球,我发球!!
				p->npenaltyball =0;
				p->chooserobot = 0;
			}
			else if( (p->oldball.x >20 && p->oldball.x < 24) //x
				&& (p->oldball.y > 40 && p->oldball.y < 43) //y
				&& (p->myoldpos[1].x > 48) && (p->myoldpos[2].x > 48)//用对方4个球员判断够了吧
				&& (p->myoldpos[3].x > 48 )&& (p->myoldpos[4].x > 48)
				)
			{
				p->npenaltyball =0;
				p->chooserobot = 0;			
			}
			else
			{//比赛正常!!
				NormalGame(env);
			}
		}
		break;
	case FREE_KICK:	//4:
//Free kick
		p->nfreeball =0;
		p->chooserobot = 0;
		p->npenaltyball =0;
		Meetball_p(env,3);
		shoot(env,3);
		NormalGame(env);

		break;
	case GOAL_KICK:	//5:
//Goal kick
		p->nfreeball =0;
		p->chooserobot = 0;
		p->npenaltyball =0;
		GoalKickGame(env);
		NormalGame(env);

		break;
	default :
		p->nfreeball =0;
		p->chooserobot = 0;
		p->npenaltyball =0;
		FreeKickGame(env);
		NormalGame(env);		
		break;
	}
	
}
			
//////
/*AuntPenalty 防点球策略
*/
//
void AuntPenalty ( Environment *env )
{//防点球
	Mydata * p;
	p=(Mydata *)env->userData;
	
	Vector3D go;
	int robot=0;
	{//先校正姿态,再去拦球
	double OX=	FLEFT - (GTOP - GBOT);	// 该点为球门中心 向后移动半个球门
	double OY=	(FTOP + FBOT)/2;			//球门中心	
	double ballx=p->curball.x	;
	double bally=p->curball.y	;
	double gx = p->robot[robot].pos.x ;	
	double gx_outline = FLEFT + 2.2;		//对x坐标的限定,防止离球门线太远了
	double gx_inline = FLEFT + 1.8;
	double gy = p->robot[robot].pos.y;		//跑位点,初值为当前位置
	double goalline = FLEFT + 3;
	bool   notout= true;	//不出击
	bool   standby = false;	//限制x 坐标
	bool   XX=false;	//是否旋转



	if( XX && ballx < gx + 0.5 )
	{//旋转
		if(bally < gy && bally > gy-4 )
		{
			Velocity(env,robot,-125,125);
			return ;
		}
		else if(bally > gy && bally < gy+4)
		{
			Velocity(env,robot,125,-125);
			return ;
		}
	}


	gy = OY + ( goalline - OX ) * (bally - OY)/(ballx - OX);
	
	if(notout)
	{
		if(gy > GTOP)
			gy = GTOP;
		else if(gy < GBOT)
			gy = GBOT;
	}
	go.y = gy ;

	if(standby)
	{
		if(gx > gx_outline )
			gx = gx_outline;
		else if(gx < gx_inline)
			gx = gx_inline;
	}

	go.x = gx ;
		GoaliePosition(env,robot,go,90,1.5);
	}
	go.x =10;
	go.y =p->robot[3].pos.y;
	PositionAndStop(env,3,go,90);
	go.x =10;
	go.y =p->robot[2].pos.y;
	PositionAndStop(env,2,go,90);
	go.x =20;
	go.y =45;
	PositionAndStop(env,1,go,90);
	go.x =20;
	go.y =38;
	PositionAndStop(env,4,go,90);

}

void Penalty ( Environment *env )
{//这个点球踢法,可能只针对那种守门员只是在门区防守的
	
	Mydata * p;
	p=(Mydata *)env->userData;
	
	Vector3D pos;
	pos.x=FRIGHT;
	pos.y=40;
	Kick(env,2,pos);
	PredictBall(env,2);
	PositionBallX(env,2,p->preball,240,1.5);

}

⌨️ 快捷键说明

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