metaforce/Runtime/World/CScriptRandomRelay.cpp

71 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
2021-04-10 01:42:06 -07:00
namespace metaforce {
2019-03-12 20:46:20 -07:00
CScriptRandomRelay::CScriptRandomRelay(TUniqueId uid, std::string_view name, const CEntityInfo& info, s32 sendSetSize,
s32 sendSetVariance, bool percentSize, bool active)
2018-12-07 21:30:43 -08:00
: CEntity(uid, info, active, name)
2019-03-12 20:46:20 -07:00
, x34_sendSetSize((percentSize && sendSetSize > 100) ? 100 : sendSetSize)
, x38_sendSetVariance(sendSetVariance)
, x3c_percentSize(percentSize) {}
2018-12-07 21:30:43 -08: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) {
2018-12-07 21:30:43 -08:00
return;
}
2018-12-07 21:30:43 -08:00
SendLocalScriptMsgs(EScriptObjectState::Zero, stateMgr);
}
}
2018-12-07 21:30:43 -08:00
void CScriptRandomRelay::SendLocalScriptMsgs(EScriptObjectState state, CStateManager& stateMgr) {
if (state != EScriptObjectState::Zero) {
SendScriptMsgs(state, stateMgr, EScriptObjectMessage::None);
return;
}
2019-03-12 20:46:20 -07:00
std::vector<std::pair<CEntity*, EScriptObjectMessage>> objs;
objs.reserve(10);
for (const SConnection& conn : x20_conns) {
const auto list = stateMgr.GetIdListForScript(conn.x8_objId);
2019-03-12 20:46:20 -07:00
for (auto it = list.first; it != list.second; ++it) {
CEntity* ent = stateMgr.ObjectById(it->second);
if (ent && ent->GetActive()) {
2019-03-12 20:46:20 -07:00
objs.emplace_back(ent, conn.x4_msg);
}
}
2019-03-12 20:46:20 -07:00
}
2016-09-02 13:57:45 -07:00
2019-03-12 20:46:20 -07:00
s32 targetSetSize = x34_sendSetSize;
if (x3c_percentSize) {
2019-03-12 20:46:20 -07:00
targetSetSize = s32(0.5f + (float(x34_sendSetSize * objs.size()) / 100.f));
}
2019-03-12 20:46:20 -07:00
targetSetSize += s32(float(x38_sendSetVariance) * 2.f * stateMgr.GetActiveRandom()->Float()) - x38_sendSetVariance;
targetSetSize = zeus::clamp(0, targetSetSize, 64);
while (objs.size() > targetSetSize) {
const s32 randomRemoveIdx = s32(stateMgr.GetActiveRandom()->Float() * float(objs.size()) * 0.99f);
2019-03-12 20:46:20 -07:00
auto it = objs.begin();
for (s32 i = 0; i < randomRemoveIdx; ++i) {
++it;
if (it == objs.end()) {
2019-03-12 20:46:20 -07:00
break;
}
2019-03-12 20:46:20 -07:00
}
if (it != objs.end()) {
2019-03-12 20:46:20 -07:00
objs.erase(it);
}
2019-03-12 20:46:20 -07:00
}
2016-09-02 13:57:45 -07:00
for (const auto& o : objs) {
2019-03-12 20:46:20 -07:00
stateMgr.SendScriptMsg(o.first, GetUniqueId(), o.second);
}
}
2021-04-10 01:42:06 -07:00
} // namespace metaforce