xoreos  0.0.5
module.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 "src/common/util.h"
26 #include "src/common/error.h"
27 #include "src/common/ustring.h"
28 
29 #include "src/graphics/camera.h"
30 
31 #include "src/events/events.h"
32 
36 
38 #include "src/engines/jade/area.h"
40 
41 namespace Engines {
42 
43 namespace Jade {
44 
45 bool Module::Action::operator<(const Action &s) const {
46  return timestamp < s.timestamp;
47 }
48 
49 
50 Module::Module(::Engines::Console &console) : _console(&console), _hasModule(false),
51  _running(false), _exit(false) {
52 
53 }
54 
56  try {
57  clear();
58  } catch (...) {
59  }
60 }
61 
62 void Module::clear() {
63  unload(true);
64 }
65 
66 void Module::load(const Common::UString &module) {
67  if (isRunning()) {
68  // We are currently running a module. Schedule a safe change instead
69 
70  changeModule(module);
71  return;
72  }
73 
74  // We are not currently running a module. Directly load the new module
75  loadModule(module);
76 }
77 
78 void Module::loadModule(const Common::UString &module) {
79  unload(false);
80 
81  _module = module;
82 
83  try {
84 
85  load();
86 
87  } catch (Common::Exception &e) {
88  _module.clear();
89 
90  e.add("Failed loading module \"%s\"", module.c_str());
91  throw e;
92  }
93 
94  _newModule.clear();
95 
96  _hasModule = true;
97 }
98 
100  unloadPC();
101 
102  _pc.reset(pc);
103 
104  addObject(*_pc);
105 }
106 
108  return _pc.get();
109 }
110 
111 bool Module::isLoaded() const {
112  return _hasModule && _area && _pc;
113 }
114 
115 bool Module::isRunning() const {
116  return !EventMan.quitRequested() && _running && !_exit;
117 }
118 
119 void Module::exit() {
120  _exit = true;
121 }
122 
124  // TODO: Module::showMenu()
125 }
126 
128  bool UNUSED(noWidescreen), bool UNUSED(resetZoom)) {
129  // TODO
130  return false;
131 }
132 
133 void Module::load() {
134  loadArea();
135 }
136 
138  _area.reset(new Area(*this, _module));
139 }
140 
141 void Module::unload(bool completeUnload) {
142  leaveArea();
143  unloadArea();
144 
145  if (completeUnload) {
146  unloadPC();
147  }
148 
149  _eventQueue.clear();
150  _delayedActions.clear();
151 
152  _newModule.clear();
153  _hasModule = false;
154 
155  _module.clear();
156 }
157 
159  _area.reset();
160 }
161 
163  if (!_pc)
164  return;
165 
166  removeObject(*_pc);
167 
168  _pc.reset();
169 }
170 
172  if (module == getName()) {
173  warning("Module \"%s\" is already currently loaded", module.c_str());
174  return;
175  }
176 
177  _newModule = module;
178 }
179 
181  if (_newModule.empty())
182  return;
183 
184  _console->hide();
185 
186  const Common::UString newModule = _newModule;
187 
188  unload(false);
189 
190  _exit = true;
191 
192  loadModule(newModule);
193  enter();
194 }
195 
197  if (!_hasModule || !_area)
198  throw Common::Exception("Module::enter(): Lacking a module?!?");
199 
200  if (!_pc)
201  throw Common::Exception("Module::enter(): Lacking a PC?!?");
202 
203  _console->printf("Entering module \"%s\"", _module.c_str());
204 
205  CameraMan.reset();
206  CameraMan.setOrientation(90.0f, 0.0f, 0.0f);
207  CameraMan.update();
208 
209  enterArea();
210 
211  _running = true;
212  _exit = false;
213 }
214 
216  leaveArea();
217 
218  _running = false;
219  _exit = true;
220 }
221 
223  _area->show();
224 
225  _area->runScript(kScriptOnEnter, _area.get(), _pc.get());
226 }
227 
229  if (_area) {
230  _area->runScript(kScriptOnExit, _area.get(), _pc.get());
231 
232  _area->hide();
233  }
234 }
235 
236 void Module::addEvent(const Events::Event &event) {
237  _eventQueue.push_back(event);
238 }
239 
241  if (!isRunning())
242  return;
243 
244  replaceModule();
245 
246  if (!isRunning())
247  return;
248 
249  handleEvents();
250  handleActions();
251 }
252 
254  for (EventQueue::const_iterator event = _eventQueue.begin(); event != _eventQueue.end(); ++event) {
255  // Handle console
256  if (_console->isVisible()) {
257  _console->processEvent(*event);
258  continue;
259  }
260 
261  if (event->type == Events::kEventKeyDown) {
262  // Menu
263  if (event->key.keysym.sym == SDLK_ESCAPE) {
264  showMenu();
265  continue;
266  }
267 
268  // Console
269  if ((event->key.keysym.sym == SDLK_d) && (event->key.keysym.mod & KMOD_CTRL)) {
270  _console->show();
271  continue;
272  }
273  }
274 
275  // Camera
276  if (!_console->isVisible())
277  if (FreeRoamCam.handleCameraInput(*event))
278  continue;
279 
280  _area->addEvent(*event);
281  }
282 
283  _eventQueue.clear();
284 
285  CameraMan.update();
286 
287  _area->processEventQueue();
288 }
289 
291  uint32 now = EventMan.getTimestamp();
292 
293  while (!_delayedActions.empty()) {
294  ActionQueue::iterator action = _delayedActions.begin();
295 
296  if (now < action->timestamp)
297  break;
298 
299  if (action->type == kActionScript)
300  ScriptContainer::runScript(action->script, action->state,
301  action->owner, action->triggerer);
302 
303  _delayedActions.erase(action);
304  }
305 }
306 
307 void Module::movePC(float x, float y, float z) {
308  if (!_pc)
309  return;
310 
311  _pc->setPosition(x, y, z);
312  movedPC();
313 }
314 
315 void Module::movePC(const Common::UString &module) {
316  if (module.empty() || (module == _module))
317  return;
318 
319  loadModule(module);
320 }
321 
323  if (!_pc)
324  return;
325 
326  float x, y, z;
327  _pc->getPosition(x, y, z);
328 
329  // Roughly head position
330  CameraMan.setPosition(x, y, z + 1.8f);
331  CameraMan.update();
332 }
333 
335  return _module;
336 }
337 
339  return _area.get();
340 }
341 
343  const Aurora::NWScript::ScriptState &state,
345  Aurora::NWScript::Object *triggerer, uint32 delay) {
346  Action action;
347 
348  action.type = kActionScript;
349  action.script = script;
350  action.state = state;
351  action.owner = owner;
352  action.triggerer = triggerer;
353  action.timestamp = EventMan.getTimestamp() + delay;
354 
355  _delayedActions.insert(action);
356 }
357 
358 } // End of namespace Jade
359 
360 } // End of namespace Engines
void movedPC()
Notify the module that the PC was moved.
Definition: module.cpp:322
void exit()
Exit the currently running module.
Definition: module.cpp:119
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
bool isVisible() const
Definition: console.cpp:797
void unload(bool completeUnload=true)
Unload the whole shebang.
Definition: module.cpp:141
Common::UString _module
The current module&#39;s name.
Definition: module.h:168
bool startConversation(const Common::UString &conv, Creature &pc, Object &obj, bool noWidescreen=false, bool resetZoom=true)
Start a conversation.
Definition: module.cpp:127
void addObject(Jade::Object &object)
Add an object to this container.
A class holding an UTF-8 string.
Definition: ustring.h:48
Aurora::NWScript::ObjectReference triggerer
Definition: module.h:145
Area * getCurrentArea()
Return the area the PC is currently in.
Definition: module.cpp:338
bool _exit
Should we exit the module?
Definition: module.h:166
Camera management.
Module(::Engines::Console &console)
Definition: module.cpp:50
void removeObject(Jade::Object &object)
Remove an object from this container.
Common::UString _newModule
The module we should change to.
Definition: module.h:169
Common::ScopedPtr< Creature > _pc
The player character we use.
Definition: module.h:164
Exception that provides a stack of explanations.
Definition: error.h:36
#define FreeRoamCam
Creature * getPC()
Return the currently playing PC.
Definition: module.cpp:107
SDL_Event Event
Definition: types.h:42
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:110
Keyboard key was pressed.
Definition: types.h:46
::Engines::Console * _console
Definition: module.h:156
Basic exceptions to throw.
void leave()
Leave the running module, quitting it.
Definition: module.cpp:215
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
Common::UString script
Definition: module.h:141
void processEventQueue()
Process the current event queue.
Definition: module.cpp:240
An area in Jade Empire, holding all objects and rooms within, as well as general area properties like...
Definition: area.h:57
void changeModule(const Common::UString &module)
Schedule a change to a new module.
Definition: module.cpp:171
void replaceModule()
Actually replace the currently running module.
Definition: module.cpp:180
The global events manager.
void loadModule(const Common::UString &module)
Load the actual module.
Definition: module.cpp:78
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void enter()
Enter the loaded module, starting it.
Definition: module.cpp:196
Generic Aurora engines (debug) console.
Aurora::NWScript::ScriptState state
Definition: module.h:143
void usePC(Creature *pc)
Use this character as the player character.
Definition: module.cpp:99
StackException Exception
Definition: error.h:59
void warning(const char *s,...)
Definition: util.cpp:33
An area.
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
An object within a Jade area.
Definition: object.h:53
Unicode string handling.
void showMenu()
Show the ingame main menu.
Definition: module.cpp:123
uint32_t uint32
Definition: types.h:204
A module.
void printf(const char *s,...) GCC_PRINTF(2
Definition: console.cpp:1093
bool processEvent(const Events::Event &event)
Definition: console.cpp:817
void movePC(float x, float y, float z)
Move the player character to this position within the current area.
Definition: module.cpp:307
#define CameraMan
Shortcut for accessing the camera manager.
Definition: camera.h:83
Generic Aurora engines utility functions.
void addEvent(const Events::Event &event)
Add a single event for consideration into the event queue.
Definition: module.cpp:236
void clear()
Clear the whole context.
Definition: module.cpp:62
Common::ScopedPtr< Area > _area
The current module&#39;s area.
Definition: module.h:171
const Common::UString & getName() const
Return the module&#39;s name.
Definition: module.cpp:334
Aurora::NWScript::ObjectReference owner
Definition: module.h:144
bool _hasModule
Do we have a module?
Definition: module.h:158
Engine utility class for free-roam camera handling.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
bool isLoaded() const
Is a module currently loaded and ready to run?
Definition: module.cpp:111
bool isRunning() const
Is a module currently running?
Definition: module.cpp:115
bool operator<(const Action &s) const
Definition: module.cpp:45
EventQueue _eventQueue
Definition: module.h:173
bool _running
Are we currently running a module?
Definition: module.h:159
A creature in a Jade Empire area.
ActionQueue _delayedActions
Definition: module.h:174
void delayScript(const Common::UString &script, const Aurora::NWScript::ScriptState &state, Aurora::NWScript::Object *owner, Aurora::NWScript::Object *triggerer, uint32 delay)
Definition: module.cpp:342