xoreos  0.0.5
functions.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 <cassert>
26 #include <cstdlib>
27 
28 #include <boost/bind.hpp>
29 
30 #include "src/common/util.h"
31 
34 
39 
41 
43 
44 namespace Engines {
45 
46 namespace DragonAge2 {
47 
48 Functions::Functions(Game &game) : _game(&game) {
50 }
51 
53  FunctionMan.clear();
54 }
55 
59 
60  for (size_t i = 0; i < ARRAYSIZE(kFunctionPointers); i++) {
61  const FunctionPointer &fPtr = kFunctionPointers[i];
62  const FunctionSignature &fSig = kFunctionSignatures[i];
63  const FunctionDefaults &fDef = kFunctionDefaults[i];
64 
65  const uint32 id = fPtr.id;
66 
67  assert((fSig.id == id) && (fDef.id == id));
68 
70  signature.push_back(fSig.returnType);
71  for (size_t j = 0; j < ARRAYSIZE(fSig.parameters); j++) {
72  if (fSig.parameters[j] == kTypeVoid)
73  break;
74 
75  signature.push_back(fSig.parameters[j]);
76  }
77 
79  for (size_t j = 0; j < ARRAYSIZE(fDef.defaults); j++) {
80  if (!fDef.defaults[j])
81  break;
82 
83  defaults.push_back(Aurora::NWScript::Variable(*fDef.defaults[j]));
84  }
85 
86  const funcPtr f = fPtr.func ? fPtr.func : &Functions::unimplementedFunction;
87 
88  FunctionMan.registerFunction(fPtr.name, id, boost::bind(f, this, _1), signature, defaults);
89  }
90 }
91 
93  warning("TODO: %s %s(%s)", Aurora::NWScript::formatType(ctx.getReturn().getType()).c_str(),
95 }
96 
97 Common::UString Functions::formatFloat(float f, int width, int decimals) {
98  return Common::UString::format("%*.*f", width, decimals, f);
99 }
100 
103  if (!object || (object->getType() == kObjectTypeInvalid))
104  return 0;
105 
106  if (object->getType() == kObjectTypeSelf)
107  return ctx.getCaller();
108 
109  return object;
110 }
111 
112 void Functions::jumpTo(DragonAge2::Object *object, float x, float y, float z) {
113  object->setPosition(x, y, z);
114 }
115 
116 } // End of namespace DragonAge2
117 
118 } // End of namespace Engines
A container of Dragon Age II objects.
std::vector< class Variable > Parameters
Definition: types.h:54
A class holding an UTF-8 string.
Definition: ustring.h:48
Common::UString formatType(Type type)
Construct a string describing this variable type.
Definition: util.cpp:130
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:101
The context handling the gameplay in Dragon Age II.
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
void jumpTo(DragonAge2::Object *object, float x, float y, float z)
Definition: functions.cpp:112
const Aurora::NWScript::Variable * defaults[7]
Definition: functions.h:66
An object in a Dragon Age II area.
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:70
The NWScript function manager.
static const FunctionSignature kFunctionSignatures[]
The table defining the signature (return type and type of parameters) of each engine function...
Definition: functions.h:70
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
Utility templates and functions.
Fake value to describe the calling object in a script.
Definition: types.h:84
std::vector< Type > Signature
Definition: types.h:52
const Common::UString & getName() const
void(Functions::* funcPtr)(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.h:50
void warning(const char *s,...)
Definition: util.cpp:33
#define FunctionMan
Definition: functionman.h:84
static DragonAge2::Object * toObject(::Aurora::NWScript::Object *object)
uint32_t uint32
Definition: types.h:204
static const FunctionDefaults kFunctionDefaults[]
The table defining the default values for the parameters of each engine function. ...
Definition: functions.h:71
Common::UString formatParams(const FunctionContext &ctx)
Construct a string describing parameters of this function.
Definition: util.cpp:109
static Common::UString formatFloat(float f, int width=18, int decimals=9)
Definition: functions.cpp:97
static const FunctionPointer kFunctionPointers[]
The table defining the name and function pointer of each engine function.
Definition: functions.h:69
Basic Dragon Age II type definitions.
void unimplementedFunction(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.cpp:92
NWScript utility functions.
Dragon Age II engine functions.
Tables defining the engine functions in Dragon Age II.