forked from nuvie/nuvie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverseInterpret.cpp
1623 lines (1505 loc) · 49.6 KB
/
ConverseInterpret.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Created by Joseph Applegate
* Copyright (C) 2003 The Nuvie Team
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <ctype.h>
#include "nuvieDefs.h"
#include "U6misc.h"
#include "Player.h"
#include "Party.h"
#include "U6UseCode.h"
#include "ActorManager.h"
#include "GameClock.h"
#include "Map.h"
#include "MapWindow.h"
#include "Effect.h"
#include "Script.h"
#include "ConverseInterpret.h"
#include "ConverseSpeech.h"
//#define CONVERSE_DEBUG
ConverseInterpret::ConverseInterpret(Converse *owner)
{
converse = owner;
b_frame = NULL;
decl_v = decl_t = 0x00;
in_start = 0;
unwait();
answer_mode = ANSWER_NO;
stopped = false;
db_lvar = false;
db_loc = 0;
db_offset = 0;
}
ConverseInterpret::~ConverseInterpret()
{
leave_all(); // deletes b_frame when empty
}
/* Returns a value from the current location in the ConvScript, respecting any
* present data-size definition.
*/
struct ConverseInterpret::in_val_s ConverseInterpret::read_value()
{
ConvScript *cs = converse->script;
converse_value nval = cs->read();
uint8 dtype = nval;
if(nval == 0xd3)
nval = cs->read();
else if(nval == 0xd2)
nval = cs->read4();
else if(nval == 0xd4)
nval = cs->read2();
else
dtype = 0;
struct in_val_s ival;
ival.v = nval;
ival.d = dtype;
return(ival);
}
/* Read a control statement from the script.
*/
void ConverseInterpret::collect_input()
{
ConvScript *cs = converse->script;
converse_value cval = cs->peek();
// special cases
if(cval == U6OP_JUMP) // 0xb0 (location)
{
add_val(cs->read()); // OPC
add_val(cs->read4());
return;
}
else if(cval == U6OP_SIDENT) // 0xff, get NPC identity
{
add_val(cs->read()); // OPC
add_val(cs->read()); // number
add_text(); // name
return;
}
else if(cval == U6OP_KEYWORDS // 0xef, get keyword list
|| cval == U6OP_ASKC // ...0xf8, or characters allowed as input
|| cval == U6OP_SLOOK) // ...0xf1, or description
{
add_val(cs->read()); // OPC
add_text();
return;
}
// standard
uint32 arg_num = 1; // which arg. of control statement will be evaluated
do
{
if(is_print(cs->peek()) && (cs->overflow(1) || !is_valop(cs->peek(1))))
break;
if(cs->peek() == U6OP_EVAL) // 0xa7
{
cs->skip();
// HACK forced by this input-collection design, don't eval if not
// running (but this was supposed to be checked only in exec())
if(!top_frame() || top_frame()->run) // "no frame" == run
eval(arg_num++);
}
else
{
struct in_val_s nval = read_value();
add_val(nval.v, nval.d);
}
} while(!cs->overflow() && !is_ctrl(cs->peek()));
}
/* Interpret control codes and text from the ConvScript.
*/
void ConverseInterpret::step()
{
ConvScript *cs = converse->script;
flush();
while(!waiting() && !cs->overflow() && !converse->is_waiting_for_scroll())
{
if(is_print(cs->peek()))
{
add_text(); // collect
converse->set_output(get_text());
}
else if(is_ctrl(cs->peek()))
{
in_start = cs->pos();
collect_input(); // get opcode + data (1 statement/instruction)
}
else
{
#ifdef CONVERSE_DEBUG
DEBUG(1,LEVEL_DEBUGGING, "Converse: skipped 0x%02x at %04x\n", cs->peek(), cs->pos());
#endif
converse->print("[Tried to print a control char.]\n");
cs->skip();
}
exec(); // run it
}
if(cs->overflow() && !stopped)
{
converse->print("\n[EOF]\n");
stop();
}
}
/* Collect text from script and add to end of collected text statement, or add
* one character to text.
*/
void ConverseInterpret::add_text(unsigned char c)
{
ConvScript *cs = converse->script;
do
{
text.append(1,(unsigned char)cs->read());
} while(!cs->overflow() && is_print(cs->peek()));
}
/* Add some code or data to end of collected control statement.
*/
void ConverseInterpret::add_val(converse_value c, uint8 d)
{
struct in_val_s ivs;
ivs.v = c;
ivs.d = d;
in.push_back(ivs);
}
/* Delete the top-level frame. If no frames are left, run==true for all code.
*/
void ConverseInterpret::leave()
{
if(top_frame())
{
struct convi_frame_s *fp = b_frame->top();
#ifdef CONVERSE_DEBUG
DEBUG(1,LEVEL_DEBUGGING,"Converse: ...leave %02x...\n", fp->start_c);
#endif
delete fp; fp = NULL;
b_frame->pop();
if(b_frame->empty())
{
delete b_frame; b_frame = NULL;
}
}
}
/* Create new top-level frame for a code block starting at control code `c'.
*/
void ConverseInterpret::enter(converse_value c)
{
struct convi_frame_s *ef = new convi_frame_s;
ef->start = in_start; // start location
ef->run = top_frame() ? top_frame()->run : true; // run if parent is
ef->break_c = 0x00;
ef->start_c = c;
if(!b_frame)
b_frame = new stack<struct convi_frame_s *>;
b_frame->push(ef);
#ifdef CONVERSE_DEBUG
DEBUG(1,LEVEL_DEBUGGING,"Converse: ...enter %02x...\n", ef->start_c);
#endif
}
/* Leave the top-frame for end-code matching `c', and enter a new frame for
* other codes. Toggle run mode if `c' is break-code.
*/
void ConverseInterpret::do_frame(converse_value c)
{
switch(c)
{
case U6OP_IF: // 0xa1
enter(U6OP_IF);
break;
case U6OP_ENDIF: // 0xa2
leave(); // (0xa1)
break;
case U6OP_KEYWORDS: // 0xef
if(!top_frame() || top_frame()->start_c != U6OP_KEYWORDS)
enter(U6OP_KEYWORDS); // 0xef...0xef...0xee, can't be recursive
break;
case U6OP_ENDANSWER: // 0xee
leave(); // (0xef)
break;
}
if(c != 0x00 && c == get_break())
set_run(!get_run());
}
/* Run (or skip) the stored control and text statements, and clear them.
*/
void ConverseInterpret::exec()
{
do_frame(get_val(0));
if(!top_frame() || top_frame()->run) // "no frame" == run
{
if(val_count())
do_ctrl();
if(!converse->get_output().empty())
do_text();
}
#ifdef CONVERSE_DEBUG
else if(get_val(0) != 0x00) // show stepped over ctrl code (not text)
DEBUG(1,LEVEL_DEBUGGING,"Converse: %04x ----: %02x\n", in_start, get_val(0));
#endif
flush();
converse->set_output(""); // clear output
}
/* Print script text, resolving special symbols as they are encountered.
*/
void ConverseInterpret::do_text()
{
string output = get_formatted_text(converse->get_output().c_str());
converse->print(output.c_str());
}
/* Print script text, resolving special symbols as they are encountered.
*/
string ConverseInterpret::get_formatted_text(const char *c_str)
{
unsigned int i = 0;
char symbol[3] = { '\0', '\0', '\0' },
intval[16];
string output;
const uint32 len = strlen(c_str);
uint32 last_value = 0;
while(i < len)
{
switch(c_str[i])
{
case '$' :
case '#' : // copy symbol
strncpy(symbol, &c_str[i], 2);
// evaluate
if(!strcmp(symbol, "$G")) // gender title
output.append(converse->player->get_gender_title());
else if(!strcmp(symbol, "$N")) // npc name
output.append(converse->name);
else if(!strcmp(symbol, "$P")) // player name
output.append(converse->player->get_name());
else if(!strcmp(symbol, "$T")) // time of day
output.append(converse->clock->get_time_of_day_string());
else if(!strcmp(symbol, "$Y")) // Y-string
output.append(get_ystr());
else if(!strcmp(symbol, "$Z")) // previous input
output.append(converse->get_svar(U6TALK_VAR_INPUT));
else if(symbol[0] == '$' // value of a string variable
&& isdigit(symbol[1]))
output.append(converse->get_svar(strtol(&symbol[1], NULL, 10)));
else if(symbol[0] == '#' // value of a variable
&& isdigit(symbol[1]))
{
last_value = converse->get_var(strtol(&symbol[1], NULL, 10));
snprintf(intval, 16, "%u", last_value);
output.append((char *)intval);
output.append("");
}
else
output.append(symbol);
i += 2;
break;
case '{' : i++; break; //FIXME what does this FM-Towns command do?
case '~' :
if(i+3 <= len)
{
i++;
if(c_str[i] == 'P')
converse->get_speech()->play_speech(converse->script_num, (int)strtol(&c_str[i+1], NULL, 10));
for(i++;isdigit(c_str[i]) && i < len;)
i++;
}
break;
case '/' : // singular
case '\\' : // plural
{
bool show = false;
if ((last_value == 1 && c_str[i] == '/') ||
(last_value != 1 && c_str[i] == '\\')) show = true;
++i;
unsigned int count = 0;
while (i+count < len &&
c_str[i+count] >= 'a' && c_str[i+count] <= 'z')
// NOTE: the above check might not work for non-english
count++;
if (show) output.append(count,c_str[i]);
i += count;
break;
}
default :
//skip fm-towns keyword commands when in original ui mode.
// They take the following form. '+actor_numberKeyword+' eg. '+3leave+'
if(c_str[i] == '+' && !Game::get_game()->is_new_style())
{
if(i+3 <= len)
{
i++;
for(;c_str[i] != 0x2b && i < len;) //0x2b = '+'
i++;
i++;
}
}
else
{
output.append(1,c_str[i]);
i += 1;
}
break;
}
}
return output;
}
/* Execute a control statement/instruction (control code + optional arguments
* which must have been evaluated already.)
*/
void ConverseInterpret::do_ctrl()
{
stack<converse_typed_value> st;
#ifdef CONVERSE_DEBUG
DEBUG(1,LEVEL_DEBUGGING,"Converse: %04x INSTR:", in_start);
for(uint32 v = 0; v < val_count(); v++)
DEBUG(1,LEVEL_DEBUGGING," 0x%02x", get_val(v));
DEBUG(1,LEVEL_DEBUGGING,"\n");
#endif
converse_typed_value v = {U6OP_VAR, 0};
while(val_count())
{
v.val = pop_val();
st.push(v);
}
op(st);
}
/* Returns last item value from input list (and removes it from the same.)
*/
converse_value ConverseInterpret::pop_val()
{
converse_value ret = 0;
if(!in.empty())
{
ret = get_val(val_count() - 1);
in.resize(val_count() - 1);
}
return(ret);
}
/* Returns last item data-size/type from value list (and removes it from the
* same.)
*/
uint8 ConverseInterpret::pop_val_size()
{
converse_value ret = 0;
if(!in.empty())
{
ret = get_val_size(val_count() - 1);
in.resize(val_count() - 1);
}
return(ret);
}
/* Returns any item value from the input list (and leaves the list alone.)
*/
converse_value ConverseInterpret::get_val(uint32 vi)
{
if(vi < in.size())
return(in[vi].v);
return(0);
}
/* Returns any item data-size/type from the input list (and leaves the list
* alone.)
*/
uint8 ConverseInterpret::get_val_size(uint32 vi)
{
if(vi < in.size())
return(in[vi].d);
return(0);
}
void ConverseInterpret::set_ystr(const char *s)
{
ystring = s ? s : "";
converse->set_svar(U6TALK_VAR_YSTRING, ystring.c_str());
}
/* Set the return string `sn' to `s'.
*/
void ConverseInterpret::set_rstr(uint32 sn, const char *s)
{
if(sn >= rstrings.size())
rstrings.resize(rstrings.size() + 1);
rstrings[sn] = s ? s : "";
}
/* Add a return string.
* Returns the index into the list, which is used by an instruction.
*/
converse_value ConverseInterpret::add_rstr(const char *s)
{
rstrings.push_back(s ? s : "");
return(rstrings.size() - 1);
}
/* Returns and removes top item from a value stack.
*/
converse_value ConverseInterpret::pop_arg(stack<converse_typed_value> &vs)
{
converse_value ret = 0;
if(!vs.empty())
{
converse_typed_value val = vs.top();
ret = val.val;
vs.pop();
}
return(ret);
}
converse_typed_value ConverseInterpret::pop_typed_arg(stack<converse_typed_value> &vs)
{
converse_typed_value ret = {0,0};
if(!vs.empty())
{
ret = vs.top();
vs.pop();
}
return(ret);
}
#if 0
bool MDTalkInterpret::op(stack<converse_value> &i)
{
bool success = true;
converse_value v[4], in; // args
switch(in == pop_arg(i))
{
default:
i.push(in);
success = ConverseInterpret::op(i);
}
return(success);
}
#endif
bool ConverseInterpret::op_create_new(stack<converse_typed_value> &i)
{
converse_value v[4];
Actor *cnpc = NULL;
v[0] = pop_arg(i); // npc
v[1] = pop_arg(i); // obj
v[2] = pop_arg(i); // qual
v[3] = pop_arg(i); // quant
cnpc = converse->actors->get_actor(npc_num(v[0]));
if(cnpc)
{
if(Game::get_game()->get_game_type() == NUVIE_GAME_U6 && v[1] == 76) // Amulet of Submission
{
cnpc->remove_readied_object(ACTOR_NECK);
Obj *cnpc_obj = cnpc->inventory_new_object(76, 1, 0);
cnpc->add_readied_object(cnpc_obj);
}
else
cnpc->inventory_new_object(v[1], v[3], v[2]);
}
return true;
}
bool WOUConverseInterpret::op_create_new(stack<converse_typed_value> &i)
{
converse_value v[4];
Actor *cnpc = NULL;
v[0] = pop_arg(i); // npc
v[1] = pop_arg(i); // obj
v[2] = pop_arg(i); // qual
v[3] = pop_arg(i); // quant
cnpc = converse->actors->get_actor(npc_num(v[0]));
if(cnpc)
{
if(cnpc->can_carry_object(v[1], v[3]))
{
cnpc->inventory_new_object(v[1], v[3], v[2]);
converse->set_var(WOUTALK_VAR_ADD_TO_INVENTORY_FAILED, 0);
}
else
{
converse->set_var(WOUTALK_VAR_ADD_TO_INVENTORY_FAILED, 1); //This var is set to true when the object cannot be added to the actor's inventory
}
}
return true;
}
/* Run control code with arguments/operands (arranged on a stack from
* top to bottom.)
*/
bool ConverseInterpret::op(stack<converse_typed_value> &i)
{
bool success = true;
converse_value v[4] = { 0, 0, 0, 0 }; // args
converse_value in;
ConvScript *cs = converse->script;
Actor *cnpc = NULL;
Obj *cnpc_obj = NULL;
Player *player = converse->player;
// converse_db_s *cdb;
in = pop_arg(i);
#ifdef CONVERSE_DEBUG
DEBUG(1, LEVEL_DEBUGGING, "op %s(%x)\n", op_str(in), in);
#endif
switch(in)
{
case U6OP_SLEEP: // 0x9e
// Note: It's usually unecessary to wait for the effect, as it
// pauses input and the user can't continue the conversation until
// the effect is complete.
new SleepEffect(5); // sleep until sunrise
break;
case U6OP_HORSE: // 0x9c
// FIXME: probably need to do more real actor/object set-up here
cnpc = converse->actors->get_actor(npc_num(pop_arg(i)));
cnpc_obj = cnpc->make_obj();
cnpc_obj->frame_n = 0; // FIX for actors orginal direction.
cnpc_obj->obj_n = OBJ_U6_HORSE_WITH_RIDER; // mount up.
cnpc->init_from_obj(cnpc_obj);
delete_obj(cnpc_obj);
break;
case U6OP_IF: // 0xa1 (test val)
set_break(U6OP_ELSE);
set_run(pop_arg(i) ? true : false);
break;
case U6OP_ENDIF: // 0xa2
break; // (frame only)
case U6OP_ELSE: // 0xa3
break; // (frame only_
case U6OP_SETF: // 0xa4 (npc, flagnum)
cnpc = converse->actors->get_actor(npc_num(pop_arg(i)));
if(cnpc)
cnpc->set_flag(pop_arg(i));
break;
case U6OP_CLEARF: // 0xa5 (npc, flagnum)
cnpc = converse->actors->get_actor(npc_num(pop_arg(i)));
if(cnpc)
cnpc->clear_flag(pop_arg(i));
break;
case U6OP_DECL: // 0xa6
db_lvar = false;
v[0] = pop_arg(i); // variable
v[1] = pop_arg(i); // type
if(!i.empty())
{
converse_typed_value top = i.top();
if((top.val == 0xb2 || top.val == 0xb4))
{
if(top.val == 0xb2) // eg DB1[#2] = ...
{
pop_arg(i); //0xb2
pop_arg(i); //0xb4 DATA op.
db_lvar = true;
db_loc = v[0];
db_offset = converse->get_var(v[1]);
}
else if(top.val == 0xb4) // eg DB1[0] = ...
{
pop_arg(i); //0xb4 DATA op.
db_lvar = true;
db_loc = v[0];
db_offset = v[1];
}
#ifdef CONVERSE_DEBUG
DEBUG(1, LEVEL_DEBUGGING, "DB lvar found. location = %x, offset = %x", db_loc, db_offset);
#endif
}
}
let(v[0], v[1]);
break;
case U6OP_ASSIGN: // 0xa8
case U6OP_SETNAME: // 0xd8
v[0] = pop_arg(i); // value
if(in == U6OP_ASSIGN) // 0xa8
{
if(db_lvar)
{
set_db_integer(db_loc, db_offset, v[0]);
}
else
if(decl_v <= U6TALK_VAR__LAST_)
{
if(decl_t == 0xb3)
converse->set_svar(decl_v, get_rstr(v[0]));
else if(decl_t == 0xb2)
converse->set_var(decl_v, v[0]);
}
else
{
converse->print("[Illegal variable]\n");
DEBUG(0, LEVEL_ERROR, "[Illegal variable] decl_v = %x decl_t = %x\n", decl_v, decl_t);
}
}
else // // 0xd8, assign name of npc `result' to ystr
{
v[0] = npc_num(v[0]);
// get name from party information
if(player->get_party()->contains_actor(v[0]))
{
v[0] = player->get_party()->get_member_num(v[0]);
set_ystr(player->get_party()->get_actor_name(v[0]));
}
else
set_ystr(converse->npc_name(v[0])); // read from script
}
let(); // clear
break;
case U6OP_JUMP: // 0xb0
v[0] = pop_arg(i);
#ifdef CONVERSE_DEBUG
DEBUG(1,LEVEL_DEBUGGING,"Converse: JUMP 0x%04x\n", v[0]);
#endif
cs->seek(v[0]);
leave_all(); // always run
break;
case U6OP_DPRINT: // 0xb5
{
v[0] = pop_arg(i); // db location
v[1] = pop_arg(i); // index
char *dstring = get_db_string(v[0], v[1]);
if(dstring)
{
converse->set_output(dstring); // data may have special symbols
// converse->print(dstring); // data can have no special symbols -- wrong
// converse->print("\n");
free(dstring);
}
break;
}
case U6OP_BYE: // 0xb6
stop();
break;
case U6OP_NEW: // 0xb9 (npc, obj, qual, quant)
op_create_new(i);
break;
case U6OP_DELETE: // 0xba
//bool Actor::inventory_del_object(uint16 obj_n, uint8 qty, uint8 quality);
v[0] = pop_arg(i); // npc
v[1] = pop_arg(i); // obj
v[2] = pop_arg(i); // qual
v[3] = pop_arg(i); // quant
cnpc = converse->actors->get_actor(npc_num(v[0]));
if(cnpc)
cnpc->inventory_del_object(v[1], v[3], v[2]);
break;
case U6OP_GIVE: // 0xc9
v[0] = pop_arg(i); // obj
v[1] = pop_arg(i); // qual
v[2] = pop_arg(i); // from
v[3] = pop_arg(i); // to
cnpc = converse->actors->get_actor(npc_num(v[2]));
if(!cnpc)
break;
cnpc->inventory_del_object(v[0], 1, v[1]);
cnpc = converse->actors->get_actor(npc_num(v[3]));
if(cnpc)
cnpc->inventory_new_object(v[0], 1, v[1]);
break;
case U6OP_ADDKARMA: // 0xc4
player->add_karma(pop_arg(i));
break;
case U6OP_SUBKARMA: // 0xc5
player->subtract_karma(pop_arg(i));
break;
case MDOP_MISC_ACTION: // 0xd1
v[0] = pop_arg(i); // action number
Game::get_game()->get_script()->call_talk_script(v[0]);
break;
case U6OP_RESURRECT: // 0xd6
{
v[0] = npc_num(pop_arg(i));
uint16 body = OBJ_U6_DEAD_BODY;
if(Game::get_game()->get_game_type() == NUVIE_GAME_SE)
body = OBJ_SE_DEAD_BODY;
else if(Game::get_game()->get_game_type() == NUVIE_GAME_MD)
body = OBJ_MD_DEAD_BODY;
if(v[0] == 0) // Party
cnpc = converse->player->get_party()->who_has_obj(body ,0,false);
else
cnpc = converse->actors->get_actor(v[0]);
cnpc_obj = cnpc->inventory_get_object(body, 0, false);
if(Game::get_game()->get_game_type() == NUVIE_GAME_U6 && !cnpc_obj)
{
if(v[0] == 0)
cnpc = converse->player->get_party()->who_has_obj(OBJ_U6_MOUSE,0,false);
cnpc_obj = cnpc->inventory_get_object(OBJ_U6_MOUSE, 0, false);
}
if(cnpc_obj != NULL)
{
if(converse->actors->resurrect_actor(cnpc_obj, converse->player->get_actor()->get_location()))
{
converse->objects->unlink_from_engine(cnpc_obj);
delete_obj(cnpc_obj);
}
}
break;
}
case U6OP_HEAL: // 0xd9
cnpc = converse->actors->get_actor(npc_num(pop_arg(i)));
if(cnpc)
cnpc->heal();
break;
case U6OP_CURE: // 0xdb
cnpc = converse->actors->get_actor(npc_num(pop_arg(i)));
if(cnpc)
cnpc->set_poisoned(false);
break;
case U6OP_WORKTYPE: // 0xcd
v[0] = pop_arg(i); // npc
v[1] = pop_arg(i); // worktype
cnpc = converse->actors->get_actor(npc_num(v[0]));
if(cnpc)
cnpc->set_worktype(v[1]);
break;
case U6OP_INVENTORY: // 0xbe
converse->print("!inventory\n");
break;
case U6OP_PORTRAIT: // 0xbf
converse->show_portrait(npc_num(pop_arg(i)));
break;
case U6OP_WAIT: // 0xcb, set page-break on scroller and wait
converse->print("*");
wait();
break;
case U6OP_ENDANSWER: // 0xee
break; // (frame only)
case U6OP_KEYWORDS: // 0xef (text:keywords)
if(answer_mode != ANSWER_DONE) // havn't already answered
{
answer_mode = ANSWER_NO;
if(check_keywords(get_text(), converse->get_input()))
answer_mode = ANSWER_YES;
}
break; // (frame only)
case U6OP_SIDENT: // 0xff, arg 1 is id number, name follows
v[0] = pop_arg(i);
if(v[0] != converse->npc_num)
DEBUG(0,LEVEL_WARNING,
"Converse: npc number in script (%d) does not"
" match actor number (%d)\n", v[0], converse->npc_num);
converse->name = strdup(get_text().c_str()); // collected
break;
case U6OP_SLOOK: // 0xf1, description follows
converse->desc = strdup(get_formatted_text(get_text().c_str()).c_str()); // collected
converse->print("\nYou see ");
converse->print(converse->desc);
converse->print("\n");
break;
case U6OP_SCONVERSE: // 0xf2
break;
case U6OP_SPREFIX: // 0xf3, Unknown
break;
case U6OP_ANSWER: // 0xf6
set_break(U6OP_KEYWORDS); // run or skip to next 0xef
if(answer_mode == ANSWER_YES) // depending on input comparison
{
set_run(true);
answer_mode = ANSWER_DONE; // don't do further comparisons
}
else
set_run(false);
break;
case U6OP_ASK: // 0xf7
answer_mode = ANSWER_NO; // reset answer switch
converse->collect_input();
break;
case U6OP_ASKC: // 0xf8 (blocking, single character input)
answer_mode = ANSWER_NO; // reset answer switch
converse->poll_input(get_text().c_str(), false); // collected=allowed input
break;
case U6OP_INPUTSTR: // 0xf9 (string or integer)
answer_mode = ANSWER_NO; // reset answer switch
//fall through here
case U6OP_INPUT: // 0xfb (integer)
v[0] = pop_arg(i); // var
v[1] = pop_arg(i); // type (should be 0xb2)
let(v[0], v[1]);
converse->poll_input();
break;
case U6OP_INPUTNUM: // 0xfc
v[0] = pop_arg(i); // var
v[1] = pop_arg(i); // type
let(v[0], v[1]);
converse->poll_input("0123456789");
break;
default:
converse->print("[Unknown command]\n");
DEBUG(0,LEVEL_ERROR,"Converse: UNK OP=%02x\n", in);
success = false;
}
return(success);
}
/* The other set of codes, these operate on the input values, so call them
* valops. Output goes back to the stack.
*/
bool ConverseInterpret::evop(stack<converse_typed_value> &i)
{
bool success = true;
converse_value v[4]; // input
converse_typed_value in;
converse_typed_value out;
Actor *cnpc = NULL;
Obj *cnpc_obj = NULL;
// converse_db_s *cdb;
Player *player = converse->player;
out.type = U6OP_VAR;
out.val = 0;
in.val = pop_arg(i);
#ifdef CONVERSE_DEBUG
DEBUG(1, LEVEL_DEBUGGING, "evop %s(%x)\n", evop_str(in.val), in.val);
#endif
switch(in.val)
{
case U6OP_GT: // 0x81
v[1] = pop_arg(i);
v[0] = pop_arg(i);
if(v[0] > v[1])
out.val = 1;
break;
case U6OP_GE: // 0x82
v[1] = pop_arg(i);
v[0] = pop_arg(i);
if(v[0] >= v[1])
out.val = 1;
break;
case U6OP_LT: // 0x83
v[1] = pop_arg(i);
v[0] = pop_arg(i);
if(v[0] < v[1])
out.val = 1;
break;
case U6OP_LE: // 0x84
v[1] = pop_arg(i);
v[0] = pop_arg(i);
if(v[0] <= v[1])
out.val = 1;
break;
case U6OP_NE: // 0x85
if(pop_arg(i) != pop_arg(i))
out.val = 1;
break;
case U6OP_EQ: // 0x86
out.val = evop_eq(i);
break;
case U6OP_ADD: // 0x90
out.val = pop_arg(i) + pop_arg(i);
break;
case U6OP_SUB: // 0x91
v[1] = pop_arg(i);
v[0] = pop_arg(i);
out.val = v[0] - v[1];
break;
case U6OP_MUL: // 0x92
out.val = pop_arg(i) * pop_arg(i);
break;
case U6OP_DIV: // 0x93
v[1] = pop_arg(i);
v[0] = pop_arg(i);
if(v[1] == 0)
{
DEBUG(0,LEVEL_ERROR,"Converse: Divide error\n");
success = false;
stop();
}
else
out.val = v[0] / v[1];
break;
case U6OP_LOR: // 0x94
v[1] = pop_arg(i);
v[0] = pop_arg(i);
if(v[0] || v[1])
out.val = 1;
break;
case U6OP_LAND: // 0x95
v[1] = pop_arg(i);
v[0] = pop_arg(i);
if(v[0] && v[1])
out.val = 1;
break;
case U6OP_CANCARRY: // 0x9a
cnpc = converse->actors->get_actor(pop_arg(i));
if(cnpc)
out.val = (converse_value)((cnpc->inventory_get_max_weight()
- cnpc->get_inventory_weight()) * 10);
break;