1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the FIFE team                              *
3  *   http://www.fifengine.net                                              *
4  *   This file is part of FIFE.                                            *
5  *                                                                         *
6  *   FIFE is free software; you can redistribute it and/or                 *
7  *   modify it under the terms of the GNU Lesser General Public            *
8  *   License as published by the Free Software Foundation; either          *
9  *   version 2.1 of the License, or (at your option) any later version.    *
10  *                                                                         *
11  *   This library is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
14  *   Lesser General Public License for more details.                       *
15  *                                                                         *
16  *   You should have received a copy of the GNU Lesser General Public      *
17  *   License along with this library; if not, write to the                 *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
20  ***************************************************************************/
21 
22 #ifndef FIFE_MAP_MAP_H
23 #define FIFE_MAP_MAP_H
24 
25 // Standard C++ library includes
26 #include <list>
27 #include <string>
28 #include <vector>
29 
30 // 3rd party library includes
31 
32 // FIFE includes
33 // These includes are split up in two parts, separated by one empty line
34 // First block: files included from the FIFE root src directory
35 // Second block: files included from the same folder
36 #include "util/base/fifeclass.h"
37 #include "util/resource/resource.h"
38 #include "model/metamodel/timeprovider.h"
39 #include "util/structures/rect.h"
40 
41 #include "location.h"
42 
43 namespace FIFE {
44 
45 	class RendererBase;
46 	class RenderBackend;
47 	class Layer;
48 	class CellGrid;
49 	class Map;
50 	class Camera;
51 	class Instance;
52 	class TriggerController;
53 
54 	/** Listener interface for changes happening on map
55 	 */
56 	class MapChangeListener {
57 	public:
~MapChangeListener()58 		virtual ~MapChangeListener() {};
59 
60 		/** Called when some layer is changed on map. @see LayerChangeListener
61 		 * Layer is effectively changed, in case some of its instances
62 		 * is created, deleted or changed during latest update cycle
63 		 * @param map where change occurred
64 		 * @param changedLayers list of layers containing some changes
65 		 * @note Does not report layer creations and deletions
66 		 */
67 		virtual void onMapChanged(Map* map, std::vector<Layer*>& changedLayers) = 0;
68 
69 		/** Called when some layer gets created on the map
70 		 * @param map where change occurred
71 		 * @param layer which got created
72 		 */
73 		virtual void onLayerCreate(Map* map, Layer* layer) = 0;
74 
75 		/** Called when some layer gets deleted on map
76 		 * @param map where change occurred
77 		 * @param layer which will be deleted
78 		 * @note right after this call, layer actually gets deleted!
79 		 */
80 		virtual void onLayerDelete(Map* map, Layer* layer) = 0;
81 	};
82 
83 	/** A container of \c Layer(s).
84 	 *
85 	 * The actual data is contained in \c Layer objects
86 	 * @see Layer
87 	 */
88 	class Map : public FifeClass {
89 		public:
90 
91 			/** Construct a map
92 			 * To add map to model, one should call Model::addMap (otherwise
93 			 * map is not registered with the engine properly)
94 			 */
95 			Map(const std::string& identifier, RenderBackend* renderbackend,
96 				const std::vector<RendererBase*>& renderers, TimeProvider* tp_master=NULL);
97 
98 			/** Destructor
99 			 */
100 			~Map();
101 
102 			/** Get the identifier for this map.
103 			 */
getId()104 			const std::string& getId() const { return m_id; }
105 
106 			/** Sets the identifier for this map.
107 			 */
setId(const std::string & id)108 			void setId(const std::string& id) { m_id = id; }
109 
110 			/** Add a Layer to this Map. Map owns the returned pointer to the new Layer, so don't delete it!
111 			 */
112 			Layer* createLayer(const std::string& identifier, CellGrid* grid);
113 
114 			/** Delete a layer from the map
115 			 */
116 			void deleteLayer(Layer*);
117 
118 			/** Get the layers on this map.
119 			 */
getLayers()120 			const std::list<Layer*>& getLayers() const { return m_layers; }
121 
122 			/** Get the layer with the given id.
123 			 */
124 			Layer* getLayer(const std::string& identifier);
125 
126 			/** Get the overall number of layers
127 			 */
128 			uint32_t getLayerCount() const;
129 
130 			/** Delete all layers from the map
131 			 */
132 			void deleteLayers();
133 
134 			/** Retrieves the minimum/maximum coordinates of instances on the map.
135 			 * @param min A reference to a ExactModelCoordinate that will hold the minimum coordinate.
136 			 * @param max A reference to a ExactModelCoordinate that will hold the maximum coordinate.
137 			 */
138 			void getMinMaxCoordinates(ExactModelCoordinate& min, ExactModelCoordinate& max);
139 
140 			/** Called periodically to update events on map
141 			 * @returns true, if map was changed
142 			 */
143 			bool update();
144 
145 			/** Sets speed for the map. See Model::setTimeMultiplier.
146 			 */
setTimeMultiplier(float multip)147 			void setTimeMultiplier(float multip) { m_timeProvider.setMultiplier(multip); }
148 
149 			/** Gets model speed. @see setTimeMultiplier.
150 			 */
getTimeMultiplier()151 			float getTimeMultiplier() const { return m_timeProvider.getMultiplier(); }
152 
153 			/** Gets timeprovider used in the map
154 			 */
getTimeProvider()155 			TimeProvider* getTimeProvider() { return &m_timeProvider; }
156 
157 			/** Adds new change listener
158 			* @param listener to add
159 			*/
160 			void addChangeListener(MapChangeListener* listener);
161 
162 			/** Removes associated change listener
163 			* @param listener to remove
164 			*/
165 			void removeChangeListener(MapChangeListener* listener);
166 
167 			/** Returns true, if map information was changed during previous update round
168 			*/
isChanged()169 			bool isChanged() { return !m_changedLayers.empty(); }
170 
171 			/** Returns layers that were changed during previous update round
172 			*/
getChangedLayers()173 			std::vector<Layer*>& getChangedLayers() { return m_changedLayers; }
174 
175 			/** Adds camera to the map. The Map takes ownership of the camera
176 				so don't delete it.
177 			*/
178 			Camera* addCamera(const std::string& id, const Rect& viewport);
179 
180 			/** Removes a camera from the map
181 			*/
182 			void removeCamera(const std::string& id);
183 
184 			/** Get a camera by its identifier.
185 			*/
186 			Camera* getCamera(const std::string& id);
187 
188 			/** Get a list containing all cameras.
189 			*/
190 			const std::vector<Camera*>& getCameras() const;
191 
192 			/** Return the number of enabled cameras in this map.
193 			 */
194 			uint32_t getActiveCameraCount() const;
195 
setFilename(const std::string & file)196 			void setFilename(const std::string& file) { m_filename = file; }
getFilename()197 			const std::string& getFilename() const { return m_filename; }
198 
199 			/** Adds instance that is to be transferred to another layer.
200 			* @param instance A pointer to the instance that is to be transferred.
201 			* @param target A const reference to the target location.
202 			*/
203 			void addInstanceForTransfer(Instance* instance, const Location& target);
204 
205 			/** Removes instance that should be transferred to another layer.
206 			* @param instance A pointer to the instance that should be transferred.
207 			*/
208 			void removeInstanceForTransfer(Instance* instance);
209 
210 			/** Creates cellcaches for this map. Called from maploader.
211 			*/
212 			void initializeCellCaches();
213 
214 			/** Creates cellcaches for this map. Called from maploader.
215 			*/
216 			void finalizeCellCaches();
217 
218 			/**
219 			 */
getTriggerController()220 			TriggerController* getTriggerController() const { return m_triggerController; };
221 
222 		private:
223 			std::string m_id;
224 			std::string m_filename;
225 
226 			std::list<Layer*> m_layers;
227 			TimeProvider m_timeProvider;
228 
229 			Map(const Map& map);
230 			Map& operator=(const Map& map);
231 
232 			//! listeners for map changes
233 			std::vector<MapChangeListener*> m_changeListeners;
234 
235 			//! holds changed layers after each update
236 			std::vector<Layer*> m_changedLayers;
237 
238 			//! holds the cameras attached to this map
239 			std::vector<Camera*> m_cameras;
240 
241 			//! pointer to renderbackend
242 			RenderBackend* m_renderBackend;
243 
244 			//! holds handles to all created renderers
245 			std::vector<RendererBase*> m_renderers;
246 
247 			//! true, if something was changed on map during previous update (layer change, creation, deletion)
248 			bool m_changed;
249 
250 			//! holds instances which should be transferred on the next update
251 			std::map<Instance*, Location> m_transferInstances;
252 
253 			TriggerController* m_triggerController;
254 	};
255 
256 }
257 
258 #endif
259