-
Notifications
You must be signed in to change notification settings - Fork 182
/
player.cpp
1100 lines (950 loc) · 29.8 KB
/
player.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 <cstring>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
# include <Shellapi.h>
# ifndef _DEBUG
# include <winioctl.h>
# include <dbghelp.h>
static void InitMiniDumpWriter();
# endif
#elif defined(GEKKO)
# include <fat.h>
#elif defined(EMSCRIPTEN)
# include <emscripten.h>
#elif defined(PSP2)
# include <psp2/kernel/processmgr.h>
#elif defined(_3DS)
# include <3ds.h>
#endif
#include "async_handler.h"
#include "audio.h"
#include "cache.h"
#include "filefinder.h"
#include "game_actors.h"
#include "game_map.h"
#include "game_message.h"
#include "game_enemyparty.h"
#include "game_party.h"
#include "game_player.h"
#include "game_switches.h"
#include "game_system.h"
#include "game_temp.h"
#include "game_variables.h"
#include "graphics.h"
#include "inireader.h"
#include "input.h"
#include "ldb_reader.h"
#include "lmt_reader.h"
#include "lsd_reader.h"
#include "main_data.h"
#include "output.h"
#include "player.h"
#include "reader_lcf.h"
#include "reader_util.h"
#include "scene_battle.h"
#include "scene_logo.h"
#include "utils.h"
#include "version.h"
namespace Player {
bool exit_flag;
bool reset_flag;
bool debug_flag;
bool hide_title_flag;
bool window_flag;
bool fps_flag;
bool battle_test_flag;
int battle_test_troop_id;
bool new_game_flag;
int load_game_id;
int party_x_position;
int party_y_position;
std::vector<int> party_members;
int start_map_id;
bool no_rtp_flag;
bool no_audio_flag;
bool mouse_flag;
std::string encoding;
std::string escape_symbol;
int engine;
std::string game_title;
int frames;
#ifdef EMSCRIPTEN
std::string emscripten_game_name;
#endif
#ifdef _3DS
bool use_dsp;
bool is_3dsx;
#endif
}
namespace {
double start_time;
double next_frame;
// Overwritten by --encoding
std::string forced_encoding;
FileRequestBinding system_request_id;
FileRequestBinding save_request_id;
FileRequestBinding map_request_id;
}
void Player::Init(int argc, char *argv[]) {
frames = 0;
// Display a nice version string
std::stringstream header;
std::string addtl_ver(PLAYER_ADDTL);
header << "EasyRPG Player " << PLAYER_VERSION;
if (!addtl_ver.empty())
header << " " << addtl_ver;
header << " started";
Output::Debug(header.str().c_str());
unsigned int header_width = header.str().length();
header.str("");
header << std::setfill('=') << std::setw(header_width) << "=";
Output::Debug(header.str().c_str());
#ifdef GEKKO
// Init libfat (Mount SD/USB)
if (!fatInitDefault()) {
Output::Error("Couldn't mount any storage medium!");
}
#elif defined(_3DS)
// Starting debug console
gfxInitDefault();
consoleInit(GFX_BOTTOM, NULL);
APT_SetAppCpuTimeLimit(30);
consoleClear();
// Check if we already have access to csnd:SND, if not, we will perform a kernel privilege escalation
Handle csndHandle = 0;
use_dsp = false;
#ifndef FORCE_DSP
srvGetServiceHandleDirect(&csndHandle, "csnd:SND");
if (csndHandle) {
Output::Debug("csnd:SND has been selected as audio service.");
svcCloseHandle(csndHandle);
} else {
Output::Debug("csnd:SND is unavailable...");
#endif
srvGetServiceHandleDirect(&csndHandle, "dsp::DSP");
if (csndHandle) {
Output::Debug("dsp::DSP has been selected as audio service.");
use_dsp = true;
svcCloseHandle(csndHandle);
} else {
Output::Error("dsp::DSP is unavailable. Please dump a DSP firmware to use EasyRPG Player. If the problem persists, please report us the issue.");
}
#ifndef FORCE_DSP
}
#endif
fsInit();
sdmcInit();
#ifndef CITRA3DS_COMPATIBLE
romfsInit();
#endif
hidInit();
// Enable 804 Mhz mode if on N3DS
bool isN3DS;
APT_CheckNew3DS(&isN3DS);
if (isN3DS) {
osSetSpeedupEnable(true);
}
#endif
#if (defined(_WIN32) && defined(NDEBUG) && defined(WINVER) && WINVER >= 0x0600)
InitMiniDumpWriter();
#endif
srand(time(NULL));
Utils::SeedRandomNumberGenerator(time(NULL));
ParseCommandLine(argc, argv);
#ifdef EMSCRIPTEN
Output::IgnorePause(true);
// Create initial directory structure in our private area
// Retrieve save directory from persistent storage
EM_ASM(
FS.mkdir("easyrpg");
FS.chdir("easyrpg");
var dirs = ['Backdrop', 'Battle', 'Battle2', 'BattleCharSet', 'BattleWeapon', 'CharSet', 'ChipSet', 'FaceSet', 'Frame', 'GameOver', 'Monster', 'Movie', 'Music', 'Panorama', 'Picture', 'Sound', 'System', 'System2', 'Title', 'Save'];
dirs.forEach(function(dir) { FS.mkdir(dir) });
FS.mount(IDBFS, {}, 'Save');
FS.syncfs(true, function(err) {
});
);
#endif
Main_Data::Init();
DisplayUi.reset();
if(! DisplayUi) {
DisplayUi = BaseUi::CreateUi
(SCREEN_TARGET_WIDTH,
SCREEN_TARGET_HEIGHT,
!window_flag,
RUN_ZOOM);
}
}
void Player::Run() {
Scene::Push(std::make_shared<Scene>());
Scene::Push(std::shared_ptr<Scene>(static_cast<Scene*>(new Scene_Logo())));
reset_flag = false;
// Reset frames before starting
FrameReset();
// Main loop
#ifdef EMSCRIPTEN
emscripten_set_main_loop(Player::MainLoop, 0, 0);
#elif defined(_3DS)
while (aptMainLoop() && (Graphics::IsTransitionPending() || Scene::instance->type != Scene::Null))
{
hidScanInput();
Player::MainLoop();
}
#else
while (Graphics::IsTransitionPending() || Scene::instance->type != Scene::Null)
MainLoop();
#endif
}
void Player::MainLoop() {
Scene::instance->MainFunction();
for (size_t i = 0; i < Scene::old_instances.size(); ++i) {
Graphics::Pop();
}
Scene::old_instances.clear();
if (!Graphics::IsTransitionPending() && Scene::instance->type == Scene::Null) {
Exit();
}
}
void Player::Pause() {
Audio().BGM_Pause();
}
void Player::Resume() {
Input::ResetKeys();
Audio().BGM_Resume();
FrameReset();
}
void Player::Update(bool update_scene) {
// available ms per frame, game logic expects 60 fps
static const double framerate_interval = 1000.0 / Graphics::GetDefaultFps();
next_frame = start_time + framerate_interval;
#ifdef EMSCRIPTEN
// Ticks in emscripten are unreliable due to how the main loop works:
// This function is only called 60 times per second instead of theoretical
// 1000s of times.
Graphics::Update(true);
#else
// Time left before next frame? Let's render the current frame.
double cur_time = (double)DisplayUi->GetTicks();
if (cur_time < next_frame) {
Graphics::Update(true);
cur_time = (double)DisplayUi->GetTicks();
// Still time after graphic update? Yield until it's time for next one.
if (cur_time < next_frame) {
DisplayUi->Sleep((uint32_t)(next_frame - cur_time));
}
} else {
Graphics::Update(false);
}
#endif
// Normal logic update
if (Input::IsTriggered(Input::TOGGLE_FPS)) {
fps_flag = !fps_flag;
}
if (Input::IsTriggered(Input::TAKE_SCREENSHOT)) {
Output::TakeScreenshot();
}
if (Input::IsTriggered(Input::SHOW_LOG)) {
Output::ToggleLog();
}
DisplayUi->ProcessEvents();
if (exit_flag) {
Scene::PopUntil(Scene::Null);
} else if (reset_flag) {
reset_flag = false;
if (Scene::Find(Scene::Title) && Scene::instance->type != Scene::Title) {
Scene::PopUntil(Scene::Title);
Audio().BGM_Fade(800);
// Do not update this scene until it's properly set up in the next main loop
update_scene = false;
}
}
Audio().Update();
Input::Update();
if (update_scene) {
Scene::instance->Update();
}
start_time = next_frame;
++frames;
}
void Player::FrameReset() {
// When update started
start_time = (double)DisplayUi->GetTicks();
// available ms per frame, game logic expects 60 fps
static const double framerate_interval = 1000.0 / Graphics::GetDefaultFps();
// When next frame is expected
next_frame = start_time + framerate_interval;
Graphics::FrameReset();
}
int Player::GetFrames() {
return frames;
}
void Player::Exit() {
#ifdef EMSCRIPTEN
emscripten_cancel_main_loop();
BitmapRef surface = DisplayUi->GetDisplaySurface();
std::string error = "You can turn off your browser now.";
Text::Draw(*surface, 55, DisplayUi->GetHeight() / 2 - 6, Color(255, 255, 255, 255), error);
DisplayUi->UpdateDisplay();
#endif
Font::Dispose();
Graphics::Quit();
FileFinder::Quit();
Output::Quit();
DisplayUi.reset();
#ifdef PSP2
sceKernelExitProcess(0);
#endif
#ifdef _3DS
hidExit();
gfxExit();
sdmcExit();
romfsExit();
fsExit();
#endif
}
void Player::ParseCommandLine(int argc, char *argv[]) {
#ifdef _3DS
is_3dsx = argc > 0;
#endif
#if defined(_WIN32) && !defined(__WINRT__)
LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
#endif
engine = EngineNone;
#ifdef EMSCRIPTEN
window_flag = true;
#else
window_flag = false;
#endif
fps_flag = false;
debug_flag = false;
hide_title_flag = false;
exit_flag = false;
reset_flag = false;
battle_test_flag = false;
battle_test_troop_id = 0;
new_game_flag = false;
load_game_id = -1;
party_x_position = -1;
party_y_position = -1;
start_map_id = -1;
no_rtp_flag = false;
no_audio_flag = false;
mouse_flag = false;
std::vector<std::string> args;
std::stringstream ss;
for (int i = 1; i < argc; ++i) {
ss << argv[i] << " ";
#if defined(_WIN32) && !defined(__WINRT__)
args.push_back(Utils::LowerCase(Utils::FromWideString(argv_w[i])));
#else
args.push_back(Utils::LowerCase(argv[i]));
#endif
}
Output::Debug("CLI: %s", ss.str().c_str());
std::vector<std::string>::const_iterator it;
for (it = args.begin(); it != args.end(); ++it) {
ss << *it << " ";
}
for (it = args.begin(); it != args.end(); ++it) {
if (*it == "window" || *it == "--window") {
window_flag = true;
}
else if (*it == "--show-fps") {
fps_flag = true;
}
else if (*it == "--enable-mouse") {
mouse_flag = true;
}
else if (*it == "testplay" || *it == "--test-play") {
debug_flag = true;
}
else if (*it == "hidetitle" || *it == "--hide-title") {
hide_title_flag = true;
}
else if (*it == "battletest") {
++it;
if (it == args.end()) {
return;
}
battle_test_flag = true;
battle_test_troop_id = atoi((*it).c_str());
if (battle_test_troop_id == 0) {
--it;
battle_test_troop_id = (argc > 4) ? atoi(argv[4]) : 0;
}
}
else if (*it == "--battle-test") {
++it;
if (it == args.end()) {
return;
}
battle_test_flag = true;
battle_test_troop_id = atoi((*it).c_str());
}
else if (*it == "--project-path") {
++it;
if (it == args.end()) {
return;
}
#ifdef _WIN32
Main_Data::SetProjectPath(*it);
BOOL cur_dir = SetCurrentDirectory(Utils::ToWideString(Main_Data::GetProjectPath()).c_str());
if (cur_dir) {
Main_Data::SetProjectPath(".");
}
#else
// case sensitive
Main_Data::SetProjectPath(argv[it - args.begin() + 1]);
#endif
}
else if (*it == "--save-path") {
++it;
if (it == args.end()) {
return;
}
// case sensitive
Main_Data::SetSavePath(argv[it - args.begin() + 1]);
}
else if (*it == "--new-game") {
new_game_flag = true;
}
else if (*it == "--load-game-id") {
++it;
if (it == args.end()) {
return;
}
load_game_id = atoi((*it).c_str());
}
/*else if (*it == "--load-game") {
// load game by filename
}
else if (*it == "--database") {
// overwrite database file
}
else if (*it == "--map-tree") {
// overwrite map tree file
}
else if (*it == "--start-map") {
// overwrite start map by filename
}*/
else if (*it == "--seed") {
++it;
if (it == args.end()) {
return;
}
srand(atoi((*it).c_str()));
Utils::SeedRandomNumberGenerator(atoi((*it).c_str()));
}
else if (*it == "--start-map-id") {
++it;
if (it == args.end()) {
return;
}
start_map_id = atoi((*it).c_str());
}
else if (*it == "--start-position") {
++it;
if (it == args.end() || it == args.end()-1) {
return;
}
party_x_position = atoi((*it).c_str());
++it;
party_y_position = atoi((*it).c_str());
}
else if (*it == "--start-party") {
while (++it != args.end() && isdigit((*it)[0])) {
party_members.push_back(atoi((*it).c_str()));
}
--it;
}
else if (*it == "--engine") {
++it;
if (it == args.end()) {
return;
}
if (*it == "rpg2k" || *it == "2000") {
engine = EngineRpg2k;
}
else if (*it == "rpg2kv150" || *it == "2000v150") {
engine = EngineRpg2k | EngineMajorUpdated;
}
else if (*it == "rpg2k3" || *it == "2003") {
engine = EngineRpg2k3;
}
else if (*it == "rpg2k3v105" || *it == "2003v105") {
engine = EngineRpg2k3 | EngineMajorUpdated;
}
else if (*it == "rpg2k3e") {
engine = EngineRpg2k3 | EngineMajorUpdated | EngineRpg2k3E;
}
}
else if (*it == "--encoding") {
++it;
if (it == args.end()) {
return;
}
forced_encoding = *it;
}
else if (*it == "--disable-audio") {
no_audio_flag = true;
}
else if (*it == "--disable-rtp") {
no_rtp_flag = true;
}
else if (*it == "--version" || *it == "-v") {
PrintVersion();
exit(0);
}
else if (*it == "--help" || *it == "-h" || *it == "/?") {
PrintUsage();
exit(0);
}
#ifdef EMSCRIPTEN
else if (*it == "--game") {
++it;
if (it == args.end()) {
return;
}
emscripten_game_name = *it;
}
#endif
}
}
static void OnSystemFileReady(FileRequestResult* result) {
Game_System::SetSystemName(result->file);
}
void Player::CreateGameObjects() {
GetEncoding();
escape_symbol = ReaderUtil::Recode("\\", encoding);
if (escape_symbol.empty()) {
Output::Error("Invalid encoding: %s.", encoding.c_str());
}
std::string game_path = Main_Data::GetProjectPath();
std::string save_path = Main_Data::GetSavePath();
if (game_path == save_path) {
Output::Debug("Using %s as Game and Save directory", game_path.c_str());
} else {
Output::Debug("Using %s as Game directory", game_path.c_str());
Output::Debug("Using %s as Save directory", save_path.c_str());
}
LoadDatabase();
std::string ini_file = FileFinder::FindDefault(INI_NAME);
INIReader ini(ini_file);
if (ini.ParseError() != -1) {
std::string title = ini.Get("RPG_RT", "GameTitle", GAME_TITLE);
game_title = ReaderUtil::Recode(title, encoding);
no_rtp_flag = ini.Get("RPG_RT", "FullPackageFlag", "0") == "1"? true : no_rtp_flag;
}
std::stringstream title;
if (!game_title.empty()) {
Output::Debug("Loading game %s", game_title.c_str());
title << game_title << " - ";
} else {
Output::Warning("Could not read game title.");
}
title << GAME_TITLE;
DisplayUi->SetTitle(title.str());
if (engine == EngineNone) {
if (Data::system.ldb_id == 2003) {
engine = EngineRpg2k3;
if (FileFinder::FindDefault("ultimate_rt_eb.dll").empty()) {
// Heuristic: Detect if game was converted from 2000 to 2003 and
// no typical 2003 feature was used at all (breaks .flow e.g.)
if (Data::classes.size() == 1 &&
Data::classes[0].name.empty() &&
Data::system.menu_commands.empty() &&
Data::system.system2_name.empty() &&
Data::battleranimations.size() == 1 &&
Data::battleranimations[0].name.empty()) {
engine = EngineRpg2k;
Output::Debug("Using RPG2k Interpreter (heuristic)");
} else {
Output::Debug("Using RPG2k3 Interpreter");
}
} else {
engine |= EngineRpg2k3E;
Output::Debug("Using RPG2k3 (English release, v1.11) Interpreter");
}
} else {
engine = EngineRpg2k;
Output::Debug("Using RPG2k Interpreter");
}
if (FileFinder::IsMajorUpdatedTree()) {
engine |= EngineMajorUpdated;
Output::Debug("RPG2k >= v1.50 / RPG2k3 >= v1.05 detected");
} else {
Output::Debug("RPG2k < v1.50 / RPG2k3 < v1.05 detected");
}
}
Output::Debug("Engine configured as: 2k=%d 2k3=%d 2k3Legacy=%d MajorUpdated=%d 2k3E=%d", Player::IsRPG2k(), Player::IsRPG2k3(), Player::IsRPG2k3Legacy(), Player::IsMajorUpdatedVersion(), Player::IsRPG2k3E());
if (!no_rtp_flag) {
FileFinder::InitRtpPaths();
}
ResetGameObjects();
}
void Player::ResetGameObjects() {
if (Data::system.system_name != Game_System::GetSystemName()) {
FileRequestAsync* request = AsyncHandler::RequestFile("System", Data::system.system_name);
request->SetImportantFile(true);
system_request_id = request->Bind(&OnSystemFileReady);
request->Start();
}
Main_Data::game_data.Setup();
Game_Actors::Init();
Game_Map::Init();
Game_Message::Init();
Game_Switches.Reset();
Game_System::Init();
Game_Temp::Init();
Game_Variables.Reset();
Main_Data::game_enemyparty.reset(new Game_EnemyParty());
Main_Data::game_party.reset(new Game_Party());
Main_Data::game_player.reset(new Game_Player());
Main_Data::game_screen.reset(new Game_Screen());
FrameReset();
}
void Player::LoadDatabase() {
// Load Database
Data::Clear();
if (!FileFinder::IsRPG2kProject(*FileFinder::GetDirectoryTree()) &&
!FileFinder::IsEasyRpgProject(*FileFinder::GetDirectoryTree())) {
// Unlikely to happen because of the game browser only launches valid games
Output::Debug("%s is not a supported project", Main_Data::GetProjectPath().c_str());
Output::Error("%s\n\n%s\n\n%s\n\n%s","No valid game was found.",
"EasyRPG must be run from a game folder containing\nRPG_RT.ldb and RPG_RT.lmt.",
"This engine only supports RPG Maker 2000 and 2003\ngames.",
"RPG Maker XP, VX, VX Ace and MV are NOT supported.");
}
// Try loading EasyRPG project files first, then fallback to normal RPG Maker
std::string edb = FileFinder::FindDefault(DATABASE_NAME_EASYRPG);
std::string emt = FileFinder::FindDefault(TREEMAP_NAME_EASYRPG);
bool easyrpg_project = !edb.empty() && !emt.empty();
if (easyrpg_project) {
if (!LDB_Reader::LoadXml(edb)) {
Output::ErrorStr(LcfReader::GetError());
}
if (!LMT_Reader::LoadXml(emt)) {
Output::ErrorStr(LcfReader::GetError());
}
}
else {
std::string ldb = FileFinder::FindDefault(DATABASE_NAME);
std::string lmt = FileFinder::FindDefault(TREEMAP_NAME);
if (!LDB_Reader::Load(ldb, encoding)) {
Output::ErrorStr(LcfReader::GetError());
}
if (!LMT_Reader::Load(lmt, encoding)) {
Output::ErrorStr(LcfReader::GetError());
}
}
}
static void OnMapSaveFileReady(FileRequestResult*) {
Game_Actors::Fixup();
Game_Map::SetupFromSave();
Main_Data::game_player->MoveTo(
Main_Data::game_data.party_location.position_x,
Main_Data::game_data.party_location.position_y
);
Main_Data::game_player->Refresh();
RPG::Music current_music = Main_Data::game_data.system.current_music;
Game_System::BgmStop();
Game_System::BgmPlay(current_music);
}
void Player::LoadSavegame(const std::string& save_name) {
std::unique_ptr<RPG::Save> save = LSD_Reader::Load(save_name, encoding);
if (!save.get()) {
Output::Error("%s", LcfReader::GetError().c_str());
}
Main_Data::game_data = *save.get();
Main_Data::game_data.system.Fixup();
int map_id = save->party_location.map_id;
FileRequestAsync* map = Game_Map::RequestMap(map_id);
save_request_id = map->Bind(&OnMapSaveFileReady);
map->SetImportantFile(true);
FileRequestAsync* system = AsyncHandler::RequestFile("System", Game_System::GetSystemName());
system->SetImportantFile(true);
system_request_id = system->Bind(&OnSystemFileReady);
map->Start();
system->Start();
}
static void OnMapFileReady(FileRequestResult*) {
int map_id = Player::start_map_id == -1 ?
Data::treemap.start.party_map_id : Player::start_map_id;
int x_pos = Player::party_x_position == -1 ?
Data::treemap.start.party_x : Player::party_x_position;
int y_pos = Player::party_y_position == -1 ?
Data::treemap.start.party_y : Player::party_y_position;
if (Player::party_members.size() > 0) {
Main_Data::game_party->Clear();
std::vector<int>::iterator member;
for (member = Player::party_members.begin(); member != Player::party_members.end(); ++member) {
Main_Data::game_party->AddActor(*member);
}
}
Game_Map::Setup(map_id);
Main_Data::game_player->MoveTo(x_pos, y_pos);
Main_Data::game_player->Refresh();
Game_Map::PlayBgm();
}
void Player::SetupPlayerSpawn() {
int map_id = Player::start_map_id == -1 ?
Data::treemap.start.party_map_id : Player::start_map_id;
FileRequestAsync* request = Game_Map::RequestMap(map_id);
map_request_id = request->Bind(&OnMapFileReady);
request->SetImportantFile(true);
request->Start();
}
std::string Player::GetEncoding() {
encoding = forced_encoding;
// command line > ini > detection > current locale
if (encoding.empty()) {
std::string ini = FileFinder::FindDefault(INI_NAME);
encoding = ReaderUtil::GetEncoding(ini);
}
if (encoding.empty() || encoding == "auto") {
encoding = "";
std::string ldb = FileFinder::FindDefault(DATABASE_NAME);
std::vector<std::string> encodings = ReaderUtil::DetectEncodings(ldb);
#ifndef EMSCRIPTEN
for (std::string& enc : encodings) {
// Heuristic: Check if encoded title and system name matches the one on the filesystem
// When yes is a good encoding. Otherwise try the next ones.
escape_symbol = ReaderUtil::Recode("\\", enc);
if (escape_symbol.empty()) {
// Bad encoding
Output::Debug("Bad encoding: %s. Trying next.", enc.c_str());
continue;
}
if ((Data::system.title_name.empty() ||
!FileFinder::FindImage("Title", ReaderUtil::Recode(Data::system.title_name, enc)).empty()) &&
(Data::system.system_name.empty() ||
!FileFinder::FindImage("System", ReaderUtil::Recode(Data::system.system_name, enc)).empty())) {
// Looks like a good encoding
encoding = enc;
break;
} else {
Output::Debug("Detected encoding: %s. Files not found. Trying next.", enc.c_str());
}
}
#endif
if (!encodings.empty() && encoding.empty()) {
// No encoding found that matches the files, maybe RTP missing.
// Use the first one instead
encoding = encodings[0];
}
escape_symbol = "";
if (!encoding.empty()) {
Output::Debug("Detected encoding: %s", encoding.c_str());
} else {
Output::Debug("Encoding not detected");
encoding = ReaderUtil::GetLocaleEncoding();
}
}
return encoding;
}
void Player::PrintVersion() {
std::string additional(PLAYER_ADDTL);
std::stringstream version;
version << PLAYER_VERSION;
if (!additional.empty())
version << " " << additional;
std::cout << "EasyRPG Player " << version.str() << std::endl;
}
void Player::PrintUsage() {
std::cout <<
R"(EasyRPG Player - An open source interpreter for RPG Maker 2000/2003 games.
Options:
--battle-test N Start a battle test with monster party N.
--disable-audio Disable audio (in case you prefer your own music).
--disable-rtp Disable support for the Runtime Package (RTP).
--encoding N Instead of auto detecting the encoding or using
the one in RPG_RT.ini, the encoding N is used.
Use "auto" for automatic detection.
--engine ENGINE Disable auto detection of the simulated engine.
Possible options:
rpg2k - RPG Maker 2000 engine (v1.00 - v1.10)
rpg2kv150 - RPG Maker 2000 engine (v1.50 - v1.51)
rpg2k3 - RPG Maker 2003 engine (v1.00 - v1.04)
rpg2k3v105 - RPG Maker 2003 engine (v1.05 - v1.09a)
rpg2k3e - RPG Maker 2003 (English release) engine
--fullscreen Start in fullscreen mode.
--show-fps Enable frames per second counter.
--enable-mouse Use mouse click for decision and scroll wheel for lists
--hide-title Hide the title background image and center the
command menu.
--load-game-id N Skip the title scene and load SaveN.lsd
(N is padded to two digits).
--new-game Skip the title scene and start a new game directly.
--project-path PATH Instead of using the working directory the game in
PATH is used.
--save-path PATH Instead of storing save files in the game directory
they are stored in PATH. The directory must exist.
When using the game browser all games will share
the same save directory!
--seed N Seeds the random number generator with N.
--start-map-id N Overwrite the map used for new games and use.
MapN.lmu instead (N is padded to four digits).
Incompatible with --load-game-id.
--start-position X Y Overwrite the party start position and move the
party to position (X, Y).
Incompatible with --load-game-id.
--start-party A B... Overwrite the starting party members with the actors
with IDs A, B, C...
Incompatible with --load-game-id.
--test-play Enable TestPlay mode.
--window Start in window mode.
-v, --version Display program version and exit.
-h, --help Display this help and exit.
For compatibility with the legacy RPG Maker runtime the following arguments
are supported:
BattleTest N Same as --battle-test. When N is not a valid number
the 4th argument is used as the party id.
HideTitle Same as --hide-title.
TestPlay Same as --test-play.
Window Same as --window.
Game related parameters (e.g. new-game and load-game-id) don't work correctly when the
startup directory does not contain a valid game (and the game browser loads)
Alex, EV0001 and the EasyRPG authors wish you a lot of fun!)" << std::endl;
}
bool Player::IsRPG2k() {
return (engine & EngineRpg2k) == EngineRpg2k;
}
bool Player::IsRPG2k3Legacy() {
return (engine == EngineRpg2k3 || engine == (EngineRpg2k3 | EngineMajorUpdated));
}
bool Player::IsRPG2k3() {
return (engine & EngineRpg2k3) == EngineRpg2k3;
}
bool Player::IsMajorUpdatedVersion() {
return (engine & EngineMajorUpdated) == EngineMajorUpdated;
}
bool Player::IsRPG2k3E() {
return (engine & EngineRpg2k3E) == EngineRpg2k3E;
}
bool Player::IsCP932() {
return (encoding == "ibm-943_P15A-2003" || encoding == "932");
}
#if (defined(_WIN32) && defined(NDEBUG) && defined(WINVER) && WINVER >= 0x0600)
// Minidump code for Windows
// Original Author: Oleg Starodumov (www.debuginfo.com)
// Modified by EasyRPG Team
typedef BOOL (__stdcall *MiniDumpWriteDumpFunc) (
IN HANDLE hProcess,
IN DWORD ProcessId,
IN HANDLE hFile,
IN MINIDUMP_TYPE DumpType,
IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
);
static WCHAR szModulName[_MAX_FNAME];
static MiniDumpWriteDumpFunc TheMiniDumpWriteDumpFunc;
static BOOL CALLBACK MyMiniDumpCallback(PVOID,
const PMINIDUMP_CALLBACK_INPUT pInput,
PMINIDUMP_CALLBACK_OUTPUT pOutput
) {