2018-10-07 03:42:33 +00:00
|
|
|
#pragma once
|
2015-08-17 05:26:58 +00:00
|
|
|
|
2020-03-30 00:54:29 +00:00
|
|
|
#include <array>
|
2015-08-17 20:33:58 +00:00
|
|
|
#include <memory>
|
2019-09-22 21:52:05 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2022-02-22 06:59:47 +00:00
|
|
|
#include "Runtime/AutoMapper/CMapWorldInfo.hpp"
|
2019-09-22 21:52:05 +00:00
|
|
|
#include "Runtime/CBasics.hpp"
|
|
|
|
#include "Runtime/CGameOptions.hpp"
|
|
|
|
#include "Runtime/CPlayerState.hpp"
|
2021-06-03 21:39:49 +00:00
|
|
|
#include "Runtime/CScriptMailbox.hpp"
|
2019-09-22 21:52:05 +00:00
|
|
|
#include "Runtime/World/CWorld.hpp"
|
|
|
|
#include "Runtime/World/CWorldTransManager.hpp"
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2016-09-25 01:58:20 +00:00
|
|
|
class CSaveWorldMemory;
|
|
|
|
|
2022-02-21 09:04:16 +00:00
|
|
|
class WordBitmap {
|
2022-02-22 06:59:47 +00:00
|
|
|
std::vector<u32> x0_words;
|
|
|
|
size_t x10_bitCount = 0;
|
2022-02-21 09:04:16 +00:00
|
|
|
|
|
|
|
public:
|
2022-02-22 06:59:47 +00:00
|
|
|
void Reserve(size_t bitCount) { x0_words.reserve((bitCount + 31) / 32); }
|
|
|
|
[[nodiscard]] size_t GetBitCount() const { return x10_bitCount; }
|
|
|
|
[[nodiscard]] bool GetBit(size_t idx) const {
|
2022-02-21 09:04:16 +00:00
|
|
|
size_t wordIdx = idx / 32;
|
2022-02-22 06:59:47 +00:00
|
|
|
if (wordIdx >= x0_words.size()) {
|
2022-02-21 09:04:16 +00:00
|
|
|
return false;
|
2022-02-22 06:59:47 +00:00
|
|
|
}
|
2022-02-21 09:04:16 +00:00
|
|
|
size_t wordCur = idx % 32;
|
2022-02-22 06:59:47 +00:00
|
|
|
return ((x0_words[wordIdx] >> wordCur) & 0x1) != 0u;
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
2022-02-22 06:59:47 +00:00
|
|
|
void SetBit(size_t idx) {
|
2022-02-21 09:04:16 +00:00
|
|
|
size_t wordIdx = idx / 32;
|
2022-02-22 06:59:47 +00:00
|
|
|
while (wordIdx >= x0_words.size()) {
|
|
|
|
x0_words.push_back(0);
|
|
|
|
}
|
2022-02-21 09:04:16 +00:00
|
|
|
size_t wordCur = idx % 32;
|
2022-02-22 06:59:47 +00:00
|
|
|
x0_words[wordIdx] |= (1 << wordCur);
|
|
|
|
x10_bitCount = std::max(x10_bitCount, idx + 1);
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
2022-02-22 06:59:47 +00:00
|
|
|
void UnsetBit(size_t idx) {
|
2022-02-21 09:04:16 +00:00
|
|
|
size_t wordIdx = idx / 32;
|
2022-02-22 06:59:47 +00:00
|
|
|
while (wordIdx >= x0_words.size()) {
|
|
|
|
x0_words.push_back(0);
|
|
|
|
}
|
2022-02-21 09:04:16 +00:00
|
|
|
size_t wordCur = idx % 32;
|
2022-02-22 06:59:47 +00:00
|
|
|
x0_words[wordIdx] &= ~(1 << wordCur);
|
|
|
|
x10_bitCount = std::max(x10_bitCount, idx + 1);
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
2022-02-22 06:59:47 +00:00
|
|
|
void Clear() {
|
|
|
|
x0_words.clear();
|
|
|
|
x10_bitCount = 0;
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Iterator {
|
|
|
|
friend class WordBitmap;
|
|
|
|
const WordBitmap& m_bmp;
|
|
|
|
size_t m_idx = 0;
|
|
|
|
Iterator(const WordBitmap& bmp, size_t idx) : m_bmp(bmp), m_idx(idx) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
using value_type = bool;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = bool*;
|
|
|
|
using reference = bool&;
|
|
|
|
|
|
|
|
Iterator& operator++() {
|
|
|
|
++m_idx;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-02-22 06:59:47 +00:00
|
|
|
bool operator*() const { return m_bmp.GetBit(m_idx); }
|
2022-02-21 09:04:16 +00:00
|
|
|
bool operator!=(const Iterator& other) const { return m_idx != other.m_idx; }
|
|
|
|
};
|
2022-02-22 06:59:47 +00:00
|
|
|
[[nodiscard]] Iterator begin() const { return Iterator(*this, 0); }
|
|
|
|
[[nodiscard]] Iterator end() const { return Iterator(*this, x10_bitCount); }
|
2022-02-21 09:04:16 +00:00
|
|
|
};
|
|
|
|
|
2021-06-04 06:08:05 +00:00
|
|
|
class CScriptLayerManager {
|
2018-12-08 05:30:43 +00:00
|
|
|
friend class CSaveWorldIntermediate;
|
|
|
|
std::vector<CWorldLayers::Area> x0_areaLayers;
|
2022-02-21 09:04:16 +00:00
|
|
|
WordBitmap x10_saveLayers;
|
2017-01-07 01:58:05 +00:00
|
|
|
|
2016-09-25 01:58:20 +00:00
|
|
|
public:
|
2021-06-04 06:08:05 +00:00
|
|
|
CScriptLayerManager() = default;
|
2022-02-18 07:37:54 +00:00
|
|
|
CScriptLayerManager(CInputStream& reader, const CWorldSaveGameInfo& saveWorld);
|
2016-10-09 07:45:04 +00:00
|
|
|
|
2022-02-22 06:59:47 +00:00
|
|
|
[[nodiscard]] bool IsLayerActive(int areaIdx, int layerIdx) const {
|
|
|
|
return ((x0_areaLayers[areaIdx].m_layerBits >> layerIdx) & 1) != 0u;
|
|
|
|
}
|
2016-09-25 01:58:20 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void SetLayerActive(int areaIdx, int layerIdx, bool active) {
|
2022-02-22 06:59:47 +00:00
|
|
|
if (active) {
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_areaLayers[areaIdx].m_layerBits |= uint64_t(1) << layerIdx;
|
2022-02-22 06:59:47 +00:00
|
|
|
} else {
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_areaLayers[areaIdx].m_layerBits &= ~(uint64_t(1) << layerIdx);
|
2022-02-22 06:59:47 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-10-09 07:45:04 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void InitializeWorldLayers(const std::vector<CWorldLayers::Area>& layers);
|
2016-10-09 21:41:23 +00:00
|
|
|
|
2022-02-22 06:59:47 +00:00
|
|
|
[[nodiscard]] u32 GetAreaLayerCount(int areaIdx) const { return x0_areaLayers[areaIdx].m_layerCount; }
|
|
|
|
[[nodiscard]] u32 GetAreaCount() const { return x0_areaLayers.size(); }
|
2016-10-09 21:41:23 +00:00
|
|
|
|
2022-02-18 07:37:54 +00:00
|
|
|
void PutTo(COutputStream& writer) const;
|
2016-09-25 01:58:20 +00:00
|
|
|
};
|
2015-08-17 22:05:00 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
class CWorldState {
|
|
|
|
CAssetId x0_mlvlId;
|
|
|
|
TAreaId x4_areaId = kInvalidAreaId;
|
2021-06-04 06:08:05 +00:00
|
|
|
std::shared_ptr<CScriptMailbox> x8_mailbox;
|
2018-12-08 05:30:43 +00:00
|
|
|
std::shared_ptr<CMapWorldInfo> xc_mapWorldInfo;
|
|
|
|
CAssetId x10_desiredAreaAssetId;
|
2021-06-04 06:08:05 +00:00
|
|
|
std::shared_ptr<CScriptLayerManager> x14_layerState;
|
2017-01-07 01:58:05 +00:00
|
|
|
|
2016-08-14 03:00:58 +00:00
|
|
|
public:
|
2018-12-08 05:30:43 +00:00
|
|
|
explicit CWorldState(CAssetId id);
|
2022-02-18 07:37:54 +00:00
|
|
|
CWorldState(CInputStream& reader, CAssetId mlvlId, const CWorldSaveGameInfo& saveWorld);
|
2018-12-08 05:30:43 +00:00
|
|
|
CAssetId GetWorldAssetId() const { return x0_mlvlId; }
|
|
|
|
void SetAreaId(TAreaId aid) { x4_areaId = aid; }
|
|
|
|
TAreaId GetCurrentAreaId() const { return x4_areaId; }
|
|
|
|
CAssetId GetDesiredAreaAssetId() const { return x10_desiredAreaAssetId; }
|
|
|
|
void SetDesiredAreaAssetId(CAssetId id) { x10_desiredAreaAssetId = id; }
|
2021-06-04 06:08:05 +00:00
|
|
|
const std::shared_ptr<CScriptMailbox>& Mailbox() const { return x8_mailbox; }
|
2018-12-08 05:30:43 +00:00
|
|
|
const std::shared_ptr<CMapWorldInfo>& MapWorldInfo() const { return xc_mapWorldInfo; }
|
2021-06-04 06:08:05 +00:00
|
|
|
const std::shared_ptr<CScriptLayerManager>& GetLayerState() const { return x14_layerState; }
|
2022-02-18 07:37:54 +00:00
|
|
|
void PutTo(COutputStream& writer, const CWorldSaveGameInfo& savw) const;
|
2016-08-14 03:00:58 +00:00
|
|
|
};
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
class CGameState {
|
|
|
|
friend class CStateManager;
|
|
|
|
|
2020-03-30 00:54:29 +00:00
|
|
|
std::array<bool, 128> x0_{};
|
2020-04-16 23:59:19 +00:00
|
|
|
u32 x80_ = 0;
|
2018-12-08 05:30:43 +00:00
|
|
|
CAssetId x84_mlvlId;
|
|
|
|
std::vector<CWorldState> x88_worldStates;
|
|
|
|
std::shared_ptr<CPlayerState> x98_playerState;
|
|
|
|
std::shared_ptr<CWorldTransManager> x9c_transManager;
|
|
|
|
double xa0_playTime = 0.0;
|
|
|
|
CPersistentOptions xa8_systemOptions;
|
|
|
|
CGameOptions x17c_gameOptions;
|
|
|
|
CHintOptions x1f8_hintOptions;
|
|
|
|
u32 x20c_saveFileIdx = 0;
|
|
|
|
u64 x210_cardSerial = 0;
|
|
|
|
std::vector<u8> x218_backupBuf;
|
2020-04-20 04:57:50 +00:00
|
|
|
bool x228_24_hardMode : 1 = false;
|
|
|
|
bool x228_25_initPowerupsAtFirstSpawn : 1 = true;
|
2016-09-25 01:58:20 +00:00
|
|
|
|
2015-08-17 20:33:58 +00:00
|
|
|
public:
|
2018-12-08 05:30:43 +00:00
|
|
|
CGameState();
|
2022-02-18 07:37:54 +00:00
|
|
|
CGameState(CInputStream& stream, u32 saveIdx);
|
2018-12-08 05:30:43 +00:00
|
|
|
void SetCurrentWorldId(CAssetId id);
|
2019-08-14 10:04:11 +00:00
|
|
|
std::shared_ptr<CPlayerState> GetPlayerState() const { return x98_playerState; }
|
|
|
|
std::shared_ptr<CWorldTransManager> GetWorldTransitionManager() const { return x9c_transManager; }
|
2019-12-11 04:51:33 +00:00
|
|
|
void SetTotalPlayTime(double time);
|
|
|
|
double GetTotalPlayTime() const { return xa0_playTime; }
|
2018-12-08 05:30:43 +00:00
|
|
|
CPersistentOptions& SystemOptions() { return xa8_systemOptions; }
|
|
|
|
CGameOptions& GameOptions() { return x17c_gameOptions; }
|
|
|
|
CHintOptions& HintOptions() { return x1f8_hintOptions; }
|
|
|
|
CWorldState& StateForWorld(CAssetId mlvlId);
|
|
|
|
CWorldState& CurrentWorldState() { return StateForWorld(x84_mlvlId); }
|
|
|
|
CAssetId CurrentWorldAssetId() const { return x84_mlvlId; }
|
|
|
|
void SetHardMode(bool v) { x228_24_hardMode = v; }
|
|
|
|
bool GetHardMode() const { return x228_24_hardMode; }
|
2022-02-18 07:37:54 +00:00
|
|
|
void ReadPersistentOptions(CInputStream& r);
|
2018-12-08 05:30:43 +00:00
|
|
|
void SetPersistentOptions(const CPersistentOptions& opts) { xa8_systemOptions = opts; }
|
|
|
|
void ImportPersistentOptions(const CPersistentOptions& opts);
|
|
|
|
void ExportPersistentOptions(CPersistentOptions& opts) const;
|
|
|
|
void SetGameOptions(const CGameOptions& opts) { x17c_gameOptions = opts; }
|
|
|
|
void WriteBackupBuf();
|
|
|
|
std::vector<u8>& BackupBuf() { return x218_backupBuf; }
|
|
|
|
u32 GetFileIdx() const { return x20c_saveFileIdx; }
|
|
|
|
void SetFileIdx(u32 idx) { x20c_saveFileIdx = idx; }
|
|
|
|
void SetCardSerial(u64 serial) { x210_cardSerial = serial; }
|
|
|
|
u64 GetCardSerial() const { return x210_cardSerial; }
|
2022-02-18 07:37:54 +00:00
|
|
|
void PutTo(COutputStream& writer);
|
2018-12-08 05:30:43 +00:00
|
|
|
float GetHardModeDamageMultiplier() const;
|
|
|
|
float GetHardModeWeaponMultiplier() const;
|
|
|
|
void InitializeMemoryWorlds();
|
|
|
|
void InitializeMemoryStates();
|
|
|
|
|
|
|
|
struct GameFileStateInfo {
|
|
|
|
double x0_playTime;
|
|
|
|
CAssetId x8_mlvlId;
|
|
|
|
float xc_health;
|
|
|
|
u32 x10_energyTanks;
|
|
|
|
u32 x14_timestamp;
|
|
|
|
u32 x18_itemPercent;
|
|
|
|
float x1c_scanPercent;
|
|
|
|
bool x20_hardMode;
|
|
|
|
};
|
|
|
|
static GameFileStateInfo LoadGameFileState(const u8* data);
|
2015-08-17 05:26:58 +00:00
|
|
|
};
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|