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/error.h"
26 #include "src/common/util.h"
27 
28 #include "src/aurora/gff3file.h"
29 #include "src/aurora/2dafile.h"
30 #include "src/aurora/2dareg.h"
31 #include "src/aurora/talkman.h"
32 
34 
36 
38 #include "src/engines/jade/types.h"
39 
40 namespace Engines {
41 
42 namespace Jade {
43 
45  _appearanceType(Aurora::kFieldIDInvalid), _lastOpenedBy(0), _lastClosedBy(0) {
46 
47  load(placeable);
48 }
49 
51 }
52 
54  if (_model)
55  _model->show();
56 }
57 
59  leave();
60  if (_model)
61  _model->hide();
62 }
63 
64 void Placeable::load(const Aurora::GFF3Struct &placeable) {
65  _resRef = placeable.getString("ResRef");
66 
67  if (!_resRef.empty()) {
68  try {
70  pla(new Aurora::GFF3File(_resRef, Aurora::kFileTypePLA, MKTAG('P', 'L', 'A', ' ')));
71 
72  loadBlueprint(pla->getTopLevel());
74 
75  } catch (Common::Exception &e) {
76  e.add("Placeable \"%s\" has no blueprint", _tag.c_str());
77  throw;
78  }
79  }
80 
81  loadInstance(placeable);
83 }
84 
86  //Conversation
87  _conversation = gff.getString("Conversation", _conversation);
88 
89  // Appearance
90  _appearanceType = gff.getUint("AppearanceType", _appearanceType);
91 
92  // Sound Cue
93  _soundCue = gff.getString("SoundCue");
94 
95  // Scripts
96  readScripts(gff);
97 }
98 
101  warning("placeable \"%s\" without an appearance", _tag.c_str());
102  return;
103  }
104 
105  const Aurora::TwoDARow &placeable = TwoDAReg.get2DA("placeables").getRow(_appearanceType);
106 
107  // Modelname
108  _modelName = placeable.getString("modelname");
109 
110  uint32 strref = placeable.getInt("strref");
111  _name = TalkMan.getString(strref);
112 }
113 
115  // Tag
116  _tag = gff.getString("Tag", _tag);
117 
118  // Sound Cue
119  _soundCue = gff.getString("SoundCue");
120 
121  // Active
122  _active = gff.getBool("Active");
123 
124  // Modelname
125  Common::UString appearance = gff.getString("Appearance", _modelName);
126  if (!appearance.empty())
127  _modelName = appearance;
128 
129  loadPositional(gff);
130 
131  // Name
132  gff.getString("Name");
133 
134  // State
135  _state = gff.getSint("State");
136 
137  Common::UString fsmName = gff.getString("FSM");
138  _fsm.reset(new Aurora::GFF3File(fsmName, Aurora::kFileTypeFSM, MKTAG('F', 'S', 'M', ' ')));
139 }
140 
142 
143  // Model
144 
145  if (!_modelName.empty()) {
147 
148  if (!_model)
149  throw Common::Exception("Failed to load placeable model \"%s\"",
150  _modelName.c_str());
151 
152  // Clickable
153  _model->setTag(_tag);
154  _model->setClickable(isClickable());
155 
156  // Position and Orientation
157  float x, y, z, angle;
158  getPosition(x, y, z);
159  _model->setPosition(x, y, z);
160  getOrientation(x, y, z, angle);
161  _model->setOrientation(x, y, z, angle);
162 
163  // ID
164  _ids.push_back(_model->getID());
165  } else
166  warning("Placeable \"%s\" (\"%s\") has no model", _name.c_str(), _tag.c_str());
167 
168 }
169 
171  highlight(true);
172 }
173 
175  highlight(false);
176 }
177 
178 void Placeable::highlight(bool enabled) {
179  if (_model)
180  _model->drawBound(enabled);
181 }
182 
183 bool Placeable::open(Object *opener) {
184  int32 newState = nextState("open");
185 
186  if (newState == -1) {
187  runScript(kScriptOnFailToOpen, this, opener);
188  return false;
189  }
190 
191  _lastOpenedBy = opener;
192  _state = newState;
193 
195  runScript(kScriptOnOpen, this, opener);
196 
197  return true;
198 }
199 
200 bool Placeable::close(Object *closer) {
201  int32 newState = nextState("closed");
202 
203  if (newState != -1) {
204  _lastClosedBy = closer;
205 
206  _state = newState;
207 
208  runScript(kScriptOnClose, this, closer);
209 
210  return true;
211  }
212  return false;
213 }
214 
216  const Aurora::GFF3Struct &top = _fsm->getTopLevel();
217 
218  const Aurora::GFF3Struct &trans = top.getStruct("StateTrans");
219 
220  int32 inputCount = top.getSint("InputCount");
221 
222  const Aurora::GFF3Struct &inputs = top.getStruct("InputNames");
223  for (int i = 0;i < inputCount; i++) {
224  const Common::UString &inputKey = Common::UString::format("I%i", i);
225  const Common::UString &inputValue = inputs.getString(inputKey);
226  if (input == inputValue) {
227  const Common::UString &transKey = Common::UString::format("S%i_I%i", _state, i);
228  return trans.getSint(transKey);
229  }
230  }
231  return -1;
232 }
233 
234 } // End of namespace Jade
235 
236 } // 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).
#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
#define TalkMan
Shortcut for accessing the talk manager.
Definition: talkman.h:111
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 loadInstance(const Aurora::GFF3Struct &gff)
Load the placeable&#39;s instance properties.
Definition: placeable.cpp:114
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
void loadPositional(const Aurora::GFF3Struct &gff)
Load the object&#39;s positional gff struct which contains the position and orientation.
Definition: object.cpp:212
A placeable in a Jade Empire area.
uint64 getUint(const Common::UString &field, uint64 def=0) const
Definition: gff3file.cpp:436
void hide()
Hide the placeable&#39;s model.
Definition: placeable.cpp:58
virtual void playAnimation(const Common::UString &animation="", bool restart=true, int32 loopCount=0)
Play an object animation.
Definition: object.cpp:201
void readScripts(const Aurora::GFF3Struct &gff)
Definition: container.cpp:91
Common::UString _resRef
The placeable&#39;s description resref.
Definition: placeable.h:69
Exception that provides a stack of explanations.
Definition: error.h:36
void leave()
The cursor left the placeable.
Definition: placeable.cpp:174
bool runScript(Script script, const Aurora::NWScript::ObjectReference owner=Aurora::NWScript::ObjectReference(), const Aurora::NWScript::ObjectReference triggerer=Aurora::NWScript::ObjectReference())
Definition: container.cpp:110
bool isClickable() const
Can the player click the object?
Definition: object.cpp:106
bool close(Object *closer)
The closer object closes this placeable.
Definition: placeable.cpp:200
Basic exceptions to throw.
int32 _state
The placeable&#39;s current state.
Definition: placeable.h:78
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
void loadBlueprint(const Aurora::GFF3Struct &gff)
Load the placeable&#39;s blueprint properties.
Definition: placeable.cpp:85
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
Basic Jade Empire type definitions.
Common::ScopedPtr< Graphics::Aurora::Model > _model
The placeable&#39;s model.
Definition: placeable.h:76
Utility templates and functions.
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).
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 3D model of an object.
Common::ScopedPtr< Aurora::GFF3File > _fsm
The placeable&#39;s state file.
Definition: placeable.h:79
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
Placeable, GFF.
Definition: types.h:337
void show()
Show the placeable&#39;s model.
Definition: placeable.cpp:53
StackException Exception
Definition: error.h:59
The global 2DA registry.
bool open(Object *opener)
The opener object opens this placeable.
Definition: placeable.cpp:183
void loadAppearance()
Load appearance-specific properties.
Definition: placeable.cpp:141
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:142
void warning(const char *s,...)
Definition: util.cpp:33
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
Common::UString _soundCue
The placeable&#39;s sound cue.
Definition: placeable.h:68
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 placeable.
Definition: placeable.cpp:178
int32 nextState(const Common::UString &input)
Determines the result State according to the state model.
Definition: placeable.cpp:215
A struct within a GFF3.
Definition: gff3file.h:164
uint32 _appearanceType
The index within the placeable 2DA.
Definition: placeable.h:71
uint32_t uint32
Definition: types.h:204
const GFF3Struct & getStruct(const Common::UString &field) const
Definition: gff3file.cpp:728
Placeable(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:44
The global talk manager for Aurora strings.
Common::UString _tag
Definition: object.h:56
static const uint32 kFieldIDInvalid
Definition: types.h:443
Common::UString _modelName
The model&#39;s resource name.
Definition: placeable.h:67
void enter()
The cursor entered the placeable.
Definition: placeable.cpp:170
Common::UString getString(const Common::UString &field, const Common::UString &def="") const
Definition: gff3file.cpp:527
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 loadProperties()
Load properties from placeable.2da.
Definition: placeable.cpp:99
Object * _lastClosedBy
The object that last closed this placeable object.
Definition: placeable.h:74
Object * _lastOpenedBy
The object that last opened this placeable object.
Definition: placeable.h:73
Finite State Machine data.
Definition: types.h:193
void load(const Aurora::GFF3Struct &placeable)
Load from a placeable instance.
Definition: placeable.cpp:64
int32_t int32
Definition: types.h:203
Generic Aurora engines model functions.