2016-04-20 21:44:18 +00:00
|
|
|
#include "CScriptGenerator.hpp"
|
2016-12-22 02:48:22 +00:00
|
|
|
#include "CStateManager.hpp"
|
2017-01-24 17:23:10 +00:00
|
|
|
#include "CWallCrawlerSwarm.hpp"
|
2017-01-15 03:07:01 +00:00
|
|
|
#include "TCastTo.hpp"
|
2016-04-20 21:44:18 +00:00
|
|
|
|
|
|
|
namespace urde
|
|
|
|
{
|
|
|
|
|
2017-11-13 06:19:18 +00:00
|
|
|
CScriptGenerator::CScriptGenerator(TUniqueId uid, std::string_view name, const CEntityInfo& info, u32 spawnCount,
|
2017-01-24 17:47:11 +00:00
|
|
|
bool reuseFollowers, const zeus::CVector3f& vec1, bool inheritXf, bool active,
|
|
|
|
float minScale, float maxScale)
|
2017-01-24 17:23:10 +00:00
|
|
|
: CEntity(uid, info, active, name)
|
2017-01-24 17:47:11 +00:00
|
|
|
, x34_spawnCount(spawnCount)
|
|
|
|
, x38_24_reuseFollowers(reuseFollowers)
|
|
|
|
, x38_25_inheritTransform(inheritXf)
|
2017-01-24 17:23:10 +00:00
|
|
|
, x3c_offset(vec1)
|
|
|
|
, x48_minScale(minScale)
|
|
|
|
, x4c_maxScale(maxScale)
|
2016-04-20 21:44:18 +00:00
|
|
|
{
|
|
|
|
}
|
2016-05-03 08:27:28 +00:00
|
|
|
|
2017-01-24 17:23:10 +00:00
|
|
|
void CScriptGenerator::Accept(IVisitor& visitor) { visitor.Visit(this); }
|
2017-01-15 03:07:01 +00:00
|
|
|
|
2016-05-03 08:27:28 +00:00
|
|
|
void CScriptGenerator::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId objId, CStateManager& stateMgr)
|
|
|
|
{
|
2017-01-31 11:21:45 +00:00
|
|
|
if (msg == EScriptObjectMessage::SetToZero && GetActive() && !x20_conns.empty())
|
2016-05-03 08:27:28 +00:00
|
|
|
{
|
2017-01-31 11:21:45 +00:00
|
|
|
std::vector<TUniqueId> follows;
|
|
|
|
follows.reserve(x20_conns.size());
|
|
|
|
for (const SConnection& conn : x20_conns)
|
2017-01-24 17:23:10 +00:00
|
|
|
{
|
2017-01-31 11:21:45 +00:00
|
|
|
if (conn.x0_state != EScriptObjectState::Zero || conn.x4_msg != EScriptObjectMessage::Follow)
|
|
|
|
continue;
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
TUniqueId uid = stateMgr.GetIdForScript(conn.x8_objId);
|
|
|
|
if (stateMgr.GetObjectById(uid) != nullptr)
|
|
|
|
follows.push_back(uid);
|
|
|
|
}
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
std::vector<std::pair<TUniqueId, TEditorId>> activates;
|
|
|
|
activates.reserve(x20_conns.size());
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
for (const SConnection& conn : x20_conns)
|
|
|
|
{
|
|
|
|
if (conn.x0_state != EScriptObjectState::Zero)
|
|
|
|
continue;
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
TUniqueId uid = stateMgr.GetIdForScript(conn.x8_objId);
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
if (conn.x4_msg != EScriptObjectMessage::Activate)
|
|
|
|
{
|
|
|
|
stateMgr.SendScriptMsgAlways(GetUniqueId(), uid, conn.x4_msg);
|
|
|
|
continue;
|
2017-01-24 17:23:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
if (stateMgr.GetObjectById(uid) != nullptr)
|
|
|
|
activates.emplace_back(uid, conn.x8_objId);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 i = 0; i < x34_spawnCount; ++i)
|
|
|
|
{
|
|
|
|
if (activates.size() == 0 || follows.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
u32 activatesRand = 0.99f * (stateMgr.GetActiveRandom()->Float() * activates.size());
|
|
|
|
u32 followsRand = 0.99f * (stateMgr.GetActiveRandom()->Float() * follows.size());
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
for (u32 j = 0; j < activates.size(); ++j)
|
|
|
|
if (TCastToConstPtr<CScriptSound>(stateMgr.GetObjectById(activates[j].first)))
|
|
|
|
activatesRand = j;
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
std::pair<TUniqueId, TEditorId> idPair = activates[activatesRand];
|
|
|
|
CEntity* activate = stateMgr.ObjectById(idPair.first);
|
|
|
|
CEntity* follow = stateMgr.ObjectById(follows[followsRand]);
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
if (!activate || !follow)
|
|
|
|
break;
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
bool oldGeneratingObject = stateMgr.GetIsGeneratingObject();
|
|
|
|
stateMgr.SetIsGeneratingObject(true);
|
|
|
|
std::pair<TEditorId, TUniqueId> objId = stateMgr.GenerateObject(idPair.second);
|
|
|
|
stateMgr.SetIsGeneratingObject(oldGeneratingObject);
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
if (objId.second != kInvalidUniqueId)
|
|
|
|
{
|
|
|
|
TCastToPtr<CActor> activateActor(stateMgr.ObjectById(idPair.first));
|
|
|
|
TCastToPtr<CActor> followActor(follow);
|
|
|
|
TCastToPtr<CWallCrawlerSwarm> wallCrawlerSwarm(follow);
|
2017-01-24 17:23:10 +00:00
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
if (activateActor && wallCrawlerSwarm)
|
|
|
|
{
|
|
|
|
if (x38_25_inheritTransform)
|
|
|
|
activateActor->SetTransform(wallCrawlerSwarm->GetTransform());
|
|
|
|
activateActor->SetTranslation(wallCrawlerSwarm->GetLastKilledOffset() + x3c_offset);
|
|
|
|
}
|
|
|
|
else if (activateActor && followActor)
|
|
|
|
{
|
|
|
|
if (x38_25_inheritTransform)
|
|
|
|
activateActor->SetTransform(followActor->GetTransform());
|
|
|
|
activateActor->SetTranslation(followActor->GetTranslation() + x3c_offset);
|
|
|
|
}
|
|
|
|
else if (follow != nullptr)
|
2017-01-24 17:23:10 +00:00
|
|
|
{
|
2017-01-31 11:21:45 +00:00
|
|
|
TCastToPtr<CActor> activateActor(stateMgr.ObjectById(objId.second));
|
2017-01-24 17:23:10 +00:00
|
|
|
|
|
|
|
if (activateActor && wallCrawlerSwarm)
|
|
|
|
{
|
|
|
|
if (x38_25_inheritTransform)
|
|
|
|
activateActor->SetTransform(wallCrawlerSwarm->GetTransform());
|
|
|
|
activateActor->SetTranslation(wallCrawlerSwarm->GetLastKilledOffset() + x3c_offset);
|
|
|
|
}
|
|
|
|
else if (activateActor && followActor)
|
|
|
|
{
|
|
|
|
if (x38_25_inheritTransform)
|
|
|
|
activateActor->SetTransform(followActor->GetTransform());
|
2017-01-24 17:47:11 +00:00
|
|
|
activateActor->SetTranslation(followActor->GetTranslation() + x3c_offset);
|
2017-01-24 17:23:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 11:21:45 +00:00
|
|
|
float rnd = stateMgr.GetActiveRandom()->Range(x48_minScale, x4c_maxScale);
|
|
|
|
CModelData* mData = followActor->ModelData();
|
|
|
|
if (mData && (mData->AnimationData() || mData->GetNormalModel()))
|
|
|
|
mData->SetScale(rnd * mData->GetScale());
|
2017-01-24 17:23:10 +00:00
|
|
|
}
|
2017-01-31 11:21:45 +00:00
|
|
|
|
|
|
|
stateMgr.SendScriptMsg(activate, GetUniqueId(), EScriptObjectMessage::Activate);
|
|
|
|
|
|
|
|
activates.erase(std::find(activates.begin(), activates.end(), idPair));
|
|
|
|
if (!x38_24_reuseFollowers)
|
|
|
|
follows.erase(std::find(follows.begin(), follows.end(), follows[followsRand]));
|
2017-01-24 17:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-03 08:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CEntity::AcceptScriptMsg(msg, objId, stateMgr);
|
|
|
|
}
|
2016-04-20 21:44:18 +00:00
|
|
|
}
|