?? patternfire.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 + -