/
ScriptedConversation.cpp
473 lines (336 loc) · 12.2 KB
/
ScriptedConversation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright 2011-2016 Arx Libertatis Team (see the AUTHORS file)
*
* This file is part of Arx Libertatis.
*
* Arx Libertatis is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arx Libertatis is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Arx Libertatis. If not, see <http://www.gnu.org/licenses/>.
*/
/* Based on:
===========================================================================
ARX FATALIS GPL Source Code
Copyright (C) 1999-2010 Arkane Studios SA, a ZeniMax Media company.
This file is part of the Arx Fatalis GPL Source Code ('Arx Fatalis Source Code').
Arx Fatalis Source Code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Arx Fatalis Source Code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Arx Fatalis Source Code. If not, see
<http://www.gnu.org/licenses/>.
In addition, the Arx Fatalis Source Code is also subject to certain additional terms. You should have received a copy of these
additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Arx
Fatalis Source Code. If not, please request a copy in writing from Arkane Studios at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing Arkane Studios, c/o
ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "script/ScriptedConversation.h"
#include <sstream>
#include "core/Localisation.h"
#include "graphics/Math.h"
#include "game/Entity.h"
#include "game/EntityManager.h"
#include "game/NPC.h"
#include "gui/Speech.h"
#include "gui/Interface.h"
#include "io/resource/ResourcePath.h"
#include "scene/Interactive.h"
#include "scene/GameSound.h"
#include "script/ScriptUtils.h"
extern Vec3f LASTCAMPOS;
extern Anglef LASTCAMANGLE;
namespace script {
namespace {
class ConversationCommand : public Command {
public:
ConversationCommand() : Command("conversation") { }
Result execute(Context & context) {
std::string param = context.getWord();
if(param != "off") {
LogWarning << "Invalid used of stubbed conversation command";
}
return Success;
}
};
class PlayCommand : public Command {
public:
PlayCommand() : Command("play", AnyEntity) { }
Result execute(Context & context) {
bool unique = false;
SoundLoopMode loop = ARX_SOUND_PLAY_ONCE;
float pitch = 1.f;
bool stop = false;
bool no_pos = false;
HandleFlags("ilpso") {
unique = test_flag(flg, 'i');
if(flg & flag('l')) {
loop = ARX_SOUND_PLAY_LOOPED;
}
if(flg & flag('p')) {
pitch = Random::getf(0.9f, 1.1f);
}
stop = test_flag(flg, 's');
no_pos = test_flag(flg, 'o');
}
res::path sample = res::path::load(context.getStringVar(context.getWord())).set_ext("wav");
DebugScript(' ' << options << ' ' << sample);
Entity * io = context.getEntity();
if(stop) {
ARX_SOUND_Stop(io->sound);
io->sound = audio::INVALID_ID;
} else {
if(unique && io->sound != audio::INVALID_ID) {
ARX_SOUND_Stop(io->sound);
}
audio::SampleId num;
// TODO(broken-scripts) should be a flag instead of depending on the event
if(no_pos || SM_INVENTORYUSE == context.getMessage()) {
num = ARX_SOUND_PlayScript(sample, NULL, pitch, loop);
} else {
num = ARX_SOUND_PlayScript(sample, io, pitch, loop);
}
if(unique) {
io->sound = (num == ARX_SOUND_TOO_FAR) ? audio::INVALID_ID : num;
}
if(num == audio::INVALID_ID) {
ScriptWarning << "unable to load sound file " << sample;
return Failed;
}
}
return Success;
}
};
class PlaySpeechCommand : public Command {
public:
PlaySpeechCommand() : Command("playspeech") { }
Result execute(Context & context) {
res::path sample = res::path::load(context.getWord());
DebugScript(' ' << sample);
Entity * io = context.getEntity();
audio::SampleId num = ARX_SOUND_PlaySpeech(sample, io && io->show == 1 ? io : NULL);
if(num == audio::INVALID_ID) {
ScriptWarning << "unable to load sound file " << sample;
return Failed;
}
return Success;
}
};
class HeroSayCommand : public Command {
public:
HeroSayCommand() : Command("herosay") { }
Result execute(Context & context) {
HandleFlags("d") {
if((flg & flag('d'))) {
context.skipWord();
return Success;
}
}
std::string text = context.getStringVar(context.getWord());
DebugScript(' ' << options << " \"" << text << '"');
if(!text.empty() && text[0] == '[') {
text = getLocalised(loadUnlocalized(text), "Not Found");
}
ARX_SPEECH_Add(text);
return Success;
}
Result peek(Context & context) {
/*
* TODO Technically this command has a side effect so it should abort non-destructive execution.
* However, the on combine event for books unconditionally uses this command. resulting
* in all books lighting up in the inventory when an item is selected for combining.
*/
(void)context.getFlags();
context.skipWord();
return Success;
}
};
class SetSpeakPitchCommand : public Command {
public:
SetSpeakPitchCommand() : Command("setspeakpitch", IO_NPC) { }
Result execute(Context & context) {
float pitch = context.getFloat();
if(pitch < .6f) {
pitch = .6f;
}
DebugScript(' ' << pitch);
context.getEntity()->_npcdata->speakpitch = pitch;
return Success;
}
};
class SpeakCommand : public Command {
static void computeACSPos(CinematicSpeech & acs, Entity * io, EntityHandle ionum) {
if(io) {
ActionPoint id = io->obj->fastaccess.view_attach;
if(id != ActionPoint()) {
acs.pos1 = actionPointPosition(io->obj, id);
} else {
acs.pos1 = io->pos + Vec3f(0.f, io->physics.cyl.height, 0.f);
}
}
if(ValidIONum(ionum)) {
Entity * ioo = entities[ionum];
ActionPoint id = ioo->obj->fastaccess.view_attach;
if(id != ActionPoint()) {
acs.pos2 = actionPointPosition(ioo->obj, id);
} else {
acs.pos2 = ioo->pos + Vec3f(0.f, ioo->physics.cyl.height, 0.f);
}
}
}
static void parseParams(CinematicSpeech & acs, Context & context, Entity * speaker) {
std::string target = context.getWord();
Entity * t = entities.getById(target, context.getEntity());
acs.ionum = (t == NULL) ? EntityHandle() : t->index();
acs.startpos = context.getFloat();
acs.endpos = context.getFloat();
computeACSPos(acs, speaker, acs.ionum);
}
void parseCinematicSpeech(CinematicSpeech & acs, Context & context, Entity * speaker) {
std::string command = context.getWord();
if(command == "keep") {
acs.type = ARX_CINE_SPEECH_KEEP;
acs.pos1 = LASTCAMPOS;
acs.pos2.x = LASTCAMANGLE.getPitch();
acs.pos2.y = LASTCAMANGLE.getYaw();
acs.pos2.z = LASTCAMANGLE.getRoll();
} else if(command == "zoom") {
acs.type = ARX_CINE_SPEECH_ZOOM;
acs.startangle.setPitch(context.getFloat());
acs.startangle.setYaw(context.getFloat());
acs.endangle.setPitch(context.getFloat());
acs.endangle.setYaw(context.getFloat());
acs.startpos = context.getFloat();
acs.endpos = context.getFloat();
acs.ionum = context.getEntity()->index();
computeACSPos(acs, speaker, acs.ionum);
} else if(command == "ccctalker_l" || command == "ccctalker_r") {
acs.type = (command == "ccctalker_r") ? ARX_CINE_SPEECH_CCCTALKER_R : ARX_CINE_SPEECH_CCCTALKER_L;
parseParams(acs, context, speaker);
} else if(command == "ccclistener_l" || command == "ccclistener_r") {
acs.type = (command == "ccclistener_r") ? ARX_CINE_SPEECH_CCCLISTENER_R : ARX_CINE_SPEECH_CCCLISTENER_L;
parseParams(acs, context, speaker);
} else if(command == "side" || command == "side_l" || command == "side_r") {
acs.type = (command == "side_l") ? ARX_CINE_SPEECH_SIDE_LEFT : ARX_CINE_SPEECH_SIDE;
parseParams(acs, context, speaker);
acs.m_startdist = context.getFloat(); // startdist
acs.m_enddist = context.getFloat(); // enddist
acs.m_heightModifier = context.getFloat(); // height modifier
} else {
ScriptWarning << "unexpected command: " << command;
}
}
public:
SpeakCommand() : Command("speak") { }
Result execute(Context & context) {
CinematicSpeech acs;
acs.type = ARX_CINE_SPEECH_NONE;
Entity * speaker = context.getEntity();
bool unbreakable = false;
SpeechFlags voixoff = 0;
AnimationNumber mood = ANIM_TALK_NEUTRAL;
HandleFlags("tuphaoc") {
voixoff |= (flg & flag('t')) ? ARX_SPEECH_FLAG_NOTEXT : SpeechFlags(0);
unbreakable = test_flag(flg, 'u');
if(flg & flag('p')) {
speaker = entities.player();
}
if(flg & flag('h')) {
mood = ANIM_TALK_HAPPY;
}
if(flg & flag('a')) {
mood = ANIM_TALK_ANGRY;
}
// Crash when we set speak pitch to 1,
// Variable use for a division, 0 is not possible
voixoff |= (flg & flag('o')) ? ARX_SPEECH_FLAG_OFFVOICE : SpeechFlags(0);
if(flg & flag('c')) {
parseCinematicSpeech(acs, context, speaker);
}
}
std::string text = context.getWord();
if(text == "killall") {
if(!options.empty()) {
ScriptWarning << "unexpected options: " << options << " killall";
}
ARX_SPEECH_Reset();
return Success;
}
std::string data = loadUnlocalized(context.getStringVar(text));
DebugScript(' ' << options << ' ' << data); // TODO debug more
if(data.empty()) {
ARX_SPEECH_ClearIOSpeech(context.getEntity());
return Success;
}
if(!cinematicBorder.isActive()) {
voixoff |= ARX_SPEECH_FLAG_NOTEXT;
}
long speechnum = ARX_SPEECH_AddSpeech(speaker, data, mood, voixoff);
if(speechnum < 0) {
return Failed;
}
size_t onspeechend = context.skipCommand();
if(onspeechend != size_t(-1)) {
aspeech[speechnum].scrpos = onspeechend;
aspeech[speechnum].es = context.getScript();
aspeech[speechnum].ioscript = context.getEntity();
if(unbreakable) {
aspeech[speechnum].flags |= ARX_SPEECH_FLAG_UNBREAKABLE;
}
aspeech[speechnum].cine = acs;
}
return Success;
}
Result peek(Context & context) {
HandleFlags("tuphaoc") {
if(flg & flag('c')) {
CinematicSpeech acs;
parseCinematicSpeech(acs, context, context.getEntity());
}
}
std::string text = context.getWord();
if(text == "killall") {
return Success;
}
std::string data = loadUnlocalized(context.getStringVar(text));
if(data.empty()) {
return Success;
}
std::string command = context.getCommand(false);
size_t onspeechend = context.skipCommand();
if((!command.empty() && command != "nop") || onspeechend != size_t(-1)) {
LogWarning << onspeechend;
return AbortDestructive;
}
return Success;
}
};
class SetStrikeSpeechCommand : public Command {
public:
SetStrikeSpeechCommand() : Command("setstrikespeech", AnyEntity) { }
Result execute(Context & context) {
context.getEntity()->strikespeech = script::loadUnlocalized(context.getWord());
DebugScript(' ' << context.getEntity()->strikespeech);
return Success;
}
};
} // anonymous namespace
void setupScriptedConversation() {
ScriptEvent::registerCommand(new ConversationCommand);
ScriptEvent::registerCommand(new PlayCommand);
ScriptEvent::registerCommand(new PlaySpeechCommand);
ScriptEvent::registerCommand(new HeroSayCommand);
ScriptEvent::registerCommand(new SetSpeakPitchCommand);
ScriptEvent::registerCommand(new SpeakCommand);
ScriptEvent::registerCommand(new SetStrikeSpeechCommand);
}
} // namespace script