1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2010 by The Allacrost Project
3 //                         All Rights Reserved
4 //
5 // This code is licensed under the GNU GPL version 2. It is free software
6 // and you may modify it and/or redistribute it under the terms of this license.
7 // See http://www.gnu.org/copyleft/gpl.html for details.
8 ///////////////////////////////////////////////////////////////////////////////
9 
10 /** ****************************************************************************
11 *** \file    map_utils.h
12 *** \author  Tyler Olsen, roots@allacrost.org
13 *** \brief   Header file for map mode utility code
14 *** *****************************************************************************/
15 
16 #ifndef __MAP_UTILS_HEADER__
17 #define __MAP_UTILS_HEADER__
18 
19 // Allacrost utilities
20 #include "utils.h"
21 #include "defs.h"
22 
23 namespace hoa_map {
24 
25 //! Determines whether the code in the hoa_map namespace should print debug statements or not.
26 extern bool MAP_DEBUG;
27 
28 namespace private_map {
29 
30 /** \name Screen Coordiante System Constants
31 *** \brief Represents the size of the visible screen in map tiles and the collision grid
32 *** Every map tile is 32x32 pixels, and every collision grid element is one quarter of that
33 *** area (16x16). Thus the number of collision grid elements that compose the screen are
34 *** four times the number of tiles that are visible on the screen. This also means the number
35 *** of rows and columns of grid elements that encompass the screen are twice that of the
36 *** number of rows and columns of tiles.
37 **/
38 //@{
39 const float SCREEN_COLS = 32.0f;
40 const float SCREEN_ROWS = 24.0f;
41 const float HALF_SCREEN_COLS = 16.0f;
42 const float HALF_SCREEN_ROWS = 12.0f;
43 
44 const uint16 TILE_COLS = 16; // Number of tile columns that fit on the screen
45 const uint16 TILE_ROWS = 12; // Number of tile rows that fit on the screen
46 const uint16 HALF_TILE_COLS = 8;
47 const uint16 HALF_TILE_ROWS = 6;
48 
49 const uint16 GRID_LENGTH = 32; // Length of a grid element in pixels
50 const uint16 TILE_LENGTH = 64; // Length of a tile in pixels
51 const uint16 HALF_TILE_LENGTH = 32;
52 //@}
53 
54 
55 /** \name Map State Enum
56 *** \brief Represents the current state of operation during map mode.
57 **/
58 //@{
59 enum MAP_STATE {
60 	STATE_INVALID    = 0,
61 	STATE_EXPLORE    = 1, //!< Standard state, player has control to move about map
62 	STATE_SCENE      = 2, //!< Like the explore state but player has no control (input is ignored)
63 	STATE_DIALOGUE   = 3, //!< When a dialogue is active
64 	STATE_TREASURE   = 4, //!< Active when a treasure has been procured by the player
65 	STATE_TOTAL      = 5
66 };
67 //@}
68 
69 
70 /** \name Map Context Constants
71 *** \brief Constants used to represent all 32 possible map contexts
72 *** Note that only one bit is set for each context. This is done so that the collision
73 *** grid for all contexts can be stored in a single integer. This also simplifies the
74 *** complexity of collision detection for map sprites.
75 **/
76 enum MAP_CONTEXT {
77 	MAP_CONTEXT_01 = 0x00000001, // Also known as the base context
78 	MAP_CONTEXT_02 = 0x00000002,
79 	MAP_CONTEXT_03 = 0x00000004,
80 	MAP_CONTEXT_04 = 0x00000008,
81 	MAP_CONTEXT_05 = 0x00000010,
82 	MAP_CONTEXT_06 = 0x00000020,
83 	MAP_CONTEXT_07 = 0x00000040,
84 	MAP_CONTEXT_08 = 0x00000080,
85 	MAP_CONTEXT_09 = 0x00000100,
86 	MAP_CONTEXT_10 = 0x00000200,
87 	MAP_CONTEXT_11 = 0x00000400,
88 	MAP_CONTEXT_12 = 0x00000800,
89 	MAP_CONTEXT_13 = 0x00001000,
90 	MAP_CONTEXT_14 = 0x00002000,
91 	MAP_CONTEXT_15 = 0x00004000,
92 	MAP_CONTEXT_16 = 0x00008000,
93 	MAP_CONTEXT_17 = 0x00010000,
94 	MAP_CONTEXT_18 = 0x00020000,
95 	MAP_CONTEXT_19 = 0x00040000,
96 	MAP_CONTEXT_20 = 0x00080000,
97 	MAP_CONTEXT_21 = 0x00100000,
98 	MAP_CONTEXT_22 = 0x00200000,
99 	MAP_CONTEXT_23 = 0x00400000,
100 	MAP_CONTEXT_24 = 0x00800000,
101 	MAP_CONTEXT_25 = 0x01000000,
102 	MAP_CONTEXT_26 = 0x02000000,
103 	MAP_CONTEXT_27 = 0x04000000,
104 	MAP_CONTEXT_28 = 0x08000000,
105 	MAP_CONTEXT_29 = 0x10000000,
106 	MAP_CONTEXT_30 = 0x20000000,
107 	MAP_CONTEXT_31 = 0x40000000,
108 	MAP_CONTEXT_32 = 0x80000000
109 };
110 
111 
112 //! \brief The number of tiles that are found in a tileset image (512x512 pixel image containing 32x32 pixel tiles)
113 const uint32 TILES_PER_TILESET = 256;
114 
115 
116 //! \brief Used to identify the type of map object
117 enum MAP_OBJECT_TYPE {
118 	PHYSICAL_TYPE = 0,
119 	VIRTUAL_TYPE = 1,
120 	SPRITE_TYPE = 2,
121 	ENEMY_TYPE = 3,
122 	TREASURE_TYPE = 4
123 };
124 
125 
126 /** \name Map Sprite Speeds
127 *** \brief Common speeds for sprite movement.
128 *** These values are the time (in milliseconds) that it takes a sprite to walk
129 *** the distance of one map grid (16 pixels).
130 **/
131 //@{
132 const float VERY_SLOW_SPEED  = 225.0f;
133 const float SLOW_SPEED       = 190.0f;
134 const float NORMAL_SPEED     = 150.0f;
135 const float FAST_SPEED       = 110.0f;
136 const float VERY_FAST_SPEED  = 75.0f;
137 //@}
138 
139 
140 /** \name Sprite Direction Constants
141 *** \brief Constants used for determining sprite directions
142 *** Sprites are allowed to travel in eight different directions, however the sprite itself
143 *** can only be facing one of four ways: north, south, east, or west. Because of this, it
144 *** is possible to travel, for instance, northwest facing north <i>or</i> northwest facing west.
145 *** The "NW_NORTH" constant means that the sprite is traveling to the northwest and is
146 *** facing towards the north.
147 ***
148 *** \note These constants include a series of shorthands (MOVING_NORTHWEST, FACING_NORTH) used
149 *** to check for movement and facing directions.
150 **/
151 //@{
152 const uint16 NORTH     = 0x0001;
153 const uint16 SOUTH     = 0x0002;
154 const uint16 WEST      = 0x0004;
155 const uint16 EAST      = 0x0008;
156 const uint16 NW_NORTH  = 0x0010;
157 const uint16 NW_WEST   = 0x0020;
158 const uint16 NE_NORTH  = 0x0040;
159 const uint16 NE_EAST   = 0x0080;
160 const uint16 SW_SOUTH  = 0x0100;
161 const uint16 SW_WEST   = 0x0200;
162 const uint16 SE_SOUTH  = 0x0400;
163 const uint16 SE_EAST   = 0x0800;
164 // Used to check for movement direction regardless of facing direction
165 const uint16 MOVING_NORTHWARD = NORTH | NW_NORTH | NW_WEST | NE_NORTH | NE_EAST;
166 const uint16 MOVING_SOUTHWARD = SOUTH | SW_SOUTH | SW_WEST | SE_SOUTH | SE_EAST;
167 const uint16 MOVING_EASTWARD = EAST | NE_EAST | NE_NORTH | SE_EAST | SE_SOUTH;
168 const uint16 MOVING_WESTWARD = WEST | NW_WEST | NW_NORTH | SW_WEST | SW_SOUTH;
169 const uint16 MOVING_NORTHWEST = NW_NORTH | NW_WEST;
170 const uint16 MOVING_NORTHEAST = NE_NORTH | NE_EAST;
171 const uint16 MOVING_SOUTHWEST = SW_SOUTH | SW_WEST;
172 const uint16 MOVING_SOUTHEAST = SE_SOUTH | SE_EAST;
173 const uint16 MOVING_ORTHOGONALLY = NORTH | SOUTH | EAST | WEST;
174 const uint16 MOVING_DIAGONALLY = MOVING_NORTHWEST | MOVING_NORTHEAST | MOVING_SOUTHWEST | MOVING_SOUTHEAST;
175 // Used to check for facing direction regardless of moving direction
176 const uint16 FACING_NORTH = NORTH | NW_NORTH | NE_NORTH;
177 const uint16 FACING_SOUTH = SOUTH | SW_SOUTH | SE_SOUTH;
178 const uint16 FACING_WEST = WEST | NW_WEST | SW_WEST;
179 const uint16 FACING_EAST = EAST | NE_EAST | SE_EAST;
180 //@}
181 
182 
183 /** \name Map Sprite Animation Constants
184 *** These constants are used to index the MapSprite#animations vector to display the correct
185 *** animation. The first 8 entries in this vector always represent the same sets of animations
186 *** for each map sprite. Not all sprites have running animations, so the next 4 entries in the
187 *** sprite's animation vector are not necessarily running animations.
188 **/
189 //@{
190 const uint32 ANIM_STANDING_SOUTH = 0;
191 const uint32 ANIM_STANDING_NORTH = 1;
192 const uint32 ANIM_STANDING_WEST  = 2;
193 const uint32 ANIM_STANDING_EAST  = 3;
194 const uint32 ANIM_WALKING_SOUTH  = 4;
195 const uint32 ANIM_WALKING_NORTH  = 5;
196 const uint32 ANIM_WALKING_WEST   = 6;
197 const uint32 ANIM_WALKING_EAST   = 7;
198 const uint32 ANIM_RUNNING_SOUTH  = 8;
199 const uint32 ANIM_RUNNING_NORTH  = 9;
200 const uint32 ANIM_RUNNING_WEST   = 10;
201 const uint32 ANIM_RUNNING_EAST   = 11;
202 const uint32 ANIM_ATTACKING_EAST = 12;
203 //@}
204 
205 
206 //! \brief Represents the various types of collisions which may occur for a sprite
207 enum COLLISION_TYPE {
208 	NO_COLLISION = 0,       //!< Indicates that no collision has occurred
209 	BOUNDARY_COLLISION = 1, //!< Happens when the sprite attempts to move outside any of the map's boundaries
210 	GRID_COLLISION = 2,     //!< Condition when the sprite's collision rectangle overlaps an invalid element of the map's collision grid
211 	OBJECT_COLLISION = 3,   //!< Occurs when the sprite collides with another map object in the same object layer
212 };
213 
214 
215 //! \brief Identifiers for the similarly named classes of map events
216 enum EVENT_TYPE {
217 	INVALID_EVENT              = 0,
218 	DIALOGUE_EVENT             = 1,
219 	SHOP_EVENT                 = 2,
220 	SOUND_EVENT                = 3,
221 	MAP_TRANSITION_EVENT       = 4,
222 	JOIN_PARTY_EVENT           = 5,
223 	BATTLE_ENCOUNTER_EVENT     = 6,
224 	SCRIPTED_EVENT             = 7,
225 	PATH_MOVE_SPRITE_EVENT     = 8,
226 	RANDOM_MOVE_SPRITE_EVENT   = 9,
227 	ANIMATE_SPRITE_EVENT       = 10,
228 };
229 
230 
231 //! \brief Defines the different states the dialogue can be in.
232 enum DIALOGUE_STATE {
233 	DIALOGUE_STATE_LINE     =  0, //!< Active when the dialogue window is in the process of displaying a line of text
234 	DIALOGUE_STATE_OPTION   =  1, //!< Active when player-selectable options are present in the dialogue window
235 };
236 
237 
238 //! \brief The maximum number of options that a line of dialogue can present to the player
239 const uint32 MAX_OPTIONS = 5;
240 
241 
242 //! \brief The number of milliseconds to take to fade out the map
243 const uint32 FADE_OUT_TIME = 2000;
244 
245 
246 /** \brief Returns the opposite facing direction of the direction given in parameter.
247 *** \return A direction that faces opposite to the argument direction
248 *** \note This is mostly used as an helper function to make sprites face each other in a conversation.
249 **/
250 uint16 CalculateOppositeDirection(const uint16 direction);
251 
252 
253 /** ****************************************************************************
254 *** \brief Represents a rectangular section of a map
255 ***
256 *** This is a small class that is used to represent rectangular map areas. These
257 *** areas are used very frequently throughout the map code to check for collision
258 *** detection, determining objects that are within a certain radius of one
259 *** another, etc.
260 *** ***************************************************************************/
261 class MapRectangle {
262 public:
MapRectangle()263 	MapRectangle() :
264 		left(0.0f), right(0.0f), top(0.0f), bottom(0.0f)
265 		{}
266 
MapRectangle(float l,float r,float t,float b)267 	MapRectangle(float l, float r, float t, float b) :
268 		left(l), right(r), top(t), bottom(b)
269 		{}
270 
271 	//! \brief The four edges of the rectangle's area
272 	float left, right, top, bottom;
273 
274 	/** \brief Determines if two rectangle objects intersect with one another
275 	*** \param first A reference to the first rectangle object
276 	*** \param second A reference to the second rectangle object
277 	*** \return True if the two rectangles intersect at any location
278 	***
279 	*** This function assumes that the rectangle objects hold map collision grid
280 	*** coordinates, where the top of the rectangle is a smaller number than the
281 	*** bottom of the rectangle and the left is a smaller number than the right.
282 	**/
283 	static bool CheckIntersection(const MapRectangle& first, const MapRectangle& second);
284 }; // class MapRectangle
285 
286 
287 /** ****************************************************************************
288 *** \brief Retains information about how the next map frame should be drawn.
289 ***
290 *** This class is used by the MapMode class to determine how the next map frame
291 *** should be drawn. This includes which tiles will be visible and the offset
292 *** coordinates for the screen. Map objects also use this information to determine
293 *** where (and if) they should be drawn.
294 ***
295 *** \note The MapMode class keeps an active object of this class with the latest
296 *** information about the map. It should be the only instance of this class that is
297 *** needed.
298 *** ***************************************************************************/
299 class MapFrame {
300 public:
301 	//! \brief The column and row indeces of the starting tile to draw (the top-left tile).
302 	int16 starting_col, starting_row;
303 
304 	//! \brief The number of columns and rows of tiles to draw on the screen.
305 	uint8 num_draw_cols, num_draw_rows;
306 
307 	//! \brief The x and y position screen coordinates to start drawing tiles from.
308 	float tile_x_start, tile_y_start;
309 
310 	/** \brief The position coordinates of the screen edges.
311 	*** These members are in terms of the map grid 16x16 pixel coordinates that map objects use.
312 	*** The presense of these coordinates make it easier for map objects to figure out whether or
313 	*** not they should be drawn on the screen. Note that these are <b>not</b> used as drawing
314 	*** cursor positions, but rather are map grid coordinates indicating where the screen edges lie.
315 	**/
316 	MapRectangle screen_edges;
317 }; // class MapFrame
318 
319 
320 /** ****************************************************************************
321 *** \brief A container class for node information in pathfinding.
322 ***
323 *** This class is used in the MapMode#_FindPath function to find an optimal
324 *** path from a given source to a destination. The path finding algorithm
325 *** employed is A* and thus many members of this class are particular to the
326 *** implementation of that algorithm.
327 *** ***************************************************************************/
328 class PathNode {
329 public:
330 	/** \brief The grid coordinates for this node
331 	*** These coordinates correspond to the collision grid, where each element
332 	*** is a 16x16 pixel space on the map.
333 	**/
334 	int16 row, col;
335 
336 	//! \name Path Scoring Members
337 	//@{
338 	//! \brief The total score for this node (f = g + h).
339 	int16 f_score;
340 
341 	//! \brief The score for this node relative to the source.
342 	int16 g_score;
343 
344 	//! \brief The Manhattan distance from this node to the destination.
345 	int16 h_score;
346 	//@}
347 
348 	//! \brief The grid coordinates for the parent of this node
349 	int16 parent_row, parent_col;
350 
351 	// ---------- Methods
352 
PathNode()353 	PathNode() : row(-1), col(-1), f_score(0), g_score(0), h_score(0), parent_row(0), parent_col(0)
354 		{}
355 
PathNode(int16 r,int16 c)356 	PathNode(int16 r, int16 c) : row(r), col(c), f_score(0), g_score(0), h_score(0), parent_row(0), parent_col(0)
357 		{}
358 
359 	//! \brief Overloaded comparison operator checks that row and col members are equal
360 	bool operator==(const PathNode& that) const
361 		{ return ((this->row == that.row) && (this->col == that.col)); }
362 
363 	//! \brief Overloaded comparison operator checks that row or col members are unequal
364 	bool operator!=(const PathNode& that) const
365 		{ return ((this->row != that.row) || (this->col != that.col)); }
366 
367 	//! \brief Overloaded comparison operator only used for path finding, compares the two f_scores
368 	bool operator<(const PathNode& that) const
369 		{ return this->f_score > that.f_score; }
370 }; // class PathNode
371 
372 } // namespace private_map
373 
374 } // namespace hoa_map
375 
376 #endif // __MAP_UTILS_HEADER__
377