forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.c
More file actions
1645 lines (1420 loc) · 50.8 KB
/
script.c
File metadata and controls
1645 lines (1420 loc) · 50.8 KB
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
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
// Copyright (c) 2011-2024, SDLPAL development team.
// All rights reserved.
//
// This file is part of SDLPAL.
//
// SDLPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 3
// as published by the Free Software Foundation.
//
// 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/>.
//
// Based on PALx Project by palxex.
// Copyright (c) 2006-2008, Pal Lockheart <palxex@gmail.com>.
//
#include "main.h"
BOOL g_fScriptSuccess = TRUE;
static int g_iCurEquipPart = -1;
static int g_iCurPlayingRNG; // 当前正在播放的 RNG 动画编号
static WORD PAL_InterpretInstruction(WORD wScriptEntry, WORD wEventObjectID)
/*++
Purpose:
Interpret and execute one instruction in the script.
Parameters:
[IN] wScriptEntry - The script entry to execute.
[IN] wEventObjectID - The event object ID which invoked the script.
Return value:
The address of the next script instruction to execute.
--*/
{
LPSCRIPTENTRY pScript = &(SCRIPT[wScriptEntry]);
LPEVENTOBJECT pEvtObj = wEventObjectID != 0 ? &g.events[wEventObjectID - 1] : NULL;
// If opr0 is ObjectID, should just use pCurrent.
LPEVENTOBJECT pCurrent;
if (pScript->rgwOperand[0] == 0 || pScript->rgwOperand[0] == 0xFFFF) {
pCurrent = pEvtObj;
} else {
pCurrent = &(g.events[pScript->rgwOperand[0] - 1]);
}
switch (pScript->wOperation) {
case 0x000B:
case 0x000C:
case 0x000D:
case 0x000E:
// walk one step
pEvtObj->wDirection = pScript->wOperation - 0x000B;
PAL_NPCWalkOneStep(wEventObjectID, 2);
break;
case 0x000F:
// Set the direction and/or gesture for event object
if (pScript->rgwOperand[0] != 0xFFFF)
pEvtObj->wDirection = pScript->rgwOperand[0];
if (pScript->rgwOperand[1] != 0xFFFF)
pEvtObj->wCurrentFrameNum = pScript->rgwOperand[1];
break;
case 0x0010:
// Walk straight to the specified position
if (!PAL_NPCWalkTo(wEventObjectID, pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 3))
wScriptEntry--;
break;
case 0x0011:
// Walk straight to the specified position, at a lower speed
if ((wEventObjectID & 1) ^ (gFrameNum & 1)) {
if (!PAL_NPCWalkTo(wEventObjectID, pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 2))
wScriptEntry--;
} else {
wScriptEntry--;
}
break;
case 0x0012:
// Set the position of the event object, relative to the party
pCurrent->x = pScript->rgwOperand[1] + PAL_X(g.viewport) + PAL_X(gPartyOffset);
pCurrent->y = pScript->rgwOperand[2] + PAL_Y(g.viewport) + PAL_Y(gPartyOffset);
break;
case 0x0013:
// Set the position of the event object
pCurrent->x = pScript->rgwOperand[1];
pCurrent->y = pScript->rgwOperand[2];
break;
case 0x0014:
// Set the gesture of the event object
pEvtObj->wCurrentFrameNum = pScript->rgwOperand[0];
pEvtObj->wDirection = kDirSouth;
break;
case 0x0015:
// Set the direction and gesture for a party member
g.wPartyDirection = pScript->rgwOperand[0];
PARTY[pScript->rgwOperand[2]].wFrame = g.wPartyDirection * 3 + pScript->rgwOperand[1];
break;
case 0x0016:
// Set the direction and gesture for an event object
if (pScript->rgwOperand[0] != 0) {
pCurrent->wDirection = pScript->rgwOperand[1];
pCurrent->wCurrentFrameNum = pScript->rgwOperand[2];
}
break;
case 0x0017: {
// set the player's extra attribute
int part = pScript->rgwOperand[0] - 0xB;
WORD *p = (WORD *)(&gEquipmentEffect[part]); // HACKHACK
p[pScript->rgwOperand[1] * MAX_PLAYER_ROLES + wEventObjectID] = (SHORT)pScript->rgwOperand[2];
} break;
case 0x0018: {
// Equip the selected item
int part = pScript->rgwOperand[0] - 0x0B;
g_iCurEquipPart = part;
// The wEventObjectID parameter here should indicate the player role
PAL_RemoveEquipmentEffect(wEventObjectID, part);
WORD prevObjID = PLAYER.rgwEquipment[part][wEventObjectID];
if (prevObjID != pScript->rgwOperand[1]) {
PLAYER.rgwEquipment[part][wEventObjectID] = pScript->rgwOperand[1];
gLastUnequippedItem = prevObjID;
int i, j;
if (PAL_GetItemIndexToInventory(pScript->rgwOperand[1], &i) && i < MAX_INVENTORY &&
g.rgInventory[i].nAmount == 1 && prevObjID != 0 && !PAL_GetItemIndexToInventory(prevObjID, &j)) {
// When the number of items you want to wear is 1 and the number of items you are wearing is also 1,
// replace them directly, instead of removing items and adding them at the end of the item menu
g.rgInventory[i].wItem = prevObjID;
} else {
PAL_AddItemToInventory(pScript->rgwOperand[1], -1);
if (prevObjID != 0)
PAL_AddItemToInventory(prevObjID, 1);
}
}
} break;
case 0x0019: {
// Increase/decrease the player's attribute
WORD *p = (WORD *)(&PLAYER); // HACKHACK
int iPlayerRole;
if (pScript->rgwOperand[2] == 0)
iPlayerRole = wEventObjectID;
else
iPlayerRole = pScript->rgwOperand[2] - 1;
p[pScript->rgwOperand[0] * MAX_PLAYER_ROLES + iPlayerRole] += (SHORT)pScript->rgwOperand[1];
} break;
case 0x001A: {
// Set player's stat
WORD *p;
// 装备过程中, 修改装备附加属性, 否则直接修改玩家属性
if (g_iCurEquipPart != -1)
p = (WORD *)&(gEquipmentEffect[g_iCurEquipPart]);
else
p = (WORD *)(&PLAYER); // HACKHACK
int iPlayerRole;
if (pScript->rgwOperand[2] == 0)
iPlayerRole = wEventObjectID;
else
iPlayerRole = pScript->rgwOperand[2] - 1;
p[pScript->rgwOperand[0] * MAX_PLAYER_ROLES + iPlayerRole] = (SHORT)pScript->rgwOperand[1];
} break;
case 0x001B:
// 0x001B: Increase/decrease player's HP
case 0x001C:
// 0x001C: Increase/decrease player's MP
case 0x001D: {
// 0x001D: Increase/decrease player's HP and MP
SHORT val = (SHORT)pScript->rgwOperand[1];
SHORT sHP = (pScript->wOperation != 0x001C) ? val : 0;
SHORT sMP = (pScript->wOperation != 0x001B) ? val : 0;
if (pScript->rgwOperand[0]) {
// Apply to everyone
g_fScriptSuccess = FALSE;
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++)
if (PAL_IncreaseHPMP(PARTY_PLAYER(i), sHP, sMP))
g_fScriptSuccess = TRUE;
} else {
// Apply to one player. The wEventObjectID parameter should indicate the player role.
g_fScriptSuccess = FALSE;
if (PAL_IncreaseHPMP(wEventObjectID, sHP, sMP))
g_fScriptSuccess = TRUE;
}
} break;
case 0x001E: {
// Increase or decrease cash by the specified amount
SHORT amount = (SHORT)(pScript->rgwOperand[0]);
if (amount < 0 && g.dwCash < -amount) // not enough cash
wScriptEntry = pScript->rgwOperand[1] - 1;
else
g.dwCash += amount;
} break;
case 0x001F:
// Add item to inventory
PAL_AddItemToInventory(pScript->rgwOperand[0], (SHORT)(pScript->rgwOperand[1]));
break;
case 0x0020: {
// Remove item from inventory
int objID = pScript->rgwOperand[0];
int amount = pScript->rgwOperand[1];
if (amount == 0)
amount = 1;
if (amount <= PAL_CountItem(objID) || pScript->rgwOperand[2] == 0) {
int owe = PAL_AddItemToInventory(objID, -amount);
if (owe <= 0) {
// Check if it's a partial removal and set the remaining amount
if (owe < 0)
amount = -owe;
// Try removing equipped item
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
for (int j = 0; j < MAX_PLAYER_EQUIPMENTS; j++) {
if (PLAYER.rgwEquipment[j][w] == objID) {
PAL_RemoveEquipmentEffect(w, j);
PLAYER.rgwEquipment[j][w] = 0;
if (--amount == 0) {
i = 9999;
break;
}
}
}
}
}
} else
wScriptEntry = pScript->rgwOperand[2] - 1;
} break;
case 0x0021:
// Inflict damage to the enemy
if (pScript->rgwOperand[0]) {
// Inflict damage to all enemies
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
if (BATTLE_ENEMY[i].wObjectID != 0)
BATTLE_ENEMY[i].e.wHealth -= pScript->rgwOperand[1];
} else {
// Inflict damage to one enemy
BATTLE_ENEMY[wEventObjectID].e.wHealth -= pScript->rgwOperand[1];
}
break;
case 0x0022:
// Revive player
if (pScript->rgwOperand[0]) {
// Apply to everyone
g_fScriptSuccess = FALSE;
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
if (PLAYER.rgwHP[w] == 0) {
PLAYER.rgwHP[w] = PLAYER.rgwMaxHP[w] * pScript->rgwOperand[1] / 10;
PAL_CurePoisonByLevel(w, 3);
for (int x = 0; x < kStatusAll; x++)
PAL_RemovePlayerStatus(w, x);
g_fScriptSuccess = TRUE;
}
}
} else {
// Apply to one player
g_fScriptSuccess = FALSE;
WORD w = wEventObjectID;
if (PLAYER.rgwHP[w] == 0) {
PLAYER.rgwHP[w] = PLAYER.rgwMaxHP[w] * pScript->rgwOperand[1] / 10;
PAL_CurePoisonByLevel(w, 3);
for (int x = 0; x < kStatusAll; x++)
PAL_RemovePlayerStatus(w, x);
g_fScriptSuccess = TRUE;
}
}
break;
case 0x0023: {
// Remove equipment from the specified player
int iPlayerRole = pScript->rgwOperand[0];
if (pScript->rgwOperand[1] == 0) {
// Remove all equipments
for (int i = 0; i < MAX_PLAYER_EQUIPMENTS; i++) {
WORD w = PLAYER.rgwEquipment[i][iPlayerRole];
if (w != 0) {
PAL_AddItemToInventory(w, 1);
PLAYER.rgwEquipment[i][iPlayerRole] = 0;
}
PAL_RemoveEquipmentEffect(iPlayerRole, i);
}
} else {
WORD w = PLAYER.rgwEquipment[pScript->rgwOperand[1] - 1][iPlayerRole];
if (w != 0) {
PAL_RemoveEquipmentEffect(iPlayerRole, pScript->rgwOperand[1] - 1);
PAL_AddItemToInventory(w, 1);
PLAYER.rgwEquipment[pScript->rgwOperand[1] - 1][iPlayerRole] = 0;
}
}
} break;
case 0x0024:
// Set the autoscript entry address for an event object
if (pScript->rgwOperand[0] != 0)
pCurrent->wAutoScript = pScript->rgwOperand[1];
break;
case 0x0025:
// Set the trigger script entry address for an event object
if (pScript->rgwOperand[0] != 0)
pCurrent->wTriggerScript = pScript->rgwOperand[1];
break;
case 0x0026:
// Show the buy item menu
PAL_MakeScene();
VIDEO_UpdateScreen(NULL);
PAL_BuyMenu(pScript->rgwOperand[0]);
break;
case 0x0027:
// Show the sell item menu
PAL_MakeScene();
VIDEO_UpdateScreen(NULL);
PAL_SellMenu();
break;
case 0x0028: {
// Apply poison to enemy
BOOL applyToAll = pScript->rgwOperand[0];
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
WORD w = BATTLE_ENEMY[i].wObjectID;
if (w == 0)
continue;
if (!applyToAll && i != wEventObjectID)
continue;
if (RandomLong(0, 9) >= OBJECT[w].enemy.wResistanceToSorcery) {
int j;
for (j = 0; j < MAX_POISONS; j++)
if (BATTLE_ENEMY[i].rgPoisons[j].wPoisonID == pScript->rgwOperand[1])
break;
if (j >= MAX_POISONS) {
for (j = 0; j < MAX_POISONS; j++) {
if (BATTLE_ENEMY[i].rgPoisons[j].wPoisonID == 0) {
BATTLE_ENEMY[i].rgPoisons[j].wPoisonID = pScript->rgwOperand[1];
BATTLE_ENEMY[i].rgPoisons[j].wPoisonScript =
PAL_RunTriggerScript(OBJECT[pScript->rgwOperand[1]].poison.wEnemyScript, wEventObjectID);
break;
}
}
}
}
}
} break;
case 0x0029: {
// Apply poison to player
BOOL applyToAll = pScript->rgwOperand[0];
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
if (!applyToAll && w != wEventObjectID)
continue;
if (RandomLong(1, 100) > PAL_GetPlayerPoisonResistance(w))
PAL_AddPoisonForPlayer(w, pScript->rgwOperand[1]);
}
} break;
case 0x002A: {
// Cure poison by object ID for enemy
BOOL applyToAll = pScript->rgwOperand[0];
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
if (BATTLE_ENEMY[i].wObjectID == 0)
continue;
if (!applyToAll && i != wEventObjectID)
continue;
for (int j = 0; j < MAX_POISONS; j++) {
if (BATTLE_ENEMY[i].rgPoisons[j].wPoisonID == pScript->rgwOperand[1]) {
BATTLE_ENEMY[i].rgPoisons[j].wPoisonID = 0;
BATTLE_ENEMY[i].rgPoisons[j].wPoisonScript = 0;
break;
}
}
}
} break;
case 0x002B: {
// Cure poison by object ID for player
BOOL applyToAll = pScript->rgwOperand[0];
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
if (!applyToAll && w != wEventObjectID)
continue;
PAL_CurePoisonByKind(w, pScript->rgwOperand[1]);
}
} break;
case 0x002C: {
// Cure poisons by level
BOOL applyToAll = pScript->rgwOperand[0];
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
if (!applyToAll && w != wEventObjectID)
continue;
PAL_CurePoisonByLevel(w, pScript->rgwOperand[1]);
}
} break;
case 0x002D:
// Set the status for player
if (!PAL_SetPlayerStatus(wEventObjectID, pScript->rgwOperand[0], pScript->rgwOperand[1])) {
g_fScriptSuccess = FALSE;
}
break;
case 0x002E:
// Set the status for enemy
if (RandomLong(0, 9) > OBJECT[BATTLE_ENEMY[wEventObjectID].wObjectID].enemy.wResistanceToSorcery) {
BATTLE_ENEMY[wEventObjectID].rgwStatus[pScript->rgwOperand[0]] = pScript->rgwOperand[1];
} else {
wScriptEntry = pScript->rgwOperand[2] - 1;
}
break;
case 0x002F:
// Remove player's status
PAL_RemovePlayerStatus(wEventObjectID, pScript->rgwOperand[0]);
break;
case 0x0030: {
// Increase player's stat temporarily by percent
WORD *ptmp = (WORD *)(&gEquipmentEffect[kBodyPartExtra]); // HACKHACK
WORD *p = (WORD *)(&PLAYER); // HACKHACK
int iPlayerRole;
if (pScript->rgwOperand[2] == 0)
iPlayerRole = wEventObjectID;
else
iPlayerRole = pScript->rgwOperand[2] - 1;
ptmp[pScript->rgwOperand[0] * MAX_PLAYER_ROLES + iPlayerRole] =
p[pScript->rgwOperand[0] * MAX_PLAYER_ROLES + iPlayerRole] * (SHORT)pScript->rgwOperand[1] / 100;
} break;
case 0x0031:
// Change battle sprite temporarily for player (变蛇)
gEquipmentEffect[kBodyPartExtra].rgwSpriteNumInBattle[wEventObjectID] = pScript->rgwOperand[0];
break;
case 0x0033:
// collect the enemy for items
if (BATTLE_ENEMY[wEventObjectID].e.wCollectValue != 0) {
g.wCollectValue += BATTLE_ENEMY[wEventObjectID].e.wCollectValue;
} else {
wScriptEntry = pScript->rgwOperand[0] - 1;
}
break;
case 0x0034:
// Transform collected enemies into items
if (!PAL_collectItem()) {
wScriptEntry = pScript->rgwOperand[0] - 1;
}
break;
case 0x0035:
// Shake the screen
VIDEO_ShakeScreen(pScript->rgwOperand[0], pScript->rgwOperand[1] ? pScript->rgwOperand[1] : 4);
if (!pScript->rgwOperand[0])
VIDEO_UpdateScreen(NULL);
break;
case 0x0036:
// Set the current playing RNG animation
g_iCurPlayingRNG = pScript->rgwOperand[0];
break;
case 0x0037:
// Play RNG animation
PAL_RNGPlay(g_iCurPlayingRNG, pScript->rgwOperand[0], pScript->rgwOperand[1] > 0 ? pScript->rgwOperand[1] : -1,
pScript->rgwOperand[2] > 0 ? pScript->rgwOperand[2] : 16);
break;
case 0x0038:
// Teleport the party out of the scene
if (!gInBattle && g.scenes[g.wCurScene - 1].wScriptOnLeave != 0) {
PAL_RunTriggerScript(g.scenes[g.wCurScene - 1].wScriptOnLeave, 0xFFFF);
} else {
// failed
g_fScriptSuccess = FALSE;
wScriptEntry = pScript->rgwOperand[0] - 1;
}
break;
case 0x0039: {
// Drain HP from enemy
BATTLE_ENEMY[wEventObjectID].e.wHealth -= pScript->rgwOperand[0];
WORD w = PARTY_PLAYER(g_Battle.wMovingPlayerIndex);
PLAYER.rgwHP[w] += pScript->rgwOperand[0];
if (PLAYER.rgwHP[w] > PLAYER.rgwMaxHP[w])
PLAYER.rgwHP[w] = PLAYER.rgwMaxHP[w];
} break;
case 0x003A:
// Player flee from the battle
if (g_Battle.fIsBoss) {
// Cannot flee from bosses
wScriptEntry = pScript->rgwOperand[0] - 1;
} else {
PAL_BattlePlayerEscape();
}
break;
case 0x003F:
// Ride the event object to the specified position, at a low speed
PAL_PartyRideEventObject(wEventObjectID, pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 2);
break;
case 0x0040:
// set the trigger method for a event object
if (pScript->rgwOperand[0] != 0)
pCurrent->wTriggerMode = pScript->rgwOperand[1];
break;
case 0x0041:
// Mark the script as failed
g_fScriptSuccess = FALSE;
break;
case 0x0042:
// Simulate a magic for player
assert(pScript->rgwOperand[2] == 0);
PAL_BattleSimulateMagic(wEventObjectID, pScript->rgwOperand[0], pScript->rgwOperand[1]);
break;
case 0x0043:
// Set background music
g.wCurMusic = pScript->rgwOperand[0];
AUDIO_PlayMusic(pScript->rgwOperand[0],
/*loop=*/pScript->rgwOperand[1] != 1,
/*fade_time=*/pScript->rgwOperand[1] == 3 ? 3.0f : 0.0f);
break;
case 0x0044:
// Ride the event object to the specified position, at the normal speed
PAL_PartyRideEventObject(wEventObjectID, pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 4);
break;
case 0x0045:
// Set battle music
g.wCurBattleMusic = pScript->rgwOperand[0];
break;
case 0x0046: {
// Set the party position on the map
int xOffset = ((g.wPartyDirection == kDirWest || g.wPartyDirection == kDirSouth) ? 16 : -16);
int yOffset = ((g.wPartyDirection == kDirWest || g.wPartyDirection == kDirNorth) ? 8 : -8);
g.viewport = PAL_XY(pScript->rgwOperand[0] * 32 + pScript->rgwOperand[2] * 16 - PAL_X(gPartyOffset),
pScript->rgwOperand[1] * 16 + pScript->rgwOperand[2] * 8 - PAL_Y(gPartyOffset));
int x = PAL_X(gPartyOffset);
int y = PAL_Y(gPartyOffset);
for (int i = 0; i < MAX_PLAYABLE_PLAYER_ROLES; i++) {
PARTY[i].x = x;
PARTY[i].y = y;
TRAIL[i].x = x + PAL_X(g.viewport);
TRAIL[i].y = y + PAL_Y(g.viewport);
TRAIL[i].wDirection = g.wPartyDirection;
x += xOffset;
y += yOffset;
}
} break;
case 0x0047:
// Play sound effect
AUDIO_PlaySound(pScript->rgwOperand[0]);
break;
case 0x0049:
// Set the state of event object
if (pScript->rgwOperand[0] != 0)
pCurrent->sState = pScript->rgwOperand[1];
break;
case 0x004A:
// Set the current battlefield
g.wCurBattleField = pScript->rgwOperand[0];
break;
case 0x004B:
// Nullify the event object for a short while
pEvtObj->sVanishTime = -15;
break;
case 0x004C: {
// chase the player
int chaseRange = pScript->rgwOperand[0];
if (chaseRange == 0)
chaseRange = 8;
int speed = pScript->rgwOperand[1];
if (speed == 0)
speed = 4;
BOOL floating = pScript->rgwOperand[2];
PAL_MonsterChasePlayer(wEventObjectID, speed, chaseRange, floating);
} break;
case 0x004E:
// Load the last saved game
PAL_FadeOut(1);
PAL_ReloadInNextTick(gCurrentSaveSlot);
return 0; // don't go further
case 0x004F:
// Fade the screen to red color (game over)
PAL_FadeToRed();
break;
case 0x0050:
// screen fade out
VIDEO_UpdateScreen(NULL);
PAL_FadeOut(pScript->rgwOperand[0] ? pScript->rgwOperand[0] : 1);
gNeedToFadeIn = TRUE;
break;
case 0x0051:
// screen fade in
VIDEO_UpdateScreen(NULL);
PAL_FadeIn(gCurPalette, gNightPalette, ((SHORT)(pScript->rgwOperand[0]) > 0) ? pScript->rgwOperand[0] : 1);
gNeedToFadeIn = FALSE;
break;
case 0x0052:
// hide the event object for a while, default 800 frames
pEvtObj->sState *= -1;
pEvtObj->sVanishTime = (pScript->rgwOperand[0] ? pScript->rgwOperand[0] : 800);
break;
case 0x0053:
// use the day palette
gNightPalette = FALSE;
break;
case 0x0054:
// use the night palette
gNightPalette = TRUE;
break;
case 0x0055: {
// Add magic to a player
WORD role = pScript->rgwOperand[1] ? pScript->rgwOperand[1] - 1 : wEventObjectID;
PAL_AddMagic(role, pScript->rgwOperand[0]);
} break;
case 0x0056: {
// Remove magic from a player
WORD role = pScript->rgwOperand[1] ? pScript->rgwOperand[1] - 1 : wEventObjectID;
PAL_RemoveMagic(role, pScript->rgwOperand[0]);
} break;
case 0x0057: {
// Set the base damage of magic according to MP value
int multiplier = (pScript->rgwOperand[1] == 0) ? 8 : pScript->rgwOperand[1];
WORD magicNumber = OBJECT[pScript->rgwOperand[0]].magic.wMagicNumber;
MAGIC[magicNumber].wBaseDamage = PLAYER.rgwMP[wEventObjectID] * multiplier;
PLAYER.rgwMP[wEventObjectID] = 0;
} break;
case 0x0058:
// Jump if there is less than the specified number of the specified items in the inventory
if (PAL_GetItemAmount(pScript->rgwOperand[0]) < (SHORT)(pScript->rgwOperand[1]))
wScriptEntry = pScript->rgwOperand[2] - 1;
break;
case 0x0059: {
// Change to the specified scene
int scene = pScript->rgwOperand[0];
if (scene > 0 && scene <= MAX_SCENES && g.wCurScene != scene) {
// Set data to load the scene in the next frame
g.wCurScene = scene;
PAL_SetLoadFlags(kLoadScene);
gEnteringScene = TRUE;
g.wLayer = 0;
}
} break;
case 0x005A:
// Halve the player's HP
// The wEventObjectID parameter here should indicate the player role
PLAYER.rgwHP[wEventObjectID] /= 2;
break;
case 0x005B: {
// Halve the enemy's HP
WORD damage = BATTLE_ENEMY[wEventObjectID].e.wHealth / 2 + 1;
if (damage > pScript->rgwOperand[0])
damage = pScript->rgwOperand[0];
BATTLE_ENEMY[wEventObjectID].e.wHealth -= damage;
} break;
case 0x005C:
// Hide for a while
g_Battle.iHidingTime = -(INT)(pScript->rgwOperand[0]);
break;
case 0x005D:
// Jump if player doesn't have the specified poison
if (!PAL_IsPlayerPoisonedByKind(wEventObjectID, pScript->rgwOperand[0]))
wScriptEntry = pScript->rgwOperand[1] - 1;
break;
case 0x005E: {
// Jump if enemy doesn't have the specified poison
int i;
for (i = 0; i < MAX_POISONS; i++)
if (BATTLE_ENEMY[wEventObjectID].rgPoisons[i].wPoisonID == pScript->rgwOperand[0])
break;
if (i >= MAX_POISONS)
wScriptEntry = pScript->rgwOperand[1] - 1;
} break;
case 0x005F:
// Kill the player immediately
// The wEventObjectID parameter here should indicate the player role
PLAYER.rgwHP[wEventObjectID] = 0;
break;
case 0x0060:
// Immediate KO of the enemy
BATTLE_ENEMY[wEventObjectID].e.wHealth = 0;
break;
case 0x0061:
// Jump if player is not poisoned
if (!PAL_IsPlayerPoisonedByLevel(wEventObjectID, 0))
wScriptEntry = pScript->rgwOperand[0] - 1;
break;
case 0x0062:
// Pause enemy chasing for a while
g.wChasespeedChangeCycles = pScript->rgwOperand[0];
g.wChaseRange = 0;
break;
case 0x0063:
// Speed up enemy chasing for a while
g.wChasespeedChangeCycles = pScript->rgwOperand[0];
g.wChaseRange = 3;
break;
case 0x0064: {
// Jump if enemy's HP is more than the specified percentage
BATTLEENEMY *enemy = &BATTLE_ENEMY[wEventObjectID];
int enemyID = OBJECT[enemy->wObjectID].enemy.wEnemyID;
if (enemy->e.wHealth * 100 > ENEMY[enemyID].wHealth * pScript->rgwOperand[0])
wScriptEntry = pScript->rgwOperand[1] - 1;
} break;
case 0x0065:
// Set the player's sprite
PLAYER.rgwSpriteNum[pScript->rgwOperand[0]] = pScript->rgwOperand[1];
if (!gInBattle && pScript->rgwOperand[2]) {
PAL_SetLoadFlags(kLoadPlayerSprite);
PAL_LoadResources();
}
break;
case 0x0066: {
// Throw weapon to enemy
int baseDamage = pScript->rgwOperand[1] * 5;
baseDamage += PLAYER.rgwAttackStrength[PARTY_PLAYER(g_Battle.wMovingPlayerIndex)] * RandomLong(0, 3);
PAL_BattleSimulateMagic((SHORT)wEventObjectID, pScript->rgwOperand[0], baseDamage);
} break;
case 0x0067:
// Enemy use magic
BATTLE_ENEMY[wEventObjectID].e.wMagic = pScript->rgwOperand[0];
BATTLE_ENEMY[wEventObjectID].e.wMagicRate = (pScript->rgwOperand[1] == 0) ? 10 : pScript->rgwOperand[1];
break;
case 0x0068:
// Jump if it's enemy's turn
if (g_Battle.fEnemyMoving)
wScriptEntry = pScript->rgwOperand[0] - 1;
break;
case 0x0069:
// Enemy escape in battle
PAL_BattleEnemyEscape();
break;
case 0x006A:
// Steal from the enemy
PAL_BattleStealFromEnemy(wEventObjectID, pScript->rgwOperand[0]);
break;
case 0x006B:
// Blow away enemies
g_Battle.iBlow = (SHORT)(pScript->rgwOperand[0]);
break;
case 0x006C:
// Walk the NPC in one step
pCurrent->x += (SHORT)(pScript->rgwOperand[1]);
pCurrent->y += (SHORT)(pScript->rgwOperand[2]);
WORD objectID;
if (pScript->rgwOperand[0] == 0 || pScript->rgwOperand[0] == 0xFFFF)
objectID = wEventObjectID;
else
objectID = pScript->rgwOperand[0];
PAL_NPCWalkOneStep(objectID, 0);
break;
case 0x006D: {
// Set the enter script and leave script for a scene
int scene = pScript->rgwOperand[0];
if (scene) {
if (pScript->rgwOperand[1]) {
g.scenes[scene - 1].wScriptOnEnter = pScript->rgwOperand[1];
}
if (pScript->rgwOperand[2]) {
g.scenes[scene - 1].wScriptOnLeave = pScript->rgwOperand[2];
}
if (pScript->rgwOperand[1] == 0 && pScript->rgwOperand[2] == 0) {
g.scenes[scene - 1].wScriptOnEnter = 0;
g.scenes[scene - 1].wScriptOnLeave = 0;
}
}
} break;
case 0x006E:
// Move the player to the specified position in one step
for (int i = 3; i >= 0; i--)
TRAIL[i + 1] = TRAIL[i];
TRAIL[0].wDirection = g.wPartyDirection;
TRAIL[0].x = PAL_X(g.viewport) + PAL_X(gPartyOffset);
TRAIL[0].y = PAL_Y(g.viewport) + PAL_Y(gPartyOffset);
g.viewport = PAL_XY(PAL_X(g.viewport) + (SHORT)(pScript->rgwOperand[0]),
PAL_Y(g.viewport) + (SHORT)(pScript->rgwOperand[1]));
g.wLayer = pScript->rgwOperand[2] * 8;
if (pScript->rgwOperand[0] != 0 || pScript->rgwOperand[1] != 0)
PAL_UpdatePartyGestures(TRUE);
break;
case 0x006F:
// Sync the state of current event object with another event object
if (pCurrent->sState == (SHORT)(pScript->rgwOperand[1]))
pEvtObj->sState = (SHORT)(pScript->rgwOperand[1]);
break;
case 0x0070:
// Walk the party to the specified position
PAL_PartyWalkTo(pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 2);
break;
case 0x0071:
// Wave the screen
g.wScreenWave = pScript->rgwOperand[0];
gWaveProgression = (SHORT)(pScript->rgwOperand[1]);
break;
case 0x0073:
// Fade the screen to scene
VIDEO_BackupScreen(gpScreen);
PAL_MakeScene();
VIDEO_FadeScreen(pScript->rgwOperand[0]);
break;
case 0x0074:
// Jump if not all players are full HP
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
if (PLAYER.rgwHP[w] < PLAYER.rgwMaxHP[w]) {
wScriptEntry = pScript->rgwOperand[0] - 1;
break;
}
}
break;
case 0x0075:
// Set the player party
g.wMaxPartyMemberIndex = 0;
for (int i = 0; i < 3; i++) {
if (pScript->rgwOperand[i] != 0) {
PARTY_PLAYER(g.wMaxPartyMemberIndex) = pScript->rgwOperand[i] - 1;
g.wMaxPartyMemberIndex++;
}
}
g.wMaxPartyMemberIndex--;
// Reload the player sprites
PAL_SetLoadFlags(kLoadPlayerSprite);
PAL_LoadResources();
memset(g.rgPoisonStatus, 0, sizeof(g.rgPoisonStatus));
PAL_UpdateEquipmentEffect();
break;
case 0x0076:
// Show FBP picture
VIDEO_FillScreenBlack();
VIDEO_UpdateScreen(NULL);
break;
case 0x0077:
// Stop current playing music
AUDIO_PlayMusic(0, FALSE, (pScript->rgwOperand[0] == 0) ? 2.0f : (FLOAT)(pScript->rgwOperand[0]) * 3);
g.wCurMusic = 0;
break;
case 0x0078:
// 战后返回地图
break;
case 0x0079:
// Jump if the specified player is in the party
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
if (PLAYER.rgwName[PARTY_PLAYER(i)] == pScript->rgwOperand[0]) {
wScriptEntry = pScript->rgwOperand[1] - 1;
break;
}
}
break;
case 0x007A:
// Walk the party to the specified position, at a higher speed
PAL_PartyWalkTo(pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 4);
break;
case 0x007B:
// Walk the party to the specified position, at the highest speed
PAL_PartyWalkTo(pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 8);
break;
case 0x007C:
// Walk straight to the specified position
if ((wEventObjectID & 1) ^ (gFrameNum & 1)) {
if (!PAL_NPCWalkTo(wEventObjectID, pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2], 4))
wScriptEntry--;
} else {
wScriptEntry--;
}
break;
case 0x007D:
// Move the event object
pCurrent->x += (SHORT)(pScript->rgwOperand[1]);
pCurrent->y += (SHORT)(pScript->rgwOperand[2]);
break;
case 0x007E:
// Set the layer of event object
pCurrent->sLayer = (SHORT)(pScript->rgwOperand[1]);
break;
case 0x007F:
// Move the viewport
PAL_MoveViewport(pScript->rgwOperand[0], pScript->rgwOperand[1], pScript->rgwOperand[2]);
break;
case 0x0080:
// Toggle day/night palette
gNightPalette = !(gNightPalette);
PAL_PaletteFade(gCurPalette, gNightPalette, !(pScript->rgwOperand[0]));
break;
case 0x0081: {
// Jump if the player is not facing the specified event object
WORD evtObjID = pScript->rgwOperand[0];
WORD dist = pScript->rgwOperand[1];
if (evtObjID <= SCENE_EVENT_OBJ_IDX_ST || evtObjID > SCENE_EVENT_OBJ_IDX_ED) {
// The event object is not in the current scene
wScriptEntry = pScript->rgwOperand[2] - 1;
g_fScriptSuccess = FALSE;
break;
}
int x = pCurrent->x;