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/util.h"
26 #include "src/common/maths.h"
27 #include "src/common/uuid.h"
28 
29 #include "src/aurora/gff4file.h"
30 #include "src/aurora/gdafile.h"
31 #include "src/aurora/2dareg.h"
32 
34 
36 
39 
41 #include "src/engines/sonic/area.h"
42 
44 static const uint32 kTypeAppearances[] = {
45  84, 81, 82, 0xFFFFFFFF, 89, 0xFFFFFFFF, 92, 0xFFFFFFFF,
46  0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 101,
47  136, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
48  0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
49  127, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 130, 0xFFFFFFFF, 134,
50  135, 0xFFFFFFFF, 0xFFFFFFFF,
51 };
52 
53 namespace Engines {
54 
55 namespace Sonic {
56 
58  _placeableID(0xFFFFFFFF), _typeID(0xFFFFFFFF), _appearanceID(0xFFFFFFFF), _scale(1.0f) {
60  ObjectMan.registerObject(this);
61 
62  load(placeable);
63 }
64 
66  ObjectMan.unregisterObject(this);
67 
68  hide();
69 
70  try {
72  } catch (...) {
73  }
74 }
75 
77  if (_model)
78  _model->show();
79 }
80 
82  if (_model)
83  _model->hide();
84 }
85 
87  warning("Placeable %u: \"%s\"", _id, _tag.c_str());
88 
89  highlight(true);
90 }
91 
93  highlight(false);
94 }
95 
96 void Placeable::highlight(bool enabled) {
97  if (_model)
98  _model->drawBound(enabled);
99 }
100 
101 void Placeable::setPosition(float x, float y, float z) {
102  Object::setPosition(x, y, z);
103  Object::getPosition(x, y, z);
104 
105  if (_model) {
106  Area::getWorldPosition(x, y, z, x, y, z);
107 
108  _model->setPosition(x, y, z);
109  }
110 }
111 
112 void Placeable::setOrientation(float x, float y, float z, float angle) {
113  Object::setOrientation(x, y, z, angle);
114  Object::getOrientation(x, y, z, angle);
115 
116  if (_model)
117  _model->setOrientation(x, y, z, angle);
118 }
119 
120 void Placeable::load(const Aurora::GFF4Struct &placeable) {
121  _tag = placeable.getString(Aurora::kGFF4Tag);
122 
123  _placeableID = placeable.getUint(40023, 0xFFFFFFFF);
124  _typeID = placeable.getUint(40018, 0xFFFFFFFF);
125 
126  _appearanceID = 0xFFFFFFFF;
129 
130  if (_appearanceID != 0xFFFFFFFF) {
131  const Aurora::GDAFile &appearances = TwoDAReg.getGDA("appearances");
132 
133  if (appearances.hasRow(_appearanceID)) {
134  _modelName = appearances.getString(_appearanceID, 2122127238);
135  _scale = appearances.getFloat (_appearanceID, "Scale", 1.0f);
136  }
137  }
138 
139  if (!_modelName.empty()) {
140  /* TODO: This basically just protects against an exception for opening the
141  * same archive twice when loading objects using the same model.
142  * We should probably add some kind of reference counting somewhere...
143  */
144  try {
145  indexMandatoryArchive(_modelName + ".nsbtx", 100 + _id, &_modelTexture);
146  } catch (...) {
147  }
148 
150 
151  if (_model) {
152  _model->setScale(_scale, _scale, _scale);
153 
154  _model->setClickable(true);
155  _modelID = _model->getID();
156  }
157  }
158 
159  if (placeable.hasField(Aurora::kGFF4Position)) {
160  double x = 0.0, y = 0.0, z = 0.0;
161 
162  placeable.getVector3(Aurora::kGFF4Position, x, y, z);
163  setPosition(x, y, z);
164  }
165 
166  double orientation = placeable.getDouble(Aurora::kGFF4Orientation);
167  setOrientation(0.0f, 1.0f, 0.0f, Common::rad2deg(orientation));
168 }
169 
170 } // End of namespace Sonic
171 
172 } // End of namespace Engines
virtual void getOrientation(float &x, float &y, float &z, float &angle) const
Return the object&#39;s orientation.
Definition: object.cpp:66
#define ObjectMan
Definition: objectman.h:56
Common::ChangeID _modelTexture
The resource change created by indexing the model&#39;s texture.
Definition: placeable.h:69
The context holding a Sonic Chronicles: The Dark Brotherhood area.
bool getVector3(uint32 field, double &v1, double &v2, double &v3) const
Definition: gff4file.cpp:999
Generic Aurora engines resource utility functions.
Handling BioWare&#39;s GDAs (2DAs, two-dimensional array, within V4.0 GFFs).
Common::UString getString(uint32 field, Common::Encoding encoding, const Common::UString &def="") const
Return a field string, read from the given encoding.
Definition: gff4file.cpp:949
virtual void setPosition(float x, float y, float z)
Set the placeable&#39;s position within its area.
Definition: placeable.cpp:101
float getFloat(size_t row, uint32 columnHash, float def=0.0f) const
Definition: gdafile.cpp:202
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
virtual void setOrientation(float x, float y, float z, float angle)
Set the placeable&#39;s orientation.
Definition: placeable.cpp:112
Mathematical helpers.
uint32 _placeableID
The placeable&#39;s identifer from GFF.
Definition: placeable.h:73
Utility functions for generating unique IDs.
uint32 _appearanceID
The placeable&#39;s appearance.
Definition: placeable.h:75
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
static void getWorldPosition(float x, float y, float z, float &worldX, float &worldY, float &worldZ)
Return a position that would position on object at these coordinates within the area.
Definition: area.cpp:105
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:60
An object within a Sonic area.
Definition: object.h:40
void deindexResources(Common::ChangeID &changeID)
Remove previously added resources from the ResourceManager.
Definition: resources.cpp:164
Handling version V4.0/V4.1 of BioWare&#39;s GFFs (generic file format).
NWScript object manager.
void indexMandatoryArchive(const Common::UString &file, uint32 priority, const std::vector< byte > &password, Common::ChangeID *changeID)
Definition: resources.cpp:36
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Common::UString _modelName
The name of the model representing this placeable.
Definition: placeable.h:78
uint32 _typeID
The placeable&#39;s type.
Definition: placeable.h:74
double getDouble(uint32 field, double def=0.0) const
Definition: gff4file.cpp:925
Utility templates and functions.
A 3D model of an object.
static const uint32 kTypeAppearances[]
Map a placeable type ID onto an appearance ID.
Definition: placeable.cpp:44
bool hasField(uint32 field) const
Does this specific field exist?
Definition: gff4file.cpp:573
void hide()
Hide the placeable&#39;s model.
Definition: placeable.cpp:81
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
void enter()
The cursor entered the placeable.
Definition: placeable.cpp:86
The global 2DA registry.
void warning(const char *s,...)
Definition: util.cpp:33
Common::UString getString(size_t row, uint32 columnHash, const Common::UString &def="") const
Definition: gdafile.cpp:165
virtual void setOrientation(float x, float y, float z, float angle)
Set the object&#39;s orientation.
Definition: object.cpp:80
uint32_t uint32
Definition: types.h:204
Common::UString _tag
Definition: object.h:56
bool hasRow(size_t row) const
Does this row exist in the GDA?
Definition: gdafile.cpp:69
virtual void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: object.cpp:74
static float rad2deg(float rad)
Definition: maths.h:93
A placeable object in a Sonic Chronicles: The Dark Brotherhood area.
void leave()
The cursor left the placeable.
Definition: placeable.cpp:92
void show()
Show the placeable&#39;s model.
Definition: placeable.cpp:76
Graphics::Aurora::Model * loadModelObject(const Common::UString &resref, const Common::UString &texture)
Definition: model.cpp:47
uint64 getUint(uint32 field, uint64 def=0) const
Definition: gff4file.cpp:897
Common::ScopedPtr< Graphics::Aurora::Model > _model
The placeable&#39;s model.
Definition: placeable.h:71
void highlight(bool enabled)
(Un)Highlight the placeable.
Definition: placeable.cpp:96
uint32 generateIDNumber()
Definition: uuid.cpp:46
uint32 _modelID
The ID of the object&#39;s model.
Definition: object.h:81
float _scale
The scale modifying this placeable&#39;s model.
Definition: placeable.h:81
Class to hold the GFF&#39;d two-dimensional array of a GDA file.
Definition: gdafile.h:62
void load(const Aurora::GFF4Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:120
Placeable(const Aurora::GFF4Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:57
Generic Aurora engines model functions.