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