View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.hideandseek.observer;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.Map.Entry;
8   
9   import com.google.inject.Inject;
10  
11  import cz.cuni.amis.pogamut.base.communication.command.IAct;
12  import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
13  import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
14  import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
15  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
16  import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
17  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ConfigurationObserver;
18  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.InitializeObserver;
19  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
20  import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
21  import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
22  
23  public class HSObserver extends UT2004Observer {
24  
25  	private boolean observing = false;
26  	
27  	private IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>> playerUpdatedListener = new IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>>() {
28  
29  		@Override
30  		public void notify(WorldObjectUpdatedEvent<Player> event) {
31  			playerUpdated(event);
32  		}
33  	};
34  	
35  	private Map<Player, Long> visibleSinceMillis = new HashMap<Player, Long>();
36  	
37  	@Inject
38  	public HSObserver(UT2004AgentParameters params, IComponentBus bus, IAgentLogger agentLogger, UT2004WorldView worldView, IAct act) {
39  		super(params, bus, agentLogger, worldView, act);
40  		if (!(params instanceof HSObserverParams)) throw new RuntimeException("HSObserver must be instantiated with HSObserverParams not " + params.getClass().getSimpleName() + ".");
41  		getLogger().addDefaultConsoleHandler();
42  	}	
43  		
44  	public HSObserverParams getParams() {
45  		return (HSObserverParams)super.getParams();
46  	};
47  	
48  	protected void startAgent() {
49  		super.startAgent();
50  		
51  		getWorldView().addObjectListener(Player.class, WorldObjectUpdatedEvent.class, playerUpdatedListener);
52  		
53  		configureObserver();
54  	}
55  		
56  	public void configureObserver() {
57  		// TELL WE WANT TO OBSERVE THE CONCRETE BOT
58  		getAct().act(new InitializeObserver().setId(getParams().getBotIdToObserve()));
59  		// CONFIGURE WHICH MESSAGES WE WANT TO RECEIVE
60  		getAct().act(
61  				new ConfigurationObserver()
62  					.setAll(true)      // WE WANT TO RECEIVE UPDATES	
63  					.setUpdate(0.21)   // UPDATE TIME						
64  					.setSelf(false)    // WE WANT TO RECEIVE "Self"
65  					.setAsync(false)   // export Async events (BotDamaged, PlayerDamaged, BotKilled, PlayerKilled, HearNoise, etc.)
66  					.setGame(false)    // Export GAME message
67  					.setSee(true)	   // NavPoints, Items, Players
68  					.setSpecial(false) // SPECIFIC GAME TYPE MESSAGES (BOM, FLG, DOM)
69  		);
70  		log.info("START OBSERVING: " + getParams().getBotIdToObserve());
71  	}
72  	
73  	protected void playerUpdated(WorldObjectUpdatedEvent<Player> event) {
74  		Player player = event.getObject();
75  				
76  		if (player.isVisible()) {
77  			// PLAYER IS/BECOMES VISIBLE
78  			synchronized(visibleSinceMillis) {
79  				if (visibleSinceMillis.containsKey(player)) {
80  					// PLAYER REMAINS VISIBLE, probably location/rotation/speed etc. change
81  					return;
82  				}
83  				// PLAYER BECOMES VISIBLE
84  				visibleSinceMillis.put(player, System.currentTimeMillis());
85  			}
86  		} else {
87  			synchronized(visibleSinceMillis) {
88  				// PLAYER NOT VISIBLE
89  				if (visibleSinceMillis.containsKey(player)) {
90  					// PLAYER VISIBILITY LOST
91  					visibleSinceMillis.remove(player);
92  					return;
93  				}
94  			}
95  		}
96  	}
97  	
98  	public List<Player> getPlayersVisibleMoreThanMillis(long millis) {		
99  		List<Player> result = new ArrayList<Player>(visibleSinceMillis.size());
100 		long currTime = System.currentTimeMillis();
101 		synchronized(visibleSinceMillis) {
102 			for (Entry<Player, Long> entry : visibleSinceMillis.entrySet()) {
103 				double visibleFor = currTime - entry.getValue(); 
104 				if (visibleFor > millis) {
105 					result.add(entry.getKey());
106 				}
107 			}
108 		}
109 		return result;
110 	}
111 		
112 }