[Pogamut-list] listener working example
michal.bida
pogamut-forum at artemis.ms.mff.cuni.cz
Wed Jun 17 14:39:07 CEST 2009
Re: listener working example
Author: michal.bida
This is working example of bot, that has defined two listeners - one for counting the frags and second for logging text messages (They will appear in user part of agent logs). The frag variable can be observed through introspection.
package simplebot;
import cz.cuni.pogamut.Client.Agent;
import java.util.logging.Level;
import java.util.logging.Level;
import cz.cuni.pogamut.Client.RcvMsgEvent;
import cz.cuni.pogamut.Client.RcvMsgListener;
import cz.cuni.pogamut.MessageObjects.*;
import cz.cuni.pogamut.introspection.PogProp;
/**
*/
public class Main extends Agent {
@PogProp boolean turn;
@PogProp boolean hitWall;
@PogProp boolean shooting;
@PogProp int frags = 0;
/** Creates a new instance of agent. */
public Main() {
super();
/**
* set level of logging - see logging documentation, now you will see only more relevant things
*/
this.log.setLevel(Level.INFO);
this.platformLog.setLevel(Level.INFO);
}
RcvMsgListener myListener = new RcvMsgListener() {
@Override
public void receiveMessage(RcvMsgEvent e) {
PlayerKilled pk;
pk = (PlayerKilled) e.getMessage();
if (pk.killerID == getMemory().getAgentID()) {
frags += 1;
}
}
};
RcvMsgListener myChatListener = new RcvMsgListener() {
@Override
public void receiveMessage(RcvMsgEvent e) {
GlobalChat gc;
gc = (GlobalChat) e.getMessage();
getLogger().info("Message: " + gc.string);
}
};
@Override
protected void postPrepareAgent() {
this.getBody().addTypedRcvMsgListener(myChatListener, MessageType.GLOBAL_CHAT);
this.getBody().addTypedRcvMsgListener(myListener, MessageType.PLAYER_KILLED);
}
/**
* Main method of the bot's brain - we're going to do some thinking about
* the situation we're in (how it's unfair to be the bot in the gloomy world
* of UT2004 :-).
*
* Check out the javadoc for this class - there you find a basic concept
* of this bot.
*/
@Override
protected void doLogic() {
Player player = memory.getSeePlayer();
NavPoint bod = memory.getSeeNavPoint();
hitWall = memory.isColliding();
if (hitWall) {
body.turnHorizontal(50);
try {
Thread.sleep(500);
} catch (Exception e) {
log.severe("Exception ...");
}
}
if (player != null) {
body.runToTarget(player);
body.shoot(player);
shooting = true;
} else if (bod != null) {
body.runToNavPoint(bod);
if (shooting) {
body.stopShoot();
shooting = false;
}
} else if (!memory.isMoving()) {
body.turnHorizontal(20);
}
}
/**
* NOTE: this method MUST REMAIN DEFINED + MUST REMAIN EMPTY, due to technical reasons.
*/
public static void main(String[] Args) {
}
}
More information about the Pogamut-list
mailing list