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.h
12 *** \author  Tyler Olsen, roots@allacrost.org
13 *** \brief   Header file for map mode interface.
14 ***
15 *** This file contains the interface for map mode, active when the player is
16 *** exploring town or dungeon maps. The map environments of Allacrost are
17 *** quite extensive, thus this code is responsible for processing many things.
18 *** This includes handling all tile images, objects, sprites, map events,
19 *** dialogue, and more.
20 ***
21 *** Each individual map is represented by it's own object of the MapMode class.
22 *** It is our intention in the future to retain more than one map in memory at
23 *** once to reduce or eliminate loading times when the player transitions
24 *** between maps.
25 *** ***************************************************************************/
26 
27 #ifndef __MAP_HEADER__
28 #define __MAP_HEADER__
29 
30 // Allacrost utilities
31 #include "defs.h"
32 #include "utils.h"
33 
34 // Allacrost engines
35 #include "audio.h"
36 #include "mode_manager.h"
37 #include "script.h"
38 #include "video.h"
39 #include "system.h"
40 
41 // Local map mode headers
42 #include "map_utils.h"
43 
44 //! All calls to map mode are wrapped in this namespace.
45 namespace hoa_map {
46 
47 //! An internal namespace to be used only within the map code. Don't use this namespace anywhere else!
48 namespace private_map {
49 
50 } // namespace private_map
51 
52 /** ****************************************************************************
53 *** \brief Handles the game execution while the player is exploring maps.
54 ***
55 *** This class contains all of the structures that together compose each map.
56 *** Each map has a Lua script file in which the map data is permanently retained
57 *** and various script subroutines exist that modify the map's behavior. Keep in
58 *** mind that this class alone does not represent all of the data nor all of the
59 *** code that is used in a particular map, as the map's Lua file likely retains
60 *** additional information about the map that is not represented in this class.
61 ***
62 *** Maps are composed by a series of tiles and objects. Tiles are 32x32 pixel
63 *** squares that are adjacent to one another on a map, and together make up the
64 *** map's background environment. Objects are variable sized entities that are
65 *** usually living, animated creatures (sprites), but may be something static
66 *** such as a large tree. Tiles and objects are drawn in multiple interwieving
67 *** layers to emulate a 3D environment for the game. Additionally each map has
68 *** a collision grid composed of 16x16 pixel elements that determine which
69 *** quadrants of each tile may be occupied by sprites or other objects.
70 ***
71 *** Another important concept to MapMode is that of contexts. Each map has at
72 *** least one context and can have up to a maximum of 32 contexts. Every context
73 *** has its own collision grid and its own set of tiles. Map objects and sprites
74 *** exist in one of these context and may change their context at any time.
75 *** Objects and sprites can not interact with one another when they are not
76 *** within the same context, and typically only objects that are in the same
77 *** context as the map camera are visible on the screen. You can think of each
78 *** context as essentially its own map, and the set of contexts as a set of maps
79 *** that work with one another to create a cohesive environment.
80 ***
81 *** Because this game mode is so complex, the MapMode class shares its
82 *** responsibilities with several small singleton classes that manage a
83 *** particular area of map code, such as tiles or objects. These sub-manager
84 *** classes should be viewed as extensions of the MapMode class.
85 *** ***************************************************************************/
86 class MapMode : public hoa_mode_manager::GameMode {
87 	friend void hoa_defs::BindModesToLua();
88 
89 public:
90 	//! \param filename The name of the Lua file that retains all data about the map to create
91 	MapMode(std::string filename);
92 
93 	~MapMode();
94 
95 	//! \brief Resets appropriate class members. Called whenever the MapMode object is made the active game mode.
96 	void Reset();
97 
98 	//! \brief Updates the game and calls various sub-update functions depending on the current state of map mode.
99 	void Update();
100 
101 	//! \brief The highest level draw function that will call the appropriate lower-level draw functions
102 	void Draw();
103 
104 	// The methods below this line are not intended to be used outside of the map code
105 
106 	//! \brief Empties the state stack and places an invalid state on top
107 	void ResetState();
108 
109 	/** \brief Pushes a state type to the top of the state stack, making it the active state
110 	*** \param state The state to push onto the stack
111 	**/
112 	void PushState(private_map::MAP_STATE state);
113 
114 	//! \brief Removes the highest item in the state stack
115 	void PopState();
116 
117 	/** \brief Retrieves the current map state
118 	*** \return The top-most item on the map state stack
119 	**/
120 	private_map::MAP_STATE CurrentState();
121 
PlayNewMusic(uint32 n)122 	void PlayNewMusic(uint32 n)
123 		{ _music.at(n).Play(); }
124 
125 	/** \brief Opens the map tablespace of the map script
126 	*** \param use_global Has the same effect as in ReadScriptDescriptor#OpenTable
127 	**/
128 	void OpenMapTablespace(bool use_global = false)
129 		{ _map_script.OpenTable(_map_tablespace, use_global); }
130 
131 	//! \brief Adds a new object to the ground object layer
132 	void AddGroundObject(private_map::MapObject *obj);
133 
134 	//! \brief Adds a new object to the pass object layer
135 	void AddPassObject(private_map::MapObject *obj);
136 
137 	//! \brief Adds a new object to the sky object layer
138 	void AddSkyObject(private_map::MapObject *obj);
139 
140 	//! \brief Adds a new zone to the map
141 	void AddZone(private_map::MapZone *zone);
142 
143 	/** \brief Checks if a GlobalEnemy with the specified id is already loaded in the MapMode#_enemies container
144 	*** \param id The id of the enemy to find
145 	*** \return True if the enemy is loaded
146 	**/
147 	bool IsEnemyLoaded(uint32 id) const;
148 
149 	//! \brief Class member accessor functions
150 	//@{
CurrentInstance()151 	static MapMode* const CurrentInstance()
152 		{ return _current_instance; }
153 
GetMapName()154 	const hoa_utils::ustring& GetMapName() const
155 		{ return _map_name; }
156 
GetMapEventGroup()157 	hoa_global::GlobalEventGroup* GetMapEventGroup() const
158 		{ return _map_event_group; }
159 
GetMapScript()160 	hoa_script::ReadScriptDescriptor& GetMapScript()
161 		{ return _map_script; }
162 
GetTileSupervisor()163 	private_map::TileSupervisor* GetTileSupervisor() const
164 		{ return _tile_supervisor; }
165 
GetObjectSupervisor()166 	private_map::ObjectSupervisor* GetObjectSupervisor() const
167 		{ return _object_supervisor; }
168 
GetEventSupervisor()169 	private_map::EventSupervisor* GetEventSupervisor() const
170 		{ return _event_supervisor; }
171 
GetDialogueSupervisor()172 	private_map::DialogueSupervisor* GetDialogueSupervisor() const
173 		{ return _dialogue_supervisor; }
174 
GetTreasureSupervisor()175 	private_map::TreasureSupervisor* GetTreasureSupervisor() const
176 		{ return _treasure_supervisor; }
177 
GetMapFrame()178 	const private_map::MapFrame& GetMapFrame() const
179 		{ return _map_frame; }
180 
GetCamera()181 	private_map::VirtualSprite* GetCamera() const
182 		{ return _camera; }
183 
SetCamera(private_map::VirtualSprite * sprite)184 	void SetCamera(private_map::VirtualSprite* sprite)
185 		{ _camera = sprite; }
186 
GetNumMapContexts()187 	uint8 GetNumMapContexts() const
188 		{ return _num_map_contexts; }
189 
GetCurrentContext()190 	private_map::MAP_CONTEXT GetCurrentContext() const
191 		{ return _current_context; }
192 
IsShowDialogueIcons()193 	bool IsShowDialogueIcons() const
194 		{ return _show_dialogue_icons; }
195 
SetShowDialogueIcons(bool state)196 	void SetShowDialogueIcons(bool state)
197 		{ _show_dialogue_icons = state; }
198 
GetDialogueIcon()199 	const hoa_video::AnimatedImage& GetDialogueIcon() const
200 		{ return _dialogue_icon; }
201 
GetLocationGraphic()202 	const hoa_video::StillImage& GetLocationGraphic() const
203 		{ return _location_graphic; }
204 	//@}
205 
206 	void PlayMusic(uint32 track_num);
207 
208 private:
209 	// ----- Members : Names and Identifiers -----
210 
211 	/** \brief A reference to the current instance of MapMode
212 	*** This is used by other map clases to be able to refer to the map that they exist in.
213 	**/
214 	static MapMode* _current_instance;
215 
216 	//! \brief The name of the Lua file that represents the map
217 	std::string _map_filename;
218 
219 	/** \brief The map's unique name as it is used to identify a Lua namespace table
220 	*** To avoid Lua naming conflicts between multiple map files, all map data is encompassed within
221 	*** a namespace (a Lua table) that is unique to each map.
222 	**/
223 	std::string _map_tablespace;
224 
225 	//! \brief The name of the map, as it will be read by the player in the game.
226 	hoa_utils::ustring _map_name;
227 
228 	//! \brief A pointer to the object containing all of the event information for the map
229 	hoa_global::GlobalEventGroup* _map_event_group;
230 
231 	/** \brief The interface to the file which contains all the map's stored data and subroutines.
232 	*** This class generally performs a large amount of communication with this script continuously.
233 	*** The script remains open for as long as the MapMode object exists.
234 	**/
235 	hoa_script::ReadScriptDescriptor _map_script;
236 
237 	// ----- Members : Supervisor Class Objects and Script Functions -----
238 
239 	//! \brief Instance of helper class to map mode. Responsible for tile related operations.
240 	private_map::TileSupervisor* _tile_supervisor;
241 
242 	//! \brief Instance of helper class to map mode. Responsible for object and sprite related operations.
243 	private_map::ObjectSupervisor* _object_supervisor;
244 
245 	//! \brief Instance of helper class to map mode. Responsible for updating and managing active map events.
246 	private_map::EventSupervisor* _event_supervisor;
247 
248 	//! \brief Instance of helper class to map mode. Responsible for dialogue execution and display operations.
249 	private_map::DialogueSupervisor* _dialogue_supervisor;
250 
251 	//! \brief Class member object which processes all information related to treasure discovery
252 	private_map::TreasureSupervisor* _treasure_supervisor;
253 
254 	/** \brief A script function which assists with the MapMode#Update method
255 	*** This function implements any custom update code that the specific map needs to be performed.
256 	*** The most common operation that this script function performs is to check for trigger conditions
257 	*** that cause map events to occur
258 	**/
259 	ScriptObject _update_function;
260 
261 	/** \brief Script function which assists with the MapMode#Draw method
262 	*** This function allows for drawing of custom map visuals. Usually this includes lighting or
263 	*** other visual effects for the map environment.
264 	**/
265 	ScriptObject _draw_function;
266 
267 	// ----- Members : Properties and State -----
268 
269 	//! \brief Retains information needed to correctly draw the next map frame
270 	private_map::MapFrame _map_frame;
271 
272 	//! \brief A pointer to the map sprite that the map camera will focus on
273 	private_map::VirtualSprite* _camera;
274 
275 	//! \brief The number of contexts that this map uses (at least 1, at most 32)
276 	uint8 _num_map_contexts;
277 
278 	/** \brief The currently active map context
279 	*** This is always equal to the context of the object pointed to by the _camera member
280 	**/
281 	private_map::MAP_CONTEXT _current_context;
282 
283 	/** \brief Maintains a stack state for the different modes of operation that the map may be in
284 	*** The top (back) of the stack is the active mode
285 	**/
286 	std::vector<private_map::MAP_STATE> _state_stack;
287 
288 	//! \brief While true, all user input commands to map mode are ignored
289 	bool _ignore_input;
290 
291 	//! \brief While true, the player is not allowed to run at all.
292 	bool _run_disabled;
293 
294 	//! \brief If true, the player's stamina will not drain as they run
295 	bool _run_forever;
296 
297 	/** \brief A counter for the player's stamina
298 	*** This value ranges from 0 (empty) to 10000 (full). Stamina takes 10 seconds to completely fill from
299 	*** the empty state and 5 seconds to empty from the full state.
300 	**/
301 	uint32 _run_stamina;
302 
303 	//! \brief Indicates if dialogue icons should be drawn above NPCs who have unread dialogue
304 	bool _show_dialogue_icons;
305 
306 	//! \brief Index of current music track to play
307 	uint32 _current_track;
308 
309 	// ----- Members : Timing and Graphics -----
310 
311 	/** \brief A timer used for when the player first enters the map
312 	*** This timer is set to 7000 ms (7 seconds) and is used to display the map's location graphic
313 	*** and name at the top center of the screen. The graphic and text are faded in for the first
314 	*** two seconds, drawn opaquely for the next three seconds, and faded out in the final two seconds.
315 	**/
316 	hoa_system::SystemTimer _intro_timer;
317 
318 	//! \brief Freestyle art image of the current map
319 	hoa_video::StillImage _location_graphic;
320 
321 	//! \brief An icon graphic which appears over the heads of NPCs who have dialogue that has not been read by the player
322 	hoa_video::AnimatedImage _dialogue_icon;
323 
324 	//! \brief Image which underlays the stamina bar for running
325 	hoa_video::StillImage _stamina_bar_background;
326 
327 	//! \brief Image which overlays the stamina bar to show that the player has unlimited running
328 	hoa_video::StillImage _stamina_bar_infinite_overlay;
329 
330 	// ----- Members : Containers -----
331 
332 	//! \brief The music that the map will need to make use of
333 	std::vector<hoa_audio::MusicDescriptor> _music;
334 
335 	//! \brief The sounds that the map needs available to it
336 	std::vector<hoa_audio::SoundDescriptor> _sounds;
337 
338 	/** \brief A container for the various foes which may appear on this map
339 	*** These enemies are kept at their initial stats. When they are passed to battle mode,
340 	*** a copy of each enemy is made and initialized there.
341 	**/
342 	std::vector<hoa_global::GlobalEnemy*> _enemies;
343 
344 	// ----- Methods -----
345 
346 	//! \brief Loads all map data contained in the Lua file that defines the map
347 	void _Load();
348 
349 	//! \brief A helper function to Update() that is called only when the map is in the explore state
350 	void _UpdateExplore();
351 
352 	//! \brief A helper function to Update() that is called only when the map is in the dialogue state
353 	void _UpdateDialogue();
354 
355 	//! \brief Calculates information about how to draw the next map frame
356 	void _CalculateMapFrame();
357 
358 	//! \brief Draws all visible map tiles and sprites to the screen
359 	void _DrawMapLayers();
360 
361 	//! \brief Draws all GUI visuals, such as dialogue icons and the stamina bar
362 	void _DrawGUI();
363 }; // class MapMode
364 
365 } // namespace hoa_map;
366 
367 #endif
368