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

📄 memory.c

📁 足球机器人仿真组CMU97的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
  int ClosestOpponent;  switch(MarkChangeMethod){  case MARK_OBEY: break;  case MARK_CLOSEST:     /* Might want to prevent switching too hastily--first look for mark? */    /* Don't yet Make sure the player's in home range */    if (GetMark() != UNKNOWN_PLAYER)      return;    ClosestOpponent = GetClosestOpponent();    if ( ClosestOpponent && IsWithinMyMaxRange(TheirSide,ClosestOpponent) )      SetMark(ClosestOpponent);     else      SetMark(UNKNOWN_PLAYER);    break;  case MARK_OPEN: my_error("Haven't defined MARK_OPEN yet"); break;  }}int   Memory::GetMyReceiverList(int *ReceiverList){    /* Go by my actual location, not my position */  int MyActualLocation = GetPositionOfMyLocation();  int *Rlist = GetCurrentFormation()->GetCandidateReceivers(MyActualLocation);  for (int i=0; i<TEAM_SIZE; i++)    ReceiverList[i] = Rlist[i]; /* Copy the list into here */  int myPosition = GetMyPosition();  int NumOptions = 0;  while (ReceiverList[NumOptions] != MyActualLocation && NumOptions < TEAM_SIZE)    NumOptions++;    return NumOptions;}#define SETPLAY_TIME_LIMIT 100void Memory::InitializeSetPlay(){#if PRACTICE  return;#endif  float ballx = GetBallGlobalX();  float bally = GetBallGlobalY();  int NextPlayMode;  if ( PlayMode == BEFORE_KICK_OFF )    NextPlayMode = KickOffMode;  else    NextPlayMode = PlayMode;  /* To assign the x and y positions -- depends on where ball is*/  if ( !CurrentSetPlayFormation->Initialize(NextPlayMode,ballx,bally) )    return;  /* Go through the positions in order, finding the closest player     to that position, and mark the player as going there       */  /* When it's me set my mode to Insetplay                      */  float minDist, dist, x, y;  int closestPosition, i, closestPlayer;  int PositionAssigned[TEAM_SIZE];  for (i=0; i<TEAM_SIZE; i++) PositionAssigned[i] = FALSE;  for (i=0; i<CurrentSetPlayFormation->GetNumPositions(); i++){    minDist = 1000;    CurrentSetPlayFormation->GetSetPlayPositionXY(i,&x,&y);    for (int position=0; position<TEAM_SIZE; position++){      dist = DistanceToPositionHome(position,x,y);      if (dist < minDist && !PositionAssigned[position] && 	  PositionType(position) != GOALTENDER){ /* goalie not in set play */	minDist = dist;	closestPosition = position;      }    }    closestPlayer = GetPositionPlayer(closestPosition);    if ( closestPlayer == UNKNOWN_PLAYER && GetMyPosition() == UNKNOWN_POSITION ){      SetMyPosition(GetNewPosition());      ANNOUNCEMYPOSITION;      closestPlayer = GetPositionPlayer(closestPosition);    }       CurrentSetPlayFormation->AssignSetPlayPosition(i,closestPosition);    PositionAssigned[closestPosition] = TRUE;    if ( closestPlayer == MyNumber ){      SetMyselfInSetPlay();    }    /* printf("%d  closest postion: %d   player: %d  ball (%.1f, %.1f)  pos (%.1f %.1f)\n",	     MyNumber,closestPosition,closestPlayer,ballx,bally,x,y); */  }  ClearStatuses();  SetPlay = TRUE;  SetPlayStartTime = 0;                /* Not started yet */  SetPlayInvokeTime = CurrentTime;  SetPlayTimeLimit = SETPLAY_TIME_LIMIT;  SetPlayStartActionChosen = 0;  SetPlayStartReceiver = 0;}int Memory::SetPlayPositionFilled(int pos){  /* Pos is the SetPlay position */  int formationPos = CurrentSetPlayFormation->SetPlayPositionPlayer(pos);  if ( formationPos == UNKNOWN_POSITION ){    my_error("Why aren't all set play positions filled?");  }  int player = GetPositionPlayer(formationPos);  if ( player == UNKNOWN_PLAYER ) {    /* printf("don't know who's playing position  %d   at %d\n",	   formationPos, CurrentTime);*/    return FALSE;  }  /* Assuming I know if I'm in place */  if ( player == MyNumber || !strcmp(GetStatus(MySide,player),OPEN_STAT) )    return TRUE;  else     return FALSE;}int Memory::AllInSetPlayPosition(){  float positionx,positiony,gx,gy,dist;  for (int pos=0; pos<CurrentSetPlayFormation->GetNumPositions(); pos++){    CurrentSetPlayFormation->GetSetPlayPositionXY(pos,&positionx,&positiony);    GetMySetPlayXY(&gx,&gy);    dist = GetDistance(&positionx,&positiony,&gx,&gy);    /* Don't worry about far players being in position */    if ( !SetPlayPositionFilled(pos) && dist < MAX_COMMUNICATE_DIST - 5){      /* printf(" %d not filled\n",pos); */      return FALSE;    }  }  return TRUE;}int Memory::GetSetPlayStarterPosition(){  return CurrentSetPlayFormation->GetStarter();  /* could be UNKNOWN_POSITION */}int Memory::PositionInSetPlay(int pos){  for (int i=0; i<CurrentSetPlayFormation->GetNumPositions(); i++){    if ( CurrentSetPlayFormation->SetPlayPositionPlayer(i) == pos )      return TRUE;  }  return FALSE;}int Memory::MyPositionInSetPlay(){  return PositionInSetPlay(GetMyPosition());}int Memory::BallInSetPlayPosition(){  float ballx,bally;  Mem->GetBallGlobalXY(&ballx,&bally);  float ball_leeway = 1;  float balldist  = Mem->GetBallDistance();  if ( balldist > 50 ) ball_leeway = 3; /* Might not see it well enough */  else ball_leeway += 2*(balldist/50);  switch(PlayMode){  case THEIR_KICK_IN:  case MY_KICK_IN:    if ( fabs(fabs(bally) - Y0) > ball_leeway ){ return FALSE; }    break;  case MY_CORNER_KICK:    if ( fabs(ballx - X0) > ball_leeway )      { return FALSE; }    if ( fabs(fabs(bally) - Y0) > ball_leeway ){ return FALSE; }    break;  case THEIR_CORNER_KICK:    if ( fabs(ballx + X0) > ball_leeway )      { return FALSE; }    if ( fabs(fabs(bally) - Y0) > ball_leeway ){ return FALSE; }    break;  case BEFORE_KICK_OFF:  case THEIR_KICK_OFF:  case MY_KICK_OFF:    /* Ball is definitely at 0,0 */    /* if ( fabs(ballx) > ball_leeway ){ return FALSE; }       if ( fabs(bally) > ball_leeway ){ return FALSE; }    */    break;  case MY_GOAL_KICK:    if ( fabs(ballx + GA_X) > ball_leeway ){ return FALSE;}    if ( fabs(fabs(bally) - GA_Y) > ball_leeway ){ return FALSE;}    break;  case THEIR_GOAL_KICK:    if ( fabs(ballx - GA_X) > ball_leeway ){ return FALSE;}    if ( fabs(fabs(bally) - GA_Y) > ball_leeway ){ return FALSE;}    break;  case THEIR_FREE_KICK:  case MY_FREE_KICK:    break;  default: return FALSE;  /* No set play for that mode */  }  return TRUE;}void Memory::ModifySetPlayStartAction(){  if ( GetMySetPlayPositionType() != SETPLAY_STARTER )    my_error("Only the starter should have to modify setplay action");  /* Send the ball to the earliest filled position */  /* Otherwise just shoot                          */  /* Start with pos 1 so not to consider self      */  int NumSetPlayPositions = CurrentSetPlayFormation->GetNumPositions();  int choicePosition = 0;  if ( SetPlayStartActionChosen > 1 ){ /* don't switch more than twice */    choicePosition = SetPlayStartReceiver;  }  else if ( NumSetPlayPositions > 2 ){  /* There's a choice */    int filledPositions[NumSetPlayPositions];    for (int pos=1; pos<NumSetPlayPositions; pos++){      if ( SetPlayPositionFilled(pos) )	filledPositions[pos] = TRUE;      else 	filledPositions[pos] = FALSE;    }        int formationPos;    int teammates[2];        formationPos = CurrentSetPlayFormation->SetPlayPositionPlayer(1);    teammates[0] = GetPositionPlayer(formationPos);    formationPos = CurrentSetPlayFormation->SetPlayPositionPlayer(2);    teammates[1] = GetPositionPlayer(formationPos);    float confidence[2];        if ( filledPositions[1] && filledPositions[2] ){      choicePosition = FindTreeConfidences(2, teammates, confidence) + 1;          SetPlayStartActionChosen++;      SetPlayStartReceiver = choicePosition;      /* choose better of two */    }    else {      for (int pos=1; pos<NumSetPlayPositions; pos++)	if ( filledPositions[pos] ){  /* Take first filled position */	  choicePosition = pos;	  break;	}    }  }  else { /* There's no choice.  Just check if the player's there */    if ( SetPlayPositionFilled(1) )      choicePosition = 1;  }  if ( choicePosition ){    CurrentSetPlayFormation->SpecifySetPlayAction(0,choicePosition,10);    return;  }  /* Nobody in position */  if ( PlayMode == MY_GOAL_KICK )    CurrentSetPlayFormation->SpecifySetPlayAction(0,0,Y0*BallLocationSide(),0);  else    CurrentSetPlayFormation->SpecifySetPlayAction(0,X0,0,0);}float Memory::GetPositionSetPlayX(int pos){  return CurrentSetPlayFormation->GetSetPlayPlayerX(pos);}float Memory::GetPositionSetPlayY(int pos){  return CurrentSetPlayFormation->GetSetPlayPlayerY(pos);}void  Memory::GetPositionSetPlayXY(int pos, float *x, float *y){  *x = GetPositionSetPlayX(pos); *y = GetPositionSetPlayY(pos);}float Memory::GetMySetPlayX(){  return GetPositionSetPlayX(GetMyPosition());}float Memory::GetMySetPlayY(){  return GetPositionSetPlayY(GetMyPosition());}void  Memory::GetMySetPlayXY(float *x, float *y){  *x = GetMySetPlayX(); *y = GetMySetPlayY();}float  Memory::GetMySetPlayPositionBuffer(){  return CurrentSetPlayFormation->GetPositionBuffer(GetMyPosition());}int Memory::InMySetPlayPosition(){  float x,y,gx,gy;  GetMySetPlayXY(&x,&y);  GetGlobalXY(&gx,&gy);  float dist = GetDistance(&x,&y,&gx,&gy);  if (dist < GetMySetPlayPositionBuffer())    return TRUE;  else    return FALSE;}float Memory::GetMySetPlayAimX(){  return CurrentSetPlayFormation->GetSetPlayPlayerAimX(GetMyPosition());}float Memory::GetMySetPlayAimY(){  return CurrentSetPlayFormation->GetSetPlayPlayerAimY(GetMyPosition());}void  Memory::GetMySetPlayAimXY(float *x, float *y){  *x = GetMySetPlayAimX(); *y = GetMySetPlayAimY();}int   Memory::GetMySetPlayAimPosition(){  return CurrentSetPlayFormation->GetAimPosition(GetMyPosition());}int   Memory::GetMySetPlayWaitTime(){  return CurrentSetPlayFormation->GetWaitTime(GetMyPosition());}int   Memory::GetMySetPlayPositionType(){  int position = GetMyPosition();  if ( position == UNKNOWN_POSITION )     my_error("Setplay position type with no position?");  return CurrentSetPlayFormation->GetPositionType(position);}void  Memory::SetTheirSound(int num, char *msg, int time){  strcpy(their_sounds[num],msg);  their_sounds_times[num] = time;}  char *Memory::GetTheirSound(int num){  return their_sounds[num];}int   Memory::GetTheirSoundTime(int num){  return their_sounds_times[num];}void  Memory::ClearTheirSound(int num){  SetTheirSound(num,"blank",-1);}void  Memory::ClearTheirSounds(){  for (int num=0; num< NUM_THEIR_SOUNDS_SAVED; num++)    ClearTheirSound(num);}int   Memory::GetTheirOldestSoundNumber(){  int oldest=-1, oldesttime = 1000000;  for (int num=0; num< NUM_THEIR_SOUNDS_SAVED; num++)    if ( GetTheirSoundTime(num) < oldesttime && GetTheirSoundTime(num) >=0 ){      oldest = num;      oldesttime = GetTheirSoundTime(num);    }  return oldest;}int   Memory::GetTheirBlankSoundNumber(){  for (int num=0; num< NUM_THEIR_SOUNDS_SAVED; num++)    if ( GetTheirSoundTime(num) < 0 )      return num;  return -1;}char *Memory::GetTheirOldestSound(){  int oldest = GetTheirOldestSoundNumber();  if (oldest < 0)    return NULL;  else    return GetTheirSound(oldest);  /* could be NULL */}void  Memory::ReplaceTheirOldestSound(char *msg){  int oldest = GetTheirBlankSoundNumber();  if ( oldest < 0)    oldest = GetTheirOldestSoundNumber();  SetTheirSound(oldest,msg,CurrentTime);}

⌨️ 快捷键说明

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