View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.logic;
2   
3   import java.util.logging.Level;
4   import java.util.logging.Logger;
5   
6   import com.google.inject.Inject;
7   
8   import cz.cuni.amis.pogamut.base.agent.module.IAgentLogic;
9   import cz.cuni.amis.pogamut.base.communication.command.ICommandListener;
10  import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
11  import cz.cuni.amis.pogamut.base.communication.worldview.react.EventReact;
12  import cz.cuni.amis.pogamut.base.communication.worldview.react.ObjectEventReact;
13  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
14  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencyType;
15  import cz.cuni.amis.pogamut.base3d.ILockableVisionWorldView;
16  import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
17  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Respawn;
18  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ConfigChange;
19  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.EndMessage;
20  
21  public class SyncUT2004BotLogic<BOT extends UT2004Bot<? extends ILockableVisionWorldView, ?, ?>> extends UT2004BotLogic<BOT> {
22  
23  	private ObjectEventReact<ConfigChange, ?> configChangeReaction;
24  	
25  	private EventReact<EndMessage>        endReactionAfterRespawn;
26  	private int						      shouldExecuteLogicLatch = 0;
27  	
28  	private ICommandListener<Respawn> respawnListener = new ICommandListener<Respawn>() {		
29  
30  		@Override
31  		public void notify(Respawn event) {			
32  			synchronized(respawnListener) {
33  				endReactionAfterRespawn.enable();
34  				shouldExecuteLogicLatch = 2;
35  			}
36  		}
37  		
38  	};
39  	
40  	@Inject
41  	public SyncUT2004BotLogic(BOT agent, IAgentLogic logic) {
42  		this(agent, logic, null, new ComponentDependencies(ComponentDependencyType.STARTS_AFTER).add(agent.getWorldView()));
43  	}
44  	
45  	public SyncUT2004BotLogic(BOT agent, IAgentLogic logic, Logger log) {
46  		this(agent, logic, log, new ComponentDependencies(ComponentDependencyType.STARTS_AFTER).add(agent.getWorldView()));
47  	}
48  	
49  	public SyncUT2004BotLogic(BOT agent, IAgentLogic logic, Logger log, ComponentDependencies dependencies) {
50  		super(agent, logic, log, dependencies);
51  		endReactionAfterRespawn = new EventReact<EndMessage>(EndMessage.class, agent.getWorldView()) {
52  			@Override
53  			protected void react(EndMessage event) {
54  				synchronized(respawnListener) {
55  					if (shouldExecuteLogicLatch > 0) {
56  						--shouldExecuteLogicLatch;
57  					}
58  				}
59  			}
60  		};
61  		agent.getAct().addCommandListener(Respawn.class, respawnListener);
62  		
63  		configChangeReaction = new ObjectEventReact<ConfigChange, IWorldObjectEvent<ConfigChange>>(ConfigChange.class, agent.getWorldView()) {
64  			@Override
65  			protected void react(IWorldObjectEvent<ConfigChange> event) {
66  				setLogicFrequency(1 / (Math.max(0.05, event.getObject().getVisionTime() - 0.049)));
67  			}
68  		};
69  	}
70  	
71  	@Override
72  	protected void beforeLogic(String threadName) {
73  		super.beforeLogic(threadName);
74  		if (log.isLoggable(Level.FINEST)) log.finest(threadName + ": Locking world view.");
75  		agent.getWorldView().lock();
76  		if (log.isLoggable(Level.FINER)) log.finer(threadName + ": World view locked.");
77  	}
78  	
79  	@Override
80  	protected void afterLogic(String threadName) {
81  		super.afterLogic(threadName);
82  		if (log.isLoggable(Level.FINEST)) log.finest(threadName + ": Unlocking world view.");
83  		agent.getWorldView().unlock();
84  		if (log.isLoggable(Level.FINER)) log.finer(threadName + ": World view unlocked.");
85  	}
86  	
87  	@Override
88  	protected void afterLogicException(String threadName, Throwable e) {
89  		super.afterLogicException(threadName, e);
90  		if (agent.getWorldView().isLocked()) {
91  			if (log.isLoggable(Level.FINEST)) log.finest("Unlocking world view.");
92  			agent.getWorldView().unlock();
93  			if (log.isLoggable(Level.FINER)) log.finer("World view unlocked.");
94  		}
95  	}
96  	
97  	@Override
98  	protected boolean shouldExecuteLogic() {
99  		synchronized(respawnListener) {
100 			if (shouldExecuteLogicLatch != 0) {
101 				if (log.isLoggable(Level.INFO)) log.info("Respawn command sensed - waiting for the bot respawn to execute logic with correct world view state.");
102 				return false;
103 			} else {
104 				return true;
105 			}
106 		}			
107 	}
108 	
109 }