📄 mattihetero.java
字号:
if shotvalue(tempangle+ Math.abs(Math.sin(norm_anglediff(theta,opponents[i].t)))*opponents[i].r<ROBOT_RADIUS) // negative value value = -1; */// Vec2[] shots=Vec2[];// System.out.println("bestshot: "+best_shot + " value: "+ maxvalue); abstract_robot.setDisplayString("find shot: "+who); return best_shot; } /** Get some sort of a quality factor for a shot in the indicated direction */ private double shot_value(double theta) { double value=0; double temp_value=0; for (int i=0; i< teammates.length; i++) { if (teammates[i].r <PASS_DISTANCE) // positve = attractive value { if (normalizeZero(theirgoal.t-teammates[i].t)<0) //teammate on the left side of goal? { temp_value=normalizeZero(teammates[i].t-theta); if (temp_value>ROBOT_RADIUS/teammates[i].r) // pointing in front of the teammate? { temp_value=1-temp_value/(Math.PI/6); if (temp_value>0) value+=temp_value; } } else // teammate on the right side { temp_value=normalizeZero(teammates[i].t-theta); if ( temp_value<-ROBOT_RADIUS/teammates[i].r) // pointing in front of the teammate? { temp_value=1+temp_value/(Math.PI/6); if (temp_value>0) value+=temp_value; } } } } //for // Pointing towards their goal (between the goal-Posts? temp_value =(normalizeZero( ball.t-theirgoal.t)); if ((temp_value<normalizeZero(theirleftpost.t-theirgoal.t))&& temp_value>normalizeZero(theirrightpost.t-theirgoal.t)) if (theirgoal.r<PASS_DISTANCE) value+=3.5; else value+=0.7; // Pointing towards an opponent -> overrides all the previous values for (int i=0; i< opponents.length; i++) { if (Math.abs(Math.sin(normalizeZero(opponents[i].t-theta)))*opponents[i].r<ROBOT_RADIUS) // negative value value = -1; }// System.out.println("shot: "+value+" at "+theta); return value; } /* * Given two vectors, return a vector pointing from the first to thesecond */ private Vec2 toward(Vec2 a, Vec2 b) { Vec2 temp = new Vec2(b.x, b.y); temp.sub(a); return temp; } /** Compute a vector, that points behind the ball (from the ball) Choosing offensive or defensive strategy, depending on the position of theball */ private Vec2 behindball() { Vec2 behind_ball = toward(ball,ourgoal); if (behind_ball.r >DEFENSE_RADIUS) { // choose offensive strategy if ball far from our goal // --> behind is calculated with respect to the other goal: behind_ball = toward(theirgoal,ball); behind_ball.setr(ROBOT_RADIUS*1.0); // abstract_robot.setDisplayString("Attack"); } else { behind_ball.setr(abstract_robot.RADIUS*1.5);// abstract_robot.setDisplayString("Defense"); }; return behind_ball; } /** Returns the location in front of the teammate towards the goal*/ private Vec2 frontofteammate(Vec2 player) { Vec2 before = toward(player,theirgoal); before.setr(ROBOT_RADIUS*2); return before; } /** Normalize an angle into the range [-PI,PI] */ private double normalizeZero(double angle) { while (angle>Math.PI) angle -= 2*Math.PI; while (angle<-Math.PI) angle += 2*Math.PI; // range -PI .. PI return angle; } /** returns a vector to get around the ball to the behindspot without bumpingthe ball */ private Vec2 find_around_ball (Vec2 spotfromball) { Vec2 behindspot = new Vec2(spotfromball); behindspot.setr(ROBOT_RADIUS*0.3); behindspot.add(ball); if(Math.abs(normalizeZero(spotfromball.t-toward(ball,me).t))>Math.PI/2) // test if really behind { if (normalizeZero(behindspot.t-ball.t) >0) // pass on the left of ball { behindspot.rotate(1.2*Math.asin(Math.min(1,ROBOT_RADIUS/ball.r))); abstract_robot.setDisplayString("Turn 'left"); } else { behindspot.rotate(-1.2*Math.asin(Math.min(1,ROBOT_RADIUS/ball.r))); abstract_robot.setDisplayString("Turn 'right"); }; } else // behind the ball behindspot.r < ball.r if ((behindspot.r<ROBOT_RADIUS)&&i_have_ball()) { double dribble_cheat = 0.5; if (normalizeZero(behindspot.t-ball.t)>0) behindspot.rotate(dribble_cheat); else behindspot.rotate(-dribble_cheat); abstract_robot.setDisplayString("Dribble"); } return behindspot; } /** Same thing as find_around_ball, but to get around another player */ private Vec2 find_around_player (Vec2 player,Vec2 spot) { Vec2 behindspot = new Vec2(toward(player,spot)); behindspot.add(player); if(Math.abs(normalizeZero(toward(player,spot).t-toward(player,me).t))>Math.PI/2) // test if really behind if (normalizeZero(behindspot.t-player.t) >0) // pass on the left of player { behindspot.rotate(1.2*Math.asin(Math.min(1,1.9*ROBOT_RADIUS/player.r))); abstract_robot.setDisplayString("avoid left"); } else { behindspot.rotate(-1.2*Math.asin(Math.min(1,1.9*ROBOT_RADIUS/player.r))); abstract_robot.setDisplayString("avoid right"); }; return behindspot; } /** Do we control the ball? */ private boolean we_have_ball() // { return(distance(ball,closest_teammate_to(ball))<distance(ball,closest_opponent_to(ball))); } /** Am I at the ball and behind the ball? */ private boolean i_have_ball() // { Vec2 spot = new Vec2(ball); return(i_am_closest_to(ball)&&(Math.abs(normalizeZero(theirgoal.t-ball.t))<Math.PI/2)); } private double distance(Vec2 spot1,Vec2 spot2) { if ((spot1==null)||(spot2==null)) return 9999; else { Vec2 temp = new Vec2(spot1); temp.sub(spot2); return temp.r; } } /** Who is closest to the point (from our team)? */ private Vec2 closest_teammate_to(Vec2 spot) // { double closest_r; Vec2 closest = null; closest_r=9999; //distance(me,spot); for (int i=0; i< teammates.length; i++) { if (distance(teammates[i],spot)<closest_r) { closest = teammates[i]; closest_r=distance(closest,spot); } } return closest; } private Vec2 closest_opponent_to(Vec2 spot) // { double closest_r; Vec2 closest = me; closest_r=distance(me,spot); for (int i=0; i< opponents.length; i++) { if (distance(opponents[i],spot)<closest_r) { closest = opponents[i]; closest_r=distance(closest,spot); } } return closest; } /**Am I closest to this spot? (overestimating the distances of the other teammates, so several canbe closest) */ private boolean i_am_closest_to(Vec2 spot) { Vec2 behind = behindball(); behind.setr(2*ROBOT_RADIUS); behind.add(ball); return ((distance(closest_teammate_to(spot),spot)>spot.r*0.9)|| (distance(closest_teammate_to(behind),behind)>behind.r*0.9)); } /** am I leftmost? */ private boolean i_am_leftmost() // { Vec2 leftmost = me; for (int i=0; i< teammates.length; i++) { // slightly egoistic view. tends to see itself on the left even if its not exactly the case if (SIDE*teammates[i].y< SIDE*leftmost.y-ROBOT_RADIUS/2) leftmost = teammates[i]; } return me==leftmost; } /** am I rightmost ? */ private boolean i_am_rightmost() // { Vec2 rightmost = me; for (int i=0; i< teammates.length; i++) { if (SIDE*teammates[i].y-ROBOT_RADIUS/2> SIDE*rightmost.y) rightmost = teammates[i]; } return me==rightmost; } /** am I backmost ? */ private boolean i_am_backmost() // { Vec2 backmost = me; for (int i=0; i< teammates.length; i++) { if (SIDE*teammates[i].x> SIDE*backmost.x) backmost = teammates[i]; } return me==backmost; } /** am I frontmost ? */ private boolean i_am_frontmost() // { Vec2 frontmost = me; for (int i=0; i< teammates.length; i++) { if (SIDE*teammates[i].x< SIDE*frontmost.y) frontmost = teammates[i]; } return me==frontmost; } //from DTeam private void debug( String message) { //if( DEBUG) System.out.println( ": " + message); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -