View Javadoc

1   package cz.cuni.amis.pogamut.ut2004multi.communication.worldview;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.logging.Level;
6   
7   import cz.cuni.amis.pogamut.base.communication.translator.event.IWorldChangeEvent;
8   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObject;
9   import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
10  import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
11  import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
12  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
13  import cz.cuni.amis.pogamut.base.component.lifecyclebus.ILifecycleBus;
14  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
15  import cz.cuni.amis.pogamut.base3d.worldview.object.IViewable;
16  import cz.cuni.amis.pogamut.base3d.worldview.object.event.WorldObjectDisappearedEvent;
17  import cz.cuni.amis.pogamut.multi.agent.ITeamedAgentId;
18  import cz.cuni.amis.pogamut.multi.communication.messages.SharedBatchFinishedEvent;
19  import cz.cuni.amis.pogamut.multi.communication.translator.event.ICompositeWorldObjectUpdatedEvent;
20  import cz.cuni.amis.pogamut.multi.communication.translator.event.ILocalWorldObjectUpdatedEvent;
21  import cz.cuni.amis.pogamut.multi.communication.worldview.ISharedWorldView;
22  import cz.cuni.amis.pogamut.multi.communication.worldview.impl.BatchAwareLocalWorldView;
23  import cz.cuni.amis.pogamut.multi.communication.worldview.object.ICompositeWorldObject;
24  import cz.cuni.amis.pogamut.multi.communication.worldview.object.ILocalWorldObject;
25  import cz.cuni.amis.pogamut.multi.communication.worldview.object.ISharedWorldObject;
26  import cz.cuni.amis.pogamut.multi.communication.worldview.object.IStaticWorldObject;
27  import cz.cuni.amis.pogamut.multi.communication.worldview.object.event.DummyObjectEvent.EventType;
28  import cz.cuni.amis.pogamut.multi.utils.timekey.TimeKey;
29  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BeginMessage;
30  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.EndMessage;
31  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
32  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.UT2004CompositeObjectCreator;
33  import cz.cuni.amis.pogamut.ut2004multi.communication.worldview.objects.ILocalGBViewable;
34  
35  /**
36   * Implements the logic to appear/disappear objects based on the GB2004 batches.
37   * @author srlok
38   *
39   */
40  public abstract class UT2004VisionLocalWorldView extends BatchAwareLocalWorldView{
41  
42  	public UT2004VisionLocalWorldView(ComponentDependencies dependencies,
43  			ILifecycleBus bus, IAgentLogger logger,
44  			ISharedWorldView parentWorldView, ITeamedAgentId agentId) {
45  		super(dependencies, bus, logger, parentWorldView, agentId);
46  		// TODO Auto-generated constructor stub
47  	}
48  	
49  	@Override
50  	protected ICompositeWorldObject createCompositeObject(ILocalWorldObject localObject, ISharedWorldObject sharedObject, IStaticWorldObject staticObject)
51  	{
52  		return UT2004CompositeObjectCreator.createObject(localObject, sharedObject, staticObject);
53  	}
54  	
55  	protected Collection<WorldObjectId> lastBatch = new ArrayList<WorldObjectId>();
56  	protected Collection<WorldObjectId> currentBatch = new ArrayList<WorldObjectId>();
57  	
58  	private long currentBatchTime = -1;
59  	
60  	@Override
61  	public synchronized void notify(IWorldChangeEvent event)
62  	{
63  		/*if (isBatchBeginEvent(event))
64  		{
65  			log.info(event.toString());
66  		}
67  		if (isBatchEndEvent(event))
68  		{
69  			log.info(event.toString());
70  		}
71  		if (event instanceof SharedBatchFinishedEvent)
72  		{
73  			log.info(event.toString());
74  		}
75  		if ( currentBatchTime < event.getSimTime() )
76  		{
77  			currentBatchTime = event.getSimTime();
78  		}*/
79  		if ( event instanceof ILocalWorldObjectUpdatedEvent )
80  		{
81  			localEventNotify((ILocalWorldObjectUpdatedEvent)event);
82  		}
83  		else if ( event instanceof ICompositeWorldObjectUpdatedEvent )
84  		{
85  			ILocalWorldObjectUpdatedEvent locEvent = ((ICompositeWorldObjectUpdatedEvent)event).getLocalEvent();
86  			if ( locEvent != null)
87  			{
88  				localEventNotify(locEvent);
89  			}
90  		}
91  		else if ( isBatchEndEvent(event))
92  		{
93  			processBatches();
94  		}
95  		super.notify(event);
96  	}
97  	
98  	@Override
99  	protected void disappearObject(WorldObjectId id, long time)
100 	{
101 		ILocalGBViewable object = (ILocalGBViewable)getLocal(id, TimeKey.get(time));
102 		ILocalWorldObjectUpdatedEvent disEvent = object.createDisappearEvent();
103 		object = (ILocalGBViewable)disEvent.update(object).getObject();
104 		raiseEvent( new WorldObjectDisappearedEvent((IViewable)this.get(id, TimeKey.get(time)) , time) );
105 		raiseEvent( new WorldObjectUpdatedEvent(this.get(id, TimeKey.get(time)), time));
106 	}
107 	
108 	/**
109 	 * Disappears objects that should not be visible (update was not recieved in this batch);
110 	 */
111 	private void processBatches()
112 	{
113 		log.fine("Processing Batches, lastBatchSize : " + lastBatch.size() );
114 		if ( !lastBatch.isEmpty() )
115 		{
116 			this.lastBatch.removeAll(currentBatch);
117 			for( WorldObjectId id : lastBatch)
118 			{
119 				if (log.isLoggable(Level.FINE) ) log.fine("Disappearing object : " + id);
120 				//buffer the object disappeared event. 
121 				super.bufferObjectEvent(id, EventType.DISAPPEARED, currentBatchTime);
122 			}
123 		}
124 		if ( log.isLoggable(Level.FINE ))
125 		log.fine("Swapping batches : Curr : " + this.currentBatch.size() + " ; last : " + this.lastBatch.size());
126 		this.lastBatch = this.currentBatch;
127 		currentBatch = new ArrayList<WorldObjectId>(this.lastBatch.size() + 10);
128 		if ( log.isLoggable(Level.FINE ))
129 		log.fine("Swapping finished : Curr : " + this.currentBatch.size() + " ; last : " + this.lastBatch.size());
130 	}
131 	
132 	/**
133 	 * If the event updates a Viewable object, this method manages making it disappear when no update is recieved for it
134 	 * - this means the object is not in FoW of our agent.
135 	 * @param event
136 	 */
137 	protected void localEventNotify(ILocalWorldObjectUpdatedEvent event)
138 	{
139 		if (log.isLoggable(Level.FINE) ) log.fine("LocalEvent recieved in batch :" + event.getSimTime() + " ; " + event.getId() );
140 		IWorldObject obj = ((IWorldObjectEvent)event).getObject();
141 		if ( obj != null)
142 		{
143 			if ( obj instanceof ILocalGBViewable )
144 			{
145 				if ( ((ILocalGBViewable) obj).isVisible() )
146 				{					
147 					currentBatch.add(obj.getId());
148 				}
149 			}
150 		}
151 	}
152 
153 }