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