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/nwn2/types.h"
31 #include "src/engines/nwn2/game.h"
36 
38 
39 namespace Engines {
40 
41 namespace NWN2 {
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 = NWN2::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  NWN2::Object *nwn2Object = NWN2::ObjectContainer::toObject(object);
248  if (!nwn2Object || (nwn2Object == target) || (nwn2Object->getArea() != target->getArea()))
249  continue;
250 
251  // Ignore invalid object types
252  const uint32 objectType = (uint32) nwn2Object->getType();
253  if (objectType >= kObjectTypeMAX)
254  continue;
255 
256  if (type & objectType)
257  objects.push_back(nwn2Object);
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  NWN2::Object *nwn2Object = NWN2::ObjectContainer::toObject(object);
290  if (!nwn2Object || (nwn2Object == target) || (nwn2Object->getArea() != target->getArea()))
291  continue;
292 
293  objects.push_back(nwn2Object);
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 = NWN2::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  NWN2::Location *moveTo = NWN2::ObjectContainer::toLocation(ctx.getParams()[0].getEngineType());
349 
350  if (!object || !moveTo)
351  return;
352 
353  float x, y, z;
354  moveTo->getPosition(x, y, z);
355 
356  jumpTo(object, moveTo->getArea(), x, y, z);
357 }
358 
360  // TODO: walkStraightLineToPoint
361  // bool walkStraightLineToPoint = ctx.getParams()[1].getInt() != 0;
362 
365 
366  if (!object || !moveTo)
367  return;
368 
369  float x, y, z;
370  moveTo->getPosition(x, y, z);
371 
372  jumpTo(object, moveTo->getArea(), x, y, z);
373 }
374 
375 } // End of namespace NWN2
376 
377 } // End of namespace Engines
A class able to sort objects by distance to a target object.
void getNearestObjectByTag(Aurora::NWScript::FunctionContext &ctx)
The context handling the gameplay in Neverwinter Nights 2.
static NWN2::Object * toObject(::Aurora::NWScript::Object *object)
void getLocalFloat(Aurora::NWScript::FunctionContext &ctx)
A class holding an UTF-8 string.
Definition: ustring.h:48
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:122
void getClickingObject(Aurora::NWScript::FunctionContext &ctx)
void getArea(Aurora::NWScript::FunctionContext &ctx)
void getObjectByTag(Aurora::NWScript::FunctionContext &ctx)
Context of an NWScript function.
void setLocalObject(Aurora::NWScript::FunctionContext &ctx)
void getEnteringObject(Aurora::NWScript::FunctionContext &ctx)
void getObjectType(Aurora::NWScript::FunctionContext &ctx)
void getTag(Aurora::NWScript::FunctionContext &ctx)
void setLocalInt(Aurora::NWScript::FunctionContext &ctx)
#define SQR(x)
static Creature * toCreature(Aurora::NWScript::Object *object)
An object within a NWN2 area.
Definition: object.h:58
void getLocalString(Aurora::NWScript::FunctionContext &ctx)
void jumpTo(NWN2::Object *object, Area *area, float x, float y, float z)
Definition: functions.cpp:129
void getLocalInt(Aurora::NWScript::FunctionContext &ctx)
A simple scoped smart pointer template.
void getPosition(float &x, float &y, float &z) const
Return the location&#39;s position.
Definition: location.cpp:52
void setLocalString(Aurora::NWScript::FunctionContext &ctx)
The context needed to run a Neverwinter Nights 2 module.
Neverwinter Nights 2 engine functions.
A creature in a Neverwinter Nights 2 area.
A container of Neverwinter Nights 2 objects.
Utility templates and functions.
Basic Neverwinter Nights 2 type definitions.
static Location * toLocation(Aurora::NWScript::EngineType *engineType)
void getLocation(Aurora::NWScript::FunctionContext &ctx)
void getPosition(Aurora::NWScript::FunctionContext &ctx)
void setVector(float x, float y, float z)
Definition: variable.cpp:335
void getName(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.
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void setLocalFloat(Aurora::NWScript::FunctionContext &ctx)
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void getExitingObject(Aurora::NWScript::FunctionContext &ctx)
uint32_t uint32
Definition: types.h:204
Common::UString & getString()
Definition: variable.cpp:314
void jumpToLocation(Aurora::NWScript::FunctionContext &ctx)
void getWaypointByTag(Aurora::NWScript::FunctionContext &ctx)
void getNearestObject(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
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:118
An object in a Neverwinter Nights 2 area.
void getDistanceToObject(Aurora::NWScript::FunctionContext &ctx)
Creature * getPC()
Return the currently playing PC.
Definition: module.cpp:78
void jumpToObject(Aurora::NWScript::FunctionContext &ctx)
void getLocalObject(Aurora::NWScript::FunctionContext &ctx)
void getNearestCreature(Aurora::NWScript::FunctionContext &ctx)
Module & getModule()
Return the module context.
Definition: game.cpp:64
void getIsObjectValid(Aurora::NWScript::FunctionContext &ctx)
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:72
int32_t int32
Definition: types.h:203
static Waypoint * toWaypoint(Aurora::NWScript::Object *object)