Beispiel #1
0
void Player::Run()
{
    //TIMETEST("Run");

	static Time last_time = Time(-100, 0);

	mpObserver->Lock();

	/** 下面几个更新顺序不能变 */
	//Formation::instance.SetTeammateFormations(); //Hereo
	CommunicateSystem::instance().Update(); //在这里解析hear信息,必须首先更新
	mpAgent->CheckCommands(mpObserver);
	mpWorldModel->Update(mpObserver);

	mpObserver->UnLock();
	
    const Time & time = mpAgent->GetWorldState().CurrentTime();

	if (last_time.T() >= 0) {
		if (time != Time(last_time.T() + 1, 0) && time != Time(last_time.T(), last_time.S() + 1)) {
			if (time == last_time) {
				mpAgent->World().SetCurrentTime(Time(last_time.T(), last_time.S() + 1)); //否则决策数据更新会出问题
			}
		}
	}

	last_time = time;

    //Look here - Player logic starts from here
	PlayMode mpCurrentPlayMode = mpAgent->World().GetPlayMode();
	PlayerState mpCurrentPlayerState = mpAgent->GetSelf();
	PositionInfo mpCurrentPositionInfo = mpAgent->Info().GetPositionInfo();
    Vector myPosition = mpAgent->GetSelf().GetPos();
    double myDisToBall = mpCurrentPositionInfo.GetBallDistToPlayer(mpAgent->GetSelfUnum());
    Vector ballpos = mpAgent->GetWorldState().GetBall().GetPos();

    if(mpCurrentPlayMode==PM_Before_Kick_Off){
    	//TODO: Replace with an array + loop
        //std::cout<<PlayerParam::instance().teamName()<<std::endl;
        if(!isPositioned){
    		if(mpAgent->GetSelfUnum() == 1){
    			Vector player_pos = Vector(-45.0, 0.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
            }
    		if(mpAgent->GetSelfUnum() == 2){
    			Vector player_pos = Vector(-45.0, 10.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 3){
    			Vector player_pos = Vector(-45.0, -10.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 4){
    			Vector player_pos = Vector(-20.0, 0.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 5){
    			Vector player_pos = Vector(-20.0, -20.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 6){
    			Vector player_pos = Vector(-20.0, 20.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 7){
    			Vector player_pos = Vector(-15.0, -15.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 8){
    			Vector player_pos = Vector(-15.0, 15.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 9){
    			Vector player_pos = Vector(0.0, -20.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 10){
    			Vector player_pos = Vector(0.0, 1.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
    		if(mpAgent->GetSelfUnum() == 11){
    			Vector player_pos = Vector(0.0, 20.0);
    			mpAgent->Move(player_pos);
    			isPositioned = true;
    		}
            std::cout << "Positioned" << std::endl;
    	}
    }

    else {  
        if(mpAgent->GetFollowBall()){
            if(mpCurrentPlayerState.IsKickable()){
                std::cout<<"player "<< mpAgent->GetSelfUnum()<<" - ball kickable"<<std::endl;
                Vector nearestHole = RoundToNearestHole(myPosition);
                if(PassPlayersAvailable()){
                    std::cout <<"--------------------------------------------------------"<<std::endl;
                    std::cout<<"player "<< mpAgent->GetSelfUnum()<<" - pass players available"<<std::endl;
                    if(PassToBestPlayer())
                        mpAgent->SetFollowBall(false);
                }
                else{
                    std::cout<<"player "<< mpAgent->GetSelfUnum()<<" - holding ball"<<std::endl;
                    ActiveBehavior beh = ActiveBehavior(*mpAgent, BT_Hold);
                    if (beh.GetType() != BT_None) {
                        mpAgent->SetActiveBehaviorInAct(beh.GetType());
                        if(beh.Execute());
                        //mpAgent->SetFollowBall(false);
                            //std::cout<<"Holding ball."<<std::endl;
                        }
                }
            }
            else
                Dasher::instance().GetBall(*mpAgent, -1, true, false);
            //std::cout<<"Player "<<mpAgent->GetSelfUnum() <<" following ball"<<std::endl;
            //std::cout<<mpAgent->RecvdMsg<<std::endl;
            //mpIntransit = true;
            //mpTarget = ballpos;
            //mpAgent->SetFollowBall(false);
        }
        
        /*
        if(mpAgent->GetFollowBall()){
            mpIntransit = true;
            mpTarget = ballpos;
        }
        */

        //TODO: Use transit variable for faster calling of the OccupyHole/Dasher functions
        //TODO: Better dash function
        //TODO: Look at buffer values
        //TODO: Find why ball holding is competed by players
        //TODO: Look at TODOs in the header file
        //TODO: Create a 'holding'/'waiting' variable which is true when the player is sspposed to kick the ball    
        
        
        else if(mpIntransit){
            if(!AreSamePoints(myPosition, mpTarget, 0.3))
                Dasher::instance().GoToPoint(*mpAgent, mpTarget, 0.3, 100, true, false);
            else{
                mpIntransit = false;
                //mpAgent->SetFollowBall(false);
            }
        }
        
        else if(mpCurrentPlayerState.IsKickable()){
            //mpAgent->SetFollowBall(false);
            //mpAgent->SetFollowBall(false);
            //&&!BallKickableByATeammate()
            //if suitable holes have players available, pass
            //else, hold on to ball
            std::cout<<"player "<< mpAgent->GetSelfUnum()<<" - ball kickable"<<std::endl;
            Vector nearestHole = RoundToNearestHole(myPosition);
            if(PassPlayersAvailable()){
                std::cout <<"--------------------------------------------------------"<<std::endl;
                std::cout<<"player "<< mpAgent->GetSelfUnum()<<" - pass players available"<<std::endl;
                if(PassToBestPlayer())
                  mpAgent->SetFollowBall(false);
            }
            else{
                std::cout<<"player "<< mpAgent->GetSelfUnum()<<" - holding ball"<<std::endl;
                ActiveBehavior beh = ActiveBehavior(*mpAgent, BT_Hold);
                if (beh.GetType() != BT_None) {
                    mpAgent->SetActiveBehaviorInAct(beh.GetType());
                    if(beh.Execute());
                    //mpAgent->SetFollowBall(false);
                        //std::cout<<"Holding ball."<<std::endl;
                }
            }
               
    	}
        
               
        else if(myDisToBall<=1){
            //Dasher::instance().GoToPoint(*mpAgent, ballpos, 0.3, 100, true, false);
            std::cout<<"player "<< mpAgent->GetSelfUnum()<<" - in transit towards ball"<<std::endl;
            //mpIntransit = true;
            //mpTarget = ballpos;
            Dasher::instance().GetBall(*mpAgent, -1, true, false);
            //mpAgent->SetFollowBall(true);
        }
        
        

        else {
            //if(myDisToBall>1)
            if(BallKickableByATeammate()){
                std::cout<<"ball kickable by teammate - will decide and occupy hole - "<<mpAgent->GetSelfUnum()<<std::endl;
                DecideAndOccupyHole();
            }
            //else if(mpAgent->GetSelfUnum()==10);
                //Dasher::instance().GoToPoint(*mpAgent, ballpos, 0.3, 100, true, false);
            //if ball is with a player
                //if player has empty holes, dash to the hole
                //else, do nothing
            //else, do nothing
        }
    }
    	
	//Formation::instance.UpdateOpponentRole(); //TODO: 暂时放在这里,教练未发来对手阵型信息时自己先计算 //Hereo

	VisualSystem::instance().ResetVisualRequest();
	//mpDecisionTree->Decision(*mpAgent); // TODO:V: Replace or remove //Hereo

	VisualSystem::instance().Decision();
	CommunicateSystem::instance().Decision();

    mpAgent->SetHistoryActiveBehaviors(); //Hereo

	Logger::instance().LogSight();
}
void BehaviorPassPlanner::Plan(std::list<ActiveBehavior> & behavior_list)
{

	std::cout<<"Inside pass plan"<<std::endl;
	if (!mSelfState.IsKickable()) return;

	const std::vector<Unum> & tm2ball = mPositionInfo.GetCloseTeammateToTeammate(mSelfState.GetUnum());

	Unum _opp = mStrategy.GetFastestOpp();
    if (!_opp) return;

	PlayerState oppState = mWorldState.GetOpponent( _opp );
	bool oppClose = oppState.IsKickable()|| oppState.GetTackleProb(true) > 0.65 ;
	for (uint i = 0; i < tm2ball.size(); ++i) {
		ActiveBehavior pass(mAgent, BT_Pass);

		pass.mTarget = mWorldState.GetTeammate(tm2ball[i]).GetPredictedPos();

		if(mWorldState.GetTeammate(tm2ball[i]).IsGoalie()){
			continue;
		}
		Vector rel_target = pass.mTarget - mBallState.GetPos();
		const std::vector<Unum> & opp2tm = mPositionInfo.GetCloseOpponentToTeammate(tm2ball[i]);
		AngleDeg min_differ = HUGE_VALUE;

		for (uint j = 0; j < opp2tm.size(); ++j) {
			Vector rel_pos = mWorldState.GetOpponent(opp2tm[j]).GetPos() - mBallState.GetPos();
			if (rel_pos.Mod() > rel_target.Mod() + 3.0) continue;

			AngleDeg differ = GetAngleDegDiffer(rel_target.Dir(), rel_pos.Dir());
			if (differ < min_differ) {
				min_differ = differ;
			}
		}

		if (min_differ < 10.0) continue;

		pass.mEvaluation = Evaluation::instance().EvaluatePosition(pass.mTarget, true);

		pass.mAngle = (pass.mTarget - mSelfState.GetPos()).Dir();
		pass.mKickSpeed = ServerParam::instance().GetBallSpeed(5, pass.mTarget.Dist(mBallState.GetPos()));
		pass.mKickSpeed = MinMax(2.0, pass.mKickSpeed, Kicker::instance().GetMaxSpeed(mAgent , pass.mAngle ,3 ));
		if(oppClose){//in oppnent control, clear it
			pass.mDetailType = BDT_Pass_Clear;
		}
		else pass.mDetailType = BDT_Pass_Direct;
		mActiveBehaviorList.push_back(pass);
	}
	if (!mActiveBehaviorList.empty()) {
		mActiveBehaviorList.sort(std::greater<ActiveBehavior>());
		if(mActiveBehaviorList.front().mDetailType == BDT_Pass_Clear){
			mActiveBehaviorList.front().mEvaluation = 1.0 + FLOAT_EPS;
		}
		behavior_list.push_back(mActiveBehaviorList.front());
	}
	else {														//如果此周期没有好的动作
		if (mAgent.IsLastActiveBehaviorInActOf(BT_Pass)) {
			ActiveBehavior pass(mAgent, BT_Pass, BDT_Pass_Direct);
			pass.mTarget = mAgent.GetLastActiveBehaviorInAct()->mTarget; //行为保持
			pass.mEvaluation = Evaluation::instance().EvaluatePosition(pass.mTarget, true);
			pass.mKickSpeed = ServerParam::instance().GetBallSpeed(5 + random() % 6, pass.mTarget.Dist(mBallState.GetPos()));
			pass.mKickSpeed = MinMax(2.0, pass.mKickSpeed, ServerParam::instance().ballSpeedMax());
			behavior_list.push_back(pass);
		}
		if(oppClose){
			Vector p;
			BallState SimBall = mBallState;
			int MinTmInter = HUGE_VALUE, MinOppInter = HUGE_VALUE, MinTm;
			Vector MinTmPos;
			for(AngleDeg dir = -45 ; dir <= 45  ; dir += 2.5){
				if(!Tackler::instance().CanTackleToDir(mAgent,dir)){
					SimBall.UpdateVel(Polar2Vector(Kicker::instance().GetMaxSpeed(mAgent,mSelfState.GetBodyDir() + dir,1),mSelfState.GetBodyDir() + dir),0,1.0);
				}
				else
					SimBall.UpdateVel(Polar2Vector(Max(Tackler::instance().GetBallVelAfterTackle(mAgent,dir).Mod(), Kicker::instance().GetMaxSpeed(mAgent,mSelfState.GetBodyDir() + dir,1)),mSelfState.GetBodyDir() + dir),0,1.0);
				for(int i = 2 ; i <= 11 ; i ++){
					if(fabs((mWorldState.GetTeammate(i).GetPos() - mSelfState.GetPos()).Dir() - dir) > 45 ){
						continue;
					}
					if(!mWorldState.GetPlayer(i).IsAlive()){continue;}
					PlayerInterceptInfo* a = mInterceptInfo.GetPlayerInterceptInfo(i);
					mInterceptInfo.CalcTightInterception(SimBall,a,true);
					if(MinTmInter > (*a).mMinCycle){
						MinTm= i;
						MinTmInter = (*a).mMinCycle;
						MinTmPos = (*a).mInterPos;
					}
				}
				for(int i = 1 ; i <= 11 ; i++){
					if(fabs((mWorldState.GetOpponent(i).GetPos() - mSelfState.GetPos()).Dir() - dir) > 45 ){
						continue;
					}
					PlayerInterceptInfo* a = mInterceptInfo.GetPlayerInterceptInfo(-i);
					mInterceptInfo.CalcTightInterception(SimBall,a,true);
					if(MinOppInter > (*a).mMinCycle){
						MinOppInter = (*a).mMinCycle;
					}
				}
				if(MinOppInter > MinTmInter){
					Ray a(mSelfState.GetPos() , mSelfState.GetBodyDir() + dir);
					Line c(ServerParam::instance().ourLeftGoalPost(),ServerParam::instance().ourRightGoalPost());
					c.Intersection(a,p);
					if(	p.Y() < ServerParam::instance().ourPenaltyArea().Top()
							|| p.Y() > ServerParam::instance().ourPenaltyArea().Bottom()){

						ActiveBehavior pass(mAgent , BT_Pass , BDT_Pass_Clear);
						pass.mTarget = MinTmPos;
						pass.mEvaluation = 1.0 + FLOAT_EPS;
						pass.mAngle = mSelfState.GetBodyDir() + dir;
						mActiveBehaviorList.push_back(pass);
					}

				}
			}
			if (!mActiveBehaviorList.empty()) {
				mActiveBehaviorList.sort(std::greater<ActiveBehavior>());
				behavior_list.push_back(mActiveBehaviorList.front());
			}
		}

	}
}