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/error.h"
30 #include "src/common/configman.h"
31 #include "src/common/filepath.h"
32 #include "src/common/filelist.h"
33 
34 #include "src/events/events.h"
35 
36 #include "src/engines/nwn2/game.h"
37 #include "src/engines/nwn2/nwn2.h"
41 #include "src/engines/nwn2/area.h"
42 
44 
45 namespace Engines {
46 
47 namespace NWN2 {
48 
49 Game::Game(NWN2Engine &engine, ::Engines::Console &console) :
50  _engine(&engine), _console(&console) {
51 
52  _functions.reset(new Functions(*this));
53 }
54 
56 }
57 
59  assert(_campaign);
60 
61  return *_campaign;
62 }
63 
65  assert(_campaign);
66 
67  return _campaign->getModule();
68 }
69 
70 void Game::run() {
71  _campaign.reset(new Campaign(*_console));
72 
73  while (!EventMan.quitRequested()) {
74  runCampaign();
75  }
76 
77  _campaign.reset();
78 }
79 
81  std::vector<Common::UString> characters;
82  getCharacters(characters, true);
83 
84  if (characters.empty())
85  throw Common::Exception("No characters in the localvault directory");
86 
87  _campaign->load("neverwinter nights 2 campaign");
88  _campaign->usePC(*characters.begin(), true);
89 
90  if (EventMan.quitRequested() || !_campaign->isLoaded()) {
91  _campaign->clear();
92  return;
93  }
94 
95  _campaign->enter();
96  EventMan.enableKeyRepeat(true);
97 
98  while (!EventMan.quitRequested() && _campaign->isRunning()) {
99  Events::Event event;
100  while (EventMan.pollEvent(event))
101  _campaign->addEvent(event);
102 
103  _campaign->processEventQueue();
104  EventMan.delay(10);
105  }
106 
107  EventMan.enableKeyRepeat(false);
108  _campaign->leave();
109 
110  _campaign->clear();
111 }
112 
113 void Game::playMusic(const Common::UString &music) {
114  if (!_campaign || !_campaign->getModule().isRunning())
115  return;
116 
117  Area *area = _campaign->getModule().getCurrentArea();
118  if (!area)
119  return;
120 
121  area->playAmbientMusic(music);
122 }
123 
125  if (!_campaign || !_campaign->getModule().isRunning())
126  return;
127 
128  Area *area = _campaign->getModule().getCurrentArea();
129  if (!area)
130  return;
131 
132  area->stopAmbientMusic();
133 }
134 
135 void Game::getCampaigns(std::vector<Common::UString> &campaigns) {
136  campaigns.clear();
137 
138  const Common::UString directory = ConfigMan.getString("NWN2_campaignDir");
139 
140  const Common::FileList camFiles(directory, -1);
141 
142  for (Common::FileList::const_iterator c = camFiles.begin(); c != camFiles.end(); ++c) {
143  if (!Common::FilePath::getFile(*c).equalsIgnoreCase("campaign.cam"))
144  continue;
145 
147  if (cam.empty() || (cam == "."))
148  continue;
149 
150  campaigns.push_back(cam);
151  }
152 
153  std::sort(campaigns.begin(), campaigns.end(), Common::UString::iless());
154 }
155 
156 void Game::getModules(std::vector<Common::UString> &modules) {
157  modules.clear();
158 
159  const Common::UString directory = ConfigMan.getString("NWN2_moduleDir");
160 
161  const Common::FileList modFiles(directory);
162 
163  for (Common::FileList::const_iterator m = modFiles.begin(); m != modFiles.end(); ++m) {
165  continue;
166 
167  modules.push_back(Common::FilePath::getStem(*m));
168  }
169 
170  std::sort(modules.begin(), modules.end(), Common::UString::iless());
171 }
172 
173 void Game::getCharacters(std::vector<Common::UString> &characters, bool local) {
174  characters.clear();
175 
176  Common::UString pcDir = ConfigMan.getString(local ? "NWN2_localPCDir" : "NWN2_serverPCDir");
177  if (pcDir.empty())
178  return;
179 
180  Common::FileList chars;
181  chars.addDirectory(pcDir);
182 
183  for (Common::FileList::const_iterator c = chars.begin(); c != chars.end(); ++c) {
185  continue;
186 
187  characters.push_back(Common::FilePath::getStem(*c));
188  }
189 
190  std::sort(characters.begin(), characters.end(), Common::UString::iless());
191 }
192 
193 } // End of namespace NWN2
194 
195 } // End of namespace Engines
The context handling the gameplay in Neverwinter Nights 2.
Neverwinter Nights 2 (debug) console.
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.
void playAmbientMusic(Common::UString music="")
Play the specified music (or the area&#39;s default) as ambient music.
Definition: area.cpp:190
An area in Neverwinter Nights 2, holding all objects, room tiles and terrain within, as well as general area properties like the current background music and ambient sounds.
Definition: area.h:65
The context holding a Neverwinter Nights 2 campaign.
The context holding a Neverwinter Nights 2 area.
bool equalsIgnoreCase(const UString &str) const
Definition: ustring.cpp:218
static void getCampaigns(std::vector< Common::UString > &campaigns)
Return a list of all campaigns.
Definition: game.cpp:135
std::list< UString >::const_iterator const_iterator
Definition: filelist.h:37
::Engines::Console * _console
Definition: game.h:79
Exception that provides a stack of explanations.
Definition: error.h:36
SDL_Event Event
Definition: types.h:42
The context needed to run a Neverwinter Nights 2 module.
Basic exceptions to throw.
Neverwinter Nights 2 engine functions.
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
The global events manager.
void playMusic(const Common::UString &music="")
Overwrite all currently playing music.
Definition: game.cpp:113
static void getModules(std::vector< Common::UString > &modules)
Return a list of all modules.
Definition: game.cpp:156
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
Common::ScopedPtr< Functions > _functions
Definition: game.h:77
static UString getDirectory(const UString &p)
Return a path&#39;s directory.
Definition: filepath.cpp:107
Common::ScopedPtr< Campaign > _campaign
Definition: game.h:76
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
static UString getStem(const UString &p)
Return a file name&#39;s stem.
Definition: filepath.cpp:87
static UString relativize(const UString &basePath, const UString &path)
Return the path relative to the base path.
Definition: filepath.cpp:142
A list of files.
Definition: filelist.h:35
void runCampaign()
Definition: game.cpp:80
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
Campaign & getCampaign()
Return the campaign context.
Definition: game.cpp:58
void stopMusic()
Force all currently playing music stopped.
Definition: game.cpp:124
A list of files.
static void getCharacters(std::vector< Common::UString > &characters, bool local)
Return a list of local player characters.
Definition: game.cpp:173
Engine class handling Neverwinter Nights 2.
Game(NWN2Engine &engine, ::Engines::Console &console)
Definition: game.cpp:49
Module & getModule()
Return the module context.
Definition: game.cpp:64
static UString getFile(const UString &p)
Return a file name without its path.
Definition: filepath.cpp:81
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
void stopAmbientMusic()
Stop the ambient music.
Definition: area.cpp:182