forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene_battle_rpg2k3.cpp
2995 lines (2569 loc) · 96 KB
/
scene_battle_rpg2k3.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 <cmath>
#include "scene_battle_rpg2k3.h"
#include <lcf/rpg/battlecommand.h>
#include <lcf/rpg/battleranimation.h>
#include <lcf/reader_util.h>
#include "drawable.h"
#include "input.h"
#include "output.h"
#include "player.h"
#include "sprite.h"
#include "sprite_enemy.h"
#include "sprite_actor.h"
#include "sprite_weapon.h"
#include "cache.h"
#include "game_actors.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_interpreter_battle.h"
#include "game_battlealgorithm.h"
#include "game_screen.h"
#include <lcf/reader_util.h>
#include "scene_gameover.h"
#include "utils.h"
#include "font.h"
#include "output.h"
#include "autobattle.h"
#include "enemyai.h"
#include <algorithm>
#include <memory>
#include "feature.h"
//#define EP_DEBUG_BATTLE2K3_STATE_MACHINE
Scene_Battle_Rpg2k3::Scene_Battle_Rpg2k3(const BattleArgs& args) :
Scene_Battle(args),
first_strike(args.first_strike)
{
}
void Scene_Battle_Rpg2k3::Start() {
Scene_Battle::Start();
InitBattleCondition(Game_Battle::GetBattleCondition());
CreateEnemySprites();
CreateActorSprites();
// We need to wait for actor and enemy graphics to load before we can finish initializing the battle.
AsyncNext([this]() { Start2(); });
}
void Scene_Battle_Rpg2k3::Start2() {
InitEnemies();
InitActors();
InitAtbGauges();
// Changed enemy place means we need to recompute Z order
ResetAllBattlerZ();
}
void Scene_Battle_Rpg2k3::InitBattleCondition(lcf::rpg::System::BattleCondition condition) {
if (condition == lcf::rpg::System::BattleCondition_pincers
&& (lcf::Data::battlecommands.placement == lcf::rpg::BattleCommands::Placement_manual
|| Main_Data::game_enemyparty->GetVisibleBattlerCount() <= 1))
{
condition = lcf::rpg::System::BattleCondition_back;
}
if (condition == lcf::rpg::System::BattleCondition_surround
&& (lcf::Data::battlecommands.placement == lcf::rpg::BattleCommands::Placement_manual
|| Main_Data::game_party->GetVisibleBattlerCount() <= 1))
{
condition = lcf::rpg::System::BattleCondition_initiative;
}
Game_Battle::SetBattleCondition(condition);
if (condition == lcf::rpg::System::BattleCondition_back || condition == lcf::rpg::System::BattleCondition_pincers) {
first_strike = false;
}
}
void Scene_Battle_Rpg2k3::InitEnemies() {
const auto& enemies = Main_Data::game_enemyparty->GetEnemies();
const auto cond = Game_Battle::GetBattleCondition();
// PLACEMENT AND DIRECTION
for (int real_idx = 0, visible_idx = 0; real_idx < static_cast<int>(enemies.size()); ++real_idx) {
auto& enemy = *enemies[real_idx];
const auto idx = enemy.IsHidden() ? real_idx : visible_idx;
enemy.SetBattlePosition(Game_Battle::Calculate2k3BattlePosition(enemy));
switch(cond) {
case lcf::rpg::System::BattleCondition_none:
enemy.SetDirectionFlipped(false);
break;
case lcf::rpg::System::BattleCondition_initiative:
case lcf::rpg::System::BattleCondition_back:
case lcf::rpg::System::BattleCondition_surround:
enemy.SetDirectionFlipped(true);
break;
case lcf::rpg::System::BattleCondition_pincers:
enemy.SetDirectionFlipped(!(idx & 1));
break;
}
visible_idx += !enemy.IsHidden();
}
}
void Scene_Battle_Rpg2k3::InitActors() {
const auto& actors = Main_Data::game_party->GetActors();
const auto cond = Game_Battle::GetBattleCondition();
// ROW ADJUSTMENT
// If all actors in the front row have battle loss conditions,
// all back row actors forced to the front row.
bool force_front_row = true;
for (auto& actor: actors) {
if (actor->GetBattleRow() == Game_Actor::RowType::RowType_front
&& !actor->IsHidden()
&& actor->CanActOrRecoverable()) {
force_front_row = false;
}
}
if (force_front_row) {
for (auto& actor: actors) {
actor->SetBattleRow(Game_Actor::RowType::RowType_front);
}
}
// PLACEMENT AND DIRECTION
for (int idx = 0; idx < static_cast<int>(actors.size()); ++idx) {
auto& actor = *actors[idx];
actor.SetBattlePosition(Game_Battle::Calculate2k3BattlePosition(actor));
if (cond == lcf::rpg::System::BattleCondition_surround) {
actor.SetDirectionFlipped(idx & 1);
} else {
actor.SetDirectionFlipped(false);
}
}
}
Scene_Battle_Rpg2k3::~Scene_Battle_Rpg2k3() {
}
void Scene_Battle_Rpg2k3::InitAtbGauge(Game_Battler& battler, int preempt_atb, int ambush_atb) {
if (battler.IsHidden() || !battler.CanActOrRecoverable()) {
return;
}
switch(Game_Battle::GetBattleCondition()) {
case lcf::rpg::System::BattleCondition_initiative:
case lcf::rpg::System::BattleCondition_surround:
battler.SetAtbGauge(preempt_atb);
break;
case lcf::rpg::System::BattleCondition_back:
case lcf::rpg::System::BattleCondition_pincers:
battler.SetAtbGauge(ambush_atb);
break;
case lcf::rpg::System::BattleCondition_none:
if (first_strike || battler.HasPreemptiveAttack()) {
battler.SetAtbGauge(preempt_atb);
} else {
battler.SetAtbGauge(Game_Battler::GetMaxAtbGauge() / 2);
}
break;
}
}
void Scene_Battle_Rpg2k3::InitAtbGauges() {
for (auto& enemy: Main_Data::game_enemyparty->GetEnemies()) {
InitAtbGauge(*enemy, 0, Game_Battler::GetMaxAtbGauge());
}
for (auto& actor: Main_Data::game_party->GetActors()) {
InitAtbGauge(*actor, Game_Battler::GetMaxAtbGauge(), 0);
}
}
template <typename O, typename M, typename C>
static bool CheckFlip(const O& others, const M& me, bool prefer_flipped, C&& cmp) {
for (auto& other: others) {
if (!other->IsHidden() && cmp(other->GetBattlePosition().x, me.GetBattlePosition().x)) {
return prefer_flipped;
}
}
return !prefer_flipped;
}
void Scene_Battle_Rpg2k3::UpdateEnemiesDirection() {
const auto& enemies = Main_Data::game_enemyparty->GetEnemies();
const auto& actors = Main_Data::game_party->GetActors();
for (int real_idx = 0, visible_idx = 0; real_idx < static_cast<int>(enemies.size()); ++real_idx) {
auto& enemy = *enemies[real_idx];
const auto idx = enemy.IsHidden() ? real_idx : visible_idx;
switch(Game_Battle::GetBattleCondition()) {
case lcf::rpg::System::BattleCondition_none:
case lcf::rpg::System::BattleCondition_initiative:
enemy.SetDirectionFlipped(CheckFlip(actors, enemy, false, std::greater_equal<>()));
break;
case lcf::rpg::System::BattleCondition_back:
enemy.SetDirectionFlipped(CheckFlip(actors, enemy, true, std::less_equal<>()));
break;
case lcf::rpg::System::BattleCondition_surround:
case lcf::rpg::System::BattleCondition_pincers:
enemy.SetDirectionFlipped(!(idx & 1));
break;
}
visible_idx += !enemy.IsHidden();
}
}
void Scene_Battle_Rpg2k3::UpdateActorsDirection() {
const auto& actors = Main_Data::game_party->GetActors();
const auto& enemies = Main_Data::game_enemyparty->GetEnemies();
for (int idx = 0; idx < static_cast<int>(actors.size()); ++idx) {
auto& actor = *actors[idx];
switch(Game_Battle::GetBattleCondition()) {
case lcf::rpg::System::BattleCondition_none:
case lcf::rpg::System::BattleCondition_initiative:
actor.SetDirectionFlipped(CheckFlip(enemies, actor, false, std::less_equal<>()));
break;
case lcf::rpg::System::BattleCondition_back:
actor.SetDirectionFlipped(CheckFlip(enemies, actor, true, std::greater_equal<>()));
break;
case lcf::rpg::System::BattleCondition_surround:
case lcf::rpg::System::BattleCondition_pincers:
actor.SetDirectionFlipped(idx & 1);
break;
}
}
}
void Scene_Battle_Rpg2k3::FaceTarget(Game_Actor& source, const Game_Battler& target) {
const auto sx = source.GetBattlePosition().x;
const auto tx = target.GetBattlePosition().x;
const bool flipped = source.IsDirectionFlipped();
if ((flipped && tx < sx) || (!flipped && tx > sx)) {
source.SetDirectionFlipped(1 - flipped);
}
}
void Scene_Battle_Rpg2k3::OnSystem2Ready(FileRequestResult* result) {
Cache::SetSystem2Name(result->file);
SetupSystem2Graphics();
}
void Scene_Battle_Rpg2k3::SetupSystem2Graphics() {
BitmapRef system2 = Cache::System2();
if (!system2) {
return;
}
ally_cursor->SetBitmap(system2);
ally_cursor->SetZ(Priority_Window);
ally_cursor->SetVisible(false);
enemy_cursor->SetBitmap(system2);
enemy_cursor->SetZ(Priority_Window);
enemy_cursor->SetVisible(false);
}
void Scene_Battle_Rpg2k3::CreateUi() {
Scene_Battle::CreateUi();
CreateBattleTargetWindow();
CreateBattleStatusWindow();
CreateBattleCommandWindow();
RecreateSpWindow(nullptr);
ally_cursor.reset(new Sprite());
enemy_cursor.reset(new Sprite());
if (lcf::Data::battlecommands.battle_type == lcf::rpg::BattleCommands::BattleType_gauge) {
item_window->SetX(Player::menu_offset_x);
item_window->SetY(Player::menu_offset_y + 64);
skill_window->SetX(Player::menu_offset_x);
skill_window->SetY(Player::menu_offset_y + 64);
}
if (lcf::Data::battlecommands.battle_type != lcf::rpg::BattleCommands::BattleType_traditional) {
int transp = IsTransparent() ? 160 : 255;
options_window->SetBackOpacity(transp);
item_window->SetBackOpacity(transp);
skill_window->SetBackOpacity(transp);
help_window->SetBackOpacity(transp);
status_window->SetBackOpacity(transp);
}
if (!Cache::System2() && Main_Data::game_system->HasSystem2Graphic()) {
FileRequestAsync* request = AsyncHandler::RequestFile("System2", Main_Data::game_system->GetSystem2Name());
request->SetGraphicFile(true);
request_id = request->Bind(&Scene_Battle_Rpg2k3::OnSystem2Ready, this);
request->Start();
} else {
SetupSystem2Graphics();
}
if (lcf::Data::battlecommands.window_size == lcf::rpg::BattleCommands::WindowSize_small) {
int height = 68;
int y = Player::screen_height - Player::menu_offset_y - height;
auto small_window = [&](auto& window) {
if (window) {
window->SetHeight(height);
window->SetY(y);
window->SetBorderY(5);
window->SetContents(Bitmap::Create(window->GetWidth() - window->GetBorderX() * 2, window->GetHeight() - window->GetBorderY() * 2));
window->SetMenuItemHeight(14);
}
};
small_window(options_window);
small_window(command_window);
small_window(skill_window);
small_window(item_window);
small_window(target_window);
options_window->Refresh();
status_window->SetY(y);
if (lcf::Data::battlecommands.battle_type == lcf::rpg::BattleCommands::BattleType_gauge) {
command_window->SetY(Player::screen_height / 2 - 80 / 2 + 12);
item_window->SetY(76);
skill_window->SetY(76);
}
}
ResetWindows(true);
}
void Scene_Battle_Rpg2k3::CreateEnemySprites() {
for (auto* enemy: Main_Data::game_enemyparty->GetEnemies()) {
enemy->SetBattleSprite(std::make_unique<Sprite_Enemy>(enemy));
}
}
void Scene_Battle_Rpg2k3::CreateActorSprites() {
for (auto* actor: Main_Data::game_party->GetActors()) {
actor->SetBattleSprite(std::make_unique<Sprite_Actor>(actor));
actor->SetWeaponSprite(std::make_unique<Sprite_Weapon>(actor));
}
}
void Scene_Battle_Rpg2k3::ResetAllBattlerZ() {
for (auto* enemy: Main_Data::game_enemyparty->GetEnemies()) {
auto* sprite = enemy->GetBattleSprite();
if (sprite) {
sprite->ResetZ();
}
}
for (auto* actor: Main_Data::game_party->GetActors()) {
auto* sprite = actor->GetActorBattleSprite();
if (sprite) {
sprite->ResetZ();
sprite->UpdatePosition();
sprite->DetectStateChange();
}
}
}
void Scene_Battle_Rpg2k3::UpdateAnimations() {
for (auto it = floating_texts.begin(); it != floating_texts.end();) {
int &time = it->remaining_time;
if (time % 2 == 0) {
int modifier = time <= 10 ? 1 :
time < 20 ? 0 :
-1;
it->sprite->SetY(it->sprite->GetY() + modifier);
}
--time;
if (time <= 0) {
it = floating_texts.erase(it);
} else {
++it;
}
}
if (running_away) {
for (auto& actor: Main_Data::game_party->GetActors()) {
Point p = actor->GetBattlePosition();
if (actor->IsDirectionFlipped()) {
p.x -= 6;
} else {
p.x += 6;
}
actor->SetBattlePosition(p);
}
}
auto frame_counter = Main_Data::game_system->GetFrameCounter();
bool ally_set = false;
if (status_window->GetActive()
&& lcf::Data::battlecommands.battle_type != lcf::rpg::BattleCommands::BattleType_traditional)
{
auto* actor = Main_Data::game_party->GetActor(status_window->GetIndex());
if (actor) {
const auto* sprite = actor->GetBattleSprite();
if (sprite) {
static const int frames[] = { 0, 1, 2, 1 };
int sprite_frame = frames[(frame_counter / 15) % 4];
ally_cursor->SetSrcRect(Rect(sprite_frame * 16, 16, 16, 16));
ally_cursor->SetVisible(true);
ally_cursor->SetX(Player::menu_offset_x + actor->GetBattlePosition().x);
ally_cursor->SetY(Player::menu_offset_y + actor->GetBattlePosition().y - 40);
if (frame_counter % 30 == 0) {
SelectionFlash(actor);
}
ally_set = true;
}
}
}
if (!ally_set) {
ally_cursor->SetVisible(false);
}
bool enemy_set = false;
if (target_window->GetActive()) {
std::vector<Game_Battler*> battlers;
Main_Data::game_enemyparty->GetActiveBattlers(battlers);
auto idx = target_window->GetIndex();
if (idx >= 0) {
auto* enemy = battlers[idx];
if (enemy) {
const auto* sprite = enemy->GetBattleSprite();
if (sprite) {
static const int frames[] = { 0, 1, 2, 1 };
int sprite_frame = frames[(frame_counter / 15) % 4];
enemy_cursor->SetSrcRect(Rect(sprite_frame * 16, 0, 16, 16));
enemy_cursor->SetVisible(true);
enemy_cursor->SetX(Player::menu_offset_x + enemy->GetBattlePosition().x + sprite->GetWidth() / 2);
enemy_cursor->SetY(Player::menu_offset_y + enemy->GetBattlePosition().y);
std::vector<lcf::rpg::State*> ordered_states = enemy->GetInflictedStatesOrderedByPriority();
if (ordered_states.size() > 0) {
help_window->Clear();
int state_counter = 0;
for (lcf::rpg::State* state : ordered_states) {
std::string state_name = fmt::format("{:9s}", state->name);
help_window->AddText(state_name, state->color, Text::AlignLeft, false);
if (++state_counter >= 5) break;
}
help_window->SetVisible(true);
} else {
help_window->SetVisible(false);
}
if (sprite_frame % 30 == 0) {
SelectionFlash(enemy);
}
enemy_set = true;
}
}
}
if (!enemy_set) {
help_window->Clear();
}
}
if (!enemy_set) {
enemy_cursor->SetVisible(false);
}
}
void Scene_Battle_Rpg2k3::DrawFloatText(int x, int y, int color, StringView text) {
Rect rect = Text::GetSize(*Font::Default(), text);
BitmapRef graphic = Bitmap::Create(rect.width, rect.height);
graphic->Clear();
graphic->TextDraw(-rect.x, -rect.y, color, text);
std::shared_ptr<Sprite> floating_text = std::make_shared<Sprite>();
floating_text->SetBitmap(graphic);
floating_text->SetOx(rect.width / 2);
floating_text->SetOy(rect.height + 5);
floating_text->SetX(Player::menu_offset_x + x);
// Move 5 pixel down because the number "jumps" with the intended y as the peak
floating_text->SetY(Player::menu_offset_y + y + 5);
floating_text->SetZ(Priority_Window + y);
FloatText float_text;
float_text.sprite = floating_text;
floating_texts.push_back(float_text);
}
bool Scene_Battle_Rpg2k3::IsTransparent() const {
return lcf::Data::battlecommands.transparency == lcf::rpg::BattleCommands::Transparency_transparent;
}
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_Rpg2k3::CreateBattleTargetWindow() {
auto commands = GetEnemyTargetNames();
int width = (lcf::Data::battlecommands.battle_type == lcf::rpg::BattleCommands::BattleType_traditional) ? 104 : 136;
int height = 80;
target_window.reset(new Window_Command(std::move(commands), width, 4));
target_window->SetHeight(height);
target_window->SetX(Player::menu_offset_x);
target_window->SetY(Player::screen_height - Player::menu_offset_y - height);
// Above other windows
target_window->SetZ(Priority_Window + 10);
if (lcf::Data::battlecommands.battle_type != lcf::rpg::BattleCommands::BattleType_traditional) {
int transp = IsTransparent() ? 160 : 255;
target_window->SetBackOpacity(transp);
}
target_window->SetSingleColumnWrapping(true);
}
void Scene_Battle_Rpg2k3::RefreshTargetWindow() {
// FIXME: Handle live refresh in traditional when the window is always visible
auto commands = GetEnemyTargetNames();
target_window->ReplaceCommands(std::move(commands));
if (!target_window->GetActive()) {
target_window->SetIndex(-1);
}
}
void Scene_Battle_Rpg2k3::CreateBattleStatusWindow() {
int w = MENU_WIDTH;
int h = 80;
int x = Player::menu_offset_x;
int y = Player::screen_height - Player::menu_offset_y - h;
switch (lcf::Data::battlecommands.battle_type) {
case lcf::rpg::BattleCommands::BattleType_traditional:
x = Player::menu_offset_x + target_window->GetWidth();
w = MENU_WIDTH - target_window->GetWidth();
break;
case lcf::rpg::BattleCommands::BattleType_alternative:
x = Player::menu_offset_x + options_window->GetWidth();
w = MENU_WIDTH - options_window->GetWidth();
break;
case lcf::rpg::BattleCommands::BattleType_gauge:
x = Player::menu_offset_x + options_window->GetWidth();
// Default window too small for 4 actors
w = MENU_WIDTH;
break;
}
status_window.reset(new Window_BattleStatus(x, y, w, h));
status_window->SetZ(Priority_Window + 1);
}
std::vector<std::string> Scene_Battle_Rpg2k3::GetBattleCommandNames(const Game_Actor* actor) {
std::vector<std::string> commands;
if (actor) {
for (auto* cmd: actor->GetBattleCommands()) {
commands.push_back(ToString(cmd->name));
}
}
if (Feature::HasRow() && lcf::Data::battlecommands.easyrpg_enable_battle_row_command) {
commands.push_back(ToString(lcf::Data::terms.row));
}
return commands;
}
void Scene_Battle_Rpg2k3::SetBattleCommandsDisable(Window_Command& window, const Game_Actor* actor) {
if (actor) {
const auto& cmds = actor->GetBattleCommands();
for (size_t i = 0; i < cmds.size(); ++i) {
auto* cmd = cmds[i];
if (cmd->type == lcf::rpg::BattleCommand::Type_escape && !IsEscapeAllowedFromActorCommand()) {
window.DisableItem(i);
} else {
window.EnableItem(i);
}
}
}
}
void Scene_Battle_Rpg2k3::CreateBattleCommandWindow() {
auto* actor = Main_Data::game_party->GetActor(0);
auto commands = GetBattleCommandNames(actor);
command_window.reset(new Window_Command(std::move(commands), option_command_mov));
SetBattleCommandsDisable(*command_window, actor);
int height = 80;
command_window->SetHeight(height);
switch (lcf::Data::battlecommands.battle_type) {
case lcf::rpg::BattleCommands::BattleType_traditional:
command_window->SetX(Player::menu_offset_x + target_window->GetWidth() - command_window->GetWidth());
command_window->SetY(Player::screen_height - Player::menu_offset_y - height);
break;
case lcf::rpg::BattleCommands::BattleType_alternative:
command_window->SetX(Player::menu_offset_x + MENU_WIDTH);
command_window->SetY(Player::screen_height - Player::menu_offset_y - height);
break;
case lcf::rpg::BattleCommands::BattleType_gauge:
command_window->SetX(Player::menu_offset_x);
command_window->SetY(Player::screen_height / 2 - height / 2);
break;
}
// Above the target window
command_window->SetZ(Priority_Window + 20);
if (lcf::Data::battlecommands.battle_type != lcf::rpg::BattleCommands::BattleType_traditional) {
int transp = IsTransparent() ? 160 : 255;
command_window->SetBackOpacity(transp);
}
}
void Scene_Battle_Rpg2k3::RefreshCommandWindow(const Game_Actor* actor) {
auto commands = GetBattleCommandNames(actor);
command_window->ReplaceCommands(std::move(commands));
SetBattleCommandsDisable(*command_window, actor);
command_window->SetIndex(-1);
}
void Scene_Battle_Rpg2k3::SetActiveActor(int idx) {
#ifdef EP_DEBUG_BATTLE2K3_STATE_MACHINE
Output::Debug("Battle2k3 SetActiveActor({}) frame={}", idx, Main_Data::game_system->GetFrameCounter());
#endif
status_window->SetIndex(idx);
active_actor = Main_Data::game_party->GetActor(idx);
auto* display_actor = active_actor ? active_actor : Main_Data::game_party->GetActor(0);
RefreshCommandWindow(display_actor);
}
void Scene_Battle_Rpg2k3::ResetWindows(bool make_invisible) {
item_window->SetHelpWindow(nullptr);
skill_window->SetHelpWindow(nullptr);
options_window->SetActive(false);
status_window->SetActive(false);
command_window->SetActive(false);
item_window->SetActive(false);
skill_window->SetActive(false);
target_window->SetActive(false);
sp_window->SetActive(false);
if (!make_invisible) {
return;
}
options_window->SetVisible(false);
status_window->SetVisible(false);
command_window->SetVisible(false);
target_window->SetVisible(false);
item_window->SetVisible(false);
skill_window->SetVisible(false);
help_window->SetVisible(false);
sp_window->SetVisible(false);
}
void Scene_Battle_Rpg2k3::MoveCommandWindows(int x, int frames) {
if (lcf::Data::battlecommands.battle_type != lcf::rpg::BattleCommands::BattleType_traditional) {
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);
if (lcf::Data::battlecommands.battle_type == lcf::rpg::BattleCommands::BattleType_alternative) {
x += status_window->GetWidth();
command_window->InitMovement(command_window->GetX(), command_window->GetY(),
x, command_window->GetY(), frames);
}
}
}
void Scene_Battle_Rpg2k3::SetState(Scene_Battle::State new_state) {
previous_state = state;
state = new_state;
if (new_state == State_SelectActor) {
auto_battle = false;
}
if (new_state == State_AutoBattle) {
auto_battle = true;
}
SetSceneActionSubState(0);
#ifdef EP_DEBUG_BATTLE2K3_STATE_MACHINE
Output::Debug("Battle2k3 SetState state={} prev={} auto_battle={}", state, previous_state, auto_battle);
#endif
}
void Scene_Battle_Rpg2k3::ReturnToMainBattleState() {
SetState(auto_battle ? State_AutoBattle : State_SelectActor);
}
void Scene_Battle_Rpg2k3::SetSceneActionSubState(int substate) {
scene_action_substate = substate;
}
static bool BattlerReadyToAct(const Game_Battler* battler) {
return battler->IsAtbGaugeFull() && battler->Exists() && battler->CanAct();
}
void Scene_Battle_Rpg2k3::UpdateReadyActors() {
const auto& actors = Main_Data::game_party->GetActors();
for (auto actor: actors) {
auto position = std::find(atb_order.begin(), atb_order.end(), actor->GetId());
if (BattlerReadyToAct(actor)) {
if (position == atb_order.end()) {
#ifdef EP_DEBUG_BATTLE2K3_STATE_MACHINE
Output::Debug("Battle2k3 UpdateReadyActors add={} frame={}", actor->GetId(), Main_Data::game_system->GetFrameCounter());
#endif
atb_order.push_back(actor->GetId());
}
} else {
if (position != atb_order.end()) {
#ifdef EP_DEBUG_BATTLE2K3_STATE_MACHINE
Output::Debug("Battle2k3 UpdateReadyActors remove={} frame={}", actor->GetId(), Main_Data::game_system->GetFrameCounter());
#endif
atb_order.erase(position);
}
}
}
}
int Scene_Battle_Rpg2k3::GetNextReadyActor() {
if (!atb_order.empty()) {
#ifdef EP_DEBUG_BATTLE2K3_STATE_MACHINE
Output::Debug("Battle2k3 GetNextReadyActor actor={} frame={}", atb_order.front(), Main_Data::game_system->GetFrameCounter());
#endif
return Main_Data::game_party->GetActorPositionInParty(atb_order.front());
}
#ifdef EP_DEBUG_BATTLE2K3_STATE_MACHINE
Output::Debug("Battle2k3 GetNextReadyActor (none) frame={}", Main_Data::game_system->GetFrameCounter());
#endif
return -1;
}
bool Scene_Battle_Rpg2k3::IsAtbAccumulating() const {
if (Game_Battle::IsBattleAnimationWaiting()) {
return false;
}
const bool active_atb = Main_Data::game_system->GetAtbMode() == lcf::rpg::SaveSystem::AtbMode_atb_active;
switch(state) {
case State_SelectEnemyTarget:
case State_SelectAllyTarget:
case State_SelectItem:
case State_SelectSkill:
case State_SelectCommand:
return active_atb;
case State_AutoBattle:
case State_SelectActor:
return true;
default:
break;
}
return false;
}
void Scene_Battle_Rpg2k3::CreateEnemyActions() {
// FIXME: RPG_RT checks animations and event ready flag?
for (auto* enemy: Main_Data::game_enemyparty->GetEnemies()) {
if (enemy->IsAtbGaugeFull() && !enemy->GetBattleAlgorithm()) {
if (!EnemyAi::SetStateRestrictedAction(*enemy)) {
if (enemy->GetEnemyAi() == -1) {
enemyai_algos[default_enemyai_algo]->SetEnemyAiAction(*enemy);
} else {
enemyai_algos[enemy->GetEnemyAi()]->SetEnemyAiAction(*enemy);
}
}
assert(enemy->GetBattleAlgorithm() != nullptr);
ActionSelectedCallback(enemy);
#ifdef EP_DEBUG_BATTLE2K3_STATE_MACHINE
Output::Debug("Battle2k3 ScheduleEnemyAction name={} type={} frame={}", enemy->GetName(), static_cast<int>(enemy->GetBattleAlgorithm()->GetType()), Main_Data::game_system->GetFrameCounter());
#endif
}
}
}
void Scene_Battle_Rpg2k3::CreateActorAutoActions() {
if (state != State_SelectActor
&& state != State_AutoBattle
&& state != State_Battle
) {
return;
}
// FIXME: RPG_RT checks only actor animations?
for (auto* actor: Main_Data::game_party->GetActors()) {
if (!BattlerReadyToAct(actor)
|| actor->GetBattleAlgorithm()
|| (actor->IsControllable() && state != State_AutoBattle)
) {
continue;
}
Game_Battler* random_target = nullptr;
switch (actor->GetSignificantRestriction()) {
case lcf::rpg::State::Restriction_attack_ally:
random_target = Main_Data::game_party->GetRandomActiveBattler();
break;
case lcf::rpg::State::Restriction_attack_enemy:
random_target = Main_Data::game_enemyparty->GetRandomActiveBattler();
break;
default:
break;
}
if (random_target) {
actor->SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::Normal>(actor, random_target));
} else {
if (actor->GetActorAi() == -1) {
this->autobattle_algos[default_autobattle_algo]->SetAutoBattleAction(*actor);
} else {
this->autobattle_algos[actor->GetActorAi()]->SetAutoBattleAction(*actor);
}
assert(actor->GetBattleAlgorithm() != nullptr);
}
actor->SetLastBattleAction(-1);
ActionSelectedCallback(actor);
}
}
bool Scene_Battle_Rpg2k3::UpdateAtb() {
if (Game_Battle::GetInterpreter().IsRunning() || Game_Message::IsMessageActive()) {
return true;
}
if (IsAtbAccumulating()) {
// FIXME: If one monster can act now, he gets his battle algo set, and we abort updating atb for other monsters
Game_Battle::UpdateAtbGauges();
}
CreateEnemyActions();
CreateActorAutoActions();
return true;
}
bool Scene_Battle_Rpg2k3::IsBattleActionPending() const {
return !battle_actions.empty();
}
bool Scene_Battle_Rpg2k3::UpdateBattleState() {
if (resume_from_debug_scene) {
resume_from_debug_scene = false;
return true;
}
UpdateScreen();
// FIXME: RPG_RT updates actors first, and this goes into doing CBA actor battle actions initiated last frame
UpdateBattlers();
UpdateUi();
const auto battle_ending = (state == State_Victory || state == State_Defeat);
if (!battle_ending) {
// FIXME: Interpreter also blocked by an RPG_RT continueBattle flag. What is this flag?
if (!Game_Battle::IsBattleAnimationWaiting()) {
if (!UpdateEvents()) {
return false;
}
}
}
// FIXME: Update Panorama
if (!battle_ending) {
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;
}
}
CheckBattleEndConditions();
UpdateAtb();
}
return true;
}
void Scene_Battle_Rpg2k3::vUpdate() {
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()) {
break;
}
if (state != State_Victory && state != State_Defeat && Game_Battle::GetInterpreter().IsRunning()) {
break;
}
if (!CheckWait()) {
break;
}
if (ProcessSceneAction() == SceneActionReturn::eWaitTillNextFrame) {
break;
}
}
UpdateAnimations();
UpdateGraphics();
}
void Scene_Battle_Rpg2k3::NextTurn(Game_Battler* battler) {
Main_Data::game_party->IncTurns();
battler->NextBattleTurn();
Game_Battle::GetInterpreterBattle().ResetPagesExecuted();
}
bool Scene_Battle_Rpg2k3::CheckBattleEndConditions() {
if (state == State_Defeat || Game_Battle::CheckLose()) {
if (state != State_Defeat) {
SetState(State_Defeat);
}
return true;
}
if (state == State_Victory || Game_Battle::CheckWin()) {
if (state != State_Victory) {
SetState(State_Victory);
}
return true;
}
return false;
}
bool Scene_Battle_Rpg2k3::CheckBattleEndAndScheduleEvents(EventTriggerType tt, Game_Battler* source) {
auto& interp = Game_Battle::GetInterpreterBattle();
if (interp.IsRunning()) {
return false;
}