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/gff3file.h"
33 #include "src/aurora/dlgfile.h"
34 
40 
41 #include "src/sound/sound.h"
42 
45 
46 #include "src/engines/nwn2/types.h"
48 
49 namespace Engines {
50 
51 namespace NWN2 {
52 
53 Object::Object(ObjectType type) : _type(type),
54  _soundSet(Aurora::kFieldIDInvalid), _static(true), _usable(true),
55  _area(0) {
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);
70 }
71 
73  return _type;
74 }
75 
77 }
78 
80 }
81 
82 void Object::show() {
83 }
84 
85 void Object::hide() {
86 }
87 
89  return _name;
90 }
91 
93  return _description;
94 }
95 
97  return _conversation;
98 }
99 
101  loadSSF();
102 
103  return _ssf.get();
104 }
105 
106 bool Object::isStatic() const {
107  return _static;
108 }
109 
110 bool Object::isUsable() const {
111  return _usable;
112 }
113 
114 bool Object::isClickable() const {
115  return !_static && _usable;
116 }
117 
118 const std::list<uint32> &Object::getIDs() const {
119  return _ids;
120 }
121 
123  return _area;
124 }
125 
126 void Object::setArea(Area *area) {
127  _area = area;
128 }
129 
131  // TODO: Object::getLocation(): Facing
132 
133  Location location;
134 
135  location.setArea(_area);
136  location.setPosition(_position[0], _position[1], _position[2]);
137  location.setFacing(0.0f);
138 
139  return location;
140 }
141 
142 void Object::getPosition(float &x, float &y, float &z) const {
143  x = _position[0];
144  y = _position[1];
145  z = _position[2];
146 }
147 
148 void Object::getOrientation(float &x, float &y, float &z, float &angle) const {
149  x = _orientation[0];
150  y = _orientation[1];
151  z = _orientation[2];
152 
153  angle = _orientation[3];
154 }
155 
156 void Object::setPosition(float x, float y, float z) {
157  _position[0] = x;
158  _position[1] = y;
159  _position[2] = z;
160 }
161 
162 void Object::setOrientation(float x, float y, float z, float angle) {
163  _orientation[0] = x;
164  _orientation[1] = y;
165  _orientation[2] = z;
166  _orientation[3] = angle;
167 }
168 
170 }
171 
173 }
174 
175 void Object::highlight(bool UNUSED(enabled)) {
176 }
177 
178 bool Object::click(Object *UNUSED(triggerer)) {
179  return true;
180 }
181 
184  return;
185 
186  const Aurora::TwoDAFile &soundSets = TwoDAReg.get2DA("soundset");
187 
188  Common::UString ssfFile = soundSets.getRow(_soundSet).getString("RESREF");
189  if (ssfFile.empty())
190  return;
191 
192  try {
193  _ssf.reset(new Aurora::SSFFile(ssfFile));
194  } catch (...) {
195  Common::exceptionDispatcherWarning("Failed to load SSF \"%s\" (object \"%s\")",
196  ssfFile.c_str(), _tag.c_str());
197  }
198 }
199 
200 void Object::readVarTable(const Aurora::GFF3List &varTable) {
201  for (Aurora::GFF3List::const_iterator v = varTable.begin(); v != varTable.end(); ++v) {
202  const Common::UString name = (*v)->getString ("Name");
203  const int32 type = (*v)->getSint ("Type");
204 
205  if (name.empty())
206  continue;
207 
208  switch (type) {
209  case -1:
211  break;
212 
213  case 1:
214  setVariable(name, (int32) (*v)->getSint("Value"));
215  break;
216 
217  case 2:
218  setVariable(name, (float) (*v)->getDouble("Value"));
219  break;
220 
221  case 3:
222  setVariable(name, (*v)->getString("Value"));
223  break;
224 
225  case 4:
226  setVariable(name, (int32)((uint32) (*v)->getUint("Value")));
227  break;
228 
229  case 5:
230  warning("TODO: Object::readVarTable(), \"%s\" has location type", name.c_str());
232  break;
233 
234  default:
235  throw Common::Exception("Unknown variable type %u (\"%s\")", type, name.c_str());
236  }
237  }
238 }
239 
241  if (gff.hasField("VarTable"))
242  readVarTable(gff.getList("VarTable"));
243 }
244 
245 void Object::speakString(const Common::UString &string, uint32 UNUSED(volume)) {
246  // TODO: Object::speakString(): Show the string in a speech bubble
247 
248  status("<%s> \"%s\"", getName().c_str(), string.c_str());
249 }
250 
252  if (conv.empty())
253  conv = _conversation;
254  if (conv.empty())
255  return;
256 
257  Common::UString text;
258  Common::UString sound;
259 
260 
261  try {
262  Aurora::DLGFile dlg(conv, this);
263 
264  const Aurora::DLGFile::Line *line = dlg.getOneLiner();
265  if (line) {
266  text = line->text.getString();
267  sound = line->sound;
268  }
269 
270  } catch (...) {
271  Common::exceptionDispatcherWarning("Failed evaluating one-liner from conversation \"%s\"", conv.c_str());
272  }
273 
274  if (!text.empty())
275  speakString(text, 0);
276  if (!sound.empty())
277  playSound(sound);
278 }
279 
281  SoundMan.stopChannel(_sound);
282 }
283 
284 void Object::playSound(const Common::UString &sound, bool pitchVariance) {
285  stopSound();
286  if (sound.empty())
287  return;
288 
289  _sound = ::Engines::playSound(sound, Sound::kSoundTypeVoice, false, 1.0f, pitchVariance);
290 }
291 
292 } // End of namespace NWN2
293 
294 } // End of namespace Engines
Sound::ChannelHandle _sound
The currently playing object sound.
Definition: object.h:165
#define ObjectMan
Definition: objectman.h:56
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void stopSound()
Stop the current object sound.
Definition: object.cpp:280
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
bool isClickable() const
Can the player click the object?
Definition: object.cpp:114
Handling BioWare&#39;s SSFs (sound set file).
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
float _orientation[4]
The object&#39;s orientation.
Definition: object.h:163
A class holding an UTF-8 string.
Definition: ustring.h:48
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:122
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
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
Class to hold a sound set.
Definition: ssffile.h:55
Context of an NWScript function.
virtual void loadModel()
Load the object&#39;s model(s).
Definition: object.cpp:76
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
const Common::UString & getDescription() const
Return the object&#39;s description.
Definition: object.cpp:92
virtual void getOrientation(float &x, float &y, float &z, float &angle) const
Return the object&#39;s orientation.
Definition: object.cpp:148
void speakOneLiner(Common::UString conv, Object *tokenTarget=0)
Speak an one-liner from the specified conversation file.
Definition: object.cpp:251
Utility functions for generating unique IDs.
uint32 _soundSet
The object&#39;s sound set, as an index into soundset.2da.
Definition: object.h:152
virtual void highlight(bool enabled)
(Un)Highlight the object.
Definition: object.cpp:175
Common::UString _name
The object&#39;s display name.
Definition: object.h:147
An object within a NWN2 area.
Definition: object.h:58
void readVarTable(const Aurora::GFF3List &varTable)
Read the object&#39;s variable table.
Definition: object.cpp:200
const Common::UString & getString(Language language, LanguageGender gender=kLanguageGenderCurrent) const
Get the string of that language.
Definition: locstring.cpp:82
The NWScript function manager.
Common::UString sound
ResRef of the sound to play while speaking this entry.
Definition: dlgfile.h:73
ObjectType _type
The object&#39;s type.
Definition: object.h:145
virtual void leave()
The cursor left the object.
Definition: object.cpp:172
virtual void hide()
Hide the object&#39;s model(s).
Definition: object.cpp:85
bool _usable
Is the object usable?
Definition: object.h:156
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
virtual ~Object()
Definition: object.cpp:68
std::list< uint32 > _ids
The object&#39;s model IDs.
Definition: object.h:158
const Line * getOneLiner() const
Return the first active non-branching entry.
Definition: dlgfile.cpp:151
NWScript object manager.
Basic exceptions to throw.
void setPosition(float x, float y, float z)
Set the location&#39;s position.
Definition: location.cpp:58
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
float _position[3]
The object&#39;s position.
Definition: object.h:162
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
Basic Neverwinter Nights 2 type definitions.
void speakString(const Common::UString &string, uint32 volume)
Speak the specified string.
Definition: object.cpp:245
Handling BioWare&#39;s 2DAs (two-dimensional array).
const Common::UString & getConversation() const
Return the object&#39;s default conversation (DLG).
Definition: object.cpp:96
Common::UString _conversation
The object&#39;s default conversation.
Definition: object.h:150
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
#define SoundMan
Shortcut for accessing the sound manager.
Definition: sound.h:293
void setArea(Area *area)
Set the location&#39;s area.
Definition: location.cpp:48
StackException Exception
Definition: error.h:59
The global 2DA registry.
const std::list< uint32 > & getIDs() const
Return the object&#39;s model IDs.
Definition: object.cpp:118
const Aurora::SSFFile * getSSF()
Return the object&#39;s sound set.
Definition: object.cpp:100
void warning(const char *s,...)
Definition: util.cpp:33
virtual void enter()
The cursor entered the object.
Definition: object.cpp:169
const Common::UString & getName() const
Return the object&#39;s name.
Definition: object.cpp:88
std::vector< const GFF3Struct * > GFF3List
Definition: types.h:449
Voice/Speech.
Definition: types.h:46
Sound::ChannelHandle playSound(const Common::UString &sound, Sound::SoundType soundType, bool loop, float volume, bool pitchVariance)
Play this sound resource.
Definition: util.cpp:81
virtual void setOrientation(float x, float y, float z, float angle)
Set the object&#39;s orientation.
Definition: object.cpp:162
bool isStatic() const
Is the object static (not manipulable at all)?
Definition: object.cpp:106
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:284
const GFF3List & getList(const Common::UString &field) const
Definition: gff3file.cpp:741
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
Area * _area
The area the object is currently in.
Definition: object.h:160
virtual void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: object.cpp:156
A struct within a GFF3.
Definition: gff3file.h:164
uint32_t uint32
Definition: types.h:204
virtual void unloadModel()
Unload the object&#39;s model(s).
Definition: object.cpp:79
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 getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:142
An object in a Neverwinter Nights 2 area.
void loadSSF()
Load the object&#39;s sound set.
Definition: object.cpp:182
bool isUsable() const
Can the object be used by the PC?
Definition: object.cpp:110
bool _static
Is the object static?
Definition: object.h:155
void setArea(Area *)
Set the area this object is currently in.
Definition: object.cpp:126
Generic Aurora engines utility functions.
ObjectType
Object type, matches the bitfield in nwscript.nss.
Definition: types.h:35
Common::ScopedPtr< Aurora::SSFFile > _ssf
The object&#39;s sound set.
Definition: object.h:153
void setVariable(const Common::UString &var, const Variable &value)
void setFacing(float facing)
Set the location&#39;s orientation.
Definition: location.cpp:68
virtual void show()
Show the object&#39;s model(s).
Definition: object.cpp:82
Location getLocation() const
Create a Location out of the object&#39;s area, position and orientation.
Definition: object.cpp:130
Manager for tokens in Aurora engines text strings.
NWScript types.
virtual bool click(Object *triggerer=0)
The object was clicked.
Definition: object.cpp:178
NWScript utility functions.
Handling BioWare&#39;s DLGs (dialog / conversation files).
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:72
uint32 generateIDNumber()
Definition: uuid.cpp:46
LocString text
The actual text of the entry.
Definition: dlgfile.h:71
int32_t int32
Definition: types.h:203
Common::UString _description
The object&#39;s description.
Definition: object.h:148