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