[Home]OnDeath

Robo Home | Changes | Preferences | AllPages

Proper handling of onDeath()

First, a test bot:

package davidalves.debug;

import robocode.AdvancedRobot;
import robocode.DeathEvent;
import robocode.HitByBulletEvent;

public class OnDeathTestBot extends AdvancedRobot {
	
	double totalDamageThisRound;
	
	public void run(){
		totalDamageThisRound = 0;
	}
	
	public void onHitByBullet(HitByBulletEvent e){
		double bulletPower = e.getBullet().getPower();
		double damage = 4.0 * bulletPower + (bulletPower > 1 ? 2 * bulletPower - 2 : 0);
		totalDamageThisRound += damage;
		out.println("onHitByBullet() called");
		out.println("> I was hit by a power " + e.getBullet().getPower() + " bullet at time " + e.getTime());
		out.println("> total damage this round: " + totalDamageThisRound);
	}
	
	public void onDeath(DeathEvent e){
		out.println("onDeath() called");
		out.println("> total damage this round: " + totalDamageThisRound);
	}
}

And some sample output from a match against sample.TrackFire:

=========================
Round 1 of 1
=========================
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 65
> total damage this round: 16.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 82
> total damage this round: 32.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 99
> total damage this round: 48.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 116
> total damage this round: 64.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 133
> total damage this round: 80.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 150
> total damage this round: 96.0
onDeath() called
> total damage this round: 96.0
SYSTEM: davidalves.debug.OnDeathTestBot has died

As you can see, the robot doesn't receive the HitByBulletEvent? for the bullet that killed it. In fact, it doesn't get any events for that turn, except for the DeathEvent?. DeathEvents? have a permanent higher priority than all other events, too, so you can't use Event.setPriority() to change the order they arrive in. So how can we get those missing events? Why, with getAllEvents?(), of course!

New onDeath() method:

public void onDeath(DeathEvent e){
	out.println("onDeath() called");
	out.println("> total damage this round: " + totalDamageThisRound);

	Vector v = getAllEvents();
	Iterator i = v.iterator();
	while(i.hasNext()){
		Object obj = i.next();
		if(obj instanceof HitByBulletEvent) onHitByBullet((HitByBulletEvent) obj);
	}
}
and you get:
=========================
Round 1 of 1
=========================
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 72
> total damage this round: 16.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 89
> total damage this round: 32.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 106
> total damage this round: 48.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 123
> total damage this round: 64.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 140
> total damage this round: 80.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 157
> total damage this round: 96.0
onDeath() called
> total damage this round: 96.0
onHitByBullet() called
> I was hit by a power 3.0 bullet at time 174
> total damage this round: 112.0
SYSTEM: davidalves.debug.OnDeathTestBot has died

Voila! No more missed events. =) This trick was first introduced in Phoenix 0.31. --David Alves


Comments

Q: Does the same apply to the onWin() event? I mean, when you win, do you miss the last BulletHit?() event. -- GrubbmGait

A: No. They are both highest priority (100) events, so they are processed first, but when you win you aren't dead before the others are processed. =) -- Voidious

I've never dealt with manipulating the priority levels of events, but itsn't this a prime example of a good time to set events to a lower priority? -- Martin

It would be, but IIRC, you can't change onDeath and onWin from the highest priority for some reason. -- Voidious

That's correct. --David Alves

And you can't set anything to priority 100. --Starrynte


Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited December 28, 2006 21:06 EST by Starrynte (diff)
Search: