xoreos  0.0.5
placeable.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 
35 
36 namespace Engines {
37 
38 namespace Witcher {
39 
41  _state(kStateDefault), _hasInventory(false) {
42 
43  load(placeable);
44 }
45 
47 }
48 
49 void Placeable::load(const Aurora::GFF3Struct &placeable) {
50  Common::UString temp = placeable.getString("TemplateResRef");
51 
53  if (!temp.empty())
54  utp.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTP, MKTAG('U', 'T', 'P', ' ')));
55 
56  Situated::load(placeable, utp ? &utp->getTopLevel() : 0);
57 }
58 
60  if (!_model)
61  return;
62 
63  switch (_state) {
64  case kStateDefault:
65  _model->setState("default");
66  break;
67 
68  case kStateOpen:
69  _model->setState("open");
70  break;
71 
72  case kStateClosed:
73  _model->setState("close");
74  break;
75 
76  case kStateDestroyed:
77  _model->setState("dead");
78  break;
79 
80  case kStateActivated:
81  _model->setState("on");
82  break;
83 
84  case kStateDeactivated:
85  _model->setState("off");
86  break;
87 
88  default:
89  _model->setState("");
90  break;
91  }
92 
93 }
94 
96  setModelState();
97 
99 }
100 
102  leave();
103 
104  Situated::hide();
105 }
106 
108  // State
109 
110  _state = (State) gff.getUint("AnimationState", (uint) _state);
111 
112  _hasInventory = gff.getBool("HasInventory", _hasInventory);
113 }
114 
116  highlight(true);
117 }
118 
120  highlight(false);
121 }
122 
123 void Placeable::highlight(bool enabled) {
124  if (_model)
125  _model->drawBound(enabled);
126 }
127 
128 bool Placeable::isOpen() const {
129  return _state == kStateOpen;
130 }
131 
133  return isOpen();
134 }
135 
136 bool Placeable::click(Object *triggerer) {
137  // If the placeable is locked, just play the appropriate sound and bail
138  if (isLocked()) {
140  return false;
141  }
142 
143  // If the object was destroyed, nothing more can be done with it
144  if (_state == kStateDestroyed)
145  return true;
146 
147  _lastUsedBy = triggerer;
148 
149  // Objects with an inventory toggle between open and closed
150  if (_hasInventory) {
151  if (isOpen())
152  return close(triggerer);
153 
154  return open(triggerer);
155  }
156 
157  // Objects without an inventory toggle between activated and deactivated
158  if (isActivated())
159  return deactivate(triggerer);
160 
161  return activate(triggerer);
162 }
163 
164 bool Placeable::open(Object *opener) {
165  if (!_hasInventory)
166  return false;
167 
168  if (isOpen())
169  return true;
170 
171  if (isLocked()) {
173  return false;
174  }
175 
176  _lastOpenedBy = opener;
177 
179  runScript(kScriptOpen, this, opener);
180 
181  _state = kStateOpen;
182 
183  return true;
184 }
185 
186 bool Placeable::close(Object *closer) {
187  if (!_hasInventory)
188  return false;
189 
190  if (!isOpen())
191  return true;
192 
193  _lastClosedBy = closer;
194 
196  runScript(kScriptClosed, this, closer);
197 
199 
200  return true;
201 }
202 
204  if (_hasInventory)
205  return false;
206 
207  if (isActivated())
208  return true;
209 
210  if (isLocked()) {
212  return false;
213  }
214 
216  runScript(kScriptUsed, this, user);
217 
219 
220  return true;
221 }
222 
224  if (_hasInventory)
225  return false;
226 
227  if (!isActivated())
228  return true;
229 
230  if (isLocked()) {
232  return false;
233  }
234 
236  runScript(kScriptUsed, this, user);
237 
239 
240  return true;
241 }
242 
243 } // End of namespace Witcher
244 
245 } // End of namespace Engines
bool isActivated() const
Is the placeable activated?
Definition: placeable.cpp:132
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
Common::UString _soundUsed
The sound the object makes when used.
Definition: situated.h:85
void hide()
Hide the placeable&#39;s model.
Definition: placeable.cpp:101
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
bool deactivate(Object *closer)
The user object deactivates this placeable.
Definition: placeable.cpp:223
bool _hasInventory
Does this placeable have an inventory?
Definition: placeable.h:92
bool getBool(const Common::UString &field, bool def=false) const
Definition: gff3file.cpp:510
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
Common::ScopedPtr< Graphics::Aurora::Model > _model
The situated object&#39;s model.
Definition: situated.h:92
State _state
The current state of the placeable.
Definition: placeable.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
Placeable(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:40
bool close(Object *closer)
The closer object closes this placeable.
Definition: placeable.cpp:186
bool isOpen() const
Is the placeable open?
Definition: placeable.cpp:128
A simple scoped smart pointer template.
bool open(Object *opener)
The opener object opens this placeable.
Definition: placeable.cpp:164
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:145
Object * _lastUsedBy
The object that last used this situated object.
Definition: situated.h:90
Utility templates and functions.
Common::UString _soundClosed
The sound the object makes when closed.
Definition: situated.h:83
Placeable template (user), GFF.
Definition: types.h:109
void enter()
The cursor entered the placeable.
Definition: placeable.cpp:115
Common::UString _soundLocked
The sound the object makes when locked.
Definition: situated.h:86
void load(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:49
A 3D model of an object.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:118
void highlight(bool enabled)
(Un)Highlight the placeable.
Definition: placeable.cpp:123
void show()
Show the placeable&#39;s model.
Definition: placeable.cpp:95
State
The state of a placeable.
Definition: placeable.h:39
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
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Generic Aurora engines utility functions.
void loadObject(const Aurora::GFF3Struct &gff)
Load placeable-specific properties.
Definition: placeable.cpp:107
bool click(Object *triggerer=0)
The placeable was clicked.
Definition: placeable.cpp:136
void leave()
The cursor left the placeable.
Definition: placeable.cpp:119
bool activate(Object *opener)
The user object activates this placeable.
Definition: placeable.cpp:203
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:97
A placeable object in a The Witcher area.
unsigned int uint
Definition: types.h:211
void setModelState()
Sync the model&#39;s state with the placeable&#39;s state.
Definition: placeable.cpp:59