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 "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/error.h"
31 #include "src/common/maths.h"
32 
33 #include "src/aurora/gff3file.h"
34 #include "src/aurora/2dafile.h"
35 #include "src/aurora/2dareg.h"
36 
39 
41 
42 #include "src/engines/kotor/door.h"
44 
45 namespace Engines {
46 
47 namespace KotOR {
48 
50  _module(&module), _genericType(Aurora::kFieldIDInvalid), _state(kStateClosed),
51  _linkedToFlag(kLinkedToNothing), _linkedToType(kObjectTypeAll) {
52 
53  load(door);
54 }
55 
57 }
58 
59 void Door::load(const Aurora::GFF3Struct &door) {
60  Common::UString temp = door.getString("TemplateResRef");
61 
63  if (!temp.empty())
64  utd.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTD, MKTAG('U', 'T', 'D', ' ')));
65 
66  Situated::load(door, utd ? &utd->getTopLevel() : 0);
67 
68  if (!utd)
69  warning("Door \"%s\" has no blueprint", _tag.c_str());
70 
71  if (!_modelName.empty()) {
72  glm::mat4 transform;
73  transform = glm::translate(transform, glm::make_vec3(_position));
74  transform = glm::rotate(transform, Common::deg2rad(_orientation[3]), glm::make_vec3(_orientation));
75 
76  _walkmesh.load(_modelName + "0", ::Aurora::kFileTypeDWK, transform);
77  }
78 }
79 
81  _genericType = gff.getUint("GenericType", _genericType);
82 
83  // State
84 
85  _state = (State) gff.getUint("AnimationState", (uint) _state);
86 
87  // Linked to
88 
89  _linkedToFlag = (LinkedToFlag) gff.getUint("LinkedToFlags", (uint) _linkedToFlag);
90  _linkedTo = gff.getString("LinkedTo", _linkedTo);
91  _linkedToModule = gff.getString("LinkedToModule", _linkedToModule);
92 
93  _transitionDestination = gff.getString("TransitionDestin", _transitionDestination);
94 
96 }
97 
99  if (_appearanceID == 0) {
101  throw Common::Exception("Door \"%s\" has no appearance ID and no generic type",
102  _tag.c_str());
103 
104  loadAppearance(TwoDAReg.get2DA("genericdoors"), _genericType);
105  } else
106  loadAppearance(TwoDAReg.get2DA("doortypes"), _appearanceID);
107 }
108 
111  return;
112 
113  uint32 column = twoda.headerToColumn("ModelName");
114  if (column == Aurora::kFieldIDInvalid)
115  column = twoda.headerToColumn("Model");
116 
117  _modelName = twoda.getRow(id).getString(column);
118  _soundAppType = twoda.getRow(_appearanceID).getInt("SoundAppType");
119 }
120 
121 void Door::hide() {
122  leave();
123 
124  Situated::hide();
125 }
126 
128 }
129 
130 void Door::enter() {
131  CursorMan.setGroup("door");
132  highlight(true);
133 }
134 
135 void Door::leave() {
136  CursorMan.set();
137  highlight(false);
138 }
139 
140 void Door::highlight(bool enabled) {
141  if (_model)
142  _model->drawBound(enabled);
143 }
144 
145 bool Door::isOpen() const {
146  return (_state == kStateOpened1) || (_state == kStateOpened2);
147 }
148 
149 bool Door::click(Object *triggerer) {
150  _lastUsedBy = triggerer;
151 
152  // If the door is closed, try to open it
153  if (!isOpen())
154  return open(triggerer);
155 
156  // If the door is open and has a click script, call that
157  if (hasScript(kScriptClick))
158  return runScript(kScriptClick, this, triggerer);
159 
160  if (!_linkedTo.empty()) {
162 
163  return true;
164  }
165 
166  // If the door is open and has no script, close it
167  return close(triggerer);
168 }
169 
170 bool Door::testCollision(const glm::vec3 &orig, const glm::vec3 &dest) const {
171  return !isOpen() && _walkmesh.testCollision(orig, dest);
172 }
173 
174 bool Door::open(Object *opener) {
175  // TODO: Door::open(): Open in direction of the opener
176 
177  if (isOpen() || (_state == kStateDestroyed))
178  return true;
179 
180  if (isLocked()) {
182  runScript(kScriptFailToOpen, this, opener);
183  return false;
184  }
185 
186  _lastOpenedBy = opener;
187 
188  if (_model)
189  _model->playAnimation("opening1");
190 
192  runScript(kScriptOpen, this, opener);
193 
195 
196  return true;
197 }
198 
199 bool Door::close(Object *closer) {
200  if (!isOpen() || (_state == kStateDestroyed))
201  return true;
202 
203  _lastClosedBy = closer;
204 
205  if (_model)
206  _model->playAnimation("closing1");
207 
209  runScript(kScriptClosed, this, closer);
210 
212 
213  return true;
214 }
215 
216 } // End of namespace KotOR
217 
218 } // 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
void loadObject(const Aurora::GFF3Struct &gff)
Load door-specific properties.
Definition: door.cpp:80
bool testCollision(const glm::vec3 &orig, const glm::vec3 &dest) const
Definition: walkmesh.cpp:71
bool isOpen() const
Is the door open?
Definition: door.cpp:145
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
bool close(Object *closer)
The closer object closes this door.
Definition: door.cpp:199
Opened in the other direction (normally counter-clock-wise).
Definition: door.h:44
Mathematical helpers.
State
The state of a door.
Definition: door.h:41
ObjectType
Object type, matches the bitfield in nwscript.nss.
Definition: types.h:33
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 door.
Definition: door.h:97
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:105
A simple scoped smart pointer template.
void load(const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:59
bool testCollision(const glm::vec3 &orig, const glm::vec3 &dest) const
Definition: door.cpp:170
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:89
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Utility templates and functions.
float _position[3]
The object&#39;s position.
Definition: object.h:156
Door walk mesh.
Definition: types.h:118
Handling BioWare&#39;s 2DAs (two-dimensional array).
void load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint=0)
Load the situated object from an instance and its blueprint.
Definition: situated.cpp:121
LinkedToFlag _linkedToFlag
Does this door link to anything?
Definition: door.h:99
A door in a Star Wars: Knights of the Old Republic area.
float _orientation[4]
The object&#39;s orientation.
Definition: object.h:157
A 3D model of an object.
uint32 _genericType
Index into the generic door types.
Definition: door.h:95
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:158
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:96
uint32 _appearanceID
The index within the situated appearance 2DA.
Definition: situated.h:91
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:106
StackException Exception
Definition: error.h:59
The global 2DA registry.
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:104
Common::UString _transitionDestination
A localized string describing where this door leads to.
Definition: door.h:105
Opened in one direction (normally clock-wise).
Definition: door.h:43
void warning(const char *s,...)
Definition: util.cpp:33
void hideSoft()
Hide the door&#39;s model if applicable.
Definition: door.cpp:127
#define CursorMan
Shortcut for accessing the cursor manager.
Definition: cursorman.h:129
Door(Module &module, const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:49
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
bool click(Object *triggerer=0)
The door was clicked.
Definition: door.cpp:149
The Aurora cursor manager.
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:200
void movePC(float x, float y, float z)
Move the player character to this position within the current area.
Definition: module.cpp:695
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:58
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A struct within a GFF3.
Definition: gff3file.h:164
void highlight(bool enabled)
(Un)Highlight the door.
Definition: door.cpp:140
uint32_t uint32
Definition: types.h:204
bool hasScript(Script script) const
Definition: container.cpp:125
Common::UString _tag
Definition: object.h:56
void enter()
The cursor entered the door.
Definition: door.cpp:130
void leave()
The cursor left the door.
Definition: door.cpp:135
static const uint32 kFieldIDInvalid
Definition: types.h:443
Module * _module
The module the door is in.
Definition: door.h:93
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.
Common::UString _linkedTo
The object tag this door links to.
Definition: door.h:101
Common::UString _linkedToModule
The module the object this door links to is in.
Definition: door.h:102
The context needed to run a Star Wars: Knights of the Old Republic module.
bool open(Object *opener)
The opener object opens this door.
Definition: door.cpp:174
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:108
static float deg2rad(float deg)
Definition: maths.h:97
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:100
uint32 _soundAppType
The index within the situated sounds 2DA.
Definition: situated.h:92
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:97
size_t headerToColumn(const Common::UString &header) const
Translate a column header to a column index.
Definition: 2dafile.cpp:412
void load(const Common::UString &resRef, ::Aurora::FileType type=::Aurora::kFileTypeWOK, const glm::mat4 &transform=glm::mat4())
Definition: walkmesh.cpp:35
void hide()
Hide the door&#39;s model.
Definition: door.cpp:121
void loadAppearance()
Load appearance-specific properties.
Definition: door.cpp:98
unsigned int uint
Definition: types.h:211
ObjectType _linkedToType
The type of the object this door links to.
Definition: door.h:100
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:97