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

📄 brianteam.java

📁 一个多机器人的仿真平台
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package Domains.SoccerBots.teams;/* * BrianTeam.java */import   EDU.gatech.cc.is.util.Vec2;import   EDU.gatech.cc.is.abstractrobot.*;import java.lang.Math;/** * Homogeneous team written by Brian McNamara *  * @author  Brian McNamara (lorgon@cc.gatech.edu) * @version $Revision: 1.1 $ *//*Here's a plausible strategy I'd like to implement:havetheball: am "behind" the ball and very near itclear: within kicking distance and no robots lie along pathopen: no opponent robot within x distancedefense/offense: which side has ball, if unclear, say defenseif( i have the ball ) then   if( clear shot ) then      shoot   else if( i am open ) then      dribble towards goal   else if( closest teammate is open && clear pass ) then      pass to him   else      ???   endifelse   if( i am closest to ball ) then      go to "behind" ball (avoid bumping it backwards)   else      if( i am closest to ourgoal ) then         go to point between ball and ourgoal nearest ourgoal      else if( on defense )         mark a man (go to nearest open opponent)      else         get open (move away from nearest opponent)      endif   endifendif*/public class BrianTeam extends ControlSystemSS{   double GOAL_WIDTH =  0.42; //?   double BALL_RADIUS = 0.01; //?   /**   Configure the control system.  This method is   called once at initialization time.  You can use it   to do whatever you like.   */   public void Configure()   {      // not used in this example.   }         public static final double ROBOT_RADIUS = 0.06; // should refer to                     // actual value, but is hardcoded for convenience   static String coor( Vec2 v )   // returns string like "(1.2, 2.0)"   {       String x = String.valueOf(v.x);      String y = String.valueOf(v.y);      return "("+x.substring(0,x.indexOf('.')+2)+","+                 y.substring(0,y.indexOf('.')+2)+")";    }   String my_name()   {      return names[(int)my_id()];   }   public long my_id()   {      return abstract_robot.getID();   }   // Used for debugging   boolean debug_reminders[] = new boolean[ Debugger.MAX ];   static long who_closest_to_ball=0;   static long who_closest_to_ourgoal=0;   static final String names[] = { "","","","","","",                                       "orange", "red   ",                                        "yellow", "purple", "white " };   static final boolean       DEBUG_CLOSEST_TO_OUR_GOAL = false,       DEBUG_CLOSEST_TO_BALL     = false,       DEBUG_STATE               = false,       DEBUG_AROUND              = false,       dummy                     = false;   static final int       SHOOT     = 0,      DRIBBLE   = 1,      RUN       = 2,      DEFEND    = 3,      OPEN_OPP  = 4,      OPEN_TEAM = 5,      OPEN_BALL = 6,      CLEAR     = 7,      foo       = -1;   static final String state_messages[] = {      " is going to shoot!",      " is dribbiling toward the goal.",      " is running toward the ball.",      " is defending the goal.",      " is trying to get open, away from opponent.",      " is trying to get open, away from teammate.",      " is trying to get open, toward the ball.",      " is trying to clear the ball.",      "" };   int current_state=0;   void set_state( int new_state )   {      if( DEBUG_STATE )      {         if( new_state != current_state )         {            current_state = new_state;            System.out.println( my_name() + state_messages[ new_state ]);         }      }   }   void debug()   {      long my_id = my_id();      if( DEBUG_CLOSEST_TO_OUR_GOAL )      {         if( i_am_closest_to( ourgoal ) )         {            if( who_closest_to_ourgoal != my_id )            {               who_closest_to_ourgoal = my_id;               System.out.println( "Robot " + names[(int)my_id] +                   " at " + coor( me ) +                  " is closest to our goal at " + coor( ourgoal ) );            }         }      }         if( DEBUG_CLOSEST_TO_BALL )      {         if( i_am_closest_to( ball ) )         {            if( who_closest_to_ball != my_id )            {               who_closest_to_ball = my_id;               System.out.println( "Robot " + names[(int)my_id] +                   " at " + coor( me ) +                  " is closest to ball at " + coor( ball ) );            }         }      }   //      new Debugger( this, Debugger.TOP_HALF_OF_FIELD );//      new Debugger( this, Debugger.CLOSEST_TO_BALL );//      new Debugger( this, Debugger.HAS_THE_BALL );//      new Debugger( this, Debugger.CLOSE_TO_THE_BALL );//      new Debugger( this, Debugger.BEHIND_THE_BALL );//      new Debugger( this, Debugger.HAS_CLEAR_SHOT );//      new Debugger( this, Debugger.IS_OPEN );//      new Debugger( this, Debugger.IN_GOALBOX, true );   }   // instance variables for convenience across modules   long curr_time;   Vec2 me, ball, ourgoal, theirgoal;   Vec2 ego_ball, ego_ourgoal, ego_theirgoal;   Vec2 ego_teammates[], ego_opponents[];   /**   Find the closest opponent, return ego vector to him   */   Vec2 closest_opponent()   {      Vec2 closestopponent = new Vec2( -99999, 0 );      for (int i=0; i< ego_opponents.length; i++)      {         if (ego_opponents[i].r < closestopponent.r)            closestopponent = ego_opponents[i];      }      return closestopponent;   }   /**   Find the closest teammate, return ego vector to him   */   Vec2 closest_teammate()   {      Vec2 closestteammate = new Vec2( -99999, 0 );      for (int i=0; i< ego_teammates.length; i++)      {         if (ego_teammates[i].r < closestteammate.r)            closestteammate = ego_teammates[i];      }      return closestteammate;   }   boolean i_am_closest_to_the_ball()   {      return i_am_closest_to( ball );   }   /**   Returns true if I am the closest of my ego_teammates to a given location   */   boolean i_am_closest_to( Vec2 something )   // should probably be rewritten to use common functions   {      double my_dist, their_dist;      Vec2 temp;      Vec2[] ego_teammates = abstract_robot.getTeammates(curr_time);      for (int i=0; i< ego_teammates.length; i++)      {         temp = new Vec2( ego_teammates[i].x, ego_teammates[i].y );         temp.add( me );         temp.sub( something );         their_dist = temp.r;         temp = abstract_robot.getPosition( curr_time );         temp = new Vec2( temp.x, temp.y );         temp.sub( something );         my_dist = temp.r;         if( their_dist < my_dist - 0.05 )   // fudge factor            return false;      }      return true;   }      /**   Return a Vec2 whose theta is directed from a to b and whose r is dist(a,b)   */   static Vec2 toward( Vec2 a, Vec2 b )   {      Vec2 temp = new Vec2( b.x, b.y );      temp.sub( a );      return temp;   }      void setInstanceVars()   {      // get the current time for timestamps      curr_time = abstract_robot.getTime();      // get a list of the positions of our ego_teammates      ego_teammates = abstract_robot.getTeammates(curr_time);      ego_opponents = abstract_robot.getOpponents(curr_time);//should create non-egos, too      me = abstract_robot.getPosition(curr_time);      ego_ball = abstract_robot.getBall(curr_time);      ball = new Vec2( ego_ball.x, ego_ball.y );      ball.add( me );      ego_ourgoal = abstract_robot.getOurGoal(curr_time);      ourgoal = new Vec2( ego_ourgoal.x, ego_ourgoal.y );      ourgoal.add( me );      ego_theirgoal = abstract_robot.getOpponentsGoal(curr_time);      theirgoal = new Vec2( ego_theirgoal.x, ego_theirgoal.y );      theirgoal.add( me );   }   /**   Called every timestep to allow the control system to   run.   */   public int TakeStep()   {      // the eventual movement command is placed here      Vec2   result;      Vec2 closestteammate = new Vec2();      setInstanceVars();      debug();      boolean kick_ball = false;      result = toward( me, ball );if( false ){   result = new Vec2( GOAL_X, -0.4 );   result = toward( me, result );}else{      if( i_have_the_ball() )      {         if( i_have_a_clear_shot() && toward(me,ego_theirgoal).r < 0.3 ) // reasonable shooting distance         {            kick_ball = true;            result = toward( me, ball );  // dribble toward goalset_state( SHOOT );         }         else if( i_am_in_my_goalbox() )         {            kick_ball = true;            result = toward( me, ball );  // dribble toward goalset_state( CLEAR );         }   // out since open() fails...//         else if( open( me, true ) ) // if i am open//         {//            result = toward( me, ball );  // dribble toward goal//         }//         else if( closest teammate is open && clear pass ) then//            pass to him         else // dunno a good strategy         {            result = toward( me, ball );  // dribble toward goalset_state( DRIBBLE );         }      }      else      {         if( i_am_closest_to( ball ) )         {            result = new Vec2( 1.0, 0 );            result.sett( toward_behind_ball() );set_state( RUN );         }         else if( i_am_closest_to( ourgoal ) &&                   ((me.x > 0)==i_am_on_west_team()) )         {            result = new Vec2( 0, 0 );                     result = toward( me, result );// play goalie//            result = new Vec2( 1.0, 0 );//            result.sett( defend_goal() );//set_state( DEFEND );         }//         else if( on defense )//            mark a man (go to nearest open opponent)         else         {            //get open (move away from nearest opponent)            result = closest_opponent();            if( result.r < 2*ROBOT_RADIUS )            {               result.sett( result.t + Math.PI );set_state( OPEN_OPP );            }            else            {               result = closest_teammate();               if( result.r < 4*ROBOT_RADIUS )               {                  result.sett( result.t + Math.PI );set_state( OPEN_TEAM );               }               else               {                  result = toward( me, ball );

⌨️ 快捷键说明

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