📄 memory.c
字号:
/* memory.c * CMUnited-97 (soccer client for Robocup-97) * Peter Stone <pstone@cs.cmu.edu> * Computer Science Department * Carnegie Mellon University * Copyright (C) 1997 Peter Stone * * CMUnited-97 was created by Peter Stone and Manuela Veloso * * You may copy and distribute this program freely as long as you retain this notice. * If you make any changes or have any comments we would appreciate a message. */#include "global.h"#define ACTIVE_DEBUG 0/*--------------------------------------------------------------*/Memory::Memory() : PositionInfo(), TeamPositionInfo(), RewardInfo(){ NewSight = FALSE; StoppedClockTime = 0; strcpy(new_sound,BLANK_MSG); ClearActives(); ClearStatuses(); ClearData(); ClearTheirSounds(); ChangePositions = FALSE; UseUnitCoaching = FALSE; EchoSounds = FALSE; UseSetPlays = FALSE; SetPlay = FALSE; CurrentSetPlayFormation = (SetPlayFormation *) new SetPlayFormation; MoveRecorded = FALSE; LastDashPower = LastDashTimes = 0; PreDashGlobalX = PreDashGlobalY = 0; stamina_estimate = STAMINA_MAX;}Memory::~Memory(){ delete CurrentSetPlayFormation;}Memory::Initialize(const char side, const int number, char *team_name, char *mode, int sock){ PositionInfo::Initialize(side, number, team_name); socket = sock; my_team_status[0] = my_team_status[number]; /* Again, keep own status in 0 */ my_team_data[0] = my_team_data[number]; if ( mode[0] == 'b' ){ /* Before_kick_off */ PlayMode = BEFORE_KICK_OFF; if ( side == 'l' ) KickOffMode = MY_KICK_OFF; else KickOffMode = THEIR_KICK_OFF; } else /* Act as if the game's in progress */ PlayMode = PLAY_ON;}void Memory::Reset(){ PositionInfo::Reset(); if ( GetCurrentFormation() != NULL ) TeamPositionInfo::Reset(); SetMyselfInactive(); ClearStatuses(); ClearData();}void Memory::Dashing(float power){ PositionInfo::Dashing(power); LastDashPower=power; stamina_estimate -= (int)power; if ( stamina_estimate < 0 ) stamina_estimate = 0;}void Memory::Tick(int marker,int time){ stamina_estimate += STAMINA_INC*(time - CurrentTime); /* Since it's before PI::Tick */ if (stamina_estimate > STAMINA_MAX) stamina_estimate = STAMINA_MAX; PositionInfo::Tick(marker,time);}#if 0float Memory::GetPlayerDistance(char side, int number){ if ( number > TEAM_SIZE ) /* It's a position, not a uniform number */ number = GetPositionHolder(number); return PositionInfo::GetPlayerDistance(side, number);}float Memory::GetPlayerAngle(char side, int number){ if ( number > TEAM_SIZE ) /* It's a position, not a uniform number */ number = GetPositionHolder(number); return PositionInfo::GetPlayerAngle(side, number);}float Memory::PlayerDistanceValid(char side, int number){ if ( number > TEAM_SIZE ) /* It's a position, not a uniform number */ number = GetPositionHolder(number); return PositionInfo::PlayerDistanceValid(side, number);}float Memory::PlayerAngleValid(char side, int number){ if ( number > TEAM_SIZE ) /* It's a position, not a uniform number */ number = GetPositionHolder(number); return PositionInfo::PlayerAngleValid(side, number);}float Memory::PlayerValid(char side, int number){ if ( number > TEAM_SIZE ) /* It's a position, not a uniform number */ number = GetPositionHolder(number); return PositionInfo::PlayerValid(side, number);}#endifint Memory::FindTeammateWithStatus(char *status){/* if ( !strcmp(status,RECEIVER_STAT) ) printf("2: %s 3: %s\n",my_team_status[2],my_team_status[3]);*/ for (int i=1; i<TEAM_SIZE+1; i++){ if ( !strcmp(my_team_status[i], status) ) return i; } return FALSE;}int Memory::FindTeammatesWithStatus(char *status, int *teammates){ int NumWithStatus = 0; for (int i=1; i<TEAM_SIZE+1; i++){ if ( !strcmp(my_team_status[i], status) ){ teammates[NumWithStatus] = i; NumWithStatus++; } } return NumWithStatus;}int Memory::SortPlayersBy(char side, char KeyFunc, float KeyNum, int* players){ int result = 0; /*Number of players sorted */ float (PositionInfo::*KeyFunction)(char,int); KeyFunction = (( KeyFunc == 'd' ) ? &PositionInfo::GetPlayerDistance : &PositionInfo::GetPlayerAngle); float (PositionInfo::*TestFunction)(char,int); TestFunction = (( KeyFunc == 'd' ) ? &PositionInfo::GetPlayerDistance : &PositionInfo::GetPlayerAngle); int num = (( side == 'b') ? TEAM_SIZE*2 : TEAM_SIZE); /* Make aux array big enough */ float vals[num]; char team = (( side == 't' ) ? TheirSide : MySide); for (int i=1; i<=TEAM_SIZE; i++){ if ( (TestFunction)(team,i) ){ players[result]=i; vals[result] =fabs(KeyNum - (KeyFunction)(team,i)); /* use diff from key*/ result++; } } if ( side == 'b' ){ /* Need to put in Their team too */ team = TheirSide; for (int i=1; i<=TEAM_SIZE; i++){ if ( (TestFunction)(team,i) ){ players[result]=i+TEAM_SIZE; /* to distinguish from my team */ vals[result] =fabs(KeyNum - (KeyFunction)(team,i)); /* use diff from key*/ result++; } } } /* Now should have all values in question in vals, with uniform number in corresponding position of players ( +TEAM_SIZE for their team if side == 'b'): Just sort em */ BubbleSort(result,players,vals); return result;}int Memory::NumTeammatesWithin(float Dist, float Ang, float ofDist, float ofAng){ int result = 0; for (int i=1; i<=TEAM_SIZE; i++){ if ( PlayerValid(MySide,i) && fabs(GetPlayerDistance(MySide,i) - ofDist) <= Dist && fabs(GetPlayerAngle (MySide,i) - ofAng ) <= Ang ) result++; } return result;}int Memory::NumOpponentsWithin(float Dist, float Ang, float ofDist, float ofAng){ int result = 0; for (int i=1; i<=TEAM_SIZE; i++){ if ( PlayerValid(TheirSide,i) && fabs(GetPlayerDistance(TheirSide,i) - ofDist) <= Dist && fabs(GetPlayerAngle (TheirSide,i) - ofAng ) <= Ang ) result++; } return result;}int Memory::NumPlayersWithin (float Dist, float Ang, float ofDist, float ofAng){ return NumTeammatesWithin(Dist,Ang,ofDist,ofAng) + NumOpponentsWithin(Dist,Ang,ofDist,ofAng);}int Memory::GetClosestTeammate(){ int ClosestPlayer = 0; float dist, ClosestDist = 1000; for (int i=1; i<TEAM_SIZE+1; i++){ if ( PlayerValid(MySide,i) && (dist=GetPlayerDistance(MySide,i)) < ClosestDist ){ ClosestDist = dist; ClosestPlayer = i; } } return ClosestPlayer;}int Memory::GetClosestOpponent(){ int ClosestPlayer = 0; float dist, ClosestDist = 1000; for (int i=1; i<TEAM_SIZE+1; i++){ if ( PlayerValid(TheirSide,i) && (dist=GetPlayerDistance(TheirSide,i)) < ClosestDist ){ ClosestDist = dist; ClosestPlayer = i; } } return ClosestPlayer;} /* This is expensive. If I do it too much, I should store global positions in the position structure---but remember to erase them every tick */float Memory::ClosestTeammateTo(float toX, float toY, int *teammate_number){ float x,y,dist,BestDist = 10000; for (int i=1; i<=TEAM_SIZE; i++){ if ( i == MyNumber || PlayerValid(MySide,i) ){ /* Exclude myself here? no. */ GetPlayerGlobalXY(MySide,i,&x,&y); dist = GetDistance( &x, &y, &toX, &toY ); if (dist < BestDist){ *teammate_number = i; BestDist = dist; } } } return BestDist;}int Memory::InOwnPenaltyArea(){ if ( MarkerValid(THEIR_GOAL) && GetGlobalX() < -PA_X && fabs(GetGlobalY()) < PA_Y ) return TRUE; else return FALSE;}int Memory::FacingBackInOwnPA(){ if ( InOwnPenaltyArea() && fabs(rad_to_deg(GetGlobalAngle())) > 90 ){ /* printf("%d facing back near own goal\n",MyNumber); */ return TRUE; } else return FALSE;}int Memory::FacingBackNearOwnGoal(){ if ( MarkerValid(MY_GOAL) && GetMarkerDistance(MY_GOAL) < 25 && fabs(rad_to_deg(GetGlobalAngle())) > 90 ){ /* printf("%d facing back near own goal\n",MyNumber); */ return TRUE; } else return FALSE;}void Memory::ClearStatus(char side, int num){ SetStatus(side,num, BLANK_STAT); // Get rid of the open status}void Memory::ClearStatuses(){ for (int i=1; i<=TEAM_SIZE; i++) SetStatus(MySide,i, BLANK_STAT); // Get rid of all the open stats}void Memory::ClearStatuses(char *status){ for (int i=1; i<=TEAM_SIZE; i++){ if ( !strcmp(GetStatus(MySide,i),status) ) SetStatus(MySide,i, BLANK_STAT); /* Get rid of all the stats = status*/ }}void Memory::ClearOtherStatuses(){ for (int i=1; i<=TEAM_SIZE; i++) { if (i != MyNumber) SetStatus(MySide,i,BLANK_STAT); // Get rid of all the open stats }}void Memory::ClearData(){ for (int i=1; i<=TEAM_SIZE; i++) SetData(i, BLANK_DAT); // Get rid of all the open stats}int Memory::HeardNewSound(){ return ( strcmp(new_sound,BLANK_MSG) ? TRUE : FALSE );}Memory::NewSound(int time, int speaker, char *msg){ if (Mem->PlayMode == BEFORE_KICK_OFF) new_sound_time = Mem->StoppedClockTime; /* time's not actually advancing */ else new_sound_time = time; new_sound_speaker = speaker; strcpy(new_sound,msg);}Memory::ClearSound(){ strcpy(new_sound,BLANK_MSG);}void Memory::ClearActives(){ for (int i=0; i<=TEAM_SIZE; i++) SetInactive(i);}void Memory::SetMyselfActive(){ SetActive(0); SetActive(MyNumber);}void Memory::SetMyselfInactive(){ SetInactive(0); SetInactive(MyNumber);}void Memory::SetAtAttention(int player){ if (player != 0 && player != MyNumber) my_error("other players should be marked as inactive when at attention"); my_team_active[player] = AT_ATTENTION; }void Memory::SetMyselfAtAttention(){ SetAtAttention(0); SetAtAttention(MyNumber);}void Memory::SetAuxiliary(int player){ if (player != 0 && player != MyNumber) my_error("other players should be marked as active when auxiliary"); my_team_active[player] = AUXILIARY; }void Memory::SetMyselfAuxiliary(){ SetAuxiliary(0); SetAuxiliary(MyNumber);}void Memory::SetInSetPlay(int player){ if (player != 0 && player != MyNumber) my_error("other players should be marked as active when InSetPlay"); my_team_active[player] = INSETPLAY; }void Memory::SetMyselfInSetPlay(){ SetInSetPlay(0); SetInSetPlay(MyNumber);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -