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