1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See https://www.gnu.org/copyleft/gpl.html for details.
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 /** ****************************************************************************
12 *** \file    map_utils.h
13 *** \author  Tyler Olsen, roots@allacrost.org
14 *** \author  Yohann Ferreira, yohann ferreira orange fr
15 *** \brief   Header file for map mode utility code
16 *** *****************************************************************************/
17 
18 #ifndef __MAP_UTILS_HEADER__
19 #define __MAP_UTILS_HEADER__
20 
21 #include "common/position_2d.h"
22 #include "common/rectangle_2d.h"
23 
24 #include "engine/video/image.h"
25 #include "engine/video/video_utils.h"
26 
27 #include <vector>
28 
29 namespace vt_map
30 {
31 
32 //! Determines whether the code in the vt_map namespace should print debug statements or not.
33 extern bool MAP_DEBUG;
34 
35 namespace private_map
36 {
37 
38 /** \name Screen Coordiante System Constants
39 *** \brief Represents the size of the visible screen in map tiles and the collision grid
40 *** Every map tile is 32x32 pixels, and every collision grid element is one quarter of that
41 *** area (16x16). Thus the number of collision grid elements that compose the screen are
42 *** four times the number of tiles that are visible on the screen. This also means the number
43 *** of rows and columns of grid elements that encompass the screen are twice that of the
44 *** number of rows and columns of tiles.
45 **/
46 //@{
47 // The zoom ratio will set how magnified (if > 1.0f) the map mode will display the tiles,
48 // sprites and objects.
49 const float MAP_ZOOM_RATIO = 2.0f;
50 
51 const float SCREEN_GRID_X_LENGTH = 64.0f / MAP_ZOOM_RATIO; // was 32 for zoom ratio 2.0f;
52 const float SCREEN_GRID_Y_LENGTH = 48.0f / MAP_ZOOM_RATIO;
53 const float HALF_SCREEN_GRID_X_LENGTH = SCREEN_GRID_X_LENGTH / 2.0f;
54 const float HALF_SCREEN_GRID_Y_LENGTH = SCREEN_GRID_Y_LENGTH / 2.0f;
55 
56 const uint16_t TILES_ON_X_AXIS = static_cast<uint16_t>(SCREEN_GRID_X_LENGTH / 2.0f); // Number of tile columns that fit on the screen
57 const uint16_t TILES_ON_Y_AXIS = static_cast<uint16_t>(SCREEN_GRID_Y_LENGTH / 2.0f); // Number of tile rows that fit on the screen
58 const uint16_t HALF_TILES_ON_X_AXIS = TILES_ON_X_AXIS / 2;
59 const uint16_t HALF_TILES_ON_Y_AXIS = TILES_ON_Y_AXIS / 2;
60 
61 // Length of a grid element in pixels (taken from the grid to screen width ratio)
62 const uint16_t GRID_LENGTH = vt_video::VIDEO_STANDARD_RES_WIDTH / SCREEN_GRID_X_LENGTH;
63 // Length of a tile in pixels
64 const uint16_t TILE_LENGTH = GRID_LENGTH * 2;
65 //@}
66 
67 /** \name Map State Enum
68 *** \brief Represents the current state of operation during map mode.
69 **/
70 //@{
71 enum MAP_STATE {
72     STATE_INVALID          = 0,
73     STATE_EXPLORE          = 1, //!< Standard state, player has control to move about the map
74     STATE_SCENE            = 2, //!< Like the explore state but player has no control (input is ignored)
75     STATE_DIALOGUE         = 3, //!< When a dialogue is active
76     STATE_TREASURE         = 4, //!< Active when a treasure has been procured by the player
77     STATE_ESCAPE           = 5, //!< Active when an escape point menu is active
78     STATE_TOTAL            = 6
79 };
80 //@}
81 
82 //! \brief The number of tiles that are found in a tileset image (512x512 pixel image containing 32x32 pixel tiles)
83 const uint32_t TILES_PER_TILESET = 256;
84 
85 //! \brief Used to identify the type of map object
86 enum MAP_OBJECT_TYPE {
87     OBJECT_TYPE   = -1, //! Default type
88     PHYSICAL_TYPE = 0,  //! This is a physical (visible) object, like a rock, a door, ect...
89     VIRTUAL_TYPE  = 1,  //! This is used as another type of NPC sprite, but without interaction.
90     SPRITE_TYPE   = 2,  //! This is a NPC sprite. Blocking for the hero, but not for the other sprites.
91     ENEMY_TYPE    = 3,  //! This is an enemy sprite. Blocking the hero, and triggering a battle.
92     TREASURE_TYPE = 4,  /** This is a treasure, can obtain a treasure from it when exploring,
93                         but blocking for movement. */
94     SAVE_TYPE     = 5,  //! This is a save point. The player can save while its character is in it.
95     ESCAPE_TYPE   = 6,  //! This is an escape point. The player can go back to the latest safe place through it.
96     TRIGGER_TYPE  = 7,  //! A trigger object.
97 
98     //! Objects without possible interaction
99     //! Those objects are ignored when searching for the nearest interactable object.
100     HALO_TYPE     = 8,  //! This is a source of light, here for eye candy.
101     LIGHT_TYPE    = 9,  //! Another light type, but will change dynamically according to the map viewpoint.
102     PARTICLE_TYPE = 10, //! A particle object.
103     SOUND_TYPE    = 11, //! An environmental sound
104     SCENERY_TYPE  = 12  //! The object is a scenery animal or harmless thing, and nothing should collide with.
105 };
106 
107 /** \name Map Sprite Speeds
108 *** \brief Common speeds for sprite movement.
109 *** These values are the time (in milliseconds) that it takes a sprite to walk
110 *** the distance of one map grid (16 pixels).
111 **/
112 //@{
113 const float VERY_SLOW_SPEED  = 225.0f;
114 const float SLOW_SPEED       = 190.0f;
115 const float NORMAL_SPEED     = 150.0f;
116 // Slightly higher than the normal speed, but less than a running character.
117 const float ENEMY_SPEED      = 130.0f;
118 const float FAST_SPEED       = 110.0f;
119 const float VERY_FAST_SPEED  = 75.0f;
120 //@}
121 
122 /** \name Sprite Direction Constants
123 *** \brief Constants used for determining sprite directions
124 *** Sprites are allowed to travel in eight different directions, however the sprite itself
125 *** can only be facing one of four ways: north, south, east, or west. Because of this, it
126 *** is possible to travel, for instance, northwest facing north <i>or</i> northwest facing west.
127 *** The "NW_NORTH" constant means that the sprite is traveling to the northwest and is
128 *** facing towards the north.
129 ***
130 *** \note These constants include a series of shorthands (MOVING_NORTHWEST, FACING_NORTH) used
131 *** to check for movement and facing directions.
132 **/
133 //@{
134 const uint16_t NORTH     = 0x0001;
135 const uint16_t SOUTH     = 0x0002;
136 const uint16_t WEST      = 0x0004;
137 const uint16_t EAST      = 0x0008;
138 const uint16_t NW_NORTH  = 0x0010;
139 const uint16_t NW_WEST   = 0x0020;
140 const uint16_t NE_NORTH  = 0x0040;
141 const uint16_t NE_EAST   = 0x0080;
142 const uint16_t SW_SOUTH  = 0x0100;
143 const uint16_t SW_WEST   = 0x0200;
144 const uint16_t SE_SOUTH  = 0x0400;
145 const uint16_t SE_EAST   = 0x0800;
146 // Used to check for movement direction regardless of facing direction
147 const uint16_t MOVING_NORTHWARD = NORTH | NW_NORTH | NW_WEST | NE_NORTH | NE_EAST;
148 const uint16_t MOVING_SOUTHWARD = SOUTH | SW_SOUTH | SW_WEST | SE_SOUTH | SE_EAST;
149 const uint16_t MOVING_EASTWARD = EAST | NE_EAST | NE_NORTH | SE_EAST | SE_SOUTH;
150 const uint16_t MOVING_WESTWARD = WEST | NW_WEST | NW_NORTH | SW_WEST | SW_SOUTH;
151 const uint16_t MOVING_NORTHWEST = NW_NORTH | NW_WEST;
152 const uint16_t MOVING_NORTHEAST = NE_NORTH | NE_EAST;
153 const uint16_t MOVING_SOUTHWEST = SW_SOUTH | SW_WEST;
154 const uint16_t MOVING_SOUTHEAST = SE_SOUTH | SE_EAST;
155 const uint16_t MOVING_ORTHOGONALLY = NORTH | SOUTH | EAST | WEST;
156 const uint16_t MOVING_DIAGONALLY = MOVING_NORTHWEST | MOVING_NORTHEAST | MOVING_SOUTHWEST | MOVING_SOUTHEAST;
157 // Used to check for facing direction regardless of moving direction
158 const uint16_t FACING_NORTH = NORTH | NW_NORTH | NE_NORTH;
159 const uint16_t FACING_SOUTH = SOUTH | SW_SOUTH | SE_SOUTH;
160 const uint16_t FACING_WEST = WEST | NW_WEST | SW_WEST;
161 const uint16_t FACING_EAST = EAST | NE_EAST | SE_EAST;
162 //@}
163 
164 /** \name Map Sprite Animation Constants
165 *** These constants are used to index the MapSprite#animations vector to display the correct
166 *** animation depending on the given direction.
167 **/
168 //@{
169 enum ANIM_DIRECTIONS {
170     ANIM_SOUTH = 0,
171     ANIM_NORTH = 1,
172     ANIM_WEST  = 2,
173     ANIM_EAST  = 3,
174     NUM_ANIM_DIRECTIONS = 4
175 };
176 //@}
177 
178 /** \brief Represents the various types of collisions which may occur for a sprite
179 *** NOTE: Used as a bitmask.
180 **/
181 enum COLLISION_TYPE {
182     NO_COLLISION =        0, //!< Indicates that no collision has occurred.
183     CHARACTER_COLLISION = 1, //!< Indicates that a collision with a character.
184     ENEMY_COLLISION =     2, //!< Indicates that a collision with an enemy.
185     WALL_COLLISION =      4, //! Indicates a collision with a wall. (Sky objects will have a wall collision on the map bounds)
186     COLLISION_TYPES =     4
187 };
188 const uint32_t ALL_COLLISION = CHARACTER_COLLISION | ENEMY_COLLISION | WALL_COLLISION;
189 
190 //! \brief Identifiers for the similarly named classes of map events
191 enum EVENT_TYPE {
192     INVALID_EVENT                   = 0,
193     DIALOGUE_EVENT                  = 1,
194     SHOP_EVENT                      = 2,
195     SOUND_EVENT                     = 3,
196     MAP_TRANSITION_EVENT            = 4,
197     JOIN_PARTY_EVENT                = 5,
198     BATTLE_ENCOUNTER_EVENT          = 6,
199     SCRIPTED_EVENT                  = 7,
200     SCRIPTED_SPRITE_EVENT           = 8,
201     CHANGE_DIRECTION_SPRITE_EVENT   = 9,
202     PATH_MOVE_SPRITE_EVENT          = 10,
203     RANDOM_MOVE_SPRITE_EVENT        = 11,
204     ANIMATE_SPRITE_EVENT            = 12,
205     TREASURE_EVENT                  = 13,
206     LOOK_AT_SPRITE_EVENT            = 14,
207     IF_EVENT                        = 15,
208     TOTAL_EVENT                     = 16
209 };
210 
211 //! \brief The number of milliseconds to take to fade out the map
212 const uint32_t MAP_FADE_OUT_TIME = 800;
213 
214 //! \brief This function adapts the images to the map scale.
215 void ScaleToMapZoomRatio(vt_video::ImageDescriptor& img);
216 
217 /** \brief Returns the opposite facing direction of the direction given in parameter.
218 *** \return A direction that faces opposite to the argument direction
219 *** \note This is mostly used as an helper function to make sprites face each other in a conversation.
220 **/
221 uint16_t GetOppositeDirection(const uint16_t direction);
222 
223 /** ****************************************************************************
224 *** \brief Retains information about how the next map frame should be drawn.
225 ***
226 *** This class is used by the MapMode class to determine how the next map frame
227 *** should be drawn. This includes which tiles will be visible and the offset
228 *** coordinates for the screen. Map objects also use this information to determine
229 *** where (and if) they should be drawn.
230 ***
231 *** \note The MapMode class keeps an active object of this class with the latest
232 *** information about the map. It should be the only instance of this class that is
233 *** needed.
234 *** ***************************************************************************/
235 class MapFrame
236 {
237 public:
238     //! \brief The x and y indeces of the starting tile to draw (the top-left tile).
239     int16_t tile_x_start, tile_y_start;
240 
241     //! \brief The x and y position screen coordinates to start drawing tiles from.
242     vt_common::Position2D tile_offset;
243 
244     //! \brief The number of tiles to draw on the x and y axes on the screen.
245     uint8_t num_draw_x_axis, num_draw_y_axis;
246 
247     /** \brief The position coordinates of the screen edges.
248     *** These members are in terms of the map grid 16x16 pixel coordinates that map objects use.
249     *** The presense of these coordinates make it easier for map objects to figure out whether or
250     *** not they should be drawn on the screen. Note that these are <b>not</b> used as drawing
251     *** cursor positions, but rather are map grid coordinates indicating where the screen edges lie.
252     **/
253     vt_common::Rectangle2D screen_edges;
254 }; // class MapFrame
255 
256 
257 /** ****************************************************************************
258 *** \brief A container class for node information in pathfinding.
259 ***
260 *** This class is used in the MapMode#_FindPath function to find an optimal
261 *** path from a given source to a destination. The path finding algorithm
262 *** employed is A* and thus many members of this class are particular to the
263 *** implementation of that algorithm.
264 *** ***************************************************************************/
265 class PathNode
266 {
267 public:
268     /** \brief The grid coordinates for this node
269     *** These coordinates correspond to the collision grid, where each element
270     *** is a 16x16 pixel space on the map.
271     **/
272     int16_t tile_x, tile_y;
273 
274     //! \name Path Scoring Members
275     //@{
276     //! \brief The total score for this node (f = g + h).
277     int16_t f_score;
278 
279     //! \brief The score for this node relative to the source.
280     int16_t g_score;
281 
282     //! \brief The Manhattan distance from this node to the destination.
283     int16_t h_score;
284     //@}
285 
286     //! \brief The grid coordinates for the parent of this node
287     int16_t parent_x, parent_y;
288 
289     // ---------- Methods
290 
PathNode()291     PathNode() : tile_x(-1), tile_y(-1), f_score(0), g_score(0), h_score(0), parent_x(0), parent_y(0)
292     {}
293 
PathNode(int16_t x_,int16_t y_)294     PathNode(int16_t x_, int16_t y_) : tile_x(x_), tile_y(y_), f_score(0), g_score(0), h_score(0), parent_x(0), parent_y(0)
295     {}
296 
297     //! \brief Overloaded comparison operator, only checks that the tile_x and tile_y members are equal
298     bool operator==(const PathNode &that) const {
299         return ((this->tile_x == that.tile_x) && (this->tile_y == that.tile_y));
300     }
301 
302     //! \brief Overloaded comparison operator, only checks that the tile_x or tile_y members are unequal
303     bool operator!=(const PathNode &that) const {
304         return ((this->tile_x != that.tile_x) || (this->tile_y != that.tile_y));
305     }
306 
307     //! \brief Overloaded comparison operator only used for path finding, compares the two f_scores
308     bool operator<(const PathNode &that) const {
309         return this->f_score > that.f_score;
310     }
311 }; // class PathNode
312 
313 typedef std::vector<vt_common::Position2D> Path;
314 
315 } // namespace private_map
316 
317 } // namespace vt_map
318 
319 #endif // __MAP_UTILS_HEADER__
320