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

📄 strategy.cpp

📁 FIRA机器人足球赛simurosot5vs5
💻 CPP
字号:
//定义类型,常量
#include "Strategy.h"

//定义动作函数
#include "vsv.h"

// NormalGame 在没有犯规和罚球的情况下的策略


void NormalGame ( Environment *env )
{
	Mydata * p;
	p=(Mydata *)env->userData;
	Vector3D pos,begin,end;
    static int flag=1;
	int i,count;
    double alfa;
    
	if(flag==1){
		PredictBall(env,2);
		Kick(env,3,p->preball);
		Kick(env,4,p->preball);
	    flag++;
	}
	Goalie(env,0);
    
	if(p->curball.x>=50&&p->curball.x<61)
		Kick(env,1,CONSTGATE);
	else if(p->curball.x>=61&&p->curball.x<78.6){
		   if(p->curball.y<27.8){
              pos.x=37.2;
			  pos.y=20;
		   }
		   else if(p->curball.y<=58.5){
			   PredictBall(env,2);
			   pos.x=37.2;
			   pos.y=p->preball.y;
		   }
		   else{
			   pos.x=37.2;
			   pos.y=66.2;
		   }
		   PositionAndStop(env,1,pos);
	}
	else  if(p->curball.x>78.6&&
		     ((p->curball.y<34&&p->curball.y>6.3)||(p->curball.y>48&&p->curball.y<77.2))){
		       Order(env);
		       count=120;			 
			   while(count>0){
				  Kick(env,1,p->slowrobot);
				  count--;
			   }
	}	  		
      
	switch(p->ballArea){
      
	  case 1:
	  case 2: 	  
	   Order(env);
	   pos.x=50;
	   pos.y=9;
	   Kick(env,p->mainrobot,pos);
	   Kick(env,p->slowrobot,pos);		  
	   
	   /*PredictBall(env,2);	   
	   Kick(env,p->mainrobot,p->preball);
	   Kick(env,p->slowrobot,p->preball);*/
	   pos.x=19;
	   pos.y=58;
       for(i=1;i<5;i++)
         if(fabs(p->opp[i].pos.x-pos.x)<15&&fabs(p->opp[i].pos.y-pos.y)<15)
			 break;
       if(i<5)
		  PositionAndThrough(env,p->defentrobot,p->opp[i].pos,125);
	   else if(p->curball.y<28){ 
		   pos.x=20;
		   pos.y=58;
		   PositionAndStop(env,p->defentrobot,pos,-145);
	   }
	   else 
          Kick(env,p->defentrobot,0);

	   if(p->curball.x<12&&p->curball.y>27)
         PositionAndThrough(env,p->cutrobot,p->robot[0].pos);
	   else if(p->curball.x<FLEFT+4&&p->curball.x<17){
		   pos.x=9;
		   pos.y=9;
		   Kick(env,p->cutrobot,pos);
	   }
	   else{
	     pos.x=20;
	     pos.y=p->robot[0].pos.y;
         PositionAndStop(env,p->cutrobot,pos);
	   }
	    break;

      case 3:
	  case 4:
	   Order(env);
	   pos.x=50;
	   pos.y=74;
	   Kick(env,p->mainrobot,pos);
	   Kick(env,p->slowrobot,pos);

	   /*PredictBall(env,2);
	   Kick(env,p->mainrobot,p->preball);
	   Kick(env,p->slowrobot,p->preball);*/
	   pos.x=19;
	   pos.y=27;

       for(i=1;i<5;i++)
         if(fabs(p->opp[i].pos.x-pos.x)<15&&fabs(p->opp[i].pos.y-pos.y)<15)
			 break;
       if(i<5)
		  PositionAndThrough(env,p->defentrobot,p->opp[i].pos,125);
	   else if(p->curball.y>66){ 
		   pos.x=20;
		   pos.y=27;
		   PositionAndStop(env,p->defentrobot,pos,145);
	   }
       else 
		   Kick(env,p->defentrobot,0);
	  

	   if(p->curball.x<12&&p->curball.y<59)
         PositionAndThrough(env,p->cutrobot,p->robot[0].pos);

	   else if(p->curball.x>FLEFT+4&&p->curball.x<17){
		   pos.x=9;
		   pos.y=77;
		   Kick(env,p->cutrobot,pos);
	   }
	   else{
	     pos.x=20;
	     pos.y=p->robot[0].pos.y;
         PositionAndStop(env,p->cutrobot,pos);
	   }

		  break;
	  case 5: 
	  case 6:
	   Order(env);
       Kick(env,p->mainrobot,1,1.5);
	   Kick(env,p->slowrobot,3,1.5);
	   Kick(env,p->cutrobot,5,1.5);

	   pos.x=19;
	   pos.y=58;
	   PositionAndStop(env,p->defentrobot,pos,-135);
	   break;
	  case 7:
	  case 8: 
	   Order(env);
	   Kick(env,p->mainrobot,1,1.5);
	   Kick(env,p->slowrobot,3,1.5);
       Kick(env,p->cutrobot,5,1.5);

	   pos.x=20;
	   pos.y=27;
	   PositionAndStop(env,p->defentrobot,pos,135);
	    break;
      case 9: 
		 Order(env);		 
		 shoot(env,p->mainrobot);
         
		 if(p->curball.x>p->robot[p->mainrobot].pos.x&&p->curball.y<10){
			 count=120;
			 while(count>0){
			   Kick(env,p->slowrobot,p->mainrobot);
			   Kick(env,p->cutrobot,p->slowrobot);
			   count--;				   
			 }			 
		 }
		 else{
			 PredictBall(env,2);
			 PositionAndStop(env,p->slowrobot,p->preball);

		  pos.x=77;
		  pos.y=57.5;
		  PositionAndStop(env,p->cutrobot,pos,-45);
		 }
		 break;
      case 10:  
         Order(env);
		 shoot(env,p->mainrobot);		 

         PredictBall(env,2);
		 PositionAndStop(env,p->slowrobot,p->preball);
         
		 pos.x=77;
		 pos.y=57.5;
		 PositionAndStop(env,p->cutrobot,pos,-45);
		       
		 break;
      case 11:  
		  Order(env);
		  shoot(env,p->mainrobot);

		  PredictBall(env,2);
		  PositionAndStop(env,p->slowrobot,p->preball);
		  
		  pos.x=77;
		  pos.y=26;
		  PositionAndStop(env,p->cutrobot,pos,-45);
		 break; 
      case 12:
		 Order(env);		 
		 shoot(env,p->mainrobot);

		 if(p->curball.x>p->robot[p->mainrobot].pos.x&&p->curball.y>74.2){
			 count=120;
			 while(count>0){			  
			   Kick(env,p->slowrobot,p->mainrobot);
			   Kick(env,p->cutrobot,p->slowrobot);			   
			   count--;
			 }			 
		 }
		 else{			
			 PredictBall(env,2);
			 PositionAndStop(env,p->slowrobot,p->preball);
			 pos.x=77;
		     pos.y=26;
		     PositionAndStop(env,p->cutrobot,pos,-45);
		 }
		 break;
      case 13:
		  Order(env);		  
		 shoot(env,p->mainrobot);

		  if(p->curball.x<92){
			   count=120;
			   while(count>0){			     
				  Kick(env,p->slowrobot,p->mainrobot);
			      count--;
			  }			  
		  }
		  else
		    shoot(env,p->slowrobot);

		  
		 if(p->curball.x<87||p->curball.y<20){            
			count=120;
			while(count>0){
				Kick(env,p->cutrobot,p->slowrobot);
				count--;
			}
		 }
		 else{
		     pos.x=77;
			 pos.y=57.5;
		     PositionAndStop(env,p->cutrobot,pos,-45);
		 }

		 break;
	  case 14:	
		  Order(env);

		  if(p->curball.y>GBOTY){   //包抄
			PredictBall(env,2);
			begin=p->robot[p->cutrobot].pos,end=p->preball;
			alfa=Atan(begin,end);
            PAngle(env,p->cutrobot,alfa,125); 
		  }
		  else{
		  //if(!shoot1(env,p->cutrobot)){
            pos.x=77;
			pos.y=57.5;
		    PositionAndStop(env,p->cutrobot,pos,-45);
		  }	
		  
		 shoot(env,p->mainrobot);
		  if(p->curball.y<34){
			 count=120;
	     	 while(count>0){		            
				    Kick(env,p->slowrobot,p->mainrobot);
			        count--;					
				 }
		  }
		  else{
			  if(p->ballspeed.y>1) {             
			   PositionBallX(env,p->mainrobot,p->robot[p->mainrobot].pos,-100,1);
			   //shoot(env,p->slowrobot);
			  }
			  else{
			   PositionBallX(env,p->mainrobot,p->robot[p->mainrobot].pos,-70,1);
			   //shoot(env,p->slowrobot);
			  }	
			  Kick(env,p->slowrobot,CONSTGATE);
		  }

		 break;
      case 15:
		  Order(env);

		  if(p->curball.y<GTOPY){
			 PredictBall(env,2);
		    begin=p->robot[p->cutrobot].pos,end=p->preball;
		    alfa=Atan(begin,end);
            PAngle(env,p->cutrobot,alfa,125);
		  } 
		  else{
			  //if(!shoot1(env,p->cutrobot)){
            pos.x=77;pos.y=26;
		    PositionAndStop(env,p->cutrobot,pos,-45);
		  }

		 shoot(env,p->mainrobot);
		  if(p->curball.y>48){
			 count=120;	
			 while(count>0){		         
				 Kick(env,p->slowrobot,p->mainrobot);
			     count--;
			   }			
		  }
		  else{
			  if(p->ballspeed.y>1){
				 PositionBallX(env,p->mainrobot,p->robot[p->mainrobot].pos,100,1);
				 //shoot(env,p->slowrobot);
			  }
			  else{
				 PositionBallX(env,p->mainrobot,p->robot[p->mainrobot].pos,70,1);
				 //shoot(env,p->slowrobot);
			  }
			  Kick(env,p->slowrobot,CONSTGATE);
		  }
		
		 break;
      case 16:
		 Order(env);		 
		 shoot(env,p->mainrobot);

		 if(p->curball.x<92){
			count=120;
 			while(count>0){
				 Kick(env,p->slowrobot,p->mainrobot);
			     count--;				
			}
		 }
		  else
               shoot(env,p->slowrobot);
		  
		  if(p->curball.x<87||p->curball.y>66){
            count=100;
			while(count>0){
				Kick(env,p->cutrobot,p->slowrobot);
				count--;
			}
		 }
		 else{
		   pos.x=77;pos.y=26;
	       PositionAndStop(env,p->cutrobot,pos,45);
		 }
		 break;
	}
}


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

}

void PenaltyBallGame(Environment *env)
{
	Mydata * p;
	p=(Mydata *)env->userData;
	Vector3D go={0,0,0};

	
	switch (env->whoseBall)
	{
	case ANYONES_BALL:
		break;
		PenaltyGoalie (env, &p->robot[0] );
	case BLUE_BALL:
		PenaltyGoalie (env, &p->robot[0] );
		break;
	case YELLOW_BALL:
		PenaltyGoalie (env, &p->robot[0] );
		break;
	}
}

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

}

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

void PenaltyGoalie ( Environment *env, Robot *robot )
{
	Mydata * p;
	p=(Mydata *)env->userData;
}


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

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;
		NormalGame(env);

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

		break;
	default :
		p->nfreeball =0;
		p->chooserobot = 0;
		p->npenaltyball =0;
		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 + -