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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
|
#include "script-call-internal.hpp"
// script-call.cpp - EAthena script frontend, engine, and library.
//
// Copyright © ????-2004 Athena Dev Teams
// Copyright © 2004-2011 The Mana World Development Team
// Copyright © 2011 Chuck Miller
// Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
// Copyright © 2013 wushin
//
// This file is part of The Mana World (Athena server)
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
#include "../generic/intern-pool.hpp"
#include "../io/cxxstdio.hpp"
#include "../mmo/cxxstdio_enums.hpp"
#include "../high/core.hpp"
#include "battle.hpp"
#include "battle_conf.hpp"
#include "globals.hpp"
#include "map.hpp"
#include "npc.hpp"
#include "pc.hpp"
#include "script-fun.hpp"
#include "script-parse-internal.hpp"
#include "script-persist.hpp"
#include "script-startup-internal.hpp"
#include "../poison.hpp"
namespace tmwa
{
namespace map
{
constexpr bool DEBUG_RUN = false;
static
struct ScriptConfigRun
{
static const
int check_cmdcount = 8192;
static const
int check_gotocount = 512;
} script_config;
/*==========================================
* ridからsdへの解決
*------------------------------------------
*/
dumb_ptr<map_session_data> script_rid2sd(ScriptState *st)
{
dumb_ptr<map_session_data> sd = map_id2sd(st->rid);
if (!sd)
{
PRINTF("script_rid2sd: fatal error ! player not attached!\n"_fmt);
}
return sd;
}
/*==========================================
* 変数の読み取り
*------------------------------------------
*/
void get_val(dumb_ptr<block_list> sd, struct script_data *data)
{
MATCH_BEGIN (*data)
{
MATCH_CASE (const ScriptDataParam&, u)
{
if (sd == nullptr)
PRINTF("get_val error param SP::%d\n"_fmt, u.reg.sp());
int numi = 0;
if (sd)
numi = pc_readparam(sd->is_player(), u.reg.sp());
*data = ScriptDataInt{numi};
}
MATCH_CASE (const ScriptDataVariable&, u)
{
ZString name_ = variable_names.outtern(u.reg.base());
VarName name = stringish<VarName>(name_);
char prefix = name.front();
char postfix = name.back();
if (prefix != '$')
{
if (sd == nullptr)
PRINTF("get_val error name?:%s\n"_fmt, name);
}
if (postfix == '$')
{
RString str;
if (prefix == '@' || prefix == '.')
{
if (sd)
str = pc_readregstr(sd, u.reg);
}
else if (prefix == '$')
{
Option<P<RString>> s_ = mapregstr_db.search(u.reg);
OMATCH_BEGIN_SOME (s, s_)
{
str = *s;
}
OMATCH_END ();
}
else
{
PRINTF("script: get_val: illegal scope string variable.\n"_fmt);
str = "!!ERROR!!"_s;
}
*data = ScriptDataStr{str};
}
else
{
int numi = 0;
if (prefix == '@' || prefix == '.')
{
if (sd)
numi = pc_readreg(sd, u.reg);
}
else if (prefix == '$')
{
numi = mapreg_db.get(u.reg);
}
else if (prefix == '#')
{
if (name[1] == '#')
{
if (sd)
numi = pc_readaccountreg2(sd->is_player(), name);
}
else
{
if (sd)
numi = pc_readaccountreg(sd->is_player(), name);
}
}
else
{
if (sd)
numi = pc_readglobalreg(sd->is_player(), name);
}
*data = ScriptDataInt{numi};
}
}
}
MATCH_END ();
}
void get_val(ScriptState *st, struct script_data *data)
{
dumb_ptr<block_list> bl = nullptr;
MATCH_BEGIN (*data)
{
MATCH_CASE (const ScriptDataParam&, u)
{
bl = map_id2bl(st->rid);
}
MATCH_CASE (const ScriptDataVariable&, u)
{
ZString name_ = variable_names.outtern(u.reg.base());
VarName name = stringish<VarName>(name_);
char prefix = name.front();
if (prefix == '.' && name[1] == '@')
{
if (name.back() == '$')
{
Option<P<RString>> s = st->regstrm.search(u.reg);
ZString val = s.map([](P<RString> s_) -> ZString { return *s_; }).copy_or(""_s);
*data = ScriptDataStr{val};
}
else
*data = ScriptDataInt{st->regm.get(u.reg)};
return;
}
if (prefix == '.' && st->oid)
bl = map_id2bl(st->oid);
else if (prefix != '$' && st->rid)
bl = map_id2bl(st->rid);
}
}
MATCH_END ();
get_val(bl, data);
}
/*==========================================
* 変数の読み取り2
*------------------------------------------
*/
struct script_data get_val2(ScriptState *st, SIR reg)
{
struct script_data dat = ScriptDataVariable{reg};
get_val(st, &dat);
return dat;
}
/*==========================================
* 変数設定用
*------------------------------------------
*/
void set_reg(dumb_ptr<block_list> sd, VariableCode type, SIR reg, struct script_data vd)
{
if (type == VariableCode::PARAM)
{
int val = vd.get_if<ScriptDataInt>()->numi;
pc_setparam(sd->is_player(), reg.sp(), val);
return;
}
assert (type == VariableCode::VARIABLE);
ZString name_ = variable_names.outtern(reg.base());
VarName name = stringish<VarName>(name_);
char prefix = name.front();
char postfix = name.back();
if (postfix == '$')
{
RString str = vd.get_if<ScriptDataStr>()->str;
if (prefix == '@' || prefix == '.')
{
pc_setregstr(sd, reg, str);
}
else if (prefix == '$')
{
mapreg_setregstr(reg, str);
}
else
{
PRINTF("script: set_reg: illegal scope string variable !"_fmt);
}
}
else
{
int val = vd.get_if<ScriptDataInt>()->numi;
if (prefix == '@' || prefix == '.')
{
pc_setreg(sd, reg, val);
}
else if (prefix == '$')
{
mapreg_setreg(reg, val);
}
else if (prefix == '#')
{
if (name[1] == '#')
pc_setaccountreg2(sd->is_player(), name, val);
else
pc_setaccountreg(sd->is_player(), name, val);
}
else
{
pc_setglobalreg(sd->is_player(), name, val);
}
}
}
void set_scope_reg(ScriptState *st, SIR reg, struct script_data vd)
{
ZString name = variable_names.outtern(reg.base());
if (name.back() == '$')
{
if (auto *u = vd.get_if<ScriptDataStr>())
{
if (!u->str)
{
st->regstrm.erase(reg);
return;
}
st->regstrm.insert(reg, u->str);
}
else
st->regstrm.erase(reg);
}
else if (auto *u = vd.get_if<ScriptDataInt>())
st->regm.put(reg, u->numi);
}
void set_reg(dumb_ptr<block_list> sd, VariableCode type, SIR reg, int id)
{
struct script_data vd = ScriptDataInt{id};
set_reg(sd, type, reg, vd);
}
void set_reg(dumb_ptr<block_list> sd, VariableCode type, SIR reg, RString zd)
{
struct script_data vd = ScriptDataStr{zd};
set_reg(sd, type, reg, vd);
}
/*==========================================
* 文字列への変換
*------------------------------------------
*/
RString conv_str(ScriptState *st, struct script_data *data)
{
get_val(st, data);
assert (!data->is<ScriptDataRetInfo>());
if (auto *u = data->get_if<ScriptDataInt>())
{
AString buf = STRPRINTF("%d"_fmt, u->numi);
*data = ScriptDataStr{buf};
}
return data->get_if<ScriptDataStr>()->str;
}
/*==========================================
* 数値へ変換
*------------------------------------------
*/
int conv_num(ScriptState *st, struct script_data *data)
{
int rv = 0;
get_val(st, data);
assert (!data->is<ScriptDataRetInfo>());
MATCH_BEGIN (*data)
{
MATCH_DEFAULT ()
{
abort();
}
MATCH_CASE (const ScriptDataStr&, u)
{
RString p = u.str;
rv = atoi(p.c_str());
}
MATCH_CASE (const ScriptDataInt&, u)
{
return u.numi;
}
MATCH_CASE (const ScriptDataPos&, u)
{
return u.numi;
}
}
MATCH_END ()
*data = ScriptDataInt{rv};
return rv;
}
Borrowed<const ScriptBuffer> conv_script(ScriptState *st, struct script_data *data)
{
get_val(st, data);
return data->get_if<ScriptDataRetInfo>()->script;
}
void push_copy(struct script_stack *stack, int pos_)
{
script_data csd = stack->stack_datav[pos_];
stack->stack_datav.push_back(csd);
}
void pop_stack(struct script_stack *stack, int start, int end)
{
auto it = stack->stack_datav.begin();
stack->stack_datav.erase(it + start, it + end);
}
//
// 実行部main
//
/*==========================================
* コマンドの読み取り
*------------------------------------------
*/
static
ByteCode get_com(ScriptPointer *script)
{
if (static_cast<uint8_t>(script->peek()) >= 0x80)
{
// synthetic! Does not advance pos yet.
return ByteCode::INT;
}
return script->pop();
}
/*==========================================
* 数値の所得
*------------------------------------------
*/
static
int get_num(ScriptPointer *scr)
{
int i = 0;
int j = 0;
uint8_t val;
do
{
val = static_cast<uint8_t>(scr->pop());
i += (val & 0x7f) << j;
j += 6;
}
while (val >= 0xc0);
return i;
}
/*==========================================
* スタックから値を取り出す
*------------------------------------------
*/
static
int pop_val(ScriptState *st)
{
if (st->stack->stack_datav.empty())
return 0;
script_data& back = st->stack->stack_datav.back();
get_val(st, &back);
int rv = 0;
if (auto *u = back.get_if<ScriptDataInt>())
rv = u->numi;
st->stack->stack_datav.pop_back();
return rv;
}
static
bool isstr(struct script_data& c)
{
return c.is<ScriptDataStr>();
}
/*==========================================
* 加算演算子
*------------------------------------------
*/
static
void op_add(ScriptState *st)
{
get_val(st, &st->stack->stack_datav.back());
script_data back = st->stack->stack_datav.back();
st->stack->stack_datav.pop_back();
script_data& back1 = st->stack->stack_datav.back();
get_val(st, &back1);
if (!(isstr(back) || isstr(back1)))
{
back1.get_if<ScriptDataInt>()->numi += back.get_if<ScriptDataInt>()->numi;
}
else
{
RString sb = conv_str(st, &back);
RString sb1 = conv_str(st, &back1);
MString buf;
buf += sb1;
buf += sb;
back1 = ScriptDataStr{.str= AString(buf)};
}
}
/*==========================================
* 二項演算子(文字列)
*------------------------------------------
*/
static
void op_2str(ScriptState *st, ByteCode op, ZString s1, ZString s2)
{
int a = 0;
switch (op)
{
case ByteCode::EQ:
a = s1 == s2;
break;
case ByteCode::NE:
a = s1 != s2;
break;
case ByteCode::GT:
a = s1 > s2;
break;
case ByteCode::GE:
a = s1 >= s2;
break;
case ByteCode::LT:
a = s1 < s2;
break;
case ByteCode::LE:
a = s1 <= s2;
break;
default:
PRINTF("illegal string operater\n"_fmt);
break;
}
push_int<ScriptDataInt>(st->stack, a);
}
/*==========================================
* 二項演算子(数値)
*------------------------------------------
*/
static
void op_2num(ScriptState *st, ByteCode op, int i1, int i2)
{
switch (op)
{
case ByteCode::SUB:
i1 -= i2;
break;
case ByteCode::MUL:
i1 *= i2;
break;
case ByteCode::DIV:
i1 /= i2;
break;
case ByteCode::MOD:
i1 %= i2;
break;
case ByteCode::AND:
i1 &= i2;
break;
case ByteCode::OR:
i1 |= i2;
break;
case ByteCode::XOR:
i1 ^= i2;
break;
case ByteCode::LAND:
i1 = i1 && i2;
break;
case ByteCode::LOR:
i1 = i1 || i2;
break;
case ByteCode::EQ:
i1 = i1 == i2;
break;
case ByteCode::NE:
i1 = i1 != i2;
break;
case ByteCode::GT:
i1 = i1 > i2;
break;
case ByteCode::GE:
i1 = i1 >= i2;
break;
case ByteCode::LT:
i1 = i1 < i2;
break;
case ByteCode::LE:
i1 = i1 <= i2;
break;
case ByteCode::R_SHIFT:
i1 = i1 >> i2;
break;
case ByteCode::L_SHIFT:
i1 = i1 << i2;
break;
}
push_int<ScriptDataInt>(st->stack, i1);
}
/*==========================================
* 二項演算子
*------------------------------------------
*/
static
void op_2(ScriptState *st, ByteCode op)
{
// pop_val has unfortunate implications here
script_data d2 = st->stack->stack_datav.back();
st->stack->stack_datav.pop_back();
get_val(st, &d2);
script_data d1 = st->stack->stack_datav.back();
st->stack->stack_datav.pop_back();
get_val(st, &d1);
if (isstr(d1) && isstr(d2))
{
// ss => op_2str
op_2str(st, op, d1.get_if<ScriptDataStr>()->str, d2.get_if<ScriptDataStr>()->str);
}
else if (!(isstr(d1) || isstr(d2)))
{
// ii => op_2num
op_2num(st, op, d1.get_if<ScriptDataInt>()->numi, d2.get_if<ScriptDataInt>()->numi);
}
else
{
// si,is => error
PRINTF("script: op_2: int&str, str&int not allow.\n"_fmt);
push_int<ScriptDataInt>(st->stack, 0);
}
}
/*==========================================
* 単項演算子
*------------------------------------------
*/
static
void op_1num(ScriptState *st, ByteCode op)
{
int i1;
i1 = pop_val(st);
switch (op)
{
case ByteCode::NEG:
i1 = -i1;
break;
case ByteCode::NOT:
i1 = ~i1;
break;
case ByteCode::LNOT:
i1 = !i1;
break;
}
push_int<ScriptDataInt>(st->stack, i1);
}
/*==========================================
* 関数の実行
*------------------------------------------
*/
void run_func(ScriptState *st)
{
size_t end_sp = st->stack->stack_datav.size();
size_t start_sp = end_sp - 1;
while (!st->stack->stack_datav[start_sp].is<ScriptDataArg>())
{
start_sp--;
if (start_sp == 0)
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
PRINTF("run_func: function not found! @ %s\n"_fmt, nd ? nd->name : NpcName());
abort();
}
}
// the func is before the arg
start_sp--;
st->start = start_sp;
st->end = end_sp;
if (!st->stack->stack_datav[st->start].is<ScriptDataFuncRef>())
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
PRINTF("run_func: not a function or statement! @ %s\n"_fmt,
nd ? nd->name : NpcName());
abort();
}
size_t func = st->stack->stack_datav[st->start].get_if<ScriptDataFuncRef>()->numi;
if (DEBUG_RUN && battle_config.etc_log)
{
PRINTF("run_func : %s\n"_fmt,
builtin_functions[func].name);
PRINTF("stack dump :"_fmt);
for (script_data& d : st->stack->stack_datav)
{
MATCH_BEGIN (d)
{
MATCH_CASE (const ScriptDataInt&, u)
{
PRINTF(" int(%d)"_fmt, u.numi);
}
MATCH_CASE (const ScriptDataRetInfo&, u)
{
PRINTF(" retinfo(%p)"_fmt, static_cast<const void *>(&*u.script));
}
MATCH_CASE (const ScriptDataParam&, u)
{
PRINTF(" param(%d)"_fmt, u.reg.sp());
}
MATCH_CASE (const ScriptDataVariable&, u)
{
PRINTF(" name(%s)[%d]"_fmt, variable_names.outtern(u.reg.base()), u.reg.index());
}
MATCH_CASE (const ScriptDataArg&, u)
{
(void)u;
PRINTF(" arg"_fmt);
}
MATCH_CASE (const ScriptDataPos&, u)
{
(void)u;
PRINTF(" pos(%d)"_fmt, u.numi);
}
MATCH_CASE (const ScriptDataStr&, u)
{
(void)u;
PRINTF(" str(%s)"_fmt, u.str);
}
MATCH_CASE (const ScriptDataFuncRef&, u)
{
(void)u;
PRINTF(" func(%s)"_fmt, builtin_functions[u.numi].name);
}
}
MATCH_END ();
}
PRINTF("\n"_fmt);
}
builtin_functions[func].func(st);
pop_stack(st->stack, start_sp, end_sp);
if (st->state == ScriptEndState::RETFUNC)
{
// ユーザー定義関数からの復帰
int olddefsp = st->defsp;
pop_stack(st->stack, st->defsp, start_sp); // 復帰に邪魔なスタック削除
if (st->defsp < 4
|| !st->stack->stack_datav[st->defsp - 1].is<ScriptDataRetInfo>())
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
PRINTF("run_func: return without callfunc or callsub! @ %s\n"_fmt,
nd ? nd->name : NpcName());
abort();
}
assert (olddefsp == st->defsp); // pretty sure it hasn't changed yet
st->scriptp.code = Some(conv_script(st, &st->stack->stack_datav[olddefsp - 1])); // スクリプトを復元
st->scriptp.pos = conv_num(st, &st->stack->stack_datav[olddefsp - 2]); // スクリプト位置の復元
st->defsp = conv_num(st, &st->stack->stack_datav[olddefsp - 3]); // 基準スタックポインタを復元
// Number of arguments.
int i = conv_num(st, &st->stack->stack_datav[olddefsp - 4]); // 引数の数所得
assert (i == 0);
pop_stack(st->stack, olddefsp - 4 - i, olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
st->state = ScriptEndState::GOTO;
}
}
/*==========================================
* スクリプトの実行メイン部分
*------------------------------------------
*/
static
void run_script_main(ScriptState *st, Borrowed<const ScriptBuffer> rootscript)
{
int cmdcount = script_config.check_cmdcount;
int gotocount = script_config.check_gotocount;
struct script_stack *stack = st->stack;
st->defsp = stack->stack_datav.size();
int rerun_pos = st->scriptp.pos;
st->state = ScriptEndState::ZERO;
while (st->state == ScriptEndState::ZERO)
{
switch (ByteCode c = get_com(&st->scriptp))
{
case ByteCode::EOL:
if (stack->stack_datav.size() != st->defsp)
{
if (true)
PRINTF("stack.sp (%zu) != default (%d)\n"_fmt,
stack->stack_datav.size(),
st->defsp);
abort();
}
rerun_pos = st->scriptp.pos;
break;
case ByteCode::INT:
// synthesized!
push_int<ScriptDataInt>(stack, get_num(&st->scriptp));
break;
case ByteCode::POS:
case ByteCode::VARIABLE:
case ByteCode::FUNC_REF:
case ByteCode::PARAM:
// Note that these 3 have *very* different meanings,
// despite being encoded similarly.
{
int arg = 0;
arg |= static_cast<uint8_t>(st->scriptp.pop()) << 0;
arg |= static_cast<uint8_t>(st->scriptp.pop()) << 8;
arg |= static_cast<uint8_t>(st->scriptp.pop()) << 16;
switch(c)
{
case ByteCode::POS:
push_int<ScriptDataPos>(stack, arg);
break;
case ByteCode::VARIABLE:
push_reg<ScriptDataVariable>(stack, SIR::from(arg));
break;
case ByteCode::FUNC_REF:
push_int<ScriptDataFuncRef>(stack, arg);
break;
case ByteCode::PARAM:
SP arg_sp = static_cast<SP>(arg);
push_reg<ScriptDataParam>(stack, SIR::from(arg_sp));
break;
}
}
break;
case ByteCode::ARG:
push_int<ScriptDataArg>(stack, 0);
break;
case ByteCode::STR:
push_str<ScriptDataStr>(stack, st->scriptp.pops());
break;
case ByteCode::FUNC:
run_func(st);
if (st->state == ScriptEndState::GOTO)
{
rerun_pos = st->scriptp.pos;
st->state = ScriptEndState::ZERO;
if (st->freeloop != 1 && gotocount > 0 && (--gotocount) <= 0)
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
PRINTF("run_script: infinity loop! @ %s\n"_fmt,
nd ? nd->name : NpcName());
abort();
}
}
break;
case ByteCode::ADD:
op_add(st);
break;
case ByteCode::SUB:
case ByteCode::MUL:
case ByteCode::DIV:
case ByteCode::MOD:
case ByteCode::EQ:
case ByteCode::NE:
case ByteCode::GT:
case ByteCode::GE:
case ByteCode::LT:
case ByteCode::LE:
case ByteCode::AND:
case ByteCode::OR:
case ByteCode::XOR:
case ByteCode::LAND:
case ByteCode::LOR:
case ByteCode::R_SHIFT:
case ByteCode::L_SHIFT:
op_2(st, c);
break;
case ByteCode::NEG:
case ByteCode::NOT:
case ByteCode::LNOT:
op_1num(st, c);
break;
case ByteCode::NOP:
st->state = ScriptEndState::END;
break;
default:
if (battle_config.error_log)
{
PRINTF("unknown command : %d @ %zu\n"_fmt,
c, st->scriptp.pos);
if (st->oid)
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
PRINTF("NPC => %s\n"_fmt, nd->name);
}
if (st->rid)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
PRINTF("PC => %s\n"_fmt, sd->status_key.name.to__actual());
}
}
abort();
}
if (st->freeloop != 1 && cmdcount > 0 && (--cmdcount) <= 0)
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
PRINTF("run_script: infinity loop! @ %s\n"_fmt,
nd ? nd->name : NpcName());
abort();
}
}
switch (st->state)
{
case ScriptEndState::STOP:
break;
case ScriptEndState::END:
{
dumb_ptr<map_session_data> sd = map_id2sd(st->rid);
st->scriptp.code = None;
st->scriptp.pos = -1;
if (sd && sd->npc_id == st->oid)
npc_event_dequeue(sd);
}
break;
case ScriptEndState::RERUNLINE:
st->scriptp.pos = rerun_pos;
break;
}
if (st->state != ScriptEndState::END)
{
// 再開するためにスタック情報を保存
dumb_ptr<map_session_data> sd = map_id2sd(st->rid);
if (sd)
{
sd->npc_stackbuf = stack->stack_datav;
sd->npc_script = st->scriptp.code;
// sd->npc_pos is set later ... ???
sd->npc_scriptroot = Some(rootscript);
}
}
}
/*==========================================
* スクリプトの実行
*------------------------------------------
*/
int run_script(ScriptPointer sp, BlockId rid, BlockId oid)
{
return run_script_l(sp, rid, oid, nullptr);
}
int run_script_l(ScriptPointer sp, BlockId rid, BlockId oid,
Slice<argrec_t> args)
{
struct script_stack stack;
ScriptState st;
dumb_ptr<block_list> bl = map_id2bl(rid);
dumb_ptr<map_session_data> sd = bl? bl->is_player(): nullptr;
if (oid)
{
dumb_ptr<block_list> oid_bl = map_id2bl(oid);
if (oid_bl->bl_type == BL::NPC)
{
dumb_ptr<npc_data> nd = oid_bl->is_npc();
if(nd->npc_subtype == NpcSubtype::SCRIPT)
{
dumb_ptr<npc_data_script> nds = nd->is_script();
if (nds->scr.parent)
{
dumb_ptr<npc_data_script> parent = map_id2bl(nds->scr.parent)->is_npc()->is_script();
assert(parent->bl_type == BL::NPC && parent->npc_subtype == NpcSubtype::SCRIPT);
sp = ScriptPointer(borrow(*parent->scr.script), sp.pos);
}
}
}
}
P<const ScriptBuffer> rootscript = TRY_UNWRAP(sp.code, return -1);
int i;
if (sp.pos >> 24)
return -1;
if (sd && !sd->npc_stackbuf.empty() && sd->npc_scriptroot == Some(rootscript))
{
// 前回のスタックを復帰
sp.code = sd->npc_script;
stack.stack_datav = std::move(sd->npc_stackbuf);
}
st.stack = &stack;
st.scriptp = sp;
st.rid = rid;
st.oid = oid;
for (i = 0; i < args.size(); i++)
{
if (args[i].name.back() == '$')
pc_setregstr(sd, SIR::from(variable_names.intern(args[i].name)), args[i].v.s);
else
pc_setreg(sd, SIR::from(variable_names.intern(args[i].name)), args[i].v.i);
}
run_script_main(&st, rootscript);
stack.stack_datav.clear();
return st.scriptp.pos;
}
void set_script_var_i(dumb_ptr<map_session_data> sd, VarName var, int e, int val)
{
size_t k = variable_names.intern(var);
SIR reg = SIR::from(k, e);
set_reg(sd, VariableCode::VARIABLE, reg, val);
}
void set_script_var_s(dumb_ptr<map_session_data> sd, VarName var, int e, XString val)
{
size_t k = variable_names.intern(var);
SIR reg = SIR::from(k, e);
set_reg(sd, VariableCode::VARIABLE, reg, val);
}
int get_script_var_i(dumb_ptr<map_session_data> sd, VarName var, int e)
{
size_t k = variable_names.intern(var);
SIR reg = SIR::from(k, e);
struct script_data dat = ScriptDataVariable{.reg= reg};
get_val(sd, &dat);
if (auto *u = dat.get_if<ScriptDataInt>())
return u->numi;
PRINTF("Warning: you lied about the type and I'm too lazy to fix it!"_fmt);
return 0;
}
ZString get_script_var_s(dumb_ptr<map_session_data> sd, VarName var, int e)
{
size_t k = variable_names.intern(var);
SIR reg = SIR::from(k, e);
struct script_data dat = ScriptDataVariable{.reg= reg};
get_val(sd, &dat);
if (auto *u = dat.get_if<ScriptDataStr>())
// this is almost certainly a memory leak after CONSTSTR removal
return u->str;
PRINTF("Warning: you lied about the type and I can't fix it!"_fmt);
return ZString();
}
} // namespace map
} // namespace tmwa
|