Skip to content

Commit

Permalink
Added npc_hasItems and npc_removeInvItem(s) (aka. you can now pay for…
Browse files Browse the repository at this point in the history
… your stuff)
  • Loading branch information
ataulien committed Nov 25, 2016
1 parent 16d8d60 commit 0a6caf1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/ZenLib
32 changes: 26 additions & 6 deletions src/logic/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ const std::list<Daedalus::GameState::ItemHandle>& Inventory::getItems()
return vm.getGameState().getInventoryOf(m_NPC);
}

bool Inventory::removeItem(const std::string& symName)
bool Inventory::removeItem(const std::string& symName, unsigned int count)
{
// Get script-engine
Logic::ScriptEngine& vm = m_Engine.getWorldInstance(m_World).getScriptEngine();
// Get script-engine
Logic::ScriptEngine& vm = m_Engine.getWorldInstance(m_World).getScriptEngine();

return vm.getGameState().removeInventoryItem(vm.getSymbolIndexByName(symName), m_NPC, count);
}

bool Inventory::removeItem(size_t symIndex, unsigned int count)
{
// Get script-engine
Logic::ScriptEngine& vm = m_Engine.getWorldInstance(m_World).getScriptEngine();

return vm.getGameState().removeInventoryItem(vm.getSymbolIndexByName(symName), m_NPC);
return vm.getGameState().removeInventoryItem(symIndex, m_NPC, count);
}

bool Inventory::removeItem(Daedalus::GameState::ItemHandle item)
bool Inventory::removeItem(Daedalus::GameState::ItemHandle item, unsigned int count)
{
// Get script-engine
Logic::ScriptEngine& vm = m_Engine.getWorldInstance(m_World).getScriptEngine();
Expand All @@ -51,7 +59,7 @@ bool Inventory::removeItem(Daedalus::GameState::ItemHandle item)

Daedalus::GEngineClasses::C_Item& data = vm.getGameState().getItem(item);

return vm.getGameState().removeInventoryItem(data.instanceSymbol, m_NPC);
return vm.getGameState().removeInventoryItem(data.instanceSymbol, m_NPC, count);
}

Daedalus::GameState::ItemHandle Inventory::getItem(size_t symIndex)
Expand All @@ -71,3 +79,15 @@ Daedalus::GameState::ItemHandle Inventory::getItem(size_t symIndex)
return Daedalus::GameState::ItemHandle();
}

int Inventory::getItemCount(size_t symIndex)
{
// Get script-engine
Logic::ScriptEngine& vm = m_Engine.getWorldInstance(m_World).getScriptEngine();

Daedalus::GameState::ItemHandle item = getItem(symIndex);

if(item.isValid())
return vm.getGameState().getItem(item).count[0];

return 0;
}
10 changes: 8 additions & 2 deletions src/logic/Inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ namespace Logic
* @param symName Instance to remove
* @return True, if the item was successfully removed, false if not found
*/
bool removeItem(const std::string& symName);
bool removeItem(Daedalus::GameState::ItemHandle item);
bool removeItem(const std::string& symName, unsigned int count = 1);
bool removeItem(size_t symIndex, unsigned int count = 1);
bool removeItem(Daedalus::GameState::ItemHandle item, unsigned int count = 1);

/**
* Returns a random instance of the given item
Expand All @@ -37,6 +38,11 @@ namespace Logic
*/
Daedalus::GameState::ItemHandle getItem(size_t symIndex);

/**
* @return Count of how many items of the given type are in this inventory
*/
int getItemCount(size_t symIndex);

/**
* @param symName Name of the symbol to get the instances from
* @return List of handles of the given instance-name inside this inventory
Expand Down
46 changes: 46 additions & 0 deletions src/logic/scriptExternals/Externals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,52 @@ void ::Logic::ScriptExternals::registerEngineExternals(World::WorldInstance& wor
vm.setReturn("");
});

vm->registerExternalFunction("npc_hasitems", [=](Daedalus::DaedalusVM& vm){
uint32_t iteminstance = vm.popDataValue();
int32_t owner = vm.popVar();

VobTypes::NpcVobInformation npc = getNPCByInstance(owner);

if(npc.isValid())
{
int count = npc.playerController->getInventory().getItemCount(iteminstance);
vm.setReturn(count);
}
else
{
vm.setReturn(0);
}
});

vm->registerExternalFunction("npc_removeinvitem", [=](Daedalus::DaedalusVM& vm){
uint32_t iteminstance = vm.popDataValue();
uint32_t owner = vm.popVar();

VobTypes::NpcVobInformation npc = getNPCByInstance(owner);

if(npc.isValid())
{
int count = npc.playerController->getInventory().removeItem(iteminstance);
}

vm.setReturn(0);
});

vm->registerExternalFunction("npc_removeinvitems", [=](Daedalus::DaedalusVM& vm){
uint32_t amount = vm.popDataValue();
uint32_t iteminstance = vm.popDataValue();
uint32_t owner = vm.popVar();

VobTypes::NpcVobInformation npc = getNPCByInstance(owner);

if(npc.isValid())
{
int count = npc.playerController->getInventory().removeItem(iteminstance, amount);
}

vm.setReturn(0);
});

vm->registerExternalFunction("ai_startstate", [=](Daedalus::DaedalusVM& vm){
std::string wpname = vm.popString();
int32_t statebehaviour = vm.popDataValue();
Expand Down

0 comments on commit 0a6caf1

Please sign in to comment.