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/scopedptr.h"
26 #include "src/common/util.h"
27 #include "src/common/error.h"
28 
29 #include "src/aurora/gff3file.h"
30 #include "src/aurora/2dafile.h"
31 #include "src/aurora/2dareg.h"
32 
34 
36 
37 #include "src/engines/nwn2/door.h"
40 
41 namespace Engines {
42 
43 namespace NWN2 {
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  Common::UString temp = door.getString("TemplateResRef");
58 
60  if (!temp.empty())
61  utd.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTD, MKTAG('U', 'T', 'D', ' ')));
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 
100  if (_modelName.empty())
101  _modelName = twoda.getRow(id).getString("ModelName");
102  if (_modelName.empty())
103  _modelName = twoda.getRow(id).getString("NWN2_ModelName");
104  if (_modelName.empty())
105  _modelName = twoda.getRow(id).getString("Model");
106 
107  _invisible = twoda.getRow(id).getInt("VisibleModel") == 0;
108  _soundAppType = twoda.getRow(id).getInt("SoundAppType");
109 }
110 
112  if (!_model)
113  return;
114 
115  switch (_state) {
116  case kStateClosed:
117  _model->setState("closed");
118  break;
119 
120  case kStateOpened1:
121  _model->setState("opened1");
122  break;
123 
124  case kStateOpened2:
125  _model->setState("opened2");
126  break;
127 
128  case kStateDestroyed:
129  _model->setState("dead");
130  break;
131 
132  default:
133  _model->setState("");
134  break;
135  }
136 
137 }
138 
139 void Door::show() {
140  setModelState();
141 
142  Situated::show();
143 }
144 
145 void Door::hide() {
146  leave();
147 
148  Situated::hide();
149 }
150 
151 void Door::enter() {
152  highlight(true);
153 }
154 
155 void Door::leave() {
156  highlight(false);
157 }
158 
159 void Door::highlight(bool enabled) {
160  if (_model)
161  _model->drawBound(enabled);
162 }
163 
164 void Door::setLocked(bool locked) {
165  if (isLocked() == locked)
166  return;
167 
168  Situated::setLocked(locked);
169  // Also lock/unlock the linked door
170  evaluateLink();
171  if (_linkedDoor)
172  _linkedDoor->setLocked(locked);
173 }
174 
175 bool Door::click(Object *triggerer) {
176  _lastUsedBy = triggerer;
177 
178  // If the door is closed, try to open it
179  if (!isOpen())
180  return open(triggerer);
181 
182  // If the door is open and has a click or used script, call that
183  if (hasScript(kScriptClick))
184  return runScript(kScriptClick, this, triggerer);
185  if (hasScript(kScriptUsed))
186  return runScript(kScriptUsed , this, triggerer);
187 
188  evaluateLink();
189  if (_link) {
190  float x, y, z;
191  _link->getPosition(x, y, z);
192 
193  _module->movePC(_link->getArea(), x, y, z);
194  return true;
195  }
196 
197  // If the door is open and has no script, close it
198  return close(triggerer);
199 }
200 
201 bool Door::open(Object *opener) {
202  // TODO: Door::open(): Open in direction of the opener
203 
204  if (isOpen() || (_state == kStateDestroyed))
205  return true;
206 
207  if (isLocked()) {
209  runScript(kScriptFailToOpen, this, opener);
210  return false;
211  }
212 
213  _lastOpenedBy = opener;
214 
216  runScript(kScriptOpen, this, opener);
217 
219 
220  // Also open the linked door
221  evaluateLink();
222  if (_linkedDoor)
223  _linkedDoor->open(opener);
224 
225  return true;
226 }
227 
228 bool Door::close(Object *closer) {
229  if (!isOpen() || (_state == kStateDestroyed))
230  return true;
231 
232  if (_invisible)
233  return false;
234 
235  _lastClosedBy = closer;
236 
238  runScript(kScriptClosed, this, closer);
239 
241 
242  // Also close the linked door
243  evaluateLink();
244  if (_linkedDoor)
245  _linkedDoor->close(closer);
246 
247  return true;
248 }
249 
250 bool Door::isOpen() const {
251  return (_state == kStateOpened1) || (_state == kStateOpened2);
252 }
253 
255  if (_evaluatedLink)
256  return;
257 
258  if ((_linkedToFlag != 0) && !_linkedTo.empty()) {
260 
262  _link = _linkedDoor = dynamic_cast<Door *>(object);
263  else if (_linkedToFlag == kLinkedToWaypoint)
264  _link = _linkedWaypoint = dynamic_cast<Waypoint *>(object);
265  }
266 
267  _evaluatedLink = true;
268 }
269 
270 } // End of namespace NWN2
271 
272 } // 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
bool _evaluatedLink
Have we already tried to evaluate our link?
Definition: door.h:107
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 setLocked(bool locked)
Lock/Unlock the door.
Definition: door.cpp:164
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:122
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
Opened in one direction (normally clock-wise).
Definition: door.h:45
Common::UString _linkedTo
The object tag this door links to.
Definition: door.h:105
void highlight(bool enabled)
(Un)Highlight the door.
Definition: door.cpp:159
This door links to another door.
Definition: door.h:92
Waypoint * _linkedWaypoint
The waypoint this door links to.
Definition: door.h:110
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
bool isOpen() const
Is the door open?
Definition: door.cpp:250
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
void leave()
The cursor left the door.
Definition: door.cpp:155
Opened in the other direction (normally counter-clock-wise).
Definition: door.h:46
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:127
An object within a NWN2 area.
Definition: object.h:58
State
The state of a door.
Definition: door.h:43
void enter()
The cursor entered the door.
Definition: door.cpp:151
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:90
void loadObject(const Aurora::GFF3Struct &gff)
Load door-specific properties.
Definition: door.cpp:68
A simple scoped smart pointer template.
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:83
bool click(Object *triggerer=0)
The door was clicked.
Definition: door.cpp:175
bool open(Object *opener)
The opener object opens this door.
Definition: door.cpp:201
The context needed to run a Neverwinter Nights 2 module.
Door * _linkedDoor
The door this door links to.
Definition: door.h:109
Basic exceptions to throw.
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.
LinkedToFlag _linkedToFlag
Does this door link to anything?
Definition: door.h:104
Handling BioWare&#39;s 2DAs (two-dimensional array).
This door links to a waypoint.
Definition: door.h:93
A 3D model of an object.
Door(Module &module, const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:45
Module * _module
The module the door is in.
Definition: door.h:96
void load(const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:56
State _state
The current state of the door.
Definition: door.h:102
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
Object * _link
The object this door links to.
Definition: door.h:108
The global 2DA registry.
void movePC(const Common::UString &area)
Move the player character to this area.
Definition: module.cpp:428
void evaluateLink()
Evaluate our link.
Definition: door.cpp:254
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
void setModelState()
Sync the model&#39;s state with the door&#39;s state.
Definition: door.cpp:111
uint32 _genericType
Index into the generic door types.
Definition: door.h:100
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 playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:284
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A struct within a GFF3.
Definition: gff3file.h:164
uint32_t uint32
Definition: types.h:204
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:91
void hide()
Hide the door&#39;s model.
Definition: door.cpp:145
A door in a Neverwinter Nights 2 area.
static const uint32 kFieldIDInvalid
Definition: types.h:443
void show()
Show the door&#39;s model.
Definition: door.cpp:139
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:142
Door template (user), GFF.
Definition: types.h:107
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
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
bool close(Object *closer)
The closer object closes this door.
Definition: door.cpp:228
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:106
virtual void setLocked(bool locked)
Lock/Unlock the situated object.
Definition: situated.cpp:131
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:82
void loadAppearance()
Load appearance-specific properties.
Definition: door.cpp:83
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
bool _invisible
Is the door invisible?
Definition: door.h:98
Object * getFirstObjectByTag(const Common::UString &tag) const
Return the first object with this tag.
bool hasScript(Script script) const
Definition: container.cpp:122
unsigned int uint
Definition: types.h:211
A waypoint in a Neverwinter Nights 2 area.