forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscene_save.cpp
167 lines (139 loc) · 4.6 KB
/
scene_save.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
/*
* 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 <sstream>
#ifdef EMSCRIPTEN
# include <emscripten.h>
#endif
#include <lcf/data.h>
#include "dynrpg.h"
#include "filefinder.h"
#include "game_actor.h"
#include "game_map.h"
#include "game_party.h"
#include "game_switches.h"
#include "game_variables.h"
#include "game_party.h"
#include "game_actors.h"
#include "game_system.h"
#include "game_targets.h"
#include "game_screen.h"
#include "game_pictures.h"
#include <lcf/lsd/reader.h>
#include "output.h"
#include "player.h"
#include "scene_save.h"
#include "version.h"
Scene_Save::Scene_Save() :
Scene_File(ToString(lcf::Data::terms.save_game_message)) {
Scene::type = Scene::Save;
}
void Scene_Save::Start() {
Scene_File::Start();
for (int i = 0; i < Utils::Clamp<int32_t>(lcf::Data::system.easyrpg_max_savefiles, 3, 99); i++) {
file_windows[i]->SetHasSave(true);
file_windows[i]->Refresh();
}
}
void Scene_Save::Action(int index) {
Save(fs, index + 1);
Scene::Pop();
}
std::string Scene_Save::GetSaveFilename(const FilesystemView& fs, int slot_id) {
const auto save_file = fmt::format("Save{:02d}.lsd", slot_id);
std::string filename = fs.FindFile(save_file);
if (filename.empty()) {
filename = save_file;
}
return filename;
}
bool Scene_Save::Save(const FilesystemView& fs, int slot_id, bool prepare_save) {
const auto filename = GetSaveFilename(fs, slot_id);
Output::Debug("Saving to {}", filename);
auto save_stream = FileFinder::Save().OpenOutputStream(filename);
if (!save_stream) {
Output::Warning("Failed saving to {}", filename);
return false;
}
return Save(save_stream, slot_id, prepare_save);
}
bool Scene_Save::Save(std::ostream& os, int slot_id, bool prepare_save) {
lcf::rpg::Save save;
auto& title = save.title;
// TODO: Maybe find a better place to setup the save file?
int size = (int)Main_Data::game_party->GetActors().size();
Game_Actor* actor;
if (size > 3) {
actor = Main_Data::game_party->GetActors()[3];
title.face4_id = actor->GetFaceIndex();
title.face4_name = ToString(actor->GetFaceName());
}
if (size > 2) {
actor = Main_Data::game_party->GetActors()[2];
title.face3_id = actor->GetFaceIndex();
title.face3_name = ToString(actor->GetFaceName());
}
if (size > 1) {
actor = Main_Data::game_party->GetActors()[1];
title.face2_id = actor->GetFaceIndex();
title.face2_name = ToString(actor->GetFaceName());
}
if (size > 0) {
actor = Main_Data::game_party->GetActors()[0];
title.face1_id = actor->GetFaceIndex();
title.face1_name = ToString(actor->GetFaceName());
title.hero_hp = actor->GetHp();
title.hero_level = actor->GetLevel();
title.hero_name = ToString(actor->GetName());
}
Main_Data::game_system->SetSaveSlot(slot_id);
save.party_location = Main_Data::game_player->GetSaveData();
Game_Map::PrepareSave(save);
if (prepare_save) {
lcf::LSD_Reader::PrepareSave(save, PLAYER_SAVEGAME_VERSION);
Main_Data::game_system->IncSaveCount();
}
save.targets = Main_Data::game_targets->GetSaveData();
save.system = Main_Data::game_system->GetSaveData();
save.system.switches = Main_Data::game_switches->GetData();
save.system.variables = Main_Data::game_variables->GetData();
save.inventory = Main_Data::game_party->GetSaveData();
save.actors = Main_Data::game_actors->GetSaveData();
save.screen = Main_Data::game_screen->GetSaveData();
save.pictures = Main_Data::game_pictures->GetSaveData();
save.system.scene = Scene::instance ? Scene::rpgRtSceneFromSceneType(Scene::instance->type) : -1;
// 2k RPG_RT always stores SaveMapEvent with map_id == 0.
if (Player::IsRPG2k()) {
for (auto& sme: save.map_info.events) {
sme.map_id = 0;
}
}
auto lcf_engine = Player::IsRPG2k3() ? lcf::EngineVersion::e2k3 : lcf::EngineVersion::e2k;
bool res = lcf::LSD_Reader::Save(os, save, lcf_engine, Player::encoding);
DynRpg::Save(slot_id);
#ifdef EMSCRIPTEN
// Save changed file system
EM_ASM({
FS.syncfs(function(err) {
});
});
#endif
return res;
}
bool Scene_Save::IsSlotValid(int) {
return true;
}