xoreos  0.0.5
functions_object.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/scopedptr.h"
26 #include "src/common/util.h"
27 
29 
30 #include "src/engines/jade/types.h"
31 #include "src/engines/jade/game.h"
35 
37 
38 namespace Engines {
39 
40 namespace Jade {
41 
47 
49  // TODO: This should return the *last* entered object, i.e. it should remember past triggerers.
50  ctx.getReturn() = ctx.getTriggerer();
51 }
52 
54  // TODO: This should return the *last* exited object, i.e. it should remember past triggerers.
55  ctx.getReturn() = ctx.getTriggerer();
56 }
57 
59  ctx.getReturn() = getParamObject(ctx, 0) != 0;
60 }
61 
64 }
65 
67  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
68  Common::UString varName = Common::UString::format("LOCAL_INT_%i", ctx.getParams()[1].getInt());
69  if (object)
70  ctx.getReturn() = object->getVariable(varName, kTypeInt).getInt();
71 }
72 
74  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
75  Common::UString varName = Common::UString::format("LOCAL_BOOL_%i", ctx.getParams()[1].getInt());
76  if (object)
77  ctx.getReturn() = object->getVariable(varName, kTypeInt).getInt() != 0;
78 }
79 
81  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
82  Common::UString varName = Common::UString::format("LOCAL_FLOAT_%i", ctx.getParams()[1].getInt());
83  if (object)
84  ctx.getReturn() = object->getVariable(varName, kTypeFloat).getFloat();
85 }
86 
88  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
89  Common::UString varName = Common::UString::format("LOCAL_STRING_%i", ctx.getParams()[1].getInt());
90  if (object)
91  ctx.getReturn() = object->getVariable(varName, kTypeString).getString();
92 }
93 
95  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
96  Common::UString varName = Common::UString::format("LOCAL_OBJECT_%i", ctx.getParams()[1].getInt());
97  if (object)
98  ctx.getReturn() = object->getVariable(varName, kTypeObject).getObject();
99 }
100 
102  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
103  Common::UString varName = Common::UString::format("LOCAL_INT_%i", ctx.getParams()[1].getInt());
104  if (object)
105  object->setVariable(varName, ctx.getParams()[2].getInt());
106 }
107 
109  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
110  Common::UString varName = Common::UString::format("LOCAL_BOOL_%i", ctx.getParams()[1].getInt());
111  if (object)
112  object->setVariable(varName, ctx.getParams()[2].getInt() != 0);
113 }
114 
116  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
117  Common::UString varName = Common::UString::format("LOCAL_FLOAT_%i", ctx.getParams()[1].getInt());
118  if (object)
119  object->setVariable(varName, ctx.getParams()[2].getFloat());
120 }
121 
123  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
124  Common::UString varName = Common::UString::format("LOCAL_STRING_%i", ctx.getParams()[1].getInt());
125  if (object)
126  object->setVariable(varName, ctx.getParams()[2].getString());
127 }
128 
130  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
131  Common::UString varName = Common::UString::format("LOCAL_OBJECT_%i", ctx.getParams()[1].getInt());
132  if (object)
133  object->setVariable(varName, ctx.getParams()[2].getObject());
134 }
135 
138 
139  ctx.getReturn() = (int32) (object ? object->getType() : -1);
140 }
141 
143  ctx.getReturn().getString().clear();
144 
145  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
146  if (object)
147  ctx.getReturn() = object->getTag();
148 }
149 
151  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
152 
154  if (object)
155  ctx.getReturn() = (Aurora::NWScript::Object *) object->getArea();
156 }
157 
160  if (!object)
161  return;
162 
163  ctx.getReturn() = object->getLocation();
164 }
165 
167  ctx.getReturn().setVector(0.0f, 0.0f, 0.0f);
168 
170  if (!object)
171  return;
172 
173  float x, y, z;
174  object->getPosition(x, y, z);
175 
176  ctx.getReturn().setVector(x, y, z);
177 }
178 
179 #define SQR(x) ((x) * (x))
181  ctx.getReturn() = -1.0f;
182 
185  if (!object1 || !object2)
186  return;
187 
188  float x1, y1, z1;
189  object1->getPosition(x1, y1, z1);
190 
191  float x2, y2, z2;
192  object2->getPosition(x2, y2, z2);
193 
194  ctx.getReturn() = sqrtf(SQR(x1 - x2) + SQR(y1 - y2) + SQR(z1 - z2));
195 }
196 #undef SQR
197 
199  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
200 
201  const Common::UString &tag = ctx.getParams()[0].getString();
202  if (tag.empty())
203  return;
204 
205  int nth = ctx.getParams()[1].getInt();
206 
208  while (nth-- > 0)
209  search->next();
210 
211  ctx.getReturn() = search->get();
212 }
213 
215  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
216 
217  const Common::UString &tag = ctx.getParams()[0].getString();
218  if (tag.empty())
219  return;
220 
222  Aurora::NWScript::Object *object = 0;
223 
224  while ((object = search->next())) {
225  Waypoint *waypoint = Jade::ObjectContainer::toWaypoint(object);
226 
227  if (waypoint) {
228  ctx.getReturn() = (Aurora::NWScript::Object *) waypoint;
229  break;
230  }
231  }
232 }
233 
235  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
236 
238  if (!target)
239  return;
240 
241  // Bitfield of type(s) to check for
242  uint32 type = ctx.getParams()[0].getInt();
243  // We want the nth nearest object
244  size_t nth = MAX<int32>(ctx.getParams()[2].getInt() - 1, 0);
245 
247  Aurora::NWScript::Object *object = 0;
248 
249  std::list<Object *> objects;
250  while ((object = search->next())) {
251  // Needs to be a valid object, not the target, but in the target's area
252  Jade::Object *nwnObject = Jade::ObjectContainer::toObject(object);
253  if (!nwnObject || (nwnObject == target) || (nwnObject->getArea() != target->getArea()))
254  continue;
255 
256  // Ignore invalid object types
257  uint32 objectType = (uint32) nwnObject->getType();
258  if ((objectType == kObjectTypeInvalid) || (objectType >= kObjectTypeMAX))
259  continue;
260 
261  // Convert the type into a bitfield value and check against the type bitfield
262 
263  if (type & (1 << (objectType - 1)))
264  objects.push_back(nwnObject);
265  }
266 
267  objects.sort(ObjectDistanceSort(*target));
268 
269  std::list<Object *>::iterator it = objects.begin();
270  for (size_t n = 0; (n < nth) && (it != objects.end()); ++n)
271  ++it;
272 
273  if (it != objects.end())
274  ctx.getReturn() = *it;
275 }
276 
279  if (!object)
280  return;
281 
282  Animation animation = (Animation) ctx.getParams()[0].getInt();
283 
284  // TODO: speed, second
285  // float speed = ctx.getParams()[1].getFloat();
286  // float seconds = ctx.getParams()[2].getFloat();
287 
288  object->playAnimation(animation);
289 }
290 
293  Jade::Location *moveTo = Jade::ObjectContainer::toLocation(ctx.getParams()[0].getEngineType());
294 
295  if (!object || !moveTo)
296  return;
297 
298  float x, y, z;
299  moveTo->getPosition(x, y, z);
300 
301  jumpTo(object, moveTo->getArea(), x, y, z);
302 }
303 
305  // TODO: walkStraightLineToPoint
306  // bool walkStraightLineToPoint = ctx.getParams()[1].getInt() != 0;
307 
310 
311  if (!object || !moveTo)
312  return;
313 
314  float x, y, z;
315  moveTo->getPosition(x, y, z);
316 
317  jumpTo(object, moveTo->getArea(), x, y, z);
318 }
319 
322  ctx.getReturn() = object->getConversation();
323 }
324 
327  ctx.getReturn() = (int32) (object->getLastTriggerer() != 0);
328 }
329 
332  if (!object)
333  return;
334 
335  bool noCollide = ctx.getParams()[1].getInt() != 0;
336 
337  object->setNoCollide(noCollide);
338 }
339 
340 } // End of namespace Jade
341 
342 } // End of namespace Engines
An object within a Jade Empire area.
void getEnteringObject(Aurora::NWScript::FunctionContext &ctx)
void getObjectType(Aurora::NWScript::FunctionContext &ctx)
void getExitingObject(Aurora::NWScript::FunctionContext &ctx)
void setLocalObject(Aurora::NWScript::FunctionContext &ctx)
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:122
Module & getModule()
Return the module context.
Definition: game.cpp:61
void jumpToObject(Aurora::NWScript::FunctionContext &ctx)
A class holding an UTF-8 string.
Definition: ustring.h:48
void jumpTo(Jade::Object *object, Area *area, float x, float y, float z)
Definition: functions.cpp:129
void getNearestObject(Aurora::NWScript::FunctionContext &ctx)
Context of an NWScript function.
Jade Empire engine functions.
void setLocalBool(Aurora::NWScript::FunctionContext &ctx)
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:118
The context handling the gameplay in Jade Empire.
void getLocalFloat(Aurora::NWScript::FunctionContext &ctx)
void setLocalString(Aurora::NWScript::FunctionContext &ctx)
void getPosition(float &x, float &y, float &z) const
Return the location&#39;s position.
Definition: location.cpp:52
static Waypoint * toWaypoint(Aurora::NWScript::Object *object)
void getLocalString(Aurora::NWScript::FunctionContext &ctx)
static Creature * toPC(Aurora::NWScript::Object *object)
void getLocalInt(Aurora::NWScript::FunctionContext &ctx)
void getObjectByTag(Aurora::NWScript::FunctionContext &ctx)
A simple scoped smart pointer template.
A class able to sort objects by distance to a target object.
A container of Jade Empire objects.
#define SQR(x)
void getIsPC(Aurora::NWScript::FunctionContext &ctx)
static Jade::Object * toObject(::Aurora::NWScript::Object *object)
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
void getTag(Aurora::NWScript::FunctionContext &ctx)
Basic Jade Empire type definitions.
void getArea(Aurora::NWScript::FunctionContext &ctx)
Object * getObject() const
Definition: variable.cpp:321
Utility templates and functions.
void getDistanceToObject(Aurora::NWScript::FunctionContext &ctx)
void setVector(float x, float y, float z)
Definition: variable.cpp:335
void getIsObjectValid(Aurora::NWScript::FunctionContext &ctx)
ObjectSearch * findObjectsByTag(const Common::UString &tag) const
Return a search context to iterate over all objects with this tag.
ObjectSearch * findObjects() const
Return a search context to iterate over all objects.
void setLocalFloat(Aurora::NWScript::FunctionContext &ctx)
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void setLocalInt(Aurora::NWScript::FunctionContext &ctx)
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:142
void getPosition(Aurora::NWScript::FunctionContext &ctx)
Object * getLastTriggerer() const
Return the object that last triggered this object.
Definition: object.cpp:191
void getLocalBool(Aurora::NWScript::FunctionContext &ctx)
An object within a Jade area.
Definition: object.h:53
uint32_t uint32
Definition: types.h:204
Common::UString & getString()
Definition: variable.cpp:314
void getWaypointByTag(Aurora::NWScript::FunctionContext &ctx)
A module.
void getLocalObject(Aurora::NWScript::FunctionContext &ctx)
void jumpToLocation(Aurora::NWScript::FunctionContext &ctx)
static Location * toLocation(Aurora::NWScript::EngineType *engineType)
void getPriorActivation(Aurora::NWScript::FunctionContext &ctx)
void getObjectConversationResref(Aurora::NWScript::FunctionContext &ctx)
void getLocation(Aurora::NWScript::FunctionContext &ctx)
void setObjectNoCollide(Aurora::NWScript::FunctionContext &ctx)
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:58
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
void playAnimation(Aurora::NWScript::FunctionContext &ctx)
int32_t int32
Definition: types.h:203