metaforce/Runtime/World/CScriptRandomRelay.cpp

65 lines
2.3 KiB
C++
Raw Normal View History

#include "Runtime/World/CScriptRandomRelay.hpp"
#include "Runtime/CStateManager.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2018-12-08 05:30:43 +00:00
namespace urde {
2019-03-13 03:46:20 +00:00
CScriptRandomRelay::CScriptRandomRelay(TUniqueId uid, std::string_view name, const CEntityInfo& info, s32 sendSetSize,
s32 sendSetVariance, bool percentSize, bool active)
2018-12-08 05:30:43 +00:00
: CEntity(uid, info, active, name)
2019-03-13 03:46:20 +00:00
, x34_sendSetSize((percentSize && sendSetSize > 100) ? 100 : sendSetSize)
, x38_sendSetVariance(sendSetVariance)
, x3c_percentSize(percentSize) {}
2018-12-08 05:30:43 +00:00
void CScriptRandomRelay::Accept(IVisitor& visitor) { visitor.Visit(this); }
void CScriptRandomRelay::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId objId, CStateManager& stateMgr) {
CEntity::AcceptScriptMsg(msg, objId, stateMgr);
if (msg == EScriptObjectMessage::SetToZero) {
if (!x30_24_active)
return;
SendLocalScriptMsgs(EScriptObjectState::Zero, stateMgr);
}
}
2018-12-08 05:30:43 +00:00
void CScriptRandomRelay::SendLocalScriptMsgs(EScriptObjectState state, CStateManager& stateMgr) {
if (state != EScriptObjectState::Zero) {
SendScriptMsgs(state, stateMgr, EScriptObjectMessage::None);
return;
}
2019-03-13 03:46:20 +00:00
std::vector<std::pair<CEntity*, EScriptObjectMessage>> objs;
objs.reserve(10);
for (const SConnection& conn : x20_conns) {
auto list = stateMgr.GetIdListForScript(conn.x8_objId);
for (auto it = list.first; it != list.second; ++it) {
CEntity* ent = stateMgr.ObjectById(it->second);
if (ent && ent->GetActive())
objs.emplace_back(ent, conn.x4_msg);
}
2019-03-13 03:46:20 +00:00
}
2016-09-02 20:57:45 +00:00
2019-03-13 03:46:20 +00:00
s32 targetSetSize = x34_sendSetSize;
if (x3c_percentSize)
targetSetSize = s32(0.5f + (float(x34_sendSetSize * objs.size()) / 100.f));
targetSetSize += s32(float(x38_sendSetVariance) * 2.f * stateMgr.GetActiveRandom()->Float()) - x38_sendSetVariance;
targetSetSize = zeus::clamp(0, targetSetSize, 64);
while (objs.size() > targetSetSize) {
s32 randomRemoveIdx = s32(stateMgr.GetActiveRandom()->Float() * float(objs.size()) * 0.99f);
auto it = objs.begin();
for (s32 i = 0; i < randomRemoveIdx; ++i) {
++it;
if (it == objs.end())
break;
}
if (it != objs.end())
objs.erase(it);
}
2016-09-02 20:57:45 +00:00
2019-03-13 03:46:20 +00:00
for (const auto& o : objs)
stateMgr.SendScriptMsg(o.first, GetUniqueId(), o.second);
}
2018-12-08 05:30:43 +00:00
} // namespace urde