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
37
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
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
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
134
135
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 }