xoreos  0.0.5
functions_action.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 "src/common/error.h"
26 
27 #include "src/aurora/types.h"
28 #include "src/aurora/resman.h"
29 
33 
34 #include "src/engines/nwn/types.h"
35 #include "src/engines/nwn/game.h"
36 #include "src/engines/nwn/module.h"
38 #include "src/engines/nwn/door.h"
40 
42 
43 namespace Engines {
44 
45 namespace NWN {
46 
48  Common::UString script = ctx.getScriptName();
49  if (script.empty())
50  throw Common::Exception("Functions::assignCommand(): Script needed");
51 
52  const Aurora::NWScript::ScriptState &state = ctx.getParams()[1].getScriptState();
53 
54  _game->getModule().delayScript(script, state, getParamObject(ctx, 0), ctx.getTriggerer(), 0);
55 }
56 
58  Common::UString script = ctx.getScriptName();
59  if (script.empty())
60  throw Common::Exception("Functions::assignCommand(): Script needed");
61 
62  uint32 delay = ctx.getParams()[0].getFloat() * 1000;
63 
64  const Aurora::NWScript::ScriptState &state = ctx.getParams()[1].getScriptState();
65 
66  _game->getModule().delayScript(script, state, ctx.getCaller(), ctx.getTriggerer(), delay);
67 }
68 
70  Common::UString script = ctx.getParams()[0].getString();
71 
72  // Max resource name length is 16, and ExecuteScript should truncate accordingly
73  script.truncate(16);
74 
75  if (!ResMan.hasResource(script, Aurora::kFileTypeNCS))
76  return;
77 
78  Aurora::NWScript::Object *object = getParamObject(ctx, 1);
79  try {
80  Aurora::NWScript::NCSFile ncs(script);
81 
82  // Let the child script inherit the environment of this parent script
84  if (env)
85  ncs.setEnvironment(*env);
86 
87  ncs.run(object);
88  } catch (...) {
89  Common::exceptionDispatcherWarning("Failed ExecuteScript(\"%s\", %s)",
90  script.c_str(), Aurora::NWScript::formatTag(object).c_str());
91  }
92 }
93 
94 // TODO: These functions need to assign an action, instead of simply delaying the script a bit.
95 
97  Common::UString script = ctx.getScriptName();
98  if (script.empty())
99  throw Common::Exception("Functions::actionDoCommand(): Script needed");
100 
101  const Aurora::NWScript::ScriptState &state = ctx.getParams()[0].getScriptState();
102 
103  _game->getModule().delayScript(script, state, ctx.getCaller(), ctx.getTriggerer(), 0);
104 }
105 
108  if (door)
110 }
111 
114  if (door)
116 }
117 
120 
121  if (object)
122  object->speakString(ctx.getParams()[0].getString(), ctx.getParams()[1].getInt());
123 }
124 
128  if (!source || !target)
129  return;
130 
131  Creature *pc = NWN::ObjectContainer::toPC(target);
132  if (!pc) {
133  warning("TODO: ActionStartConversation: Non-PC target \"%s\"", target->getTag().c_str());
134  return;
135  }
136 
137  if (source->getPCSpeaker()) {
138  if (source->getPCSpeaker() != pc) {
139  Creature *otherPC = NWN::ObjectContainer::toPC(source->getPCSpeaker());
140 
141  warning("Functions::actionStartConversation(): "
142  "Object \"%s\" already in conversation with PC \"%s\"",
143  source->getTag().c_str(), otherPC ? otherPC->getName().c_str() : "");
144  return;
145  }
146  }
147 
148  Common::UString conversation = ctx.getParams()[1].getString();
149  if (conversation.empty())
150  conversation = source->getConversation();
151 
152  // TODO: privateConv
153  // bool privateConv = ctx.getParams()[2].getInt() != 0;
154 
155  const bool playHello = ctx.getParams()[2].getInt() != 0;
156 
157  _game->getModule().startConversation(conversation, *pc, *source, playHello);
158 }
159 
161  playAnimation(ctx);
162 }
163 
165  jumpToLocation(ctx);
166 }
167 
170  NWN::Location *moveTo = NWN::ObjectContainer::toLocation(ctx.getParams()[0].getEngineType());
171 
172  if (!object || !moveTo)
173  return;
174 
175  float x, y, z;
176  moveTo->getPosition(x, y, z);
177 
178  jumpTo(object, moveTo->getArea(), x, y, z);
179 
181 }
182 
184  jumpToObject(ctx);
185 }
186 
190 
191  if (!object || !moveTo)
192  return;
193 
194  float x, y, z;
195  moveTo->getPosition(x, y, z);
196 
197  jumpTo(object, moveTo->getArea(), x, y, z);
198 
200 }
201 
202 } // End of namespace NWN
203 
204 } // End of namespace Engines
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
void actionMoveToObject(Aurora::NWScript::FunctionContext &ctx)
void jumpTo(NWN::Object *object, Area *area, float x, float y, float z)
Definition: functions.cpp:129
A container of Neverwinter Nights objects.
Common::UString formatTag(const Object *object)
Construct a string with the tag of this object.
Definition: util.cpp:34
A class holding an UTF-8 string.
Definition: ustring.h:48
const Common::UString & getConversation() const
Return the object&#39;s default conversation (DLG).
Definition: object.cpp:102
void jumpToLocation(Aurora::NWScript::FunctionContext &ctx)
const Common::UString & getName() const
Return the object&#39;s name.
Definition: object.cpp:90
Context of an NWScript function.
void actionJumpToObject(Aurora::NWScript::FunctionContext &ctx)
void actionOpenDoor(Aurora::NWScript::FunctionContext &ctx)
void unimplementedFunction(Aurora::NWScript::FunctionContext &ctx)
Definition: functions.cpp:97
void truncate(const iterator &it)
Definition: ustring.cpp:343
Basic Neverwinter Nights type definitions.
static NWN::Object * toObject(::Aurora::NWScript::Object *object)
void playAnimation(Aurora::NWScript::FunctionContext &ctx)
void actionMoveToLocation(Aurora::NWScript::FunctionContext &ctx)
virtual void speakString(const Common::UString &string, uint32 volume)
Speak the specified string.
Definition: object.cpp:210
An NCS, BioWare&#39;s NWN Compile Script.
Definition: ncsfile.h:86
static Location * toLocation(Aurora::NWScript::EngineType *engineType)
void actionJumpToLocation(Aurora::NWScript::FunctionContext &ctx)
Exception that provides a stack of explanations.
Definition: error.h:36
void delayScript(const Common::UString &script, const Aurora::NWScript::ScriptState &state, Aurora::NWScript::Object *owner, Aurora::NWScript::Object *triggerer, uint32 delay)
Definition: module.cpp:756
const Common::UString & getTag() const
Definition: object.h:49
static Door * toDoor(Aurora::NWScript::Object *object)
void exceptionDispatcherWarning(const char *s,...)
Exception dispatcher that prints the exception as a warning, and adds another reason on top...
Definition: error.cpp:158
The context needed to run a Neverwinter Nights module.
Basic exceptions to throw.
static Aurora::NWScript::Object * getParamObject(const Aurora::NWScript::FunctionContext &ctx, size_t n)
Definition: functions.cpp:118
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
const Variable & run(Object *owner=0, Object *triggerer=0)
Run the current script, from start to finish.
Definition: ncsfile.cpp:350
void actionPlayAnimation(Aurora::NWScript::FunctionContext &ctx)
void delayCommand(Aurora::NWScript::FunctionContext &ctx)
void assignCommand(Aurora::NWScript::FunctionContext &ctx)
static Creature * toPC(Aurora::NWScript::Object *object)
bool open(Object *opener)
The opener object opens this door.
Definition: door.cpp:208
bool close(Object *closer)
The closer object closes this door.
Definition: door.cpp:235
const Common::UString & getScriptName() const
Aurora::NWScript::Object * getPCSpeaker() const
Return the PC currently speaking with this object.
Definition: object.cpp:128
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void actionStartConversation(Aurora::NWScript::FunctionContext &ctx)
VariableContainer * getCurrentEnvironment() const
void warning(const char *s,...)
Definition: util.cpp:33
Handling BioWare&#39;s NWN Compiled Scripts.
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
Script, NWScript bytecode.
Definition: types.h:79
void getPosition(float &x, float &y, float &z) const
Return the location&#39;s position.
Definition: location.cpp:52
uint32_t uint32
Definition: types.h:204
A creature in a Neverwinter Nights area.
bool startConversation(const Common::UString &conv, Creature &pc, NWN::Object &obj, bool playHello=true)
Start a conversation.
Definition: module.cpp:683
The context handling the gameplay in Neverwinter Nights.
void jumpToObject(Aurora::NWScript::FunctionContext &ctx)
A door in a Neverwinter Nights area.
void actionSpeakString(Aurora::NWScript::FunctionContext &ctx)
NWScript utility functions.
Neverwinter Nights engine functions.
Module & getModule()
Return the module context.
Definition: game.cpp:70
void actionDoCommand(Aurora::NWScript::FunctionContext &ctx)
void actionCloseDoor(Aurora::NWScript::FunctionContext &ctx)
void executeScript(Aurora::NWScript::FunctionContext &ctx)
The global resource manager for Aurora resources.
void setEnvironment(const VariableContainer &env)
Overwrite the environment.
Definition: ncsfile.cpp:302