forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscene_battle_rpg2k.cpp
1901 lines (1585 loc) · 53.1 KB
/
scene_battle_rpg2k.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
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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.
*
* EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <sstream>
#include "input.h"
#include "player.h"
#include "sprite.h"
#include "sprite_enemy.h"
#include "game_battler.h"
#include "game_system.h"
#include "game_party.h"
#include "game_enemy.h"
#include "game_enemyparty.h"
#include "game_message.h"
#include "game_battle.h"
#include "game_battlealgorithm.h"
#include "game_screen.h"
#include "battle_animation.h"
#include <lcf/reader_util.h>
#include "scene_battle_rpg2k.h"
#include "scene_battle.h"
#include "scene_gameover.h"
#include "game_interpreter_battle.h"
#include "output.h"
#include "rand.h"
#include "autobattle.h"
#include "enemyai.h"
#include "battle_message.h"
Scene_Battle_Rpg2k::Scene_Battle_Rpg2k(const BattleArgs& args) :
Scene_Battle(args)
{
}
Scene_Battle_Rpg2k::~Scene_Battle_Rpg2k() {
}
void Scene_Battle_Rpg2k::Start() {
Scene_Battle::Start();
CreateEnemySprites();
}
void Scene_Battle_Rpg2k::CreateUi() {
Scene_Battle::CreateUi();
status_window.reset(new Window_BattleStatus(0, (SCREEN_TARGET_HEIGHT-80), SCREEN_TARGET_WIDTH - option_command_mov, 80));
CreateBattleTargetWindow();
CreateBattleCommandWindow();
battle_message_window.reset(new Window_BattleMessage(0, (SCREEN_TARGET_HEIGHT - 80), SCREEN_TARGET_WIDTH, 80));
if (!IsEscapeAllowed()) {
auto it = std::find(battle_options.begin(), battle_options.end(), Escape);
if (it != battle_options.end()) {
options_window->DisableItem(std::distance(battle_options.begin(), it));
}
}
SetCommandWindows(0);
ResetWindows(true);
battle_message_window->SetVisible(true);
}
void Scene_Battle_Rpg2k::CreateEnemySprites() {
for (auto* enemy: Main_Data::game_enemyparty->GetEnemies()) {
auto sprite = std::make_unique<Sprite_Enemy>(enemy);
sprite->SetVisible(true);
enemy->SetBattleSprite(std::move(sprite));
}
}
static std::vector<std::string> GetEnemyTargetNames() {
std::vector<std::string> commands;
std::vector<Game_Battler*> enemies;
Main_Data::game_enemyparty->GetActiveBattlers(enemies);
for (auto& enemy: enemies) {
commands.push_back(ToString(enemy->GetName()));
}
return commands;
}
void Scene_Battle_Rpg2k::CreateBattleTargetWindow() {
auto commands = GetEnemyTargetNames();
target_window.reset(new Window_Command(std::move(commands), 136, 4));
target_window->SetHeight(80);
target_window->SetY(SCREEN_TARGET_HEIGHT-80);
// Above other windows
target_window->SetZ(Priority_Window + 10);
}
void Scene_Battle_Rpg2k::RefreshTargetWindow() {
auto commands = GetEnemyTargetNames();
target_window->ReplaceCommands(std::move(commands));
}
void Scene_Battle_Rpg2k::CreateBattleCommandWindow() {
std::vector<std::string> commands = {
ToString(lcf::Data::terms.command_attack),
ToString(lcf::Data::terms.command_skill),
ToString(lcf::Data::terms.command_defend),
ToString(lcf::Data::terms.command_item)
};
command_window.reset(new Window_Command(std::move(commands), 76));
command_window->SetHeight(80);
command_window->SetX(SCREEN_TARGET_WIDTH - option_command_mov);
command_window->SetY(SCREEN_TARGET_HEIGHT-80);
}
void Scene_Battle_Rpg2k::RefreshCommandWindow() {
command_window->SetItemText(1, active_actor->GetSkillName());
}
void Scene_Battle_Rpg2k::SetState(Scene_Battle::State new_state) {
previous_state = state;
state = new_state;
SetSceneActionSubState(0);
}
bool Scene_Battle_Rpg2k::UpdateBattleState() {
if (resume_from_debug_scene) {
resume_from_debug_scene = false;
return true;
}
UpdateScreen();
UpdateBattlers();
UpdateUi();
battle_message_window->Update();
if (!UpdateEvents()) {
return false;
}
if (!UpdateTimers()) {
return false;
}
if (Input::IsTriggered(Input::DEBUG_MENU)) {
if (this->CallDebug()) {
// Set this flag so that when we return and run update again, we resume exactly from after this point.
resume_from_debug_scene = true;
return false;
}
}
return true;
}
void Scene_Battle_Rpg2k::Update() {
const auto process_scene = UpdateBattleState();
while (process_scene) {
// Something ended the battle.
if (Scene::instance.get() != this) {
break;
}
if (IsWindowMoving()) {
break;
}
if (Game_Message::IsMessageActive() || Game_Battle::GetInterpreter().IsRunning()) {
break;
}
if (!CheckWait()) {
break;
}
if (ProcessSceneAction() == SceneActionReturn::eWaitTillNextFrame) {
break;
}
}
Game_Battle::UpdateGraphics();
}
void Scene_Battle_Rpg2k::SetSceneActionSubState(int substate) {
scene_action_substate = substate;
}
void Scene_Battle_Rpg2k::NextTurn() {
Main_Data::game_party->IncTurns();
Game_Battle::GetInterpreterBattle().ResetPagesExecuted();
}
bool Scene_Battle_Rpg2k::CheckBattleEndAndScheduleEvents() {
if (CheckBattleEndConditions()) {
return false;
}
auto& interp = Game_Battle::GetInterpreterBattle();
int page = interp.ScheduleNextPage(nullptr);
#ifdef EP_DEBUG_BATTLE2K_STATE_MACHINE
if (page) {
Output::Debug("Battle2k ScheduleNextEventPage Scheduled Page {} frame={}", page, Main_Data::game_system->GetFrameCounter());
} else {
Output::Debug("Battle2k ScheduleNextEventPage No Events to Run frame={}", Main_Data::game_system->GetFrameCounter());
}
#else
(void)page;
#endif
return !interp.IsRunning();
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneAction() {
#ifdef EP_DEBUG_BATTLE2K_STATE_MACHINE
static int last_state = -1;
static int last_substate = -1;
if (state != last_state || scene_action_substate != last_substate) {
Output::Debug("Battle2k ProcessSceneAction({},{}) frames={}", state, scene_action_substate, Main_Data::game_system->GetFrameCounter());
last_state = state;
last_substate = scene_action_substate;
}
#endif
switch (state) {
case State_Start:
return ProcessSceneActionStart();
case State_SelectOption:
return ProcessSceneActionFightAutoEscape();
case State_SelectActor:
return ProcessSceneActionActor();
case State_AutoBattle:
return ProcessSceneActionAutoBattle();
case State_SelectCommand:
return ProcessSceneActionCommand();
case State_SelectItem:
return ProcessSceneActionItem();
case State_SelectSkill:
return ProcessSceneActionSkill();
case State_SelectEnemyTarget:
return ProcessSceneActionEnemyTarget();
case State_SelectAllyTarget:
return ProcessSceneActionAllyTarget();
case State_Battle:
return ProcessSceneActionBattle();
case State_Victory:
return ProcessSceneActionVictory();
case State_Defeat:
return ProcessSceneActionDefeat();
case State_Escape:
return ProcessSceneActionEscape();
}
assert(false && "Invalid SceneActionState!");
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionStart() {
enum SubState {
eBegin,
eDisplayMonsters,
eFirstStrike,
eClear
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
battle_message_window->SetVisible(true);
std::vector<Game_Battler *> visible_enemies;
// First time entered, initialize.
Main_Data::game_enemyparty->GetActiveBattlers(visible_enemies);
for (auto& enemy: visible_enemies) {
// Format and wordwrap all messages, then pull them out and push them back 1 at a time.
battle_message_window->PushWithSubject(lcf::Data::terms.encounter, enemy->GetName());
}
battle_result_messages = battle_message_window->GetLines();
battle_result_messages_it = battle_result_messages.begin();
battle_message_window->Clear();
if (!visible_enemies.empty()) {
SetWait(4, 4);
}
SetSceneActionSubState(eDisplayMonsters);
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == eDisplayMonsters) {
if (battle_result_messages_it == battle_result_messages.end()) {
SetSceneActionSubState(eFirstStrike);
return SceneActionReturn::eContinueThisFrame;
}
if (battle_message_window->IsPageFilled()) {
battle_message_window->Clear();
SetWait(4, 4);
return SceneActionReturn::eContinueThisFrame;
}
battle_message_window->Push(*battle_result_messages_it);
++battle_result_messages_it;
if (battle_result_messages_it == battle_result_messages.end() ||
battle_message_window->IsPageFilled()) {
SetWait(30, 70);
}
else {
SetWait(8, 8);
}
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == eFirstStrike) {
battle_message_window->Clear();
battle_result_messages.clear();
battle_result_messages_it = battle_result_messages.end();
if (first_strike) {
battle_message_window->Push(lcf::Data::terms.special_combat);
SetWait(30, 70);
}
SetSceneActionSubState(eClear);
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == eClear) {
battle_message_window->Clear();
SetState(State_SelectOption);
}
return SceneActionReturn::eContinueThisFrame;
}
void Scene_Battle_Rpg2k::ResetWindows(bool make_invisible) {
options_window->SetActive(false);
status_window->SetActive(false);
command_window->SetActive(false);
item_window->SetActive(false);
skill_window->SetActive(false);
target_window->SetActive(false);
battle_message_window->SetActive(false);
if (!make_invisible) {
return;
}
options_window->SetVisible(false);
status_window->SetVisible(false);
command_window->SetVisible(false);
target_window->SetVisible(false);
battle_message_window->SetVisible(false);
item_window->SetVisible(false);
skill_window->SetVisible(false);
help_window->SetVisible(false);
}
void Scene_Battle_Rpg2k::SetCommandWindows(int x) {
options_window->SetX(x);
x += options_window->GetWidth();
status_window->SetX(x);
x += status_window->GetWidth();
command_window->SetX(x);
}
void Scene_Battle_Rpg2k::MoveCommandWindows(int x, int frames) {
options_window->InitMovement(options_window->GetX(), options_window->GetY(),
x, options_window->GetY(), frames);
x += options_window->GetWidth();
status_window->InitMovement(status_window->GetX(), status_window->GetY(),
x, status_window->GetY(), frames);
x += status_window->GetWidth();
command_window->InitMovement(command_window->GetX(), command_window->GetY(),
x, command_window->GetY(), frames);
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionFightAutoEscape() {
enum SubState {
eBegin,
eCheckEvents,
eMoveWindow,
eWaitForInput,
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
battle_message_window->SetVisible(true);
SetSceneActionSubState(eCheckEvents);
}
if (scene_action_substate == eCheckEvents) {
if (!CheckBattleEndAndScheduleEvents()) {
return SceneActionReturn::eContinueThisFrame;
}
// No Auto battle/Escape when all actors are sleeping or similar
if (!Main_Data::game_party->IsAnyControllable()) {
SetState(State_SelectActor);
return SceneActionReturn::eContinueThisFrame;
}
SetSceneActionSubState(eMoveWindow);
}
if (scene_action_substate == eMoveWindow) {
options_window->SetVisible(true);
status_window->SetVisible(true);
status_window->SetIndex(-1);
command_window->SetIndex(-1);
command_window->SetVisible(true);
battle_message_window->SetVisible(false);
status_window->Refresh();
if (previous_state == State_SelectCommand) {
MoveCommandWindows(0, 8);
} else {
SetCommandWindows(0);
}
SetSceneActionSubState(eWaitForInput);
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == eWaitForInput) {
options_window->SetActive(true);
if (Input::IsTriggered(Input::DECISION)) {
if (!message_window->IsVisible()) {
switch (battle_options[options_window->GetIndex()]) {
case Battle: // Battle
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
RefreshTargetWindow();
target_window->SetVisible(false);
SetState(State_SelectActor);
break;
case AutoBattle: // Auto Battle
SetState(State_AutoBattle);
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
break;
case Escape: // Escape
if (!IsEscapeAllowed()) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Buzzer));
}
else {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
SetState(State_Escape);
}
break;
}
}
return SceneActionReturn::eWaitTillNextFrame;
}
}
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionActor() {
SelectNextActor(false);
return SceneActionReturn::eContinueThisFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionAutoBattle() {
SelectNextActor(true);
return SceneActionReturn::eContinueThisFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionCommand() {
enum SubState {
eMoveWindow,
eWaitForInput,
};
if (scene_action_substate == eMoveWindow) {
RefreshCommandWindow();
ResetWindows(true);
options_window->SetVisible(true);
status_window->SetVisible(true);
command_window->SetVisible(true);
if (previous_state == State_SelectActor) {
command_window->SetIndex(0);
}
MoveCommandWindows(-options_window->GetWidth(), 8);
SetSceneActionSubState(eWaitForInput);
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == eWaitForInput) {
command_window->SetActive(true);
if (Input::IsTriggered(Input::DECISION)) {
switch (command_window->GetIndex()) {
case 0: // Attack
AttackSelected();
break;
case 1: // Skill
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
SetState(State_SelectSkill);
break;
case 2: // Defense
DefendSelected();
break;
case 3: // Item
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Decision));
SetState(State_SelectItem);
break;
default:
// no-op
break;
}
return SceneActionReturn::eWaitTillNextFrame;
}
if (Input::IsTriggered(Input::CANCEL)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
--actor_index;
SelectPreviousActor();
return SceneActionReturn::eWaitTillNextFrame;
}
}
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionItem() {
enum SubState {
eBegin,
eWaitForInput,
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
item_window->SetVisible(true);
item_window->SetActive(true);
item_window->SetActor(active_actor);
item_window->Refresh();
item_window->SetHelpWindow(help_window.get());
help_window->SetVisible(true);
SetSceneActionSubState(eWaitForInput);
}
if (scene_action_substate == eWaitForInput) {
if (Input::IsTriggered(Input::DECISION)) {
ItemSelected();
return SceneActionReturn::eWaitTillNextFrame;
}
if (Input::IsTriggered(Input::CANCEL)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
SetState(State_SelectCommand);
return SceneActionReturn::eWaitTillNextFrame;
}
}
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionSkill() {
enum SubState {
eBegin,
eWaitForInput,
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
skill_window->SetActive(true);
skill_window->SetActor(active_actor->GetId());
if (previous_state == State_SelectCommand) {
skill_window->RestoreActorIndex(actor_index - 1);
}
skill_window->SetVisible(true);
skill_window->SetHelpWindow(help_window.get());
help_window->SetVisible(true);
SetSceneActionSubState(eWaitForInput);
}
if (scene_action_substate == eWaitForInput) {
if (Input::IsTriggered(Input::DECISION)) {
skill_window->SaveActorIndex(actor_index - 1);
SkillSelected();
return SceneActionReturn::eWaitTillNextFrame;
}
if (Input::IsTriggered(Input::CANCEL)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
skill_window->SaveActorIndex(actor_index - 1);
SetState(State_SelectCommand);
return SceneActionReturn::eWaitTillNextFrame;
}
}
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionEnemyTarget() {
enum SubState {
eBegin,
eWaitForInput,
};
std::vector<Game_Battler*> enemies;
Main_Data::game_enemyparty->GetActiveBattlers(enemies);
Game_Enemy* target = static_cast<Game_Enemy*>(enemies[target_window->GetIndex()]);
if (scene_action_substate == eBegin) {
select_target_flash_count = 0;
ResetWindows(false);
target_window->SetVisible(true);
target_window->SetActive(true);
target_window->SetVisible(true);
target_window->SetIndex(0);
SetSceneActionSubState(eWaitForInput);
}
++select_target_flash_count;
if (select_target_flash_count == 60) {
SelectionFlash(target);
select_target_flash_count = 0;
}
if (scene_action_substate == eWaitForInput) {
if (Input::IsTriggered(Input::DECISION)) {
EnemySelected();
return SceneActionReturn::eWaitTillNextFrame;
}
if (Input::IsTriggered(Input::CANCEL)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
SetState(previous_state);
return SceneActionReturn::eWaitTillNextFrame;
}
}
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionAllyTarget() {
enum SubState {
eBegin,
eWaitForInput,
};
if (scene_action_substate == eBegin) {
ResetWindows(false);
status_window->SetActive(true);
status_window->SetVisible(true);
status_window->SetIndex(0);
SetSceneActionSubState(eWaitForInput);
}
if (scene_action_substate == eWaitForInput) {
if (Input::IsTriggered(Input::DECISION)) {
AllySelected();
return SceneActionReturn::eWaitTillNextFrame;
}
if (Input::IsTriggered(Input::CANCEL)) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
SetState(previous_state);
return SceneActionReturn::eWaitTillNextFrame;
}
}
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionBattle() {
enum SubState {
eBegin,
ePreAction,
eBattleAction,
ePost,
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
battle_message_window->SetVisible(true);
SetSceneActionSubState(ePreAction);
}
if (scene_action_substate == ePreAction) {
// Remove actions for battlers who were killed or removed from the battle.
while (!battle_actions.empty() && !battle_actions.front()->Exists()) {
RemoveCurrentAction();
}
// Check for end battle, and run events before action
// This happens before each battler acts and also right after the last battler acts.
if (!CheckBattleEndAndScheduleEvents()) {
return SceneActionReturn::eContinueThisFrame;
}
if (battle_actions.empty()) {
SetSceneActionSubState(ePost);
return SceneActionReturn::eContinueThisFrame;
}
auto* battler = battle_actions.front();
// If we will start a new battle action, first check for state changes
// such as death, paralyze, confuse, etc..
PrepareBattleAction(battler);
pending_battle_action = battler->GetBattleAlgorithm();
#ifdef EP_DEBUG_BATTLE2K_STATE_MACHINE
Output::Debug("Battle2k StartBattleAction battler={} frame={}", battler->GetName(), Main_Data::game_system->GetFrameCounter());
#endif
// Initialize battle state
battle_action_wait = 0;
SetBattleActionState(BattleActionState_Begin);
battle_action_start_index = 0;
battle_action_results_index = 0;
battle_action_dmg_index = 0;
battle_action_substate_index = 0;
pending_message = {};
SetSceneActionSubState(eBattleAction);
}
if (scene_action_substate == eBattleAction) {
if (ProcessBattleAction(pending_battle_action.get()) == BattleActionReturn::eContinue) {
return SceneActionReturn::eContinueThisFrame;
}
pending_battle_action = nullptr;
RemoveCurrentAction();
battle_message_window->Clear();
SetSceneActionSubState(ePreAction);
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == ePost) {
// Everybody acted
actor_index = 0;
first_strike = false;
SetState(State_SelectOption);
return SceneActionReturn::eWaitTillNextFrame;
}
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionVictory() {
enum SubState {
eBegin = 0,
eEnd = 1,
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
battle_message_window->Clear();
battle_message_window->SetVisible(true);
int exp = Main_Data::game_enemyparty->GetExp();
int money = Main_Data::game_enemyparty->GetMoney();
std::vector<int> drops;
Main_Data::game_enemyparty->GenerateDrops(drops);
auto pm = PendingMessage();
pm.SetEnableFace(false);
pm.SetWordWrapped(Player::IsRPG2kE());
pm.PushLine(ToString(lcf::Data::terms.victory) + Player::escape_symbol + "|");
std::stringstream ss;
if (exp > 0) {
PushExperienceGainedMessage(pm, exp);
}
if (money > 0) {
PushGoldReceivedMessage(pm, money);
}
PushItemRecievedMessages(pm, drops);
Main_Data::game_system->BgmPlay(Main_Data::game_system->GetSystemBGM(Main_Data::game_system->BGM_Victory));
// Update attributes
std::vector<Game_Battler*> ally_battlers;
Main_Data::game_party->GetActiveBattlers(ally_battlers);
pm.PushPageEnd();
for (auto& ally: ally_battlers) {
Game_Actor* actor = static_cast<Game_Actor*>(ally);
actor->ChangeExp(actor->GetExp() + exp, &pm);
}
Main_Data::game_party->GainGold(money);
for (auto& item: drops) {
Main_Data::game_party->AddItem(item, 1);
}
Game_Message::SetPendingMessage(std::move(pm));
SetSceneActionSubState(eEnd);
return SceneActionReturn::eContinueThisFrame;
}
EndBattle(BattleResult::Victory);
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionDefeat() {
enum SubState {
eBegin = 0,
eEnd = 1,
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
battle_message_window->Clear();
battle_message_window->SetVisible(true);
Main_Data::game_system->SetMessagePositionFixed(true);
Main_Data::game_system->SetMessagePosition(2);
Main_Data::game_system->SetMessageTransparent(false);
auto pm = PendingMessage();
pm.SetEnableFace(false);
pm.SetWordWrapped(Player::IsRPG2kE());
pm.PushLine(ToString(lcf::Data::terms.defeat));
Main_Data::game_system->BgmPlay(Main_Data::game_system->GetSystemBGM(Main_Data::game_system->BGM_GameOver));
Game_Message::SetPendingMessage(std::move(pm));
SetSceneActionSubState(eEnd);
return SceneActionReturn::eContinueThisFrame;
}
EndBattle(BattleResult::Defeat);
return SceneActionReturn::eWaitTillNextFrame;
}
Scene_Battle_Rpg2k::SceneActionReturn Scene_Battle_Rpg2k::ProcessSceneActionEscape() {
enum SubState {
eBegin = 0,
eSuccess = 1,
eFailure = 2,
};
if (scene_action_substate == eBegin) {
ResetWindows(true);
battle_message_window->Clear();
battle_message_window->SetVisible(true);
auto next_ss = TryEscape() ? eSuccess : eFailure;
if (next_ss == eSuccess) {
battle_message_window->Push(lcf::Data::terms.escape_success);
} else {
battle_message_window->Push(lcf::Data::terms.escape_failure);
}
SetWait(10, 60);
SetSceneActionSubState(next_ss);
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == eSuccess) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Escape));
EndBattle(BattleResult::Escape);
return SceneActionReturn::eContinueThisFrame;
}
if (scene_action_substate == eFailure) {
SetState(State_Battle);
NextTurn();
CreateEnemyActions();
CreateExecutionOrder();
return SceneActionReturn::eContinueThisFrame;
}
return SceneActionReturn::eWaitTillNextFrame;
}
void Scene_Battle_Rpg2k::SetBattleActionState(BattleActionState state) {
battle_action_state = state;
SetBattleActionSubState(0);
}
void Scene_Battle_Rpg2k::SetBattleActionSubState(int substate, bool reset_index) {
battle_action_substate = substate;
if (reset_index) {
battle_action_substate_index = 0;
}
}
Scene_Battle_Rpg2k::BattleActionReturn Scene_Battle_Rpg2k::ProcessBattleAction(Game_BattleAlgorithm::AlgorithmBase* action) {
if (action == nullptr) {
Output::Warning("ProcessBattleAction: Invalid battle action");
Output::Warning("Please report a bug!");
return BattleActionReturn::eFinished;
}
#ifdef EP_DEBUG_BATTLE2K_STATE_MACHINE
static int last_state = -1;
static int last_substate = -1;
static int last_substate_index = -1;
if (battle_action_state != last_state || battle_action_substate != last_substate || battle_action_substate_index != last_substate_index) {
Output::Debug("Battle2k ProcessBattleAction({}, {},{},{}) frames={}", action->GetSource()->GetName(), battle_action_state, battle_action_substate, battle_action_substate_index, Main_Data::game_system->GetFrameCounter());
last_state = battle_action_state;
last_substate = battle_action_substate;
last_substate_index = battle_action_substate_index;
}
#endif
switch (battle_action_state) {
case BattleActionState_Begin:
return ProcessBattleActionBegin(action);
case BattleActionState_Usage:
return ProcessBattleActionUsage(action);
case BattleActionState_Animation:
return ProcessBattleActionAnimation(action);
case BattleActionState_AnimationReflect:
return ProcessBattleActionAnimationReflect(action);
case BattleActionState_Execute:
return ProcessBattleActionExecute(action);
case BattleActionState_Critical:
return ProcessBattleActionCritical(action);
case BattleActionState_Apply:
return ProcessBattleActionApply(action);
case BattleActionState_Failure:
return ProcessBattleActionFailure(action);
case BattleActionState_Damage:
return ProcessBattleActionDamage(action);
case BattleActionState_Params:
return ProcessBattleActionParamEffects(action);
case BattleActionState_States:
return ProcessBattleActionStateEffects(action);
case BattleActionState_Attributes:
return ProcessBattleActionAttributeEffects(action);
case BattleActionState_Finished:
return ProcessBattleActionFinished(action);
}
assert(false && "Invalid BattleActionState!");
return BattleActionReturn::eFinished;
}
Scene_Battle_Rpg2k::BattleActionReturn Scene_Battle_Rpg2k::ProcessBattleActionBegin(Game_BattleAlgorithm::AlgorithmBase* action) {
enum SubState {
eBegin = 0,
eShowMessage,
ePost,
};
auto* src = action->GetSource();
if (battle_action_substate == eBegin) {
assert(src->Exists());
battle_message_window->Clear();
bool show_message = false;
src->NextBattleTurn();
std::vector<int16_t> states_to_heal = src->BattleStateHeal();
src->ApplyConditions();
const lcf::rpg::State* pri_state = nullptr;
bool pri_was_healed = false;
for (size_t id = 1; id <= lcf::Data::states.size(); ++id) {
auto was_healed = std::find(states_to_heal.begin(), states_to_heal.end(), id) != states_to_heal.end();
if (!was_healed && !src->HasState(id)) {
continue;
}
auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, id);
if (!pri_state || state->priority >= pri_state->priority) {
pri_state = state;
pri_was_healed = was_healed;
}
}
if (pri_state != nullptr) {
StringView msg = pri_was_healed
? pri_state->message_recovery
: pri_state->message_affected;