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

📄 patternfire.c

📁 偶然收集到的一个C语言源码
💻 C
字号:
/**
 * 用模式匹配算法开火的机器人
 * @author xiemin
 */
#include <airobot/c/SimpleRobot.h>

//开火时的炮弹能量
#define POWER 0.5
//用于匹配段的长度
#define MATCH_LENGHT 20
//保留历史纪录的最大长度
#define HISTORY_LENGHT 2000

//对手的速度记录
double velocityRecord[HISTORY_LENGHT];
//对手的方向记录
double headingRecord[HISTORY_LENGHT];
//当前纪录位置
int currentIndex;

//记录当前的机器人状态
void record(struct Bot* bot);
//计算最佳的匹配点
int getMatchIndex();
//得到开火的位置
void getFirePoint(int matchIndex, double power, double* fireX, double* fireY);

void onRoundBegin(struct RoundBeginAction* action)
{
	currentIndex = 0;
}

void onTick(struct TickAction* action)
{
	int matchIndex;
	double fireX, fireY;
	struct Bot* opponent = getFirstOpponent();
	if(opponent==NULL) return;
	
	record(opponent);
	matchIndex = getMatchIndex();
	getFirePoint(matchIndex, POWER, &fireX, &fireY);
	fireOnPoint(fireX, fireY, POWER);
}
		
//记录当前的机器人状态
void record(struct Bot* bot)
{
	velocityRecord[currentIndex] = bot->velocity;
	headingRecord[currentIndex] = bot->heading;
	currentIndex++;
}

//计算最佳的匹配点
int getMatchIndex()
{
	double beatSimilarity=1000000;
	int matchIndex=0, i, j;
	//这里取i<currentFrame-100是为了避免比较样本和被比较样本重复
	//和留取足够的节点给递推未来坐标用
	for(i=MATCH_LENGHT; i<currentIndex-MATCH_LENGHT; i++)
	{
		//取10个样本节点计算相似度
		double similarity=0;
		for(j=1; j<=MATCH_LENGHT; j++)
		{
			similarity+=fabs(velocityRecord[i-j]-velocityRecord[currentIndex-j]);
			similarity+=fabs(headingRecord[i-j]-headingRecord[currentIndex-j]);
			//加了权值得匹配度计算
			//similarity+=fabs(velocityRecord[i-j]-velocityRecord[currentIndex-j])/8;
			//similarity+=fabs(headingRecord[i-j]-headingRecord[currentIndex-j])/PI;
		}
		//记录最相似的相似度,以及对应的记录节点下标
	   	if(similarity<beatSimilarity)
	   	{
		    matchIndex=i;
		   	beatSimilarity=similarity;
		}
	}
	return matchIndex;
}

//得到开火的位置
void getFirePoint(int matchIndex, double power, double* fireX, double* fireY)
{
	//预测位置
   	double x = getFirstOpponent()->x;
   	double y = getFirstOpponent()->y;
   	double dis;
    int time = 0;
    while(matchIndex+time<currentIndex)
    {
    	dis = distance(getX(), getY(), x, y);
    	if(dis/getBulletVelocity(power)<=time) break;
    	nextPoint(x, y, headingRecord[matchIndex+time], 
    		velocityRecord[matchIndex+time], &x, &y);
    	time++;
    }
	*fireX = x;
	*fireY = y;
}

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


⌨️ 快捷键说明

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