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/nwn/types.h"
31 #include "src/engines/nwn/game.h"
32 #include "src/engines/nwn/module.h"
34 #include "src/engines/nwn/object.h"
36 
38 
39 namespace Engines {
40 
41 namespace NWN {
42 
48 
50  ctx.getReturn() = ctx.getTriggerer();
51 }
52 
54  // TODO: This should return the *last* entered object, i.e. it should remember past triggerers.
55  ctx.getReturn() = ctx.getTriggerer();
56 }
57 
59  // TODO: This should return the *last* exited object, i.e. it should remember past triggerers.
60  ctx.getReturn() = ctx.getTriggerer();
61 }
62 
64  ctx.getReturn() = getParamObject(ctx, 0) != 0;
65 }
66 
68  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
69  if (object)
70  ctx.getReturn() = object->getVariable(ctx.getParams()[1].getString(), kTypeInt).getInt();
71 }
72 
74  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
75  if (object)
76  ctx.getReturn() = object->getVariable(ctx.getParams()[1].getString(), kTypeFloat).getFloat();
77 }
78 
80  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
81  if (object)
82  ctx.getReturn() = object->getVariable(ctx.getParams()[1].getString(), kTypeString).getString();
83 }
84 
86  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
87  if (object)
88  ctx.getReturn() = object->getVariable(ctx.getParams()[1].getString(), kTypeObject).getObject();
89 }
90 
92  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
93  if (object)
94  object->setVariable(ctx.getParams()[1].getString(), ctx.getParams()[2].getInt());
95 }
96 
98  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
99  if (object)
100  object->setVariable(ctx.getParams()[1].getString(), ctx.getParams()[2].getFloat());
101 }
102 
104  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
105  if (object)
106  object->setVariable(ctx.getParams()[1].getString(), ctx.getParams()[2].getString());
107 }
108 
110  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
111  if (object)
112  object->setVariable(ctx.getParams()[1].getString(), ctx.getParams()[2].getObject());
113 }
114 
117 
119  if (!object || ((uint32)object->getType() >= kObjectTypeMAX))
120  return;
121 
122  ctx.getReturn() = (int32) object->getType();
123 }
124 
126  ctx.getReturn().getString().clear();
127 
128  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
129 
130  // PCs don't have a tag, and the scripts depend on that
131  if (object == _game->getModule().getPC())
132  object = 0;
133 
134  if (object)
135  ctx.getReturn() = object->getTag();
136 }
137 
139  // TODO: bOriginalName
140 
142  ctx.getReturn().getString() = object ? object->getName() : "";
143 }
144 
146  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
147 
149  if (object)
150  ctx.getReturn() = (Aurora::NWScript::Object *) object->getArea();
151 }
152 
155  if (!object)
156  return;
157 
158  ctx.getReturn() = object->getLocation();
159 }
160 
162  ctx.getReturn().setVector(0.0f, 0.0f, 0.0f);
163 
165  if (!object)
166  return;
167 
168  float x, y, z;
169  object->getPosition(x, y, z);
170 
171  ctx.getReturn().setVector(x, y, z);
172 }
173 
174 #define SQR(x) ((x) * (x))
176  ctx.getReturn() = -1.0f;
177 
180  if (!object1 || !object2)
181  return;
182 
183  float x1, y1, z1;
184  object1->getPosition(x1, y1, z1);
185 
186  float x2, y2, z2;
187  object2->getPosition(x2, y2, z2);
188 
189  ctx.getReturn() = sqrtf(SQR(x1 - x2) + SQR(y1 - y2) + SQR(z1 - z2));
190 }
191 #undef SQR
192 
194  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
195 
196  const Common::UString &tag = ctx.getParams()[0].getString();
197  if (tag.empty())
198  return;
199 
200  int nth = ctx.getParams()[1].getInt();
201 
203  while (nth-- > 0)
204  search->next();
205 
206  ctx.getReturn() = search->get();
207 }
208 
210  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
211 
212  const Common::UString &tag = ctx.getParams()[0].getString();
213  if (tag.empty())
214  return;
215 
217  Aurora::NWScript::Object *object = 0;
218 
219  while ((object = search->next())) {
220  Waypoint *waypoint = NWN::ObjectContainer::toWaypoint(object);
221 
222  if (waypoint) {
223  ctx.getReturn() = (Aurora::NWScript::Object *) waypoint;
224  break;
225  }
226  }
227 }
228 
230  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
231 
233  if (!target)
234  return;
235 
236  // Bitfield of type(s) to check for
237  uint32 type = ctx.getParams()[0].getInt();
238  // We want the nth nearest object
239  size_t nth = MAX<int32>(ctx.getParams()[2].getInt() - 1, 0);
240 
242  Aurora::NWScript::Object *object = 0;
243 
244  std::list<Object *> objects;
245  while ((object = search->next())) {
246  // Needs to be a valid object, not the target, but in the target's area
247  NWN::Object *nwnObject = NWN::ObjectContainer::toObject(object);
248  if (!nwnObject || (nwnObject == target) || (nwnObject->getArea() != target->getArea()))
249  continue;
250 
251  // Ignore invalid object types
252  const uint32 objectType = (uint32) nwnObject->getType();
253  if (objectType >= kObjectTypeMAX)
254  continue;
255 
256  if (type & objectType)
257  objects.push_back(nwnObject);
258  }
259 
260  objects.sort(ObjectDistanceSort(*target));
261 
262  std::list<Object *>::iterator it = objects.begin();
263  for (size_t n = 0; (n < nth) && (it != objects.end()); ++n)
264  ++it;
265 
266  if (it != objects.end())
267  ctx.getReturn() = *it;
268 }
269 
271  ctx.getReturn() = (Object *) 0;
272 
273  const Common::UString &tag = ctx.getParams()[0].getString();
274  if (tag.empty())
275  return;
276 
278  if (!target)
279  return;
280 
281  size_t nth = MAX<int32>(ctx.getParams()[2].getInt() - 1, 0);
282 
284  Aurora::NWScript::Object *object = 0;
285 
286  std::list<Object *> objects;
287  while ((object = search->next())) {
288  // Needs to be a valid object, not the target, but in the target's area
289  NWN::Object *nwnObject = NWN::ObjectContainer::toObject(object);
290  if (!nwnObject || (nwnObject == target) || (nwnObject->getArea() != target->getArea()))
291  continue;
292 
293  objects.push_back(nwnObject);
294  }
295 
296  objects.sort(ObjectDistanceSort(*target));
297 
298  std::list<Object *>::iterator it = objects.begin();
299  for (size_t n = 0; (n < nth) && (it != objects.end()); ++n)
300  ++it;
301 
302  if (it != objects.end())
303  ctx.getReturn() = *it;
304 }
305 
307  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
308 
310  if (!target)
311  return;
312 
313  size_t nth = MAX<int32>(ctx.getParams()[3].getInt() - 1, 0);
314 
315  /* TODO: Criteria:
316  *
317  * int crit1Type = ctx.getParams()[0].getInt();
318  * int crit1Value = ctx.getParams()[1].getInt();
319  * int crit2Type = ctx.getParams()[4].getInt();
320  * int crit2Value = ctx.getParams()[5].getInt();
321  * int crit3Type = ctx.getParams()[6].getInt();
322  * int crit3Value = ctx.getParams()[7].getInt();
323  */
324 
326  Aurora::NWScript::Object *object = 0;
327 
328  std::list<Object *> creatures;
329  while ((object = search->next())) {
330  Creature *creature = NWN::ObjectContainer::toCreature(object);
331 
332  if (creature && (creature != target) && (creature->getArea() == target->getArea()))
333  creatures.push_back(creature);
334  }
335 
336  creatures.sort(ObjectDistanceSort(*target));
337 
338  std::list<Object *>::iterator it = creatures.begin();
339  for (size_t n = 0; (n < nth) && (it != creatures.end()); ++n)
340  ++it;
341 
342  if (it != creatures.end())
343  ctx.getReturn() = *it;
344 }
345 
348  if (!object)
349  return;
350 
351  const Animation animation = (Animation) ctx.getParams()[0].getInt();
352 
353  float speed = ctx.getParams()[1].getFloat();
354  float seconds = ctx.getParams()[2].getFloat();
355 
356  if (isAnimationLooping(animation)) {
357  /* Looping animations. Animations that can play for a variable amount
358  of time, like talking or meditating. */
359 
360  // Length of 0 seconds? Don't play the animation.
361  if (seconds == 0.0f)
362  return;
363 
364  if (seconds < 0.0f) {
365  warning("Functions::%s(): Seconds == %f", ctx.getName().c_str(), seconds);
366  return;
367  }
368 
369  if (speed <= 0.0f) {
370  warning("Functions::%s(): Speed == %f", ctx.getName().c_str(), speed);
371  return;
372  }
373 
374  } else {
375  /* Fire-and-forget animations. Simple one-action animations, like
376  bowing, drinking from a flask, etc. On these animations, the speed
377  is ignored; they're always played a normal speed. Likewise, the
378  animation length (in seconds) is ignored; they're always played
379  one single time. We set the length to zero so that our animation
380  system can figure out the specifics. */
381 
382  speed = 1.0f;
383  seconds = 0.0f;
384  }
385 
386  object->playAnimation(animation, true, seconds, speed);
387 }
388 
391  NWN::Location *moveTo = NWN::ObjectContainer::toLocation(ctx.getParams()[0].getEngineType());
392 
393  if (!object || !moveTo)
394  return;
395 
396  float x, y, z;
397  moveTo->getPosition(x, y, z);
398 
399  jumpTo(object, moveTo->getArea(), x, y, z);
400 }
401 
403  // TODO: walkStraightLineToPoint
404  // bool walkStraightLineToPoint = ctx.getParams()[1].getInt() != 0;
405 
408 
409  if (!object || !moveTo)
410  return;
411 
412  float x, y, z;
413  moveTo->getPosition(x, y, z);
414 
415  jumpTo(object, moveTo->getArea(), x, y, z);
416 }
417 
418 } // End of namespace NWN
419 
420 } // End of namespace Engines
void getWaypointByTag(Aurora::NWScript::FunctionContext &ctx)
void getLocation(Aurora::NWScript::FunctionContext &ctx)
Creature * getPC()
Return the currently playing PC.
Definition: module.cpp:298
void getName(Aurora::NWScript::FunctionContext &ctx)
void jumpTo(NWN::Object *object, Area *area, float x, float y, float z)
Definition: functions.cpp:129
A container of Neverwinter Nights objects.
void getLocalInt(Aurora::NWScript::FunctionContext &ctx)
A class holding an UTF-8 string.
Definition: ustring.h:48
void setLocalObject(Aurora::NWScript::FunctionContext &ctx)
void jumpToLocation(Aurora::NWScript::FunctionContext &ctx)
static Creature * toCreature(Aurora::NWScript::Object *object)
Context of an NWScript function.
void getNearestObject(Aurora::NWScript::FunctionContext &ctx)
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:136
void getLocalObject(Aurora::NWScript::FunctionContext &ctx)
void getLocalString(Aurora::NWScript::FunctionContext &ctx)
Basic Neverwinter Nights type definitions.
void setLocalFloat(Aurora::NWScript::FunctionContext &ctx)
static NWN::Object * toObject(::Aurora::NWScript::Object *object)
void getClickingObject(Aurora::NWScript::FunctionContext &ctx)
void playAnimation(Aurora::NWScript::FunctionContext &ctx)
void getEnteringObject(Aurora::NWScript::FunctionContext &ctx)
static Location * toLocation(Aurora::NWScript::EngineType *engineType)
A simple scoped smart pointer template.
A class able to sort objects by distance to a target object.
The context needed to run a Neverwinter Nights module.
#define SQR(x)
void getIsObjectValid(Aurora::NWScript::FunctionContext &ctx)
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:118
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
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.
void getExitingObject(Aurora::NWScript::FunctionContext &ctx)
void setVector(float x, float y, float z)
Definition: variable.cpp:335
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.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void getObjectType(Aurora::NWScript::FunctionContext &ctx)
const Common::UString & getName() const
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void warning(const char *s,...)
Definition: util.cpp:33
void getArea(Aurora::NWScript::FunctionContext &ctx)
void getPosition(Aurora::NWScript::FunctionContext &ctx)
An object in a Neverwinter Nights area.
void getNearestCreature(Aurora::NWScript::FunctionContext &ctx)
void getDistanceToObject(Aurora::NWScript::FunctionContext &ctx)
bool isAnimationLooping(Animation animation)
Return true if the specified animation allows looping.
Definition: types.cpp:166
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)
uint32_t uint32
Definition: types.h:204
A creature in a Neverwinter Nights area.
Common::UString & getString()
Definition: variable.cpp:314
void setLocalInt(Aurora::NWScript::FunctionContext &ctx)
The context handling the gameplay in Neverwinter Nights.
void jumpToObject(Aurora::NWScript::FunctionContext &ctx)
void getObjectByTag(Aurora::NWScript::FunctionContext &ctx)
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:73
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
Neverwinter Nights engine functions.
Module & getModule()
Return the module context.
Definition: game.cpp:70
void getTag(Aurora::NWScript::FunctionContext &ctx)
void getLocalFloat(Aurora::NWScript::FunctionContext &ctx)
void getNearestObjectByTag(Aurora::NWScript::FunctionContext &ctx)
int32_t int32
Definition: types.h:203