📄 strategy.cpp
字号:
/*************************************************************************** * Copyright (C) 2004 - 2006 by ZJUBase *
* National Lab of Industrial Control Tech. * * Zhejiang University, China * * * * Achievements of ZJUBase in RoboCup Soccer 3D Simulation League: *
* In RoboCup championship, *
* - June 2006 - 3rd place in RoboCup 2006, Bremen, Germany (lost * * the semi-final with a coin toss) *
* - July 2005 - 3rd place in RoboCup 2005, Osaka, Japan (share the * * 3rd place with team Caspian) *
* In RoboCup local events, *
* - April 2006 - 2nd place in RoboCup Iran Open 2006, Tehran, Iran * * (lost the final with a coin toss) *
* - December 2005 - 2nd place in AI Games 2005, Isfahan, Iran *
* - July 2005 - champion in China Robot Competition 2005, Changzhou, * * China *
* - October 2004 - champion in China Soccer Robot Competition 2004, * * Guangzhou, China *
* * * Team members: *
* Currently the team leader is, * * Hao JIANG (jianghao@iipc.zju.edu.cn; riveria@gmail.com) *
* In the next season, the leader will be * * Yifeng ZHANG (yfzhang@iipc.zju.edu.cn) *
* ZJUBase 3D agent is created by * * Dijun LUO (djluo@iipc.zju.edu.cn) *
* All the members who has ever contributed: * * Jun JIANG *
* Xinfeng DU (xfdu@iipc.zju.edu.cn) *
* Yang ZHOU (yzhou@iipc.zju.edu.cn) *
* Zhipeng YANG *
* Xiang FAN *
* *
* Team Manager: *
* Ms. Rong XIONG (rxiong@iipc.zju.edu.cn) *
* *
* If you met any problems or you have something to discuss about * * ZJUBase. Please feel free to contact us through EMails given below. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#include "Global.h"#include "Strategy.h"
Strategy strategy;
#include "Situation.h"#include <stdarg.h>#include "math.h"
extern Situation *ST;extern ControlPoint position_cp;static ServerSettings *SS;
#define for if(0); else for
double theta[3][4];double dist[3][4];double coefficientDist[3][4][3]={{{0,0,13},{0,-0.162107,5},{0.0426,0.6261,5},{0,0,13}},{{0,-0.4,9},{0,0,10},{0,0,10}},{{0,0.55,-9},{0,0,14},{0,0,14}}};double coefficientTheta[3][4][2]={{{-1.2067,180-97.3558},{1.35,180},{8.37552,0},{1.2067,97.3558-180}},{{0,180},{-2.0439,67.4176},{2.0439,-67.4176}},{{0,0},{-1.7585,180-88.1536},{1.7585,88.1536-180}}};double coefficientghost[3][3];double a[3][2]={{0.35,0.5},{0.4,0.5},{0.3,0.4}}; //what's a stand for please refer to "ghost and ball" by dxf in 111:simulate:dxfdouble m[3]={0.74,0.4,0.03};
Strategy::Strategy(void){}
Strategy::~Strategy(void){}
void Strategy::Init(ServerSettings* ss){ SS=ss; coefficientGhost(SS->mFieldLength);}
Vector3 Strategy::getFormationPosition(){ return SBSPPosition(global.wm.myNumber);}
double Strategy::GetShootProb(Angle &ang){ Vector3 goal(52.5,0,0); Angle ang_ball_me = (global.wm.ballPos - global.wm.myPos).ang(); Angle ang_gall_ball = (goal - global.wm.ballPos).ang();
if(fabs(ang_ball_me - ang_gall_ball)<20&&(global.wm.ballPos - global.wm.myPos).mod()<1.101){ ang = 0; return 1.0; }
return 0.0;}
//////////////////OriginalPosition///////////////
Vector3 Strategy::SBSPPosition(int num)
{
Vector3 pos(0,0,0);
if(num<1||num>11)
return pos;
if (num == 1)
{
pos.x = -SS->mFieldLength/2+3;
pos.y = global.wm.ballPos.y*(pos.x + SS->mFieldLength/2)
/(global.wm.ballPos.x + SS->mFieldLength/2);
if (fabs(pos.y) > SS->mGoalWidth/2)
{
if (pos.y > 0)
pos.y = SS->mGoalWidth/2;
else pos.y = -SS->mGoalWidth/2;
}
return pos;
}
else{
pos.x=getX( num-1, global.wm.ballPos.x, global.wm.ballPos.y);
pos.y=getY(num-1,global.wm.ballPos.y,global.wm.ballPos.x);
pos.z=0.22;
return OffSide(pos,num);
}
}
////////////////PositionSet/////////////////////
/*
Vector3 Strategy::SBSPPosition(int num)
{
Vector3 pos(0,0,0);
if(num<1||num>11)
return pos;
pos=OffSide(getPosition(num,global.wm.ballPos.x,global.wm.ballPos.y),num);
return pos;
if((global.wm.ballPos.x>-20&&num>=9&&num<=11&&((global.wm.ballPos-global.wm.myPos).mod()<40))){
if((global.wm.myPos-pos).mod()<5){
pos=OffSide(getBestRunPoint(pos),num);
return pos;
}
else
return OffSide(pos,num);
}
else{
return OffSide(pos,num);
}
}
*/
////////////////////offside, add by dxf
Vector3 Strategy::OffSide(Vector3 pos,int num)
{
double x=pos.x;
if (global.wm.pm == PM_PlayOn) {
// find the offside line
double off_x[2] = {0, 0};
for (int i = 0; i < 11; ++i) {
double xx = global.wm.oppPos[i].x + global.wm.oppVel[i].x * 5;
double yy = global.wm.oppPos[i].y + global.wm.oppVel[i].y * 5;
// do not consider the opponents out from the field
if (fabs(xx) > X(100) || fabs(yy) > Y(100)) continue;
// find the two players with largest X scale
if (xx > off_x[1]) {
off_x[1] = xx;
if (off_x[1] > off_x[0]) {
swap(off_x[0], off_x[1]);
}
}
}
off_x[1] = max(off_x[1], global.wm.ballPos.x);
#ifdef WIN32
ShowLine(Vector3(off_x[1], -Y(100), 0), Vector3(off_x[1], Y(100), 0), "255 128 128");
#endif
// find the player with largest X scale of strategic position in our team
double forward_x = -X(100);
for (int i = 0; i < 11; ++i) {
double xx = getX(i, global.wm.ballPos.x, global.wm.ballPos.y);
forward_x = max(forward_x, xx);
}
// adjust the strategic position
double back_dis_para[12] = {0, 0, 0, 0, 0, 0, 0.25, 0.5, 0.5, 1.0, 1.0, 1.0};
x -= back_dis_para[num] * max(0, forward_x - off_x[1] + 1.5);
}
pos.x=x;
#ifdef WIN32
ShowCircle(pos, 0.5, "128 255 255 2");
#endif
return pos;
}
////////////////////////////
////////////////////// OriginalFormation/////////////add by dxf
void Strategy::coefficientGhost(double length)
{
for(int i=0;i<3;i++){
coefficientghost[i][0]=1.0/(2*length)*(a[i][1]-a[i][0]);
coefficientghost[i][1]=1.0/2.0*(a[i][1]+a[i][0]);
coefficientghost[i][2]=3.0*length/8.0*a[i][0]+length/8.0*a[i][1]-length/2.0*m[i];
}
}//calculate the coefficient of ghost, using parameter field-length
double PolarX(double dist,double theta)
{
return dist*cos(theta*3.1415926/180);
}
double PolarY(double dist,double theta)
{
return dist*sin(theta*3.1415926/180);
}
int getI(int num)
{
if(num<1)
return 4;
if(num>0&&num<5)
return 0;
if(num>4&&num<8)
return 1;
if(num>7)
return 2;
return 4;
}//calculate group number
int getJ(int num)
{
int i;
i=getI(num);
if(i==0)
return num-1;
if(i==1)
return num-5;
if(i==2)
return num-8;
return -1;
}//calculate member number in group
double getJ(int num,double ballX,double ballY)
{
double j;
if(num<1)
return 0.2;
if(num>0&&num<5) {
if(ballX>21)
return j=0.2;
else
return j=-0.0027*ballX+0.2584;
};
if(num>4&&num<8) {
if(ballX>52.5||ballX<-52.5)
return j=0.3;
else if(ballX<-36||ballX==-36||ballX==-52.5)
return j=0.0061*ballX+0.6196;
else if(ballX>36||ballX==36||ballX==52.5)
return j=-0.0061*ballX+0.6196;
else return j=0.4;
};
if(num>7){
if(ballX<-21)
return j=0.2;
else
return j=0.0027*ballX+0.2584;
}
return 0;
}//calculate parameter of ghost's Y position according to ballX
double Strategy::getGhostX(int num,double ballX)
{
int i;
double x;
i=getI(num);
if(i==4)
return x=0.0927*ballX-46.3659;
x=coefficientghost[i][0]*pow(ballX,2)+coefficientghost[i][1]*ballX+coefficientghost[i][2];
return x;
}////calculate ghost's X position
double Strategy::getGhostY(int num,double ballX,double ballY)
{
double j;
j=getJ(num, ballX,ballY);
return j*ballY;
}//calculate ghost's X position
void Strategy::getCoefficient(int num,double ballX,double ballY)
{
int i,j;
double ghostX,theta1,theta2,dist1,dist2;
static int k=0,m=-1,l=0;
static double bx=ballX;
i=getI(num);
j=getJ(num);
ghostX=getGhostX(num, ballX);
if(i==0&&j>0&&j<3) {
if(global.wm.pm == PM_BeforeKickOff || global.wm.pm ==PM_KickOff_Our ||
global.wm.pm ==PM_KickOff_Opp ||
(global.wm.ourPos[2] - global.wm.ourPos[3]).mod() < 2){
k=0,m=-1,l=0;
}
if(ballX<40.753){
if((bx>40.753||bx==40.753)) {
if(abs(theta[0][1])>=abs(theta[0][2]))
l=1;
else
l=-1;
if(ballY>0) {
k=1;m=1;
}
else {
k=-1;m=-1;
}
}//sign decide when exchang roles of two center defenders
for(j=1;j<3;j++){
dist[i][j]=coefficientDist[i][j][0]*pow(ghostX,2)+coefficientDist[i][j][1]*ghostX+coefficientDist[i][j][2];
theta[i][j]=coefficientTheta[i][j][0]*ghostX+coefficientTheta[i][j][1];
}
}
if(ballX<0){
theta1=-m*155;
theta2=-m*(-155);
dist1=8;
dist2=8;
}
if(ballX>=0&&ballX<40.753){
if(ballY>8)
m=1;
if(ballY<-8)
m=-1;
theta1=-m*theta[0][1];
theta2=-m*theta[0][2];
dist1=dist[0][1];
dist2=dist[0][2];
}
if(ballX>=40.753){
theta1=180;
theta2=0;
dist1=5;
dist2=5;
}//piecewise function of two center defenders
if(((k*m==1)&&l==1)||((k*m==-1)&&l==-1)||k==0){
theta[0][1]=theta1;
theta[0][2]=theta2;
dist[0][1]=dist1;
dist[0][2]=dist2;
}
else{
theta[0][1]=theta2;
theta[0][2]=theta1;
dist[0][1]=dist2;
dist[0][2]=dist1;
}
bx=ballX;
}//solve two center defenders' role exchange
if(num!=2&&num!=3){
dist[i][j]=coefficientDist[i][j][0]*pow(ghostX,2)+coefficientDist[i][j][1]*ghostX+coefficientDist[i][j][2];
theta[i][j]=coefficientTheta[i][j][0]*ghostX+coefficientTheta[i][j][1];
}
}
double Strategy::getX(int num, double ballX,double ballY)
{
int i,j;
double ghostX,x ;
ghostX=getGhostX(num, ballX);
i=getI(num);
if(i==4)
return ghostX;
j=getJ(num);
getCoefficient(num,ballX,ballY);
x=PolarX(dist[i][j],theta[i][j]);
x=ghostX+x;
return x;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -