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/locstring.h"
30 #include "src/aurora/gff3file.h"
31 #include "src/aurora/2dafile.h"
32 #include "src/aurora/2dareg.h"
33 
35 
38 
40 
41 namespace Engines {
42 
43 namespace Witcher {
44 
46  _soundAppType(Aurora::kFieldIDInvalid), _locked(false),
47  _lastOpenedBy(0), _lastClosedBy(0), _lastUsedBy(0) {
48 
49 }
50 
52 }
53 
55  if (_model)
56  return;
57 
58  if (_modelName.empty()) {
59  warning("Situated object \"%s\" (\"%s\") has no model", _name.getString().c_str(), _tag.c_str());
60  return;
61  }
62 
64  if (!_model)
65  throw Common::Exception("Failed to load situated object model \"%s\"",
66  _modelName.c_str());
67 
68  // Positioning
69 
70  float x, y, z, angle;
71 
72  getPosition(x, y, z);
73  setPosition(x, y, z);
74 
75  getOrientation(x, y, z, angle);
76  setOrientation(x, y, z, angle);
77 
78  // Clickable
79 
80  _model->setTag(_tag);
81  _model->setClickable(isClickable());
82 
83  _ids.push_back(_model->getID());
84 }
85 
87  hide();
88 
89  _model.reset();
90 }
91 
93  if (_model)
94  _model->show();
95 }
96 
98  if (_model)
99  _model->hide();
100 }
101 
102 void Situated::setPosition(float x, float y, float z) {
103  Object::setPosition(x, y, z);
104  Object::getPosition(x, y, z);
105 
106  if (_model)
107  _model->setPosition(x, y, z);
108 }
109 
110 void Situated::setOrientation(float x, float y, float z, float angle) {
111  Object::setOrientation(x, y, z, angle);
112  Object::getOrientation(x, y, z, angle);
113 
114  if (_model)
115  _model->setOrientation(x, y, z, angle);
116 }
117 
118 bool Situated::isLocked() const {
119  return _locked;
120 }
121 
122 void Situated::setLocked(bool locked) {
123  _locked = locked;
124 }
125 
127  return _lastOpenedBy;
128 }
129 
131  return _lastClosedBy;
132 }
133 
135  return _lastUsedBy;
136 }
137 
138 void Situated::load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint) {
139  // General properties
140 
141  if (blueprint)
142  loadProperties(*blueprint); // Blueprint
143  loadProperties(instance); // Instance
144 
145 
146  // Specialized object properties
147 
148  if (blueprint)
149  loadObject(*blueprint); // Blueprint
150  loadObject(instance); // Instance
151 
152 
153  // Sounds
154 
155  loadSounds();
156 
157  // Position
158 
159  setPosition(instance.getDouble("X"),
160  instance.getDouble("Y"),
161  instance.getDouble("Z"));
162 
163  // Orientation
164 
165  float bearing = instance.getDouble("Bearing");
166 
167  float rotX = 0.0f;
168  float rotY = 0.0f;
169  float rotZ = 1.0f;
170  float rotW = Common::rad2deg(bearing);
171 
172  if (instance.hasField("OrientationW")) {
173  rotX = instance.getDouble("OrientationX");
174  rotY = instance.getDouble("OrientationY");
175  rotZ = instance.getDouble("OrientationZ");
176  rotW = Common::rad2deg(acos(instance.getDouble("OrientationW")) * 2.0);
177  }
178 
179  setOrientation(rotX, rotY, rotZ, rotW);
180 }
181 
183  // Unique ID and tag
184  _uniqueID = gff.getString("UniqueID", _uniqueID);
185  _tag = gff.getString("Tag", _tag);
186 
187  // Name
188  gff.getLocString("LocName", _name);
189 
190  // Description
191  gff.getLocString("Description", _description);
192 
194 
195  // Appearance
196  _modelName = gff.getString("ModelName", _modelName);
197 
198  // Sounds
199  _soundAppType = gff.getUint("SoundAppType", _soundAppType);
200 
201  // Conversation
202  _conversation = gff.getString("Conversation", _conversation);
203 
204  // Static
205  _static = gff.getBool("Static", _static);
206 
207  // Usable
208  _usable = gff.getBool("Useable", _usable);
209 
210  // Locked
211  _locked = gff.getBool("Locked", _locked);
212 
213  // Scripts
214  readScripts(gff);
215 }
216 
219  return;
220 
221  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("placeableobjsnds");
222 
223  _soundOpened = twoda.getRow(_soundAppType).getString("Opened");
224  _soundClosed = twoda.getRow(_soundAppType).getString("Closed");
225  _soundDestroyed = twoda.getRow(_soundAppType).getString("Destroyed");
226  _soundUsed = twoda.getRow(_soundAppType).getString("Used");
227  _soundLocked = twoda.getRow(_soundAppType).getString("Locked");
228 }
229 
230 } // End of namespace Witcher
231 
232 } // End of namespace Engines
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void loadModel()
Load the situated object&#39;s model.
Definition: situated.cpp:54
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
Common::UString _soundUsed
The sound the object makes when used.
Definition: situated.h:85
void load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint=0)
Load the situated object from an instance and its blueprint.
Definition: situated.cpp:138
virtual void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: object.cpp:146
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
bool getLocString(const Common::UString &field, LocString &str) const
Definition: gff3file.cpp:614
An object within a Witcher area.
Definition: object.h:51
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:92
Object * getLastClosedBy() const
Return the object that last closed this situated object.
Definition: situated.cpp:130
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
Mathematical helpers.
A situated object in a The Witcher area.
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:129
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:132
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:76
const Common::UString & getString(Language language, LanguageGender gender=kLanguageGenderCurrent) const
Get the string of that language.
Definition: locstring.cpp:82
bool _locked
Is the situated object locked?
Definition: situated.h:80
bool isClickable() const
Can the player click the object?
Definition: object.cpp:104
Object * getLastOpenedBy() const
Return the object that last opened this situated object.
Definition: situated.cpp:126
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:90
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
virtual void setLocked(bool locked)
Lock/Unlock the situated object.
Definition: situated.cpp:122
virtual void getOrientation(float &x, float &y, float &z, float &angle) const
Return the object&#39;s orientation.
Definition: object.cpp:138
Utility templates and functions.
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:83
Aurora::LocString _description
The object&#39;s description.
Definition: object.h:147
uint32 _soundAppType
The index within the situated sounds 2DA.
Definition: situated.h:78
double getDouble(const Common::UString &field, double def=0.0) const
Definition: gff3file.cpp:514
Common::UString _soundDestroyed
The sound the object makes when destroyed.
Definition: situated.h:84
Handling BioWare&#39;s 2DAs (two-dimensional array).
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
A 3D model of an object.
Common::UString _uniqueID
A globally unique ID.
Definition: object.h:144
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void loadProperties(const Aurora::GFF3Struct &gff)
Definition: situated.cpp:182
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
void setOrientation(float x, float y, float z, float angle)
Set the situated object&#39;s orientation.
Definition: situated.cpp:110
StackException Exception
Definition: error.h:59
void unloadModel()
Unload the situated object&#39;s model.
Definition: situated.cpp:86
The global 2DA registry.
bool _static
Is the object static?
Definition: object.h:151
void warning(const char *s,...)
Definition: util.cpp:33
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:118
Handling BioWare&#39;s localized strings.
virtual void refreshLocalized()
Refresh all localized strings.
Definition: object.cpp:93
bool _usable
Is the object usable?
Definition: object.h:152
void setPosition(float x, float y, float z)
Set the situated object&#39;s position.
Definition: situated.cpp:102
virtual void setOrientation(float x, float y, float z, float angle)
Set the object&#39;s orientation.
Definition: object.cpp:152
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:88
void show()
Show the situated object&#39;s model.
Definition: situated.cpp:92
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:89
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A struct within a GFF3.
Definition: gff3file.h:164
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:82
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
Common::UString _conversation
The object&#39;s default conversation.
Definition: object.h:149
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
static float rad2deg(float rad)
Definition: maths.h:93
Aurora::LocString _name
The object&#39;s name.
Definition: object.h:146
Generic Aurora engines utility functions.
Object * getLastUsedBy() const
Return the object that last used this situated object.
Definition: situated.cpp:134
virtual void loadObject(const Aurora::GFF3Struct &gff)=0
Load object-specific properties.
Graphics::Aurora::Model * loadModelObject(const Common::UString &resref, const Common::UString &texture)
Definition: model.cpp:47
Situated(ObjectType type)
Definition: situated.cpp:45
std::list< uint32 > _ids
The object&#39;s model IDs.
Definition: object.h:154
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:97
Generic Aurora engines model functions.