xoreos  0.0.5
placeable.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 "glm/gtc/type_ptr.hpp"
26 #include "glm/gtc/matrix_transform.hpp"
27 
28 #include "src/common/scopedptr.h"
29 #include "src/common/util.h"
30 #include "src/common/maths.h"
31 
32 #include "src/aurora/gff3file.h"
33 #include "src/aurora/2dafile.h"
34 #include "src/aurora/2dareg.h"
35 
37 
39 
41 
42 namespace Engines {
43 
44 namespace KotOR2 {
45 
47  _state(kStateDefault), _hasInventory(false) {
48 
49  load(placeable);
50 }
51 
53 }
54 
55 void Placeable::load(const Aurora::GFF3Struct &placeable) {
56  Common::UString temp = placeable.getString("TemplateResRef");
57 
59  if (!temp.empty())
60  utp.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTP, MKTAG('U', 'T', 'P', ' ')));
61 
62  Situated::load(placeable, utp ? &utp->getTopLevel() : 0);
63 
64  if (!utp)
65  warning("Placeable \"%s\" has no blueprint", _tag.c_str());
66 
67  if (!_modelName.empty()) {
68  glm::mat4 transform;
69  transform = glm::translate(transform, glm::make_vec3(_position));
70  transform = glm::rotate(transform, Common::deg2rad(_orientation[3]), glm::make_vec3(_orientation));
71 
73  }
74 }
75 
77  leave();
78 
80 }
81 
83  // State
84 
85  _state = (State) gff.getUint("AnimationState", (uint) _state);
86 
87  _hasInventory = gff.getBool("HasInventory", _hasInventory);
88 }
89 
92  return;
93 
94  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("placeables");
95 
96  _modelName = twoda.getRow(_appearanceID).getString("ModelName");
97  _soundAppType = twoda.getRow(_appearanceID).getInt("SoundAppType");
98 }
99 
101  highlight(true);
102 }
103 
105  highlight(false);
106 }
107 
108 void Placeable::highlight(bool enabled) {
109  if (_model)
110  _model->drawBound(enabled);
111 }
112 
113 bool Placeable::isOpen() const {
114  return (_state == kStateOpen) || (_state == kStateActivated);
115 }
116 
118  return isOpen();
119 }
120 
121 bool Placeable::click(Object *triggerer) {
122  // If the placeable is locked, just play the appropriate sound and bail
123  if (isLocked()) {
125  return false;
126  }
127 
128  // If the object was destroyed, nothing more can be done with it
129  if (_state == kStateDestroyed)
130  return true;
131 
132  _lastUsedBy = triggerer;
133 
134  // Objects with an inventory toggle between open and closed
135  if (_hasInventory) {
136  if (isOpen())
137  return close(triggerer);
138 
139  return open(triggerer);
140  }
141 
142  // Objects without an inventory toggle between activated and deactivated
143  if (isActivated())
144  return deactivate(triggerer);
145 
146  return activate(triggerer);
147 }
148 
149 bool Placeable::open(Object *opener) {
150  if (!_hasInventory)
151  return false;
152 
153  if (isOpen())
154  return true;
155 
156  if (isLocked()) {
158  return false;
159  }
160 
161  _lastOpenedBy = opener;
162 
164  runScript(kScriptOpen, this, opener);
165 
166  _state = kStateOpen;
167 
168  return true;
169 }
170 
171 bool Placeable::close(Object *closer) {
172  if (!_hasInventory)
173  return false;
174 
175  if (!isOpen())
176  return true;
177 
178  _lastClosedBy = closer;
179 
181  runScript(kScriptClosed, this, closer);
182 
184 
185  return true;
186 }
187 
189  if (_hasInventory)
190  return false;
191 
192  if (isActivated())
193  return true;
194 
195  if (isLocked()) {
197  return false;
198  }
199 
201  runScript(kScriptUsed, this, user);
202 
204 
205  return true;
206 }
207 
209  if (_hasInventory)
210  return false;
211 
212  if (!isActivated())
213  return true;
214 
215  if (isLocked()) {
217  return false;
218  }
219 
221  runScript(kScriptUsed, this, user);
222 
224 
225  return true;
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
Placeable walk mesh.
Definition: types.h:119
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
bool click(Object *triggerer=0)
The placeable was clicked.
Definition: placeable.cpp:121
#define MKTAG(a0, a1, a2, a3)
A wrapper macro used around four character constants, like &#39;DATA&#39;, to ensure portability.
Definition: endianness.h:140
bool isActivated() const
Is the placeable activated?
Definition: placeable.cpp:117
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 reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
float _position[3]
The object&#39;s position.
Definition: object.h:139
float _orientation[4]
The object&#39;s orientation.
Definition: object.h:140
bool activate(Object *opener)
The user object activates this placeable.
Definition: placeable.cpp:188
Mathematical helpers.
void hide()
Hide the placeable&#39;s model.
Definition: placeable.cpp:76
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:95
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
Aurora::GFF3File * loadOptionalGFF3(const Common::UString &gff3, Aurora::FileType type, uint32 id, bool repairNWNPremium)
Load a GFF3, but return 0 instead of throwing on error.
Definition: util.cpp:150
State _state
The current state of the placeable.
Definition: placeable.h:91
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
A simple scoped smart pointer template.
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:102
A placeable in a Star Wars: Knights of the Old Republic II - The Sith Lords area. ...
bool close(Object *closer)
The closer object closes this placeable.
Definition: placeable.cpp:171
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
State
The state of a placeable.
Definition: placeable.h:39
Utility templates and functions.
void enter()
The cursor entered the placeable.
Definition: placeable.cpp:100
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:104
Placeable template (user), GFF.
Definition: types.h:109
Handling BioWare&#39;s 2DAs (two-dimensional array).
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:150
Placeable(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:46
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
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:97
The global 2DA registry.
uint32 _soundAppType
The index within the situated sounds 2DA.
Definition: situated.h:90
void highlight(bool enabled)
(Un)Highlight the placeable.
Definition: placeable.cpp:108
void warning(const char *s,...)
Definition: util.cpp:33
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:169
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:58
Engines::KotOR::Walkmesh _walkmesh
Definition: situated.h:106
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
void leave()
The cursor left the placeable.
Definition: placeable.cpp:104
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:94
void loadAppearance()
Load appearance-specific properties.
Definition: placeable.cpp:90
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
bool _hasInventory
Does this placeable have an inventory?
Definition: placeable.h:93
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Generic Aurora engines utility functions.
bool isOpen() const
Is the placeable open?
Definition: placeable.cpp:113
bool deactivate(Object *closer)
The user object deactivates this placeable.
Definition: placeable.cpp:208
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:101
bool open(Object *opener)
The opener object opens this placeable.
Definition: placeable.cpp:149
static float deg2rad(float deg)
Definition: maths.h:97
void loadObject(const Aurora::GFF3Struct &gff)
Load placeable-specific properties.
Definition: placeable.cpp:82
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:87
void load(const Common::UString &resRef, ::Aurora::FileType type=::Aurora::kFileTypeWOK, const glm::mat4 &transform=glm::mat4())
Definition: walkmesh.cpp:35
void load(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:55
unsigned int uint
Definition: types.h:211