forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfight.c
More file actions
2678 lines (2178 loc) · 87 KB
/
fight.c
File metadata and controls
2678 lines (2178 loc) · 87 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/>.
//
#include "main.h"
#include <math.h>
extern WORD g_rgPlayerPos[3][3][2];
BOOL PAL_IsPlayerDying(WORD wPlayerRole) {
return PLAYER.rgwHP[wPlayerRole] < min(100, PLAYER.rgwMaxHP[wPlayerRole] / 5);
}
BOOL PAL_IsPlayerHealthy(WORD wPlayerRole) {
return !PAL_IsPlayerDying(wPlayerRole) && gPlayerStatus[wPlayerRole][kStatusSleep] == 0 &&
gPlayerStatus[wPlayerRole][kStatusConfused] == 0 && gPlayerStatus[wPlayerRole][kStatusSilence] == 0 &&
gPlayerStatus[wPlayerRole][kStatusParalyzed] == 0 && gPlayerStatus[wPlayerRole][kStatusPuppet] == 0;
}
static BOOL PAL_CanPlayerAct(WORD wPlayerRole, BOOL fConsiderConfused) {
return PLAYER.rgwHP[wPlayerRole] != 0 && gPlayerStatus[wPlayerRole][kStatusSleep] == 0 &&
gPlayerStatus[wPlayerRole][kStatusParalyzed] == 0 &&
(!fConsiderConfused || gPlayerStatus[wPlayerRole][kStatusConfused] == 0);
}
static BOOL PAL_isPlayerBadStatus(WORD wPlayerRole) {
return gPlayerStatus[wPlayerRole][kStatusConfused] > 0 || gPlayerStatus[wPlayerRole][kStatusSleep] > 0 ||
gPlayerStatus[wPlayerRole][kStatusParalyzed] > 0;
}
static INT PAL_BattleSelectAutoTargetFrom(INT begin) {
int prev = g_Battle.UI.iPrevEnemyTarget;
if (prev >= 0 && prev <= g_Battle.wMaxEnemyIndex && BATTLE_ENEMY[prev].wObjectID != 0 &&
BATTLE_ENEMY[prev].e.wHealth > 0) {
return prev;
}
for (int count = 0, i = (begin >= 0 ? begin : 0); count < MAX_ENEMIES_IN_TEAM; count++) {
if (BATTLE_ENEMY[i].wObjectID != 0 && BATTLE_ENEMY[i].e.wHealth > 0)
return i;
i = (i + 1) % (g_Battle.wMaxEnemyIndex + 1);
}
return -1;
}
INT PAL_BattleSelectAutoTarget(WORD wMagic) {
if (wMagic == 0) {
if (PAL_PlayerCanAttackAll(PARTY_PLAYER(g_Battle.UI.wCurPlayerIndex)))
return -1;
} else {
if (OBJECT[wMagic].magic.wFlags & kMagicFlagApplyToAll)
return -1;
}
return PAL_BattleSelectAutoTargetFrom(0);
}
// https://github.com/palxex/palresearch/blob/master/EXE/txts/%E9%9A%8F%E8%AE%B0.txt
static SHORT PAL_CalcBaseDamage(WORD wAttackStrength, WORD wDefense) {
if (wAttackStrength > wDefense)
return (SHORT)((wAttackStrength - wDefense * 0.8) * 2 + 0.5);
else if (wAttackStrength > wDefense * 0.6)
return (SHORT)(wAttackStrength - wDefense * 0.6 + 0.5);
else
return 0;
}
// https://github.com/palxex/palresearch/blob/master/EXE/txts/%E9%9A%8F%E8%AE%B0.txt
SHORT PAL_CalcMagicDamage(WORD wMagicStrength, WORD wDefense, const WORD rgwElementalResistance[NUM_MAGIC_ELEMENTAL],
WORD wPoisonResistance, WORD wResistanceMultiplier, WORD wMagicObject) {
MAGIC_T *magic = &MAGIC[OBJECT[wMagicObject].magic.wMagicNumber];
// 上浮 10 %
wMagicStrength *= RandomFloat(10, 11);
wMagicStrength /= 10;
SHORT sDamage = PAL_CalcBaseDamage(wMagicStrength, wDefense) / 4 + magic->wBaseDamage;
WORD wElem = magic->wElemental;
if (wElem != 0) {
if (wElem > NUM_MAGIC_ELEMENTAL)
sDamage *= 10 - ((FLOAT)wPoisonResistance / wResistanceMultiplier); // 毒抗
else
sDamage *= 10 - ((FLOAT)rgwElementalResistance[wElem - 1] / wResistanceMultiplier); // 五行抗
sDamage /= 5;
if (wElem <= NUM_MAGIC_ELEMENTAL) {
sDamage *= 10 + BATTLEFIELD[g.wCurBattleField].rgsMagicEffect[wElem - 1]; // 战地属性加成
sDamage /= 10;
}
}
return sDamage;
}
static SHORT PAL_CalcPhysicalAttackDamage(WORD wAttackStrength, WORD wDefense, WORD wAttackResistance) {
SHORT sDamage = PAL_CalcBaseDamage(wAttackStrength, wDefense);
if (wAttackResistance != 0)
sDamage /= wAttackResistance;
return sDamage;
}
static SHORT PAL_GetEnemyDexterity(WORD wEnemyIndex) {
return BATTLE_ENEMY[wEnemyIndex].e.wDexterity + (BATTLE_ENEMY[wEnemyIndex].e.wLevel + 6) * 3;
}
static WORD PAL_GetPlayerActualDexterity(WORD wPlayerRole) {
WORD wDexterity = PAL_GetPlayerDexterity(wPlayerRole);
if (gPlayerStatus[wPlayerRole][kStatusHaste] != 0) // 身法
wDexterity *= 3;
if (wDexterity > 999)
wDexterity = 999;
return wDexterity;
}
static VOID PAL_BattleUpdateAllEnemyFrame() {
for (int j = 0; j <= g_Battle.wMaxEnemyIndex; j++) {
BATTLEENEMY *enemy = &BATTLE_ENEMY[j];
if (enemy->wObjectID == 0 || enemy->rgwStatus[kStatusSleep] != 0 || enemy->rgwStatus[kStatusParalyzed] != 0)
continue;
// 切换下一张 idle frame
if (--enemy->e.wIdleAnimSpeed == 0) {
enemy->wCurrentFrame++;
enemy->e.wIdleAnimSpeed = ENEMY[OBJECT[enemy->wObjectID].enemy.wEnemyID].wIdleAnimSpeed;
}
if (enemy->wCurrentFrame >= enemy->e.wIdleFrames)
enemy->wCurrentFrame = 0;
}
}
VOID PAL_BattleDelay(WORD wDuration, WORD wObjectID, BOOL fUpdateGesture) {
DWORD dwTime = PAL_GetTicks() + BATTLE_FRAME_TIME;
for (int i = 0; i < wDuration; i++) {
// Update the gesture of enemies.
if (fUpdateGesture)
PAL_BattleUpdateAllEnemyFrame();
// Wait for the time of one frame. Accept input here.
PAL_DelayUntil(dwTime);
// Set the time of the next frame.
dwTime = PAL_GetTicks() + BATTLE_FRAME_TIME;
PAL_BattleMakeScene();
VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
PAL_BattleUIUpdate();
if (wObjectID != 0) {
if (wObjectID == BATTLE_LABEL_ESCAPEFAIL) // HACKHACK
PAL_DrawText(PAL_GetWord(wObjectID), PAL_XY(130, 75), 15, TRUE, FALSE);
else
PAL_DrawText(PAL_GetWord(wObjectID), PAL_XY(210, 50), 15, TRUE, FALSE);
}
VIDEO_UpdateScreen(NULL);
}
}
// Backup HP and MP values of all players and enemies.
static VOID PAL_BattleBackupStat(VOID) {
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
if (BATTLE_ENEMY[i].wObjectID == 0)
continue;
BATTLE_ENEMY[i].wPrevHP = BATTLE_ENEMY[i].e.wHealth;
}
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD wPlayerRole = PARTY_PLAYER(i);
BATTLE_PLAYER[i].wPrevHP = PLAYER.rgwHP[wPlayerRole];
BATTLE_PLAYER[i].wPrevMP = PLAYER.rgwMP[wPlayerRole];
}
}
// Display the HP and MP changes of all players and enemies.
// Return TRUE if there are any number displayed, FALSE if not.
static BOOL PAL_BattleDisplayStatChange(VOID) {
BOOL f = FALSE;
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
BATTLEENEMY *enemy = &BATTLE_ENEMY[i];
if (enemy->wObjectID == 0)
continue;
if (enemy->wPrevHP != enemy->e.wHealth) {
int x = PAL_X(enemy->pos) - 9;
int y = PAL_Y(enemy->pos) - 115;
if (y < 10)
y = 10;
SHORT sDamage = enemy->e.wHealth - enemy->wPrevHP;
PAL_BattleUIShowNum(abs(sDamage), PAL_XY(x, y), sDamage < 0 ? kNumColorBlue : kNumColorYellow);
f = TRUE;
}
}
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD wPlayerRole = PARTY_PLAYER(i);
BATTLEPLAYER *player = &BATTLE_PLAYER[i];
if (player->wPrevHP != PLAYER.rgwHP[wPlayerRole]) {
int x = PAL_X(player->pos) - 9;
int y = PAL_Y(player->pos) - 75;
if (y < 10)
y = 10;
SHORT sDamage = PLAYER.rgwHP[wPlayerRole] - player->wPrevHP;
PAL_BattleUIShowNum(abs(sDamage), PAL_XY(x, y), sDamage < 0 ? kNumColorBlue : kNumColorYellow);
f = TRUE;
}
if (player->wPrevMP != PLAYER.rgwMP[wPlayerRole]) {
int x = PAL_X(player->pos) - 9;
int y = PAL_Y(player->pos) - 67;
if (y < 10)
y = 10;
SHORT sDamage = PLAYER.rgwMP[wPlayerRole] - player->wPrevMP;
// Only show MP increasing
if (sDamage > 0)
PAL_BattleUIShowNum((WORD)(sDamage), PAL_XY(x, y), kNumColorCyan);
f = TRUE;
}
}
return f;
}
// Essential checks after an action is executed.
static VOID PAL_BattlePostActionCheck(BOOL fCheckPlayers) {
BOOL fFade = FALSE;
BOOL fEnemyRemaining = FALSE;
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
BATTLEENEMY *enemy = &BATTLE_ENEMY[i];
if (enemy->wObjectID == 0)
continue;
if ((SHORT)(enemy->e.wHealth) <= 0) {
// This enemy is KO'ed
g_Battle.iExpGained += enemy->e.wExp;
g_Battle.iCashGained += enemy->e.wCash;
AUDIO_PlaySound(enemy->e.wDeathSound);
enemy->wObjectID = 0;
if (enemy->lpSprite)
free(enemy->lpSprite);
enemy->lpSprite = NULL;
fFade = TRUE;
continue;
}
fEnemyRemaining = TRUE;
}
if (!fEnemyRemaining) {
g_Battle.fEnemyCleared = TRUE;
g_Battle.UI.state = kBattleUIWait;
}
if (fCheckPlayers && !gAutoBattle) {
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
// This player is just KO'ed
if (PLAYER.rgwHP[w] < BATTLE_PLAYER[i].wPrevHP && PLAYER.rgwHP[w] == 0) {
WORD wCover = PLAYER.rgwCoveredBy[w];
if (!PAL_CanPlayerAct(wCover, TRUE))
continue;
int j;
for (j = 0; j <= g.wMaxPartyMemberIndex; j++)
if (PARTY_PLAYER(j) == wCover)
break;
if (j > g.wMaxPartyMemberIndex)
continue;
WORD wName = PLAYER.rgwName[wCover];
if (OBJECT[wName].player.wScriptOnFriendDeath != 0) {
PAL_BattleDelay(10, 0, TRUE);
PAL_BattleMakeScene();
VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
VIDEO_UpdateScreen(NULL);
g_Battle.BattleResult = kBattleResultPause;
// 触发友方愤怒
OBJECT[wName].player.wScriptOnFriendDeath =
PAL_RunTriggerScript(OBJECT[wName].player.wScriptOnFriendDeath, wCover);
g_Battle.BattleResult = kBattleResultOnGoing;
PAL_ClearKeyState();
goto end;
}
}
}
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD w = PARTY_PLAYER(i);
if (gPlayerStatus[w][kStatusSleep] != 0 || gPlayerStatus[w][kStatusConfused] != 0)
continue;
if (PLAYER.rgwHP[w] < BATTLE_PLAYER[i].wPrevHP && PLAYER.rgwHP[w] > 0 && PAL_IsPlayerDying(w) &&
BATTLE_PLAYER[i].wPrevHP >= PLAYER.rgwMaxHP[w] / 5) {
WORD wCover = PLAYER.rgwCoveredBy[w];
if (!PAL_CanPlayerAct(wCover, TRUE))
continue;
int j;
for (j = 0; j <= g.wMaxPartyMemberIndex; j++)
if (PARTY_PLAYER(j) == wCover)
break;
if (j > g.wMaxPartyMemberIndex)
continue;
WORD wName = PLAYER.rgwName[w];
AUDIO_PlaySound(PLAYER.rgwDyingSound[w]);
if (OBJECT[wName].player.wScriptOnDying != 0) {
PAL_BattleDelay(10, 0, TRUE);
PAL_BattleMakeScene();
VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
VIDEO_UpdateScreen(NULL);
g_Battle.BattleResult = kBattleResultPause;
// 触发自己虚弱
OBJECT[wName].player.wScriptOnDying = PAL_RunTriggerScript(OBJECT[wName].player.wScriptOnDying, w);
g_Battle.BattleResult = kBattleResultOnGoing;
PAL_ClearKeyState();
}
goto end;
}
}
}
end:
if (fFade) {
VIDEO_BackupScreen(g_Battle.lpSceneBuf);
PAL_BattleMakeScene();
PAL_BattleFadeScene();
}
// Fade out the summoned god
if (g_Battle.lpSummonSprite != NULL) {
PAL_BattleUpdateFighters();
PAL_BattleDelay(1, 0, FALSE);
free(g_Battle.lpSummonSprite);
g_Battle.lpSummonSprite = NULL;
g_Battle.sBackgroundColorShift = 0;
VIDEO_BackupScreen(g_Battle.lpSceneBuf);
PAL_BattleMakeScene();
PAL_BattleFadeScene();
}
}
// Update players' and enemies' gesture frames and locations in battle.
VOID PAL_BattleUpdateFighters(VOID) {
// Update the gesture frame for all players
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD wPlayerRole = PARTY_PLAYER(i);
BATTLEPLAYER *player = &BATTLE_PLAYER[i];
if (!player->fDefending)
player->pos = player->posOriginal;
player->iColorShift = 0;
if (PLAYER.rgwHP[wPlayerRole] == 0) {
if (gPlayerStatus[wPlayerRole][kStatusPuppet] == 0)
player->wCurrentFrame = 2; // dead
else
player->wCurrentFrame = 0; // puppet
} else {
if (gPlayerStatus[wPlayerRole][kStatusSleep] != 0 || PAL_IsPlayerDying(wPlayerRole))
player->wCurrentFrame = 1;
else if (player->fDefending && !g_Battle.fEnemyCleared)
player->wCurrentFrame = 3;
else
player->wCurrentFrame = 0;
}
}
// Reset the pos / color / frame for all enemies
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
BATTLEENEMY *enemy = &BATTLE_ENEMY[i];
if (enemy->wObjectID == 0)
continue;
enemy->pos = enemy->posOriginal;
enemy->iColorShift = 0;
if (enemy->rgwStatus[kStatusSleep] > 0 || enemy->rgwStatus[kStatusParalyzed] > 0) {
enemy->wCurrentFrame = 0;
}
}
// Update the gesture frame for all enemies
PAL_BattleUpdateAllEnemyFrame();
}
static WORD PAL_getPlayerDexterity(int i) {
WORD wPlayerRole = PARTY_PLAYER(i);
WORD wDexterity = PAL_GetPlayerActualDexterity(wPlayerRole);
switch (BATTLE_PLAYER[i].action.ActionType) {
case kBattleActionCoopMagic:
wDexterity *= 10;
break;
case kBattleActionDefend:
wDexterity *= 5;
break;
case kBattleActionMagic:
if ((OBJECT[BATTLE_PLAYER[i].action.wActionID].magic.wFlags & kMagicFlagUsableToEnemy) == 0) // 对友方施法
wDexterity *= 3;
break;
case kBattleActionFlee:
wDexterity /= 2;
break;
case kBattleActionUseItem:
wDexterity *= 3;
break;
default:
break;
}
if (PAL_IsPlayerDying(wPlayerRole))
wDexterity /= 2;
return wDexterity;
}
VOID PAL_BattlePlayerCheckReady(VOID) {
// 有人处于指令状态,就调用 UI 显示行动选择菜单, 一次只处理一个人
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
if (BATTLE_PLAYER[i].state == kFighterCom) {
PAL_BattleUIPlayerReady(i);
break;
}
}
}
// 在战斗界面的每一帧被调用,负责更新画面、检测胜负、处理玩家输入(选菜单)、计算行动顺序(速度)、以及执行具体的攻击或法术逻辑。
VOID PAL_BattleStartFrame(VOID) {
if (!g_Battle.fEnemyCleared)
PAL_BattleUpdateFighters();
// Update the scene
PAL_BattleMakeScene();
VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
// If all enemies are cleared. Won the battle.
if (g_Battle.fEnemyCleared) {
g_Battle.BattleResult = kBattleResultWon;
AUDIO_PlaySound(0);
return;
}
// If all players are dead. Lost the battle.
BOOL fEnded = TRUE;
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD wPlayerRole = PARTY_PLAYER(i);
if (PLAYER.rgwHP[wPlayerRole] != 0) {
fEnded = FALSE;
break;
}
}
if (fEnded) {
g_Battle.BattleResult = kBattleResultLost;
return;
}
if (g_Battle.Phase == kBattlePhaseSelectAction) {
if (g_Battle.UI.state == kBattleUIWait) {
int idx;
for (idx = 0; idx <= g.wMaxPartyMemberIndex; idx++) {
WORD wPlayerRole = PARTY_PLAYER(idx);
// Don't select action for this player cannot act
if (!PAL_CanPlayerAct(wPlayerRole, TRUE))
continue;
// Start the menu for the first player whose action is not yet selected.
if (BATTLE_PLAYER[idx].state == kFighterWait) {
g_Battle.wMovingPlayerIndex = idx;
BATTLE_PLAYER[idx].state = kFighterCom;
PAL_BattleUIPlayerReady(idx);
break;
} else if (BATTLE_PLAYER[idx].action.ActionType == kBattleActionCoopMagic) {
// 如果有人选择了合体技, 这通常消耗所有人的回合, 因此跳过后续队员的指令选择
idx = g.wMaxPartyMemberIndex + 1;
break;
}
}
// 所有角色的指令都下达完毕, 进入回合结算准备
if (idx > g.wMaxPartyMemberIndex) {
// Backup all actions once not repeating.
if (!g_Battle.fRepeat) {
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++)
BATTLE_PLAYER[i].prevAction = BATTLE_PLAYER[i].action;
}
// Actions for all players are decided, fill in the action queue.
g_Battle.fRepeat = FALSE;
g_Battle.fForce = FALSE;
g_Battle.fFlee = FALSE;
g_Battle.fPrevAutoAtk = g_Battle.UI.fAutoAttack;
g_Battle.fPrevPlayerAutoAtk = FALSE;
g_Battle.iCurAction = 0;
// Reset queue.
for (int i = 0; i < MAX_ACTIONQUEUE_ITEMS; i++) {
g_Battle.ActionQueue[i].wIndex = 0xFFFF;
g_Battle.ActionQueue[i].fIsSecond = FALSE;
g_Battle.ActionQueue[i].wDexterity = 0xFFFF;
}
int j = 0; // index in queue
// Put all enemies into action queue.
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
if (BATTLE_ENEMY[i].wObjectID == 0)
continue;
g_Battle.ActionQueue[j].fIsEnemy = TRUE;
g_Battle.ActionQueue[j].wIndex = i;
g_Battle.ActionQueue[j].fIsSecond = FALSE;
g_Battle.ActionQueue[j].wDexterity = PAL_GetEnemyDexterity(i) * RandomFloat(0.9f, 1.1f);
j++;
if (BATTLE_ENEMY[i].e.wDualMove) {
g_Battle.ActionQueue[j].fIsEnemy = TRUE;
g_Battle.ActionQueue[j].wIndex = i;
g_Battle.ActionQueue[j].fIsSecond = FALSE;
g_Battle.ActionQueue[j].wDexterity = PAL_GetEnemyDexterity(i) * RandomFloat(0.9f, 1.1f);
// 比较两次攻击的身法, 强制让身法较低 (较慢) 的那次攻击标记为第二次
if (g_Battle.ActionQueue[j].wDexterity <= g_Battle.ActionQueue[j - 1].wDexterity)
g_Battle.ActionQueue[j].fIsSecond = TRUE;
else
g_Battle.ActionQueue[j - 1].fIsSecond = TRUE;
j++;
}
}
// Put all players into action queue.
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD wPlayerRole = PARTY_PLAYER(i);
g_Battle.ActionQueue[j].fIsEnemy = FALSE;
g_Battle.ActionQueue[j].wIndex = i;
if (!PAL_CanPlayerAct(wPlayerRole, FALSE)) {
// players who are unable to move should attack physically if recovered in the same turn
g_Battle.ActionQueue[j].wDexterity = 0;
BATTLE_PLAYER[i].action.ActionType = kBattleActionAttack;
BATTLE_PLAYER[i].action.wActionID = 0;
BATTLE_PLAYER[i].state = kFighterAct;
} else {
// 混乱状态强制改为普攻
if (gPlayerStatus[wPlayerRole][kStatusConfused] > 0) {
BATTLE_PLAYER[i].action.ActionType = kBattleActionAttack;
BATTLE_PLAYER[i].action.wActionID = 0; // avoid be deduced to autoattack
BATTLE_PLAYER[i].state = kFighterAct;
}
g_Battle.ActionQueue[j].wDexterity = PAL_getPlayerDexterity(i) * RandomFloat(0.9f, 1.1f);
}
j++;
}
// Sort the action queue by dexterity value
for (int i = 0; i < MAX_ACTIONQUEUE_ITEMS; i++) {
for (int j = i; j < MAX_ACTIONQUEUE_ITEMS; j++) {
if ((SHORT)g_Battle.ActionQueue[i].wDexterity < (SHORT)g_Battle.ActionQueue[j].wDexterity) {
ACTIONQUEUE t = g_Battle.ActionQueue[i];
g_Battle.ActionQueue[i] = g_Battle.ActionQueue[j];
g_Battle.ActionQueue[j] = t;
}
}
}
// Perform the actions
g_Battle.Phase = kBattlePhasePerformAction;
}
}
} else {
// Are all actions finished?
if (g_Battle.iCurAction >= MAX_ACTIONQUEUE_ITEMS ||
g_Battle.ActionQueue[g_Battle.iCurAction].wDexterity == 0xFFFF) {
// Restore player from defending
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
BATTLE_PLAYER[i].fDefending = FALSE;
BATTLE_PLAYER[i].pos = BATTLE_PLAYER[i].posOriginal;
}
// 执行中毒脚本
PAL_BattleBackupStat();
for (int i = 0; i <= g.wMaxPartyMemberIndex; i++) {
WORD wPlayerRole = PARTY_PLAYER(i);
for (int j = 0; j < MAX_POISONS; j++) {
if (g.rgPoisonStatus[j][i].wPoisonID != 0) {
g.rgPoisonStatus[j][i].wPoisonScript =
PAL_RunTriggerScript(g.rgPoisonStatus[j][i].wPoisonScript, wPlayerRole);
}
}
// 递减状态持续回合数
for (int j = 0; j < kStatusAll; j++)
if (gPlayerStatus[wPlayerRole][j] > 0)
gPlayerStatus[wPlayerRole][j]--;
}
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
for (int j = 0; j < MAX_POISONS; j++) {
if (BATTLE_ENEMY[i].rgPoisons[j].wPoisonID != 0) {
BATTLE_ENEMY[i].rgPoisons[j].wPoisonScript =
PAL_RunTriggerScript(BATTLE_ENEMY[i].rgPoisons[j].wPoisonScript, (WORD)i);
}
}
// 递减状态持续回合数
for (int j = 0; j < kStatusAll; j++)
if (BATTLE_ENEMY[i].rgwStatus[j] > 0)
BATTLE_ENEMY[i].rgwStatus[j]--;
}
PAL_BattlePostActionCheck(FALSE);
if (PAL_BattleDisplayStatChange())
PAL_BattleDelay(8, 0, TRUE);
// 更新隐身剩余回合
if (g_Battle.iHidingTime > 0) {
if (--g_Battle.iHidingTime == 0) {
VIDEO_BackupScreen(g_Battle.lpSceneBuf);
PAL_BattleMakeScene();
PAL_BattleFadeScene();
}
}
// 运行敌方回合开始脚本
if (g_Battle.iHidingTime == 0) {
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++) {
if (BATTLE_ENEMY[i].wObjectID == 0)
continue;
BATTLE_ENEMY[i].wScriptOnTurnStart = PAL_RunTriggerScript(BATTLE_ENEMY[i].wScriptOnTurnStart, i);
}
}
// Clear all item-using records
for (int i = 0; i < MAX_INVENTORY; i++)
g.rgInventory[i].nAmountInUse = 0;
// Proceed to next turn...
g_Battle.Phase = kBattlePhaseSelectAction;
g_Battle.fThisTurnCoop = FALSE;
} else {
// Perform action in queue.
int idx = g_Battle.ActionQueue[g_Battle.iCurAction].wIndex;
BATTLEENEMY *enemy = &BATTLE_ENEMY[idx];
if (g_Battle.ActionQueue[g_Battle.iCurAction].fIsEnemy) {
// 敌人行动
if (g_Battle.iHidingTime == 0 && BATTLE_ENEMY[idx].wObjectID != 0) {
if (enemy->rgwStatus[kStatusConfused] == 0 && enemy->rgwStatus[kStatusParalyzed] == 0 &&
enemy->rgwStatus[kStatusSleep] == 0)
enemy->wScriptOnReady = PAL_RunTriggerScript(enemy->wScriptOnReady, idx);
g_Battle.fEnemyMoving = TRUE;
PAL_BattleEnemyPerformAction(idx);
g_Battle.fEnemyMoving = FALSE;
}
} else if (BATTLE_PLAYER[idx].state == kFighterAct) {
// 玩家行动
BATTLEPLAYER *player = &BATTLE_PLAYER[idx];
WORD wPlayerRole = PARTY_PLAYER(idx);
// 行动前再次检查状态, 防止中间状态改变
if (PLAYER.rgwHP[wPlayerRole] == 0) {
if (gPlayerStatus[wPlayerRole][kStatusPuppet] == 0)
player->action.ActionType = kBattleActionPass;
} else if (gPlayerStatus[wPlayerRole][kStatusSleep] > 0 || gPlayerStatus[wPlayerRole][kStatusParalyzed] > 0) {
player->action.ActionType = kBattleActionPass;
} else if (gPlayerStatus[wPlayerRole][kStatusConfused] > 0) {
player->action.ActionType = (PAL_IsPlayerDying(wPlayerRole) ? kBattleActionPass : kBattleActionAttackMate);
} else if (player->action.ActionType == kBattleActionAttack && player->action.wActionID != 0) {
// ref: PAL_BattleCommitAction
g_Battle.fPrevPlayerAutoAtk = TRUE;
} else if (g_Battle.fPrevPlayerAutoAtk) {
// 本回合有人选择围攻, 强制转换为普通攻击
g_Battle.UI.wCurPlayerIndex = idx;
g_Battle.UI.iSelectedIndex = player->action.sTarget;
g_Battle.UI.wActionType = kBattleActionAttack;
PAL_BattleCommitAction(FALSE);
}
// Perform the action for this player.
g_Battle.wMovingPlayerIndex = idx;
PAL_BattlePlayerPerformAction(idx);
}
g_Battle.iCurAction++;
}
}
// The R and F keys and Fleeing should affect all players
if (g_Battle.UI.MenuState == kBattleMenuMain && g_Battle.UI.state == kBattleUISelectMove) {
if (g_InputState.dwKeyPress & kKeyRepeat) {
g_Battle.fRepeat = TRUE;
g_Battle.UI.fAutoAttack = g_Battle.fPrevAutoAtk;
} else if (g_InputState.dwKeyPress & kKeyForce) {
g_Battle.fForce = TRUE;
}
}
if (g_Battle.fRepeat)
g_InputState.dwKeyPress = kKeyRepeat;
else if (g_Battle.fForce)
g_InputState.dwKeyPress = kKeyForce;
else if (g_Battle.fFlee)
g_InputState.dwKeyPress = kKeyFlee;
// Update the battle UI
PAL_BattleUIUpdate();
}
// Commit the action which the player decided.
VOID PAL_BattleCommitAction(BOOL fRepeat) {
BATTLEPLAYER *player = &BATTLE_PLAYER[g_Battle.UI.wCurPlayerIndex];
if (!fRepeat) {
// clear action cache first; avoid cache pollution
memset(&player->action, 0, sizeof(player->action));
player->action.ActionType = g_Battle.UI.wActionType;
player->action.sTarget = (SHORT)g_Battle.UI.iSelectedIndex;
if (g_Battle.UI.wActionType == kBattleActionAttack)
player->action.wActionID = (g_Battle.UI.fAutoAttack ? 1 : 0);
else
player->action.wActionID = g_Battle.UI.wObjectID;
} else {
// 重复上回合的动作
SHORT target = player->action.sTarget;
player->action = player->prevAction;
player->action.sTarget = target;
if (player->action.ActionType == kBattleActionPass) {
player->action.ActionType = kBattleActionAttack;
player->action.wActionID = 0;
player->action.sTarget = -1;
}
}
// 有效性检查与资源预扣除
if (player->action.ActionType == kBattleActionMagic) {
MAGIC_T *magic = &MAGIC[OBJECT[player->action.wActionID].magic.wMagicNumber];
if (PLAYER.rgwMP[PARTY_PLAYER(g_Battle.UI.wCurPlayerIndex)] < magic->wCostMP) {
WORD type = magic->wType;
// MP 不足降级处理
if (type == kMagicTypeApplyToPlayer || type == kMagicTypeApplyToParty || type == kMagicTypeTrance) {
player->action.ActionType = kBattleActionDefend;
} else {
player->action.ActionType = kBattleActionAttack;
if (player->action.sTarget == -1)
player->action.sTarget = 0;
player->action.wActionID = 0;
}
}
} else if ((player->action.ActionType == kBattleActionUseItem &&
(OBJECT[player->action.wActionID].item.wFlags & kItemFlagConsuming)) ||
player->action.ActionType == kBattleActionThrowItem) {
for (WORD w = 0; w < MAX_INVENTORY; w++) {
if (g.rgInventory[w].wItem == player->action.wActionID) {
g.rgInventory[w].nAmountInUse++; // 标记物品被占用
break;
}
}
}
// 如果动作是逃跑,设置全局战斗标志
if (g_Battle.UI.wActionType == kBattleActionFlee)
g_Battle.fFlee = TRUE;
player->state = kFighterAct;
g_Battle.UI.state = kBattleUIWait;
}
// Show the physical attack effect for player.
static VOID PAL_BattleShowPlayerAttackAnim(WORD wPlayerIndex, BOOL fCritical) {
WORD wPlayerRole = PARTY_PLAYER(wPlayerIndex);
BATTLEPLAYER *player = &BATTLE_PLAYER[wPlayerIndex];
SHORT sTarget = player->action.sTarget;
int enemy_x = 0, enemy_y = 0, enemy_h = 0, dist = 0;
if (sTarget != -1) {
// 单体攻击,获取目标敌人的坐标、高度
enemy_x = PAL_X(BATTLE_ENEMY[sTarget].pos);
enemy_y = PAL_Y(BATTLE_ENEMY[sTarget].pos);
enemy_h = PAL_RLEGetHeight(PAL_SpriteGetFrame(BATTLE_ENEMY[sTarget].lpSprite, BATTLE_ENEMY[sTarget].wCurrentFrame));
// 目标在后排,计算偏移量
if (sTarget >= 3)
dist = (sTarget - wPlayerIndex) * 8;
} else {
// 全体攻击默认中心点
enemy_x = 150;
enemy_y = 100;
}
// Play the attack voice
if (PLAYER.rgwHP[wPlayerRole] > 0) {
if (!fCritical)
AUDIO_PlaySound(PLAYER.rgwAttackSound[wPlayerRole]);
else
AUDIO_PlaySound(PLAYER.rgwCriticalSound[wPlayerRole]);
}
// Show the animation
int x = enemy_x - dist + 64;
int y = enemy_y + dist + 20;
// 抬手近身
player->wCurrentFrame = 8;
if (gPlayerStatus[wPlayerRole][kStatusDualAttack] > 0 && PAL_PlayerCanAttackAll(wPlayerRole)) {
// 原地
x = g_rgPlayerPos[g.wMaxPartyMemberIndex][wPlayerIndex][0] - 8;
y = g_rgPlayerPos[g.wMaxPartyMemberIndex][wPlayerIndex][1] - 4;
player->pos = player->fSecondAttack ? PAL_XY(x - 12, y - 8) : PAL_XY(x, y);
} else {
player->pos = PAL_XY(x, y);
}
PAL_BattleDelay(2, 0, TRUE);
x -= 10;
y -= 2;
if (gPlayerStatus[wPlayerRole][kStatusDualAttack] > 0 && PAL_PlayerCanAttackAll(wPlayerRole)) {
// 原地
x = g_rgPlayerPos[g.wMaxPartyMemberIndex][wPlayerIndex][0] - 8;
y = g_rgPlayerPos[g.wMaxPartyMemberIndex][wPlayerIndex][1] - 4;
player->pos = player->fSecondAttack ? PAL_XY(x - 12, y - 8) : PAL_XY(x, y);
} else {
player->pos = PAL_XY(x, y);
}
PAL_BattleDelay(1, 0, TRUE);
// 攻击动作
player->wCurrentFrame = 9;
x -= 16;
y -= 4;
AUDIO_PlaySound(PLAYER.rgwWeaponSound[wPlayerRole]);
x = enemy_x;
y = enemy_y - enemy_h / 3 + 10;
DWORD dwTime = PAL_GetTicks() + BATTLE_FRAME_TIME;
int index = BATTLE_EFFECT[PAL_GetPlayerBattleSprite(wPlayerRole)][1] * 3;
for (int i = 0; i < 3; i++) {
LPCBITMAPRLE b = PAL_SpriteGetFrame(g_Battle.lpEffectSprite, index++); // 攻击特效
// Wait for the time of one frame. Accept input here.
PAL_DelayUntil(dwTime);
// Set the time of the next frame.
dwTime = PAL_GetTicks() + BATTLE_FRAME_TIME;
// Update the gesture of enemies.
PAL_BattleUpdateAllEnemyFrame();
PAL_BattleMakeScene();
VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
if (PAL_PlayerCanAttackAll(wPlayerRole)) {
for (int j = 0; j < MAX_ENEMIES_IN_TEAM; j++) {
if (BATTLE_ENEMY[j].wObjectID != 0) {
x = ENEMY_POS.pos[j][g_Battle.wMaxEnemyIndex].x;
y = ENEMY_POS.pos[j][g_Battle.wMaxEnemyIndex].y;
y += BATTLE_ENEMY[j].e.wYPosOffset;
PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
}
}
} else {
PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
}
x -= 16;
y += 16;
PAL_BattleUIUpdate();
// 后两帧敌人受击高亮
if (i == 0) {
if (sTarget == -1) {
for (int j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
BATTLE_ENEMY[j].iColorShift = 6;
} else {
BATTLE_ENEMY[sTarget].iColorShift = 6;
}
PAL_BattleDisplayStatChange();
PAL_BattleBackupStat();
}
VIDEO_UpdateScreen(NULL);
// 最后一帧小撤步
if (i == 1)
player->pos = PAL_XY_OFFSET(player->pos, 2, 1);
}
dist = 8;
for (int i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
BATTLE_ENEMY[i].iColorShift = 0;
// 击退效果
if (sTarget == -1) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
BATTLE_ENEMY[j].pos = PAL_XY_OFFSET(BATTLE_ENEMY[j].pos, -dist, 0);
PAL_BattleDelay(1, 0, TRUE);
dist /= -2;
}
} else {
for (int i = 0; i < 3; i++) {
BATTLE_ENEMY[sTarget].pos = PAL_XY_OFFSET(BATTLE_ENEMY[sTarget].pos, -dist, dist / (-2));
PAL_BattleDelay(1, 0, TRUE);
dist /= -2;
}
}
}
static VOID PAL_BattleShowPlayerUseItemAnim(WORD wPlayerIndex, WORD wObjectID, SHORT sTarget) {
PAL_BattleDelay(4, 0, TRUE);
BATTLE_PLAYER[wPlayerIndex].pos = PAL_XY_OFFSET(BATTLE_PLAYER[wPlayerIndex].pos, -15, -7);
BATTLE_PLAYER[wPlayerIndex].wCurrentFrame = 5;
AUDIO_PlaySound(28);
// 渐亮
for (int i = 0; i <= 6; i++) {
if (sTarget == -1) {
for (int j = 0; j <= g.wMaxPartyMemberIndex; j++)
BATTLE_PLAYER[j].iColorShift = i;
} else {
BATTLE_PLAYER[sTarget].iColorShift = i;
}
PAL_BattleDelay(1, wObjectID, TRUE);
}
// 渐暗
for (int i = 5; i >= 0; i--) {
if (sTarget == -1) {
for (int j = 0; j <= g.wMaxPartyMemberIndex; j++)
BATTLE_PLAYER[j].iColorShift = i;
} else {
BATTLE_PLAYER[sTarget].iColorShift = i;
}
PAL_BattleDelay(1, wObjectID, TRUE);
}
}
// Show the effect for player before using a magic.
VOID PAL_BattleShowPlayerPreMagicAnim(WORD wPlayerIndex, BOOL fSummon) {
WORD wPlayerRole = PARTY_PLAYER(wPlayerIndex);
// 前进几步