Skip to content

Commit

Permalink
Externals: Refactored some ugly lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
ataulien committed Jul 1, 2018
1 parent 5faf297 commit fa23ae6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 244 deletions.
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
53 changes: 6 additions & 47 deletions src/logic/scriptExternals/Externals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#include <daedalus/DaedalusVM.h>
#include <debugdraw/debugdraw.h>
#include <engine/GameEngine.h>
#include <logic/DialogManager.h>
#include <logic/PlayerController.h>
#include <logic/ScriptEngine.h>
#include <logic/visuals/ModelVisual.h>
#include <ui/Hud.h>
#include <ui/PrintScreenMessages.h>
#include <ui/IntroduceChapterView.h>
#include <ui/PrintScreenMessages.h>
#include <utils/logger.h>
#include <logic/ScriptEngine.h>
#include <logic/DialogManager.h>

#include "Externals_AI.h"
#include "Externals_Npc.h"
Expand All @@ -26,51 +26,16 @@ void ::Logic::ScriptExternals::registerEngineExternals(World::WorldInstance& wor
{
Engine::BaseEngine* engine = world.getEngine();
World::WorldInstance* pWorld = &world;
using Daedalus::GameState::NpcHandle;
using Daedalus::GameState::ItemHandle;
using Daedalus::GameState::NpcHandle;

registerEngineExternals_ai(world, vm, verbose);
registerEngineExternals_snd(world, vm, verbose);
registerEngineExternals_wld(world, vm, verbose);
registerEngineExternals_npc(world, vm, verbose);

// TODO: Refractor
auto getNPCByInstance = [vm, engine](size_t instance) {
assert(vm->getDATFile().getSymbolByIndex(instance).instanceDataClass == Daedalus::EInstanceClass::IC_Npc);

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

if (!hnpc.isValid())
{
//LogWarn() << "Invalid handle in instance: " << instance << " (" << vm->getDATFile().getSymbolByIndex(instance).name << ")";
//LogWarn() << "Callstack: " << vm->getCallStack();

VobTypes::NpcVobInformation vob;
vob.entity.invalidate();
return vob;
}

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

if (userData)
{
World::WorldInstance& world = engine->getWorldInstance(userData->world);
VobTypes::NpcVobInformation vob = VobTypes::asNpcVob(world, userData->vobEntity);

return vob;
}
else
{
LogWarn() << "No userptr on npc: " << npcData.name[0];

VobTypes::NpcVobInformation vob;
vob.entity.invalidate();

return vob;
}

auto getNPCByInstance = [=](size_t instance) {
return pWorld->getScriptEngine().findNPCVobFromScriptInstance(instance);
};

/**
Expand Down Expand Up @@ -126,7 +91,6 @@ void ::Logic::ScriptExternals::registerEngineExternals(World::WorldInstance& wor
* GetDistTo...
*/
vm->registerExternalFunction("printdebuginstch", [=](Daedalus::DaedalusVM& vm) {

uint32_t arr_npc;
std::string s = vm.popString();
int32_t ch = vm.popDataValue();
Expand All @@ -140,7 +104,6 @@ void ::Logic::ScriptExternals::registerEngineExternals(World::WorldInstance& wor
LogInfo() << "DEBUG: " << s;
});


vm->registerExternalFunction("printscreen", [=](Daedalus::DaedalusVM& vm) {
int32_t timesec = vm.popDataValue();
if (verbose) LogInfo() << "timesec: " << timesec;
Expand Down Expand Up @@ -177,13 +140,10 @@ void ::Logic::ScriptExternals::registerEngineExternals(World::WorldInstance& wor
}
});



vm->registerExternalFunction("infomanager_hasfinished", [=](Daedalus::DaedalusVM& vm) {
vm.setReturn(pWorld->getDialogManager().isDialogActive() ? 0 : 1);
});


vm->registerExternalFunction("info_addchoice", [=](Daedalus::DaedalusVM& vm) {
uint32_t func = vm.popVar();
std::string text = vm.popString();
Expand All @@ -206,7 +166,6 @@ void ::Logic::ScriptExternals::registerEngineExternals(World::WorldInstance& wor
cInfo.subChoices.clear();
});


vm->registerExternalFunction("mob_hasitems", [=](Daedalus::DaedalusVM& vm) {
if (verbose) LogInfo() << "mob_hasitems";
uint32_t iteminstance = (uint32_t)vm.popDataValue();
Expand Down
69 changes: 2 additions & 67 deletions src/logic/scriptExternals/Externals_AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,73 +24,8 @@ void ScriptExternals::registerEngineExternals_ai(World::WorldInstance& world, Da
return vm->getDATFile().getSymbolByIndex(instance).instanceDataHandle.isValid();
};

// TODO: Refractor
auto getNPCByInstance = [vm, engine](size_t instance) {
assert(vm->getDATFile().getSymbolByIndex(instance).instanceDataClass == Daedalus::EInstanceClass::IC_Npc);

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

if (!hnpc.isValid())
{
//LogWarn() << "Invalid handle in instance: " << instance << " (" << vm->getDATFile().getSymbolByIndex(instance).name << ")";
//LogWarn() << "Callstack: " << vm->getCallStack();

VobTypes::NpcVobInformation vob;
vob.entity.invalidate();
return vob;
}

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

if (userData)
{
World::WorldInstance& world = engine->getWorldInstance(userData->world);
VobTypes::NpcVobInformation vob = VobTypes::asNpcVob(world, userData->vobEntity);

return vob;
}
else
{
LogWarn() << "No userptr on npc: " << npcData.name[0];

VobTypes::NpcVobInformation vob;
vob.entity.invalidate();

return vob;
}
};

auto getItemByInstance = [vm, engine](size_t instance) {
auto& parSymbol = vm->getDATFile().getSymbolByIndex(instance);
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 = vm->getGameState().getItem(hitem);
VobTypes::ScriptInstanceUserData* userData = reinterpret_cast<VobTypes::ScriptInstanceUserData*>(itemData.userPtr);

if (userData)
{
World::WorldInstance& world = engine->getWorldInstance(userData->world);
Vob::VobInformation vob = Vob::asVob(world, userData->vobEntity);

return vob;
}
else
{
LogWarn() << "No userptr on item: " << itemData.name;
}
}
else
{
LogWarn() << "could not get item handle from ParSymbol: " << parSymbol.name;
}
Vob::VobInformation vob;
vob.entity.invalidate();
return vob;
auto getNPCByInstance = [=](size_t instance) {
return pWorld->getScriptEngine().findNPCVobFromScriptInstance(instance);
};

vm->registerExternalFunction("ai_teleport", [=](Daedalus::DaedalusVM& vm) {
Expand Down
69 changes: 4 additions & 65 deletions src/logic/scriptExternals/Externals_Npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,12 @@ void ScriptExternals::registerEngineExternals_npc(World::WorldInstance& world,
return vm->getDATFile().getSymbolByIndex(instance).instanceDataHandle.isValid();
};

// TODO: Refractor
auto getNPCByInstance = [vm, engine](size_t instance) {
assert(vm->getDATFile().getSymbolByIndex(instance).instanceDataClass == Daedalus::EInstanceClass::IC_Npc);

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

if (!hnpc.isValid())
{
//LogWarn() << "Invalid handle in instance: " << instance << " (" << vm->getDATFile().getSymbolByIndex(instance).name << ")";
//LogWarn() << "Callstack: " << vm->getCallStack();

VobTypes::NpcVobInformation vob;
vob.entity.invalidate();
return vob;
}

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

if (userData)
{
World::WorldInstance& world = engine->getWorldInstance(userData->world);
VobTypes::NpcVobInformation vob = VobTypes::asNpcVob(world, userData->vobEntity);

return vob;
}
else
{
LogWarn() << "No userptr on npc: " << npcData.name[0];

VobTypes::NpcVobInformation vob;
vob.entity.invalidate();

return vob;
}
auto getNPCByInstance = [=](size_t instance) {
return pWorld->getScriptEngine().findNPCVobFromScriptInstance(instance);
};

auto getItemByInstance = [vm, engine](size_t instance) {
auto& parSymbol = vm->getDATFile().getSymbolByIndex(instance);
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 = vm->getGameState().getItem(hitem);
VobTypes::ScriptInstanceUserData* userData = reinterpret_cast<VobTypes::ScriptInstanceUserData*>(itemData.userPtr);

if (userData)
{
World::WorldInstance& world = engine->getWorldInstance(userData->world);
Vob::VobInformation vob = Vob::asVob(world, userData->vobEntity);

return vob;
}
else
{
LogWarn() << "No userptr on item: " << itemData.name;
}
}
else
{
LogWarn() << "could not get item handle from ParSymbol: " << parSymbol.name;
}
Vob::VobInformation vob;
vob.entity.invalidate();
return vob;
auto getItemByInstance = [=](size_t instance) {
return pWorld->getScriptEngine().findItemVobFromScriptInstance(instance);
};

vm->registerExternalFunction("npc_getdisttonpc", [=](Daedalus::DaedalusVM& vm) {
Expand Down
Loading

0 comments on commit fa23ae6

Please sign in to comment.