forked from teamnwah/openmw-tes3coop
added GetItemCount
This commit is contained in:
parent
3ea85b4619
commit
589f8b5ede
5 changed files with 142 additions and 1 deletions
|
@ -83,6 +83,7 @@ set(GAMEWORLD
|
|||
mwworld/globals.cpp
|
||||
mwworld/class.cpp
|
||||
mwworld/actionteleport.cpp
|
||||
mwworld/containerutil.cpp
|
||||
)
|
||||
set(GAMEWORLD_HEADER
|
||||
mwworld/refdata.hpp
|
||||
|
@ -96,6 +97,7 @@ set(GAMEWORLD_HEADER
|
|||
mwworld/actionteleport.hpp
|
||||
mwworld/containerstore.hpp
|
||||
mwworld/manualref.hpp
|
||||
mwworld/containerutil.hpp
|
||||
)
|
||||
source_group(apps\\openmw\\mwworld FILES ${GAMEWORLD} ${GAMEWORLD_HEADER})
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include "../mwworld/manualref.hpp"
|
||||
#include "../mwworld/class.hpp"
|
||||
#include "../mwworld/containerutil.hpp"
|
||||
|
||||
#include "interpretercontext.hpp"
|
||||
|
||||
|
@ -71,18 +72,91 @@ namespace MWScript
|
|||
}
|
||||
};
|
||||
|
||||
class OpGetItemCount : public Interpreter::Opcode0
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void execute (Interpreter::Runtime& runtime)
|
||||
{
|
||||
MWScript::InterpreterContext& context
|
||||
= static_cast<MWScript::InterpreterContext&> (runtime.getContext());
|
||||
|
||||
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
||||
runtime.pop();
|
||||
|
||||
MWWorld::Ptr ptr = context.getReference();
|
||||
|
||||
std::vector<MWWorld::Ptr> list;
|
||||
|
||||
MWWorld::listItemsInContainer (item,
|
||||
MWWorld::Class::get (ptr).getContainerStore (ptr),
|
||||
context.getWorld().getStore(), list);
|
||||
|
||||
Interpreter::Type_Integer sum = 0;
|
||||
|
||||
for (std::vector<MWWorld::Ptr>::iterator iter (list.begin()); iter!=list.end();
|
||||
++iter)
|
||||
{
|
||||
sum += iter->getRefData().getCount();
|
||||
}
|
||||
|
||||
runtime.push (sum);
|
||||
}
|
||||
};
|
||||
|
||||
class OpGetItemCountExplicit : public Interpreter::Opcode0
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void execute (Interpreter::Runtime& runtime)
|
||||
{
|
||||
MWScript::InterpreterContext& context
|
||||
= static_cast<MWScript::InterpreterContext&> (runtime.getContext());
|
||||
|
||||
std::string id = runtime.getStringLiteral (runtime[0].mInteger);
|
||||
runtime.pop();
|
||||
|
||||
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
||||
runtime.pop();
|
||||
|
||||
MWWorld::Ptr ptr = context.getWorld().getPtr (id, false);
|
||||
|
||||
std::vector<MWWorld::Ptr> list;
|
||||
|
||||
MWWorld::listItemsInContainer (item,
|
||||
MWWorld::Class::get (ptr).getContainerStore (ptr),
|
||||
context.getWorld().getStore(), list);
|
||||
|
||||
Interpreter::Type_Integer sum = 0;
|
||||
|
||||
for (std::vector<MWWorld::Ptr>::iterator iter (list.begin()); iter!=list.end();
|
||||
++iter)
|
||||
{
|
||||
sum += iter->getRefData().getCount();
|
||||
}
|
||||
|
||||
runtime.push (sum);
|
||||
}
|
||||
};
|
||||
|
||||
const int opcodeAddItem = 0x2000076;
|
||||
const int opcodeAddItemExplicit = 0x2000077;
|
||||
const int opcodeGetItemCount = 0x2000078;
|
||||
const int opcodeGetItemCountExplicit = 0x2000079;
|
||||
|
||||
void registerExtensions (Compiler::Extensions& extensions)
|
||||
{
|
||||
extensions.registerInstruction ("additem", "cl", opcodeAddItem, opcodeAddItemExplicit);
|
||||
extensions.registerFunction ("getitemcount", 'l', "c", opcodeGetItemCount,
|
||||
opcodeGetItemCountExplicit);
|
||||
}
|
||||
|
||||
void installOpcodes (Interpreter::Interpreter& interpreter)
|
||||
{
|
||||
interpreter.installSegment5 (opcodeAddItem, new OpAddItem);
|
||||
interpreter.installSegment5 (opcodeAddItemExplicit, new OpAddItemExplicit);
|
||||
interpreter.installSegment5 (opcodeGetItemCount, new OpGetItemCount);
|
||||
interpreter.installSegment5 (opcodeGetItemCountExplicit, new OpGetItemCountExplicit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,4 +79,6 @@ op 0x2000072-0x2000074: GetDynamic (health, magicka, fatigue), explicit referenc
|
|||
op 0x2000075: Activate
|
||||
op 0x2000076: AddItem
|
||||
op 0x2000077: AddItem, explicit reference
|
||||
opcodes 0x2000078-0x3ffffff unused
|
||||
op 0x2000078: GetItemCount
|
||||
op 0x2000079: GetItemCount, explicit reference
|
||||
opcodes 0x200007a-0x3ffffff unused
|
||||
|
|
43
apps/openmw/mwworld/containerutil.cpp
Normal file
43
apps/openmw/mwworld/containerutil.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
|
||||
#include "containerutil.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
template<typename T>
|
||||
void listItemsInContainerImp (const std::string& id,
|
||||
ESMS::CellRefList<T, MWWorld::RefData>& containerStore,
|
||||
const ESMS::RecListT<T>& store, std::vector<MWWorld::Ptr>& list)
|
||||
{
|
||||
if (const T *record = store.search (id))
|
||||
{
|
||||
for (typename ESMS::CellRefList<T, MWWorld::RefData>::List::iterator iter
|
||||
(containerStore.list.begin());
|
||||
iter!=containerStore.list.end(); ++iter)
|
||||
{
|
||||
if (iter->base==record)
|
||||
list.push_back (MWWorld::Ptr (&*iter, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
void listItemsInContainer (const std::string& id,
|
||||
ContainerStore<MWWorld::RefData>& containerStore,
|
||||
const ESMS::ESMStore& store, std::vector<Ptr>& list)
|
||||
{
|
||||
listItemsInContainerImp (id, containerStore.potions, store.potions, list);
|
||||
listItemsInContainerImp (id, containerStore.appas, store.appas, list);
|
||||
listItemsInContainerImp (id, containerStore.armors, store.armors, list);
|
||||
listItemsInContainerImp (id, containerStore.books, store.books, list);
|
||||
listItemsInContainerImp (id, containerStore.clothes, store.clothes, list);
|
||||
listItemsInContainerImp (id, containerStore.ingreds, store.ingreds, list);
|
||||
listItemsInContainerImp (id, containerStore.lights, store.lights, list);
|
||||
listItemsInContainerImp (id, containerStore.lockpicks, store.lockpicks, list);
|
||||
listItemsInContainerImp (id, containerStore.miscItems, store.miscItems, list);
|
||||
listItemsInContainerImp (id, containerStore.probes, store.probes, list);
|
||||
listItemsInContainerImp (id, containerStore.repairs, store.repairs, list);
|
||||
listItemsInContainerImp (id, containerStore.weapons, store.weapons, list);
|
||||
}
|
||||
}
|
20
apps/openmw/mwworld/containerutil.hpp
Normal file
20
apps/openmw/mwworld/containerutil.hpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifndef GAME_MWWORLD_CONTAINERUTIL_H
|
||||
#define GAME_MWWORLD_CONTAINERUTIL_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <components/esm_store/store.hpp>
|
||||
|
||||
#include "containerstore.hpp"
|
||||
#include "ptr.hpp"
|
||||
#include "refdata.hpp"
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
void listItemsInContainer (const std::string& id, ContainerStore<MWWorld::RefData>& containerStore,
|
||||
const ESMS::ESMStore& store, std::vector<Ptr>& list);
|
||||
///< append all references with the given id to list.
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue