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

📄 act.c

📁 足球机器人仿真组CMU97的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* act.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"/* Everything in here should return SUCCESS, TRIED, TRYING, or FAILURE = 0 *//* SUCCESS = positive of success, failure = positive of failure,    TRYING = I need more cycles, TRIED = pretty sure of success */#define COLLECT_DEBUG         0#define CLEAR_DEBUG           0#define DRIBBLE_DEBUG         0#define CHOICE_FUNCTION_DEBUG 0#define TUNED_PARAMS 0#if TUNED_PARAMS/* playerSpeed, timeMultiplier1, timeMultiplier2, dashTwiceBuffer,   facePointBuffer, DashPower, GoToPointBuffer *//* Backwards movement discouraged */float TunedParams[7] = { 0.590, 1.165, 1.0,   100.0, 9.191, 53.5, 0.87 };/* When back was allowed *///float TunedParams[7] = { 0.591, 1.448, 1.058,   100.0, 0.76, 50.0, 0.87 };#endif/*****************************************************************************//************        LOW LEVELS       ****************************************//*****************************************************************************//*****************************************************************************/int FaceBall(){    if ( !Mem->BallAngleValid() ) {    return FAILURE;  }  else if ( Mem->GetBallAngle() ) {           /* If not facing ball*/    TURN(Mem->GetBallAngle());                /* Face it           */    return TRIED;  }  else if ( Mem->BallAngleValid() == 1 ) {    /* Sure I'm facing   */    return SUCCESS;  }  else              /* Think I'm facing the ball, but not positive */     return TRIED;}/*#define FACEBALL FaceBall()*//*****************************************************************************/int FaceMarker(int marker ){  if ( !Mem->MarkerValid(marker) ) {    return FAILURE;  }  else if ( Mem->GetMarkerAngle(marker) ) {           /* If not facing marker*/    TURN(Mem->GetMarkerAngle(marker));                /* Face it           */    return TRIED;  }  else if ( Mem->MarkerValid(marker) == 1 ) {         /* I see it in front */    return SUCCESS;  }  else                    /* Think I'm facing the marker, but not positive */     return TRIED;}/*#define FACEMARKER(a) FaceMarker(a)*//*****************************************************************************/int FacePlayer(char side, int player ){  if ( !Mem->PlayerAngleValid(side, player) ) {    return FAILURE;  }  else if ( Mem->GetPlayerAngle(side, player) ) {  /* If not facing player*/    TURN(Mem->GetPlayerAngle(side, player));       /* Face it           */    return TRIED;  }  else if ( Mem->PlayerValid(side, player) == 1 ) {/* I see it in front */    return SUCCESS;  }  else                   /* Think I'm facing the player, but not positive */     return TRIED;}/*#define FACEPLAYER(a,b) FacePlayer(a,b)#define FACETEAMMATE(a) FacePlayer(Mem->MySide,a)#define FACEOPPONENT(a) FacePlayer(Mem->TheirSide,a)*//*****************************************************************************/int FacePoint(float x, float y, float buffer){  float dx = x - Mem->GetGlobalX();  float dy = y - Mem->GetGlobalY();  float angle = my_atan2(dy,dx);  float da = rad_to_deg(Mem->GetGlobalAngle() - angle);  CleanAngle(&da);#if 0  printf("dx: %f,dy: %f,angle: %f, da: %f\n",dx,dy,180*angle/M_PI,da);#endif  if ( fabs(da) < buffer )                               /* <1 is close enough */    return SUCCESS;  int viewAngle = Mem->GetViewAngle()/2;  if ( fabs(da) > viewAngle && Mem->Dashed ){       /* If it's a big turn and*/    int counter = 0;                                /* I was moving, verify  */        if ( Mem->PlayMode != PLAY_ON )      counter = 10; /* No rush, don't want to look silly */    else      if ( Mem->view_width != NARROW_WIDTH ) 	LOW_QUAL_VIEW;                    /* not so much advantage if narrow */    while ( fabs(da) > viewAngle ) {      if (counter++ > 10){	  if (counter > 20)	    return FAILURE;	HIGH_QUAL_VIEW;  /* I must be confused about my position */	dx  = x - Mem->GetGlobalX();   /* reevaluate it          */	dy  = y - Mem->GetGlobalY();	angle = my_atan2(dy,dx);      }      da = rad_to_deg(Mem->GetGlobalAngle() - angle);      CleanAngle(&da);#if 0      if ( (counter > 10 && Mem->PlayMode == PLAY_ON) 	   || counter > 15 )	printf("%2d:%d (%d) counter = %d Facing %.1f (%.1f %.1f). Now at %.1f (%.1f %.1f). Turning  %.1f\n",		 Mem->MyNumber,Mem->CurrentTime,Mem->PlayMode,counter,		 rad_to_deg(angle),x,y,		 rad_to_deg(Mem->GetGlobalAngle()),Mem->GetGlobalX(),Mem->GetGlobalY(),da);#endif      TURN(da);      WAITFORSIGHT;    }    HIGH_QUAL_VIEW;  }  if ( fabs(da) > buffer )                             /* <1 is close enough */    TURN(da);  return TRIED;}/*#define FACEPOINT(a,b) FacePoint(a,b,1)*//*****************************************************************************/int LookForBall(char dir){  /* Moving first, pinging only if time is left */  if ( !(Mem->BallAngleValid()) ) {       /* If Can't see ball */#if 0        float ang;    if ((ang=Mem->GetBallAngle()) > Mem->GetViewAngle()/2){      TURN(ang);                      /* Look for where ball was last */      printf("remembering ball\n");    }    else#endif      LOOK(dir);                                 /* Look for it         */     WAITUNTILFREETOACT;    if ( !Mem->NewSight ){         /* Don't talk when action's critical */      SAY(PING_BALL_MSG);     }      return TRYING;  }  else     return FACEBALL;               /* SUCCESS if sure, else TRIED       */}/*#define LOOKFORBALL LookForBall('r')*//*****************************************************************************//* Don't return until ball is found */int FindBall(){  char dir = (int_random(2) ? 'r' : 'l');  int oldWidth = Mem->view_width;  int newWidth;  if (oldWidth == NARROW_WIDTH)    newWidth = NORMAL_WIDTH;  else     newWidth = oldWidth;#if 0  newWidth = oldWidth;  /* Allows you to look at narrow, low */#endif  if ( !Mem->BallAngleValid() ) {    CHANGE_VIEW(newWidth,LOW_QUALITY);    SAY(PING_BALL_MSG);    WAITUNTILFREETOACT;  }  while ( !Mem->BallAngleValid() ) {    LOOK(dir);    WAITFORSIGHT;  }  CHANGE_VIEW(oldWidth,HIGH_QUALITY);  WAITUNTILFREETOACT;  if ( !Mem->BallAngleValid() ){ /* The ball just moved again          */    return FindBall();   }  return FACEBALL;              /* SUCCESS if sure, else TRIED        */}/* #define FINDBALL FindBall() *//*****************************************************************************/int LookForMarker(int marker, char dir ){    if ( !(Mem->MarkerValid(marker)) ) {     /* If Can't see marker */    LOOK(dir);                             /* Look for it         */     return TRYING;  }  else if (Mem->GetMarkerAngle(marker)) {  /* If not facing marker*/    TURN(Mem->GetMarkerAngle(marker));     /* Face it             */    return TRIED;  }  else if ( Mem->MarkerValid(marker) == 1 ){/* Sure you're facing */    return SUCCESS;  }  else{        /* Think you're facing the marker but not positive */    return TRIED;  }}/*#define LOOKFORMARKER(a) LookForMarker(a,'r')*//*****************************************************************************/int LookForPlayer(char side, int player, char dir ){    if ( !(Mem->PlayerAngleValid(side, player)) ) {/* If Can't see player */    if ( side == Mem->MySide ){      SAYTO(player,PING_MSG);                      /* Ping the player     */      WAITUNTILFREETOACT;    }    if ( !(Mem->PlayerAngleValid(side, player)) ) {/* check again       */#if 0      float ang;      if ((ang=Mem->GetPlayerAngle(side,player)) > 30){	TURN(ang);                      /* Look for where player was last */	printf("remembering player\n");      }      else{#endif	LOOK(dir);                                 /* Look for it         */     /*}*/      return TRYING;    }  }  /* Player angle is valid */  if (Mem->GetPlayerAngle(side, player)) {  /* If not facing player*/    TURN(Mem->GetPlayerAngle(side, player));     /* Face it             */    return TRIED;  }  else if ( Mem->PlayerAngleValid(side, player) == 1 ){/* Sure you're facing */    return SUCCESS;  }  else{              /* Think you're facing the player but not positive */    return TRIED;  }}/*#define LOOKFORPLAYER(a,b) LookForPlayer(a,b,'r')#define LOOKFORTEAMMATE(a) LookForPlayer(Mem->MySide,a,'r')#define LOOKFOROPPONENT(a) LookForPlayer(Mem->TheirSide,a,'r')*//*****************************************************************************//* Don't return until ball is found */int FindPlayer(char side, int player){  char dir = (int_random(2) ? 'r' : 'l');  if ( !Mem->PlayerAngleValid(side,player) ) {    PINGPLAYER(side,player);    WAITUNTILFREETOACT;  }  int counter=0;  int limit= 360/Mem->GetViewAngle() +1;  /* Turn more than 360 degrees */  while ( !Mem->PlayerAngleValid(side,player) ) {    if (counter++ > limit)      return FAILURE;    LOOK(dir);    WAITFORSIGHT;  }  return TRIED;  /* Know where it is, but not facing */}/* #define FINDPLAYER(a,b) FindPlayer(a,b) #define FINDTEAMMATE(a) FindPlayer(Mem->MySide,a) #define FINDOPPONENT(a) FindPlayer(Mem->TheirSide,a) *//*****************************************************************************/int Move(float dist, float ang, float dpow, int MinDashTimes, int MaxDashTimes ){  float dodgeBuffer = 35;#if 0  /* Avoid getting stuck going to ball for a setplay */  if ( Mem->GetMyActiveStatus() == INSETPLAY &&       Mem->MyPositionInSetPlay() &&       Mem->GetMySetPlayPositionType() == SETPLAY_STARTER )    dodgeBuffer = 80;#endif  /* check to which side the player is (players within 22 of -22 or 22)      */  /* Don't need to dodge if the point I'm going to is closer than the player */  if ( Mem->NumPlayersWithin(MIN(3,dist),dodgeBuffer,0,ang-dodgeBuffer) &&        Mem->PlayMode != BEFORE_KICK_OFF){     DODGEPLAYER(ang+90,100,-135);         return TRYING;  }  else if ( Mem->NumPlayersWithin(MIN(3,dist),dodgeBuffer,0,ang+dodgeBuffer) && 	    Mem->PlayMode != BEFORE_KICK_OFF){    DODGEPLAYER(ang-90,100,135);         return TRYING;  }  else {    if ( dist<15 || fabs(ang)>3 ){ /* don't turn if far */      TURN(ang);      WAITUNTILFREETOACT;    }    GO(dist,dpow,MinDashTimes,MaxDashTimes);    return TRIED;  }}/*#define DASHMOVE(a,b)   Move(0,a,b,1,2)#define GOMOVE(a,b,c,d,e) Move(a,b,c,d,e)*//*****************************************************************************//* was 160, 90 */#define CIRCLE_ANGLE 130#define CIRCLE_POWER 70float NeedToCircle(float GoalAngle ){  float BallAngle = Mem->GetBallAngle();  float BGdiff = GoalAngle-BallAngle;  CleanAngle(&BGdiff);  if ( fabs(BGdiff) > CIRCLE_ANGLE )    return BGdiff;  else    return 0;}/*#define NEEDTOCIRCLE(a) NeedToCircle(a)*//*****************************************************************************/int Shoot(float kpow, float ang, int shootTimes, int trap, int hard_shoot){  if ( !Mem->BallValid() || Mem->GetBallDistance() > KICK_DISTANCE )    return TRYING;  /* should be failure */  /* Ball's not in range*/#if PRACTICE  shootTimes = 1;  trap = 0;  hard_shoot = 0;#endif  switch( Mem->GetCurrentFormationType() ){  case fRT_FORMATION:   case fLT_FORMATION:     hard_shoot=0;    shootTimes=1;    trap=0;    break;  }  float BGdiff;  if ( (BGdiff = NEEDTOCIRCLE(ang)) ){         /* Trying to kick backwards */    CIRCLEBALL(CIRCLE_POWER,BGdiff);           /* Move around the ball     */    return TRYING;  }  else {    if (trap && Mem->PlayMode == PLAY_ON){     /* Not when ball's out      */      TRAPBALL;     }    if (hard_shoot){      KICK(30,ang);    }    for (int i=0; i<shootTimes; i++){      KICK(kpow,ang);      WAITUNTILFREETOACT;      if ( Mem->NewSight ) 	break;     /* Go back for the new sight */    }

⌨️ 快捷键说明

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