[Pogamut-list] Custom Player observer in 3.1

michal.bida pogamut-forum at diana.ms.mff.cuni.cz
Tue Jan 11 23:17:07 CET 2011


Custom Player observer in 3.1

Author: michal.bida

Hello!

Here I will show you, how to create basic player observer in Pogamut 3.1. Note that Observer class in Pogamut is a stub and does not provide much help for the programmer, but listeners work and you can send commands to the server, so basically it is fully functional.

Start by creating normal Pogamut Java Bot through __New Project__ in NetBeans.

Then add following two classes to the package:

__PogamutObserver__

{CODE()}
package pogamutobserver;

import com.google.inject.Inject;
import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
import cz.cuni.amis.pogamut.base.communication.command.IAct;
import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Initialize;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverFactory;

import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
import cz.cuni.amis.utils.exception.PogamutException;


/**
 * Custom basic observer class.
 * 
 * @author knight
 */
public class PogamutObserver extends UT2004Observer {

    /**
     * Listener for players - we will receive here all players on the server in handshake
     * and then all the players our observed player/bot can see
     */
    IWorldObjectEventListener playerListener = new IWorldObjectEventListener() {
        public void notify(WorldObjectUpdatedEvent event) {
            //System.out.println(event.getClass().getCanonicalName());
            System.out.println(event.getObject().toString());
        }
    };

    /**
     * Self message of our observed player/bot.
     */
    IWorldObjectEventListener selfListener = new IWorldObjectEventListener() {
        public void notify(WorldObjectUpdatedEvent event) {
            System.out.println(event.getObject().toString());
        }
    };

    @Inject
    public PogamutObserver(UT2004AgentParameters params, IComponentBus bus, IAgentLogger agentLogger, UT2004WorldView worldView, IAct act) {
        super(params, bus, agentLogger, worldView, act);

    }

    /**
     * We call this method manually - we can hook to observe some player in here
     * and register listeners.
     */
    public void initialize() {
        //we send init command manually - we will observe player named Shadow, if
        //he won't be on the server nothing happens - but we won't receive any messages
        getAct().act(new Initialize().setName("Shadow"));

        getWorldView().addObjectListener(Player.class, WorldObjectUpdatedEvent.class, playerListener);
        getWorldView().addObjectListener(Self.class, WorldObjectUpdatedEvent.class, selfListener);

        System.out.println("!!!!!!! Initialize");
    }
    
    /**
     * This method is called when the bot is started either from IDE or from command line.
     * It connects the bot to the game server.
     * @param args
     */
    public static void main(String args[]) throws PogamutException {

        //creating agent parameters - setting module name and connection adress
        UT2004AgentParameters params = new UT2004AgentParameters();
        params.setAgentId(new AgentId("PogamutObserver"));
        params.setWorldAddress(new SocketConnectionAddress("127.0.0.1", 3002));

        //create module that tells guice it should instantiate OUR (this) class
        CustomPogamutObserverModule module = new CustomPogamutObserverModule();

        //creating pogamut factory
        UT2004ObserverFactory fac = new UT2004ObserverFactory(module);       
        PogamutObserver obs = (PogamutObserver) fac.newAgent(params);
        
        //starting the connection - connecting to the server
        obs.start();
        //launching our custom method
        obs.initialize();
    }
}
{CODE}
and

__CustomPogamutObserverModule__ 
(CustomPogamutObserverModule is important because it is telling guice to instantiate our Observer class and not default UT2004Observer.)

{CODE()}
package pogamutobserver;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

import cz.cuni.amis.pogamut.base.agent.IAgent;
import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
import cz.cuni.amis.pogamut.ut2004.communication.translator.observer.ObserverFSM;
import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
import cz.cuni.amis.pogamut.ut2004.factory.guice.remoteagent.UT2004ObserverModule;
import cz.cuni.amis.pogamut.ut2004.observer.IUT2004Observer;

public class CustomPogamutObserverModule extends UT2004ObserverModule {

	@Override
	protected void configureModules() {
		super.configureModules();
		addModule(new AbstractModule() {

			@Override
			public void configure() {
				bind(IWorldMessageTranslator.class).to(ObserverFSM.class);
				bind(IWorldView.class).to(IVisionWorldView.class);
				bind(IVisionWorldView.class).to(UT2004WorldView.class);
				bind(ComponentDependencies.class).annotatedWith(Names.named(UT2004WorldView.WORLDVIEW_DEPENDENCY)).toProvider(worldViewDependenciesProvider);
				bind(IAgent.class).to(IUT2004Observer.class);
				bind(IUT2004Observer.class).to(PogamutObserver.class); //THIS tells guice it should instantiate our class and not default one
			}

		});
	}

}
{CODE}

Run observer by running PogamutObserver class. It will connect by default to observer port 3002 and start observing player named Shadow. 
You can choose to observe some different player by reacting in playerListener listener to Player objects that you will receive in the handshake. But don't forget to erase the line trying to observer player Shadow from Initialize() method.

Best,
Michal

-- 
Reply Link: <http://diana.ms.mff.cuni.cz/main/tiki-view_forum_thread.php?forumId=4&comments_parentId=532#form>





More information about the Pogamut-list mailing list