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

📄 communications.c

📁 足球机器人仿真组CMU97的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
    }  }  else if ( !strncmp(msg,PLAYER_POS_MSG,PLAYER_POS_MSG_LEN) ){    /* Only matters when you're near the ball */    /* else scanf too expensive */    if (Mem->GetBallDistance()>KICK_DISTANCE+5) return;/*    static int last_time = -50;    if (Mem->CurrentTime < last_time + 50) return;    last_time = Mem->CurrentTime;*/            char side;    int unum;    float x,y;    int index=PLAYER_POS_MSG_LEN;    for (int i=0; i<TEAM_SIZE*2; i++){      sscanf(&msg[index]," %c %d %f %f",&side,&unum,&x,&y);      index+=PLAYER_POS_ELM_LEN;      if (Mem->MySide == 'l') y*=-1; else x*=-1; /* coach uses different coords */      if (side != Mem->MySide || unum != Mem->MyNumber)	Mem->HearPlayerGlobalPosition(side,unum,x,y,time);    }    //printf("%d\n",Mem->CurrentTime);  }}/*****************************************************************************/void HearTeammate(int from, float ang, char *msg, int time){  char side = Mem->MySide;  /* Assume you're not hearing your own message (filtered in parse.c) */  char tmp[MAX(MAX(TO_DELIMITER_LEN,TO_UNIT_DELIMITER_LEN),TO_POS_DELIMITER_LEN)];  int  to, toMe=FALSE, toMyUnit=FALSE;  Mem->HearPlayer(side, from, ang, msg, time); /* Could go at end, but then     */                                               /* would miss if to someone else */  if ( !strncmp(msg,TO_UNIT_DELIMITER,TO_UNIT_DELIMITER_LEN) ){    sscanf(msg,"%s %d %[^)]",tmp,&to,msg); /* If the message is to a specific					      unit, trim the message and					      ignore unless it's for me.  */    if ( !Mem->IsMyUnit(to) )       return;    toMyUnit = TRUE;  }  if ( !strncmp(msg,TO_POS_DELIMITER,TO_POS_DELIMITER_LEN) ){    sscanf(msg,"%s %d %[^)]",tmp,&to,msg); /* If the message is to a specific					      position, trim the message and					      ignore unless it's for me.  */    if ( to != Mem->GetMyPosition() )       return;    toMe = TRUE;  }  if ( !strncmp(msg,TO_DELIMITER,TO_DELIMITER_LEN) ){    sscanf(msg,"%s %d %[^)]",tmp,&to,msg); /* If the message is to a specific					      teammate, trim the message and					      ignore unless it's for me.  */    if (to != Mem->MyNumber )       return;    toMe = TRUE;  }  int needsResponse    = FALSE;  int staggerResponses = FALSE;  int delayResponse    = FALSE;  /* For speed, should order these from most to least likely */  if ( !strcmp(msg, HELLO_MSG) ){    Mem->SetInactive(from);  }  if ( !strcmp(msg, PING_MSG) ){    if ( Mem->GetMyActiveStatus() != ACTIVE ){      needsResponse = TRUE;      if ( !toMe )	staggerResponses = TRUE;    }  }  if ( !strcmp(msg, ALREADY_THERE_MSG) ){    if ( Mem->GetPlayerPosition(from) == Mem->GetMyPosition() ){      Mem->SetMyPosition(GetNewPosition());        if ( Mem->GetMyActiveStatus() != ACTIVE ){	needsResponse = TRUE;	delayResponse = TRUE;      }    }  }  if ( !strcmp(msg, MY_BALL_MSG) ){    Mem->SetActive(from);    if ( Mem->GetMyActiveStatus() != ACTIVE ){      needsResponse = TRUE;      staggerResponses = TRUE;    }  }  else if ( !strncmp (msg, INFORM_POSITION_MSG, INFORM_POSITION_MSG_LEN) ){    /* To say you're leaving a position, just make position UNKNOWN_POSITION */    /*    int position;    float x,y;    sscanf(msg,INFORM_POSITION_MSG,&position,&x,&y);    Mem->SetPlayerPosition(from, position);    Mem->HearTeammateGlobalPosition(from, x, y, time);    */    my_error("Shouldn't be using INFORM_POSITION_MSG anymore");  }  else if ( !strncmp (msg, LEAVING_POSITION_MSG, LEAVING_POSITION_MSG_LEN) ){    if ( Mem->GetMyActiveStatus() == INACTIVE ){      int position;      int my_position = Mem->GetMyPosition();      sscanf(msg,LEAVING_POSITION_MSG,&position);            /*if (!position)	printf("%d:%d Heard goalie leaving position\n",Mem->MyNumber,Mem->CurrentTime);*/      if ( Mem->GetPlayerPosition(from) != UNKNOWN_POSITION )	my_error("should have already cleared this position");      if ( Mem->GetPositionPlayer(position) == from ){	my_error("should have already cleared this player");      }            if ( position != my_position )	position = ConsiderPosition(position);      if ( position != UNKNOWN_POSITION && position != my_position ){ 	/* If I'm going to take the freed position */	Mem->SetMyPosition(position);      	needsResponse = TRUE;	staggerResponses = TRUE;      }    }  }  else if ( !strncmp (msg, BALL_COORDINATES_MSG, BALL_COORDINATES_MSG_LEN) ){    float x,y;    sscanf(msg,BALL_COORDINATES_MSG,&x,&y);    Mem->HearBallGlobalPosition(x,y,time);  }  else if ( !strncmp (msg, PLAYER_COORDS_MSG, PLAYER_COORDS_MSG_LEN) ){    char side; int player;    float x,y;    sscanf(msg,PLAYER_COORDS_MSG,&side,&player,&x,&y);    Mem->HearPlayerGlobalPosition(side,player,x,y,time);  }  else if ( !strcmp (msg, READY_TO_RECEIVE_MSG) ){    Mem->SetStatus(Mem->MySide,from, OPEN_STAT);    if ( Mem->MyPositionInSetPlay() &&	 Mem->GetMySetPlayPositionType() == SETPLAY_STARTER )      needsResponse = TRUE;  }  else if ( !strcmp (msg, OK_READY_TO_RECEIVE_MSG) ){    Mem->SetMyStatus(OPEN_STAT);  }  else if ( !strcmp (msg, READY_FOR_SETPLAY_MSG) ){    needsResponse = TRUE;    staggerResponses = TRUE;  }  else if ( !strcmp (msg, SETPLAY_STARTER_MSG) ){    if ( Mem->GetSetPlayStarterPosition() != Mem->GetPlayerPosition(from) ){      /* printf("%d:%d gonna have to reinitialize (wrong starter1)\n",	     Mem->MyNumber,Mem->CurrentTime);*/      if ( Mem->SetPlay && Mem->PlayMode != MY_KICK_IN )	printf("     not  kick-in, why confusion (1) ???? starter:%d, from:%d\n",	       Mem->GetSetPlayStarterPosition(),Mem->GetPlayerPosition(from));      Mem->SetPlay = FALSE; /* force reinitialization */    }    else      Mem->SetStatus(Mem->MySide,from,SETPLAY_STARTER_STAT);  }  else if ( !strcmp (msg, NOT_SETPLAY_STARTER_MSG) ){    if ( Mem->GetSetPlayStarterPosition() == Mem->GetPlayerPosition(from) ){      /* printf("%d:%d gonna have to reinitialize (wrong starter1)\n",	     Mem->MyNumber,Mem->CurrentTime);*/      if ( Mem->SetPlay && Mem->PlayMode != MY_KICK_IN )	printf("     not  kick-in, why confusion (2) ???? starter:%d, from:%d\n",	       Mem->GetSetPlayStarterPosition(),Mem->GetPlayerPosition(from));      Mem->SetPlay = FALSE; /* force reinitialization */    }  }  else if ( !strcmp (msg, ARE_YOU_STARTER_MSG) ){    needsResponse = TRUE;    delayResponse = TRUE;  }  else if ( !strcmp (msg, READY_TO_PASS_MSG) ){    Mem->ClearStatuses(RECEIVER_STAT);    Mem->SetStatus(Mem->MySide,from, WITH_BALL_STAT);    Mem->ClearStatus(Mem->MySide,0);  }  /*else if ( !strcmp (msg, BYE_MSG) ){    ;  }*/  else if ( !strncmp (msg, PASSING_DECISION, PASSING_DECISION_LEN) ){    int receiver;    sscanf(msg,PASSING_DECISION,&receiver);    /* Could be marking self */    Mem->ClearStatus(Mem->MySide,Mem->FindTeammateWithStatus(RECEIVER_STAT));    Mem->SetStatus(Mem->MySide,receiver, RECEIVER_STAT);    if (receiver == Mem->MyNumber && Mem->GetMyActiveStatus() == INACTIVE){      Mem->SetMyselfAtAttention();    }    Mem->ClearStatus(Mem->MySide,Mem->FindTeammateWithStatus(WITH_BALL_STAT));    Mem->SetStatus(Mem->MySide,from, WITH_BALL_STAT);  }  else if ( !strncmp (msg, DATA_MSG, DATA_MSG_LEN) ){    StrReplace(&msg[DATA_MSG_LEN],COMMA_STANDIN,',');    Mem->SetData(from, &msg[DATA_MSG_LEN]); /* Fast forward past data_msg */  }  else if ( !strncmp (msg, MARK_ASSIGN_MSG, MARK_ASSIGN_MSG_LEN) ){    int mark, player;    sscanf(msg,MARK_ASSIGN_MSG,&mark,&player);    /* If not for me, keep track of who's marking whom? */    if ( player == Mem->MyNumber && Mem->IsUnitCaptain(to,from) ){      Mem->SetMark(mark);  /* Announce player that's being left? */      Mem->MarkChangeMethod = MARK_OBEY;      switch( Mem->HomeChangeMethod ){      case HOME_MARK:      case HOME_MARK_ELSE_SHIFT: break;      case HOME_SHIFT: Mem->HomeChangeMethod = HOME_MARK_ELSE_SHIFT; break;      case HOME_OBEY:  Mem->HomeChangeMethod = HOME_MARK; break;      default: my_error("What's the HomeChangeMethod?");      }    }  }  else if ( !strncmp (msg, POSITION_ASSIGN_MSG, POSITION_ASSIGN_MSG_LEN) ){    int position, player;    sscanf(msg,POSITION_ASSIGN_MSG,&position,&player);    /* If not for me, keep track of who's assigned to a position? */    if ( player == Mem->MyNumber && Mem->IsUnitCaptain(to,from) ){      Mem->SetMyPosition(position);  /* Announce player that's being left? */      if ( Mem->GetMyActiveStatus() != ACTIVE ){	needsResponse = TRUE;	delayResponse = TRUE;      }    }  }  else if ( !strcmp(msg,JOLT_MSG) ){    needsResponse = TRUE;  }  else if ( !strcmp(msg,GIVE_DATA_MSG) ){    if ( Mem->GetMyActiveStatus() != ACTIVE )      needsResponse = TRUE;  }  else if ( !strcmp(msg,PING_BALL_MSG) ){     if ( Mem->GetMyActiveStatus() != ACTIVE )      needsResponse = TRUE;    if ( Mem->PlayMode != PLAY_ON )      /* Important that closest to ball knows where it is */      staggerResponses = TRUE;  }  else if ( !strncmp(msg,PING_PLAYER_MSG,PING_PLAYER_MSG_LEN) ){      if ( Mem->GetMyActiveStatus() != ACTIVE )      needsResponse = TRUE;  }  if ( Mem->GetPlayerPosition(from) == Mem->GetMyPosition() &&        Mem->GetMyPosition() != UNKNOWN_POSITION ){    int position = Mem->GetMyPosition();    /*printf("%d:%d %d is in my position (%d) ...",	   Mem->MyNumber,Mem->CurrentTime,from,position);*/    float x,y;    Mem->GetTeammateHeardGlobalXY(from,&x,&y);    if ( Mem->PlayerDistanceToPositionHome(0,position) <	 Mem->DistanceToPositionHome(position,x,y) ){      SAYTO(from,ALREADY_THERE_MSG);      if ( !delayResponse && !staggerResponses )	delayResponse = TRUE; /* Wait for this message to get through */      /*printf(" already there\n");*/    }    else{      Mem->SetMyPosition(GetNewPosition());      /*printf(" vacating\n");*/    }  }  if ( Mem->HeardNewSound() )    needsResponse = FALSE;  /* Don't override a bending response */  if ( needsResponse ){    Mem->NewSound(time, from, msg);/* Note that this is a new sound that's been heard:				      for communications purposes */    Mem->CommunicateDelay = MIN_COMMUNICATE_DELAY; /* The default */    if (delayResponse)      Mem->CommunicateDelay = COMMUNICATE_INTERVAL;    else if (staggerResponses){      if ( Mem->MyNumber > from )	Mem->CommunicateDelay += (((Mem->MyNumber-from)-1)*2   )*COMMUNICATE_INTERVAL;      else /* Mem->MyNumber < from */	Mem->CommunicateDelay += (((from-Mem->MyNumber)-1)*2 +1)*COMMUNICATE_INTERVAL;    }  }}/*****************************************************************************/void HearTeammate(int from, int key, float ang, int formation, int formtime, int pos, 		  float x, float y, char *msg, int time){#if PRACTICE#else  if ( !KeyValid(key,from,formation,formtime,pos,x,y,time) ){    printf("%d:%d  This message from %d wasn't valid %s\n",Mem->MyNumber,time,from,msg);    HearOpponent(ang,msg,time,FALSE);     return;  }#endif  Mem->HearTeammateGlobalPosition(from, x, y, time);    Mem->HearTeammateFormationPosition(from, formation, formtime, pos);  HearTeammate(from,ang,msg,time);}/*****************************************************************************/void HearOpponent(float ang, char *msg, int time, int save){  char side = Mem->TheirSide;  /* store the angle of this player at least in some memory slot */  /* check if the angle matches one where an opponent should be  */  /* Save the msg into memory */  if ( save && !int_random(10) )    Mem->ReplaceTheirOldestSound(msg);}/*****************************************************************************/int Communicate(int behavior){    char *msg = Mem->GetNewSound();  char team[50], tmp[TO_DELIMITER_LEN];  int  from,to;  from = Mem->GetNewSoundSpeaker();  /* Following checks should already have been done, but just in case ...*/  if ( from == Mem->MyNumber )         /* Ignore self                 */    return 0;  /* For speed, should order these from most to least likely */  if ( !strcmp(msg,PING_MSG) ){  /* Give your angle position if a teammate needs it */    SAY(HELLO_MSG);  }  else if ( !strcmp(msg,PING_BALL_MSG) ){  /* Give the ball position to a teammate */    if ( Mem->PlayMode != PLAY_ON ){      /* Only if sure and you see ball correctly                */      if ( Mem->BallValid() == 1 && Mem->BallInSetPlayPosition() )	ANNOUNCEBALLCOORDINATES;     }    else if ( Mem->BallValid() == 1 && Mem->GetBallDistance() <= 30 ) /* play_on mode */      /* Only if sure                                        */      /* Only if you're one of the 2 closest to the ball ??? */      ANNOUNCEBALLCOORDINATES;   }  else if ( !strncmp(msg,PING_PLAYER_MSG,PING_PLAYER_MSG_LEN) ){      /* Give the player position to a teammate */    char side; int player;    sscanf(msg,PING_PLAYER_MSG,&side,&player);    if ( Mem->PlayerValid(side,player) == 1 )      /* Only if sure                                        */      ANNOUNCEPLAYERCOORDINATES(side,player);   }  else if ( !strcmp(msg,MY_BALL_MSG) ){    if ( Mem->GetMyActiveStatus() != ACTIVE )      if ( Mem->AmCandidateReceiverOfPlayer(from) )	SAYPASSDATATO(from);  }  else if ( !strncmp (msg, LEAVING_POSITION_MSG, LEAVING_POSITION_MSG_LEN) ){    ANNOUNCEMYPOSITION;  }  else if ( !strncmp (msg, POSITION_ASSIGN_MSG, POSITION_ASSIGN_MSG_LEN) ){    ANNOUNCEMYPOSITION;  }  if ( !strcmp(msg, ALREADY_THERE_MSG) ){    ANNOUNCEMYPOSITION;  }  else if ( !strcmp (msg, READY_FOR_SETPLAY_MSG) ){    if ( Mem->GetMyActiveStatus() == INSETPLAY && Mem->InMySetPlayPosition() )      SAY(READY_TO_RECEIVE_MSG);  }  else if ( !strcmp (msg, READY_TO_RECEIVE_MSG) ){    SAYTO(from,OK_READY_TO_RECEIVE_MSG);  }  else if ( !strcmp (msg, ARE_YOU_STARTER_MSG) ){    if ( Mem->SetPlay && Mem->MyPositionInSetPlay() ){      if ( Mem->GetMySetPlayPositionType() == SETPLAY_STARTER ){	SAY(SETPLAY_STARTER_MSG);      }      else{	SAY(NOT_SETPLAY_STARTER_MSG);      }    }  }  else if ( !strcmp(msg,GIVE_DATA_MSG) ){  /* Give your data if a teammate needs it */    SAYPASSDATATO(from);  }  else if ( !strcmp(msg,JOLT_MSG) ){       /* For the decision tree tests: client stuck*/    TURN(5);       }  return 0;  /* Depending on behavior, check for different things?*/}

⌨️ 快捷键说明

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