⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 situation.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*************************************************************************** *   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 <stdarg.h>
#include "Global.h"
#include "Utils.h"
#include "Situation.h"
#include "Strategy.h"

#define nameof(p) #p
#define ADD_MAP(p) m_PlayModeStringMap[p] = nameof(p)

// global && extern variables
ITInfo Situation::our_ITInfo[11];
ITInfo Situation::opp_ITInfo[11];
ITInfo Situation::myITInfo;
int Situation::ourByITCyc[11];
int Situation::oppByITCyc[11];
int Situation::ourByDist2Ball[11];
int Situation::oppByDist2Ball[11];
string Situation::m_DebugMessage;
ServerSettings *Situation::SS;

//=======================



double Situation::shoot_prob;



double Situation::shoot_ang;



map<TPlayMode,string> Situation::m_PlayModeStringMap;







Situation::Situation()



{



	ADD_MAP(PM_BeforeKickOff);



	ADD_MAP(PM_KickOff_Our);



	ADD_MAP(PM_KickOff_Opp);



	ADD_MAP(PM_PlayOn);



	ADD_MAP(PM_KickIn_Our);



	ADD_MAP(PM_KickIn_Opp);



	ADD_MAP(PM_CORNER_KICK_Our);



	ADD_MAP(PM_CORNER_KICK_Opp);



	ADD_MAP(PM_GOAL_KICK_Our);



	ADD_MAP(PM_GOAL_KICK_Opp);



	ADD_MAP(PM_OFFSIDE_Our);



	ADD_MAP(PM_OFFSIDE_Opp);



	ADD_MAP(PM_GameOver);



	ADD_MAP(PM_Goal_Our);



	ADD_MAP(PM_Goal_Opp);



	ADD_MAP(PM_FREE_KICK_Our);



	ADD_MAP(PM_FREE_KICK_Opp);



	ADD_MAP(PM_NONE);



}







Situation::~Situation()



{







}



void Situation::updateSituation()
{
	global.wm.collisionFlag = 0;
	global.wm.nextBallPos = global.wm.ballPos + global.wm.ballVel;

	int collisionFlagWithField;
	collisionFlagWithField = 0;
	int nCycT1Me, nCycT1Field;
	Vector3 ballPosT1Me, ballPosT1Field;
	Vector3 ballVelT1Me, ballVelT1Field;
	
	global.wm.collisionFlag = isBallInCollisionWithMe(nCycT1Me, ballPosT1Me, ballVelT1Me);
	collisionFlagWithField = isBallInCollisionWithField(nCycT1Field, ballPosT1Field, ballVelT1Field);

	if(global.wm.collisionFlag){
		BallStatus ballstatus;
		ballstatus = getBallStatusAfterCollisionWithMe(nCycT1Me, ballPosT1Me, ballVelT1Me);
		global.wm.nextBallPos = ballstatus.pos;
		global.wm.ballVel = global.wm.nextBallPos - global.wm.ballPos;
		global_previous[0].wm.ballVel = global.wm.ballVel;
		global_previous[0].wm.nextBallPos = global.wm.nextBallPos;
	}
	else if(global.wm.ballPos.z > (0.11+0.14) && collisionFlagWithField){
		BallStatus ballstatus;
		ballstatus  = getBallStatusAfterCollisionWithField(nCycT1Field, ballPosT1Field, ballVelT1Field);
		global.wm.nextBallPos = ballstatus.pos;
		global.wm.ballVel = global.wm.nextBallPos - global.wm.ballPos;
		global_previous[0].wm.ballVel = global.wm.ballVel;
		global_previous[0].wm.nextBallPos = global.wm.nextBallPos;
	}

	int i,j;

//	shoot_prob = strategy.GetShootProb(shoot_ang);
//cal the intercept infomation

	AgentStatus a,b;

	b.pos = global.wm.ballPos+global.wm.ballVel;

	b.vel = global.wm.ballVel * SS->B_decay;

	b.vel.z = (global.wm.ballVel.z+14.6795*0.2)* 0.8745-14.6795*0.2;

	AgentStatus tmp ;


	for(i=0;i<11;i++){

		a.pos = global.wm.ourPos[i];

		a.vel = global.wm.ourVel[i];

		int c = predictor.predictNrITCyc(a,b);

		 //goalia catch may derease the intercyc
		if(i==0) c-=5;

//consider the air_ball

		for(our_ITInfo[i].nCyc=c;our_ITInfo[i].nCyc<100;our_ITInfo[i].nCyc++)

			if(predictor.predictBallStatusAfterNrCycle(b,our_ITInfo[i].nCyc).pos.z<=0.5)

				break;

		tmp = predictor.predictBallStatusAfterNrCycle(b,our_ITInfo[i].nCyc);

		our_ITInfo[i].IT_point = tmp.pos;

		if(tmp.pos.x<global.wm.ourPos[i].x)

			our_ITInfo[i].nCyc += 6;

		tmp = predictor.predictBallStatusAfterNrCycle(b,our_ITInfo[i].nCyc);

		our_ITInfo[i].IT_point = tmp.pos;

		our_ITInfo[i].dDist2Ball = (global.wm.ballPos-global.wm.ourPos[i]).mod();

	}
        

	for(i=0;i<11;i++){

		a.pos = global.wm.oppPos[i];

		a.vel = global.wm.oppVel[i];

		opp_ITInfo[i].nCyc = predictor.predictNrITCyc(a,b);

		 //goalia catch may derease the intercyc
		if(i==0)       opp_ITInfo[i].nCyc-=5;

		tmp = predictor.predictBallStatusAfterNrCycle(b,opp_ITInfo[i].nCyc);

		opp_ITInfo[i].IT_point = tmp.pos;

		opp_ITInfo[i].dDist2Ball = (global.wm.ballPos-global.wm.oppPos[i]).mod();

	}

	a.pos = global.wm.nextPos;

	a.vel = global.wm.nextVel;

	int c = predictor.predictNrITCyc(a,b);

//the air_ball

	for(myITInfo.nCyc=c;myITInfo.nCyc<100;myITInfo.nCyc++)

		if(predictor.predictBallStatusAfterNrCycle(b,myITInfo.nCyc).pos.z<=0.5)

			break;

	tmp = predictor.predictBallStatusAfterNrCycle(b,myITInfo.nCyc);

	myITInfo.IT_point = tmp.pos;

	for(i=0;i<11;i++){

		ourByITCyc[i] = i;

		oppByITCyc[i] = i;

		ourByDist2Ball[i] = i;

		oppByDist2Ball[i] = i;
	}

	int m, n;	

	for(i=0;i<11;i++){

		for(j=i+1;j<11;j++){

			//===========by intercept cycle

			m = ourByITCyc[i];

			n = ourByITCyc[j];

			if(our_ITInfo[m].nCyc>our_ITInfo[n].nCyc){

				ourByITCyc[i] = n;

				ourByITCyc[j] = m;

			}

			m = oppByITCyc[i];

			n = oppByITCyc[j];

			if(opp_ITInfo[m].nCyc>opp_ITInfo[n].nCyc){

				oppByITCyc[i] = n;

				oppByITCyc[j] = m;

			}

			//===========by intercept cycle end

			//===========by distance to ball

			m = ourByDist2Ball[i];

			n = ourByDist2Ball[j];

			if(our_ITInfo[m].dDist2Ball>our_ITInfo[n].dDist2Ball){

				ourByDist2Ball[i] = n;

				ourByDist2Ball[j] = m;

			}

			m = oppByDist2Ball[i];

			n = oppByDist2Ball[j];

			if(opp_ITInfo[m].dDist2Ball>opp_ITInfo[n].dDist2Ball){

				oppByDist2Ball[i] = n;

				oppByDist2Ball[j] = m;

			}

			//===========by distance to ball end

		}

	}



//======reconsider IT_Cyc of the quickest intercepters in both teams more precisely (add by zyf)====

//re-calculate

	for(i=0;i<4;i++){

		int ourfastcyc=our_ITInfo[ourByITCyc[0]].nCyc;

		double ourfastdist=our_ITInfo[ourByITCyc[0]].dDist2Ball;

		int oppfastcyc=opp_ITInfo[oppByITCyc[0]].nCyc;

		double oppfastdist=opp_ITInfo[oppByITCyc[0]].dDist2Ball;

		if(our_ITInfo[ourByITCyc[i]].nCyc<ourfastcyc+40||our_ITInfo[ourByITCyc[i]].dDist2Ball<ourfastdist+10){

			a.pos = global.wm.ourPos[ourByITCyc[i]];

   			a.vel = global.wm.ourVel[ourByITCyc[i]];

			if(ourByITCyc[i]==0)
				predictor.predictNrITCycPreciseforGoalia(a,b,our_ITInfo[0]);
				
			else
				predictor.predictNrlTCycPrecise(a, b, our_ITInfo[ourByITCyc[i]], 0);

			}

		if(opp_ITInfo[oppByITCyc[i]].nCyc<oppfastcyc+20&&opp_ITInfo[oppByITCyc[i]].dDist2Ball<oppfastdist+10){

			a.pos = global.wm.oppPos[oppByITCyc[i]];

   			a.vel = global.wm.oppVel[oppByITCyc[i]];

			if(oppByITCyc[i]==0)
				predictor.predictNrITCycPreciseforGoalia(a,b, opp_ITInfo[0]);

			else
				predictor.predictNrlTCycPrecise(a, b, opp_ITInfo[oppByITCyc[i]], 1);

			}

		}

	a.pos = global.wm.nextPos;

	a.vel = global.wm.nextVel;

	if(global.wm.myNumber==1)
		predictor.predictNrITCycPreciseforGoalia(a, b, myITInfo);

	else
		predictor.predictNrlTCycPrecise(a, b, myITInfo,0);


//resolt by intercept cycle

	for(i=0;i<4;i++){

		for(j=i+1;j<4;j++){

			m = ourByITCyc[i];

			n = ourByITCyc[j];

			if(our_ITInfo[m].nCyc>our_ITInfo[n].nCyc){

				ourByITCyc[i] = n;

				ourByITCyc[j] = m;

			}

			m = oppByITCyc[i];

			n = oppByITCyc[j];

			if(opp_ITInfo[m].nCyc>opp_ITInfo[n].nCyc){

				oppByITCyc[i] = n;

				oppByITCyc[j] = m;

			}

		}

	}

	



}







string Situation::getThinkNodeConsiderValue(string considerValue)



{



	char str[100];



	string ret;



	if(considerValue == "play_mode"){



		AddDebugMessage("current play mode is : ");



		AddDebugMessage(m_PlayModeStringMap[global.wm.pm].c_str());



		AddDebugMessage("\r\n");



		return m_PlayModeStringMap[global.wm.pm];







	}



	if(considerValue == "should_i_shoot"){



#ifdef WIN32



		AddDebugMessage("The shoot prob is %f\r\n",shoot_prob);



		AddDebugMessage(strategy.m_DebugMessage.c_str());



#endif



		if(shoot_prob> 0.90){



#ifdef WIN32



			AddDebugMessage("The shoot angle is %f\r\n",shoot_ang);



			AddDebugMessage("\r\n");



#endif



			sprintf(str,"%d",(int)shoot_ang);



			ret = "YES ";



			ret += str;



			return ret;



		}



		else{



			return "NO";



		}



	}



	if(considerValue == "should_i_intercept"){



		if(shouldIIntercept())



			return "YES";



		else



			return "NO";



	}



//	if(considerValue == 



	return string();



}







Vector3 Situation::GetVectorFromString(char* str)



{







	if(isContain(str,"FORMATION")){



		return strategy.getFormationPosition();



	}



	if(isContain(str,"VECTOR")){



		double x,y,z;



		sscanf(str,"VECTOR_%lf_%lf_%lf",&x,&y,&z);



		return Vector3(x,y,z);



	}







	return Vector3(1);



}



void Situation::AddDebugMessage(const char *str, ...)



{



	char buf[800];



	va_list ap;



	va_start( ap, str );



	vsprintf( buf, str, ap );



	va_end(ap);



	m_DebugMessage += buf;



}



//if i'm going to intercept, judge wether to stop_ball

bool Situation::shouldIStopBall()

{

	Vector3 Player2Ball=global.wm.myPos-global.wm.ballPos;

	Angle P2Bangle=fabs(Player2Ball.ang());

	if((P2Bangle>=80	&&((fabs(global.wm.ballVel.ang())>=90&&global.wm.ballVel.mod()>0.1)||isBallBeingInOppControl()||isBallInOppControl()))

		||(opp_ITInfo[oppByITCyc[0]].nCyc<myITInfo.nCyc+3&&global.wm.ballPos.getDistToOurGoal()<18))//&&global.wm.ballVel.mod()>0.2)

		return true;

	return false;



}

bool Situation::shouldICatchWhenInter()
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -