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

📄 strategy.cpp

📁 足球机器人比赛程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// Strategy.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "Strategy.h"
#include <math.h>


#define square(a) ((a)*(a))
#define leng(a,b,c,d) sqrt(square((a)-(c))+square((b)-(d)))

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved )
{
    switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
    }
    return TRUE;
}

const double PI = 3.1415923;

char myMessage[200]; 

void PredictBall ( Environment *env );
void Goalie1 ( Robot *robot, Environment *env );
void NearBound2 ( Robot *robot, double vl, double vr, Environment *env );
void Attack2 ( Robot *robot, Environment *env );
void Defend ( Robot *robot, Environment *env, double low, double high );


void MoonAttack (Robot *robot, Environment *env );

void MoonFollowOpponent (  Robot *robot, OpponentRobot *opponent );

void Velocity ( Robot *robot, int vl, int vr );
void Angle ( Robot *robot, int desired_angle);
void Position( Robot *robot, double x, double y );


void	NormalGame_Left( Environment *env );
void	NormalGame_Right( Environment *env );
void	Attack_Left(Environment *env);
void	Attack_Right(Environment *env);
void	Defender_Left(Environment *env);
void	Defender_Right(Environment *env);
void	Defend1_Left(Environment *env);
void	Defend2_Left(Environment *env);
void	Defend3_Left(Environment *env);
void	Defend1_Right(Environment *env);
void	Defend2_Right(Environment *env);
void	Defend3_Right(Environment *env);


void	GoalKeeper_In_Left(Robot *robot,Environment *env);
void	GoalKeeper_In_Right(Robot *robot,Environment *env);
void	GoalKeeper_Out(Robot *robot,Environment *env);
void	AKick_Left(Robot *robot,Environment *env);
double	Field(double x,double y,double dest);
void	Shoot1_Left(Robot *robot,Environment *env);
void	Shoot1_Right(Robot *robot,Environment *env);
void	Defence1_Left(Robot *robot,Environment *env);
void	Defence1_Right(Robot *robot,Environment *env);
void	AvoidGoalie(Robot *robot);
void	AvoidPosition(Robot *robot, double x, double y);
void    PenaltyKick_Left(Robot *robot,Environment *env);
void    PenaltyKick_Right(Robot *robot,Environment *env);

//无方向性
void	Position0( Robot *robot, double x, double y );
void	Position1( Robot *robot, double x, double y );
bool	Position2(Robot *robot,double x, double y);
bool	GoaliePosition(Robot *robot,double x, double y);
void	Angle1(Robot *robot,int desired_angle);
void	AngleOfPosition(Robot *robot,double x, double y);

//新的策略
void kick( Environment *env, Robot *robot, double aim_angle );
void Position1_cz( Robot *robot, double x, double y );   
void Position00_cz(Robot *robot, double x, double y);
void Goalkeeper_Right_py( Robot *robot, Environment *env );
void Attack_cz2( Robot *robot, Environment *env );
void Attack_cz2( Robot *robot, Environment *env );


extern "C" STRATEGY_API void Create ( Environment *env )
//void Create ( Environment *env )
{
	
	// allocate user data and assign to env->userData
	// eg. env->userData = ( void * ) new MyVariables ();
}

extern "C" STRATEGY_API void Destroy ( Environment *env )
//void Destroy ( Environment *env )
{

}

extern "C" STRATEGY_API void Strategy ( Environment *env )
{

	


	switch (env->gameState)
	{	
		case 0:
			PredictBall ( env );
			NormalGame_Left( env );
			GoalKeeper_In_Left( &env->home [0], env );
			
//			Position1_cz( &env->home[1] , env->predictedBall.pos.x, env->predictedBall.pos.y );
			break;

		case FREE_BALL:
			NormalGame_Right(env);
			break;

		case PLACE_KICK:
			NormalGame_Right(env);
			break;	
			
		case PENALTY_KICK:
			PenaltyKick_Right(&env->home[4],env);
			break;

		case FREE_KICK:
			NormalGame_Right(env);
			break;

		case GOAL_KICK:
			NormalGame_Right(env);
			break;
	}
}


//没有减速,利用摩擦减速经验公式
//目前不如 Position1_cz 好
void Position00_cz(Robot *robot, double x, double y)
{
	int desired_angle=0, theta_e = 0, vl, vr; 		// variable  ,  statement
	double dx, dy, d_e, f_e, Ka=0.85*0.5;//Kd, ka由自己调试!
								                
	dx=x-robot->pos.x;	 //distance difference of axis X
	dy=y-robot->pos.y;	//distance difference of axis Y
	d_e=sqrt(dx*dx+dy*dy);			//by Pythagorean Theorem
									//distance difference of robot and target position

	if(dx==0 && dy==0)			 //prevents dx to be divided as '0'
		desired_angle = 90;			    
	else
		desired_angle = (int)(180./PI*atan2((double)(dy),(double)(dx)));//adding   

	theta_e = desired_angle - robot->rotation; //  
	
	while(theta_e > 180)	  	//limit of the range of  as -180~180
		theta_e -= 360;			
	while(theta_e < -180)	
		theta_e += 360;

	vl = 122 - (int)(Ka*theta_e);
	vr = 122 + (int)(Ka*theta_e);


	if( d_e < 5 )//接近时减速,靠惯性到达,场地摩擦系数很大。3.5----v=125
	{
		vl=0;
		vr=0;
	}
	Velocity( robot, vl, vr);		//save the speed of calculated robot
}


void kick( Environment *env, Robot *robot, double aim_angle )
{
	double L=2.9528 ;
	double d_angle, angle, d_length, vl, vr;
	d_angle=180/PI*atan2(robot->pos.y-env->predictedBall.pos.y,robot->pos.x-env->predictedBall.pos.x);
	d_length=leng(robot->pos.x,env->predictedBall.pos.x,robot->pos.y,env->predictedBall.pos.y);
	angle=aim_angle-d_angle;
	
	while(angle > 180)	  	//limit of the range of  as -180~180
		angle -= 360;			
	while(angle < -180)	
		angle += 360;
	

//	Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
	if( d_length > 2*L )
	{
		Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
	}
	else if(d_angle>15)
	{
		vl=100 + 15*angle*L/2;
		vr=100 - 15*angle*L/2;
		Velocity ( robot, vl, vr );
	}
	else		//d_length<L/2
	{
		Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
	}
	
}

void NormalGame_Left( Environment *env )
{
	int i, j=0, flag[5];
	double leng[5] ; 
	for(i=1;i<=4;i++)
	{
		leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);   
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(leng[i]>=leng[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}
	GoalKeeper_In_Left( &env->home [0], env );
	if(env->currentBall.pos.x<(FRIGHTX+FLEFTX)/2)
	{
		if(env->whosBall==BLUE_BALL || (env->whosBall==ANYONES_BALL && env->currentBall.pos.x<env->lastBall.pos.x) || env->currentBall.pos.x<FLEFTX+23)
			Defender_Left(env);
		else
			Attack_Left(env);
	}
	else
		Attack_Left(env);
}


//主攻
void Attack_cz1( Robot *robot, Environment *env , double x, double y )
{
//	if ( t.y > env->fieldBounds.top - 2.5 ) t.y = env->fieldBounds.top - 2.5;//边角调整
//	if ( t.y < env->fieldBounds.bottom + 2.5 ) t.y = env->fieldBounds.bottom + 2.5;
//	if ( t.x > env->fieldBounds.right - 3 ) t.x = env->fieldBounds.right - 3;
//	if ( t.x < env->fieldBounds.left + 3 ) t.x = env->fieldBounds.left + 3;
	if( env->currentBall.pos.y > 41.8 )//球在上半区域
	{
		if( env->currentBall.pos.x < 28.0 )
		{
			if( atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) < PI/4 )
			{
				Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
			}
			else if( leng( robot->pos.x, 18., robot->pos.y, 18. ) > 2.5 )
			{
				Position1_cz( robot, x, y );
			}
			else
			{
				AngleOfPosition( robot, 6.8, 41.8 );
			}
		}
		else if( env->currentBall.pos.x < 40.0 )
		{
			Position1_cz( robot, x, y );
		}
		else
		{
			Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
		}
	}
	else//球在下半区域
	{
		if( env->currentBall.pos.x < 28.0 )
		{
			if( atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) < PI/4 )
			{
				Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
			}
			else if( leng( robot->pos.x, 18., robot->pos.y, 18. ) > 2.5 )
			{
				Position1_cz( robot, 83.6-x, y );
			}
			else
			{
				AngleOfPosition( robot, 6.8, 41.8 );
			}
		}
		else if( env->currentBall.pos.x < 40.0 )
		{
			Position1_cz( robot, 83.6-x, y );
		}
		else
		{
			Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
		}
	}
}

//助攻
void Attack_cz2( Robot *robot, Environment *env )
{
	
	if( env->currentBall.pos.x < 20.0 )
	{
		kick( env, robot, 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
	}
	else if( env->currentBall.pos.x < 40.0 )
	{
		double x= 1.3*env->predictedBall.pos.x -0.3*6.82 ;//系数1.3----1.3(x-X)+X 
		double y= ( env->predictedBall.pos.y + 41.8 )/2 ;//定位于球右中的适当位置
		Position1_cz( robot, x, y );
	}
	else
		Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );

}
void NormalGame_Right( Environment *env )//右队总策略
{	
	//动态分配角色
	int i, j=0, flag[5];
	double leng[5] ; 
	for(i=1;i<=4;i++)
	{
		leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);   
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(leng[i]>=leng[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}
	
	if(env->currentBall.pos.x>(FRIGHTX+FLEFTX)/2)//球在己方半场
	{
		//防守
		if(env->whosBall==YELLOW_BALL || (env->whosBall==ANYONES_BALL &&
	env->currentBall.pos.x>env->lastBall.pos.x) || env->currentBall.pos.x>FRIGHTX-23)
		{
			Defender_Right( env );
//			Defend1_Right( env );

		}
		//进攻
		else
		{
//			Attack_Right(env);
			Position1_cz( &env->home [flag[1]], 82.0, 30.0 );//防守
			kick( env, &env->home [flag[2]], 180 );
			kick( env, &env->home [flag[3]], 180 );
			kick( env, &env->home [flag[4]], 180 );
		}
	}
	else
	{
//		Attack_Right(env);
//		kick( env, &env->home [], 13*PI/6 );
//		Attack_cz2( &env->home [3], env );
		kick( env, &env->home [flag[1]], 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
		kick( env, &env->home [flag[2]], 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
		
		Attack_cz1( &env->home [flag[3]], env , 18.0, 18.0 );
		Attack_cz1( &env->home [flag[4]], env , 30.0, 42.0 );		
	}
}


void Goalkeeper_Right_py( Robot *robot, Environment *env )//右队总策略
{	
	PredictBall ( env );
	if(env->currentBall.pos.x>(FRIGHTX+FLEFTX)/2)//球在己方半场
	{
		//防守,守门员重点防守
		if(env->whosBall==YELLOW_BALL || (env->whosBall==ANYONES_BALL &&
	env->currentBall.pos.x>env->lastBall.pos.x) || env->currentBall.pos.x>FRIGHTX-23)
		{
//			Defend1_Right( env );

		}
		//进攻
		else
		{
//			Attack_Right(env);

		}
	}
	//进攻,守门员校正
	else
	{
	
		
	}
}

void Defender_Left(Environment *env)
{
	if(env->currentBall.pos.x<FLEFTX+7 && env->currentBall.pos.y<GTOPY+3 
		&& env->currentBall.pos.y>GBOTY-3)//球在禁区
		Defend3_Left(env);
	else
	{
		if(env->currentBall.pos.x<FLEFTX+7)
			Defend2_Left(env);//底线防守阵形
		else
			Defend1_Left(env);//一般防守阵形
	}	
}

void Defender_Right(Environment *env)
{
	if(env->currentBall.pos.x>FRIGHTX-7 && env->currentBall.pos.y<GTOPY+3 
		&& env->currentBall.pos.y>GBOTY-3)//球在禁区
		Defend3_Right(env);
	else
	{
		if(env->currentBall.pos.x>FRIGHTX-7)
			Defend2_Right(env);//底线防守阵形
		else
			Defend1_Right(env);//一般防守阵形
	}
}

void Defend1_Left(Environment *env)
{
	int count=0;
	double leng[5];
	leng[0]=0;
	int i,flag[5];
	for(i=1;i<=4;i++)
	{
		if(env->home[i].pos.x>=env->currentBall.pos.x)
			count++;
	}
	for(i=1;i<=4;i++)
	{
		leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);   
		flag[i]=i;
	}
	for(i=1;i<=4;i++)
	{
		for(int j=i+1;j<=4;j++)
		{
			if(leng[i]>=leng[j])
			{
				int temp=flag[i];
				flag[i]=flag[j];
				flag[j]=temp;
			}
		}
	}

	if(count==0 || count==1 ||count==2)
	{
		Defence1_Left(&env->home[flag[1]],env);
		
		if(env->home[flag[2]].pos.x>=env->currentBall.pos.x)
		{
			if(env->currentBall.pos.y<=(FTOP+FBOT)/2)
				GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y+20);
			else
				GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y-20);
		}
		else
		{
			if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x-10,env->currentBall.pos.y))
			{
				AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y);
			}
		}

		if(env->currentBall.pos.y<=(FBOT+FTOP)/2)
		{
			if(GoaliePosition(&env->home[flag[3]],env->currentBall.pos.x-10,env->currentBall.pos.y+10))
			{
				AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
			}
		}
		else
		{
			if(GoaliePosition(&env->home[flag[3]],env->currentBall.pos.x-10,env->currentBall.pos.y-10))
			{
				AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
			}
		}
		
		double estimate_x=FLEFTX+8;
		double estimate_y=env->currentBall.pos.y;

⌨️ 快捷键说明

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