#include 'Robot.h'
#include 'Utils.h'
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
double velocity; //运动速度
double moveTime; //朝一个方向运动的时间
//函数声明
void changeDirection(void);
void doRadar(void);
double velocity;
void moveTo(double x, double y)
{
double h = heading(getBodyX(),getBodyY(),x,y);
double angle = turnAngle(getBodyHeading(),h);
turnBody(angle);
moveBody(velocity);
}
static double POWER;
#define LongW 20
#define HisLong 20000
double vMem[HisLong];
double hMem[HisLong];
static int nowI;
void record(struct ScannedRobotEvent* bot);
int getSml();
void reachfire(int matchIndex, double power, double* fireX, double*
fireY);
void record(struct ScannedRobotEvent* bot)
{
vMem[nowI] = bot->velocity;
hMem[nowI] = bot->heading;
nowI++;
}
int getSml()
{
double bS=1000000;
int mI=0, i, j;
for(i=LongW; i<nowI-LongW; i++)
{
double similarity=0;
for(j=1; j<=LongW; j++)
{
similarity+=abs(vMem[i-j]-vMem[nowI-j]);
similarity+=abs(hMem[i-j]-hMem[nowI-j]);
}
if(similarity<bS)
{
mI=i;
bS=similarity;
}
}
return mI;
}
double next(double x,double y,double heading1, double
dis1, double* nextX, double* nextY)
{
*nextX=dis1*cos(heading1)+x;
*nextY=dis1*sin(heading1)+y;
return 0.0;
}
void reachfire(int matchIndex, double power, double* fireX,
double* fireY)
{
double x =scannedRobotEvent.x;
double y = scannedRobotEvent.y;
double dis;
int time = 0;
while(matchIndex+time<nowI)
{
dis = distance(getBodyX(), getBodyY(), x,
y);
if(dis/(20.0-3.0*power)<=time)
break;
next(x, y, hMem[matchIndex+time],
vMem[matchIndex+time], &x,
&y);
time++;
}
*fireX = x;
*fireY = y;
}
void valuearea(double x,double y,double head,double* nx,double
*ny)
{
double h=heading(x,y,*nx,*ny);
double s=distance(x,y,*nx,*ny);
double tmp;
double x1=s*cos(h-head);
double y1=s*sin(h-head);
if (abs(x1)>abs(y1))
{
tmp=x1;
x1=y1;
y1=tmp;
*nx=s*cos(h+head)+x;
*ny=s*sin(h+head)+y;
}
}
//执行函数
void work()
{
int matchIndex;
double fireX, fireY;
double h;
double angle;
double nx;
double ny;
int pointX;
int pointY;
char buf[10];
struct ScannedRobotEvent oppo;
struct ScannedRobotEvent* opponent = &oppo;
oppo=scannedRobotEvent;
doRadar();
if(opponent==NULL) return;
record(opponent);
matchIndex = getSml();
reachfire(matchIndex, POWER, &fireX, &fireY);
h = heading(getBodyX(), getBodyY(), fireX, fireY);
angle = turnAngle(getGunHeading(),h);
turnGun(angle);
//println(buf);
fire(POWER);
pointX = rand() % 8;
srand( (unsigned)time( NULL ) );
pointY = rand() % 8;
nx=pointX + getBodyX()-4;
ny=pointY + getBodyY()-4;
h=heading(getBodyX(),getBodyY(),scannedRobotEvent.x,scannedRobotEvent.y);
valuearea(getBodyX(),getBodyY(),h,&nx,&ny);
moveTo(nx,ny);
}
void onBegin()
{
scannedRobotEvent.time = -5;
velocity = 20;
moveTime = 25;
}
void onHitRobot()
{
changeDirection();
}
void onHitWall()
{
changeDirection();
}
void onHitByBullet()
{
}
void doRadar()
{
if( getTime()- scannedRobotEvent.time > 3
)turnRadar(PI);
else{
double h = heading( getBodyX(),getBodyY(),
scannedRobotEvent.x, scannedRobotEvent.y
);
double angle = turnAngle(getRadarHeading(),h);
double
s=distance(getBodyX(),getBodyY(),scannedRobotEvent.x,
scannedRobotEvent.y);
turnRadar(angle);
POWER=(1-s/700.0)*1.0+0.3;
if (s<200) POWER+=0.5;
if (s<100) POWER+=0.9;
if (s<50) POWER+=1.2;
}
}
void changeDirection()
{
velocity = -velocity;
}
void onRobotDeath(){}
void onFinish(){}
void onOvertime(){}
void onBulletHit(){}
void onScannedRobot(){}