xoreos  0.0.5
item.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/error.h"
26 #include "src/common/maths.h"
27 #include "src/common/util.h"
28 
29 #include "src/aurora/gff3file.h"
30 #include "src/aurora/2dafile.h"
31 #include "src/aurora/2dareg.h"
32 
34 
37 
38 #include "src/engines/nwn/item.h"
39 
40 namespace Engines {
41 
42 namespace NWN {
43 
45  _baseItem(Aurora::kFieldIDInvalid) {
46 
47  for (size_t i = 0; i < kColorMAX; i++)
49  for (size_t i = 0; i < kArmorPartMAX; i++)
51 
52  load(item);
53 }
54 
56 }
57 
58 void Item::load(const Aurora::GFF3Struct &item) {
59  Common::UString temp = item.getString("EquippedRes");
60  if (temp.empty())
61  temp = item.getString("TemplateResRef");
62 
64  if (!temp.empty())
65  uti.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTI, MKTAG('U', 'T', 'I', ' '), true));
66 
67  load(item, uti ? &uti->getTopLevel() : 0);
68 }
69 
71  if (_model)
72  return;
73 
74  if (_modelName.empty()) {
75  warning("Item object \"%s\" (\"%s\") has no model", _name.c_str(), _tag.c_str());
76  return;
77  }
78 
80  if (!_model)
81  throw Common::Exception("Failed to load situated object model \"%s\"",
82  _modelName.c_str());
83 
84  // Positioning
85 
86  float x, y, z, angle;
87 
88  getPosition(x, y, z);
89  setPosition(x, y, z);
90 
91  getOrientation(x, y, z, angle);
92  setOrientation(x, y, z, angle);
93 
94  // Clickable
95 
96  _model->setTag(_tag);
97  _model->setClickable(isClickable());
98 
99  _ids.push_back(_model->getID());
100 }
101 
103  hide();
104 
105  _model.reset();
106 }
107 
108 void Item::show() {
109  if (_model)
110  _model->show();
111 }
112 
113 void Item::hide() {
114  if (_model)
115  _model->hide();
116 }
117 
118 void Item::load(const Aurora::GFF3Struct &instance, const Aurora::GFF3Struct *blueprint) {
119  // General properties
120 
121  if (blueprint)
122  loadProperties(*blueprint); // Blueprint
123  loadProperties(instance); // Instance
124 
125  /* TODO:
126  * - BaseItem
127  * - What exactly is this item (weapon, armor, ...)
128  * - Index into baseitems.2da
129  * - StackSize
130  * - The number of items that stack in the inventory
131  */
132 }
133 
135  static const char * const kColorNames[kColorMAX] = {
136  "Metal1Color" , "Metal2Color",
137  "Leather1Color", "Leather2Color",
138  "Cloth1Color" , "Cloth2Color"
139  };
140 
141  // Tag
142  _tag = gff.getString("Tag", _tag);
143 
144  // Name
145  _name = gff.getString("LocalizedName", _name);
146 
147  // Description
148  _description = gff.getString("Description", _description);
149 
150  // This is an index into basitem.2da which contains inventory slot info
151  _baseItem = gff.getUint("BaseItem", _baseItem);
152 
153  // TODO: Are these armor only?
154  for (size_t i = 0; i < kColorMAX; i++)
155  _colors[i] = gff.getUint(kColorNames[i], _colors[i]);
156 
157  // Armor parts
158  loadArmorParts(gff);
159 
160  // Portrait
161  loadPortrait(gff);
162 
163  // Scripts
164  readScripts(gff);
165 }
166 
168  uint32 portraitID = gff.getUint("PortraitId");
169  if (portraitID != 0) {
170  const Aurora::TwoDAFile &twoda = TwoDAReg.get2DA("portraits");
171 
172  Common::UString portrait = twoda.getRow(portraitID).getString("BaseResRef");
173  if (!portrait.empty())
174  _portrait = "po_" + portrait;
175  }
176 
177  _portrait = gff.getString("Portrait", _portrait);
178 }
179 
181  static const char * const kArmorPartNames[] = {
182  "Appearance_Head" , // Heads appear to be a special case
183  "ArmorPart_Neck" ,
184  "ArmorPart_Torso" ,
185  "ArmorPart_Pelvis",
186  "ArmorPart_Belt" ,
187  "ArmorPart_RFoot" , "ArmorPart_LFoot" ,
188  "ArmorPart_RShin" , "ArmorPart_LShin" ,
189  "ArmorPart_LThigh", "ArmorPart_RThigh",
190  "ArmorPart_RFArm" , "ArmorPart_LFArm" ,
191  "ArmorPart_RBicep", "ArmorPart_LBicep",
192  "ArmorPart_RShoul", "ArmorPart_LShoul",
193  "ArmorPart_RHand" , "ArmorPart_LHand"
194  };
195 
196  for (size_t i = 0; i < kArmorPartMAX; i++)
197  _armorParts[i] = gff.getUint(kArmorPartNames[i], _armorParts[i]);
198 }
199 
200 bool Item::isArmor() const {
201  // TODO: This should really be based on the baseitem.2da
202 
204 }
205 
207  assert((size_t)color < kColorMAX);
208 
209  return _colors[(size_t)color];
210 }
211 
212 uint32 Item::getArmorPart(size_t index) const {
213  assert(index < kArmorPartMAX);
214 
215  return _armorParts[index];
216 }
217 
218 } // End of namespace NWN
219 
220 } // End of namespace Engines
An inventory item in Neverwinter Nights.
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
virtual void getOrientation(float &x, float &y, float &z, float &angle) const
Return the object&#39;s orientation.
Definition: object.cpp:162
uint32 getArmorPart(size_t index) const
Definition: item.cpp:212
Item(const Aurora::GFF3Struct &item)
Definition: item.cpp:44
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:123
A class holding an UTF-8 string.
Definition: ustring.h:48
Common::UString _modelName
The model&#39;s resource name.
Definition: item.h:97
bool isClickable() const
Can the player click the object?
Definition: object.cpp:120
virtual void setOrientation(float x, float y, float z, float angle)
Set the object&#39;s orientation.
Definition: object.cpp:176
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
Common::UString _name
The object&#39;s display name.
Definition: object.h:165
Mathematical helpers.
bool isArmor() const
Definition: item.cpp:200
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
void loadProperties(const Aurora::GFF3Struct &gff)
Definition: item.cpp:134
uint32 getColor(Color color) const
Definition: item.cpp:206
Basic exceptions to throw.
void loadArmorParts(const Aurora::GFF3Struct &gff)
Definition: item.cpp:180
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
std::list< uint32 > _ids
The object&#39;s model IDs.
Definition: object.h:178
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:156
Utility templates and functions.
Handling BioWare&#39;s 2DAs (two-dimensional array).
void hide()
Hide the item&#39;s model.
Definition: item.cpp:113
A 3D model of an object.
virtual void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: object.cpp:170
Common::UString _description
The object&#39;s description.
Definition: object.h:166
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
void show()
Show the item&#39;s model.
Definition: item.cpp:108
StackException Exception
Definition: error.h:59
The global 2DA registry.
uint32 _armorParts[kArmorPartMAX]
The item&#39;s armor parts.
Definition: item.h:102
void warning(const char *s,...)
Definition: util.cpp:33
void unloadModel()
Unload the item&#39;s model.
Definition: item.cpp:102
Common::UString _portrait
The object&#39;s portrait.
Definition: object.h:168
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
A struct within a GFF3.
Definition: gff3file.h:164
uint32 _colors[kColorMAX]
The item&#39;s colors.
Definition: item.h:101
uint32_t uint32
Definition: types.h:204
Item template (user), GFF.
Definition: types.h:91
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
Common::ScopedPtr< Graphics::Aurora::Model > _model
The item&#39;s model.
Definition: item.h:104
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Generic Aurora engines utility functions.
uint32 _baseItem
The index within the baseitem 2DA.
Definition: item.h:99
void load(const Aurora::GFF3Struct &item)
Definition: item.cpp:58
Graphics::Aurora::Model * loadModelObject(const Common::UString &resref, const Common::UString &texture)
Definition: model.cpp:47
void loadModel()
Load the item&#39;s model.
Definition: item.cpp:70
void loadPortrait(const Aurora::GFF3Struct &gff)
Definition: item.cpp:167
Generic Aurora engines model functions.