162 lines
6.0 KiB
C++
162 lines
6.0 KiB
C++
#include "GameState.h"
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
|
|
namespace FRG {
|
|
|
|
void GameState::save(nlohmann::json& out) const
|
|
{
|
|
// Timestamp
|
|
std::time_t now = std::time(nullptr);
|
|
std::tm* tm = std::localtime(&now);
|
|
std::ostringstream ts;
|
|
ts << std::put_time(tm, "%Y-%m-%d %H:%M");
|
|
out["savedAt"] = ts.str();
|
|
|
|
// --- World ---
|
|
out["currentLocationName"] = currentLocationName;
|
|
out["isDarklands"] = isDarklands;
|
|
out["isNight"] = isNight;
|
|
out["isDawn"] = isDawn;
|
|
|
|
// --- Player resources ---
|
|
out["playerHp"] = playerHp;
|
|
out["playerMaxHp"] = playerMaxHp;
|
|
out["money"] = money;
|
|
|
|
// --- Script globals ---
|
|
nlohmann::json gi = nlohmann::json::object();
|
|
for (const auto& [k, v] : globalInts) gi[k] = v;
|
|
out["globalInts"] = std::move(gi);
|
|
|
|
nlohmann::json gf = nlohmann::json::object();
|
|
for (const auto& [k, v] : globalFloats) gf[k] = v;
|
|
out["globalFloats"] = std::move(gf);
|
|
|
|
// --- Tutorial ---
|
|
out["tutorialStep"] = static_cast<int>(tutorialStep);
|
|
out["uniIntTutorialState"] = static_cast<int>(uniIntTutorialState);
|
|
out["tutorialPhonePickedUp"] = tutorialPhonePickedUp;
|
|
out["tutorialJournalPickedUp"] = tutorialJournalPickedUp;
|
|
out["tutorialPhoneChatScreenOpened"] = tutorialPhoneChatScreenOpened;
|
|
out["tutorialJournalScreenOpened"] = tutorialJournalScreenOpened;
|
|
out["tutorialNeedOpenTaxiScreen"] = tutorialNeedOpenTaxiScreen;
|
|
|
|
// --- Phone / chat ---
|
|
out["chatUnread"] = { chatUnread[0], chatUnread[1], chatUnread[2] };
|
|
out["chatPreviewMsg"] = { chatPreviewMsg[0], chatPreviewMsg[1], chatPreviewMsg[2] };
|
|
|
|
nlohmann::json histories = nlohmann::json::array();
|
|
for (int i = 0; i < 3; ++i) {
|
|
nlohmann::json msgs = nlohmann::json::array();
|
|
for (const auto& msg : chatHistory[i]) {
|
|
msgs.push_back({ {"text", msg.text}, {"incoming", msg.incoming} });
|
|
}
|
|
histories.push_back(std::move(msgs));
|
|
}
|
|
out["chatHistory"] = std::move(histories);
|
|
|
|
// --- Inventory ---
|
|
nlohmann::json invJson;
|
|
inventory.save(invJson);
|
|
out["inventory"] = std::move(invJson);
|
|
|
|
// --- Quest journal ---
|
|
nlohmann::json questJson;
|
|
questJournal.save(questJson);
|
|
out["questJournal"] = std::move(questJson);
|
|
|
|
// --- Locations ---
|
|
nlohmann::json locsJson = nlohmann::json::object();
|
|
for (const auto& [name, loc] : locations) {
|
|
if (loc) {
|
|
nlohmann::json locJson;
|
|
loc->save(locJson);
|
|
locsJson[name] = std::move(locJson);
|
|
}
|
|
}
|
|
out["locations"] = std::move(locsJson);
|
|
|
|
out["taxiIsCalled"] = taxiIsCalled;
|
|
}
|
|
|
|
void GameState::load(const nlohmann::json& in)
|
|
{
|
|
// --- World ---
|
|
currentLocationName = in.value("currentLocationName", currentLocationName);
|
|
isDarklands = in.value("isDarklands", isDarklands);
|
|
isNight = in.value("isNight", isNight);
|
|
isDawn = in.value("isDawn", isDawn);
|
|
|
|
// --- Player resources ---
|
|
playerHp = in.value("playerHp", playerHp);
|
|
playerMaxHp = in.value("playerMaxHp", playerMaxHp);
|
|
money = in.value("money", money);
|
|
|
|
// --- Script globals ---
|
|
globalInts.clear();
|
|
if (in.contains("globalInts") && in["globalInts"].is_object()) {
|
|
for (const auto& [k, v] : in["globalInts"].items())
|
|
globalInts[k] = v.get<int>();
|
|
}
|
|
globalFloats.clear();
|
|
if (in.contains("globalFloats") && in["globalFloats"].is_object()) {
|
|
for (const auto& [k, v] : in["globalFloats"].items())
|
|
globalFloats[k] = v.get<float>();
|
|
}
|
|
|
|
// --- Tutorial ---
|
|
tutorialStep = static_cast<TutorialStep>(in.value("tutorialStep", static_cast<int>(tutorialStep)));
|
|
uniIntTutorialState = static_cast<UniIntTutorialState>(in.value("uniIntTutorialState", static_cast<int>(uniIntTutorialState)));
|
|
tutorialPhonePickedUp = in.value("tutorialPhonePickedUp", tutorialPhonePickedUp);
|
|
tutorialJournalPickedUp = in.value("tutorialJournalPickedUp", tutorialJournalPickedUp);
|
|
tutorialPhoneChatScreenOpened = in.value("tutorialPhoneChatScreenOpened", tutorialPhoneChatScreenOpened);
|
|
tutorialJournalScreenOpened = in.value("tutorialJournalScreenOpened", tutorialJournalScreenOpened);
|
|
tutorialNeedOpenTaxiScreen = in.value("tutorialNeedOpenTaxiScreen", tutorialNeedOpenTaxiScreen);
|
|
|
|
// --- Phone / chat ---
|
|
if (in.contains("chatUnread") && in["chatUnread"].is_array()) {
|
|
for (int i = 0; i < 3 && i < static_cast<int>(in["chatUnread"].size()); ++i)
|
|
chatUnread[i] = in["chatUnread"][i].get<bool>();
|
|
}
|
|
if (in.contains("chatPreviewMsg") && in["chatPreviewMsg"].is_array()) {
|
|
for (int i = 0; i < 3 && i < static_cast<int>(in["chatPreviewMsg"].size()); ++i)
|
|
chatPreviewMsg[i] = in["chatPreviewMsg"][i].get<std::string>();
|
|
}
|
|
if (in.contains("chatHistory") && in["chatHistory"].is_array()) {
|
|
for (int i = 0; i < 3 && i < static_cast<int>(in["chatHistory"].size()); ++i) {
|
|
chatHistory[i].clear();
|
|
if (in["chatHistory"][i].is_array()) {
|
|
for (const auto& msg : in["chatHistory"][i]) {
|
|
StoredChatMessage m;
|
|
m.text = msg.value("text", "");
|
|
m.incoming = msg.value("incoming", false);
|
|
chatHistory[i].push_back(std::move(m));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Inventory ---
|
|
if (in.contains("inventory")) inventory.load(in["inventory"]);
|
|
|
|
// --- Quest journal ---
|
|
if (in.contains("questJournal")) questJournal.load(in["questJournal"]);
|
|
|
|
// --- Locations ---
|
|
if (in.contains("locations") && in["locations"].is_object()) {
|
|
for (const auto& [name, locJson] : in["locations"].items()) {
|
|
auto it = locations.find(name);
|
|
if (it != locations.end() && it->second) {
|
|
it->second->load(locJson);
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Taxi ---
|
|
taxiIsCalled = in.value("taxiIsCalled", taxiIsCalled);
|
|
}
|
|
|
|
} // namespace FRG
|