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 "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/maths.h"
31 
32 #include "src/aurora/gff3file.h"
33 #include "src/aurora/2dafile.h"
34 #include "src/aurora/2dareg.h"
35 
38 
40 
42 
43 namespace Engines {
44 
45 namespace KotOR {
46 
48  _state(kStateDefault), _hasInventory(false) {
49 
50  load(placeable);
51 }
52 
54 }
55 
56 void Placeable::load(const Aurora::GFF3Struct &placeable) {
57  Common::UString temp = placeable.getString("TemplateResRef");
58 
60  if (!temp.empty())
61  utp.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTP, MKTAG('U', 'T', 'P', ' ')));
62 
63  Situated::load(placeable, utp ? &utp->getTopLevel() : 0);
64 
65  if (!utp)
66  warning("Placeable \"%s\" has no blueprint", _tag.c_str());
67 
68  if (!_modelName.empty()) {
69  glm::mat4 transform;
70  transform = glm::translate(transform, glm::make_vec3(_position));
71  transform = glm::rotate(transform, Common::deg2rad(_orientation[3]), glm::make_vec3(_orientation));
72 
74  }
75 
76  readScripts(utp->getTopLevel());
77 }
78 
80  leave();
81 
83 }
84 
86  // State
87 
88  _state = (State) gff.getUint("AnimationState", (uint) _state);
89 
90  // Inventory
91 
92  _hasInventory = gff.getBool("HasInventory", _hasInventory);
93 
94  if (_hasInventory && gff.hasField("ItemList")) {
95  Aurora::GFF3List classList = gff.getList("ItemList");
96  for (Aurora::GFF3List::const_iterator iter = classList.begin(); iter != classList.end(); ++iter) {
97  const Aurora::GFF3Struct &item = **iter;
98  _inventory.addItem(item.getString("InventoryRes"));
99  }
100  }
101 
102  // Hit Points
103 
104  _currentHitPoints = gff.getSint("CurrentHP");
105  _maxHitPoints = gff.getSint("HP");
106 
107  _minOneHitPoint = gff.getBool("Min1HP");
108 }
109 
112  return;
113 
114  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("placeables");
115 
116  _modelName = twoda.getRow(_appearanceID).getString("ModelName");
117  _soundAppType = twoda.getRow(_appearanceID).getInt("SoundAppType");
118 }
119 
121  CursorMan.setGroup("use");
122  highlight(true);
123 }
124 
126  CursorMan.set();
127  highlight(false);
128 }
129 
130 void Placeable::highlight(bool enabled) {
131  if (_model)
132  _model->drawBound(enabled);
133 }
134 
135 bool Placeable::isOpen() const {
136  return (_state == kStateOpen) || (_state == kStateActivated);
137 }
138 
140  return isOpen();
141 }
142 
143 bool Placeable::click(Object *triggerer) {
144  CursorMan.set();
145 
146  // If the placeable is locked, just play the appropriate sound and bail
147  if (isLocked()) {
149  return false;
150  }
151 
152  // If the object was destroyed, nothing more can be done with it
153  if (_state == kStateDestroyed)
154  return true;
155 
156  _lastUsedBy = triggerer;
157 
158  // Objects with an inventory toggle between open and closed
159  if (_hasInventory) {
160  if (isOpen())
161  return close(triggerer);
162 
163  return open(triggerer);
164  }
165 
166  // Objects without an inventory toggle between activated and deactivated
167  if (isActivated())
168  return deactivate(triggerer);
169 
170  return activate(triggerer);
171 }
172 
173 bool Placeable::open(Object *opener) {
174  if (!_hasInventory)
175  return false;
176 
177  if (isOpen())
178  return true;
179 
180  if (isLocked()) {
182  return false;
183  }
184 
185  _lastOpenedBy = opener;
186 
188  runScript(kScriptOpen, this, opener);
189 
190  _state = kStateOpen;
191 
192  return true;
193 }
194 
195 bool Placeable::close(Object *closer) {
196  if (!_hasInventory)
197  return false;
198 
199  if (!isOpen())
200  return true;
201 
202  _lastClosedBy = closer;
203 
205  runScript(kScriptClosed, this, closer);
206 
208 
209  return true;
210 }
211 
213  if (_hasInventory)
214  return false;
215 
216  if (isActivated())
217  return true;
218 
219  if (isLocked()) {
221  return false;
222  }
223 
225  runScript(kScriptUsed, this, user);
226 
228 
229  return true;
230 }
231 
233  if (_hasInventory)
234  return false;
235 
236  if (!isActivated())
237  return true;
238 
239  if (isLocked()) {
241  return false;
242  }
243 
245  runScript(kScriptUsed, this, user);
246 
248 
249  return true;
250 }
251 
253  return _hasInventory;
254 }
255 
257  return _inventory;
258 }
259 
260 } // End of namespace KotOR
261 
262 } // End of namespace Engines
int64 getSint(const Common::UString &field, int64 def=0) const
Definition: gff3file.cpp:473
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
Placeable walk mesh.
Definition: types.h:119
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
void load(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:56
#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 isOpen() const
Is the placeable open?
Definition: placeable.cpp:135
void highlight(bool enabled)
(Un)Highlight the placeable.
Definition: placeable.cpp:130
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
bool isActivated() const
Is the placeable activated?
Definition: placeable.cpp:139
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 reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
bool _hasInventory
Does this placeable have an inventory?
Definition: placeable.h:100
void loadObject(const Aurora::GFF3Struct &gff)
Load placeable-specific properties.
Definition: placeable.cpp:85
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
Mathematical helpers.
void enter()
The cursor entered the placeable.
Definition: placeable.cpp:120
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
bool deactivate(Object *closer)
The user object deactivates this placeable.
Definition: placeable.cpp:232
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
Object * _lastClosedBy
The object that last closed this situated object.
Definition: situated.h:105
A simple scoped smart pointer template.
void loadAppearance()
Load appearance-specific properties.
Definition: placeable.cpp:110
Common::UString _modelName
The model&#39;s resource name.
Definition: situated.h:89
bool open(Object *opener)
The opener object opens this placeable.
Definition: placeable.cpp:173
bool close(Object *closer)
The closer object closes this placeable.
Definition: placeable.cpp:195
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Placeable(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:47
Utility templates and functions.
float _position[3]
The object&#39;s position.
Definition: object.h:156
State _state
The current state of the placeable.
Definition: placeable.h:98
Placeable template (user), GFF.
Definition: types.h:109
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
float _orientation[4]
The object&#39;s orientation.
Definition: object.h:157
A 3D model of an object.
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
int _currentHitPoints
The current hitpoints of the object.
Definition: object.h:150
The global 2DA registry.
Object * _lastOpenedBy
The object that last opened this situated object.
Definition: situated.h:104
void warning(const char *s,...)
Definition: util.cpp:33
#define CursorMan
Shortcut for accessing the cursor manager.
Definition: cursorman.h:129
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
std::vector< const GFF3Struct * > GFF3List
Definition: types.h:449
bool _minOneHitPoint
If the object should have at least one hitpoint.
Definition: object.h:152
Inventory & getInventory()
Definition: placeable.cpp:256
void leave()
The cursor left the placeable.
Definition: placeable.cpp:125
A placeable in a Star Wars: Knights of the Old Republic area.
The Aurora cursor manager.
void playSound(const Common::UString &sound, bool pitchVariance=false)
Play an object sound.
Definition: object.cpp:200
Common::UString _soundUsed
The sound the object makes when used.
Definition: situated.h:99
void addItem(const Common::UString &tag, int count=1)
Definition: inventory.cpp:31
const GFF3List & getList(const Common::UString &field) const
Definition: gff3file.cpp:741
void hide()
Hide the situated object&#39;s model.
Definition: situated.cpp:58
State
The state of a placeable.
Definition: placeable.h:41
int _maxHitPoints
The maximum hitpoints of the object.
Definition: object.h:151
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A struct within a GFF3.
Definition: gff3file.h:164
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Generic Aurora engines utility functions.
bool click(Object *triggerer=0)
The placeable was clicked.
Definition: placeable.cpp:143
void readScripts(const Aurora::GFF3Struct &gff, bool clear=true)
Definition: container.cpp:134
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
Inventory _inventory
The current items of this placeable if it has an inventory.
Definition: placeable.h:101
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
void load(const Common::UString &resRef, ::Aurora::FileType type=::Aurora::kFileTypeWOK, const glm::mat4 &transform=glm::mat4())
Definition: walkmesh.cpp:35
bool activate(Object *opener)
The user object activates this placeable.
Definition: placeable.cpp:212
unsigned int uint
Definition: types.h:211
bool isLocked() const
Is the situated object locked?
Definition: situated.cpp:97
void hide()
Hide the placeable&#39;s model.
Definition: placeable.cpp:79