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

📄 strategy.cpp

📁 2002年
💻 CPP
字号:
#include "strategy.h"
#include "worldmodel.h"
#include "skill.h"
#include "perceptron.h"
#include "agent.h"
#include "log.h"

/*********************  Situation  *************************/
/************** Keep record of situation  *******************************/
Situation::Situation():MyScore(0),TheirScore(0),CurrentTime(-1),playmode(PM_No_Mode),
           LastInterruptTime(-1),ClockStopped(false),ModeChangedTime(-1){
	num_hearmodes = 0;
	IsGoalieKick = false;
	IT_continue = false;
}

Time Situation::LastCycle(){
	return (!ClockStopped || situation.ClockStoppedTime == 0) ?
		situation.CurrentTime - 1 : situation.CurrentTime;
}

void Situation::HearMode(PlayMode playmode){
	if(num_hearmodes >= CP_max_hearmodes){
		num_hearmodes = 0;
	}
	heardmode[num_hearmodes ++] = playmode;
}

void Situation::UpdateMode(){
	if(num_hearmodes > 0){
		int i;
		for(i = 0; i < num_hearmodes; i++){
			SetPlayMode(heardmode[i]);
		}
		num_hearmodes = 0;
	}
}

void Situation::SetPlayMode(PlayMode playmode){
	this->playmode = playmode;
	OnChangeMode(playmode);
}

void Situation::ForgetAll(){
	int i;
	for(i=1; i<= SP_team_size; i++){
		MyPlayer(i).ForceInValid();
		MyPlayer(i).pos_conf = 0;
		TheirPlayer(i).ForceInValid();
		TheirPlayer(i).pos_conf = 0;
	}
	Self.RestoreValid();
}

void Situation::OnChangeMode(PlayMode playmode){
	ModeChangedTime = CurrentTime;

	if(playmode != PM_Play_On)
		ForgetAll();

	switch (playmode){
	case PM_Before_Kick_Off :
	case PM_My_Offside_Kick :
	case PM_My_Free_Kick_Fault:
	case PM_My_BackPass:
		IsGoalieKick = false;
		SetPlayState=SPST_Initialize;
		DoLog(LOG_SETPLAY,"initialized");
		break;	
	case PM_Play_On:
		break;
	case PM_My_Goalie_Free_Kick:
		IsGoalieKick = true;
		Agent::setplay.goaliesetplay.movecount=ServerParam::goalie_max_moves; 
		break;
	case PM_My_Goal_Kick :
		IsGoalieKick = true;
	case PM_My_Kick_Off :
	case PM_My_Corner_Kick :
	case PM_My_Kick_In :
	case PM_My_Free_Kick :
		DoLog(LOG_SETPLAY,"Mode Changed");
		SetPlayState=SPST_Initialize;
		break;	

	case PM_Their_Offside_Kick :
	case PM_Their_Free_Kick_Fault:
	case PM_Their_BackPass:
		break;
	case PM_Drop_Ball :
		DoLog(LOG_SETPLAY,"Drop Ball");
		SetPlayState=SPST_AfterExec;
		ball.forget();
		situation.IsGoalieKick = false;
		break;
	default:
		break;
	}
}

void Situation::LogGameState(){
	switch (SetPlayState){
	case SPST_Initialize:
		DoLog(LOG_SETPLAY,"Initialization");
		break;
	case SPST_Prego:
		DoLog(LOG_SETPLAY,"PreExecute Going");
		break;
	case SPST_ExecWait:
		DoLog(LOG_SETPLAY,"Waiting For Execute");
		break;
	case SPST_Executing:
		DoLog(LOG_SETPLAY,"Executing");
		break;
	case SPST_AfterExec:
		DoLog(LOG_SETPLAY,"Play Mode");
		break;
	default:
		DoLog(LOG_SETPLAY,"UnExpected");
		break;
	}
	if (IsGoalieKick)
		DoLog(LOG_SETPLAY,"Is Goalie Kick");
}



void Situation::UpdateScore(char Side){
	if(Side == MySide)
		MyScore++;
	else
		TheirScore++;
}

void Situation::SetKickOffMode(KickOff_Mode kickoff_mode){
	this->kickoff_mode = kickoff_mode;
}

void Situation::UpdateTime(Time time){
	CurrentTime = time;
}

void Situation::JudgeSituation(){
	int num, i;
//Judge about control of the ball
	num_my_ballcontrollers = 0;
	num_their_ballcontrollers = 0;
	if(ball.distance < 30.0f || ball.pos_conf > 0.9f){
		num = FieldInfo.Num_MyVisiblePlayers();
		for(i = 0; i < num; i ++){
			if (FieldInfo.MyPlayer_Close2Ball(i).balldist > 4.0f){
			//to far to be the controller of ball
				break;
			}
			if (FieldInfo.MyPlayer_Close2Ball(i).Iscontrolball){
				My_Ballcontrollers[num_my_ballcontrollers++] = FieldInfo.MyPlayers_Close2Ball[i];
			}
		}
		num = FieldInfo.Num_TheirVisiblePlayers();
		for(i =0; i < num; i ++){
			if (FieldInfo.TheirPlayer_Close2Ball(i).balldist > 4.0f){
			//to far to be the controller of ball
				break;
			}
			if (FieldInfo.TheirPlayer_Close2Ball(i).Iscontrolball){
				Their_Ballcontrollers[num_their_ballcontrollers++] = FieldInfo.TheirPlayers_Close2Ball[i];
			}
		}		
	}
	num_ballcontrollers = num_my_ballcontrollers + num_their_ballcontrollers;

//for the sake of compatibility
	if (num_their_ballcontrollers > 0){
		ballcontroller = Their_Ballcontrollers[0];
		if (ballcontroller <= SP_team_size)
			ballcontroller += SP_team_size;
	}
	else if (num_my_ballcontrollers > 0){
		ballcontroller = My_Ballcontrollers[0];
	}
	else if (ball.kickable()){
		ballcontroller = Agent::MyNumber;
	}
	else{
		ballcontroller = -1;
	}
	BallFree = bool(ballcontroller == -1);
	/*if(ball.distance > Self.kickable_area && FieldInfo.GetPlayer_Close2Ball(0).balldist > 0.9f){
		NoiseBallFree = FieldInfo.GetPlayer_Close2Ball(0).balldist
	}*/

//TeamControl
	conf_of_control = ball.pos_conf;
	if(!BallFree){
		if (num_their_ballcontrollers > 0){
			if (num_my_ballcontrollers > 0 || ball.kickable()){
				Team_Control = 0.5f;
			}
			else{
				Team_Control = 0.0f;
			}
		}
		else{
			Team_Control = 1.0f;
		}
		IT_continue = false;
	}
	else{
		if(IT_continue && ball.Find_Kick()){
			IT_continue = false;
		}
		Skill::interception.SetInterceptionInfo(!IT_continue);
		IT_continue = true;
	}

	if (Self.attackness > 0.5f)
		IsAttack = bool(Team_Control >= 0.5f);
	else
		IsAttack = bool(Team_Control >= 0.6f);
	if(!BallFree){
		DoLog(LOG_SITUATION, "Ball is controlled by %d", ballcontroller);
	}
	else{
		DoLog(LOG_SITUATION, "most promising contoller is %d at (%.2f %.2f)", most_promising_controller, GetPlayer(situation.most_promising_controller).pos.x,
			GetPlayer(situation.most_promising_controller).pos.y);
	}

	if(!BallFree){
		most_promising_controller = ballcontroller;
		min_IT_cycles = 0;
	}
}

bool Situation::Ballwithincontrol(char side, UNum No){
	if (ball.pos_conf < 0.85 || GetPlayer(side, No).pos_conf < 0.8) return false;
	return bool(GetPlayer(side, No).balldist < ClientParam::control_margin);
}

bool Situation::IsControllerOrFutureController(char side, UNum No){
	if (side == TheirSide && No > 0 && No <= SP_team_size){
		return IsControllerOrFutureController(No + SP_team_size);
	}
	return IsControllerOrFutureController(No);
}

bool Situation::IsControllerOrFutureController(UNum No){
	return (No == ballcontroller || No == most_promising_controller) && No > 0;
}

Vector Situation::NextControlBallPos(){
	return (!BallFree || ball.speed_conf < 0.8f || !Player::IsValidNo(most_promising_controller)) ? ball.pos : next_interception_point;
}


/*****************   Formation   ***************************/
Formation::Formation(){
}

void Formation::SetFormation(){
}

Vector Formation::GetOffensivePoint(float attackness, float leftness, Vector ballpos){
	input[0] = ballpos.x / ServerParam::semi_pitch_length;
	input[1] = -fabs(ballpos.y) / ServerParam::semi_pitch_width;
	input[2] = attackness;
	if (ballpos.y < 0)
		input[3] = leftness;
	else
		input[3] = 1 - leftness;
	FMOffenseNet.SimulateNet(&input[0], &output[0]);
	if (ballpos.y > 0)
		output[1] *=-1;
	return Vector(float(ServerParam::semi_pitch_length * output[0]), float(ServerParam::semi_pitch_width * output[1]));
}

Vector Formation::GetDefensivePoint(const Role & role, Vector ballpos){
	return GetDefensivePoint(role.attackness, role.leftness, ballpos);
}

Vector Formation::GetDefensivePoint(float attackness, float leftness, Vector ballpos){
	Vector fm_defense_point;
	
	pose_limitation(ballpos.x, - ServerParam::semi_pitch_length, ServerParam::semi_pitch_length);
	pose_limitation(ballpos.y, - ServerParam::semi_pitch_width, ServerParam::semi_pitch_width);
	input[0] = ballpos.x / ServerParam::semi_pitch_length;
	input[1] = fabs(ballpos.y) / ServerParam::semi_pitch_width;
	input[2] = attackness;
	if (ballpos.y < 0)
		input[3] = leftness;
	else
		input[3] = 1 - leftness;
	FMDefenseNet.SimulateNet(&input[0], &output[0]);
	if (ballpos.y > 0){
		fm_defense_point = Vector(ServerParam::semi_pitch_length * float(output[0]), ServerParam::semi_pitch_width * float(output[1]));
	}
	else{
		fm_defense_point = Vector(ServerParam::semi_pitch_length * float(output[0]), -ServerParam::semi_pitch_width * float(output[1]));
	}
	return fm_defense_point;
}


Vector Formation::GetFormationPoint(const Player& player){
	Vector ballpos = situation.NextControlBallPos();

	if (situation.IsAttack){
		return GetOffensivePoint(player.attackness, player.leftness, ballpos);
	}
	else{
		return GetDefensivePoint(player.attackness, player.leftness, ballpos);
	}
}

Vector Formation::GetFormationPoint(){
	return GetFormationPoint(Self);
}


⌨️ 快捷键说明

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