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

📄 main.c

📁 以AIROBOT为平台构建的一个智能机器人。主源码见main.c。其余为使用的函数接口定义
💻 C
字号:
/*
 这是一个 c 机器人模板.
 以下列出的函数是用户必须定义的函数。
 你可以根据需要在下面列出的执行函数和事件处理函数中加入代码,定义自己的机器人。
*/
#include "Robot.h"
#include "Utils.h"
#include "Math.h"
#include <stdlib.h>
/////////////////////////////////////////////////////////////
int ne=0;
int donothing=1;
int d=1;
void detecNear(void);
void changeDirection(double a,double b);
void doMove(void);
void doRadar(void);
void doGun(int);
int m=1;
int i=0;
double C=100;
double getEnergy();
double r=1;
double v=8;
int back=0;
int j=0;
void doGun0();
/**
 * 执行函数
 */
void work()
{
	
	doRadar();
    detecNear();
    i++;
  if ((getBodyTurnRemaining()==0)&&(ne==0)&&(i%(rand()%3+1)==0)&&(donothing==1))
 {
	changeDirection(rand()%((int)map.height),(rand()%(int)map.width));
 }

	
  if (getBodyTurnRemaining()==0&&ne==1&&donothing==1)
 {
	 changeDirection(map.height/2,map.width/2 );
	 donothing=0;
 }
 else if(getBodyTurnRemaining()==0&&ne!=donothing&&donothing!=2)
 {
	 doMove();
 }
  else if(getBodyTurnRemaining()==0&&ne==0&&donothing==0)
 {
	 doMove();
	 donothing=1;
 }
  if(distance(getBodyX(),getBodyY(),scannedRobotEvent.x, scannedRobotEvent.y)<100&&donothing!=2)
  {
	  doGun(3);
	 fire(3);
	  
  }
  else if(distance(getBodyX(),getBodyY(),scannedRobotEvent.x, scannedRobotEvent.y)<150)
  {
	  doGun(2);
	  fire(2);
  }
  else if(distance(getBodyX(),getBodyY(),scannedRobotEvent.x, scannedRobotEvent.y)<180)
  {
	  doGun(1);
	  fire(1);
  }
  else if(donothing!=2)
	  doGun0();
if (((distance(getBodyX(),getBodyY(),scannedRobotEvent.x,scannedRobotEvent.y)<100)&&(getEnergy()>scannedRobotEvent.energy+36))||((distance(getBodyX(),getBodyY(),scannedRobotEvent.x,scannedRobotEvent.y)<60)&&(getEnergy()>scannedRobotEvent.energy+10)))
 {
	donothing=2;
 }
	if (donothing==2)
 {
	double h = heading(getBodyX(),getBodyY(),scannedRobotEvent.x,scannedRobotEvent.y);
	double angle = turnAngle(getBodyHeading(),h);
	turnBody(angle);
	doGun(3);
	fire(3);
 }
	if ((distance(getBodyX(),getBodyY(),scannedRobotEvent.x,scannedRobotEvent.y)<60)&&donothing!=2)
	{
    
	doGun(3);
	fire(3);
	}
	else v=8;
 moveBody(v);
 
 r=(rand()%10)/20.0+0.5;

}


 

//////////////////////////////////////////////////////////////
//事件处理函数,相应事件发生时被调用一次


/*
 * 当开始一轮新的战斗时触发
 */
void onBegin()
{
  detecNear();
  if (ne==1) donothing=1;
  else donothing=0;
  scannedRobotEvent.time =-5;
  j=0;
  d=1;
}

/*
* 当雷达扫描到敌人时触发
*/
void onScannedRobot()
{
 
}
	
/*
* 当打中某个敌人时触发
*/
void onBulletHit()
{
 
}
	
/*
 * 当被敌人打中时触发
 */
void onHitByBullet()
{
 
}
	
/*
 * 当撞到其它敌人时触发
 */
void onHitRobot()
{
 
 doGun0();
 fire(3);
 

}
	
/*
 * 当撞到墙时触发
 */
void onHitWall()
{
 if(donothing!=2)
	 turnBody(PI/2);
}
	
/*
 * 当某个敌人死时触发
 */
void onRobotDeath()
{
 
}
	
/*
 * 当一轮战斗结束时触发
 */
void onFinish()
{
 
}

/*
 * 当机器人执行操作超时时触发
 */
void onOvertime()
{
 
}
//改变方向//
void changeDirection(double a,double b)
{
	double angle;
	double h = heading(getBodyX(),getBodyY(),a ,b);
    angle = turnAngle(getBodyHeading(),h);
	turnBody(angle-d*r*PI/2);
	
}

//yundong//
void doMove()
{
	turnBody(d*r*PI);
	d=-d;
}
//near//
void detecNear()
{
	if((getBodyX()>((map.height)-C))||(getBodyX()<C)||(getBodyY()>((map.width)-C))||(getBodyY()<C))
	{
		ne=1;
	}
	else 
	{
		ne=0;
	}

}
//控制雷达单元 
void doRadar()
	{
		//雷达锁定
		if( getTime() - scannedRobotEvent.time > 3 ) turnRadar(PI);
		else{
			//雷达转向敌人,以实现锁定
			double h = heading(getBodyX(),getBodyY(),scannedRobotEvent.x, scannedRobotEvent.y); 
			double angle = turnAngle(getRadarHeading(),h); 
			turnRadar(angle);               
		}
	}
void doGun(int p)//提前量射击
{
	double angle,h,pv,gunangle,v;
 	v=scannedRobotEvent.velocity;//敌人运动速度
	h=heading(scannedRobotEvent.x, scannedRobotEvent.y,getBodyX(),getBodyY());//敌人和自己连线的方向(自己的x,y,敌人的x,y)
	angle=turnAngle(scannedRobotEvent.heading,h);//需要转过的角度
	pv=20-4.*p/3;//子弹速度
	gunangle=asin(v*sin(angle)/pv);
	gunangle=gunangle+(h-PI);//提前射击的方向
	angle=turnAngle(getGunHeading(),gunangle);//提前射击炮口需要转过的角度
	turnGun(angle);//转炮口
	
}
void doGun0(){
	double h = heading( getBodyX(), getBodyY(), scannedRobotEvent.x, scannedRobotEvent.y);
	double angle = turnAngle(getGunHeading(),h);
	turnGun(angle);
	
}

⌨️ 快捷键说明

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