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 
41 
43 
45 
46 namespace Engines {
47 
48 namespace KotOR2 {
49 
50 Functions::Functions(Game &game) : _game(&game) {
52 }
53 
55  FunctionMan.clear();
56 }
57 
61 
62  for (size_t i = 0; i < ARRAYSIZE(kFunctionPointers); i++) {
63  const FunctionPointer &fPtr = kFunctionPointers[i];
64  const FunctionSignature &fSig = kFunctionSignatures[i];
65  const FunctionDefaults &fDef = kFunctionDefaults[i];
66 
67  const uint32 id = fPtr.id;
68 
69  assert((fSig.id == id) && (fDef.id == id));
70 
72  signature.push_back(fSig.returnType);
73  for (size_t j = 0; j < ARRAYSIZE(fSig.parameters); j++) {
74  if (fSig.parameters[j] == kTypeVoid)
75  break;
76 
77  signature.push_back(fSig.parameters[j]);
78  }
79 
81  for (size_t j = 0; j < ARRAYSIZE(fDef.defaults); j++) {
82  if (!fDef.defaults[j])
83  break;
84 
85  defaults.push_back(Aurora::NWScript::Variable(*fDef.defaults[j]));
86  }
87 
88  const funcPtr f = fPtr.func ? fPtr.func : &Functions::unimplementedFunction;
89 
90  FunctionMan.registerFunction(fPtr.name, id, boost::bind(f, this, _1), signature, defaults);
91  }
92 }
93 
95  warning("TODO: %s %s(%s)", Aurora::NWScript::formatType(ctx.getReturn().getType()).c_str(),
97 }
98 
99 int32 Functions::getRandom(int min, int max, int32 n) {
100  if (n < 1)
101  n = 1;
102 
103  int32 r = 0;
104 
105  while (n-- > 0)
106  r += std::rand() % (max - min + 1) + min;
107 
108  return r;
109 }
110 
111 Common::UString Functions::formatFloat(float f, int width, int decimals) {
112  return Common::UString::format("%*.*f", width, decimals, f);
113 }
114 
116  KotOR2::Object *object = KotOR2::ObjectContainer::toObject(ctx.getParams()[n].getObject());
117  if (!object || (object->getType() == kObjectTypeInvalid))
118  return 0;
119 
120  if (object->getType() == kObjectTypeSelf)
121  return ctx.getCaller();
122 
123  return object;
124 }
125 
126 void Functions::jumpTo(KotOR2::Object *object, float x, float y, float z) {
127  object->setPosition(x, y, z);
128 }
129 
130 } // End of namespace KotOR2
131 
132 } // End of namespace Engines
static Common::UString formatFloat(float f, int width=18, int decimals=9)
Definition: functions.cpp:111
void jumpTo(KotOR2::Object *object, float x, float y, float z)
Definition: functions.cpp:126
Aurora::NWScript::Type parameters[15]
Definition: functions.h:62
void unimplementedFunction(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.cpp:94
const Aurora::NWScript::Variable * defaults[14]
Definition: functions.h:67
std::vector< class Variable > Parameters
Definition: types.h:54
A class holding an UTF-8 string.
Definition: ustring.h:48
The context handling the gameplay in Star Wars: Knights of the Old Republic II - The Sith Lords...
Common::UString formatType(Type type)
Construct a string describing this variable type.
Definition: util.cpp:130
A container of Star Wars: Knights of the Old Republic II - The Sith Lords objects.
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
The context holding a Star Wars: Knights of the Old Republic II - The Sith Lords area.
static const FunctionSignature kFunctionSignatures[]
The table defining the signature (return type and type of parameters) of each engine function...
Definition: functions.h:71
The NWScript function manager.
static int32 getRandom(int min, int max, int32 n=1)
Definition: functions.cpp:99
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
An object in a Star Wars: Knights of the Old Republic II - The Sith Lords area.
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.
Basic Star Wars: Knights of the Old Republic II - The Sith Lords type definitions.
Star Wars: Knights of the Old Republic II - The Sith Lords engine functions.
Fake value to describe the calling object in a script.
Definition: types.h:52
std::vector< Type > Signature
Definition: types.h:52
const Common::UString & getName() const
void warning(const char *s,...)
Definition: util.cpp:33
The context needed to run a Star Wars: Knights of the Old Republic II - The Sith Lords module...
Tables defining the engine functions in Star Wars: Knights of the Old Republic II - The Sith Lords...
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:115
#define FunctionMan
Definition: functionman.h:84
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:61
static const FunctionDefaults kFunctionDefaults[]
The table defining the default values for the parameters of each engine function. ...
Definition: functions.h:72
static KotOR2::Object * toObject(::Aurora::NWScript::Object *object)
uint32_t uint32
Definition: types.h:204
void(Functions::* funcPtr)(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.h:51
Common::UString formatParams(const FunctionContext &ctx)
Construct a string describing parameters of this function.
Definition: util.cpp:109
static const FunctionPointer kFunctionPointers[]
The table defining the name and function pointer of each engine function.
Definition: functions.h:70
NWScript utility functions.
int32_t int32
Definition: types.h:203