xoreos  0.0.5
console.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 <algorithm>
26 
27 #include <boost/bind.hpp>
28 
29 #include "src/common/ustring.h"
30 #include "src/common/util.h"
31 
32 #include "src/aurora/resman.h"
33 
35 
37 #include "src/engines/nwn2/nwn2.h"
38 #include "src/engines/nwn2/game.h"
41 #include "src/engines/nwn2/area.h"
42 
43 namespace Engines {
44 
45 namespace NWN2 {
46 
48  ::Engines::Console(engine, Graphics::Aurora::kSystemFontMono, 13),
49  _engine(&engine), _maxSizeMusic(0) {
50 
51  registerCommand("listmusic" , boost::bind(&Console::cmdListMusic , this, _1),
52  "Usage: listmusic\nList all available music resources");
53  registerCommand("stopmusic" , boost::bind(&Console::cmdStopMusic , this, _1),
54  "Usage: stopmusic\nStop the currently playing music resource");
55  registerCommand("playmusic" , boost::bind(&Console::cmdPlayMusic , this, _1),
56  "Usage: playmusic [<music>]\nPlay the specified music resource. "
57  "If none was specified, play the default area music.");
58  registerCommand("move" , boost::bind(&Console::cmdMove , this, _1),
59  "Usage: move <x> <y> <z>\nMove to this position in the current area");
60  registerCommand("listareas" , boost::bind(&Console::cmdListAreas , this, _1),
61  "Usage: listareas\nList all areas in the current module");
62  registerCommand("gotoarea" , boost::bind(&Console::cmdGotoArea , this, _1),
63  "Usage: gotoarea <area>\nMove to a specific area");
64  registerCommand("listcampaigns", boost::bind(&Console::cmdListCampaigns, this, _1),
65  "Usage: listcampaigns\nList all campaigns");
66  registerCommand("loadcampaign" , boost::bind(&Console::cmdLoadCampaign , this, _1),
67  "Usage: loadcampaign <campaign>\nLoads a campaign, "
68  "replacing the currently running one");
69  registerCommand("listmodules" , boost::bind(&Console::cmdListModules , this, _1),
70  "Usage: listmodules\nList all modules");
71  registerCommand("loadmodule" , boost::bind(&Console::cmdLoadModule , this, _1),
72  "Usage: loadmodule <module>\nLoads a module, "
73  "replacing the currently running one");
74 }
75 
77 }
78 
81 
82  updateMusic();
84  updateModules();
85  updateAreas();
86 }
87 
89  _music.clear();
90  _maxSizeMusic = 0;
91 
92  std::list<Aurora::ResourceManager::ResourceID> music;
93  ResMan.getAvailableResources(Aurora::kFileTypeBMU, music);
94 
95  for (std::list<Aurora::ResourceManager::ResourceID>::const_iterator m = music.begin();
96  m != music.end(); ++m) {
97 
98  _music.push_back(m->name);
99 
100  _maxSizeMusic = MAX(_maxSizeMusic, _music.back().size());
101  }
102 
103  std::sort(_music.begin(), _music.end(), Common::UString::iless());
104  setArguments("playmusic", _music);
105 }
106 
109 
110  setArguments("loadcampaign", _campaigns);
111 }
112 
115 
116  setArguments("loadmodule", _modules);
117 }
118 
120  setArguments("gotoarea", _engine->getGame().getModule().getIFO().getAreas());
121 }
122 
124  updateMusic();
126 }
127 
130 }
131 
133  _engine->getGame().playMusic(cl.args);
134 }
135 
136 void Console::cmdMove(const CommandLine &cl) {
137  std::vector<Common::UString> args;
138  splitArguments(cl.args, args);
139 
140  float x, z, y;
141  if ((args.size() < 3) ||
142  (std::sscanf(args[0].c_str(), "%f", &x) != 1) ||
143  (std::sscanf(args[1].c_str(), "%f", &y) != 1) ||
144  (std::sscanf(args[2].c_str(), "%f", &z) != 1)) {
145 
146  printCommandHelp(cl.cmd);
147  return;
148  }
149 
150  _engine->getGame().getModule().movePC(x, y, z);
151 }
152 
154  updateAreas();
155 
156  const std::vector<Common::UString> &areas = _engine->getGame().getModule().getIFO().getAreas();
157  for (std::vector<Common::UString>::const_iterator a = areas.begin(); a != areas.end(); ++a)
158  printf("%s (\"%s\")", a->c_str(), Area::getName(*a).c_str());
159 }
160 
162  if (cl.args.empty()) {
163  printCommandHelp(cl.cmd);
164  return;
165  }
166 
167  const std::vector<Common::UString> &areas = _engine->getGame().getModule().getIFO().getAreas();
168  for (std::vector<Common::UString>::const_iterator a = areas.begin(); a != areas.end(); ++a)
169  if (a->equalsIgnoreCase(cl.args)) {
170  hide();
171  _engine->getGame().getModule().movePC(*a);
172  return;
173  }
174 
175  printf("Area \"%s\" does not exist", cl.args.c_str());
176 }
177 
179  updateCampaigns();
180 
181  for (std::vector<Common::UString>::const_iterator c = _campaigns.begin(); c != _campaigns.end(); ++c)
182  printf("%s (\"%s\")", c->c_str(), Campaign::getName(*c).c_str());
183 }
184 
186  if (cl.args.empty()) {
187  printCommandHelp(cl.cmd);
188  return;
189  }
190 
191  updateCampaigns();
192  for (std::vector<Common::UString>::const_iterator c = _campaigns.begin(); c != _campaigns.end(); ++c) {
193  if (c->equalsIgnoreCase(cl.args)) {
194  hide();
195  _engine->getGame().getCampaign().load(*c);
196  return;
197  }
198  }
199 
200  printf("No such campaign \"%s\"", cl.args.c_str());
201 }
202 
204  updateModules();
205 
206  for (std::vector<Common::UString>::const_iterator m = _modules.begin(); m != _modules.end(); ++m) {
207  const Common::UString name = Module::getName(*m);
208 
209  if (!name.empty())
210  printf("%s (\"%s\")", m->c_str(), name.c_str());
211  else
212  printf("%s", m->c_str());
213  }
214 }
215 
217  if (cl.args.empty()) {
218  printCommandHelp(cl.cmd);
219  return;
220  }
221 
222  updateModules();
223  for (std::vector<Common::UString>::const_iterator m = _modules.begin(); m != _modules.end(); ++m) {
224  if (m->equalsIgnoreCase(cl.args)) {
225  hide();
226  _engine->getGame().getCampaign().loadModule(cl.args + ".mod");
227  return;
228  }
229  }
230 
231  printf("No such module \"%s\"", cl.args.c_str());
232 }
233 
234 } // End of namespace NWN2
235 
236 } // End of namespace Engines
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
void cmdLoadModule(const CommandLine &cl)
Definition: console.cpp:216
Common::UString args
Definition: console.h:218
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 void splitArguments(Common::UString argLine, std::vector< Common::UString > &args)
Definition: console.cpp:1539
NWN2Engine * _engine
Definition: console.h:45
void cmdGotoArea(const CommandLine &cl)
Definition: console.cpp:161
The context holding a Neverwinter Nights 2 campaign.
void cmdListAreas(const CommandLine &cl)
Definition: console.cpp:153
The context holding a Neverwinter Nights 2 area.
static void getCampaigns(std::vector< Common::UString > &campaigns)
Return a list of all campaigns.
Definition: game.cpp:135
const Common::UString & getName()
Return the area&#39;s localized name.
Definition: area.cpp:126
std::vector< Common::UString > _modules
All known modules.
Definition: console.h:50
void loadModule(const Common::UString &module)
Load a stand-alone module as a campaign.
Definition: campaign.cpp:137
The context needed to run a Neverwinter Nights 2 module.
const Common::UString & getName() const
Return the module&#39;s name.
Definition: module.cpp:488
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
#define UNUSED(x)
Definition: system.h:170
Basic Aurora graphics types.
Utility templates and functions.
void load(const Common::UString &campaign)
Load a campaign.
Definition: campaign.cpp:125
void cmdLoadCampaign(const CommandLine &cl)
Definition: console.cpp:185
void playMusic(const Common::UString &music="")
Overwrite all currently playing music.
Definition: game.cpp:113
void cmdListCampaigns(const CommandLine &cl)
Definition: console.cpp:178
const char * kSystemFontMono
Identifier used for the monospaced system font.
Definition: fontman.cpp:41
static void getModules(std::vector< Common::UString > &modules)
Return a list of all modules.
Definition: game.cpp:156
const std::vector< Common::UString > & getAreas() const
Return the list of areas in the module.
Definition: ifofile.cpp:311
void printList(const std::vector< Common::UString > &list, size_t maxSize=0)
Definition: console.cpp:1479
const Common::UString & getName() const
Return the name of the current campaign.
Definition: campaign.cpp:73
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
Console(NWN2Engine &engine)
Definition: console.cpp:47
void movePC(const Common::UString &area)
Move the player character to this area.
Definition: module.cpp:428
Unicode string handling.
std::vector< Common::UString > _music
All known music resources.
Definition: console.h:48
void cmdListModules(const CommandLine &cl)
Definition: console.cpp:203
void cmdMove(const CommandLine &cl)
Definition: console.cpp:136
void cmdStopMusic(const CommandLine &cl)
Definition: console.cpp:128
void printf(const char *s,...) GCC_PRINTF(2
Definition: console.cpp:1093
Game & getGame()
Return the context running the actual game.
Definition: nwn2.cpp:109
Campaign & getCampaign()
Return the campaign context.
Definition: game.cpp:58
void cmdListMusic(const CommandLine &cl)
Definition: console.cpp:123
void stopMusic()
Force all currently playing music stopped.
Definition: game.cpp:124
virtual void updateCaches()
Definition: console.cpp:1122
void printCommandHelp(const Common::UString &cmd)
Definition: console.cpp:1391
Engine class handling Neverwinter Nights 2.
const Aurora::IFOFile & getIFO() const
Return the IFO of the currently loaded module.
Definition: module.cpp:484
void cmdPlayMusic(const CommandLine &cl)
Definition: console.cpp:132
std::vector< Common::UString > _campaigns
All known campaigns.
Definition: console.h:49
Module & getModule()
Return the module context.
Definition: game.cpp:64
T MAX(T a, T b)
Definition: util.h:71
void setArguments(const Common::UString &cmd, const std::vector< Common::UString > &args)
Definition: console.cpp:1565
bool registerCommand(const Common::UString &cmd, const CommandCallback &callback, const Common::UString &help)
Definition: console.cpp:1576
The global resource manager for Aurora resources.
Audio, MP3 with extra header.
Definition: types.h:65