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 
36 
38 
39 namespace Engines {
40 
41 namespace Witcher {
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  if (object)
130  ctx.getReturn() = object->getTag();
131 }
132 
134  // TODO: bOriginalName
135 
137  ctx.getReturn().getString() = object ? object->getName().getString() : "";
138 }
139 
141  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
142 
144  if (object)
145  ctx.getReturn() = (Aurora::NWScript::Object *) object->getArea();
146 }
147 
150  if (!object)
151  return;
152 
153  ctx.getReturn() = object->getLocation();
154 }
155 
157  ctx.getReturn().setVector(0.0f, 0.0f, 0.0f);
158 
160  if (!object)
161  return;
162 
163  float x, y, z;
164  object->getPosition(x, y, z);
165 
166  ctx.getReturn().setVector(x, y, z);
167 }
168 
169 #define SQR(x) ((x) * (x))
171  ctx.getReturn() = -1.0f;
172 
175  if (!object1 || !object2)
176  return;
177 
178  float x1, y1, z1;
179  object1->getPosition(x1, y1, z1);
180 
181  float x2, y2, z2;
182  object2->getPosition(x2, y2, z2);
183 
184  ctx.getReturn() = sqrtf(SQR(x1 - x2) + SQR(y1 - y2) + SQR(z1 - z2));
185 }
186 #undef SQR
187 
189  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
190 
191  const Common::UString &tag = ctx.getParams()[0].getString();
192  if (tag.empty())
193  return;
194 
195  int nth = ctx.getParams()[1].getInt();
196 
198  while (nth-- > 0)
199  search->next();
200 
201  ctx.getReturn() = search->get();
202 }
203 
205  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
206 
207  const Common::UString &tag = ctx.getParams()[0].getString();
208  if (tag.empty())
209  return;
210 
212  Aurora::NWScript::Object *object = 0;
213 
214  while ((object = search->next())) {
216 
217  if (waypoint) {
218  ctx.getReturn() = (Aurora::NWScript::Object *) waypoint;
219  break;
220  }
221  }
222 }
223 
225  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
226 
228  if (!target)
229  return;
230 
231  // Bitfield of type(s) to check for
232  uint32 type = ctx.getParams()[0].getInt();
233  // We want the nth nearest object
234  size_t nth = MAX<int32>(ctx.getParams()[2].getInt() - 1, 0);
235 
237  Aurora::NWScript::Object *object = 0;
238 
239  std::list<Object *> objects;
240  while ((object = search->next())) {
241  // Needs to be a valid object, not the target, but in the target's area
242  Witcher::Object *witcherObject = Witcher::ObjectContainer::toObject(object);
243  if (!witcherObject || (witcherObject == target) || (witcherObject->getArea() != target->getArea()))
244  continue;
245 
246  // Ignore invalid object types
247  const uint32 objectType = (uint32) witcherObject->getType();
248  if (objectType >= kObjectTypeMAX)
249  continue;
250 
251  if (type & objectType)
252  objects.push_back(witcherObject);
253  }
254 
255  objects.sort(ObjectDistanceSort(*target));
256 
257  std::list<Object *>::iterator it = objects.begin();
258  for (size_t n = 0; (n < nth) && (it != objects.end()); ++n)
259  ++it;
260 
261  if (it != objects.end())
262  ctx.getReturn() = *it;
263 }
264 
266  ctx.getReturn() = (Object *) 0;
267 
268  const Common::UString &tag = ctx.getParams()[0].getString();
269  if (tag.empty())
270  return;
271 
273  if (!target)
274  return;
275 
276  size_t nth = MAX<int32>(ctx.getParams()[2].getInt() - 1, 0);
277 
279  Aurora::NWScript::Object *object = 0;
280 
281  std::list<Object *> objects;
282  while ((object = search->next())) {
283  // Needs to be a valid object, not the target, but in the target's area
284  Witcher::Object *witcherObject = Witcher::ObjectContainer::toObject(object);
285  if (!witcherObject || (witcherObject == target) || (witcherObject->getArea() != target->getArea()))
286  continue;
287 
288  objects.push_back(witcherObject);
289  }
290 
291  objects.sort(ObjectDistanceSort(*target));
292 
293  std::list<Object *>::iterator it = objects.begin();
294  for (size_t n = 0; (n < nth) && (it != objects.end()); ++n)
295  ++it;
296 
297  if (it != objects.end())
298  ctx.getReturn() = *it;
299 }
300 
302  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
303 
305  if (!target)
306  return;
307 
308  size_t nth = MAX<int32>(ctx.getParams()[3].getInt() - 1, 0);
309 
310  /* TODO: Criteria:
311  *
312  * int crit1Type = ctx.getParams()[0].getInt();
313  * int crit1Value = ctx.getParams()[1].getInt();
314  * int crit2Type = ctx.getParams()[4].getInt();
315  * int crit2Value = ctx.getParams()[5].getInt();
316  * int crit3Type = ctx.getParams()[6].getInt();
317  * int crit3Value = ctx.getParams()[7].getInt();
318  */
319 
321  Aurora::NWScript::Object *object = 0;
322 
323  std::list<Object *> creatures;
324  while ((object = search->next())) {
326 
327  if (creature && (creature != target) && (creature->getArea() == target->getArea()))
328  creatures.push_back(creature);
329  }
330 
331  creatures.sort(ObjectDistanceSort(*target));
332 
333  std::list<Object *>::iterator it = creatures.begin();
334  for (size_t n = 0; (n < nth) && (it != creatures.end()); ++n)
335  ++it;
336 
337  if (it != creatures.end())
338  ctx.getReturn() = *it;
339 }
340 
343  Witcher::Location *moveTo = Witcher::ObjectContainer::toLocation(ctx.getParams()[0].getEngineType());
344 
345  if (!object || !moveTo)
346  return;
347 
348  float x, y, z;
349  moveTo->getPosition(x, y, z);
350 
351  jumpTo(object, moveTo->getArea(), x, y, z);
352 }
353 
355  // TODO: walkStraightLineToPoint
356  // bool walkStraightLineToPoint = ctx.getParams()[1].getInt() != 0;
357 
360 
361  if (!object || !moveTo)
362  return;
363 
364  float x, y, z;
365  moveTo->getPosition(x, y, z);
366 
367  jumpTo(object, moveTo->getArea(), x, y, z);
368 }
369 
370 } // End of namespace Witcher
371 
372 } // End of namespace Engines
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:61
A container of The Witcher objects.
void setLocalFloat(Aurora::NWScript::FunctionContext &ctx)
void getNearestCreature(Aurora::NWScript::FunctionContext &ctx)
void getClickingObject(Aurora::NWScript::FunctionContext &ctx)
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:112
void getLocation(Aurora::NWScript::FunctionContext &ctx)
A class holding an UTF-8 string.
Definition: ustring.h:48
An object within a Witcher area.
Definition: object.h:51
void getEnteringObject(Aurora::NWScript::FunctionContext &ctx)
Context of an NWScript function.
void getArea(Aurora::NWScript::FunctionContext &ctx)
void getLocalFloat(Aurora::NWScript::FunctionContext &ctx)
void getNearestObjectByTag(Aurora::NWScript::FunctionContext &ctx)
void getWaypointByTag(Aurora::NWScript::FunctionContext &ctx)
static Creature * toCreature(Aurora::NWScript::Object *object)
Basic The Witcher type definitions.
static Waypoint * toWaypoint(Aurora::NWScript::Object *object)
void getLocalObject(Aurora::NWScript::FunctionContext &ctx)
void setLocalString(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:132
void jumpTo(Witcher::Object *object, Area *area, float x, float y, float z)
Definition: functions.cpp:135
#define SQR(x)
Module & getModule()
Return the module context.
Definition: game.cpp:67
A simple scoped smart pointer template.
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:124
void jumpToObject(Aurora::NWScript::FunctionContext &ctx)
A creature in a The Witcher area.
Utility templates and functions.
An object in a The Witcher area.
void setVector(float x, float y, float z)
Definition: variable.cpp:335
void getLocalString(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
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void getTag(Aurora::NWScript::FunctionContext &ctx)
void setLocalInt(Aurora::NWScript::FunctionContext &ctx)
void getLocalInt(Aurora::NWScript::FunctionContext &ctx)
void getNearestObject(Aurora::NWScript::FunctionContext &ctx)
static Location * toLocation(Aurora::NWScript::EngineType *engineType)
void getName(Aurora::NWScript::FunctionContext &ctx)
void getPosition(float &x, float &y, float &z) const
Return the location&#39;s position.
Definition: location.cpp:52
uint32_t uint32
Definition: types.h:204
The context needed to run a The Witcher module.
Common::UString & getString()
Definition: variable.cpp:314
The Witcher engine functions.
void getIsObjectValid(Aurora::NWScript::FunctionContext &ctx)
void getDistanceToObject(Aurora::NWScript::FunctionContext &ctx)
void getExitingObject(Aurora::NWScript::FunctionContext &ctx)
void getObjectType(Aurora::NWScript::FunctionContext &ctx)
void jumpToLocation(Aurora::NWScript::FunctionContext &ctx)
The context handling the gameplay in The Witcher.
void getPosition(Aurora::NWScript::FunctionContext &ctx)
void setLocalObject(Aurora::NWScript::FunctionContext &ctx)
void getObjectByTag(Aurora::NWScript::FunctionContext &ctx)
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
static Witcher::Object * toObject(::Aurora::NWScript::Object *object)
A class able to sort objects by distance to a target object.
int32_t int32
Definition: types.h:203