View Javadoc

1   package cz.cuni.amis.pogamut.defcon.agent;
2   
3   import java.util.Map;
4   
5   import com.google.inject.Inject;
6   
7   import cz.cuni.amis.pogamut.base.agent.IAgentId;
8   import cz.cuni.amis.pogamut.base.agent.impl.AbstractEmbodiedAgent;
9   import cz.cuni.amis.pogamut.base.communication.command.impl.IJNIAct;
10  import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
11  import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
12  import cz.cuni.amis.pogamut.base.component.bus.event.BusAwareCountDownLatch;
13  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
14  import cz.cuni.amis.pogamut.defcon.communication.command.impl.DefConAct;
15  import cz.cuni.amis.pogamut.defcon.communication.messages.commands.DefConCommand;
16  import cz.cuni.amis.pogamut.defcon.communication.messages.infos.GameRunningChanged;
17  import cz.cuni.amis.pogamut.defcon.communication.worldview.DefConWorldView;
18  import cz.cuni.amis.utils.NullCheck;
19  
20  /**
21   * Basic defcon agent implementation.
22   * @author Radek 'Black_Hand' Pibil
23   *
24   * @param <CONTROLLER>
25   */
26  public class DefConAgent<CONTROLLER extends IDefConAgentLogicController<DefConAgent<?>, ?>>
27  	extends AbstractEmbodiedAgent<DefConWorldView, IJNIAct>
28  	implements IDefConAgent {
29  	
30  	private CONTROLLER controller;
31  
32  	private Map<String, String> arguments;
33  
34  	/**
35  	 * Latch that is raised when {@link InitedMessage} comes.
36  	 */
37  	private BusAwareCountDownLatch initedLatch;
38  	
39  	private boolean gameStarted = false;
40  	
41  	private boolean botStoppedCalled;
42  	
43  	protected DefConWorldView worldview;
44  
45  	private IWorldEventListener<GameRunningChanged> startListener = new IWorldEventListener<GameRunningChanged>() {
46  
47  		@Override
48  		public void notify(GameRunningChanged event) {
49  			if (!event.getRunning())
50  				gameStarted();
51  		}
52  		
53  	};
54  	
55  	/**
56  	 * Inits a DefConAgent instance
57  	 * 
58  	 * @param agentId
59  	 * @param eventBus
60  	 * @param logger
61  	 * @param worldView
62  	 * @param act
63  	 * @param logic
64  	 */
65  	@Inject
66  	@SuppressWarnings("unchecked")
67  	public DefConAgent(IAgentId agentId, IComponentBus eventBus, IAgentLogger logger,
68  			DefConWorldView worldView, DefConAct act,
69  			IDefConAgentLogicController logic) {
70  		super(agentId, eventBus, logger, worldView, act);
71  		
72  		controller = (CONTROLLER) logic;
73          NullCheck.check(this.controller, "init");
74          logger.addToAllCategories(new DefConLogPublisher());
75          log.finer("Initializing bot controller...");
76          this.controller.initializeController(this);
77          log.fine("Controller initialized.");
78          worldView.addEventListener(GameRunningChanged.class, startListener);
79  		this.worldview = worldView;
80  	}
81  	
82  	/**
83  	 * Returns a set of arguments passed to defcon.
84  	 */
85  	@Override
86  	public Map<String, String> getArguments() {
87  		return arguments;
88  	}
89  
90  	/**
91  	 * Used by PogamutJBotSupport to assign arguments.
92  	 */
93  	@Override
94  	public void setOptions(Map<String, String> arguments) {
95  		this.arguments = arguments;
96  	}
97  
98  	@Override
99  	public DefConWorldView getWorldView() {
100 		return (DefConWorldView) super.getWorldView();
101 	}
102 	
103     ////////  
104     //
105     // BOT CONTROL METHODS
106     //
107     ////////
108     
109     @Override
110 	protected void startAgent() {
111     	botStoppedCalled = false;
112     	super.startAgent();
113     }
114     
115     @Override
116     protected void stopAgent() {
117     	try {
118 	    	if (botStoppedCalled) {
119 	    		controller.botStopped();
120 	    		botStoppedCalled = true;
121 	    	}
122     	} finally {
123     		super.stopAgent();
124     	}
125     }
126     
127     @Override
128     protected void killAgent() {}
129     	
130 	protected void gameStarted() {
131 		gameStarted = true;
132 	}    
133 
134     protected boolean gameHasStarted() {
135     	return gameStarted;
136     }
137     
138     @Override
139     public void act(DefConCommand command) {
140     	getAct().act(command);
141     }
142 }