back to main
JS Robots
What is it?
It's a JavaScript reimagining of Tom Poindexter's seminal CROBOTS game.
You program a script to control a robot and then put your robot up to fight against other CPU or human controlled robots each with various scripts!
Main differences between JS Robots and original CROBOTS
Overview:
- Arena size
- Coordinate system
- Compass system
- Simple Boolean detection of "in explosion or not"
- Uses floats internally (heading, speed, scan and damage are integer)
- No virtual machine proper (ie. loops are out of the question!)
Details:
- The arena is a square of 500 units by 500 units
- The top-left of the arena is (0,0). The bottom-right is (499,499).
- The compass system is oriented so that due north (up) is 0 degrees, 90 is east,
180 is south, 270 is west. One degree west (left) of due north is 359.
315 0 45
\ | /
\ | /
270 --- x --- 90
/ | \
/ | \
225 180 135
- There is no reduced damage for being further away from the centre of an explosion.
Rather it is 10 points damage if you are anywhere in the explosion.
Basic instructions
- You need at least two robots to battle (CPU and / or Player - it doesn't matter)
- Tick the checkboxes to enable various robots
- CPU robots must use one of the preset scripts
- Players can create a script from scratch, use a preset or edit a preset
- There is no proper timeslicing virtual machine, so don't use loops because you'll break it /
be cheating ;-)
Robot script API
Per robot methods
Need to prefix these with this.
-
scan(degree, resolution)
The scan() method scans for enemy robots at the specified degree and resolution.
scan() returns false if no robots are within the scan line
or a positive integer representing the distance to the closest robot.
Degree should be within the range 0-359, otherwise degree is forced into 0-359 by a modulo 360 operation, and made positive if necessary.
Resolution controls the scanner's sensing resolution, up to +/- 10 degrees.
Examples:
var range = this.scan(45, 0); //Scan the 45 degree line, with no variance
var range = this.scan(365, 10); //Scans the lines from 355 degs to 15 degs
-
cannon(degree, range)
The cannon() method fires a missile heading in the specified range and direction.
cannon() returns true if a missile was fired, or false if the cannon is reloading.
Degree is forced into the range 0-359 as in scan().
Range can be 0-350, with greater ranges truncated to 350.
Examples:
var degree = 45; //Set a direction to test
if((var range = this.scan(degree, 2)) != false) //See if a target is there
{
this.cannon(degree, range); //Fire a missile
}
-
drive(degree, speed)
The drive() method activates the robot's drive mechanism, on the specified heading and speed.
Degree is forced into the range 0-359 as in scan().
Speed is expressed as a percent, with 100 as maximum.
A speed of 0 disengages the drive.
Changes in direction can be negotiated at speeds of less than 50 percent.
Examples:
this.drive(0, 100); //Head due north, at maximum speed
this.drive(90, 0); //Stop motion
-
damage()
The damage() method returns the current amount of damage incurred.
damage() takes no arguments, and returns the integer percent of damage, 0-99.
(100 percent damage means the robot is completely disabled, thus no longer running!)
Examples:
var d = this.damage(); //Save current state
; ; ; //Other instructions
if(d != this.damage()) //Compare current state to prior state
{
this.drive(90, 100); //Robot has been hit, start moving
d = this.damage(); //Get current damage again
}
-
speed()
The speed() method returns the current speed of the robot.
speed() takes no arguments, and returns the integer percent of speed, 0-100.
Note that speed() may not always be the same as the last drive(), because of acceleration and deacceleration.
Examples:
this.drive(270, 100); //Start drive, due west full speed
; ; ; //Other instructions
if(this.speed() == 0) //Check current speed
{
this.drive(90, 20); //Ran into the west wall, or another robot so backtrack east
}
-
loc_x() and loc_y()
The loc_x() method returns the robot's current x axis location.
loc_x() takes no arguments, and returns an integer in the range 0-499.
The loc_y() method is similar to loc_x(), but returns the current y axis position.
Examples:
if(this.loc_y() > 20)
{
this.drive(0, 50); //Start heading for north wall
}
else
{
this.drive(0, 0); //Stop drive
}
Utility functions
-
rand(limit)
Returns a random integer between zero and limit (exclusive).
-
sqrt(number)
Returns the square root of a number. Number is made positive if necessary.
-
sine(degree)
Returns the trigometric sine of degree (degree being an integer from zero to 359).
-
cosine(degree)
Returns the trigometric cosine of degree (degree being an integer from zero to 359).
-
tan(degree)
Returns the trigometric tangent of degree (degree being an integer from zero to 359).
-
atan(ratio)
Returns the integer degree value, between -90 and +90, of the ratio.
Damage table
Damage accumulates thusly:
Case | Damage |
Hit wall | 2 (and speed down to zero) |
Hit other robot | 2 each (and speed down to zero) |
Caught in explosion | 10 |
Usage notes
To remember things between iterations, save it to a member variable thusly:
this.somethingToRemember = whatever();
Robot object methods are documented (above) and internal housekeeping methods / members
are prefixed with a '_' [underscore]. Avoid these names in your scripts and you will be fine.
In your robot script, any and all JavaScript functions / objects / methods are available.
The static methods of the Math object might be especially useful.
Take a look at the JavaScript guide on DevGuru.
Be sure you don't, for example, alert() anything out though because it will end in tears!
You can define a reusable method for your robot thusly:
//Define
this.reusableMethod = function(speed)
{
this.whatever = something();
this.drive(90, speed);
};
this.scan(this.scanAngle, 5); //Other stuff here
//Use
this.reusableMethod(100);
A semicolon after the method definition is required.
Bugs
There's a pretty irritating bug which we call the quick getaway bug.
Basically, if your robot's speed is zero due to colliding with another robot, then
you need to drive() away as soon as possible in the code rather than waiting for the next iteration.
See the presets for examples.
Possibly related to the above, there's also the jitterbug bug.
A robot will occasionally jitter back and forth when in front of a wall or other robot.
Another irritating bug is that starting the game again after a stop or game win isn't perfect unfortunately.
Best to reload the URL [fixed!]
Release history
Version | Date | Notes |
1.0gh | 9 November 2011 | First GitHub open source version. As live JS Robots but packaged to run
locally from File --> Open in the browser (ie. no backend technologies and no sending scores to Twitter) |
Licence
This JS Robots software source code is released under the terms of the BSD "New BSD License".
Please see the LICENCE text file in the root folder of the source code.
v1.0ghCopyright © 2009 ~ 2011 Warp Asylum Ltd (UK)