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 
37 #include "src/graphics/graphics.h"
38 
46 
49 
50 namespace Engines {
51 
52 namespace Witcher {
53 
54 Functions::Functions(Game &game) : _game(&game) {
56 }
57 
59  FunctionMan.clear();
60 }
61 
65 
66  for (size_t i = 0; i < ARRAYSIZE(kFunctionPointers); i++) {
67  const FunctionPointer &fPtr = kFunctionPointers[i];
68  const FunctionSignature &fSig = kFunctionSignatures[i];
69  const FunctionDefaults &fDef = kFunctionDefaults[i];
70 
71  const uint32 id = fPtr.id;
72 
73  assert((fSig.id == id) && (fDef.id == id));
74 
76  signature.push_back(fSig.returnType);
77  for (size_t j = 0; j < ARRAYSIZE(fSig.parameters); j++) {
78  if (fSig.parameters[j] == kTypeVoid)
79  break;
80 
81  signature.push_back(fSig.parameters[j]);
82  }
83 
85  for (size_t j = 0; j < ARRAYSIZE(fDef.defaults); j++) {
86  if (!fDef.defaults[j])
87  break;
88 
89  defaults.push_back(Aurora::NWScript::Variable(*fDef.defaults[j]));
90  }
91 
92  const funcPtr f = fPtr.func ? fPtr.func : &Functions::unimplementedFunction;
93 
94  FunctionMan.registerFunction(fPtr.name, id, boost::bind(f, this, _1), signature, defaults);
95  }
96 }
97 
99  warning("TODO: %s %s(%s)", Aurora::NWScript::formatType(ctx.getReturn().getType()).c_str(),
101 }
102 
104  const Common::UString code = ctx.getParams()[0].getString();
105  LuaScriptMan.executeString(code);
106 }
107 
108 int32 Functions::getRandom(int min, int max, int32 n) {
109  if (n < 1)
110  n = 1;
111 
112  int32 r = 0;
113 
114  while (n-- > 0)
115  r += std::rand() % (max - min + 1) + min;
116 
117  return r;
118 }
119 
120 Common::UString Functions::formatFloat(float f, int width, int decimals) {
121  return Common::UString::format("%*.*f", width, decimals, f);
122 }
123 
125  Witcher::Object *object = Witcher::ObjectContainer::toObject(ctx.getParams()[n].getObject());
126  if (!object || (object->getType() == kObjectTypeInvalid))
127  return 0;
128 
129  if (object->getType() == kObjectTypeSelf)
130  return ctx.getCaller();
131 
132  return object;
133 }
134 
135 void Functions::jumpTo(Witcher::Object *object, Area *area, float x, float y, float z) {
136  // Sanity check
137  if (!object->getArea() || !area) {
138  warning("Functions::jumpTo(): No area?!? (%d, %d)", object->getArea() != 0, area != 0);
139  return;
140  }
141 
142  GfxMan.lockFrame();
143 
144  // Are we moving between areas?
145  if (object->getArea() != area) {
146  const Common::UString &areaFrom = object->getArea()->getResRef();
147  const Common::UString &areaTo = area->getResRef();
148 
149  warning("TODO: Functions::jumpTo(): Moving from \"%s\" to \"%s\"", areaFrom.c_str(), areaTo.c_str());
150 
152  if (pc) {
153  const Common::UString &pcArea = pc->getArea()->getResRef();
154 
155  if (areaFrom == pcArea) {
156  // Moving away from the currently visible area.
157 
158  object->hide();
159  object->unloadModel();
160 
161  } else if (areaTo == pcArea) {
162  // Moving into the currently visible area.
163 
164  object->loadModel();
165  object->show();
166  }
167 
168  }
169 
170  object->setArea(area);
171  }
172 
173  // Update position
174  object->setPosition(x, y, z);
175 
176  GfxMan.unlockFrame();
177 
178  if (object == _game->getModule().getPC())
179  _game->getModule().movedPC();
180 }
181 
182 } // End of namespace Witcher
183 
184 } // End of namespace Engines
static int32 getRandom(int min, int max, int32 n=1)
Definition: functions.cpp:108
ObjectType getType() const
Return the exact type of the object.
Definition: object.cpp:61
A container of The Witcher objects.
Area * getArea() const
Return the area this object is currently in.
Definition: object.cpp:112
void(Functions::* funcPtr)(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.h:51
The global graphics manager.
An area in The Witcher, holding all objects and area geometry within, as well as general area propert...
Definition: area.h:63
std::vector< class Variable > Parameters
Definition: types.h:54
A class holding an UTF-8 string.
Definition: ustring.h:48
Fake value to describe the calling object in a script.
Definition: types.h:54
An object within a Witcher area.
Definition: object.h:51
Common::UString formatType(Type type)
Construct a string describing this variable type.
Definition: util.cpp:130
Tables defining the engine functions in The Witcher.
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
Basic The Witcher type definitions.
const Common::UString & getResRef() const
Return the area&#39;s resref (resource ID).
Definition: area.cpp:111
const Aurora::NWScript::Variable * defaults[11]
Definition: functions.h:67
void jumpTo(Witcher::Object *object, Area *area, float x, float y, float z)
Definition: functions.cpp:135
The NWScript function manager.
Module & getModule()
Return the module context.
Definition: game.cpp:67
static const FunctionDefaults kFunctionDefaults[]
The table defining the default values for the parameters of each engine function. ...
Definition: functions.h:72
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:124
A creature in a The Witcher area.
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.
An object in a The Witcher area.
void unimplementedFunction(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.cpp:98
static const FunctionSignature kFunctionSignatures[]
The table defining the signature (return type and type of parameters) of each engine function...
Definition: functions.h:71
std::vector< Type > Signature
Definition: types.h:52
const Common::UString & getName() const
void runClientLua(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.cpp:103
void warning(const char *s,...)
Definition: util.cpp:33
void movedPC()
Notify the module that the PC was moved.
Definition: module.cpp:472
#define FunctionMan
Definition: functionman.h:84
static const FunctionPointer kFunctionPointers[]
The table defining the name and function pointer of each engine function.
Definition: functions.h:70
static Common::UString formatFloat(float f, int width=18, int decimals=9)
Definition: functions.cpp:120
uint32_t uint32
Definition: types.h:204
The context needed to run a The Witcher module.
The Witcher engine functions.
Lua script manager.
The context holding a The Witcher area.
Common::UString formatParams(const FunctionContext &ctx)
Construct a string describing parameters of this function.
Definition: util.cpp:109
The context handling the gameplay in The Witcher.
#define LuaScriptMan
Shortcut for accessing the script engine.
Definition: scriptman.h:157
NWScript utility functions.
Creature * getPC()
Return the currently playing PC.
Definition: module.cpp:79
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
static Witcher::Object * toObject(::Aurora::NWScript::Object *object)
Aurora::NWScript::Type parameters[12]
Definition: functions.h:62
int32_t int32
Definition: types.h:203