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 
28 #include "src/aurora/gff3file.h"
29 
31 
33 
37 
38 namespace Engines {
39 
40 namespace Witcher {
41 
43  _module(&module), _state(kStateClosed) {
44 
45  load(door);
46 }
47 
49 }
50 
51 void Door::load(const Aurora::GFF3Struct &door) {
52  Common::UString temp = door.getString("TemplateResRef");
53 
55  if (!temp.empty())
56  utd.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTD, MKTAG('U', 'T', 'D', ' ')));
57 
58  Situated::load(door, utd ? &utd->getTopLevel() : 0);
59 }
60 
62  if (!_model)
63  return;
64 
65  switch (_state) {
66  case kStateClosed:
67  _model->setState("closed");
68  break;
69 
70  case kStateOpened1:
71  _model->setState("opened1");
72  break;
73 
74  case kStateOpened2:
75  _model->setState("opened2");
76  break;
77 
78  case kStateDestroyed:
79  _model->setState("dead");
80  break;
81 
82  default:
83  _model->setState("");
84  break;
85  }
86 
87 }
88 
89 void Door::show() {
90  setModelState();
91 
93 }
94 
95 void Door::hide() {
96  leave();
97 
99 }
100 
102  // State
103 
104  _state = (State) gff.getUint("AnimationState", (uint) _state);
105 
106  // Linked to
107 
108  _linkedTo = gff.getString("LinkedTo" , _linkedTo);
109  _linkedToModule = gff.getString("TargetModule", _linkedToModule);
110 }
111 
112 void Door::enter() {
113  highlight(true);
114 }
115 
116 void Door::leave() {
117  highlight(false);
118 }
119 
120 void Door::highlight(bool enabled) {
121  if (_model)
122  _model->drawBound(enabled);
123 }
124 
125 bool Door::click(Object *triggerer) {
126  _lastUsedBy = triggerer;
127 
128  // If the door is closed, try to open it
129  if (!isOpen())
130  return open(triggerer);
131 
132  // If the door is open and has a click or used script, call that
133  if (hasScript(kScriptClick))
134  return runScript(kScriptClick, this, triggerer);
135  if (hasScript(kScriptUsed))
136  return runScript(kScriptUsed , this, triggerer);
137 
138  if (!_linkedTo.empty())
140 
141  // If the door is open and has no script, close it
142  return close(triggerer);
143 }
144 
145 bool Door::open(Object *opener) {
146  // TODO: Door::open(): Open in direction of the opener
147 
148  if (isOpen() || (_state == kStateDestroyed))
149  return true;
150 
151  if (isLocked()) {
153  runScript(kScriptFailToOpen, this, opener);
154  return false;
155  }
156 
157  _lastOpenedBy = opener;
158 
160  runScript(kScriptOpen, this, opener);
161 
163 
164  return true;
165 }
166 
167 bool Door::close(Object *closer) {
168  if (!isOpen() || (_state == kStateDestroyed))
169  return true;
170 
171  _lastClosedBy = closer;
172 
174  runScript(kScriptClosed, this, closer);
175 
177 
178  return true;
179 }
180 
181 bool Door::isOpen() const {
182  return (_state == kStateOpened1) || (_state == kStateOpened2);
183 }
184 
185 } // End of namespace Witcher
186 
187 } // End of namespace Engines
Witcher situated object.
Definition: situated.h:41
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
bool hasScript(Script script) const
Definition: container.cpp:120
bool close(Object *closer)
The closer object closes this door.
Definition: door.cpp:167
State
The state of a door.
Definition: door.h:43
void load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint=0)
Load the situated object from an instance and its blueprint.
Definition: situated.cpp:138
A class holding an UTF-8 string.
Definition: ustring.h:48
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:207
An object within a Witcher area.
Definition: object.h:51
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
void highlight(bool enabled)
(Un)Highlight the door.
Definition: door.cpp:120
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:92
Module * _module
The module the door is in.
Definition: door.h:85
void leave()
The cursor left the door.
Definition: door.cpp:116
State _state
The current state of the door.
Definition: door.h:87
Common::UString _linkedToModule
The module the waypoint this door links to is in.
Definition: door.h:90
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
A door in a The Witcher area.
A simple scoped smart pointer template.
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:145
void load(const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:51
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:90
void hide()
Hide the door&#39;s model.
Definition: door.cpp:95
Utility templates and functions.
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:83
void setModelState()
Sync the model&#39;s state with the door&#39;s state.
Definition: door.cpp:61
void loadObject(const Aurora::GFF3Struct &gff)
Load door-specific properties.
Definition: door.cpp:101
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
A 3D model of an object.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
Common::UString _linkedTo
The waypoint tag this door links to.
Definition: door.h:89
A waypoint in a The Witcher area.
void show()
Show the door&#39;s model.
Definition: door.cpp:89
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:118
void enter()
The cursor entered the door.
Definition: door.cpp:112
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:88
void show()
Show the situated object&#39;s model.
Definition: situated.cpp:92
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:89
A struct within a GFF3.
Definition: gff3file.h:164
Common::UString _soundOpened
The sound the object makes when opened.
Definition: situated.h:82
The context needed to run a The Witcher module.
Opened in one direction (normally clock-wise).
Definition: door.h:45
Opened in the other direction (normally counter-clock-wise).
Definition: door.h:46
Door template (user), GFF.
Definition: types.h:107
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Door(Module &module, const Aurora::GFF3Struct &door)
Load from a door instance.
Definition: door.cpp:42
Generic Aurora engines utility functions.
bool isOpen() const
Is the door open?
Definition: door.cpp:181
bool open(Object *opener)
The opener object opens this door.
Definition: door.cpp:145
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:97
unsigned int uint
Definition: types.h:211
void movePC(const Common::UString &area)
Move the player character to this area.
Definition: module.cpp:418
bool click(Object *triggerer=0)
The door was clicked.
Definition: door.cpp:125