xoreos  0.0.5
situated.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/error.h"
26 #include "src/common/maths.h"
27 #include "src/common/util.h"
28 
29 #include "src/aurora/gff3file.h"
30 #include "src/aurora/2dafile.h"
31 #include "src/aurora/2dareg.h"
32 
34 
37 
39 
40 namespace Engines {
41 
42 namespace NWN {
43 
45  _soundAppType(Aurora::kFieldIDInvalid), _locked(false),
46  _lastOpenedBy(0), _lastClosedBy(0), _lastUsedBy(0) {
47 
48 }
49 
51 }
52 
54  if (_model)
55  return;
56 
57  if (_modelName.empty()) {
58  warning("Situated object \"%s\" (\"%s\") has no model", _name.c_str(), _tag.c_str());
59  return;
60  }
61 
63  if (!_model)
64  throw Common::Exception("Failed to load situated object model \"%s\"",
65  _modelName.c_str());
66 
67  // Positioning
68 
69  float x, y, z, angle;
70 
71  getPosition(x, y, z);
72  setPosition(x, y, z);
73 
74  getOrientation(x, y, z, angle);
75  setOrientation(x, y, z, angle);
76 
77  // Clickable
78 
79  _model->setTag(_tag);
80  _model->setClickable(isClickable());
81 
82  _ids.push_back(_model->getID());
83 }
84 
86  hide();
87 
89 
90  _model.reset();
91 }
92 
94  if (_model)
95  _model->show();
96 }
97 
99  hideTooltip();
100 
101  if (_model)
102  _model->hide();
103 }
104 
105 void Situated::setPosition(float x, float y, float z) {
106  Object::setPosition(x, y, z);
107  Object::getPosition(x, y, z);
108 
109  if (_model)
110  _model->setPosition(x, y, z);
111 }
112 
113 void Situated::setOrientation(float x, float y, float z, float angle) {
114  Object::setOrientation(x, y, z, angle);
115  Object::getOrientation(x, y, z, angle);
116 
117  if (_model)
118  _model->setOrientation(x, y, z, angle);
119 }
120 
121 bool Situated::isLocked() const {
122  return _locked;
123 }
124 
125 void Situated::setLocked(bool locked) {
126  _locked = locked;
127 }
128 
130  return _lastOpenedBy;
131 }
132 
134  return _lastClosedBy;
135 }
136 
138  return _lastUsedBy;
139 }
140 
141 void Situated::load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint) {
142  // General properties
143 
144  if (blueprint)
145  loadProperties(*blueprint); // Blueprint
146  loadProperties(instance); // Instance
147 
148 
149  // Specialized object properties
150 
151  if (blueprint)
152  loadObject(*blueprint); // Blueprint
153  loadObject(instance); // Instance
154 
155 
156  // Appearance
157 
159  throw Common::Exception("Situated object without an appearance");
160 
161  loadAppearance();
162  loadSounds();
163 
164 
165  // Position
166 
167  setPosition(instance.getDouble("X"),
168  instance.getDouble("Y"),
169  instance.getDouble("Z"));
170 
171  // Orientation
172 
173  float bearing = instance.getDouble("Bearing");
174 
175  setOrientation(0.0f, 0.0f, 1.0f, Common::rad2deg(bearing));
176 }
177 
179  // Tag
180  _tag = gff.getString("Tag", _tag);
181 
182  // Name
183  _name = gff.getString("LocName", _name);
184 
185  // Description
186  _description = gff.getString("Description", _description);
187 
188  // Portrait
189  loadPortrait(gff);
190 
191  // Appearance
192  _appearanceID = gff.getUint("Appearance", _appearanceID);
193 
194  // Conversation
195  _conversation = gff.getString("Conversation", _conversation);
196 
197  // Static
198  _static = gff.getBool("Static", _static);
199 
200  // Usable
201  _usable = gff.getBool("Useable", _usable);
202 
203  // Locked
204  _locked = gff.getBool("Locked", _locked);
205 
206  // Scripts
207  readScripts(gff);
208 }
209 
211  uint32 portraitID = gff.getUint("PortraitId");
212  if (portraitID != 0) {
213  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("portraits");
214 
215  Common::UString portrait = twoda.getRow(portraitID).getString("BaseResRef");
216  if (!portrait.empty())
217  _portrait = "po_" + portrait;
218  }
219 
220  _portrait = gff.getString("Portrait", _portrait);
221 }
222 
225  return;
226 
227  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("placeableobjsnds");
228 
229  _soundOpened = twoda.getRow(_soundAppType).getString("Opened");
230  _soundClosed = twoda.getRow(_soundAppType).getString("Closed");
231  _soundDestroyed = twoda.getRow(_soundAppType).getString("Destroyed");
232  _soundUsed = twoda.getRow(_soundAppType).getString("Used");
233  _soundLocked = twoda.getRow(_soundAppType).getString("Locked");
234 }
235 
237  if (!_model)
238  return false;
239 
240  if (!_tooltip) {
241  _tooltip.reset(new Tooltip(type, *_model));
242 
243  _tooltip->setAlign(0.5f);
244  _tooltip->setPortrait(_portrait);
245  }
246 
247  return true;
248 }
249 
250 } // End of namespace NWN
251 
252 } // End of namespace Engines
virtual void loadAppearance()=0
Load appearance-specific properties.
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
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
virtual void getOrientation(float &x, float &y, float &z, float &angle) const
Return the object&#39;s orientation.
Definition: object.cpp:162
virtual void setLocked(bool locked)
Lock/Unlock the situated object.
Definition: situated.cpp:125
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:123
bool getBool(const Common::UString &field, bool def=false) const
Definition: gff3file.cpp:510
A class holding an UTF-8 string.
Definition: ustring.h:48
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
Object * getLastClosedBy() const
Return the object that last closed this situated object.
Definition: situated.cpp:133
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
Situated(ObjectType type)
Definition: situated.cpp:44
bool _locked
Is the situated object locked?
Definition: situated.h:80
Object * getLastOpenedBy() const
Return the object that last opened this situated object.
Definition: situated.cpp:129
Common::UString _name
The object&#39;s display name.
Definition: object.h:165
Mathematical helpers.
void load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint=0)
Load the situated object from an instance and its blueprint.
Definition: situated.cpp:141
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:83
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:92
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
void loadProperties(const Aurora::GFF3Struct &gff)
Definition: situated.cpp:178
Utility templates and functions.
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:82
double getDouble(const Common::UString &field, double def=0.0) const
Definition: gff3file.cpp:514
Handling BioWare&#39;s 2DAs (two-dimensional array).
void unloadModel()
Unload the situated object&#39;s model.
Definition: situated.cpp:85
Common::ScopedPtr< Tooltip > _tooltip
The tooltip displayed over the object.
Definition: object.h:189
void setPosition(float x, float y, float z)
Set the situated object&#39;s position.
Definition: situated.cpp:105
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
Common::UString _soundDestroyed
The sound the object makes when destroyed.
Definition: situated.h:84
A 3D model of an object.
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
bool _static
Is the object static?
Definition: object.h:175
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
StackException Exception
Definition: error.h:59
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:89
The global 2DA registry.
uint32 _appearanceID
The index within the situated appearance 2DA.
Definition: situated.h:77
uint32 _soundAppType
The index within the situated sounds 2DA.
Definition: situated.h:78
void warning(const char *s,...)
Definition: util.cpp:33
void setOrientation(float x, float y, float z, float angle)
Set the situated object&#39;s orientation.
Definition: situated.cpp:113
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:90
void loadPortrait(const Aurora::GFF3Struct &gff)
Definition: situated.cpp:210
ObjectType
Object type, matches the bitfield in nwscript.nss.
Definition: types.h:36
Common::UString _portrait
The object&#39;s portrait.
Definition: object.h:168
A tooltip.
Definition: tooltip.h:50
Object * getLastUsedBy() const
Return the object that last used this situated object.
Definition: situated.cpp:137
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:88
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
virtual void loadObject(const Aurora::GFF3Struct &gff)=0
Load object-specific properties.
A struct within a GFF3.
Definition: gff3file.h:164
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:121
uint32_t uint32
Definition: types.h:204
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
bool _usable
Is the object usable?
Definition: object.h:176
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
void loadModel()
Load the situated object&#39;s model.
Definition: situated.cpp:53
static float rad2deg(float rad)
Definition: maths.h:93
Generic Aurora engines utility functions.
A situated object in a Neverwinter Nights area.
Graphics::Aurora::Model * loadModelObject(const Common::UString &resref, const Common::UString &texture)
Definition: model.cpp:47
bool createTooltip(Tooltip::Type type)
Create an empty tooltip.
Definition: situated.cpp:236
Common::UString _soundUsed
The sound the object makes when used.
Definition: situated.h:85
Common::UString _conversation
The object&#39;s default conversation.
Definition: object.h:170
void hideTooltip()
Hide the tooltip again.
Definition: object.cpp:337
void show()
Show the situated object&#39;s model.
Definition: situated.cpp:93
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:98
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:75
Generic Aurora engines model functions.