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 <boost/make_shared.hpp>
26 
27 #include "src/common/scopedptr.h"
28 #include "src/common/util.h"
29 #include "src/common/strutil.h"
30 
32 
39 
41 
42 namespace Engines {
43 
44 namespace DragonAge2 {
45 
51 
53  ctx.getReturn() = getParamObject(ctx, 0) != 0;
54 }
55 
57  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
58 
59  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
60  if (object)
61  ctx.getReturn() = object->getVariable(name, kTypeInt).getInt();
62 }
63 
65  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
66 
67  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
68  if (object)
69  ctx.getReturn() = object->getVariable(name, kTypeFloat).getFloat();
70 }
71 
73  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
74 
75  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
76  if (object)
77  ctx.getReturn() = object->getVariable(name, kTypeString).getString();
78 }
79 
81  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
82 
83  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
84  if (object)
85  ctx.getReturn() = object->getVariable(name, kTypeObject).getObject();
86 }
87 
89  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
90 
91  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
92  if (object)
93  ctx.getReturn() = object->getVariable(name, kTypeString).getString();
94 }
95 
97  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
98 
99  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
100  if (object)
101  ctx.getReturn() = object->getVariable(name, kTypeEngineType).getEngineType();
102 }
103 
105  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
106 
107  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
108  if (object)
109  ctx.getReturn() = object->getVariable(name, kTypeEngineType).getEngineType();
110 }
111 
113  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
114 
115  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
116  if (object)
117  ctx.getReturn() = object->getVariable(name, kTypeEngineType).getEngineType();
118 }
119 
121  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
122 
123  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
124  if (object)
125  ctx.getReturn() = object->getVariable(name, kTypeEngineType).getEngineType();
126 }
127 
129  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
130 
131  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
132  if (object)
133  ctx.getReturn() = object->getVariable(name, kTypeEngineType).getEngineType();
134 }
135 
137  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
138 
139  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
140  if (object)
141  ctx.getReturn() = object->getVariable(name, kTypeEngineType).getEngineType();
142 }
143 
145  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
146 
147  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
148  if (object)
149  object->setVariable(name, ctx.getParams()[2].getInt());
150 }
151 
153  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
154 
155  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
156  if (object)
157  object->setVariable(name, ctx.getParams()[2].getFloat());
158 }
159 
161  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
162 
163  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
164  if (object)
165  object->setVariable(name, ctx.getParams()[2].getString());
166 }
167 
169  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
170 
171  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
172  if (object)
173  object->setVariable(name, ctx.getParams()[2].getObject());
174 }
175 
177  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
178 
179  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
180  if (object)
181  object->setVariable(name, ctx.getParams()[2].getString());
182 }
183 
185  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
186 
187  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
188  if (object)
189  object->setVariable(name, ctx.getParams()[2].getEngineType());
190 }
191 
193  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
194 
195  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
196  if (object)
197  object->setVariable(name, ctx.getParams()[2].getEngineType());
198 }
199 
201  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
202 
203  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
204  if (object)
205  object->setVariable(name, ctx.getParams()[2].getEngineType());
206 }
207 
209  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
210 
211  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
212  if (object)
213  object->setVariable(name, ctx.getParams()[2].getEngineType());
214 }
215 
217  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
218 
219  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
220  if (object)
221  object->setVariable(name, ctx.getParams()[2].getEngineType());
222 }
223 
225  const Common::UString name = Common::composeString(ctx.getParams()[1].getInt());
226 
227  Aurora::NWScript::Object *object = getParamObject(ctx, 0);
228  if (object)
229  object->setVariable(name, ctx.getParams()[2].getEngineType());
230 }
231 
234 
236  if (!object || ((uint32)object->getType() >= kObjectTypeMAX))
237  return;
238 
239  ctx.getReturn() = (int32) object->getType();
240 }
241 
243  ctx.getReturn().getString().clear();
244 
245  const Aurora::NWScript::Object *object = getParamObject(ctx, 0);
246  if (object)
247  ctx.getReturn() = object->getTag();
248 }
249 
252  ctx.getReturn().getString() = object ? object->getResRef() : "";
253 }
254 
256  ctx.getReturn().getString() = "";
257 
259  if (!object)
260  return;
261 
262  ctx.getReturn().getString() = object->getNonLocalizedName();
263  if (ctx.getReturn().getString().empty())
264  ctx.getReturn().getString() = object->getName().getString();
265 }
266 
269  if (!object)
270  return;
271 
272  object->setNonLocalizedName(ctx.getParams()[1].getString());
273 }
274 
276  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
277 
279  if (!object)
280  return;
281 
283  if (!campaign)
284  return;
285 
286  ctx.getReturn() = (Aurora::NWScript::Object *) campaign->getCurrentArea();
287 }
288 
290  ctx.getReturn().setVector(0.0f, 0.0f, 0.0f);
291 
293  if (!object)
294  return;
295 
296  float x, y, z;
297  object->getPosition(x, y, z);
298 
299  ctx.getReturn().setVector(x, y, z);
300 }
301 
302 #define SQR(x) ((x) * (x))
304  ctx.getReturn() = -1.0f;
305 
308  if (!object1 || !object2)
309  return;
310 
311  float x1, y1, z1;
312  object1->getPosition(x1, y1, z1);
313 
314  float x2, y2, z2;
315  object2->getPosition(x2, y2, z2);
316 
317  ctx.getReturn() = sqrtf(SQR(x1 - x2) + SQR(y1 - y2) + SQR(z1 - z2));
318 }
319 #undef SQR
320 
322  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
323 
325  if (!campaign)
326  return;
327 
328  const Common::UString &tag = ctx.getParams()[0].getString();
329  if (tag.empty())
330  return;
331 
332  int nth = ctx.getParams()[1].getInt();
333 
335  while (nth-- > 0)
336  search->next();
337 
338  ctx.getReturn() = search->get();
339 }
340 
343  if (!campaign)
344  return;
345 
347  if (!target)
348  return;
349 
350  // Bitfield of type(s) to check for
351  const uint32 type = ctx.getParams()[1].getInt();
352  // We want the nth nearest object
353  size_t count = MAX<int32>(ctx.getParams()[2].getInt(), 0);
354 
355  if (count == 0)
356  return;
357 
359 
360  // TODO: nCheckLiving
361  // TODO: nCheckPerceived
362 
363  const bool includeSelf = ctx.getParams()[5].getInt() != 0;
364  if (includeSelf) {
365  result.push_back(boost::make_shared<Aurora::NWScript::Variable>(target));
366  count--;
367  }
368 
369  if (count == 0)
370  return;
371 
373  Aurora::NWScript::Object *object = 0;
374 
375  std::list<Object *> objects;
376  while ((object = search->next())) {
377  // Needs to be a valid object and not the target
379  if (!daObject || (daObject == target))
380  continue;
381 
382  // Ignore invalid object types
383  const uint32 objectType = (uint32) daObject->getType();
384  if (objectType >= kObjectTypeMAX)
385  continue;
386 
387  if (type & objectType)
388  objects.push_back(daObject);
389  }
390 
391  objects.sort(ObjectDistanceSort(*target));
392 
393  for (std::list<Object *>::iterator it = objects.begin(); it != objects.end() && count > 0; ++it, count--)
394  result.push_back(boost::make_shared<Aurora::NWScript::Variable>(*it));
395 }
396 
399  if (!campaign)
400  return;
401 
403  if (!target)
404  return;
405 
406  const Common::UString &tag = ctx.getParams()[1].getString();
407  if (tag.empty())
408  return;
409 
410  // Bitfield of type(s) to check for
411  const uint32 type = ctx.getParams()[2].getInt();
412  // We want the nth nearest object
413  size_t count = MAX<int32>(ctx.getParams()[3].getInt(), 0);
414 
415  if (count == 0)
416  return;
417 
419 
420  // TODO: nCheckLiving
421  // TODO: nCheckPerceived
422 
423  const bool includeSelf = ctx.getParams()[6].getInt() != 0;
424  if (includeSelf) {
425  result.push_back(boost::make_shared<Aurora::NWScript::Variable>(target));
426  count--;
427  }
428 
429  if (count == 0)
430  return;
431 
433  Aurora::NWScript::Object *object = 0;
434 
435  std::list<Object *> objects;
436  while ((object = search->next())) {
437  // Needs to be a valid object and not the target
439  if (!daObject || (daObject == target))
440  continue;
441 
442  // Ignore invalid object types
443  const uint32 objectType = (uint32) daObject->getType();
444  if (objectType >= kObjectTypeMAX)
445  continue;
446 
447  if (type & objectType)
448  objects.push_back(daObject);
449  }
450 
451  objects.sort(ObjectDistanceSort(*target));
452 
453  for (std::list<Object *>::iterator it = objects.begin(); it != objects.end() && count > 0; ++it, count--)
454  result.push_back(boost::make_shared<Aurora::NWScript::Variable>(*it));
455 }
456 
458  ctx.getReturn() = (Aurora::NWScript::Object *) 0;
459 
461  if (!campaign)
462  return;
463 
465  if (!target)
466  return;
467 
468  const Common::UString &tag = ctx.getParams()[1].getString();
469  if (tag.empty())
470  return;
471 
472  const bool includeSelf = ctx.getParams()[2].getInt() != 0;
473  if (includeSelf && (target->getTag() == tag)) {
474  ctx.getReturn() = (Aurora::NWScript::Object *) target;
475  return;
476  }
477 
479  Aurora::NWScript::Object *object = 0;
480 
481  std::list<Object *> objects;
482  while ((object = search->next())) {
483  // Needs to be a valid object and not the target
485  if (!daObject || (daObject == target))
486  continue;
487 
488  objects.push_back(daObject);
489  }
490 
491  objects.sort(ObjectDistanceSort(*target));
492 
493  if (!objects.empty())
494  ctx.getReturn() = (Aurora::NWScript::Object *) *objects.begin();
495 }
496 
497 } // End of namespace DragonAge2
498 
499 } // End of namespace Engines
void getObjectType(Aurora::NWScript::FunctionContext &ctx)
void getLocalItemProperty(Aurora::NWScript::FunctionContext &ctx)
Campaigns & getCampaigns()
Return the campaigns context.
Definition: game.cpp:57
void getLocalPlayer(Aurora::NWScript::FunctionContext &ctx)
std::vector< boost::shared_ptr< Variable > > Array
Definition: variable.h:58
void setLocalEvent(Aurora::NWScript::FunctionContext &ctx)
void getObjectByTag(Aurora::NWScript::FunctionContext &ctx)
A container of Dragon Age II objects.
void getLocalLocation(Aurora::NWScript::FunctionContext &ctx)
A class holding an UTF-8 string.
Definition: ustring.h:48
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:101
UString composeString(T value)
Convert any POD integer, float/double or bool type into a string.
Definition: strutil.cpp:276
void setLocalItemProperty(Aurora::NWScript::FunctionContext &ctx)
Context of an NWScript function.
A class able to sort objects by distance to a target object.
void getLocalInt(Aurora::NWScript::FunctionContext &ctx)
void getLocalResource(Aurora::NWScript::FunctionContext &ctx)
The context handling the gameplay in Dragon Age II.
void getLocalFloat(Aurora::NWScript::FunctionContext &ctx)
Utility templates and functions for working with strings and streams.
An object in a Dragon Age II area.
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:70
A simple scoped smart pointer template.
const Common::UString & getTag() const
Definition: object.h:49
void setLocalFloat(Aurora::NWScript::FunctionContext &ctx)
Area * getCurrentArea() const
Return the area the PC is currently in.
Definition: campaign.cpp:292
void getNearestObject(Aurora::NWScript::FunctionContext &ctx)
void getLocalObject(Aurora::NWScript::FunctionContext &ctx)
EngineType * getEngineType() const
Definition: variable.cpp:328
void UT_getNearestObjectByTag(Aurora::NWScript::FunctionContext &ctx)
Object * getObject() const
Definition: variable.cpp:321
Utility templates and functions.
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 getLocalEvent(Aurora::NWScript::FunctionContext &ctx)
void setLocalInt(Aurora::NWScript::FunctionContext &ctx)
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void setName(Aurora::NWScript::FunctionContext &ctx)
void getLocalCommand(Aurora::NWScript::FunctionContext &ctx)
void getResRef(Aurora::NWScript::FunctionContext &ctx)
void setLocalEffect(Aurora::NWScript::FunctionContext &ctx)
Campaign * getCurrentCampaign() const
Return the currently running campaign.
Definition: campaigns.cpp:311
void getName(Aurora::NWScript::FunctionContext &ctx)
The context holding a Dragon Age II campaign.
void getDistanceBetween(Aurora::NWScript::FunctionContext &ctx)
"effect", "event", "location", "talent"...
Definition: types.h:43
The context managing and running the Dragon Age II campaigns.
static DragonAge2::Object * toObject(::Aurora::NWScript::Object *object)
uint32_t uint32
Definition: types.h:204
const Array & getArray() const
Definition: variable.cpp:353
Common::UString & getString()
Definition: variable.cpp:314
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:120
void getTag(Aurora::NWScript::FunctionContext &ctx)
void setLocalObject(Aurora::NWScript::FunctionContext &ctx)
void getNearestObjectByTag(Aurora::NWScript::FunctionContext &ctx)
void setLocalPlayer(Aurora::NWScript::FunctionContext &ctx)
void getLocalEffect(Aurora::NWScript::FunctionContext &ctx)
void getPosition(Aurora::NWScript::FunctionContext &ctx)
void setLocalLocation(Aurora::NWScript::FunctionContext &ctx)
Basic Dragon Age II type definitions.
void setLocalResource(Aurora::NWScript::FunctionContext &ctx)
void getArea(Aurora::NWScript::FunctionContext &ctx)
void setLocalString(Aurora::NWScript::FunctionContext &ctx)
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
Dragon Age II engine functions.
void getLocalString(Aurora::NWScript::FunctionContext &ctx)
void setLocalCommand(Aurora::NWScript::FunctionContext &ctx)
#define SQR(x)
int32_t int32
Definition: types.h:203
void isObjectValid(Aurora::NWScript::FunctionContext &ctx)