2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/World/CScriptPickupGenerator.hpp"
|
|
|
|
|
|
|
|
#include "Runtime/CStateManager.hpp"
|
|
|
|
#include "Runtime/World/CScriptPickup.hpp"
|
|
|
|
#include "Runtime/World/CWallCrawlerSwarm.hpp"
|
|
|
|
|
2019-09-21 13:07:13 +00:00
|
|
|
#include "TCastTo.hpp" // Generated file, do not modify include path
|
2016-12-10 02:35:20 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
namespace urde {
|
2019-02-18 05:47:46 +00:00
|
|
|
|
2017-11-13 06:19:18 +00:00
|
|
|
CScriptPickupGenerator::CScriptPickupGenerator(TUniqueId uid, std::string_view name, const CEntityInfo& info,
|
2016-12-10 02:35:20 +00:00
|
|
|
const zeus::CVector3f& pos, float frequency, bool active)
|
2019-02-18 05:47:46 +00:00
|
|
|
: CEntity(uid, info, active, name), x34_position(pos), x40_frequency(frequency) {
|
|
|
|
ResetDelayTimer();
|
|
|
|
}
|
2016-12-10 02:35:20 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CScriptPickupGenerator::Accept(IVisitor& visitor) { visitor.Visit(this); }
|
2017-01-15 03:07:01 +00:00
|
|
|
|
2019-02-18 05:47:46 +00:00
|
|
|
void CScriptPickupGenerator::ResetDelayTimer() {
|
2020-05-07 17:00:45 +00:00
|
|
|
if (x40_frequency > 0.f) {
|
2019-02-18 05:47:46 +00:00
|
|
|
x44_delayTimer += 100.f / x40_frequency;
|
2020-05-07 17:00:45 +00:00
|
|
|
} else {
|
2019-02-18 05:47:46 +00:00
|
|
|
x44_delayTimer = FLT_MAX;
|
2020-05-07 17:00:45 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScriptPickupGenerator::GetGeneratorIds(CStateManager& mgr, TUniqueId sender,
|
|
|
|
std::vector<TUniqueId>& idsOut) const {
|
|
|
|
idsOut.reserve(std::max(size_t(1), GetConnectionList().size()));
|
|
|
|
for (const auto& conn : GetConnectionList()) {
|
|
|
|
if (conn.x0_state == EScriptObjectState::Zero && conn.x4_msg == EScriptObjectMessage::Follow) {
|
2020-05-07 17:00:45 +00:00
|
|
|
const TUniqueId id = mgr.GetIdForScript(conn.x8_objId);
|
2019-02-18 05:47:46 +00:00
|
|
|
if (id != kInvalidUniqueId) {
|
|
|
|
if (const CEntity* ent = mgr.GetObjectById(id)) {
|
2020-05-07 17:00:45 +00:00
|
|
|
if (ent->GetActive()) {
|
2019-02-18 05:47:46 +00:00
|
|
|
idsOut.push_back(id);
|
2020-05-07 17:00:45 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-10 02:35:20 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-05-07 17:00:45 +00:00
|
|
|
if (idsOut.empty()) {
|
2019-02-18 05:47:46 +00:00
|
|
|
idsOut.push_back(sender);
|
2020-05-07 17:00:45 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
}
|
2016-12-10 02:35:20 +00:00
|
|
|
|
2019-02-18 05:47:46 +00:00
|
|
|
float CScriptPickupGenerator::GetPickupTemplates(CStateManager& mgr,
|
|
|
|
std::vector<std::pair<float, TEditorId>>& idsOut) const {
|
|
|
|
float totalPossibility = 0.f;
|
|
|
|
CPlayerState& pState = *mgr.GetPlayerState();
|
|
|
|
idsOut.reserve(GetConnectionList().size());
|
|
|
|
for (const auto& conn : GetConnectionList()) {
|
|
|
|
if (conn.x0_state == EScriptObjectState::Zero && conn.x4_msg == EScriptObjectMessage::Activate) {
|
2020-05-07 17:00:45 +00:00
|
|
|
const TUniqueId id = mgr.GetIdForScript(conn.x8_objId);
|
2019-02-18 05:47:46 +00:00
|
|
|
if (id != kInvalidUniqueId) {
|
2020-05-07 17:00:45 +00:00
|
|
|
if (const TCastToConstPtr<CScriptPickup> pickup = mgr.GetObjectById(id)) {
|
|
|
|
const CPlayerState::EItemType item = pickup->GetItem();
|
2019-02-18 05:47:46 +00:00
|
|
|
float possibility = pickup->GetPossibility();
|
|
|
|
float multiplier = 1.f;
|
|
|
|
bool doAlways = false;
|
|
|
|
bool doThirtyPerc = false;
|
|
|
|
switch (item) {
|
|
|
|
case CPlayerState::EItemType::Missiles:
|
|
|
|
if (pState.HasPowerUp(CPlayerState::EItemType::Missiles)) {
|
|
|
|
if (pState.GetItemAmount(CPlayerState::EItemType::Missiles) <
|
2020-05-07 17:00:45 +00:00
|
|
|
pState.GetItemCapacity(CPlayerState::EItemType::Missiles)) {
|
2019-02-18 05:47:46 +00:00
|
|
|
doAlways = true;
|
2020-05-07 17:00:45 +00:00
|
|
|
} else {
|
2019-02-18 05:47:46 +00:00
|
|
|
doThirtyPerc = true;
|
2020-05-07 17:00:45 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CPlayerState::EItemType::PowerBombs:
|
|
|
|
if (pState.HasPowerUp(CPlayerState::EItemType::PowerBombs)) {
|
|
|
|
if (pState.GetItemAmount(CPlayerState::EItemType::PowerBombs) <
|
|
|
|
pState.GetItemCapacity(CPlayerState::EItemType::PowerBombs)) {
|
|
|
|
doAlways = true;
|
2020-05-07 17:00:45 +00:00
|
|
|
if (pState.GetItemAmount(CPlayerState::EItemType::PowerBombs) < 2 && possibility >= 10.f &&
|
|
|
|
possibility < 25.f) {
|
2019-02-18 05:47:46 +00:00
|
|
|
multiplier = 2.f;
|
2020-05-07 17:00:45 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
} else {
|
|
|
|
doThirtyPerc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CPlayerState::EItemType::HealthRefill:
|
2020-05-07 17:00:45 +00:00
|
|
|
if (pState.GetHealthInfo().GetHP() < pState.CalculateHealth()) {
|
2019-02-18 05:47:46 +00:00
|
|
|
doAlways = true;
|
2020-05-07 17:00:45 +00:00
|
|
|
} else {
|
2019-02-18 05:47:46 +00:00
|
|
|
doThirtyPerc = true;
|
2020-05-07 17:00:45 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
doAlways = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-21 04:12:13 +00:00
|
|
|
const bool thirtyPercTest = mgr.GetActiveRandom()->Float() < 0.3f;
|
2019-02-18 05:47:46 +00:00
|
|
|
if ((doAlways || (doThirtyPerc && thirtyPercTest)) && possibility > 0.f) {
|
|
|
|
totalPossibility += possibility * multiplier;
|
2020-03-21 04:12:13 +00:00
|
|
|
idsOut.emplace_back(possibility, conn.x8_objId);
|
2019-02-18 05:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalPossibility;
|
2016-12-10 02:35:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 05:47:46 +00:00
|
|
|
void CScriptPickupGenerator::GeneratePickup(CStateManager& mgr, TEditorId templateId, TUniqueId generatorId) const {
|
|
|
|
CEntity* pickupTempl = mgr.ObjectById(mgr.GetIdForScript(templateId));
|
|
|
|
CEntity* generator = mgr.ObjectById(generatorId);
|
2020-05-07 17:00:45 +00:00
|
|
|
|
|
|
|
if (pickupTempl == nullptr || generator == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool oldGeneratingObject = mgr.GetIsGeneratingObject();
|
|
|
|
mgr.SetIsGeneratingObject(true);
|
|
|
|
const auto p = mgr.GenerateObject(templateId);
|
|
|
|
mgr.SetIsGeneratingObject(oldGeneratingObject);
|
|
|
|
|
|
|
|
if (p.second == kInvalidUniqueId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEntity* newObj = mgr.ObjectById(p.second);
|
|
|
|
CActor* newAct = TCastToPtr<CActor>(newObj).GetPtr();
|
|
|
|
CScriptPickup* newPickup = TCastToPtr<CScriptPickup>(newObj).GetPtr();
|
|
|
|
const CActor* generatorAct = TCastToConstPtr<CActor>(generator).GetPtr();
|
|
|
|
const CWallCrawlerSwarm* swarmAct = TCastToConstPtr<CWallCrawlerSwarm>(generator).GetPtr();
|
|
|
|
|
|
|
|
if (newAct && swarmAct) {
|
|
|
|
newAct->SetTranslation(swarmAct->GetLastKilledOffset() + x34_position);
|
|
|
|
} else if (newAct && generatorAct) {
|
|
|
|
newAct->SetTranslation(generatorAct->GetTranslation() + x34_position);
|
2019-02-18 05:47:46 +00:00
|
|
|
}
|
2020-05-07 17:00:45 +00:00
|
|
|
|
|
|
|
if (newPickup) {
|
|
|
|
newPickup->SetGenerated();
|
|
|
|
}
|
|
|
|
|
|
|
|
mgr.SendScriptMsg(newObj, GetUniqueId(), EScriptObjectMessage::Activate);
|
2016-12-10 02:35:20 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
|
|
|
|
void CScriptPickupGenerator::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId sender, CStateManager& stateMgr) {
|
|
|
|
if (msg == EScriptObjectMessage::SetToZero && x30_24_active && x40_frequency != 100.f) {
|
|
|
|
x44_delayTimer -= 1.f;
|
|
|
|
if (x44_delayTimer < 0.000009f) {
|
|
|
|
ResetDelayTimer();
|
|
|
|
} else {
|
|
|
|
std::vector<TUniqueId> generatorIds;
|
|
|
|
GetGeneratorIds(stateMgr, sender, generatorIds);
|
|
|
|
std::vector<std::pair<float, TEditorId>> pickupTemplates;
|
2020-05-07 17:00:45 +00:00
|
|
|
const float totalProb = GetPickupTemplates(stateMgr, pickupTemplates);
|
2019-02-18 05:47:46 +00:00
|
|
|
if (!pickupTemplates.empty()) {
|
2020-05-07 17:00:45 +00:00
|
|
|
const float r = stateMgr.GetActiveRandom()->Range(0.f, totalProb);
|
2019-02-18 05:47:46 +00:00
|
|
|
float f2 = 0.f;
|
|
|
|
size_t count = 0;
|
2020-04-22 10:37:34 +00:00
|
|
|
for (const auto& id : pickupTemplates) {
|
2020-05-07 17:00:45 +00:00
|
|
|
if (r >= f2 && r <= f2 + id.first) {
|
2019-02-18 05:47:46 +00:00
|
|
|
break;
|
2020-05-07 17:00:45 +00:00
|
|
|
}
|
2019-02-18 05:47:46 +00:00
|
|
|
f2 += id.first;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
if (count != pickupTemplates.size()) {
|
2020-05-07 17:00:45 +00:00
|
|
|
const TEditorId templateId = pickupTemplates[count].second;
|
2019-02-18 05:47:46 +00:00
|
|
|
GeneratePickup(stateMgr, templateId,
|
|
|
|
generatorIds[stateMgr.GetActiveRandom()->Float() * generatorIds.size() * 0.99f]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CEntity::AcceptScriptMsg(msg, sender, stateMgr);
|
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
} // namespace urde
|