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