J R O B O T S ============= A strategic programming game system By Drew Bowering Curtis Yallop 1999/09/15 Writing Your Own Robots ======================= Writing your own robots using JRobots is fairly easy, if you know how to program in Java already. If you don't know Java, please look for a good book! I may write a tutorial in a later version, but that can wait. 1) The game world ----------------- In this game, 2 or more robots will be loaded to run their pre written programs, designed to destroy all of the other robots. The robot's world is a grid, 1000 units wide and 1000 units high. The grid is measured using coordinates, (0,0) being the lower-left and (1000,1000) being the upper right. All of the edges are walled, and are impassable. Directions in the game are measured in radians. They follow the normal mathematical convention, as shown below: 0.5*PI | (To convert from radians to degrees, | multiply by 180/PI) | PI -----------+----------- 0 | | (To convert from degrees to radians, | multiply by PI/180) 1.5*PI The robots themselves have the following capabilities: - They can travel in any direction, at a variety of speeds (denoted 0-100). - They can tell where they are on the grid. - They are equipped with a grenade launcher, that can fire a grenade in any direction (independent of travel direction), for a distance of up to 700 units. It travels fairly fast, but takes time. It has a blast radius of 10 units. - They are equipped with a missile launcher, which can fire a missile up to 1000 units away. Its blast radius is 50 units. The missile fires like a grenade. The robot can only carry 5 missiles, and cannot reload. Use missiles sparingly! - They are equipped with a gun, that can hit a target up to 150 units away. The bullet will hit the first thing in its path, giving it a small amount of damage. It takes virtually no time for the bullet to travel its full distance. 2) Writing a Robot ------------------ The first thing you should know about is the Robot class. The Robot class is an abstract base class, from which your robot class will be derived. Let's look at the abstract members of the Robot class: public abstract void main(); public abstract String robotName(); public abstract String programmerName(); public abstract void shot(double Damage); First of all, the main() function is where the guts of your robot are implemented. When the game loads your robot class, it will call the main() function to get your robot moving. The robotName() function is there so you can give a name to your robot, which will be shown as the game starts up. It is simple to implement, and usually looks something like this: public String robotName() { return new String("My Robot"); } The "My Robot" string can be whatever name you want to give to your robot. The programmerName() function is just like the robotName() function, but it returns your name instead. It is implemented just like the robotName() function, except that you put you name (or whatever clever alias you give yourself) in the string. The shot() function is there to let you know when your robot gets shot. It will be called by the game itself, and you can put whatever code you like in here -- even nothing if you don't care about getting shot. You might implement it like this: public void shot(double Damage) { drive(Math.PI, 100); } This will cause your robot to run straight west at full speed whenever it gets shot. 3) Doing Stuff -------------- How do you get the robot to do anything, you ask? Well, there are several protected functions in the Robot base class, which are already implemented. You call these functions to get your robot doing things. Let's take a look: protected void drive(double dir,double throttle); protected boolean grenade(double dir,double distance); protected boolean gun(double dir); protected boolean missile(double dir, double distance); protected double scan(double dir,double precision); protected DPoint getLocation(); protected double getDamage(); You can call all of these functions from within your main() function (or any other function you implement within your robot class) directly. They don't need any prefix. The drive() function tells your robot to move around. call the drive function like this: drive(Math.PI, 70); This causes the robot to head west at 70% full speed. There are acceleration times, so the robot won't head that way instantly. If it was previously heading due east, it will slow to a stop, turn around, and accelerate west. The grenade() function tells the robot to lob a grenade. Call it like this: grenade(Math.PI, 500); This will cause the robot to fling a grenade 500 units due west. Note that the grenade will actually travel the distance you specify, and explode on that spot. If a robot stands between you and the target, the grenade will travel over it to the target, where it will explode and damage anything within 10 units. The gun() function will fire the robot's gun. Call it like this: gun(0); This will fire the gun due east. Notice the lack of a distance argument. This is because the bullet will hit the first target it encounters, and inflict damage upon it. If no target is within 150 units, the bullet will disappear and do nothing. The missile() function works exactly like the grenade() function. The only difference is that the missile can travel up to 1000 units on the map, and inflicts damage over a greater area. The scan() function serves as your eyes to the outside. This function is used to find the other robots. Call it like this: d = scan(Math.PI, precision); The first argument is the direction in which to look. It is independent of the travel direction. The precision argument is the number of radians in the "slice" that we are looking down. If a robot is spotted anywhere in this slice, the distance that it is from you will be returned. If no robot is found, 0 is returned. PI-(prec/2) 0.5*PI \ | \ | \ | \ | + \ | \ | \| PI -------------+------------ 0 /| / | / | / | / | / | / | PI+(prec/2) 1.5*PI In this diagram, the function was called as scan(Math.PI, prec);. Another robot (the +) is within the slice, so the function returns the distance between the tho robots. The value of prec can be anywhere between 0 and PRECISION_MAX (defined in RobotConstants.class). It is equal to about 10 degrees. The getLocation() function returns the location of the robot. Call it like this: DPoint p = getLocation(); The returned DPoint is a simple class with 2 members, x and y. These correspond to the robot's current location. The getDamage() function returns how damaged your robot is. It is a value from 0 to 100. If it is 0, your robot is in mint condition. If it is 100, your robot is destroyed. 4) Ideas -------- Look at the robot sources in the TestRobots directory for some sample robot implementations. You may use these as skeletons for building your own robots. Try new strategies with your robots, being able to program them gives you limitless possibilities!