xoreos  0.0.5
object.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/uuid.h"
28 
29 #include "src/aurora/ssffile.h"
30 #include "src/aurora/2dafile.h"
31 #include "src/aurora/2dareg.h"
32 #include "src/aurora/dlgfile.h"
33 
39 
40 #include "src/sound/sound.h"
41 
44 
45 #include "src/engines/nwn/types.h"
46 #include "src/engines/nwn/object.h"
47 
48 namespace Engines {
49 
50 namespace NWN {
51 
52 Object::Object(ObjectType type) : _type(type),
53  _soundSet(Aurora::kFieldIDInvalid), _static(false), _usable(true),
54  _pcSpeaker(0), _area(0) {
55 
57  ObjectMan.registerObject(this);
58 
59  _position [0] = 0.0f;
60  _position [1] = 0.0f;
61  _position [2] = 0.0f;
62  _orientation[0] = 0.0f;
63  _orientation[1] = 0.0f;
64  _orientation[2] = 0.0f;
65  _orientation[3] = 0.0f;
66 }
67 
69  ObjectMan.unregisterObject(this);
71 }
72 
74  return _type;
75 }
76 
78 }
79 
82 }
83 
84 void Object::show() {
85 }
86 
87 void Object::hide() {
88 }
89 
91  return _name;
92 }
93 
95  return _description;
96 }
97 
99  return _portrait;
100 }
101 
103  return _conversation;
104 }
105 
107  loadSSF();
108 
109  return _ssf.get();
110 }
111 
112 bool Object::isStatic() const {
113  return _static;
114 }
115 
116 bool Object::isUsable() const {
117  return _usable;
118 }
119 
120 bool Object::isClickable() const {
121  return !_static && _usable;
122 }
123 
124 const std::list<uint32> &Object::getIDs() const {
125  return _ids;
126 }
127 
129  return _pcSpeaker;
130 }
131 
133  _pcSpeaker = pc;
134 }
135 
137  return _area;
138 }
139 
140 void Object::setArea(Area *area) {
141  _area = area;
142 }
143 
145  // TODO: Object::getLocation(): Facing
146 
147  Location location;
148 
149  location.setArea(_area);
150  location.setPosition(_position[0], _position[1], _position[2]);
151  location.setFacing(0.0f);
152 
153  return location;
154 }
155 
156 void Object::getPosition(float &x, float &y, float &z) const {
157  x = _position[0];
158  y = _position[1];
159  z = _position[2];
160 }
161 
162 void Object::getOrientation(float &x, float &y, float &z, float &angle) const {
163  x = _orientation[0];
164  y = _orientation[1];
165  z = _orientation[2];
166 
167  angle = _orientation[3];
168 }
169 
170 void Object::setPosition(float x, float y, float z) {
171  _position[0] = x;
172  _position[1] = y;
173  _position[2] = z;
174 }
175 
176 void Object::setOrientation(float x, float y, float z, float angle) {
177  _orientation[0] = x;
178  _orientation[1] = y;
179  _orientation[2] = z;
180  _orientation[3] = angle;
181 }
182 
184 }
185 
187 }
188 
189 void Object::highlight(bool UNUSED(enabled)) {
190 }
191 
194  return;
195 
196  const Aurora::TwoDAFile &soundSets = TwoDAReg.get2DA("soundset");
197 
198  Common::UString ssfFile = soundSets.getRow(_soundSet).getString("RESREF");
199  if (ssfFile.empty())
200  return;
201 
202  try {
203  _ssf.reset(new Aurora::SSFFile(ssfFile));
204  } catch (...) {
205  Common::exceptionDispatcherWarning("Failed to load SSF \"%s\" (object \"%s\")",
206  ssfFile.c_str(), _tag.c_str());
207  }
208 }
209 
210 void Object::speakString(const Common::UString &string, uint32 UNUSED(volume)) {
211  if (!showSpeechTooltip(string))
212  status("<%s> \"%s\"", getName().c_str(), string.c_str());
213 }
214 
216  if (conv.empty())
217  conv = _conversation;
218  if (conv.empty())
219  return;
220 
221  Common::UString text;
222  Common::UString sound;
223 
224 
225  try {
226  Aurora::DLGFile dlg(conv, this, true);
227 
228  const Aurora::DLGFile::Line *line = dlg.getOneLiner();
229  if (line) {
230  text = line->text.getString();
231  sound = line->sound;
232  }
233 
234  } catch (...) {
235  Common::exceptionDispatcherWarning("Failed evaluating one-liner from conversation \"%s\"", conv.c_str());
236  }
237 
238  if (!text.empty())
239  speakString(text, 0);
240  if (!sound.empty())
241  playSound(sound);
242 }
243 
245  SoundMan.stopChannel(_sound);
246 }
247 
248 void Object::playSound(const Common::UString &sound, bool pitchVariance) {
249  stopSound();
250  if (sound.empty())
251  return;
252 
253  _sound = ::Engines::playSound(sound, Sound::kSoundTypeVoice, false, 1.0f, pitchVariance);
254 }
255 
256 bool Object::click(Object *UNUSED(triggerer)) {
257  return true;
258 }
259 
261  Aurora::NWScript::FunctionContext ctx = FunctionMan.createContext("BeginConversation");
262 
263  ctx.setCaller(this);
264  ctx.setTriggerer(triggerer);
265 
266  ctx.getParams()[0] = "";
267  ctx.getParams()[1] = (Aurora::NWScript::Object *) 0;
268 
269  FunctionMan.call("BeginConversation", ctx);
270 
271  return true;
272 }
273 
274 void Object::playAnimation(const Common::UString &animation, bool restart, float length, float speed) {
275  warning("TODO: Object::playAnimation(\"%s\", %s, %f, %f",
276  animation.c_str(), restart ? "true" : "false", length, speed);
277 }
278 
279 void Object::playAnimation(Animation animation, bool restart, float length, float speed) {
280  warning("TODO: Object::playAnimation(%d, %s, %f, %f",
281  (int) animation, restart ? "true" : "false", length, speed);
282 }
283 
285  return false;
286 }
287 
290  return false;
291 
292  _tooltip->clearLines();
293  _tooltip->addLine(_name, 0.5f, 0.5f, 1.0f, 1.0f);
294 
295  return true;
296 }
297 
300  return false;
301 
302  _tooltip->clearLines();
303  _tooltip->addLine(line, 1.0f, 1.0f, 1.0f, 1.0f);
304 
305  return true;
306 }
307 
309  hideTooltip();
310 
311  _tooltip.reset();
312 }
313 
315  hideTooltip();
316 
317  if (!createFeedbackTooltip())
318  return false;
319 
321 
322  return true;
323 }
324 
326  hideTooltip();
327 
328  if (!createSpeechTooltip(line))
329  return false;
330 
331  // Show the speech bubble immediately, for 12s (two rounds)
332  _tooltip->show(0, 12000);
333 
334  return true;
335 }
336 
338  if (_tooltip)
339  _tooltip->hide();
340 }
341 
342 } // End of namespace NWN
343 
344 } // End of namespace Engines
const Common::UString & getPortrait() const
Return the object&#39;s portrait.
Definition: object.cpp:98
virtual void unloadModel()
Unload the object&#39;s model(s).
Definition: object.cpp:80
#define ObjectMan
Definition: objectman.h:56
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void destroyTooltip()
Destroy all tooltips on this object.
Definition: object.cpp:308
bool showSpeechTooltip(const Common::UString &line)
Create and show a tooltip with a line the object speaks.
Definition: object.cpp:325
virtual void getOrientation(float &x, float &y, float &z, float &angle) const
Return the object&#39;s orientation.
Definition: object.cpp:162
Handling BioWare&#39;s SSFs (sound set file).
virtual bool createTooltip(Tooltip::Type type)
Create an empty tooltip.
Definition: object.cpp:284
bool isUsable() const
Can the object be used by the PC?
Definition: object.cpp:116
bool beginConversation(Object *triggerer)
Begin a conversation between the triggerer and this object.
Definition: object.cpp:260
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
bool createFeedbackTooltip()
Create a tooltip with the name and/or portrait of the object.
Definition: object.cpp:288
A class holding an UTF-8 string.
Definition: ustring.h:48
const Common::UString & getConversation() const
Return the object&#39;s default conversation (DLG).
Definition: object.cpp:102
bool isClickable() const
Can the player click the object?
Definition: object.cpp:120
virtual void setOrientation(float x, float y, float z, float angle)
Set the object&#39;s orientation.
Definition: object.cpp:176
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
const Common::UString & getName() const
Return the object&#39;s name.
Definition: object.cpp:90
Class to hold a sound set.
Definition: ssffile.h:55
Context of an NWScript function.
Common::UString _name
The object&#39;s display name.
Definition: object.h:165
Location getLocation() const
Create a Location out of the object&#39;s area, position and orientation.
Definition: object.cpp:144
Aurora::NWScript::Object * _pcSpeaker
The current PC speaking with the object.
Definition: object.h:180
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:136
Utility functions for generating unique IDs.
Basic Neverwinter Nights type definitions.
virtual bool click(Object *triggerer=0)
The object was clicked.
Definition: object.cpp:256
const Common::UString & getDescription() const
Return the object&#39;s description.
Definition: object.cpp:94
An area in Neverwinter Nights, holding all objects and room tiles within, as well as general area pro...
Definition: area.h:63
bool isStatic() const
Is the object static (not manipulable at all)?
Definition: object.cpp:112
virtual void speakString(const Common::UString &string, uint32 volume)
Speak the specified string.
Definition: object.cpp:210
const Common::UString & getString(Language language, LanguageGender gender=kLanguageGenderCurrent) const
Get the string of that language.
Definition: locstring.cpp:82
void setArea(Area *area)
Set the location&#39;s area.
Definition: location.cpp:48
A scripted speech line on an object.
Definition: tooltip.h:55
The NWScript function manager.
Common::UString sound
ResRef of the sound to play while speaking this entry.
Definition: dlgfile.h:73
virtual void enter()
The cursor entered the object.
Definition: object.cpp:183
virtual void playAnimation(const Common::UString &animation="", bool restart=true, float length=0.0f, float speed=1.0f)
Play an object animation.
Definition: object.cpp:274
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
const Line * getOneLiner() const
Return the first active non-branching entry.
Definition: dlgfile.cpp:151
NWScript object manager.
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
std::list< uint32 > _ids
The object&#39;s model IDs.
Definition: object.h:178
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:156
float _orientation[4]
The object&#39;s orientation.
Definition: object.h:185
virtual ~Object()
Definition: object.cpp:68
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
static uint32 getDefaultDelay()
Returns the configured default delay, in ms, before a tooltip appears.
Definition: tooltip.cpp:539
Handling BioWare&#39;s 2DAs (two-dimensional array).
Common::ScopedPtr< Tooltip > _tooltip
The tooltip displayed over the object.
Definition: object.h:189
virtual void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: object.cpp:170
Common::UString _description
The object&#39;s description.
Definition: object.h:166
Area * _area
The area the object is currently in.
Definition: object.h:182
bool showFeedbackTooltip()
Create and show a tooltip with the name and/or portrait of the object.
Definition: object.cpp:314
Aurora::NWScript::Object * getPCSpeaker() const
Return the PC currently speaking with this object.
Definition: object.cpp:128
bool _static
Is the object static?
Definition: object.h:175
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:248
The global sound manager, handling all sound output.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
bool createSpeechTooltip(const Common::UString &line)
Create a tooltip with a line the object should speak.
Definition: object.cpp:298
#define SoundMan
Shortcut for accessing the sound manager.
Definition: sound.h:293
The global 2DA registry.
void setPosition(float x, float y, float z)
Set the location&#39;s position.
Definition: location.cpp:58
virtual void setArea(Area *)
Set the area this object is currently in.
Definition: object.cpp:140
void warning(const char *s,...)
Definition: util.cpp:33
ObjectType _type
The object&#39;s type.
Definition: object.h:163
Voice/Speech.
Definition: types.h:46
void speakOneLiner(Common::UString conv, Object *tokenTarget=0)
Speak an one-liner from the specified conversation file.
Definition: object.cpp:215
ObjectType
Object type, matches the bitfield in nwscript.nss.
Definition: types.h:36
Sound::ChannelHandle playSound(const Common::UString &sound, Sound::SoundType soundType, bool loop, float volume, bool pitchVariance)
Play this sound resource.
Definition: util.cpp:81
Common::UString _portrait
The object&#39;s portrait.
Definition: object.h:168
An object in a Neverwinter Nights area.
void stopSound()
Stop the current object sound.
Definition: object.cpp:244
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
uint32 _soundSet
The object&#39;s sound set, as an index into soundset.2da.
Definition: object.h:172
#define FunctionMan
Definition: functionman.h:84
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
void setFacing(float facing)
Set the location&#39;s orientation.
Definition: location.cpp:68
Name and/or portrait of an object in the 3D world.
Definition: tooltip.h:54
virtual void leave()
The cursor left the object.
Definition: object.cpp:186
virtual void loadModel()
Load the object&#39;s model(s).
Definition: object.cpp:77
uint32_t uint32
Definition: types.h:204
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
void status(const char *s,...)
Definition: util.cpp:52
virtual void highlight(bool enabled)
(Un)Highlight the object.
Definition: object.cpp:189
bool _usable
Is the object usable?
Definition: object.h:176
virtual void show()
Show the object&#39;s model(s).
Definition: object.cpp:84
Common::ScopedPtr< Aurora::SSFFile > _ssf
The object&#39;s sound set.
Definition: object.h:173
Generic Aurora engines utility functions.
const Aurora::SSFFile * getSSF()
Return the object&#39;s sound set.
Definition: object.cpp:106
void setPCSpeaker(Aurora::NWScript::Object *pc)
Set the PC currently speaking with this object.
Definition: object.cpp:132
Manager for tokens in Aurora engines text strings.
NWScript types.
Sound::ChannelHandle _sound
The currently playing object sound.
Definition: object.h:187
NWScript utility functions.
float _position[3]
The object&#39;s position.
Definition: object.h:184
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:73
Common::UString _conversation
The object&#39;s default conversation.
Definition: object.h:170
Handling BioWare&#39;s DLGs (dialog / conversation files).
void loadSSF()
Load the object&#39;s sound set.
Definition: object.cpp:192
void hideTooltip()
Hide the tooltip again.
Definition: object.cpp:337
virtual void hide()
Hide the object&#39;s model(s).
Definition: object.cpp:87
uint32 generateIDNumber()
Definition: uuid.cpp:46
const std::list< uint32 > & getIDs() const
Return the object&#39;s model IDs.
Definition: object.cpp:124
LocString text
The actual text of the entry.
Definition: dlgfile.h:71