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