1 /***************************************************************************
2  *   Copyright (C) 2006-2012 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_PROTOTYPE_H
23 #define FIFE_PROTOTYPE_H
24 
25 // Standard C++ library includes
26 #include <string>
27 #include <map>
28 #include <list>
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/resource/resource.h"
37 #include "util/math/angles.h"
38 
39 namespace FIFE {
40 
41 	class Action;
42 	class IPather;
43 	class IVisual;
44 
45 	/** Object class
46 	 *
47 	 * Objects describe the properties of objects.
48 	 * Objects may inherit default values from another object.
49 	 *
50 	 */
51 	class Object {
52 	public:
53 		/** Constructor
54 		 * An object may optionally inherit default attributes
55 		 * from another object. This object may override these
56 		 * defaults, but it may not CHANGE the inherited values.
57 		 *
58 		 * Objects are created by calling Model::createObject, thus
59 		 * this method should really be called only by Model or test code
60 		 * @see Model in model/model.h for creation of objects.
61 		 */
62 		Object(const std::string& identifier, const std::string& name_space, Object* inherited=NULL);
63 
64 		/** Destructor
65 		 */
66 		~Object();
67 
getId()68 		const std::string& getId() const { return m_id; }
getNamespace()69 		const std::string& getNamespace() const { return m_namespace; }
70 
71 		/** Sets the identifier for this object.
72 		 */
setId(const std::string & id)73 		void setId(const std::string& id) { m_id = id; }
74 
75 		/** Adds new action with given id. In case there is action already
76 		 *  with given id, returns it instead of new object
77 		 *  Action instances are managed by object
78 		 * @param identifier An identifier or name to give the action.
79 		 * @param is_default if true, becomes default action for this object
80 		 * 	default objects are used e.g. when showing them on editor.
81 		 *      if multiple default actions are created, last one remains.
82 		 *      In case there's no explicit default action created, first
83 		 *      action created becomes the default
84 		 */
85 		Action* createAction(const std::string& identifier, bool is_default=false);
86 
87 		/** Gets action with given id. If not found, returns NULL
88 		 */
89 		Action* getAction(const std::string& identifier, bool deepsearch = true) const;
90 
91 		/** Gets all available action ids of the object and packs them into a list
92 		 */
93 		std::list<std::string> getActionIds() const;
94 
95 		/** Sets default action assigned to this object. If not available, then default action is not changed.
96 		 */
97 		void setDefaultAction(const std::string& identifier);
98 
99 		/** Gets default action assigned to this object. If none available, returns NULL
100 		 */
101 		Action* getDefaultAction() const;
102 
103 		/** Sets pather used by instances created out of this object
104 		 */
105 		void setPather(IPather* pather);
106 
107 		/** Gets associated pather
108 		 */
109 		IPather* getPather() const;
110 
111 		/** Gets an object where this object was inherited from
112 		 * @see inherited object
113 		 */
114 		Object* getInherited() const;
115 
116 		/** Sets visualization to be used. Transfers ownership.
117 		 */
118 		void adoptVisual(IVisual* visual);
119 
120 		/** Gets used visualization
121 		 */
getVisual()122 		template<typename T> T* getVisual() const { return reinterpret_cast<T*>(m_visual); }
123 
124 		/** Sets if object blocks movement
125 		 */
126 		void setBlocking(bool blocking);
127 
128 		/** Gets if object blocks movement
129 		 */
130 		bool isBlocking() const;
131 
132 		/** Set to true, if object is such that it doesn't move
133 		 */
134 		void setStatic(bool stat);
135 
136 		/** Gets if object moves
137 		 */
138 		bool isStatic() const;
139 
140 		void setFilename(const std::string& file);
141 		const std::string& getFilename() const;
142 
143 		/** Sets the cell stack position.
144 		 *  Is used to determine which Instance is on top of a cell.
145 		 * @param position The stack position on a cell, range 0-255.
146 		 */
147 		void setCellStackPosition(uint8_t position);
148 
149 		/** Returns cell stack position.
150 		 * @return The stack position on a cell, range 0-255.
151 		 */
152 		uint8_t getCellStackPosition() const;
153 
154 		/** Gets if object uses special cost.
155 		 * @return A boolean, true if the object uses special cost, otherwise false.
156 		 */
157 		bool isSpecialCost() const;
158 
159 		/** Sets the cost id.
160 		 * @param cost A const reference to a string which contains the identifier.
161 		 */
162 		void setCostId(const std::string& cost);
163 
164 		/** Returns the cost id.
165 		 * @return A const reference to a string which contains the identifier.
166 		 */
167 		std::string getCostId() const;
168 
169 		/** Sets the cost.
170 		 * @param cost A double which value is used as cost.
171 		 */
172 		void setCost(double cost);
173 
174 		/** Returns the cost.
175 		 * @return A double which value is used as cost.
176 		 */
177 		double getCost() const;
178 
179 		/** Gets if object uses special speed modifier.
180 		 * @return A boolean, true if the object uses special speed, otherwise false.
181 		 */
182 		bool isSpecialSpeed() const;
183 
184 		/** Sets the speed modifier.
185 		 * @param cost A double which value is used as speed multiplier.
186 		 */
187 		void setSpeed(double cost);
188 
189 		/** Returns the speed modifier.
190 		 * @return A double which value is used as speed multiplier.
191 		 */
192 		double getSpeed() const;
193 
194 		/** Gets if object uses special cost.
195 		 * @return A boolean, true if the object uses special cost, otherwise false.
196 		 */
197 		bool isMultiObject() const;
198 
199 		/** Adds a multi part identifier.
200 		 * @param partId A const reference to a string that holds the identifier.
201 		 */
202 		void addMultiPartId(const std::string& partId);
203 
204 		/** Returns all multi part identifiers.
205 		 * @return A const reference to a list that holds the identifiers.
206 		 */
207 		std::list<std::string> getMultiPartIds() const;
208 
209 		/** Removes a multi part identifier.
210 		 * @param partId A const reference to a string that holds the identifier.
211 		 */
212 		void removeMultiPartId(const std::string& partId);
213 
214 		/** Removes all multi part identifiers.
215 		 */
216 		void removeAllMultiPartIds();
217 
218 		/** Gets if object is a part of a multi object.
219 		 * @return A boolean, true if the object is a part of a multi object, otherwise false.
220 		 */
221 		bool isMultiPart() const;
222 
223 		/** Sets the object as a part of a multi object.
224 		 * @param part A boolean, true if the object is a part of a multi object, otherwise false.
225 		 */
226 		void setMultiPart(bool part);
227 
228 		/** Adds a object as a part of a multi object.
229 		 * @param obj A pointer to the multi part object.
230 		 */
231 		void addMultiPart(Object* obj);
232 
233 		/** Returns all multi part objects.
234 		 * @return A const reference to a set that holds the objects.
235 		 */
236 		std::set<Object*> getMultiParts() const;
237 
238 		/** Removes a multi part object.
239 		 * @param obj A pointer to the part object.
240 		 */
241 		void removeMultiPart(Object* obj);
242 
243 		/** Removes all multi part objects.
244 		 */
245 		void removeMultiParts();
246 
247 		/** Adds rotationally dependent coordinates for this object part.
248 		 * @param rotation A integer value for the angle.
249 		 * @param coord A ModelCoordinate as relative coordinate, 0,0 is always the multi object center.
250 		 */
251 		void addMultiPartCoordinate(int32_t rotation, ModelCoordinate coord);
252 
253 		/** Returns all rotationally dependent coordinates from this object part.
254 		 * @return A const reference to a multimap which contains the coordinates per rotation.
255 		 */
256 		std::multimap<int32_t, ModelCoordinate> getMultiPartCoordinates() const;
257 
258 		/** Returns all object part coordinates for the given rotation.
259 		 * @param rotation A integer value for the angle.
260 		 * @return A vector which contains the coordinates.
261 		 */
262 		std::vector<ModelCoordinate> getMultiPartCoordinates(int32_t rotation) const;
263 
264 		/** Returns all multi object coordinates for the given rotation.
265 		 * @param rotation A integer value for the angle.
266 		 * @return A vector which contains the coordinates.
267 		 */
268 		std::vector<ModelCoordinate> getMultiObjectCoordinates(int32_t rotation) const;
269 
270 		/** Sets the rotation anchor for this multi object.
271 		 * Is used to rotate the images from multi part objects around this relative point,
272 		 * default is 0.0, 0.0 the center of the multi object.
273 		 * @param anchor A const reference to a ExactModelCoordinate that holds the anchor coordinate.
274 		 */
275 		void setRotationAnchor(const ExactModelCoordinate& anchor);
276 
277 		/** Returns the rotation anchor for this multi object.
278 		 * @return A const reference to a ExactModelCoordinate that holds the anchor coordinate.
279 		 */
280 		ExactModelCoordinate getRotationAnchor() const;
281 
282 		/** Sets the rotation to restricted.
283 		 * If this is enabled the multi object uses only rotation values are which based on multi coordinates.
284 		 * @param restrict A boolean, if true the rotation will be restricted, false for free rotation.
285 		 */
286 		void setRestrictedRotation(bool restrict);
287 
288 		/** Gets if object uses restricted rotations.
289 		 * @return A boolean, true if the object uses restricted rotations, otherwise false.
290 		 */
291 		bool isRestrictedRotation() const;
292 
293 		/** Returns the most obvious rotation, based on multi coordinates.
294 		 * @param rotation A integer value for the original angle.
295 		 * @return A integer value for the obvious rotation.
296 		 */
297 		int32_t getRestrictedRotation(int32_t rotation);
298 
299 		/** Sets z-step range for object.
300 		 *  0 means it can not climb, with a value of 1 it can climb a z-height of 1 and so on.
301 		 * @param zRange The z-step range as int.
302 		 */
303 		void setZStepRange(int32_t zRange);
304 
305 		/** Returns z-step range from object. In case it is not limited -1 is returned.
306 		 * @return The z-step range as int.
307 		 */
308 		int32_t getZStepRange() const;
309 
310 		/** Sets the area id that the instances of this object adds to their cells.
311 		 * @param id The area id, default is "".
312 		 */
313 		void setArea(const std::string& id);
314 
315 		/** Gets the area id that the instances of this object adds to their cells.
316 		 * @return The area id, default is "".
317 		 */
318 		std::string getArea() const;
319 
320 		/** Adds an area id to walkable area. The instances of this object
321 		 *  can only walk on cells that part of the given areas.
322 		 * @param id The area id.
323 		 */
324 		void addWalkableArea(const std::string& id);
325 
326 		/** Removes an area id from walkable areas.
327 		 * @param id The area id.
328 		 */
329 		void removeWalkableArea(const std::string& id);
330 
331 		/** Returns a list that contains all walkable area ids.
332 		 * @return A list that contains all walkable area ids as strings.
333 		 */
334 		std::list<std::string> getWalkableAreas() const;
335 
336 		/** Compares equality of two objects
337 		 */
338 		bool operator==(const Object& obj) const;
339 
340 		/** Compares unequality of two objects
341 		 */
342 		bool operator!=(const Object& obj) const;
343 
344 	private:
345 		//! identifier
346 		std::string m_id;
347 
348 		//! namespace
349 		std::string m_namespace;
350 
351 		//! filename
352 		std::string m_filename;
353 
354 		//! pointer to inherited object
355 		Object* m_inherited;
356 
357 		//! pointer to object visual
358 		IVisual* m_visual;
359 
360 		class BasicObjectProperty {
361 		public:
362 			//! Constructor
363 			BasicObjectProperty();
364 
365 			//! Destructor
366 			~BasicObjectProperty();
367 
368 			//! area id
369 			std::string m_area;
370 
371 			//! holds action ids and assigned actions
372 			std::map<std::string, Action*>* m_actions;
373 
374 			//! pointer to default action
375 			Action* m_defaultAction;
376 
377 			//! indicates if object blocks
378 			bool m_blocking;
379 
380 			//remove this with a if (MovableObjectProperty)
381 			//! indicates if object is static
382 			bool m_static;
383 
384 			//! position on cellstack
385 			uint8_t m_cellStack;
386 		};
387 
388 		class MovableObjectProperty {
389 		public:
390 			//! Constructor
391 			MovableObjectProperty();
392 
393 			//! Destructor
394 			~MovableObjectProperty();
395 
396 			//! pointer to pathfinder
397 			IPather* m_pather;
398 
399 			//! cost identifier
400 			std::string m_costId;
401 
402 			//! cost value, default 1.0
403 			double m_cost;
404 
405 			//! speed modifier, default 1.0
406 			double m_speed;
407 
408 			//! z range value
409 			int32_t m_zRange;
410 
411 			//! list contains walkable area ids
412 			std::list<std::string> m_walkableAreas;
413 		};
414 
415 		class MultiObjectProperty {
416 		public:
417 			//! Constructor
418 			MultiObjectProperty();
419 
420 			//! Destructor
421 			~MultiObjectProperty();
422 
423 			//! indicates if object is part of multi object
424 			bool m_multiPart;
425 
426 			//! indicates if object uses only restricted rotations
427 			bool m_restrictedRotation;
428 
429 			//! list with part identifiers
430 			std::list<std::string> m_multiPartIds;
431 
432 			//! rotation anchor
433 			ExactModelCoordinate m_rotationAnchor;
434 
435 			//! set contains part objects
436 			std::set<Object*> m_multiParts;
437 
438 			//! part object angles
439 			type_angle2id m_partAngleMap;
440 
441 			//! multi object angles
442 			type_angle2id m_multiAngleMap;
443 
444 			//! part object coordinates
445 			std::multimap<int32_t, ModelCoordinate> m_multiPartCoordinates;
446 
447 			//! multi object coordinates
448 			std::multimap<int32_t, ModelCoordinate> m_multiObjectCoordinates;
449 		};
450 
451 		BasicObjectProperty* m_basicProperty;
452 		MovableObjectProperty* m_moveProperty;
453 		MultiObjectProperty* m_multiProperty;
454 	};
455 
456 } //FIFE
457 #endif
458 
459