<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://robowiki.net/w/index.php?action=history&amp;feed=atom&amp;title=RandomMovementBot%2FCode</id>
	<title>RandomMovementBot/Code - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://robowiki.net/w/index.php?action=history&amp;feed=atom&amp;title=RandomMovementBot%2FCode"/>
	<link rel="alternate" type="text/html" href="http://robowiki.net/w/index.php?title=RandomMovementBot/Code&amp;action=history"/>
	<updated>2026-04-28T08:13:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.1</generator>
	<entry>
		<id>http://robowiki.net/w/index.php?title=RandomMovementBot/Code&amp;diff=51080&amp;oldid=prev</id>
		<title>MultiplyByZer0: Migrate from old RoboWiki</title>
		<link rel="alternate" type="text/html" href="http://robowiki.net/w/index.php?title=RandomMovementBot/Code&amp;diff=51080&amp;oldid=prev"/>
		<updated>2017-09-01T15:54:01Z</updated>

		<summary type="html">&lt;p&gt;Migrate from old RoboWiki&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Navbox small&lt;br /&gt;
| title  = RandomMovementBot Sub-pages&lt;br /&gt;
| parent = RandomMovementBot&lt;br /&gt;
| page1  = Code&lt;br /&gt;
| page2  = Archived Talk&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
// $Id: RandomMovementBot.java,v 1.2 2004/01/04 18:52:08 peter Exp $&lt;br /&gt;
package wiki.etc;&lt;br /&gt;
import robocode.*;&lt;br /&gt;
import robocode.util.Utils;&lt;br /&gt;
import java.awt.geom.*;&lt;br /&gt;
&lt;br /&gt;
// This code is released under the RoboWiki Public Code Licence (RWPCL), datailed on:&lt;br /&gt;
// http://robowiki.net/?RWPCL&lt;br /&gt;
//&lt;br /&gt;
// RandomMovementBot, by PEZ. Demonstrating a simple, extensible way to go about random 1v1 movement.&lt;br /&gt;
// Code and discussions about this bot available on: http://robowiki.net/?RandomMovementBot&lt;br /&gt;
&lt;br /&gt;
public class RandomMovementBot extends AdvancedRobot {&lt;br /&gt;
    static final double MAX_VELOCITY = 8;&lt;br /&gt;
    static final double WALL_MARGIN = 25;&lt;br /&gt;
    Point2D robotLocation;&lt;br /&gt;
    Point2D enemyLocation;&lt;br /&gt;
    double enemyDistance;&lt;br /&gt;
    double enemyAbsoluteBearing;&lt;br /&gt;
    double movementLateralAngle = 0.2;&lt;br /&gt;
&lt;br /&gt;
    public void run() {&lt;br /&gt;
        setAdjustRadarForGunTurn(true);&lt;br /&gt;
&lt;br /&gt;
        do {&lt;br /&gt;
            turnRadarRightRadians(Double.POSITIVE_INFINITY); &lt;br /&gt;
        } while (true);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void onScannedRobot(ScannedRobotEvent e) {&lt;br /&gt;
        robotLocation = new Point2D.Double(getX(), getY());&lt;br /&gt;
        enemyAbsoluteBearing = getHeadingRadians() + e.getBearingRadians();&lt;br /&gt;
        enemyDistance = e.getDistance();&lt;br /&gt;
        enemyLocation = vectorToLocation(enemyAbsoluteBearing, enemyDistance, robotLocation);&lt;br /&gt;
&lt;br /&gt;
	move();&lt;br /&gt;
&lt;br /&gt;
        setTurnRadarRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing - getRadarHeadingRadians()) * 2);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Always try to move a bit further away from the enemy.&lt;br /&gt;
    // Only when a wall forces us we will close in on the enemy. We never bounce off walls.&lt;br /&gt;
    void move() {&lt;br /&gt;
	considerChangingDirection();&lt;br /&gt;
	Point2D robotDestination = null;&lt;br /&gt;
	double tries = 0;&lt;br /&gt;
	do {&lt;br /&gt;
	    robotDestination = vectorToLocation(absoluteBearing(enemyLocation, robotLocation) + movementLateralAngle,&lt;br /&gt;
		    enemyDistance * (1.1 - tries / 100.0), enemyLocation);&lt;br /&gt;
	    tries++;&lt;br /&gt;
	} while (tries &amp;lt; 100 &amp;amp;&amp;amp; !fieldRectangle(WALL_MARGIN).contains(robotDestination));&lt;br /&gt;
	goTo(robotDestination);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void considerChangingDirection() {&lt;br /&gt;
	// Change lateral direction at random&lt;br /&gt;
	// Tweak this to go for flat movement&lt;br /&gt;
	double flattenerFactor = 0.05;&lt;br /&gt;
	if (Math.random() &amp;lt; flattenerFactor) {&lt;br /&gt;
	    movementLateralAngle *= -1;&lt;br /&gt;
	}&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    RoundRectangle2D fieldRectangle(double margin) {&lt;br /&gt;
        return new RoundRectangle2D.Double(margin, margin,&lt;br /&gt;
	    getBattleFieldWidth() - margin * 2, getBattleFieldHeight() - margin * 2, 75, 75);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void goTo(Point2D destination) {&lt;br /&gt;
        double angle = Utils.normalRelativeAngle(absoluteBearing(robotLocation, destination) - getHeadingRadians());&lt;br /&gt;
	double turnAngle = Math.atan(Math.tan(angle));&lt;br /&gt;
        setTurnRightRadians(turnAngle);&lt;br /&gt;
        setAhead(robotLocation.distance(destination) * (angle == turnAngle ? 1 : -1));&lt;br /&gt;
	// Hit the brake pedal hard if we need to turn sharply&lt;br /&gt;
	setMaxVelocity(Math.abs(getTurnRemaining()) &amp;gt; 33 ? 0 : MAX_VELOCITY);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static Point2D vectorToLocation(double angle, double length, Point2D sourceLocation) {&lt;br /&gt;
	return vectorToLocation(angle, length, sourceLocation, new Point2D.Double());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static Point2D vectorToLocation(double angle, double length, Point2D sourceLocation, Point2D targetLocation) {&lt;br /&gt;
        targetLocation.setLocation(sourceLocation.getX() + Math.sin(angle) * length,&lt;br /&gt;
            sourceLocation.getY() + Math.cos(angle) * length);&lt;br /&gt;
	return targetLocation;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static double absoluteBearing(Point2D source, Point2D target) {&lt;br /&gt;
        return Math.atan2(target.getX() - source.getX(), target.getY() - source.getY());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Source Code]]&lt;/div&gt;</summary>
		<author><name>MultiplyByZer0</name></author>
		
	</entry>
</feed>