2016-07-24 23:14:58 +00:00
|
|
|
#include "CRelayTracker.hpp"
|
|
|
|
#include "CStateManager.hpp"
|
|
|
|
#include "CSaveWorld.hpp"
|
|
|
|
#include "World/CWorld.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace urde {
|
|
|
|
|
|
|
|
CRelayTracker::CRelayTracker(CBitStreamReader& in, const CSaveWorld& saveworld) {
|
|
|
|
u32 relayCount = saveworld.GetRelayCount();
|
|
|
|
if (saveworld.GetRelayCount()) {
|
|
|
|
std::vector<bool> relayStates(saveworld.GetRelayCount());
|
|
|
|
for (u32 i = 0; i < relayCount; ++i)
|
|
|
|
relayStates[i] = in.ReadEncoded(1);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < relayCount; ++i) {
|
|
|
|
if (!relayStates[i])
|
|
|
|
continue;
|
|
|
|
x0_relayStates.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
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
bool CRelayTracker::HasRelay(TEditorId id) {
|
|
|
|
return std::find(x0_relayStates.begin(), x0_relayStates.end(), id) != x0_relayStates.end();
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CRelayTracker::AddRelay(TEditorId id) {
|
|
|
|
if (std::find(x0_relayStates.begin(), x0_relayStates.end(), id) == x0_relayStates.end())
|
|
|
|
x0_relayStates.push_back(id);
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CRelayTracker::RemoveRelay(TEditorId id) {
|
|
|
|
if (std::find(x0_relayStates.begin(), x0_relayStates.end(), id) != x0_relayStates.end())
|
|
|
|
x0_relayStates.erase(std::remove(x0_relayStates.begin(), x0_relayStates.end(), id), x0_relayStates.end());
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 04:41:35 +00:00
|
|
|
void CRelayTracker::SendMsgs(TAreaId areaId, CStateManager& stateMgr) {
|
2018-12-08 05:30:43 +00:00
|
|
|
const CWorld* world = stateMgr.WorldNC();
|
|
|
|
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
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (!HasRelay(relay.GetRelayId()))
|
|
|
|
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
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (!HasRelay(relay.GetRelayId()) || !relay.GetActive())
|
|
|
|
continue;
|
2016-07-24 23:14:58 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
RemoveRelay(relay.GetRelayId());
|
|
|
|
}
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CRelayTracker::PutTo(CBitStreamWriter& out, const CSaveWorld& saveworld) {
|
|
|
|
u32 relayCount = saveworld.GetRelayCount();
|
|
|
|
std::vector<bool> relays(relayCount);
|
|
|
|
|
2019-02-10 04:41:35 +00:00
|
|
|
for (const TEditorId& id : x0_relayStates) {
|
|
|
|
s32 idx = saveworld.GetRelayIndex(id);
|
|
|
|
if (idx >= 0)
|
|
|
|
relays[idx] = true;
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < relayCount; ++i)
|
|
|
|
out.WriteEncoded(u32(relays[i]), 1);
|
2016-07-24 23:14:58 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
} // namespace urde
|