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

📄 avoidbullet.c

📁 偶然收集到的一个C语言源码
💻 C
字号:
#include <airobot/c/SimpleRobot.h>

/*计算目标点时采用的搜索半径*/
#define SEARCH_DIS 100
/*每次迭代的递增量*/
#define GAP toRadians(1)
/*最大的存储子弹的数量*/
#define MAX_BULLET 10

/*虚拟子弹*/
struct VirtualBullet
{
	int valid; /*判断子弹是否有效*/
	double fireX, fireY; /*发弹的坐标*/
	double x, y; /*子弹当前的坐标*/	
	double heading; /*子弹飞行的方向*/
	double velocity; /*子弹飞行的速度*/
};
struct VirtualBullet bullets[MAX_BULLET];

/*返回下一个可用的子弹的位置*/
int getNextBullet(void);
/*更新子弹的信息*/
void updateBullets(void);
void updateBullet(struct VirtualBullet*);

/*计算目标点*/
void getDestination(double* x, double* y);
/*计算指定目标点的风险系数*/
double getRisk(double x, double y);


void onTick(struct TickAction* action)
{
	double x, y;
	updateBullets();
	getDestination(&x, &y);
	moveTo(x, y);
}

void onFire(struct FireAction* action)
{
	int index;
	struct Bot* fireBot = getBotById(action->bullet.owner);
	if(isTeammate(fireBot)) return;
	
	index = getNextBullet();
	if(index<0) return;
	
	bullets[index].valid = 1;
	bullets[index].fireX = action->bullet.fireX;
	bullets[index].fireY = action->bullet.fireY;
	bullets[index].x = action->bullet.fireX;
	bullets[index].y = action->bullet.fireY;
	bullets[index].velocity = getBulletVelocity(action->bullet.power);
	bullets[index].heading = heading(bullets[index].fireX, bullets[index].fireY, getX(), getY());
}
		
void onRoundBegin(struct RoundBeginAction* action)
{
	int i;
	for(i=0; i<MAX_BULLET; i++) bullets[i].valid = 0;
}

int getNextBullet()
{
	int i;
	for(i=0; i<MAX_BULLET; i++)
	{
		if(!bullets[i].valid) return i;
	}
	return -1;
}

void updateBullets(void)
{
	int i;
	for(i=0; i<MAX_BULLET; i++)
	{
		if(bullets[i].valid) updateBullet(&(bullets[i]));
	}
}

void updateBullet(struct VirtualBullet* bullet)
{
	double nextX, nextY;

	nextPoint(bullet->x, bullet->y, bullet->heading, bullet->velocity, &nextX, &nextY);
	bullet->x = nextX;
	bullet->y = nextY;
	
	if(distance(bullet->fireX, bullet->fireY, bullet->x, bullet->y)>
		distance(bullet->fireX, bullet->fireY, getX(), getY()))
	{
		bullet->valid = 0;
	}
}

void getDestination(double* x, double* y)
{
	double bestRist = 100000000;
	int i;
	for(i=0; i*GAP<2*PI; i++)
	{
		double rist, nextX, nextY;
		//下一个点
		nextPoint(getX(), getY(), i*GAP, SEARCH_DIS, &nextX, &nextY);
		//保证该点在场地中
		if(!inCourt(nextX, nextY, RADIUS)) continue;
		//比较该点的危险系数
		rist = getRisk(nextX, nextY);
		if(rist<bestRist)
		{
			*x = nextX;
			*y = nextY;
			bestRist = rist;
		}
	}
}

double getRisk(double x, double y)
{
	int i;
	double rist = 0;
	for(i=0; i<MAX_BULLET; i++)
	{
		double dis;
		if(!bullets[i].valid) continue;
		dis = distance(x, y, bullets[i].x, bullets[i].y);
		rist += 1/dis;
	}
	return rist;
}


/**
 * 机器人程序入口
 */
int main(int argC, char* argV[])
{
	tickHook = onTick;
	roundBeginHook = onRoundBegin;
	fireHook = onFire;
	return startup(argC, argV);
}

																	

⌨️ 快捷键说明

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