forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgame_interpreter.cpp
3978 lines (3439 loc) · 109 KB
/
game_interpreter.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/>.
*/
// Headers
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <cassert>
#include <ctime>
#include "game_interpreter.h"
#include "audio.h"
#include "dynrpg.h"
#include "filefinder.h"
#include "game_map.h"
#include "game_event.h"
#include "game_enemyparty.h"
#include "game_ineluki.h"
#include "game_player.h"
#include "game_targets.h"
#include "game_switches.h"
#include "game_variables.h"
#include "game_party.h"
#include "game_actors.h"
#include "game_system.h"
#include "game_message.h"
#include "game_pictures.h"
#include "game_screen.h"
#include "spriteset_map.h"
#include "sprite_character.h"
#include "scene_gameover.h"
#include "scene_map.h"
#include "scene_save.h"
#include "scene.h"
#include "game_clock.h"
#include "input.h"
#include "main_data.h"
#include "output.h"
#include "player.h"
#include "util_macro.h"
#include <lcf/reader_util.h>
#include <lcf/lsd/reader.h>
#include "game_battle.h"
#include "utils.h"
#include "transition.h"
#include "baseui.h"
#include "algo.h"
#include "rand.h"
#include "game_multiplayer_senders.h"
enum BranchSubcommand {
eOptionBranchElse = 1
};
constexpr int Game_Interpreter::loop_limit;
constexpr int Game_Interpreter::call_stack_limit;
constexpr int Game_Interpreter::subcommand_sentinel;
Game_Interpreter::Game_Interpreter(bool _main_flag) {
main_flag = _main_flag;
Clear();
}
Game_Interpreter::~Game_Interpreter() {
}
// Clear.
void Game_Interpreter::Clear() {
_state = {};
_keyinput = {};
_async_op = {};
}
// Is interpreter running.
bool Game_Interpreter::IsRunning() const {
return !_state.stack.empty();
}
// Setup.
void Game_Interpreter::Push(
const std::vector<lcf::rpg::EventCommand>& _list,
int event_id,
bool started_by_decision_key
) {
if (_list.empty()) {
return;
}
if ((int)_state.stack.size() > call_stack_limit) {
Output::Error("Call Event limit ({}) has been exceeded", call_stack_limit);
}
lcf::rpg::SaveEventExecFrame frame;
frame.ID = _state.stack.size() + 1;
frame.commands = _list;
frame.current_command = 0;
frame.triggered_by_decision_key = started_by_decision_key;
frame.event_id = event_id;
if (_state.stack.empty() && main_flag && !Game_Battle::IsBattleRunning()) {
Main_Data::game_system->ClearMessageFace();
Main_Data::game_player->SetMenuCalling(false);
Main_Data::game_player->SetEncounterCalling(false);
}
_state.stack.push_back(std::move(frame));
}
void Game_Interpreter::KeyInputState::fromSave(const lcf::rpg::SaveEventExecState& save) {
*this = {};
// Maniac Patch aware check functions for parameters that handle
// keyboard and mouse through a bitmask
bool is_maniac = Player::IsPatchManiac();
auto check_key = [&](auto& var) {
if (is_maniac) {
return (var & 1) != 0;
} else {
return var != 0;
}
};
auto check_mouse = [&](auto& var) {
return (var & 2) != 0;
};
wait = save.keyinput_wait;
// FIXME: There is an RPG_RT bug where keyinput_variable is uint8_t
// which we currently have to emulate. So the value from the save could be wrong.
variable = save.keyinput_variable;
if (save.keyinput_all_directions) {
keys[Keys::eDown] = true;
keys[Keys::eLeft] = true;
keys[Keys::eRight] = true;
keys[Keys::eUp] = true;
} else {
if (Player::IsRPG2k3()) {
keys[Keys::eDown] = check_key(save.keyinput_2k3down);
keys[Keys::eLeft] = check_key(save.keyinput_2k3left);
keys[Keys::eRight] = check_key(save.keyinput_2k3right);
keys[Keys::eUp] = check_key(save.keyinput_2k3up);
} else {
keys[Keys::eDown] = check_key(save.keyinput_2kdown_2k3operators);
keys[Keys::eLeft] = check_key(save.keyinput_2kleft_2k3shift);
keys[Keys::eRight] = check_key(save.keyinput_2kright);
keys[Keys::eUp] = check_key(save.keyinput_2kup);
}
}
keys[Keys::eDecision] = check_key(save.keyinput_decision);
keys[Keys::eCancel] = check_key(save.keyinput_cancel);
if (Player::IsRPG2k3()) {
keys[Keys::eShift] = check_key(save.keyinput_2kleft_2k3shift);
keys[Keys::eNumbers] = check_key(save.keyinput_2kshift_2k3numbers);
keys[Keys::eOperators] = check_key(save.keyinput_2kdown_2k3operators);
if (is_maniac) {
keys[Keys::eMouseLeft] = check_mouse(save.keyinput_decision);
keys[Keys::eMouseRight] = check_mouse(save.keyinput_cancel);
keys[Keys::eMouseMiddle] = check_mouse(save.keyinput_2kleft_2k3shift);
keys[Keys::eMouseScrollUp] = check_mouse(save.keyinput_2k3up);
keys[Keys::eMouseScrollDown] = check_mouse(save.keyinput_2k3down);
}
} else {
keys[Keys::eShift] = check_key(save.keyinput_2kshift_2k3numbers);
}
time_variable = save.keyinput_time_variable;
timed = save.keyinput_timed;
// FIXME: Rm2k3 has no LSD chunk for this.
wait_frames = 0;
}
void Game_Interpreter::KeyInputState::toSave(lcf::rpg::SaveEventExecState& save) const {
save.keyinput_wait = 0;
save.keyinput_variable = 0;
save.keyinput_all_directions = 0;
save.keyinput_decision = 0;
save.keyinput_cancel = 0;
save.keyinput_2kshift_2k3numbers = 0;
save.keyinput_2kdown_2k3operators = 0;
save.keyinput_2kleft_2k3shift = 0;
save.keyinput_2kright = 0;
save.keyinput_2kup = 0;
save.keyinput_time_variable = 0;
save.keyinput_2k3down = 0;
save.keyinput_2k3left = 0;
save.keyinput_2k3right = 0;
save.keyinput_2k3up = 0;
save.keyinput_timed = 0;
save.keyinput_wait = wait;
// FIXME: There is an RPG_RT bug where keyinput_variable is uint8_t
// which we currently have to emulate. So this assignment truncates.
save.keyinput_variable = variable;
if (keys[Keys::eDown]
&& keys[Keys::eLeft]
&& keys[Keys::eRight]
&& keys[Keys::eUp]) {
save.keyinput_all_directions = true;
} else {
if (Player::IsRPG2k3()) {
save.keyinput_2k3down = keys[Keys::eDown];
save.keyinput_2k3left = keys[Keys::eLeft];
save.keyinput_2k3right = keys[Keys::eRight];
save.keyinput_2k3up = keys[Keys::eUp];
} else {
// RM2k uses these chunks for directions.
save.keyinput_2kdown_2k3operators = keys[Keys::eDown];
save.keyinput_2kleft_2k3shift = keys[Keys::eLeft];
save.keyinput_2kright = keys[Keys::eRight];
save.keyinput_2kup = keys[Keys::eUp];
}
}
save.keyinput_decision = keys[Keys::eDecision];
save.keyinput_cancel = keys[Keys::eCancel];
if (Player::IsRPG2k3()) {
save.keyinput_2kleft_2k3shift = keys[Keys::eShift];
save.keyinput_2kshift_2k3numbers = keys[Keys::eNumbers];
save.keyinput_2kdown_2k3operators = keys[Keys::eOperators];
if (Player::IsPatchManiac()) {
if (keys[Keys::eMouseLeft]) {
save.keyinput_decision |= 2;
}
if (keys[Keys::eMouseRight]) {
save.keyinput_cancel |= 2;
}
if (keys[Keys::eMouseMiddle]) {
save.keyinput_2kleft_2k3shift |= 2;
}
if (keys[Keys::eMouseScrollUp]) {
save.keyinput_2k3up |= 2;
}
if (keys[Keys::eMouseScrollDown]) {
save.keyinput_2k3down |= 2;
}
}
} else {
save.keyinput_2kshift_2k3numbers = keys[Keys::eShift];
}
save.keyinput_time_variable = time_variable;
save.keyinput_timed = timed;
// FIXME: Rm2k3 has no LSD chunk for this.
//void = wait_frames;
}
lcf::rpg::SaveEventExecState Game_Interpreter::GetState() const {
auto save = _state;
_keyinput.toSave(save);
return save;
}
void Game_Interpreter::SetupWait(int duration) {
if (duration == 0) {
// 0.0 waits 1 frame
_state.wait_time = 1;
} else {
_state.wait_time = duration * DEFAULT_FPS / 10;
}
}
bool Game_Interpreter::ReachedLoopLimit() const {
return loop_count >= loop_limit;
}
int Game_Interpreter::GetThisEventId() const {
auto event_id = GetCurrentEventId();
if (event_id == 0 && Player::IsRPG2k3E()) {
// RM2k3E allows "ThisEvent" commands to run from called
// common events. It operates on the last map event in
// the call stack.
for (auto iter = _state.stack.rbegin()++;
iter != _state.stack.rend(); ++iter) {
if (iter->event_id != 0) {
event_id = iter->event_id;
break;
}
}
}
return event_id;
}
uint8_t& Game_Interpreter::ReserveSubcommandIndex(int indent) {
auto& frame = GetFrame();
auto& path = frame.subcommand_path;
if (indent >= (int)path.size()) {
// This fixes an RPG_RT bug where RPG_RT would resize
// the array with uninitialized values.
path.resize(indent + 1, subcommand_sentinel);
}
return path[indent];
}
void Game_Interpreter::SetSubcommandIndex(int indent, int idx) {
ReserveSubcommandIndex(indent) = idx;
}
int Game_Interpreter::GetSubcommandIndex(int indent) const {
auto* frame = GetFramePtr();
if (frame == nullptr) {
return subcommand_sentinel;
}
auto& path = frame->subcommand_path;
if ((int)path.size() <= indent) {
return subcommand_sentinel;
}
return path[indent];
}
// Update
void Game_Interpreter::Update(bool reset_loop_count) {
if (reset_loop_count) {
loop_count = 0;
}
// Always reset async status when we enter interpreter loop.
_async_op = {};
if (!IsRunning()) {
return;
}
if (Input::IsTriggered(Input::DEBUG_ABORT_EVENT) && Player::debug_flag && !Game_Battle::IsBattleRunning()) {
if (Game_Message::IsMessageActive()) {
Game_Message::GetWindow()->FinishMessageProcessing();
}
if (!Main_Data::game_player->GetMoveRoute().move_commands.empty()) {
Main_Data::game_player->CancelMoveRoute();
}
EndEventProcessing();
return;
}
for (; loop_count < loop_limit; ++loop_count) {
// If something is calling a menu, we're allowed to execute only 1 command per interpreter. So we pass through if loop_count == 0, and stop at 1 or greater.
// RPG_RT compatible behavior.
if (loop_count > 0 && Scene::instance->HasRequestedScene()) {
break;
}
// Previous command triggered an async operation.
if (IsAsyncPending()) {
break;
}
if (main_flag) {
if (Main_Data::game_player->IsBoardingOrUnboarding())
break;
if (Main_Data::game_player->InVehicle() && Main_Data::game_player->GetVehicle()->IsAscendingOrDescending())
break;
if (Game_Message::IsMessagePending())
break;
} else {
if (Game_Message::IsMessageActive() && _state.show_message) {
break;
}
}
_state.show_message = false;
_state.abort_on_escape = false;
if (_state.wait_time > 0) {
_state.wait_time--;
break;
}
if (_state.wait_key_enter) {
if (Game_Message::IsMessageActive()) {
break;
}
if (!Input::IsTriggered(Input::DECISION)) {
break;
}
_state.wait_key_enter = false;
}
if (_state.wait_movement) {
if (Game_Map::IsAnyMovePending()) {
break;
}
_state.wait_movement = false;
}
if (_keyinput.wait) {
if (Game_Message::IsMessageActive()) {
break;
}
const int key = _keyinput.CheckInput();
Main_Data::game_variables->Set(_keyinput.variable, key);
Game_Map::SetNeedRefresh(true);
if (key == 0) {
++_keyinput.wait_frames;
break;
}
if (_keyinput.timed) {
// 10 per second
Main_Data::game_variables->Set(_keyinput.time_variable,
(_keyinput.wait_frames * 10) / Game_Clock::GetTargetGameFps());
}
_keyinput.wait = false;
}
auto* frame = GetFramePtr();
if (frame == nullptr) {
break;
}
if (Game_Map::GetNeedRefresh()) {
Game_Map::Refresh();
}
// Previous operations could have modified the stack.
// So we need to fetch the frame again.
frame = GetFramePtr();
if (frame == nullptr) {
break;
}
// Pop any completed stack frames
if (frame->current_command >= (int)frame->commands.size()) {
if (!OnFinishStackFrame()) {
break;
}
continue;
}
// Save the frame index before we call events.
int current_frame_idx = _state.stack.size() - 1;
const int index_before_exec = frame->current_command;
if (!ExecuteCommand()) {
break;
}
if (Game_Battle::IsBattleRunning() && Player::IsRPG2k3() && Game_Battle::CheckWin()) {
// Interpreter is cancelled when a win condition is fulfilled in RPG2k3 battle
break;
}
// Last event command removed the frame? We're done.
if (current_frame_idx >= (int)_state.stack.size() ) {
continue;
}
// Note: In the case we executed a CallEvent command, be sure to
// increment the old frame and not the new one we just pushed.
frame = &_state.stack[current_frame_idx];
// Only do auto increment if the command didn't manually
// change the index.
if (index_before_exec == frame->current_command) {
frame->current_command++;
}
} // for
if (loop_count > loop_limit - 1) {
auto* frame = GetFramePtr();
int event_id = frame ? frame->event_id : 0;
// Executed Events Count exceeded (10000)
Output::Debug("Event {} exceeded execution limit", event_id);
}
if (Game_Map::GetNeedRefresh()) {
Game_Map::Refresh();
}
}
// Setup Starting Event
void Game_Interpreter::Push(Game_Event* ev) {
Push(ev->GetList(), ev->GetId(), ev->WasStartedByDecisionKey());
}
void Game_Interpreter::Push(Game_Event* ev, const lcf::rpg::EventPage* page, bool triggered_by_decision_key) {
Push(page->event_commands, ev->GetId(), triggered_by_decision_key);
}
void Game_Interpreter::Push(Game_CommonEvent* ev) {
Push(ev->GetList(), 0, false);
}
bool Game_Interpreter::CheckGameOver() {
if (!Game_Battle::IsBattleRunning() && !Main_Data::game_party->IsAnyActive()) {
// Empty party is allowed
if (Main_Data::game_party->GetBattlerCount() > 0) {
Scene::instance->SetRequestedScene(std::make_shared<Scene_Gameover>());
return true;
}
}
return false;
}
void Game_Interpreter::SkipToNextConditional(std::initializer_list<Cmd> codes, int indent) {
auto& frame = GetFrame();
const auto& list = frame.commands;
auto& index = frame.current_command;
if (index >= static_cast<int>(list.size())) {
return;
}
for (++index; index < static_cast<int>(list.size()); ++index) {
const auto& com = list[index];
if (com.indent > indent) {
continue;
}
if (std::find(codes.begin(), codes.end(), static_cast<Cmd>(com.code)) != codes.end()) {
break;
}
}
}
int Game_Interpreter::DecodeInt(lcf::DBArray<int32_t>::const_iterator& it) {
int value = 0;
for (;;) {
int x = *it++;
value <<= 7;
value |= x & 0x7F;
if (!(x & 0x80))
break;
}
return value;
}
const std::string Game_Interpreter::DecodeString(lcf::DBArray<int32_t>::const_iterator& it) {
std::ostringstream out;
int len = DecodeInt(it);
for (int i = 0; i < len; i++)
out << (char)*it++;
std::string result = lcf::ReaderUtil::Recode(out.str(), Player::encoding);
return result;
}
lcf::rpg::MoveCommand Game_Interpreter::DecodeMove(lcf::DBArray<int32_t>::const_iterator& it) {
lcf::rpg::MoveCommand cmd;
cmd.command_id = *it++;
switch (cmd.command_id) {
case 32: // Switch ON
case 33: // Switch OFF
cmd.parameter_a = DecodeInt(it);
break;
case 34: // Change Graphic
cmd.parameter_string = lcf::DBString(DecodeString(it));
cmd.parameter_a = DecodeInt(it);
break;
case 35: // Play Sound Effect
cmd.parameter_string = lcf::DBString(DecodeString(it));
cmd.parameter_a = DecodeInt(it);
cmd.parameter_b = DecodeInt(it);
cmd.parameter_c = DecodeInt(it);
break;
}
return cmd;
}
// Execute Command.
bool Game_Interpreter::ExecuteCommand() {
auto& frame = GetFrame();
const auto& com = frame.commands[frame.current_command];
switch (static_cast<Cmd>(com.code)) {
case Cmd::ShowMessage:
return CommandShowMessage(com);
case Cmd::MessageOptions:
return CommandMessageOptions(com);
case Cmd::ChangeFaceGraphic:
return CommandChangeFaceGraphic(com);
case Cmd::ShowChoice:
return CommandShowChoices(com);
case Cmd::ShowChoiceOption:
return CommandShowChoiceOption(com);
case Cmd::ShowChoiceEnd:
return CommandShowChoiceEnd(com);
case Cmd::InputNumber:
return CommandInputNumber(com);
case Cmd::ControlSwitches:
return CommandControlSwitches(com);
case Cmd::ControlVars:
return CommandControlVariables(com);
case Cmd::TimerOperation:
return CommandTimerOperation(com);
case Cmd::ChangeGold:
return CommandChangeGold(com);
case Cmd::ChangeItems:
return CommandChangeItems(com);
case Cmd::ChangePartyMembers:
return CommandChangePartyMember(com);
case Cmd::ChangeExp:
return CommandChangeExp(com);
case Cmd::ChangeLevel:
return CommandChangeLevel(com);
case Cmd::ChangeParameters:
return CommandChangeParameters(com);
case Cmd::ChangeSkills:
return CommandChangeSkills(com);
case Cmd::ChangeEquipment:
return CommandChangeEquipment(com);
case Cmd::ChangeHP:
return CommandChangeHP(com);
case Cmd::ChangeSP:
return CommandChangeSP(com);
case Cmd::ChangeCondition:
return CommandChangeCondition(com);
case Cmd::FullHeal:
return CommandFullHeal(com);
case Cmd::SimulatedAttack:
return CommandSimulatedAttack(com);
case Cmd::Wait:
return CommandWait(com);
case Cmd::PlayBGM:
return CommandPlayBGM(com);
case Cmd::FadeOutBGM:
return CommandFadeOutBGM(com);
case Cmd::PlaySound:
return CommandPlaySound(com);
case Cmd::EndEventProcessing:
return CommandEndEventProcessing(com);
case Cmd::Comment:
case Cmd::Comment_2:
return CommandComment(com);
case Cmd::GameOver:
return CommandGameOver(com);
case Cmd::ChangeHeroName:
return CommandChangeHeroName(com);
case Cmd::ChangeHeroTitle:
return CommandChangeHeroTitle(com);
case Cmd::ChangeSpriteAssociation:
return CommandChangeSpriteAssociation(com);
case Cmd::ChangeActorFace:
return CommandChangeActorFace(com);
case Cmd::ChangeVehicleGraphic:
return CommandChangeVehicleGraphic(com);
case Cmd::ChangeSystemBGM:
return CommandChangeSystemBGM(com);
case Cmd::ChangeSystemSFX:
return CommandChangeSystemSFX(com);
case Cmd::ChangeSystemGraphics:
return CommandChangeSystemGraphics(com);
case Cmd::ChangeScreenTransitions:
return CommandChangeScreenTransitions(com);
case Cmd::MemorizeLocation:
return CommandMemorizeLocation(com);
case Cmd::SetVehicleLocation:
return CommandSetVehicleLocation(com);
case Cmd::ChangeEventLocation:
return CommandChangeEventLocation(com);
case Cmd::TradeEventLocations:
return CommandTradeEventLocations(com);
case Cmd::StoreTerrainID:
return CommandStoreTerrainID(com);
case Cmd::StoreEventID:
return CommandStoreEventID(com);
case Cmd::EraseScreen:
return CommandEraseScreen(com);
case Cmd::ShowScreen:
return CommandShowScreen(com);
case Cmd::TintScreen:
return CommandTintScreen(com);
case Cmd::FlashScreen:
return CommandFlashScreen(com);
case Cmd::ShakeScreen:
return CommandShakeScreen(com);
case Cmd::WeatherEffects:
return CommandWeatherEffects(com);
case Cmd::ShowPicture:
return CommandShowPicture(com);
case Cmd::MovePicture:
return CommandMovePicture(com);
case Cmd::ErasePicture:
return CommandErasePicture(com);
case Cmd::SpriteTransparency:
return CommandSpriteTransparency(com);
case Cmd::MoveEvent:
return CommandMoveEvent(com);
case Cmd::MemorizeBGM:
return CommandMemorizeBGM(com);
case Cmd::PlayMemorizedBGM:
return CommandPlayMemorizedBGM(com);
case Cmd::KeyInputProc:
return CommandKeyInputProc(com);
case Cmd::ChangeMapTileset:
return CommandChangeMapTileset(com);
case Cmd::ChangePBG:
return CommandChangePBG(com);
case Cmd::ChangeEncounterRate:
return CommandChangeEncounterRate(com);
case Cmd::TileSubstitution:
return CommandTileSubstitution(com);
case Cmd::TeleportTargets:
return CommandTeleportTargets(com);
case Cmd::ChangeTeleportAccess:
return CommandChangeTeleportAccess(com);
case Cmd::EscapeTarget:
return CommandEscapeTarget(com);
case Cmd::ChangeEscapeAccess:
return CommandChangeEscapeAccess(com);
case Cmd::ChangeSaveAccess:
return CommandChangeSaveAccess(com);
case Cmd::ChangeMainMenuAccess:
return CommandChangeMainMenuAccess(com);
case Cmd::ConditionalBranch:
return CommandConditionalBranch(com);
case Cmd::Label:
return true;
case Cmd::JumpToLabel:
return CommandJumpToLabel(com);
case Cmd::Loop:
return CommandLoop(com);
case Cmd::BreakLoop:
return CommandBreakLoop(com);
case Cmd::EndLoop:
return CommandEndLoop(com);
case Cmd::EraseEvent:
return CommandEraseEvent(com);
case Cmd::CallEvent:
return CommandCallEvent(com);
case Cmd::ReturntoTitleScreen:
return CommandReturnToTitleScreen(com);
case Cmd::ChangeClass:
return CommandChangeClass(com);
case Cmd::ChangeBattleCommands:
return CommandChangeBattleCommands(com);
case Cmd::ElseBranch:
return CommandElseBranch(com);
case Cmd::EndBranch:
return CommandEndBranch(com);
case Cmd::ExitGame:
return CommandExitGame(com);
case Cmd::ToggleFullscreen:
return CommandToggleFullscreen(com);
case Cmd::Maniac_GetSaveInfo:
return CommandManiacGetSaveInfo(com);
case Cmd::Maniac_Load:
return CommandManiacLoad(com);
case Cmd::Maniac_Save:
return CommandManiacSave(com);
case Cmd::Maniac_EndLoadProcess:
return CommandManiacEndLoadProcess(com);
case Cmd::Maniac_GetMousePosition:
return CommandManiacGetMousePosition(com);
case Cmd::Maniac_SetMousePosition:
return CommandManiacSetMousePosition(com);
case Cmd::Maniac_ShowStringPicture:
return CommandManiacShowStringPicture(com);
case Cmd::Maniac_GetPictureInfo:
return CommandManiacGetPictureInfo(com);
case Cmd::Maniac_ControlVarArray:
return CommandManiacControlVarArray(com);
case Cmd::Maniac_KeyInputProcEx:
return CommandManiacKeyInputProcEx(com);
case Cmd::Maniac_RewriteMap:
return CommandManiacRewriteMap(com);
case Cmd::Maniac_ControlGlobalSave:
return CommandManiacControlGlobalSave(com);
case Cmd::Maniac_ChangePictureId:
return CommandManiacChangePictureId(com);
case Cmd::Maniac_SetGameOption:
return CommandManiacSetGameOption(com);
case Cmd::Maniac_CallCommand:
return CommandManiacCallCommand(com);
default:
return true;
}
}
bool Game_Interpreter::OnFinishStackFrame() {
auto& frame = GetFrame();
const bool is_base_frame = _state.stack.size() == 1;
if (main_flag && is_base_frame && !Game_Battle::IsBattleRunning()) {
Main_Data::game_system->ClearMessageFace();
}
int event_id = frame.event_id;
if (is_base_frame && event_id > 0) {
Game_Event* evnt = Game_Map::GetEvent(event_id);
if (!evnt) {
Output::Error("Call stack finished with invalid event id {}. This can be caused by a vehicle teleport?", event_id);
} else if (main_flag) {
evnt->OnFinishForegroundEvent();
}
}
if (!main_flag && is_base_frame) {
// Parallel events will never clear the base stack frame. Instead we just
// reset the index back to 0 and wait for a frame.
// This not only optimizes away copying event code, it's also RPG_RT compatible.
frame.current_command = 0;
} else {
// If a called frame, or base frame of foreground interpreter, pop the stack.
_state.stack.pop_back();
}
return !is_base_frame;
}
std::vector<std::string> Game_Interpreter::GetChoices(int max_num_choices) {
const auto& frame = GetFrame();
const auto& list = frame.commands;
auto& index = frame.current_command;
// Let's find the choices
int current_indent = list[index + 1].indent;
std::vector<std::string> s_choices;
for (int index_temp = index + 1; index_temp < static_cast<int>(list.size()); ++index_temp) {
const auto& com = list[index_temp];
if (com.indent != current_indent) {
continue;
}
if (static_cast<Cmd>(com.code) == Cmd::ShowChoiceOption && com.parameters.size() > 0 && com.parameters[0] < max_num_choices) {
// Choice found
s_choices.push_back(ToString(list[index_temp].string));
}
if (static_cast<Cmd>(com.code) == Cmd::ShowChoiceEnd) {
break;
}
}
return s_choices;
}
bool Game_Interpreter::CommandOptionGeneric(lcf::rpg::EventCommand const& com, int option_sub_idx, std::initializer_list<Cmd> next) {
const auto sub_idx = GetSubcommandIndex(com.indent);
if (sub_idx == option_sub_idx) {
// Executes this option, so clear the subidx to skip all other options.
SetSubcommandIndex(com.indent, subcommand_sentinel);
} else {
SkipToNextConditional(next, com.indent);
}
return true;
}
bool Game_Interpreter::CommandShowMessage(lcf::rpg::EventCommand const& com) { // code 10110
auto& frame = GetFrame();
const auto& list = frame.commands;
auto& index = frame.current_command;
if (!Game_Message::CanShowMessage(main_flag)) {
return false;
}
auto pm = PendingMessage();
pm.SetIsEventMessage(true);
// Set first line
pm.PushLine(ToString(com.string));
++index;
// Check for continued lines via ShowMessage_2
while (index < static_cast<int>(list.size()) && static_cast<Cmd>(list[index].code) == Cmd::ShowMessage_2) {
// Add second (another) line
pm.PushLine(ToString(list[index].string));
++index;
}
// Handle Choices or number
if (index < static_cast<int>(list.size())) {
// If next event command is show choices
if (static_cast<Cmd>(list[index].code) == Cmd::ShowChoice) {
std::vector<std::string> s_choices = GetChoices(4);
// If choices fit on screen
if (static_cast<int>(s_choices.size()) <= (4 - pm.NumLines())) {
pm.SetChoiceCancelType(list[index].parameters[0]);
SetupChoices(s_choices, com.indent, pm);
++index;
}
} else if (static_cast<Cmd>(list[index].code) == Cmd::InputNumber) {
// If next event command is input number
// If input number fits on screen
if (pm.NumLines() < 4) {
int digits = list[index].parameters[0];
int variable_id = list[index].parameters[1];
pm.PushNumInput(variable_id, digits);
++index;
}
}
}
Game_Message::SetPendingMessage(std::move(pm));
_state.show_message = true;
return true;
}
bool Game_Interpreter::CommandMessageOptions(lcf::rpg::EventCommand const& com) { //code 10120
if (!Game_Message::CanShowMessage(main_flag)) {
return false;
}
Main_Data::game_system->SetMessageTransparent(com.parameters[0] != 0);
Main_Data::game_system->SetMessagePosition(com.parameters[1]);
Main_Data::game_system->SetMessagePositionFixed(com.parameters[2] == 0);
Main_Data::game_system->SetMessageContinueEvents(com.parameters[3] != 0);
return true;
}
bool Game_Interpreter::CommandChangeFaceGraphic(lcf::rpg::EventCommand const& com) { // Code 10130
if (!Game_Message::CanShowMessage(main_flag)) {
return false;
}
Main_Data::game_system->SetMessageFaceName(ToString(com.string));
Main_Data::game_system->SetMessageFaceIndex(com.parameters[0]);
Main_Data::game_system->SetMessageFaceRightPosition(com.parameters[1] != 0);
Main_Data::game_system->SetMessageFaceFlipped(com.parameters[2] != 0);
return true;
}
void Game_Interpreter::SetupChoices(const std::vector<std::string>& choices, int indent, PendingMessage& pm) {
// Set choices to message text
pm.SetChoiceResetColors(false);
for (int i = 0; i < 4 && i < static_cast<int>(choices.size()); i++) {
pm.PushChoice(choices[i]);
}
pm.SetChoiceContinuation([this, indent](int choice_result) {
SetSubcommandIndex(indent, choice_result);
return AsyncOp();
});
// save game compatibility with RPG_RT
ReserveSubcommandIndex(indent);
}
bool Game_Interpreter::CommandShowChoices(lcf::rpg::EventCommand const& com) { // code 10140
auto& index = GetFrame().current_command;
if (!Game_Message::CanShowMessage(main_flag)) {
return false;
}
auto pm = PendingMessage();
pm.SetIsEventMessage(true);
// Choices setup
std::vector<std::string> choices = GetChoices(4);
pm.SetChoiceCancelType(com.parameters[0]);
SetupChoices(choices, com.indent, pm);
Game_Message::SetPendingMessage(std::move(pm));
_state.show_message = true;
++index;
return false;
}
bool Game_Interpreter::CommandShowChoiceOption(lcf::rpg::EventCommand const& com) { //code 20140
const auto opt_sub_idx = com.parameters[0];
return CommandOptionGeneric(com, opt_sub_idx, {Cmd::ShowChoiceOption, Cmd::ShowChoiceEnd});
}
bool Game_Interpreter::CommandShowChoiceEnd(lcf::rpg::EventCommand const& /* com */) { //code 20141
return true;
}
bool Game_Interpreter::CommandInputNumber(lcf::rpg::EventCommand const& com) { // code 10150
if (!Game_Message::CanShowMessage(main_flag)) {