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 "src/common/scopedptr.h"
26 #include "src/common/util.h"
27 
28 #include "src/aurora/gff3file.h"
29 #include "src/aurora/2dafile.h"
30 #include "src/aurora/2dareg.h"
31 
33 
35 
37 
38 namespace Engines {
39 
40 namespace NWN {
41 
43  _state(kStateDefault), _hasInventory(false) {
44 
45  load(placeable);
46 }
47 
49 }
50 
51 void Placeable::load(const Aurora::GFF3Struct &placeable) {
52  Common::UString temp = placeable.getString("TemplateResRef");
53 
55  if (!temp.empty())
56  utp.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTP, MKTAG('U', 'T', 'P', ' '), true));
57 
58  Situated::load(placeable, utp ? &utp->getTopLevel() : 0);
59 }
60 
62  if (!_model)
63  return;
64 
65  switch (_state) {
66  case kStateDefault:
67  _model->setState("default");
68  break;
69 
70  case kStateOpen:
71  _model->setState("open");
72  break;
73 
74  case kStateClosed:
75  _model->setState("close");
76  break;
77 
78  case kStateDestroyed:
79  _model->setState("dead");
80  break;
81 
82  case kStateActivated:
83  _model->setState("on");
84  break;
85 
86  case kStateDeactivated:
87  _model->setState("off");
88  break;
89 
90  default:
91  _model->setState("");
92  break;
93  }
94 
95 }
96 
98  setModelState();
99 
100  Situated::show();
101 }
102 
104  leave();
105 
106  Situated::hide();
107 }
108 
110  // State
111 
112  _state = (State) gff.getUint("AnimationState", (uint) _state);
113 
114  _hasInventory = gff.getBool("HasInventory", _hasInventory);
115 }
116 
118  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("placeables");
119 
120  _modelName = twoda.getRow(_appearanceID).getString("ModelName");
121  _soundAppType = twoda.getRow(_appearanceID).getInt("SoundAppType");
122 }
123 
125  highlight(true);
126 }
127 
129  highlight(false);
130 }
131 
132 void Placeable::highlight(bool enabled) {
133  if (_model)
134  _model->drawBound(enabled);
135 
136  if (enabled)
138  else
139  hideTooltip();
140 }
141 
142 bool Placeable::isOpen() const {
143  return (_state == kStateOpen) || (_state == kStateActivated);
144 }
145 
147  return isOpen();
148 }
149 
150 bool Placeable::click(Object *triggerer) {
151  // If the placeable is locked, just play the appropriate sound and bail
152  if (isLocked()) {
154  return false;
155  }
156 
157  // If the object was destroyed, nothing more can be done with it
158  if (_state == kStateDestroyed)
159  return true;
160 
161  _lastUsedBy = triggerer;
162 
163  // Objects with an inventory toggle between open and closed
164  if (_hasInventory) {
165  if (isOpen())
166  return close(triggerer);
167 
168  return open(triggerer);
169  }
170 
171  // Objects without an inventory toggle between activated and deactivated
172  if (isActivated())
173  return deactivate(triggerer);
174 
175  return activate(triggerer);
176 }
177 
178 bool Placeable::open(Object *opener) {
179  if (!_hasInventory)
180  return false;
181 
182  if (isOpen())
183  return true;
184 
185  if (isLocked()) {
187  return false;
188  }
189 
190  _lastOpenedBy = opener;
191 
193  runScript(kScriptOpen, this, opener);
194 
195  _state = kStateOpen;
196 
197  return true;
198 }
199 
200 bool Placeable::close(Object *closer) {
201  if (!_hasInventory)
202  return false;
203 
204  if (!isOpen())
205  return true;
206 
207  _lastClosedBy = closer;
208 
210  runScript(kScriptClosed, this, closer);
211 
213 
214  return true;
215 }
216 
218  if (_hasInventory)
219  return false;
220 
221  if (isActivated())
222  return true;
223 
224  if (isLocked()) {
226  return false;
227  }
228 
230  runScript(kScriptUsed, this, user);
231 
233 
234  return true;
235 }
236 
238  if (_hasInventory)
239  return false;
240 
241  if (!isActivated())
242  return true;
243 
244  if (isLocked()) {
246  return false;
247  }
248 
250  runScript(kScriptUsed, this, user);
251 
253 
254  return true;
255 }
256 
258  static const Common::UString kEmptySound;
259 
260  switch (animation) {
263  return _soundClosed;
264 
266  return _soundOpened;
267 
269  return _soundClosed;
270 
271  default:
272  break;
273  }
274 
275  return kEmptySound;
276 }
277 
278 void Placeable::playAnimation(const Common::UString &animation, bool restart, float length, float speed) {
279  Situated::playAnimation(animation, restart, length, speed);
280 }
281 
282 void Placeable::playAnimation(Animation animation, bool restart, float length, float speed) {
283  if (!isAnimationLooping(animation))
284  length = 0.0f;
285 
286  const Common::UString anim = getPlaceableAnimationName(animation);
287  const Common::UString &sound = getAnimationSound(animation);
288 
289  playSound(sound);
290  if (_model)
291  _model->playAnimation(anim, restart, length, speed);
292 }
293 
294 } // End of namespace NWN
295 
296 } // 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).
#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
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 load(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:51
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
A placeable object in a Neverwinter Nights area.
void setModelState()
Sync the model&#39;s state with the placeable&#39;s state.
Definition: placeable.cpp:61
const Common::UString & getAnimationSound(Animation animation)
Definition: placeable.cpp:257
State _state
The current state of the placeable.
Definition: placeable.h:101
bool isOpen() const
Is the placeable open?
Definition: placeable.cpp:142
bool activate(Object *opener)
The user object activates this placeable.
Definition: placeable.cpp:217
void load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint=0)
Load the situated object from an instance and its blueprint.
Definition: situated.cpp:141
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
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:83
void leave()
The cursor left the placeable.
Definition: placeable.cpp:128
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:92
State
The state of a placeable.
Definition: placeable.h:39
A simple scoped smart pointer template.
bool click(Object *triggerer=0)
The placeable was clicked.
Definition: placeable.cpp:150
virtual void playAnimation(const Common::UString &animation="", bool restart=true, float length=0.0f, float speed=1.0f)
Play an object animation.
Definition: object.cpp:274
Placeable(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:42
void hide()
Hide the placeable&#39;s model.
Definition: placeable.cpp:103
Utility templates and functions.
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:82
void loadObject(const Aurora::GFF3Struct &gff)
Load placeable-specific properties.
Definition: placeable.cpp:109
Placeable template (user), GFF.
Definition: types.h:109
Handling BioWare&#39;s 2DAs (two-dimensional array).
bool deactivate(Object *closer)
The user object deactivates this placeable.
Definition: placeable.cpp:237
void highlight(bool enabled)
(Un)Highlight the placeable.
Definition: placeable.cpp:132
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
A 3D model of an object.
bool showFeedbackTooltip()
Create and show a tooltip with the name and/or portrait of the object.
Definition: object.cpp:314
void enter()
The cursor entered the placeable.
Definition: placeable.cpp:124
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:248
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
void show()
Show the placeable&#39;s model.
Definition: placeable.cpp:97
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:89
The global 2DA registry.
uint32 _appearanceID
The index within the situated appearance 2DA.
Definition: situated.h:77
uint32 _soundAppType
The index within the situated sounds 2DA.
Definition: situated.h:78
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:90
void loadAppearance()
Load appearance-specific properties.
Definition: placeable.cpp:117
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:88
void playAnimation(const Common::UString &animation="", bool restart=true, float length=0.0f, float speed=1.0f)
Play a placeable animation.
Definition: placeable.cpp:278
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
bool isAnimationLooping(Animation animation)
Return true if the specified animation allows looping.
Definition: types.cpp:166
A struct within a GFF3.
Definition: gff3file.h:164
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:121
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Generic Aurora engines utility functions.
Common::UString getPlaceableAnimationName(Animation animation)
Return the name (as found in the models) of a specific placeable animation.
Definition: types.cpp:136
bool open(Object *opener)
The opener object opens this placeable.
Definition: placeable.cpp:178
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:139
bool _hasInventory
Does this placeable have an inventory?
Definition: placeable.h:103
bool isActivated() const
Is the placeable activated?
Definition: placeable.cpp:146
void hideTooltip()
Hide the tooltip again.
Definition: object.cpp:337
void show()
Show the situated object&#39;s model.
Definition: situated.cpp:93
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:98
unsigned int uint
Definition: types.h:211
bool close(Object *closer)
The closer object closes this placeable.
Definition: placeable.cpp:200
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:75