Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/logic/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,57 @@ void ScriptEngine::unregisterNpc(Handle::EntityHandle e)
{
m_WorldNPCs.erase(e);
}

VobTypes::NpcVobInformation Logic::ScriptEngine::findNPCVobFromScriptInstance(size_t symbolIdx)
{
using Daedalus::GameState::NpcHandle;

assert(m_pVM->getDATFile().getSymbolByIndex(symbolIdx).instanceDataClass == Daedalus::EInstanceClass::IC_Npc);

NpcHandle hnpc = ZMemory::handleCast<NpcHandle>(m_pVM->getDATFile().getSymbolByIndex(symbolIdx).instanceDataHandle);

if (!hnpc.isValid())
{
VobTypes::NpcVobInformation vob;
vob.entity.invalidate();
return vob;
}

// Get data of npc this belongs to
Daedalus::GEngineClasses::C_Npc& npcData = m_pVM->getGameState().getNpc(hnpc);
VobTypes::ScriptInstanceUserData* userData = reinterpret_cast<VobTypes::ScriptInstanceUserData*>(npcData.userPtr);

if (userData)
{
return VobTypes::asNpcVob(m_World, userData->vobEntity);
}
else
{
VobTypes::NpcVobInformation vob;
vob.entity.invalidate();

return vob;
}
}

Vob::VobInformation Logic::ScriptEngine::findItemVobFromScriptInstance(size_t symbolIdx)
{
auto& parSymbol = m_pVM->getDATFile().getSymbolByIndex(symbolIdx);
Daedalus::GameState::ItemHandle hitem = ZMemory::handleCast<Daedalus::GameState::ItemHandle>(parSymbol.instanceDataHandle);

if (hitem.isValid())
{
// Get data of npc this belongs to
Daedalus::GEngineClasses::C_Item& itemData = m_pVM->getGameState().getItem(hitem);
VobTypes::ScriptInstanceUserData* userData = reinterpret_cast<VobTypes::ScriptInstanceUserData*>(itemData.userPtr);

if (userData)
{
return Vob::asVob(m_World, userData->vobEntity);
}
}

Vob::VobInformation vob;
vob.entity.invalidate();
return vob;
}
10 changes: 10 additions & 0 deletions src/logic/ScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <daedalus/DaedalusVM.h>
#include <handle/HandleDef.h>
#include <math/mathlib.h>
#include <components/VobClasses.h>
using json = nlohmann::json;

namespace Daedalus
Expand Down Expand Up @@ -196,6 +197,15 @@ namespace Logic
*/
void onLogEntryAdded(const std::string& topic, const std::string& entry);

/**
* @return NPC-Vob linked to the given script symbol
*/
VobTypes::NpcVobInformation findNPCVobFromScriptInstance(size_t symbolIdx);

/**
* @return Item laying on the ground linked to the given script symbol
*/
Vob::VobInformation findItemVobFromScriptInstance(size_t symbolIdx);
protected:

/**
Expand Down
Loading