xoreos  0.0.5
door.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/error.h"
27 
28 #include "src/aurora/gff3file.h"
29 #include "src/aurora/2dafile.h"
30 #include "src/aurora/2dareg.h"
31 
34 
36 
37 #include "src/engines/nwn/door.h"
39 #include "src/engines/nwn/module.h"
40 
41 namespace Engines {
42 
43 namespace NWN {
44 
46  _module(&module), _invisible(false), _genericType(Aurora::kFieldIDInvalid),
47  _state(kStateClosed), _linkedToFlag(kLinkedToNothing), _evaluatedLink(false),
48  _link(0), _linkedDoor(0), _linkedWaypoint(0) {
49 
50  load(door);
51 }
52 
54 }
55 
56 void Door::load(const Aurora::GFF3Struct &door) {
57  const Common::UString temp = door.getString("TemplateResRef");
58 
60  if (!temp.empty())
61  utd.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTD, MKTAG('U', 'T', 'D', ' '), true));
62 
63  Situated::load(door, utd ? &utd->getTopLevel() : 0);
64 
65  setModelState();
66 }
67 
69  // Generic type
70 
71  _genericType = gff.getUint("GenericType", _genericType);
72 
73  // State
74 
75  _state = (State) gff.getUint("AnimationState", (uint) _state);
76 
77  // Linked to
78 
79  _linkedToFlag = (LinkedToFlag) gff.getUint("LinkedToFlags", (uint) _linkedToFlag);
80  _linkedTo = gff.getString("LinkedTo", _linkedTo);
81 }
82 
84  if (_appearanceID == 0) {
86  _invisible = true;
87  else
88  loadAppearance(TwoDAReg.get2DA("genericdoors"), _genericType);
89  } else
90  loadAppearance(TwoDAReg.get2DA("doortypes"), _appearanceID);
91 
92  // Invisible doors have no model and are always open
93  if (_invisible) {
94  _modelName.clear();
96  }
97 
98  // Doors have a default, fallback portrait
99  if (_portrait.empty())
100  _portrait = "po_door01_";
101 }
102 
104  uint32 modelColumn = twoda.headerToColumn("ModelName");
105  if (modelColumn == Aurora::kFieldIDInvalid)
106  modelColumn = twoda.headerToColumn("Model");
107 
108  _invisible = twoda.getRow(id).getInt("VisibleModel") == 0;
109  _modelName = twoda.getRow(id).getString(modelColumn);
110  _soundAppType = twoda.getRow(id).getInt("SoundAppType");
111 }
112 
114  if (!_model)
115  return;
116 
117  switch (_state) {
118  case kStateClosed:
119  _model->setState("closed");
120  break;
121 
122  case kStateOpened1:
123  _model->setState("opened1");
124  break;
125 
126  case kStateOpened2:
127  _model->setState("opened2");
128  break;
129 
130  case kStateDestroyed:
131  _model->setState("dead");
132  break;
133 
134  default:
135  _model->setState("");
136  break;
137  }
138 
139 }
140 
141 void Door::show() {
142  setModelState();
143 
144  Situated::show();
145 }
146 
147 void Door::hide() {
148  leave();
149 
150  Situated::hide();
151 }
152 
153 void Door::enter() {
154  highlight(true);
155 
156  evaluateLink();
157  if (isOpen() && _link)
158  CursorMan.set("trans", "up");
159  else
160  CursorMan.set("door", "up");
161 }
162 
163 void Door::leave() {
164  highlight(false);
165  CursorMan.set();
166 }
167 
168 void Door::highlight(bool enabled) {
169  if (_model)
170  _model->drawBound(enabled);
171 }
172 
173 void Door::setLocked(bool locked) {
174  if (isLocked() == locked)
175  return;
176 
177  Situated::setLocked(locked);
178  // Also lock/unlock the linked door
179  evaluateLink();
180  if (_linkedDoor)
181  _linkedDoor->setLocked(locked);
182 }
183 
184 bool Door::click(Object *triggerer) {
185  _lastUsedBy = triggerer;
186 
187  // If the door is closed, try to open it
188  if (!isOpen())
189  return open(triggerer);
190 
191  // If the door is open and has a click script, call that
192  if (hasScript(kScriptClick))
193  return runScript(kScriptClick, this, triggerer);
194 
195  evaluateLink();
196  if (_link) {
197  float x, y, z;
198  _link->getPosition(x, y, z);
199 
200  _module->movePC(_link->getArea(), x, y, z);
201  return true;
202  }
203 
204  // If the door is open and has no script, close it
205  return close(triggerer);
206 }
207 
208 bool Door::open(Object *opener) {
209  // TODO: Door::open(): Open in direction of the opener
210 
211  if (isOpen() || (_state == kStateDestroyed))
212  return true;
213 
214  if (isLocked()) {
216  runScript(kScriptFailToOpen, this, opener);
217  return false;
218  }
219 
220  _lastOpenedBy = opener;
221 
223  runScript(kScriptOpen, this, opener);
224 
226 
227  // Also open the linked door
228  evaluateLink();
229  if (_linkedDoor)
230  _linkedDoor->open(opener);
231 
232  return true;
233 }
234 
235 bool Door::close(Object *closer) {
236  if (!isOpen() || (_state == kStateDestroyed))
237  return true;
238 
239  if (_invisible)
240  return false;
241 
242  _lastClosedBy = closer;
243 
245  runScript(kScriptClosed, this, closer);
246 
248 
249  // Also close the linked door
250  evaluateLink();
251  if (_linkedDoor)
252  _linkedDoor->close(closer);
253 
254  return true;
255 }
256 
257 bool Door::isOpen() const {
258  return (_state == kStateOpened1) || (_state == kStateOpened2);
259 }
260 
262  if (_evaluatedLink)
263  return;
264 
265  if ((_linkedToFlag != 0) && !_linkedTo.empty()) {
267 
269  _link = _linkedDoor = dynamic_cast<Door *>(object);
270  else if (_linkedToFlag == kLinkedToWaypoint)
271  _link = _linkedWaypoint = dynamic_cast<Waypoint *>(object);
272  }
273 
274  _evaluatedLink = true;
275 }
276 
278  static const Common::UString kEmptySound;
279 
280  switch (animation) {
281  case kAnimationDoorClose:
282  return _soundClosed;
283 
284  case kAnimationDoorOpen1:
285  return _soundOpened;
286 
287  case kAnimationDoorOpen2:
288  return _soundOpened;
289 
291  return _soundDestroyed;
292 
293  default:
294  break;
295  }
296 
297  return kEmptySound;
298 }
299 
300 void Door::playAnimation(const Common::UString &animation, bool restart, float length, float speed) {
301  Situated::playAnimation(animation, restart, length, speed);
302 }
303 
304 void Door::playAnimation(Animation animation, bool restart, float length, float speed) {
305  if (!isAnimationLooping(animation))
306  length = 0.0f;
307 
308  const bool alternate = _state == kStateOpened2;
309 
310  const Common::UString anim = getDoorAnimationName(animation, alternate);
311  const Common::UString &sound = getAnimationSound(animation);
312 
313  playSound(sound);
314  if (_model)
315  _model->playAnimation(anim, restart, length, speed);
316 }
317 
318 } // End of namespace NWN
319 
320 } // End of namespace Engines
LinkedToFlag _linkedToFlag
Does this door link to anything?
Definition: door.h:113
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
Door(Module &module, const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:45
virtual void setLocked(bool locked)
Lock/Unlock the situated object.
Definition: situated.cpp:125
This door links to a waypoint.
Definition: door.h:102
Common::UString getDoorAnimationName(Animation animation, bool alternate)
Return the name (as found in the models) of a specific door animation.
Definition: types.cpp:151
bool hasScript(Script script) const
Definition: container.cpp:114
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
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 * _link
The object this door links to.
Definition: door.h:117
Opened in one direction (normally clock-wise).
Definition: door.h:45
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:136
void movePC(const Common::UString &area)
Move the player character to this area.
Definition: module.cpp:689
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
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:92
void hide()
Hide the door&#39;s model.
Definition: door.cpp:147
const Common::UString & getAnimationSound(Animation animation)
Definition: door.cpp:277
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
void setModelState()
Sync the model&#39;s state with the door&#39;s state.
Definition: door.cpp:113
uint32 _genericType
Index into the generic door types.
Definition: door.h:109
The context needed to run a Neverwinter Nights module.
Waypoint * _linkedWaypoint
The waypoint this door links to.
Definition: door.h:119
Basic exceptions to throw.
This door links to another door.
Definition: door.h:101
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:156
void highlight(bool enabled)
(Un)Highlight the door.
Definition: door.cpp:168
Utility templates and functions.
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:82
State _state
The current state of the door.
Definition: door.h:111
Handling BioWare&#39;s 2DAs (two-dimensional array).
Opened in the other direction (normally counter-clock-wise).
Definition: door.h:46
void evaluateLink()
Evaluate our link.
Definition: door.cpp:261
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
Common::UString _soundDestroyed
The sound the object makes when destroyed.
Definition: situated.h:84
void setLocked(bool locked)
Lock/Unlock the door.
Definition: door.cpp:173
bool open(Object *opener)
The opener object opens this door.
Definition: door.cpp:208
Module * _module
The module the door is in.
Definition: door.h:105
A 3D model of an object.
bool isOpen() const
Is the door open?
Definition: door.cpp:257
bool close(Object *closer)
The closer object closes this door.
Definition: door.cpp:235
void enter()
The cursor entered the door.
Definition: door.cpp:153
void show()
Show the door&#39;s model.
Definition: door.cpp:141
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:248
Door * _linkedDoor
The door this door links to.
Definition: door.h:118
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:89
bool _invisible
Is the door invisible?
Definition: door.h:107
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
void leave()
The cursor left the door.
Definition: door.cpp:163
#define CursorMan
Shortcut for accessing the cursor manager.
Definition: cursorman.h:129
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
The Aurora cursor manager.
Common::UString _portrait
The object&#39;s portrait.
Definition: object.h:168
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:88
bool _evaluatedLink
Have we already tried to evaluate our link?
Definition: door.h:116
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
uint32_t uint32
Definition: types.h:204
static const uint32 kFieldIDInvalid
Definition: types.h:443
void playAnimation(const Common::UString &animation="", bool restart=true, float length=0.0f, float speed=1.0f)
Play a door animation.
Definition: door.cpp:300
Common::UString _linkedTo
The object tag this door links to.
Definition: door.h:114
void loadAppearance()
Load appearance-specific properties.
Definition: door.cpp:83
Door template (user), GFF.
Definition: types.h:107
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Generic Aurora engines utility functions.
bool click(Object *triggerer=0)
The door was clicked.
Definition: door.cpp:184
A door in a Neverwinter Nights area.
State
The state of a door.
Definition: door.h:43
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:139
Object * getFirstObjectByTag(const Common::UString &tag) const
Return the first object with this tag.
void load(const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:56
size_t headerToColumn(const Common::UString &header) const
Translate a column header to a column index.
Definition: 2dafile.cpp:412
A waypoint in a Neverwinter Nights area.
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
void loadObject(const Aurora::GFF3Struct &gff)
Load door-specific properties.
Definition: door.cpp:68
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:75