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

📄 behave.c

📁 足球机器人仿真组CMU97的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* behave.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"static char *BehaviorName = "Dunno";/*****************************************************************************/int Defend(float dist, float gpow, float spow){  static int _INPOSITION_ = FALSE;  static int _DEFENDING_ = FALSE;  /*printf("%d/%d - ",_INPOSITION_,_DEFENDING_);*/  if ( !_INPOSITION_ && !_DEFENDING_) {         /* If out of position, go home */    if ( GOALIEHOME(gpow) >= TRIED )        /* Turned to face ball         */      _INPOSITION_ = TRUE;  }  else if ( !_INPOSITION_ && _DEFENDING_ ){    if ( (Mem->BallValid() && Mem->GetBallDistance() > dist) ||	 (Mem->MarkerValid(MY_GOAL) && Mem->GetMarkerDistance(MY_GOAL) > dist*2) )      _DEFENDING_ = FALSE;                    /* Stop and go back home       */    else       CHASEANDSHOOT( gpow,spow );  }  else { /* _INPOSITION_ */   _DEFENDING_ = TRUE;                       /* _DEFENDING_ Mode until ball				              comes close and then leaves */   if ( COLLECTANDSHOOT( &ChooseNN,dist,gpow,spow ) >= TRYING )     _INPOSITION_ = FALSE;                   /* Moved to ball               */ }  return _INPOSITION_;}/*****************************************************************************/int Kamram(float dist, float gpow, float spow){static int _INPOSITION_ = FALSE;static int _ATTACKING_ = FALSE;/*printf("%d/%d - ",_INPOSITION_,_ATTACKING_);*/  if ( !_INPOSITION_ && !_ATTACKING_) {         /* If out of position, go home */    if ( KAMRAMHOME(gpow) >= TRIED )        /* Turned to face ball         */      _INPOSITION_ = TRUE;  }  else if ( !_INPOSITION_ && _ATTACKING_ ){    if ( (Mem->BallValid()  && Mem->GetBallDistance() > dist) ||	 (Mem->MarkerValid(THEIR_GOAL) && Mem->GetMarkerDistance(THEIR_GOAL) > dist*2) )      _ATTACKING_ = FALSE;                    /* Stop and go back home       */    else       CHASEANDSHOOT( gpow,spow );  }  else { /* _INPOSITION_ */   _ATTACKING_ = TRUE;                       /* _ATTACKING_ Mode until ball				              comes close and then leaves */   if ( COLLECTANDSHOOT( &ChooseNN,dist,gpow,spow ) >= TRYING )     _INPOSITION_ = FALSE;                   /* Moved to ball               */ }  return _INPOSITION_;}/*****************************************************************************/int TreePass(int (*ReceiverChoiceFunction)(), 	     float cdist, float pdist, float gpow, float dpow, float spow){  #define _TREEPASS_LOOKING_   0  #define _TREEPASS_INFORMING_ _TREEPASS_LOOKING_   +4  #define _TREEPASS_CHOOSING_  _TREEPASS_INFORMING_ +1  #define _TREEPASS_PINGING_   _TREEPASS_CHOOSING_  +1  #define _TREEPASS_PASSING_   _TREEPASS_PINGING_   +1  #define _TREEPASS_RESET_     _TREEPASS_PASSING_   +1  static int Phase = _TREEPASS_LOOKING_;  static int receiver = 0;  static int HangCounter = 0;  static int HangCounter2 = 0;  int Result, ball_coming;  switch(Phase){  case _TREEPASS_LOOKING_: /* Waiting to collect the ball */#if 0    ball_coming = Mem->FindTeammateWithStatus(RECEIVER_STAT); /* The player to whom								     the ball is coming */    if ( ball_coming == 0 || ball_coming == Mem->MyNumber ){      Result = CHASEBALL(gpow);    }    else{      Result = 0;    }  /* Look for the ball */#else    Result = COLLECTBALL(&ChooseNN,cdist,gpow);#endif    if ( Result == SUCCESS ){    /* Or if Result == TRIED? */      Phase++;    }    else if ( HangCounter++ > 100 ){/* Coach needs to reset ball */      SAYTOCOACH(WAKEUP_MSG);      HangCounter = 0;    }    break;  case _TREEPASS_INFORMING_:    if ( Mem->BallValid() && Mem->GetBallDistance() > KICK_DISTANCE ){      Phase = _TREEPASS_LOOKING_;  /* Return to beginning phase if ball's gone */      break;    }    HangCounter = 0;    Mem->ClearOtherStatuses(); /* Get rid of all the open stats */    Mem->ClearData();    SAY(READY_TO_PASS_MSG);    receiver = 0;    Phase++;    break;  case _TREEPASS_CHOOSING_: /* Waiting for answer */    if ( Mem->BallValid() && Mem->GetBallDistance() > KICK_DISTANCE ){      Phase = _TREEPASS_LOOKING_;  /* Return to beginning phase if ball's gone */      break;    }    receiver = (*ReceiverChoiceFunction)();    if ( receiver ){      char Announcement[MAXMESG];      sprintf(Announcement,PASSING_DECISION,receiver);      SAY(Announcement);      Phase++;    }    else if (HangCounter++ > 10){      SAY(READY_TO_PASS_MSG);  /* One of the players may have lost a message */      HangCounter = 0;#if SAVE_PASS_DATA      HangCounter2++;          /* Or one player might be stuck at global angle 0 */#else      Phase = _TREEPASS_LOOKING_;#endif    }    else if (HangCounter2 > 10){      SAY(JOLT_MSG);      HangCounter2 = 0;    }    break;  case _TREEPASS_PINGING_:    if ( Mem->BallValid() && Mem->GetBallDistance() > KICK_DISTANCE ){      Phase = _TREEPASS_LOOKING_;  /* Return to beginning phase if ball's gone */      break;    }    if ( !receiver ){       Phase = _TREEPASS_CHOOSING_;      break;    }    if ( FACETEAMMATE(receiver) == SUCCESS ) /* Sure I'm facing the player */      Phase++;    if ( !strcmp(Mem->GetData(receiver),BLANK_DAT) )      SAYTO(receiver,GIVE_DATA_MSG);  /* Ping the individual */    break;  case _TREEPASS_PASSING_: /* Pass the ball */    if ( Mem->BallValid() && Mem->GetBallDistance() > 15 ){      Phase = _TREEPASS_LOOKING_;  /* Return to beginning phase if ball's gone */      break;    }    if ( receiver ){	Result = APPROACHANDPASSTO( receiver, 150, pdist, gpow, dpow, spow );	if ( Result == TRIED )	  Phase++;      }    else /* !receiver */      my_error("should have a receiver");    break;  case _TREEPASS_RESET_:        Phase = _TREEPASS_LOOKING_;      Mem->SetStatus(Mem->MySide,receiver,BLANK_STAT);  /* Clear that the player's open				             	      eventually should clear ALL opens */      /*Mem->Acting(FORG);*/      break;  default: Phase++;  }  return Phase;}/*****************************************************************************/int TreeTestReceiveAndAttack(int (*ChoiceFunction)(float*,float*), 		     float CollectDist, float DashPower, float ShotPower){  static int _RECEIVED_ = 0;  static int counter = 0;  int ball_coming;  switch(_RECEIVED_){  case 0:     if ( fabs(rad_to_deg(Mem->GetGlobalAngle())) < .0001 ){        /* Position just got set, wait for RESET msg */      if ( counter++ < 2 )	break;      else	counter = 0;    }    /* Hack to see the other players with high quality while looking for ball */    ball_coming = Mem->FindTeammateWithStatus(RECEIVER_STAT); /* The player to whom							       the ball is coming */    /* Just so the player can see with high quality     */    /* Requires a line commented out from Lookforplayer */    if( !ball_coming && !Mem->BallValid() )      LOOKFORTEAMMATE(1);    /* Hack to see the other players with high quality while looking for ball */    else if ( RECEIVEPASS(ChoiceFunction, CollectDist, DashPower) == SUCCESS )      _RECEIVED_ = 1;    break;  case 1:     int result = COLLECTANDKNOCK(LC_FLAG,&ChooseNN,CollectDist,DashPower,ShotPower);    if ( result == FAILURE )      _RECEIVED_ = 0;  /* Ball's out of range again */    else if ( result == TRIED )      SAYTOCOACH(KICKED_MSG); /* 0 is for coach */    break;  }}/*****************************************************************************/int TreeReceiveAndKnock(int (*ChoiceFunction)(float*,float*), 			float CollectDist, float DashPower, int Marker, 			float ShotPower){  #define _RECEIVEANDKNOCK_POSITIONING_ 0  #define _RECEIVEANDKNOCK_RECEIVING_   _RECEIVEANDKNOCK_POSITIONING_+1  #define _RECEIVEANDKNOCK_KNOCKING_    _RECEIVEANDKNOCK_RECEIVING_+1  static int Phase = _RECEIVEANDKNOCK_POSITIONING_;  int ball_coming;  switch(Phase){  case _RECEIVEANDKNOCK_POSITIONING_:    if ( HOLDPOSITION(DashPower) == SUCCESS )      Phase++;    break;  case _RECEIVEANDKNOCK_RECEIVING_:    ball_coming = Mem->FindTeammateWithStatus(RECEIVER_STAT); /* The player to whom							       the ball is coming */    if( !ball_coming && !Mem->BallValid() )      LOOKFORBALL;    else if ( RECEIVEPASS(ChoiceFunction, CollectDist, DashPower) == SUCCESS )      Phase++;  /* Received it */    break;  case _RECEIVEANDKNOCK_KNOCKING_:     int result = COLLECTANDKNOCK(Marker,&ChooseNN,CollectDist,DashPower,ShotPower);    if ( result == FAILURE )      Phase = _RECEIVEANDKNOCK_POSITIONING_; /* Ball's out of range again */    break;  }}/*****************************************************************************/int StartSetPlay(int (*ReceiverChoiceFunction)(), 		 float cdist, float pdist, 		 float gpow, float dpow, float spow){  #define _STARTSETPLAY_DRIBBLING_ 0  #define _STARTSETPLAY_PASSING_   _STARTSETPLAY_DRIBBLING_ +1  #define _STARTSETPLAY_RESET_     _STARTSETPLAY_PASSING_   +1  static int Phase = _STARTSETPLAY_DRIBBLING_;  static int HangCounter = 0;  switch(Phase){  case _STARTSETPLAY_DRIBBLING_:    if ( !Mem->BallValid() || Mem->GetBallDistance() > cdist )      HOLDPOSITION(gpow);                  /* Ball's gone   */    else if ( !Mem->NumOpponentsWithin(pdist) ){       /* Go to the ball and dribble at their goal */      COLLECTANDDRIBBLE(THEIR_GOAL,&ChooseNN,cdist,gpow,dpow);    }    else{                                  /* Near defender */      Phase++;      HangCounter = 0;      TreePass(ReceiverChoiceFunction,cdist,pdist,gpow,dpow,spow);    }    break;  case _STARTSETPLAY_PASSING_:    if ( TreePass(ReceiverChoiceFunction,cdist,pdist,gpow,dpow,spow) == 	 _TREEPASS_RESET_ ){ /* Once more for Treepass to reset, then go on */      TreePass(ReceiverChoiceFunction,cdist,pdist,gpow,dpow,spow);      Phase++;    }    else if ( HangCounter++ > 20 ) {	Phase = _STARTSETPLAY_DRIBBLING_;    }	    break;  case _STARTSETPLAY_RESET_:    if ( HOLDPOSITION(gpow) == SUCCESS )      Phase = _STARTSETPLAY_DRIBBLING_;    break;  default: Phase++;  }  return Phase;}/*****************************************************************************/#define FIND_BALL_TEST 0int ChangePositionsAndAct (int (*ActFunction)(char*,int*),			   float DashPower, float ShotPower){      /*** Should pass CollectDist into HoldPositionAndAct--do the 3 collectdist    options here INSTEAD of there        ***/    #define _CHANGEPANDA_LOOKING_   0  #define _CHANGEPANDA_GOING_     _CHANGEPANDA_LOOKING_+1  #define _CHANGEPANDA_RETURNING_ _CHANGEPANDA_GOING_  +1  static int LastPhase = _CHANGEPANDA_LOOKING_;  if ( Mem->PlayMode==BEFORE_KICK_OFF ) {    for ( int i=1; i<=TEAM_SIZE; i++){      if ( Mem->TeammateValid(i) && Mem->GetPlayerPosition(i) == UNKNOWN_POSITION 	   && !int_random(10)){	SAYTO(i,PING_MSG);	return LastPhase;      }    }  }  if ( !Mem->SetPlay && Mem->GetMyActiveStatus() != INSETPLAY )    Mem->UpdateActive(); #if FIND_BALL_TEST  static int looking = 0;#endif  if ( !Mem->BallValid() ){ /* Just looking for the ball */    if ( LastPhase != _CHANGEPANDA_LOOKING_ )       LastPhase = _CHANGEPANDA_LOOKING_;#if FIND_BALL_TEST    looking = Mem->CurrentTime;#endif    FINDBALL;    }#if 1  else if (1){#if FIND_BALL_TEST    if (looking){      printf("%d Found the ball in %d (%d)\n",Mem->MyNumber,	     Mem->CurrentTime - looking, Mem->view_width);      looking = 0;    }#endif    HOLDPOSITIONANDACT(ActFunction,DashPower,ShotPower);    return 0;  }#endif  else if ( Mem->GetMyActiveStatus() == ACTIVE ){    if ( HOLDPOSITIONANDACT(ActFunction,DashPower,ShotPower) != TRIED )      my_error( "Should return TRIED");    if ( LastPhase != _CHANGEPANDA_GOING_ ) {  /* Say after doing */      /*************************************************************/      /* Announce that I'm going to the ball if I wasn't last step */      /* Others should free my position to fill                    */      /*************************************************************/       SAY(MY_BALL_MSG);       LastPhase = _CHANGEPANDA_GOING_;    }    else{      ;/*if ( !int_random(4) )	SAY(MY_BALL_MSG);      /* Gives far away players my position */    }  }  else { /* ball is farther than CollectDist:  going to position */    if ( LastPhase != _CHANGEPANDA_RETURNING_ ){      /*************************************************************/      /* Consider changing positions--announce if I do             */      /* When I was chasing ball, see if someone else filled my    */      /* position.  If not, go back, if so look for a new one      */      /*************************************************************/      if ( Mem->GetPositionPlayer(Mem->GetMyPosition()) != Mem->MyNumber ){	/* Someone took my position   */	Mem->SetMyPosition(GetNewPosition());        }      ANNOUNCEMYPOSITION;        LastPhase = _CHANGEPANDA_RETURNING_;    }    else { /* I'm happy holding my position, but I need to consider if another	      is better */      int position;      if ( (position = ConsiderNewPosition()) != UNKNOWN_POSITION ){	Mem->SetMyPosition(position);	ANNOUNCEMYPOSITION;       }    }    int result = HOLDPOSITIONANDACT(ActFunction,DashPower,ShotPower);    if ( result != TRYING && result != SUCCESS )      my_error( "Should return TRYING or SUCCESS");  }  return LastPhase;}/*****************************************************************************//*****************     Switch Function     ***********************************//*****************************************************************************/void DoBehavior(int behavior){    static int FIRST_TIME = TRUE;

⌨️ 快捷键说明

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