2021-06-03 21:39:49 +00:00
|
|
|
#include "Runtime/CScriptMailbox.hpp"
|
2019-12-22 20:04:07 +00:00
|
|
|
|
2021-06-05 05:27:52 +00:00
|
|
|
#include "Runtime/CWorldSaveGameInfo.hpp"
|
2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/CStateManager.hpp"
|
|
|
|
#include "Runtime/World/CWorld.hpp"
|
|
|
|
|
2016-07-24 23:14:58 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2021-06-05 05:27:52 +00:00
|
|
|
CScriptMailbox::CScriptMailbox(CBitStreamReader& in, const CWorldSaveGameInfo& saveWorld) {
|
2020-03-25 19:53:45 +00:00
|
|
|
const u32 relayCount = saveWorld.GetRelayCount();
|
|
|
|
if (saveWorld.GetRelayCount()) {
|
|
|
|
std::vector<bool> relayStates(saveWorld.GetRelayCount());
|
|
|
|
for (u32 i = 0; i < relayCount; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
relayStates[i] = in.ReadEncoded(1);
|
2020-03-25 19:53:45 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < relayCount; ++i) {
|
2020-03-25 19:53:45 +00:00
|
|
|
if (!relayStates[i]) {
|
2018-12-08 05:30:43 +00:00
|
|
|
continue;
|
2021-06-07 19:29:18 +00:00
|
|
|
}
|
|
|
|
x0_relays.push_back(saveWorld.GetRelayEditorId(i));
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
bool CScriptMailbox::HasMsg(TEditorId id) const {
|
|
|
|
return std::find(x0_relays.cbegin(), x0_relays.cend(), id) != x0_relays.cend();
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
void CScriptMailbox::AddMsg(TEditorId id) {
|
|
|
|
if (HasMsg(id)) {
|
2020-03-18 05:03:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
x0_relays.push_back(id);
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
void CScriptMailbox::RemoveMsg(TEditorId id) {
|
|
|
|
if (!HasMsg(id)) {
|
2020-03-18 05:03:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
std::erase(x0_relays, id);
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
void CScriptMailbox::SendMsgs(TAreaId areaId, CStateManager& stateMgr) {
|
2019-08-14 14:58:54 +00:00
|
|
|
const CWorld* world = stateMgr.GetWorld();
|
2018-12-08 05:30:43 +00:00
|
|
|
u32 relayCount = world->GetRelayCount();
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
bool hasActiveRelays = false;
|
|
|
|
for (u32 i = 0; i < relayCount; ++i) {
|
|
|
|
const CWorld::CRelay& relay = world->GetRelay(i);
|
|
|
|
if (relay.GetTargetId().AreaNum() != areaId)
|
|
|
|
continue;
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
if (!HasMsg(relay.GetRelayId()))
|
2018-12-08 05:30:43 +00:00
|
|
|
continue;
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
stateMgr.SendScriptMsg(kInvalidUniqueId, relay.GetTargetId(), EScriptObjectMessage(relay.GetMessage()),
|
|
|
|
EScriptObjectState::Any);
|
|
|
|
if (relay.GetActive())
|
|
|
|
hasActiveRelays = true;
|
|
|
|
}
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (!hasActiveRelays)
|
|
|
|
return;
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
for (u32 i = 0; i < relayCount; ++i) {
|
|
|
|
const CWorld::CRelay& relay = world->GetRelay(i);
|
|
|
|
if (relay.GetTargetId().AreaNum() != areaId)
|
|
|
|
continue;
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
if (!HasMsg(relay.GetRelayId()) || !relay.GetActive())
|
2018-12-08 05:30:43 +00:00
|
|
|
continue;
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
RemoveMsg(relay.GetRelayId());
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 05:27:52 +00:00
|
|
|
void CScriptMailbox::PutTo(CBitStreamWriter& out, const CWorldSaveGameInfo& saveWorld) {
|
2020-03-25 19:53:45 +00:00
|
|
|
const u32 relayCount = saveWorld.GetRelayCount();
|
2018-12-08 05:30:43 +00:00
|
|
|
std::vector<bool> relays(relayCount);
|
|
|
|
|
2021-06-03 21:39:49 +00:00
|
|
|
for (const TEditorId& id : x0_relays) {
|
2020-03-25 19:53:45 +00:00
|
|
|
const s32 idx = saveWorld.GetRelayIndex(id);
|
|
|
|
if (idx >= 0) {
|
2019-02-10 04:41:35 +00:00
|
|
|
relays[idx] = true;
|
2020-03-25 19:53:45 +00:00
|
|
|
}
|
2019-02-10 04:41:35 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2020-03-25 19:53:45 +00:00
|
|
|
for (u32 i = 0; i < relayCount; ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
out.WriteEncoded(u32(relays[i]), 1);
|
2020-03-25 19:53:45 +00:00
|
|
|
}
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|