Uploading Your Bot

The upload system performs a syntax check, but because of its integrative nature is not 100% safe to spot all errors.

You should ALWAYS make sure your bot module compiles fine with:

  perl -c Cosair/AI/Bot/

on your local machine. The "Cosair::AI" package you need for this can be downloaded from the AGD website.

If you get redefinition warnings, include the following line right below the warnings pragma:

  use warnings;
  no warnings 'redefine';

This is in the template since December 2010.

Defining State Machines

Expressive Strings like "offensive" or "defensive" are strongly recommended for your states.

The obvious solution is to save the state in the bot memory.

  $self->set_mem('state', 'offensive');

When checking states later, the switch control structure with given/when is very well suited.
You have to activate it once at the top in your source code:

  use feature "switch";

and later use:

  given ($self->mem('state')) {
      when ('offensive') { ... }
      when ('defensive') { ... }

See the section "Switch statements" in:

  perldoc perlsyn

Defining New Methods

When you want new methods for your bot, it is recommended to implement them as normal object methods.
Define your new method below the official ones:

  sub my_method_a {
      my $self = shift;
      my $param1 = shift;

Then you can call the method as usual from any other method:

  $self->my_method_a(param1, ...);

This gives you access to all $self and all objects, especially the Cosair::AI::Control in your own methods.

Storing Complex Data Structures in the Memory

In order to store complex nested data structures like arrays of hashes etc. use the standard Data::Dumper module.

For documentation see or

  perldoc Data::Dumper
  use Data::Dumper;

Searching Errors

If you get an error in a Cosair::AI method, but don't know from where it was called, you can test for it before calling the method.

For example, assume the Cosair::AI::Nation->nearest_planet_of_nation() method fails because it is called with a Cosair::AI::NmePlanet argument instead of a Cosair::AI::NmeNation argument.

Whenever calling this method in your code:

  $target = $own_nation->nearest_planet_of_nation($nme_nation);

you can add a check before:

  die "invalid argument" if ref $nme_nation ne "Cosair::AI::NmeNation";

and log more details later if you spotted the location of the error in your code.

For more explanations on this see:

  perldoc -f ref
last change: 2011-01-12 11:46:56 by Darko
Back to Start

Back to Main Page