#include <airobot/c/SimpleRobot.h>
/**
* 用统计学瞄准的机器人
* @author xiemin
*/
#define BEARING_GAP 0.0523 /*统计概率时角度的间隔,3度*/
#define POWER 0.1 /*发弹的火力*/
#define DISTANCE_GAP 100 /*按距离分区的间隔*/
#define FACTOR_SIZE 30 /*概率分布数组的大小*/
#define STATIST_SIZE 15 /*总的分区数*/
#define WAVE_SIZE 60 /*波的个数*/
/*概率*/
struct Statist
{
//概率分布数组
double factors[FACTOR_SIZE];
};
/*波,用于统计敌人移动的概率*/
struct Wave
{
int statist; /*当前分区下的概率分布*/
double center;/*波的中心方向*/
double startX, startY;/*波的开始位置*/
double velocity;/*波的速度*/
long startTime;/*波出发的时间*/
int active;/*波是否是活动的*/
};
struct Statist statists[STATIST_SIZE];
struct Wave waves[WAVE_SIZE];
int getIndex(double bearing); /* 得到指定的夹角在概率数组中的索引*/
int getHeightestIndex(int statist); /* 得到概率最高的索引 */
double getBestFireBearing(int statist); /* 得到最好的开火夹角 */
void rate(int statist, double bearing); /* 提高指定角度下的概率值 */
void addWave(struct Bot*); /*添加一个Wave*/
int getNextWave(void); /*返回下一个空闲的Wave位置*/
int getStatist(struct Bot*); /*返回与Bot对应的statist*/
void updateWaves(struct Bot*); /*更新所有的Wave*/
void updateWave(int wave, long time, double x, double y); /*更新Wave*/
void doMove(void); /*执行移动*/
void doFire(struct Bot* bot);/*执行开火操作*/
void onTick(struct TickAction* action)
{
struct Bot* opponent = getFirstOpponent();
if(opponent==NULL) return;
addWave(opponent);
updateWaves(opponent);
doFire(opponent);
doMove();
}
void onRoundBegin(struct RoundBeginAction* action)
{
//清除上一轮发射的波
int i;
for(i=0; i<WAVE_SIZE; i++)
waves[i].active = 0;
}
int getIndex(double bearing)
{
return FACTOR_SIZE/2+(int)(bearing/BEARING_GAP);
}
int getHeightestIndex(int statist)
{
int heightestIndex = 0;
int i;
for(i=0; i<FACTOR_SIZE; i++)
{
if(statists[statist].factors[i]>statists[statist].factors[heightestIndex])
{
heightestIndex = i;
}
}
return heightestIndex;
}
void rate(int statist, double bearing)
{
int i;
statists[statist].factors[getIndex(bearing)]+=0.01;
for(i=0; i<FACTOR_SIZE; i++)
statists[statist].factors[i] *= 0.99;
}
double getBestFireBearing(int statist)
{
return (getHeightestIndex(statist)-FACTOR_SIZE/2)*BEARING_GAP;
}
void addWave(struct Bot* bot)
{
int wave = getNextWave();
if(wave<0) return;
waves[wave].active = 1;
waves[wave].statist = getStatist(bot);
waves[wave].center = heading(getX(), getY(), bot->x, bot->y);
waves[wave].startX = getX();
waves[wave].startY = getY();
waves[wave].startTime = getTime();
waves[wave].velocity = getBulletVelocity(POWER);
}
void updateWaves(struct Bot* bot)
{
int i;
for(i=0; i<WAVE_SIZE; i++)
{
if(waves[i].active)
{
updateWave(i, getTime(), bot->x, bot->y);
}
}
}
void updateWave(int wave, long time, double x, double y)
{
//检查是否撞到了对手
double d1 = distance(waves[wave].startX, waves[wave].startY, x, y);
double d2 = waves[wave].velocity*(time-waves[wave].startTime);
//hited!!
if(d2>=d1)
{
double h = heading(waves[wave].startX, waves[wave].startY, x, y);
//计算开火的夹角
double b = bearing(h, waves[wave].center);
//更新这个夹角的命中率
rate(waves[wave].statist, b);
waves[wave].active = 0;
}
}
int getNextWave(void)
{
int i;
for(i=0; i<WAVE_SIZE; i++)
{
if(!waves[i].active) return i;
}
return -1;
}
int getStatist(struct Bot* bot)
{
double d = distance(getX(), getY(), bot->x, bot->y);
return (int)(d/DISTANCE_GAP);
}
void doMove(void)
{
//移动到场地的中央
moveTo(getCourtWidth()/2, getCourtHeight()/2);
}
void doFire(struct Bot* bot)
{
if(getFirePrepareTime()<=0)
{
int statist = getStatist(bot);
double b = getBestFireBearing(statist);
double h = heading(getX(), getY(), bot->x, bot->y);
fire(h+b, POWER);
}
}
/**
* 机器人程序入口
*/
int main(int argC, char* argV[])
{
tickHook = onTick;
roundBeginHook = onRoundBegin;
return startup(argC, argV);
}
没有合适的资源?快使用搜索试试~ 我知道了~
C语言学习利器—AI-CODE坦克机器人
共78个文件
xml:30个
c:28个
obj:9个
需积分: 14 7 下载量 91 浏览量
2009-12-28
13:23:56
上传
评论 1
收藏 384KB RAR 举报
温馨提示
《C语言学习利器—AI-CODE坦克机器人》-钟民-
资源推荐
资源详情
资源评论
收起资源包目录
C语言Ai-Tank.rar (78个子文件)
《C语言学习利器—AI-CODE坦克机器人》-钟民-源代码-4528
8章
AvoidBullet
AvoidBullet.tds 384KB
AvoidBullet.participant.xml 894B
AvoidBullet.obj 4KB
AvoidBullet.c 3KB
AvoidBullet.cc.project.xml 2KB
AvoidWall
AvoidWall.c 1KB
AvoidWall.participant.xml 1014B
5章
Circle
Circle.participant.xml 914B
Circle.c 288B
Walls
Walls.participant.xml 906B
Walls.c 1KB
Firgure82
Firgure82.c 1KB
SuperCenter
SuperCenter.c 440B
Firgure81
Figure8.obj 3KB
Figure8.cc.project.xml 2KB
Figure8.participant.xml 886B
Figure8.tds 384KB
Figure8.c 717B
6章
FireLine
FireLine.participant.xml 954B
FireLine.c 811B
FireOriginal
FireOriginal.participant.xml 911B
FireOriginal.c 544B
FireFirst
FireFirst.c 1KB
FireCircle
FireCircle.c 1KB
FireCircle.participant.xml 952B
4章
first
first.c 2KB
11章
Filer
filer 8B
Filer.tds 384KB
Filer.cc.project.xml 2KB
Filer.participant.xml 882B
Filer.obj 3KB
Filer.c 1KB
3章
Center
Center.rcu 22KB
Center.participant.xml 436B
Center.c 466B
7章
Collection
Collection.c 537B
Collection.obj 3KB
Collection.tds 384KB
Collection.participant.xml 892B
Collection.cc.project.xml 2KB
13章
Drawer
Drawer.cc.project.xml 2KB
Drawer.tds 384KB
Drawer.obj 3KB
Drawer.c 1KB
Drawer.participant.xml 884B
9章
PatternFire
PatternFire.c 3KB
PatternFire.participant.xml 924B
Wrap系列
Wrap.participant.xml 901B
WrapRandom.c 3KB
WrapAvoidWall.c 3KB
WrapKeepDis.participant.xml 981B
Wrap.c 1KB
WrapRandom.participant.xml 1021B
WrapAvoidWall.participant.xml 997B
WrapKeepDis.c 2KB
StatistFire
StatistFire.obj 4KB
StatistFire.participant.xml 894B
StatistFire.c 4KB
StatistFire.tds 384KB
StatistFire.cc.project.xml 2KB
12章
Worm
Worm.c 2KB
16章
Dancer
Dancer.obj 3KB
Dancer.participant.xml 884B
Dancer.c 1KB
Dancer.tds 384KB
Dancer.cc.project.xml 2KB
RamFire
RamFire.obj 3KB
RamFire.c 578B
RamFire.tds 384KB
RamFire.cc.project.xml 2KB
RamFire.participant.xml 886B
Dolly
Dolly.c 1KB
Shining
Shining.obj 3KB
Shining.cc.project.xml 2KB
Shining.participant.xml 886B
Shining.tds 384KB
Shining.c 555B
Tracker
Tracker.c 492B
共 78 条
- 1
资源评论
loll1011
- 粉丝: 2
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功