?? arsplfield.cpp
字號:
#include "StdAfx.h"
#include "ARSPlField.h"
#include <math.h>
int ARSPlField::ms_rand = 0;
ARSPlField::ARSPlField()
{
dKExclude = 1;
dKAttract = 10;
ms_rand = 0;
}
ARSAction* ARSPlField::CreateAction()
{
ARSPlField * tmp = new ARSPlField;
return tmp;
}
bool ARSPlField::ExecuteAction(const ARSSTUATION & stSituation, MOVEDIRECTION & nextDirection)
{
int iPoliceID = stSituation.iID-1;
ARSPOINT ptPoliceSelf = stSituation.ptPloice[iPoliceID];
ARSPOINT ptThief = stSituation.ptThief;
double dSumX = 0,dSumY = 0,dField,dR;
dR = sqrt((ptThief.x - ptPoliceSelf.x)*(ptThief.x - ptPoliceSelf.x)+(ptThief.y - ptPoliceSelf.y)*(ptThief.y - ptPoliceSelf.y));
dField = dKAttract/dR;
dSumX = dField*(ptThief.x - ptPoliceSelf.x)/dR;
dSumY = dField*(ptThief.y - ptPoliceSelf.y)/dR;
for (int i = 0;i < stSituation.ptPloice.size();i++)
{
if (i==iPoliceID)
continue;
dR = sqrt((ptPoliceSelf.x - stSituation.ptPloice[i].x)*(ptPoliceSelf.x - stSituation.ptPloice[i].x)
+(ptPoliceSelf.y - stSituation.ptPloice[i].y)*(ptPoliceSelf.y - stSituation.ptPloice[i].y));
dField = dKExclude/dR;
dSumX += dField*(ptPoliceSelf.x - stSituation.ptPloice[i].x)/dR;
dSumY += dField*(ptPoliceSelf.y - stSituation.ptPloice[i].y)/dR;
}
double dx,dy;
dx = fabs(dSumX);
dy = fabs(dSumY);
int d;
if (dx >=dy)
{
if (dSumX>0)
{
nextDirection = RIGHT;
d = 1;
}
else
{
nextDirection = LEFT;
d = -1;
}
for (i = 0;i < stSituation.ptPloice.size();i++)
{
if (i==iPoliceID)
continue;
if (ptPoliceSelf.x+d == stSituation.ptPloice[i].x&&ptPoliceSelf.y == stSituation.ptPloice[i].y
||ptPoliceSelf.x+d == ptThief.x&&ptPoliceSelf.y == ptThief.y)
{
if (dSumY<0.0005)
{
ms_rand+=2;
if (ms_rand%2)
{
nextDirection = STILL;
break;
}
}
if (dSumY>0)
nextDirection = UP;
else
nextDirection = DOWN;
break;
}
}
}
else
{
if (dSumY>0)
{
nextDirection = UP;
d = 1;
}
else
{
nextDirection = DOWN;
d = -1;
}
for (i = 0;i < stSituation.ptPloice.size();i++)
{
if (i==iPoliceID)
continue;
if (ptPoliceSelf.x == stSituation.ptPloice[i].x&&ptPoliceSelf.y+d == stSituation.ptPloice[i].y
||ptPoliceSelf.x == ptThief.x&&ptPoliceSelf.y+d == ptThief.y)
{
if (dSumX<0.0005)
{
if ((ms_rand++)%2)
{
nextDirection = STILL;
break;
}
}
if (dSumX>0)
nextDirection = RIGHT;
else
nextDirection = LEFT;
break;
}
}
}
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -