[Home]CustomEvents

Robo Home | Changes | Preferences | AllPages

Showing revision 13
Robocode lets you define your own events and can help in dispatching event calls to your robot. To create a customized event you make a class that sublasses Condition. The class must define an instance method named "public boolean test()". This method is called by Robocode every turn for all registrered events. The class might look like so:
class Mine extends Condition {
   private AdvancedRobot bot;
   private double x;
   private int y;

   Mine(AdvancedRobot _bot, double _x, int _y) {
      this.bot = _bot;
      this. = _x;
      this.b = _y;
   }

   public boolean test() {
      if (Math.abs(Point2D.distance(bot.getX(), bot.getY(), x, y)) < 50) {
         bot.removeCustomEvent(this);
         return true;
      }
      return false;
   }

   public getCoordinates() {
      return new Point2D.Double(x, y);
   }
}
If you registrer custom events that have a best-before date you must make sure you inregistrer the event. In the above example this is done when the test() method meets the true condition.

To place a mine you register a custom event from inside your bot code:

public void run() {
  registerCustomEvent(new Mine(this, 300, 300));
}

To check for if you have run into a mine you create a custom event handler:

public void onCustomEvent(CustomEvent event) {
   Condition condition = event.getCondition();
   if (condition instanceof Mine) {
      System.out.println("Boom!!!! Coordinates: " + ((Mine)condition).getCoordinates());
   }
}
Check out VirtualBullets/VirtualBulletsBot for an example on how custom events can be used to create Waves. And fire those questions you might have on the above information. --PEZ

i have used custom events in a couple of my bots,but nothing like the kind i see PEZ using in VirtualBullets/VirtualBulletsBot and Falcon/WithoutShrinkingTricks. i understand that using them gets rid of the loop that goes through all of the virtualbullets/waves and checks to see if they hit/pass the target. after looking at this code, i don't see how it actually does that. anyone care to enlighten me?--andrew

Yes, it's quite easy actually. Robocode has that loop built in already. It loops through all registered events and runs their test() method. So by registering the Wave as a custom event you know that each turn Robocode will run its test() method. Put your VisitCountStats update inside the test() method or in your onCustomEvent() method, whichever you think is clearer. Aristocles does the former, Tityus does the latter. (At least 0.9.1 did, 0.9.6 is a failed attempt to make an inproved Aristocles. Look at the source code of those bots too if you like. -- PEZ

Good coding practice would dictate you do it in the event handler. No actions should be taken in a test method. Unless you are trying to make a small bot, and it's smaller in the test method, do it properly. -- Tango

The funny thing is that in Tityus it's smaller to do it in the event handler. But in Aristocles it's the other way around. CodeSize management is voodoo. Damn cool voodoo, but still voodoo. -- PEZ


Robo Home | Changes | Preferences | AllPages
Edit revision 13 of this page | View other revisions | View current revision
Edited May 18, 2006 23:45 EST by Florent (diff)
Search: