Menu

Robocode fire help

Help
Irfon
2010-04-21
2012-09-15
  • Irfon

    Irfon - 2010-04-21

    Ok. I have to make a team which surrounds the enemy. and shoots at it when it
    surrounds the enemy. I have more or less got them to surround the enemy (some
    group together and shoot each other).for now I set it to fire when they get
    the message about the enemy, Is there a way to make them shoot only when they
    are a certain distance from the enemy?

     
  • Flemming N. Larsen

    Well, one thing you could do is to create your own Condition, i.e. create a
    class that inherits from Condition, which should return true when it fulfills
    your criteria, e.g. like this:

    class FireDistanceCondition extends Condition {
        public boolean test() {
            // Calculate distance to enemy robot, where (enemyX, enemyY) helds the coordinates of the enemy
            double dx = getX() - enemyX;
            double dy = getY() - enemyY;
            double distance = Math.sqrt(dx*dx + dy*dy);
    
            // Our condition is met if the distance < 200 and the gun is able to fire
            return distance < 200 && getGunHeat() == 0;
        }
    }
    

    In you run() method of your robot, you must add this line before your main
    loop (e.g. while(true)):

    addCustomEvent(new FireDistanceCondition);
    

    Next you should implement an event handler for your custom event:

    public void onCustomEvent(CustomEvent event) {
        if (event.getCondition() instanceof FireDistanceCondition) {
            setFire(1); // or alternatively use fire(1) instead
        }
    }
    
     
  • Flemming N. Larsen

    Sorry, the formatting for the last part went wrong. Here it is:

    ...

    Next you should implement an event handler for your custom event:

    public void onCustomEvent(CustomEvent event) {
        if (event.getCondition() instanceof FireDistanceCondition) {
            setFire(1); // or alternatively use fire(1) instead
        }
    }
    
     

Log in to post a comment.

MongoDB Logo MongoDB