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/util.h"
26 #include "src/common/scopedptr.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 NWN2 {
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', ' ')));
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  if (_modelName.empty())
121  _modelName = twoda.getRow(_appearanceID).getString("ModelName");
122  if (_modelName == "RESERVED")
123  _modelName.clear();
124 
125  if (_modelName.empty())
126  _modelName = twoda.getRow(_appearanceID).getString("NWN2_ModelName");
127 
128  _soundAppType = twoda.getRow(_appearanceID).getInt("SoundAppType");
129 }
130 
132  highlight(true);
133 }
134 
136  highlight(false);
137 }
138 
139 void Placeable::highlight(bool enabled) {
140  if (_model)
141  _model->drawBound(enabled);
142 }
143 
144 bool Placeable::isOpen() const {
145  return _state == kStateOpen;
146 }
147 
149  return isOpen();
150 }
151 
152 bool Placeable::click(Object *triggerer) {
153  // If the placeable is locked, just play the appropriate sound and bail
154  if (isLocked()) {
156  return false;
157  }
158 
159  // If the object was destroyed, nothing more can be done with it
160  if (_state == kStateDestroyed)
161  return true;
162 
163  _lastUsedBy = triggerer;
164 
165  // Objects with an inventory toggle between open and closed
166  if (_hasInventory) {
167  if (isOpen())
168  return close(triggerer);
169 
170  return open(triggerer);
171  }
172 
173  // Objects without an inventory toggle between activated and deactivated
174  if (isActivated())
175  return deactivate(triggerer);
176 
177  return activate(triggerer);
178 }
179 
180 bool Placeable::open(Object *opener) {
181  if (!_hasInventory)
182  return false;
183 
184  if (isOpen())
185  return true;
186 
187  if (isLocked()) {
189  return false;
190  }
191 
192  _lastOpenedBy = opener;
193 
195  runScript(kScriptOpen, this, opener);
196 
197  _state = kStateOpen;
198 
199  return true;
200 }
201 
202 bool Placeable::close(Object *closer) {
203  if (!_hasInventory)
204  return false;
205 
206  if (!isOpen())
207  return true;
208 
209  _lastClosedBy = closer;
210 
212  runScript(kScriptClosed, this, closer);
213 
215 
216  return true;
217 }
218 
220  if (_hasInventory)
221  return false;
222 
223  if (isActivated())
224  return true;
225 
226  if (isLocked()) {
228  return false;
229  }
230 
232  runScript(kScriptUsed, this, user);
233 
235 
236  return true;
237 }
238 
240  if (_hasInventory)
241  return false;
242 
243  if (!isActivated())
244  return true;
245 
246  if (isLocked()) {
248  return false;
249  }
250 
252  runScript(kScriptUsed, this, user);
253 
255 
256  return true;
257 }
258 
259 } // End of namespace NWN2
260 
261 } // 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
uint32 _soundAppType
The index within the situated sounds 2DA.
Definition: situated.h:78
void loadObject(const Aurora::GFF3Struct &gff)
Load placeable-specific properties.
Definition: placeable.cpp:109
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
void loadAppearance()
Load appearance-specific properties.
Definition: placeable.cpp:117
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
void hide()
Hide the placeable&#39;s model.
Definition: placeable.cpp:103
bool _hasInventory
Does this placeable have an inventory?
Definition: placeable.h:94
bool isOpen() const
Is the placeable open?
Definition: placeable.cpp:144
Placeable(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:42
void setModelState()
Sync the model&#39;s state with the placeable&#39;s state.
Definition: placeable.cpp:61
State _state
The current state of the placeable.
Definition: placeable.h:92
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
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:127
An object within a NWN2 area.
Definition: object.h:58
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:90
A simple scoped smart pointer template.
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:83
void leave()
The cursor left the placeable.
Definition: placeable.cpp:135
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:75
void show()
Show the situated object&#39;s model.
Definition: situated.cpp:101
Utility templates and functions.
void load(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:51
Placeable template (user), GFF.
Definition: types.h:109
Handling BioWare&#39;s 2DAs (two-dimensional array).
void highlight(bool enabled)
(Un)Highlight the placeable.
Definition: placeable.cpp:139
bool open(Object *opener)
The opener object opens this placeable.
Definition: placeable.cpp:180
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
The global 2DA registry.
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
bool deactivate(Object *closer)
The user object deactivates this placeable.
Definition: placeable.cpp:239
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:94
uint32 _appearanceID
The index within the situated appearance 2DA.
Definition: situated.h:77
void enter()
The cursor entered the placeable.
Definition: placeable.cpp:131
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:284
State
The state of a placeable.
Definition: placeable.h:39
Common::UString _soundUsed
The sound the object makes when used.
Definition: situated.h:85
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A struct within a GFF3.
Definition: gff3file.h:164
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:91
A placeable object in a Neverwinter Nights 2 area.
bool isActivated() const
Is the placeable activated?
Definition: placeable.cpp:148
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:92
void show()
Show the placeable&#39;s model.
Definition: placeable.cpp:97
Generic Aurora engines utility functions.
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:147
void load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint=0)
Load the situated object from an instance and its blueprint.
Definition: situated.cpp:147
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:106
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:82
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
bool click(Object *triggerer=0)
The placeable was clicked.
Definition: placeable.cpp:152
bool close(Object *closer)
The closer object closes this placeable.
Definition: placeable.cpp:202
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
unsigned int uint
Definition: types.h:211
bool activate(Object *opener)
The user object activates this placeable.
Definition: placeable.cpp:219