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