xoreos  0.0.5
creature.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/error.h"
28 #include "src/common/ustring.h"
29 
30 #include "src/aurora/2dafile.h"
31 #include "src/aurora/2dareg.h"
32 #include "src/aurora/gff3file.h"
33 #include "src/aurora/locstring.h"
34 
37 
40 
42 
43 namespace Engines {
44 
45 namespace Jade {
46 
47 Creature::Creature(const Aurora::GFF3Struct &creature) : Object(kObjectTypeCreature), _isPC(false), _autoBalance(0),
48  _appearance(Aurora::kFieldIDInvalid), _headType(0) {
49 
50  load(creature);
51 }
52 
53 Creature::Creature() : Object(kObjectTypeCreature), _isPC(false), _autoBalance(0),
54  _appearance(Aurora::kFieldIDInvalid), _headType(0) {
55 }
56 
58 }
59 
61  if (_model)
62  _model->show();
63 }
64 
66  if (_model)
67  _model->hide();
68 }
69 
70 bool Creature::isPC() const {
71  return _isPC;
72 }
73 
75  return _autoBalance;
76 }
77 
78 void Creature::setPosition(float x, float y, float z) {
79  Object::setPosition(x, y, z);
80  Object::getPosition(x, y, z);
81 
82  if (_model)
83  _model->setPosition(x, y, z);
84 }
85 
86 void Creature::setOrientation(float x, float y, float z, float angle) {
87  Object::setOrientation(x, y, z, angle);
88  Object::getOrientation(x, y, z, angle);
89 
90  if (_model)
91  _model->setOrientation(x, y, z, angle);
92 }
93 
94 void Creature::load(const Aurora::GFF3Struct &creature) {
95  Common::UString temp = creature.getString("ResRef");
96 
97  if (!temp.empty()) {
98  try {
100  cre(new Aurora::GFF3File(temp, Aurora::kFileTypeCRE, MKTAG('C', 'R', 'E', ' ')));
101 
102  loadBlueprint(cre->getTopLevel());
103 
104  } catch (Common::Exception &e) {
105  e.add("Creature \"%s\" has no blueprint", temp.c_str());
106  throw;
107  }
108  }
109 
110  // Tag
111  _tag = creature.getString("Tag");
112 
113  // Appearance
114 
116  throw Common::Exception("Creature without an appearance");
117 
118  loadAppearance();
119 
120  loadInstance(creature);
121 }
122 
124  if (gff.hasField("Looks")) {
125  const Aurora::GFF3Struct &looks = gff.getStruct("Looks");
126 
127  // Appearance
128  _appearance = looks.getSint("Appearance", _appearance);
129 
130  // Head
131  if (looks.hasField("HeadType")) {
132  _headType = looks.getSint("HeadType");
133  }
134  }
135 
136  if (gff.hasField("Stats")) {
137  const Aurora::GFF3Struct &stats = gff.getStruct("Stats");
138 
139  // Conversation
140  _conversation = stats.getString("Conversation", _conversation);
141 
142  // AutoBalance
143  _autoBalance = stats.getBool("AutoBalance");
144  }
145 
146  // Scripts
147  readScripts(gff);
148 }
149 
151 
152  loadPositional(instance);
153 
154  // Active
155  _active = instance.getBool("Active");
156 }
157 
159  loadBody();
160  loadHead();
161 }
162 
164  const Aurora::TwoDARow &appearance = TwoDAReg.get2DA("appearance").getRow(_appearance);
165 
166  const Common::UString bodyModel = appearance.getString(Common::UString("MODELA"));
167 
168  _model.reset(loadModelObject(bodyModel));
169  if (!_model)
170  return;
171 
172  _ids.push_back(_model->getID());
173 
174  _model->setTag(_tag);
175  _model->setClickable(isClickable());
176 }
177 
179  if (!_model || !_headType)
180  return;
181 
182  const Aurora::TwoDARow &chest = TwoDAReg.get2DA("creaturehooks").getRow(3); // TODO row with label Chest
183 
184  const Aurora::TwoDARow &headtype = TwoDAReg.get2DA("heads").getRow(_headType);
185 
186  const Common::UString headModelName = headtype.getString("model");
187 
188  Graphics::Aurora::Model *headModel = loadModelObject(headModelName);
189  if (!headModel)
190  return;
191 
192  _model->attachModel(chest.getString("hook"), headModel);
193 }
194 
196  _name = "Fakoo McFakeston";
197  _tag = Common::UString::format("[PC: %s]", _name.c_str());
198 
199  _isPC = true;
200 }
201 
203  highlight(true);
204 }
205 
207  highlight(false);
208 }
209 
210 void Creature::highlight(bool enabled) {
211  _model->drawBound(enabled);
212 }
213 
214 } // End of namespace Jade
215 
216 } // End of namespace Engines
int64 getSint(const Common::UString &field, int64 def=0) const
Definition: gff3file.cpp:473
Handling version V3.2/V3.3 of BioWare&#39;s GFFs (generic file format).
void enter()
The cursor entered the creature.
Definition: creature.cpp:202
#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 _conversation
The object&#39;s default conversation.
Definition: object.h:152
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
bool getBool(const Common::UString &field, bool def=false) const
Definition: gff3file.cpp:510
void setPosition(float x, float y, float z)
Set the creature&#39;s position.
Definition: creature.cpp:78
A class holding an UTF-8 string.
Definition: ustring.h:48
uint32 _headType
The creature&#39;s type of head if separate from the model.
Definition: creature.h:85
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
void loadPositional(const Aurora::GFF3Struct &gff)
Load the object&#39;s positional gff struct which contains the position and orientation.
Definition: object.cpp:212
bool hasField(const Common::UString &field) const
Does this specific field exist?
Definition: gff3file.cpp:400
Mathematical helpers.
virtual void setPosition(float x, float y, float z)
Set the object&#39;s position within its area.
Definition: object.cpp:156
void show()
Show the creature&#39;s model.
Definition: creature.cpp:60
void hide()
Hide the creature&#39;s model.
Definition: creature.cpp:65
void loadInstance(const Aurora::GFF3Struct &gff)
Definition: creature.cpp:150
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:91
void createFakePC()
Create a fake player character creature for testing purposes.
Definition: creature.cpp:195
Exception that provides a stack of explanations.
Definition: error.h:36
bool isClickable() const
Can the player click the object?
Definition: object.cpp:106
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
void leave()
The cursor left the creature.
Definition: creature.cpp:206
Utility templates and functions.
int32 getAutoBalance() const
Definition: creature.cpp:74
virtual void getOrientation(float &x, float &y, float &z, float &angle) const
Return the object&#39;s orientation.
Definition: object.cpp:148
Handling BioWare&#39;s 2DAs (two-dimensional array).
Creature()
Create a dummy creature instance.
Definition: creature.cpp:53
A GFF (generic file format) V3.2/V3.3 file, found in all Aurora games except Sonic Chronicles: The Da...
Definition: gff3file.h:85
A node within a 3D model.
void load(const Aurora::GFF3Struct &creature)
Definition: creature.cpp:94
A 3D model of an object.
bool _active
Is the object currently active/available/visible?
Definition: object.h:156
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
StackException Exception
Definition: error.h:59
The global 2DA registry.
bool _isPC
Is the creature a PC?
Definition: creature.h:80
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:142
Handling BioWare&#39;s localized strings.
An object within a Jade area.
Definition: object.h:53
std::list< uint32 > _ids
The object&#39;s model IDs.
Definition: object.h:160
virtual void highlight(bool enabled)
(Un)Highlight the creature.
Definition: creature.cpp:210
Unicode string handling.
Common::ScopedPtr< Graphics::Aurora::Model > _model
The creature&#39;s model.
Definition: creature.h:87
Creature, GFF.
Definition: types.h:331
void setOrientation(float x, float y, float z, float angle)
Set the creature&#39;s orientation.
Definition: creature.cpp:86
A struct within a GFF3.
Definition: gff3file.h:164
const GFF3Struct & getStruct(const Common::UString &field) const
Definition: gff3file.cpp:728
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
uint32 _appearance
The creature&#39;s general appearance.
Definition: creature.h:84
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
Generic Aurora engines utility functions.
Common::UString _name
The object&#39;s display name.
Definition: object.h:149
Graphics::Aurora::Model * loadModelObject(const Common::UString &resref, const Common::UString &texture)
Definition: model.cpp:47
A row within a 2DA file.
Definition: 2dafile.h:61
void loadBlueprint(const Aurora::GFF3Struct &gff)
Definition: creature.cpp:123
virtual void setOrientation(float x, float y, float z, float angle)
Set the object&#39;s orientation.
Definition: object.cpp:162
bool isPC() const
Is the creature a player character?
Definition: creature.cpp:70
int32_t int32
Definition: types.h:203
A creature in a Jade Empire area.
Generic Aurora engines model functions.