[Pogamut-list] Controlling Bot
jakub.gemrot
pogamut-forum at diana.ms.mff.cuni.cz
Tue Dec 21 21:13:17 CET 2010
Re: Controlling Bot
Author: jakub.gemrot
Hi!
Actually, I've created a test case that is a bit complex, but I will try to cut it down to important parts... so, here is my observer code:
{CODE()}
package cz.cuni.amis.pogamut.ut2004.observer.impl;
import java.util.concurrent.TimeUnit;
import com.google.inject.Inject;
import cz.cuni.amis.pogamut.base.agent.module.IAgentLogic;
import cz.cuni.amis.pogamut.base.agent.module.LogicModule;
import cz.cuni.amis.pogamut.base.communication.command.IAct;
import cz.cuni.amis.pogamut.base.communication.messages.CommandMessage;
import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectListener;
import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
import cz.cuni.amis.pogamut.base.component.bus.event.BusAwareCountDownLatch;
import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ConfigurationObserver;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.InitializeObserver;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BeginMessage;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BotDamaged;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.EndMessage;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Item;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.PlayerScore;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
import cz.cuni.amis.utils.exception.PogamutException;
public class TestUT2004BotObserver extends UT2004Observer {
LogicModule logicModule;
@Inject
public TestUT2004BotObserver(UT2004AgentParameters params,
IComponentBus bus, IAgentLogger agentLogger,
UT2004WorldView worldView, IAct act) {
super(params, bus, agentLogger, worldView, act);
// HOOK LISTENERS
getWorldView().addObjectListener(GameInfo.class, gameInfoListener);
getWorldView().addObjectListener(Self.class, selfListener);
getWorldView().addObjectListener(NavPoint.class, navPointListener);
getWorldView().addObjectListener(Item.class, itemListener);
getWorldView().addObjectListener(Player.class, playerListener);
getWorldView().addEventListener(BotDamaged.class, botDamagedListener);
getWorldView().addEventListener(BeginMessage.class, beginMessageListener);
getWorldView().addEventListener(EndMessage.class, endMessageListener);
getWorldView().addEventListener(PlayerScore.class, playerScoreListener);
}
private boolean gameInfoReceived = false;
private IWorldObjectListener gameInfoListener = new IWorldObjectListener() {
@Override
public void notify(IWorldObjectEvent event) {
gameInfoReceived = true;
}
};
protected boolean selfReceived = false;
private IWorldObjectListener selfListener = new IWorldObjectListener() {
@Override
public void notify(IWorldObjectEvent event) {
selfReceived = true;
}
};
protected boolean navPointReceived = false;
private IWorldObjectListener navPointListener = new IWorldObjectListener() {
@Override
public void notify(IWorldObjectEvent event) {
navPointReceived = true;
}
};
private boolean itemReceived = false;
private IWorldObjectListener itemListener = new IWorldObjectListener() {
@Override
public void notify(IWorldObjectEvent event) {
itemReceived = true;
}
};
protected boolean playerReceived = false;
private IWorldObjectListener playerListener = new IWorldObjectListener() {
@Override
public void notify(IWorldObjectEvent event) {
playerReceived = true;
}
};
protected boolean botDamagedReceived = false;;
private IWorldEventListener botDamagedListener = new IWorldEventListener() {
@Override
public void notify(BotDamaged event) {
botDamagedReceived = true;
}
};
protected boolean beginMessageReceived = false;;
private IWorldEventListener beginMessageListener = new IWorldEventListener() {
@Override
public void notify(BeginMessage event) {
beginMessageReceived = true;
}
};
protected boolean endMessageReceived = false;
private IWorldEventListener endMessageListener = new IWorldEventListener() {
@Override
public void notify(EndMessage event) {
endMessageReceived = true;
}
};
/**
* Sends custom command to the GameBots.
*
* Shortcut for getAct().act(command).
*
* @param command
*/
public void act(CommandMessage command) {
getAct().act(command);
}
// call after observer is starting
public void startObserving(String observedBotId) {
// initialize observer
act(new InitializeObserver().setId(getParams().getObservedBotId()));
act(new ConfigurationObserver().setUpdate(0.2).setAll(true).setAsync(true).setGame(true).setSee(true).setSpecial(true));
}
//// after some time, call
/**
* This method will check which messages we have received from observed bot...
*/
public boolean testMessages() {
StringBuffer sb = new StringBuffer();
sb.append("ERRORS ");
if (!gameInfoReceived) {
sb.append("| GAME INFO NOT RECEIVED");
}
if (!selfReceived) {
sb.append("| SELF NOT RECEIVED");
}
if (!navPointReceived) {
sb.append("| NAVPOINT NOT RECEIVED");
}
if (!itemReceived) {
sb.append("| ITEMS NOT RECEIVED");
}
if (!playerReceived) {
sb.append("| PLAYER NOT RECEIVED");
}
if (!botDamagedReceived) {
sb.append("| BOT DAMAGED NOT RECEIVED");
}
if (!beginMessageReceived) {
sb.append("| BEGIN MESSAGE NOT RECEIVED");
}
if (!endMessageReceived) {
sb.append("| END MESSAGE NOT RECEIVED");
}
if (!sb.toString().equals("ERRORS: ")) {
user.severe("!!!!! " + sb.toString());
throw new PogamutException(sb.toString(), this);
}
return true;
}
}
{CODE
Well it's not the code I'm using (I've rewritten it), but fundamental parts are there.
As you can see - nothing special.
Are you sure, that you're hurting the bot? (Probably silly question...)
Cheers,
Jimmy
--
Reply Link: <http://diana.ms.mff.cuni.cz/main/tiki-view_forum_thread.php?forumId=4&comments_reply_threadId=4&comments_parentId=446&post_reply=1#form>
More information about the Pogamut-list
mailing list