📄 communications.c
字号:
/* communications.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 SOUND_OFF 0#define TIME_ADDITION 37 /* Must be positive */int GetKey(int from, int formation, int formtime, int pos, float x, float y){ /* Can put in a time component */ int key = from * (Mem->CurrentTime + TIME_ADDITION) ; /*printf("%d's key: %d\n",from,key);*/ return key;}/*****************************************************************************/int KeyValid(int key,int from, int formation, int formtime, int pos, float x, float y, int time){ int sentTime = key/from - TIME_ADDITION; /*printf("%d got %d's key: %d at %d (now %d) \n",Mem->MyNumber,from,key,sentTime,time);*/ if ( abs(sentTime - time) < 60 ) /* Was 10 for communication paper */ return TRUE; else return FALSE;}/*****************************************************************************/int Say (char *msg){ char buf[MAXMESG];#if SOUND_OFF return 0;#endif#if PRACTICE return 0;#endif /* Should have team, number, formation, formation time, position, x, y, msg */ /* Change here must also happen in sayto, saytounit, saytoposition, communicate,*/ /* and in parse.c (including call to HearTeammate below) */ int from = Mem->MyNumber; int formation,formtime,position; if ( Mem->GetCurrentFormation() == UNKNOWN_FORMATION ){ formation = UNKNOWN_FORMATION; formtime = position = 0; } else{ formation = Mem->GetCurrentFormationType(); formtime = Mem->FormationTime; position = Mem->GetMyPosition(); } float x = Mem->GetGlobalX(); float y = Mem->GetGlobalY(); int key = GetKey(from,formation,formtime,position,x,y); sprintf(buf,"(say %s %d %d %d %d %d %.1f %.1f %s)\n", Mem->MyTeamName, from, key, formation, formtime, position, x, y, msg); if(send_message(buf, GLOBAL_sock) == -1){ return -1; } return 0;}/*#define SAY(a) if(Say(a)==-1) return -1*//*****************************************************************************/int SayTo (int num, char *msg){ char buf[MAXMESG];#if SOUND_OFF return 0;#endif#if PRACTICE return 0;#endif if ( !num || num>TEAM_SIZE ) my_error("Can't SayTo a player that doesn't exist"); if ( num == Mem->MyNumber ) my_error("Why talking to myself?"); int from = Mem->MyNumber; int formation,formtime,position; if ( Mem->GetCurrentFormation() == UNKNOWN_FORMATION ){ formation = UNKNOWN_FORMATION; formtime = position = 0; } else{ formation = Mem->GetCurrentFormationType(); formtime = Mem->FormationTime; position = Mem->GetMyPosition(); } float x = Mem->GetGlobalX(); float y = Mem->GetGlobalY(); int key = GetKey(from,formation,formtime,position,x,y); sprintf(buf,"(say %s %d %d %d %d %d %.1f %.1f %s %d %s)\n", Mem->MyTeamName, from, key, formation, formtime, position, x, y, TO_DELIMITER, num, msg); if(send_message(buf, GLOBAL_sock) == -1){ return -1; } return 0;}/*#define SAYTO(a,b) if(SayTo(a,b)==-1) return -1#define SAYTOCOACH(a) if(SayTo(0,b)==-1) return -1*//*****************************************************************************/int SayToPosition (int position, char *msg){ char buf[MAXMESG];#if SOUND_OFF return 0;#endif#if PRACTICE return 0;#endif if ( position > TEAM_SIZE ) my_error("How many positions are there?"); int from = Mem->MyNumber; int formation,formtime,myposition; if ( Mem->GetCurrentFormation() == UNKNOWN_FORMATION ){ formation = UNKNOWN_FORMATION; formtime = myposition = 0; } else{ formation = Mem->GetCurrentFormationType(); formtime = Mem->FormationTime; myposition = Mem->GetMyPosition(); } float x = Mem->GetGlobalX(); float y = Mem->GetGlobalY(); int key = GetKey(from,formation,formtime,myposition,x,y); sprintf(buf,"(say %s %d %d %d %d %d %.1f %.1f %s %d %s)\n", Mem->MyTeamName, from, key, formation, formtime, myposition, x, y, TO_POS_DELIMITER, position, msg); if(send_message(buf, GLOBAL_sock) == -1){ return -1; } return 0;}/*#define SAYTOPOSITION(a,b) if(SayToPosition(a,b)==-1) return -1*//*****************************************************************************/int SayToUnit (int unit, char *msg){ char buf[MAXMESG];#if SOUND_OFF return 0;#endif#if PRACTICE return 0;#endif if ( !Mem->IsMyUnit(unit) ) my_error("Can't SayToUnit unless I'm in the unit"); int from = Mem->MyNumber; int formation,formtime,position; if ( Mem->GetCurrentFormation() == UNKNOWN_FORMATION ){ formation = UNKNOWN_FORMATION; formtime = position = 0; } else{ formation = Mem->GetCurrentFormationType(); formtime = Mem->FormationTime; position = Mem->GetMyPosition(); } float x = Mem->GetGlobalX(); float y = Mem->GetGlobalY(); int key = GetKey(from,formation,formtime,position,x,y); sprintf(buf,"(say %s %d %d %d %d %d %.1f %.1f %s %d %s)\n", Mem->MyTeamName, from, key, formation, formtime, position, x, y, TO_UNIT_DELIMITER, unit, msg); if(send_message(buf, GLOBAL_sock) == -1){ return -1; } return 0;}/*#define SAYTOUNIT(a,b) if(SayToUnit(a,b)==-1) return -1*//*****************************************************************************/int EchoTheirSound(){ char buf[MAXMESG]; char *msg = Mem->GetTheirOldestSound(); if ( msg != NULL ){ sprintf(buf,"(say %s",msg); if(send_message(buf, GLOBAL_sock) == -1) return -1; /* printf("%d:%d echoing message %s\n",Mem->MyNumber,Mem->CurrentTime,msg); */ } return 0;}/*#define ECHOTHEIRSOUND EchoTheirSound()*//*****************************************************************************/int AnnounceMyPosition(){/* char Announcement[MAXMESG]; sprintf(Announcement,INFORM_POSITION_MSG,Mem->GetMyPosition()); SAY(Announcement); *//* if (Mem->GetMyActiveStatus() == ACTIVE) printf("%d:%d Well then I can't assume the player's inactive when saying 'here'\n", Mem->MyNumber,Mem->CurrentTime); */ SAY(HELLO_MSG); return 0;}/*#define ANNOUNCEMYPOSITION AnnounceMyPosition()*//*****************************************************************************/int AnnounceLeavingPosition(int position){ char Announcement[MAXMESG]; sprintf(Announcement,LEAVING_POSITION_MSG,position); SAY(Announcement); }/*#define ANNOUNCELEAVINGPOSITION(a) AnnounceLeavingPosition(a)*//*****************************************************************************/int AnnounceBallCoordinates(){ char Announcement[MAXMESG]; float x,y; Mem->polar_to_globalxy(Mem->GetBallDistance(),Mem->GetBallAngleRad(),&x,&y); sprintf(Announcement,SAY_BALL_COORDINATES_MSG,x,y); SAY(Announcement); return 0;}/*#define ANNOUNCEBALLCOORDINATES AnnounceBallCoordinates()*//*****************************************************************************/int AnnouncePlayerCoordinates(char side, int player){ char Announcement[MAXMESG]; float x,y; Mem->polar_to_globalxy(Mem->GetPlayerDistance(side,player), Mem->GetPlayerAngleRad(side,player),&x,&y); sprintf(Announcement,SAY_PLAYER_COORDS_MSG,side,player,x,y); SAY(Announcement); return 0;}/*#define ANNOUNCEPLAYERCOORDINATES(a,b) AnnouncePlayerCoordinates(a,b)#define ANNOUNCETEAMMATECOORDINATES(a) AnnouncePlayerCoordinates(Mem->MySide,a)#define ANNOUNCEOPPONENTCOORDINATES(a) AnnouncePlayerCoordinates(Mem->TheirSide,a)*//*****************************************************************************/int PingPlayer(char side, int player){ char Announcement[MAXMESG]; sprintf(Announcement,PING_PLAYER_MSG,side,player); SAY(Announcement); return 0;}#define PINGPLAYER(a,b) PingPlayer(a,b)#define PINGTEAMMATE(a) PingPlayer(Mem->MySide,a)#define PINGOPPONENT(a) PingPlayer(Mem->TheirSide,a)/*****************************************************************************/int AssignMark(int unit, int player, int mark){ if ( !Mem->IsUnitMember(unit,player) || !Mem->IsUnitCaptain(unit,Mem->MyNumber) ) my_error("Only captains can assign their unit-members marks"); char Announcement[MAXMESG]; sprintf(Announcement,MARK_ASSIGN_MSG,mark,player); SAYTOUNIT(unit, Announcement); return 0;}/*#define ASSIGNMARK(a,b,c) AssignMark(a,b,c)*//*****************************************************************************/int AssignPosition(int unit, int player, int position){ if ( !Mem->IsUnitMember(unit,player) || !Mem->IsUnitCaptain(unit,Mem->MyNumber) ) my_error("Only captains can assign their unit-members positions"); char Announcement[MAXMESG]; sprintf(Announcement,POSITION_ASSIGN_MSG,position,player); SAYTOUNIT(unit, Announcement); return 0;}/*#define ASSIGNPOSITION(a,b,c) AssignPosition(a,b,c)*//*****************************************************************************/int SayPassDataTo(int player){ char outStream[MAXMESG]; char dataStream[MAXMESG]; GetPassData(outStream, player, 'd');/* Get the data stream */ StrReplace(outStream,',',COMMA_STANDIN); sprintf(dataStream,DATA_MSG,outStream);/* Put on the DATA_MSG tag */ SAYTO(player,dataStream);}/*#define SAYPASSDATATO(a) SayPassDataTo(a)*//*****************************************************************************/void HearCoach(char *msg, int time){ if ( msg[0] <= 'z' && msg[0] >= 'a' ) my_error("Coach messages should be capitalized"); if ( !strncmp(msg,RESET_MSG,5) ){ /* A reset message */ Mem->Reset(); } else if ( !strncmp(msg,NEW_COACH_MSG,5) ){ Mem->NewCoach = TRUE; } else if ( !strncmp(msg,SAVE_MSG,4) ){ RecordCollectResult(COLLECT_SAVE);/* Mem->Reset();*/ Mem->RecordCollectData = TRUE; } else if ( !strncmp(msg,MISS_MSG,4) ){ RecordCollectResult(COLLECT_MISS);/* Mem->Reset();*/ Mem->RecordCollectData = TRUE; } else if ( !strncmp(msg,GOAL_MSG,4) ){ RecordCollectResult(COLLECT_GOAL);/* Mem->Reset();*/ Mem->RecordCollectData = TRUE; } else if ( !strncmp(msg,PASS_SUCCESS_MSG,4) ){ RecordPassResult(PASS_SUCCESS); } else if ( !strncmp(msg,PASS_FAILURE_MSG,4) ){ RecordPassResult(PASS_FAILURE); } else if ( !strncmp(msg,PASS_MISS_MSG,4) ){ RecordPassResult(PASS_MISS); } else if ( !strncmp(msg,TEST_KICK_MSG,TEST_KICK_MSG_LEN) ){ float param1, param2, param3; sscanf(msg,TEST_KICK_MSG,¶m1,¶m2,¶m3); TESTKICKPARAMETERS(param1,param2,param3); } else if ( !strncmp(msg,TEST_COLLECT_MSG,TEST_COLLECT_MSG_LEN) ){ float param1, param2, param3, param4, param5, param6, param7, param8, param9, param10; sscanf(msg,TEST_COLLECT_MSG,¶m1,¶m2,¶m3,¶m4, ¶m5,¶m6,¶m7,¶m8,¶m9,¶m10); Mem->RecordCollectData = FALSE; if ( Mem->OriginalBehavior == JUSTIN_COLLECT ){ while( !Mem->RecordCollectData ){ Mem->NewSight = FALSE; /* Undo the sight bit */ Mem->ClearAction(); TESTCOLLECTPARAMETERS(param1,param2,param3,param4, param5, param6, param7, param8, param9); if ( !Mem->NewSight ) WAITFORSIGHT; } } else if ( Mem->OriginalBehavior == SHOOT ){ while( !Mem->RecordCollectData ){ Mem->NewSight = FALSE; /* Undo the sight bit */ Mem->ClearAction(); SHOOTATANGLE(100,param10); if ( !Mem->NewSight ) WAITFORSIGHT; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -