xoreos  0.0.5
game.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include <algorithm>
28 
29 #include "src/common/filepath.h"
30 #include "src/common/filelist.h"
31 #include "src/common/configman.h"
32 
33 #include "src/aurora/resman.h"
34 
35 #include "src/events/events.h"
36 
37 #include "src/sound/sound.h"
38 
40 
41 #include "src/engines/nwn/game.h"
42 #include "src/engines/nwn/nwn.h"
45 #include "src/engines/nwn/module.h"
46 #include "src/engines/nwn/area.h"
47 
50 
52 
53 namespace Engines {
54 
55 namespace NWN {
56 
57 Game::Game(NWNEngine &engine, ::Engines::Console &console, const Version &version) :
58  _engine(&engine), _console(&console), _version(&version) {
59 
60  _functions.reset(new Functions(*this));
61 }
62 
64 }
65 
66 const Version &Game::getVersion() const {
67  return *_version;
68 }
69 
71  assert(_module);
72 
73  return *_module;
74 }
75 
76 void Game::run() {
77  bool first = true;
78 
79  _module.reset(new Module(*_console, *_version));
80 
81  while (!EventMan.quitRequested()) {
82  mainMenu(first, first);
83  runModule();
84  }
85 
86  _module.reset();
87 }
88 
90  if (EventMan.quitRequested() || !_module->isLoaded()) {
91  _module->clear();
92  return;
93  }
94 
95  EventMan.flushEvents();
96 
97  _module->enter();
98  EventMan.enableKeyRepeat(true);
99 
100  while (!EventMan.quitRequested() && _module->isRunning()) {
101  Events::Event event;
102  while (EventMan.pollEvent(event))
103  _module->addEvent(event);
104 
105  _module->processEventQueue();
106  EventMan.delay(10);
107  }
108 
109  EventMan.enableKeyRepeat(false);
110  _module->leave();
111 
112  _module->clear();
113 }
114 
116  stopMenuMusic();
117 
118  if (music.empty())
119  music = ConfigMan.getBool("NWN_hasXP2") ? "mus_x2theme" : "mus_theme_main";
120 
122 }
123 
125  SoundMan.stopChannel(_menuMusic);
126 }
127 
128 void Game::playMusic(const Common::UString &music) {
129  if (_module && _module->isRunning()) {
130  Area *area = _module->getCurrentArea();
131  if (area)
132  area->playAmbientMusic(music);
133 
134  return;
135  }
136 
137  playMenuMusic(music);
138 }
139 
141  stopMenuMusic();
142 
143  if (_module && _module->isRunning()) {
144  Area *area = _module->getCurrentArea();
145  if (area)
146  area->stopAmbientMusic();
147  }
148 }
149 
150 void Game::mainMenu(bool playStartSound, bool showLegal) {
151  playMenuMusic();
152 
153  if (playStartSound)
154  playSound("gui_prompt", Sound::kSoundTypeSFX);
155 
156  EventMan.flushEvents();
157 
158  MainMenu menu(*_module, _console);
159 
160  if (showLegal) {
161  // Fade in, show and fade out the legal billboard
162 
163  Legal legal;
164 
165  legal.fadeIn();
166  menu.show();
167  legal.show();
168  } else
169  menu.show();
170 
171  _console->disableCommand("loadcampaign", "not available in the main menu");
172  _console->disableCommand("loadmodule" , "not available in the main menu");
173  _console->disableCommand("exitmodule" , "not available in the main menu");
174  _console->disableCommand("listareas" , "not available in the main menu");
175  _console->disableCommand("gotoarea" , "not available in the main menu");
176 
177  menu.run();
178 
179  _console->enableCommand("loadcampaign");
180  _console->enableCommand("loadmodule");
181  _console->enableCommand("exitmodule");
182  _console->enableCommand("listareas");
183  _console->enableCommand("gotoarea");
184 
185  menu.hide();
186 
187  stopMenuMusic();
188 }
189 
190 void Game::getCharacters(std::vector<Common::UString> &characters, bool local) {
191  characters.clear();
192 
193  Common::UString pcDir = ConfigMan.getString(local ? "NWN_localPCDir" : "NWN_serverPCDir");
194  if (pcDir.empty())
195  return;
196 
197  Common::FileList chars;
198  chars.addDirectory(pcDir);
199 
200  for (Common::FileList::const_iterator c = chars.begin(); c != chars.end(); ++c) {
202  continue;
203 
204  characters.push_back(Common::FilePath::getStem(*c));
205  }
206 
207  std::sort(characters.begin(), characters.end(), Common::UString::iless());
208 }
209 
210 void Game::getModules(std::vector<Common::UString> &modules) {
211  modules.clear();
212 
213  Common::UString moduleDir = ConfigMan.getString("NWN_extraModuleDir");
214  if (moduleDir.empty())
215  return;
216 
217  Common::FileList mods;
218  mods.addDirectory(moduleDir);
219 
220  for (Common::FileList::const_iterator m = mods.begin(); m != mods.end(); ++m) {
222  continue;
223 
224  modules.push_back(Common::FilePath::getStem(*m));
225  }
226 
227  std::sort(modules.begin(), modules.end(), Common::UString::iless());
228 }
229 
230 void Game::getPremiumModules(std::vector<Common::UString> &modules) {
231  modules.clear();
232 
233  Common::FileList mods(ConfigMan.getString("NWN_campaignDir"));
234 
235  for (Common::FileList::const_iterator m = mods.begin(); m != mods.end(); ++m)
236  if (isPremiumModule(*m))
237  modules.push_back(Common::FilePath::getStem(*m));
238 
239  std::sort(modules.begin(), modules.end(), Common::UString::iless());
240 }
241 
243  std::vector<Common::UString> modules;
244  getPremiumModules(modules);
245 
246  return !modules.empty();
247 }
248 
250  static const char * const kPremiumModules[] = {
251  "Neverwinter Nights - Infinite Dungeons.nwm",
252  "Neverwinter Nights - Kingmaker.nwm",
253  "Neverwinter Nights - Pirates of the Sword Coast.nwm",
254  "Neverwinter Nights - ShadowGuard.nwm",
255  "Neverwinter Nights - Witch's Wake.nwm",
256  "Neverwinter Nights - Wyvern Crown of Cormyr.nwm"
257  };
258 
259  for (size_t i = 0; i < ARRAYSIZE(kPremiumModules); i++)
260  if (Common::FilePath::getFile(module).equalsIgnoreCase(kPremiumModules[i]))
261  return true;
262 
263  return false;
264 }
265 
267  const Common::UString nwmFile = module + ".nwm";
268  const Common::UString modFile = module + ".mod";
269 
270  if (ResMan.hasArchive(module + ".nwm")) {
271  module = nwmFile;
272  return true;
273  }
274 
275  if (ResMan.hasArchive(module + ".mod")) {
276  module = modFile;
277  return true;
278  }
279 
280  return false;
281 }
282 
283 } // End of namespace NWN
284 
285 } // End of namespace Engines
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
void show()
Show the GUI.
Definition: main.cpp:108
Engine class handling Neverwinter Nights.
Common::ScopedPtr< Functions > _functions
Definition: game.h:83
A class holding an UTF-8 string.
Definition: ustring.h:48
static UString getExtension(const UString &p)
Return a file name&#39;s extension.
Definition: filepath.cpp:93
The global config manager.
static void getCharacters(std::vector< Common::UString > &characters, bool local)
Return a list of local player characters.
Definition: game.cpp:190
static void getModules(std::vector< Common::UString > &modules)
Return a list of all modules.
Definition: game.cpp:210
Game(NWNEngine &engine, ::Engines::Console &console, const Version &version)
Definition: game.cpp:57
bool equalsIgnoreCase(const UString &str) const
Definition: ustring.cpp:218
std::list< UString >::const_iterator const_iterator
Definition: filelist.h:37
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
Neverwinter Nights installation version detection.
An area in Neverwinter Nights, holding all objects and room tiles within, as well as general area pro...
Definition: area.h:63
void stopMenuMusic()
Definition: game.cpp:124
void playMusic(const Common::UString &music="")
Overwrite all currently playing music.
Definition: game.cpp:128
uint32 run(uint32 startCode=kStartCodeNone)
Run the GUI.
Definition: gui.cpp:94
The NWN main menu.
Definition: main.h:39
const Version * _version
Definition: game.h:87
void playAmbientMusic(Common::UString music="")
Play the specified music (or the area&#39;s default) as ambient music.
Definition: area.cpp:195
SDL_Event Event
Definition: types.h:42
static bool hasModule(Common::UString &module)
Does this module exist?
Definition: game.cpp:266
The context needed to run a Neverwinter Nights module.
static void getPremiumModules(std::vector< Common::UString > &modules)
Return a list of all premium modules.
Definition: game.cpp:230
void disableCommand(const Common::UString &cmd, const Common::UString &reason="")
Definition: console.cpp:972
The context holding a Neverwinter Nights area.
Neverwinter Nights (debug) console.
void playMenuMusic(Common::UString music="")
Definition: game.cpp:115
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
Sound::ChannelHandle _menuMusic
Definition: game.h:89
The global events manager.
The main menu.
The global sound manager, handling all sound output.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void mainMenu(bool playStartSound, bool showLegal)
Definition: game.cpp:150
#define SoundMan
Shortcut for accessing the sound manager.
Definition: sound.h:293
const Version & getVersion() const
Definition: game.cpp:66
static bool hasPremiumModules()
Do we actually have any premium modules installed at all?
Definition: game.cpp:242
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
Sound::ChannelHandle playSound(const Common::UString &sound, Sound::SoundType soundType, bool loop, float volume, bool pitchVariance)
Play this sound resource.
Definition: util.cpp:81
void enableCommand(const Common::UString &cmd)
Definition: console.cpp:983
static UString getStem(const UString &p)
Return a file name&#39;s stem.
Definition: filepath.cpp:87
A list of files.
Definition: filelist.h:35
const_iterator begin() const
Return a const_iterator pointing to the beginning of the list.
Definition: filelist.cpp:94
bool addDirectory(const UString &directory, int recurseDepth=0)
Add a directory to the list.
Definition: filelist.cpp:102
Sound effect.
Definition: types.h:45
Generic Aurora engines utility functions.
A list of files.
void stopAmbientMusic()
Stop the ambient music.
Definition: area.cpp:187
Common::ScopedPtr< Module > _module
Definition: game.h:82
The context handling the gameplay in Neverwinter Nights.
::Engines::Console * _console
Definition: game.h:85
static bool isPremiumModule(const Common::UString &module)
Is this module file (including extension) a premium module?
Definition: game.cpp:249
void runModule()
Definition: game.cpp:89
The NWN legal billboard.
Definition: legal.h:37
void stopMusic()
Force all currently playing music stopped.
Definition: game.cpp:140
static UString getFile(const UString &p)
Return a file name without its path.
Definition: filepath.cpp:81
Neverwinter Nights engine functions.
Module & getModule()
Return the module context.
Definition: game.cpp:70
virtual void hide()
Hide the GUI.
Definition: gui.cpp:80
The global resource manager for Aurora resources.
Utility class for manipulating file paths.
const_iterator end() const
Return a const_iterator pointing past the end of the list.
Definition: filelist.cpp:98