Hans Kellner Dot Com :: a mighty fine website!

Hans Kellner Dot Com

help help


SideTracker Robot!

SideTracker :: Source Code


Here is the source code for CRobots and C++Robots versions of my SideTracker robot. This was a fun little diversion ages ago. This little robot did rather well back then. Very simple technique.


CRobots :: SideTrac.r

/* ****************************************************************** */
/*                                                                    */
/*      Name     : SIDETRAC                                           */
/*      Strategy : Kick butt on all other robots by moving fast,      */
/*                 firing accurately.                                 */
/*      Creator  : Hans D. Kellner  -  1987     Phoenix, AZ           */
/*                                                                    */

int drive_angle;                 /* Angle to travel */
int scan_angle;                  /* Angle to scan other robots */

main()
{
    drive_angle = 0;
    scan_angle  = 0;

    while(1) {                   /* Loop until I win! */
        move_robot();            /* Go get some robots */
        scan_field();            /* Look for any and blast em! */
    }

}

move_robot()
{
    if ( speed() < 50 ) {          /* If speed is below 50 we can turn.  */
                                   /* Therefore, change angle by 90 dgrs */
        drive_angle = ( drive_angle + 90 ) % 360;
        drive ( drive_angle,99 );  /* then start motors back up to full! */
    }
                                   /* Watch out for walls */
    if ( drive_angle == 0 && loc_x() > 700 )        /* Watch for right edge */
        drive ( drive_angle,40 );                   /* Slow down for turn   */
    else if ( drive_angle == 180 && loc_x() < 300 ) /* At left edge?        */
        drive ( drive_angle,40 );                   /* All motors slow!     */
    else if ( drive_angle == 90 && loc_y() > 700 )  /* At top wall?         */
        drive ( drive_angle,40 );                   /* Hit the brakes!      */
    else if ( drive_angle == 270 && loc_y() < 300 ) /* At the bottom edge?  */
        drive ( drive_angle,40 );                   /* Whoaoaoaoa!          */

}

scan_field()
{
    int range;                           /* Distance to other robot */
    int range2;

    range = scan ( scan_angle,10 );                  /* Look for a meal!   */
    if ( range < 700 && range > 0 ) {                /* Is it in range?    */
        range2 = scan( scan_angle - 5,10 );          /* Scan in closer...  */
        if ( range2 < 700 && range2 > 0 ) {          /* In range on right? */
            cannon( scan_angle - 5, 2*(range2 - range) + range); /* Then shoot */
            scan_angle = (scan_angle - 10) % 360;    /* Track the meal!    */
        }
        else {
            cannon ( scan_angle + 5, 2*(range2 - range) + range);
            scan_angle = (scan_angle + 10) % 360;    /* Track the meal!    */
        }
    }
    else                          /* No meals in sight so rotate angle 20 */
        scan_angle = ( scan_angle + 20 ) % 360;

}

          /*  Thats all folks!  Short, sweet, DEADLY!  */
	

C++Robots :: SideTrac.c

//
//      Name     : SIDETRACKER2
//
//      Strategy : Kick butt on all other robots by moving fast,
//                 firing accurately.
//
//      Creator  : Hans Kellner - hansk@netcom.com
//
//      Version  : 1.0 - initial 1987
//               : 2.1 - modified for C++Robots - October 1994
//

#include "robots.h"

main()
{
    int driveAngle;
    int quickAngle;
    int quickScan;
    int quickRange;
    int attackRange;
    int attackAngle;
    int startTurn;

    driveAngle = 0;
    drive( driveAngle, 100 );

    quickScan  = 1;
    quickAngle = 0;

    while ( 1 )
    {
        startTurn = 0;

        if ( driveAngle == 0 )
        {
            if ( loc_x() > 8000 )
                startTurn = 1;
        }
        else
        if ( driveAngle == 90 )
        {
            if ( loc_y() > 8000 )
                startTurn = 1;
        }
        else
        if ( driveAngle == 180 )
        {
            if ( loc_x() < 2000 )
                startTurn = 1;
        }
        else
        if ( driveAngle == 270 )
        {
            if ( loc_y() < 2000 )
                startTurn = 1;
        }

        if ( startTurn )
        {
            drive( driveAngle, 50 );

            driveAngle = ( driveAngle + 90 ) % 360;

            while ( speed() > 50 )
                drive( driveAngle, 50 );

            drive( driveAngle, 100 );
        }

        if ( quickScan )
        {        
            quickRange = scan( quickAngle, 10 );

            if ( quickRange > 0 && quickRange <= 7000 )
            {
                if ( quickRange > 200 )
                    cannon( quickAngle, quickRange );

                quickScan = 0;

                attackAngle = quickAngle;
            }
            else
            {
                quickAngle = ( quickAngle + 20 ) % 360;
            }
        }
        else
        {
            if ( attackRange = scan( attackAngle, 5 ) )
            {
                if ( attackRange > 30 )
                {
                    cannon( attackAngle, attackRange );
                }
            }
            else
            if ( attackRange = scan( attackAngle + 10, 5 ) )
            {
                attackAngle += 10;

                if ( attackRange > 30 )
                {
                    cannon( attackAngle + 4, attackRange );
                }
            }
            else    
            if ( attackRange = scan( attackAngle - 10, 5 ) )
            {
                attackAngle -= 10;

                if ( attackRange > 30 )
                {
                    cannon( attackAngle - 4, attackRange );
                }
            }
            else
            {
                quickScan = 1;
            }
        }
    }
}
	

Copyright © 1999-2010 Hans Kellner. All rights reserved.