📄 handleball.cpp
字号:
/*Copyright (C) 2005 Ferdowsi University*/#include "handleball.h"#include "global.h"HandleBall::HandleBall(){}bool HandleBall::MakeShootDecision(){}bool HandleBall::MakeDribleDecision(){}bool HandleBall::MakePassDecision(){}bool HandleBall::TryHandle(){ if(self.dist2ball > KickDistance) { DoLog(LOG_HANDLE,"self.dist2ball=%.2f",self.dist2ball); return false; } return MakeHandleBallDecision();}bool HandleBall::MakeHandleBallDecision(){ //float angle,power,v_angle; float dist2oppgoal=(self.pos-Vector3f(FieldLength/2,0,0)).Length(); float min_dist2goal=200; int nearest_num=1; for(int i=1;i<=11;i++) if((MyPlayer[i].pos-Vector3f(FieldLength/2,0,0)).Length()<min_dist2goal){ min_dist2goal=(MyPlayer[i].pos-Vector3f(FieldLength/2,0,0)).Length(); nearest_num=i; } float epsilon=25; if((TheirPlayer[FieldInfo.NearestOpponent2me()].distance < 2) && (self.pos[0] < 0)) ClearBall(); else if((dist2oppgoal<20||nearest_num==MyNumber)&&!((FieldLength/2-ball.pos[0])<4&&(ball.pos[1]>8||ball.pos[1]<-8))){ epsilon=10; if((ball.pos[1]>5||ball.pos[1]<-5)&&FieldLength/2-ball.pos[0]<5) epsilon=5; //find out who is their goalie float min_opp_dist2goal=200; int Theirgoalie_num=1; for(int i=1;i<=11;i++) if((TheirPlayer[i].pos-Vector3f(FieldLength/2,0,0)).Length()<min_opp_dist2goal){ min_opp_dist2goal=(TheirPlayer[i].pos-Vector3f(FieldLength/2,0,0)).Length(); Theirgoalie_num=i; } bool side=false;//false=left true=right Line line1(-(ball.pos[1]-3.66)/(ball.pos[0]-FieldLength/2),1,3.66); Line line2(-(ball.pos[1]+3.66)/(ball.pos[0]-FieldLength/2),1,-3.66); float m=GetSlopeOfMidAngle(line1.Angle(),line2.Angle()); float y=0; y=m*(TheirPlayer[Theirgoalie_num].pos[0]-ball.pos[0])+ball.pos[1]; if(y>TheirPlayer[Theirgoalie_num].pos[1]) side=false; else side=true; if(side){ action_info.kick_direction=Rad2Deg(atan2((-GoalWidth/2+3)-ball.pos[1],FieldLength/2-ball.pos[0])); DoLog(LOG_HANDLE,"right shoot angel=%f",action_info.kick_direction); if(dist2oppgoal>15) action_info.kick_angle=15; else if(dist2oppgoal>10) action_info.kick_angle=10; else if(dist2oppgoal>5) action_info.kick_angle=5; else action_info.kick_angle=2; action_info.kick_power=100; } else{ action_info.kick_direction=Rad2Deg(atan2((GoalWidth/2-3)-ball.pos[1],FieldLength/2-ball.pos[0])); DoLog(LOG_HANDLE,"left shoot angle=%f",action_info.kick_direction); if(dist2oppgoal>15) action_info.kick_angle=15; else if(dist2oppgoal>10) action_info.kick_angle=10; else if(dist2oppgoal>5) action_info.kick_angle=5; else action_info.kick_angle=2; action_info.kick_power=100; } } else{ ActionInfo a = GetBestAction(); epsilon=25; if(a.kick_power > 0) { action_info.kick_direction = a.kick_direction; action_info.kick_angle = a.kick_angle; action_info.kick_power = a.kick_power; } else DoLog(LOG_HANDLE,"Ridim Baba!"); /* if(nearest_num==MyNumber){ for(int i=1;i<=11;i++) if((MyPlayer[i].pos-Vector3f(FieldLength/2,0,0)).Length()<min_dist2goal&&i!=MyNumber){ min_dist2goal=(MyPlaaction_info.kick_directionyer[i].pos-Vector3f(FieldLength/2,0,0)).Length(); nearest_num=i; } } epsilon=20;//15//25 action_info.kick_direction=Rad2Deg(atan2(MyPlayer[nearest_num].pos[1]-ball.pos[1],(MyPlayer[nearest_num].pos[0]+2)-ball.pos[0])); DoLog(LOG_HANDLE,"+++++++++++++pass action_info.kick_direction=%f to player %d",action_info.kick_direction,nearest_num); if(MyPlayer[nearest_num].distance>20) action_info.kick_power=100; else if(MyPlayer[nearest_num].distance>10) action_info.kick_power=80; else if(MyPlayer[nearest_num].distance>5) action_info.kick_power=60; else action_info.kick_power=40; if(OpponentCanIntercept(Vector3f(action_info.kick_power/7*cos(action_info.kick_direction),action_info.kick_power/7*sin(action_info.kick_direction),0))) { action_info.kick_angle=45; action_info.kick_power+=action_info.kick_power*0.2; DoLog(LOG_HANDLE,"Opponent Can Inetrcept! Adjust vertical action_info.kick_direction & action_info.kick_power",action_info.kick_direction,nearest_num); } else action_info.kick_angle=10; */ } if(AdjustKickPos.AdjustNeeded(action_info.kick_direction,epsilon)){ Vector3f Pos=AdjustKickPos.MoveAroundBall(action_info.kick_direction); DoLog(LOG_HANDLE,"adjust needed - pos(%f,%f)",Pos[0],Pos[1]); action_info.x = Pos[0]; action_info.y = Pos[1]; action_info.do_drive = true; } else { DoLog(LOG_HANDLE,"-------Kick------- action_info.kick_power = %.2f, action_info.kick_angle = %.2f",action_info.kick_power, action_info.kick_angle); //DoShoot(action_info.kick_power,action_info.kick_angle); action_info.do_kick = true; } return true;}void HandleBall::DoShoot(float kick_power , float kick_direction){ /* if (ball.distance < KickDistance){ DoLog(LOG_HANDLE,"ball.distance=%f , KickDist=%f",ball.distance,KickDistance); action_info.kick_angle = kick_direction; action_info.kick_direction=0; action_info.kick_power = kick_power; action_info.do_kick=true; } */}void HandleBall::ClearBall(){ float angle = NormalizeAngle(Rad2Deg(atan2(self.pos[1]-ball.pos[1],self.pos[0]-ball.pos[0])),-180); if (self.pos[0] > -38 ) { if (self.pos[0] - ball.pos[0] < -0.03) { action_info.kick_direction=angle; action_info.kick_power=100; action_info.do_kick=true; DoLog (LOG_HANDLE,"SHOOTIDIMESH1......."); } } else if (self.pos[0] < -38) { if (self.pos[0] - ball.pos[0] < -0.03 && fabs(ball.pos[1]) - fabs(self.pos[1]) > -0.07) { action_info.kick_direction=angle; action_info.kick_power=100; action_info.do_kick=true; DoLog (LOG_HANDLE,"SHOOTIDIMESH2......."); } } if(!OpponentCanIntercept(Vector3f(10*cos(angle),10*sin(angle),0))) action_info.kick_angle=5; else { action_info.kick_angle=15; DoLog (LOG_HANDLE,"Opponente namard!......."); }}ActionInfo HandleBall::GetBestAction(){ bool passable = false; bool dribblable = false; PassInfo p; DribbleInfo d; ActionInfo a; float agent_angle = NormalizeAngle(Rad2Deg(atan2((ball.pos[1]-self.pos[1]),(ball.pos[0]-self.pos[0]))),-180.0); passable = pass.CanPass(); //dribblable = dribble.CanDribble(); /* if(passable && dribblable){ PassInfo p = pass.GetBestPass(); DribbleInfo d = dribble.GetBestDribble(); if(fabs(agent_angle - p.direction) < fabs(agent_angle - d.direction)) { //do pass a.kick_power = p.speed; a.kick_direction = p.direction; a.kick_angle = p.angle; a.do_kick = true; DoLog(LOG_HANDLE, "Passing...1"); return a; } else { //do dribble a.kick_power = d.speed; a.kick_direction = d.direction; a.kick_angle = d.angle; a.do_kick = true; DoLog(LOG_HANDLE, "Dribbling...1"); return a; } } */ if(passable) { PassInfo p = pass.GetBestPass(); a.kick_power = p.speed; a.kick_direction = p.direction; a.kick_angle = p.angle; a.do_kick = true; DoLog(LOG_HANDLE, "Passing...2"); return a; } /* if(dribblable) { DribbleInfo d = dribble.GetBestDribble(); a.kick_power = d.speed; a.kick_direction = d.direction; a.kick_angle = d.angle; a.do_kick = true; DoLog(LOG_HANDLE, "Dribbling...2"); return a; } */ return a;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -