package ch.jp.robwar; /** * This class shows a sample robot brain that goes forward. * When it hits a wall, it bounces, turns, shoots and resume * its forward movement. * * @author JP Martin * @date 8.28.1998 */ public class WallBouncerShooter implements RobotBrain { /** * This internal variable is used to remember where the robot * wants to go. It is incremented by 90 each time a wall is hit. */ private double targetAngle = 270.0; /** * This internal variable, when !=0, forces the robot to back up. */ private int recule = 0; /** * This allows the robot brain to remember its past actions. */ private Instructions previousInstr; /** * This function is called by the simulation for each time step. * It returns the orders that the robot brain gives to the robot body. */ public Instructions nextMove(Feedback senses) { double rotation; // if I hit a wall, start stepping back and then turning if (senses.stimuli.hitObstacle) { targetAngle+=100; if (recule==0) recule=8; // If we hit a wall going backwards, we don't want // to step back anymore else recule=0; } // am I stepping back? if (recule>0) { recule--; previousInstr=new Instructions(0,-0.03,false); return (previousInstr); } // keeping the angles in [0..360[ makes the job easier if (targetAngle>=360) targetAngle-=360; if (targetAngle<0) targetAngle+=360; // rotation represents how much I want to turn to face // the target angle rotation=360-senses.selfInfo.orientation+targetAngle; // we want to turn the fastest way // (i.e. rather turn -10 than 350) if (rotation>180) rotation=rotation-360.0; if (rotation>180) rotation=rotation-360.0; if (rotation<-180) rotation=rotation+360.0; if (java.lang.Math.abs(rotation)>0.1) { // turn to reach targetAngle if (rotation>5) rotation=5; else if (rotation<-5) rotation=-5; previousInstr=new Instructions(rotation,0,false); return previousInstr; } else { // We have the right orientation, go forward (ev. shoot) previousInstr=new Instructions(0.0,0.1,previousInstr==null || previousInstr.getRotation()!=0); return previousInstr; } } }