View Javadoc

1   /*
2    * Copyright (C) 2013 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.cuni.amis.pogamut.ut2004.agent.navigation.navmesh;
18  
19  import java.util.logging.Logger;
20  
21  import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
22  import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
23  import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
24  import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectFirstEncounteredEvent;
25  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
26  import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
27  import cz.cuni.amis.pogamut.ut2004.agent.navigation.navmesh.drawing.IUT2004ServerProvider;
28  import cz.cuni.amis.pogamut.ut2004.agent.navigation.navmesh.drawing.NavMeshDraw;
29  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo;
30  import cz.cuni.amis.utils.NullCheck;
31  
32  /**
33   * Main class of Navigation Mesh module
34   * 
35   * @author Jakub Tomek
36   * @author Jakub Gemrot aka Jimmy
37   */
38  public class NavMeshModule {
39  	
40  	private IWorldView worldView;	
41  	private Logger log;
42  	
43  	private NavMeshDraw navMeshDraw;
44  	
45  	//
46  	// STATE
47  	//
48  	
49  	private boolean loaded = false;
50  	private GameInfo loadedForMap = null;
51  	
52  	//
53  	// NAVMESH DATA STRUCTURES
54  	//
55  	
56      private NavMesh navMesh;
57      
58      //
59      // LISTENER
60      //
61      
62      private IWorldObjectEventListener<GameInfo, IWorldObjectEvent<GameInfo>> gameInfoListener = new IWorldObjectEventListener<GameInfo, IWorldObjectEvent<GameInfo>>() {
63  		@Override
64  		public void notify(IWorldObjectEvent<GameInfo> event) {
65  			load(event.getObject());
66  		}
67  	};
68  	
69      public NavMeshModule(IUT2004ServerProvider serverProvider, IWorldView worldView, IAgentLogger logger) {
70      	if (logger == null) {
71      		log = new LogCategory("NavMesh");
72      	} else {
73      		log = logger.getCategory("NavMesh");
74      	}
75      	
76      	this.worldView = worldView;
77      	NullCheck.check(this.worldView, "worldView");
78      	
79      	navMesh = new NavMesh(worldView, log);
80      	
81      	worldView.addObjectListener(GameInfo.class, WorldObjectFirstEncounteredEvent.class, gameInfoListener);
82      	
83      	GameInfo info = worldView.getSingle(GameInfo.class);
84      	if (info != null) {
85      		load(info);
86      	}
87      	
88      	navMeshDraw = new NavMeshDraw(navMesh, log, serverProvider);
89      }
90      
91      private void clear() {
92      	log.warning("NavMesh has been cleared...");
93      	
94      	navMesh.clear();
95          
96          loaded = false;
97          loadedForMap = null;
98      }
99      	
100     private void load(GameInfo info) {
101     	if (info == null) {
102     		log.severe("Could not load for 'null' GameInfo!");
103     		return;
104     	}
105         if (loaded) {
106         	if (loadedForMap == null) {
107         		// WTF?
108         		clear();
109         	} else {
110         		if (loadedForMap.getLevel().equals(info.getLevel())) {
111         			// ALREADY INITIALIZED FOR THE SAME LEVEL
112         			return;
113         		}
114         	}
115         }
116         
117         if (navMesh.load(info)) {
118         	loaded = true;
119         	loadedForMap = info;
120         } else {
121         	loaded = false;
122         	loadedForMap = null;
123         }
124 
125 	}
126         	
127 	// ================
128     // PUBLIC INTERFACE
129     // ================
130     
131     /**
132      * Tells whether NavMesh has been initialized and {@link #getNavMesh()} is usable. 
133      * @return
134      */
135     public boolean isInitialized() {
136     	return loaded;
137     }
138     
139     /**
140      * Always non-null, always returns {@link NavMesh}. However, you should check {@link #isInitialized()} whether the {@link NavMesh} is usable.
141      * @return
142      */
143     public NavMesh getNavMesh() {
144     	return navMesh;
145     }
146     
147     public NavMeshDraw getNavMeshDraw() {
148     	return navMeshDraw;
149     }
150     
151 }