Issue #19: factored out local script handling into a separate class
This also fixes a bug related to self-destructing references (introduced during the cell handling improvements)actorid
parent
5bfd953dd4
commit
10778d8c3e
@ -0,0 +1,80 @@
|
||||
|
||||
#include "localscripts.hpp"
|
||||
|
||||
void MWWorld::LocalScripts::setIgnore (const Ptr& ptr)
|
||||
{
|
||||
mIgnore = ptr;
|
||||
}
|
||||
|
||||
void MWWorld::LocalScripts::startIteration()
|
||||
{
|
||||
mIter = mScripts.begin();
|
||||
}
|
||||
|
||||
bool MWWorld::LocalScripts::isFinished() const
|
||||
{
|
||||
if (mIter==mScripts.end())
|
||||
return true;
|
||||
|
||||
if (!mIgnore.isEmpty() && mIter->second==mIgnore)
|
||||
{
|
||||
std::list<std::pair<std::string, Ptr> >::iterator iter = mIter;
|
||||
return ++iter==mScripts.end();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::pair<std::string, MWWorld::Ptr> MWWorld::LocalScripts::getNext()
|
||||
{
|
||||
assert (!isFinished());
|
||||
|
||||
std::list<std::pair<std::string, Ptr> >::iterator iter = mIter++;
|
||||
|
||||
if (mIgnore.isEmpty() || iter->second!=mIgnore)
|
||||
return *iter;
|
||||
|
||||
return getNext();
|
||||
}
|
||||
|
||||
void MWWorld::LocalScripts::add (const std::string& scriptName, const Ptr& ptr)
|
||||
{
|
||||
mScripts.push_back (std::make_pair (scriptName, ptr));
|
||||
}
|
||||
|
||||
void MWWorld::LocalScripts::clear()
|
||||
{
|
||||
mScripts.clear();
|
||||
}
|
||||
|
||||
void MWWorld::LocalScripts::clearCell (Ptr::CellStore *cell)
|
||||
{
|
||||
std::list<std::pair<std::string, Ptr> >::iterator iter = mScripts.begin();
|
||||
|
||||
while (iter!=mScripts.end())
|
||||
{
|
||||
if (iter->second.getCell()==cell)
|
||||
{
|
||||
if (iter==mIter)
|
||||
++mIter;
|
||||
|
||||
mScripts.erase (iter++);
|
||||
}
|
||||
else
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
void MWWorld::LocalScripts::remove (const Ptr& ptr)
|
||||
{
|
||||
for (std::list<std::pair<std::string, Ptr> >::iterator iter = mScripts.begin();
|
||||
iter!=mScripts.end(); ++iter)
|
||||
if (iter->second==ptr)
|
||||
{
|
||||
if (iter==mIter)
|
||||
++mIter;
|
||||
|
||||
mScripts.erase (iter);
|
||||
break;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
#ifndef GAME_MWWORLD_LOCALSCRIPTS_H
|
||||
#define GAME_MWWORLD_LOCALSCRIPTS_H
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
#include "ptr.hpp"
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
/// \brief List of active local scripts
|
||||
class LocalScripts
|
||||
{
|
||||
std::list<std::pair<std::string, Ptr> > mScripts;
|
||||
std::list<std::pair<std::string, Ptr> >::iterator mIter;
|
||||
MWWorld::Ptr mIgnore;
|
||||
|
||||
public:
|
||||
|
||||
void setIgnore (const Ptr& ptr);
|
||||
///< Mark a single reference for ignoring during iteration over local scripts (will revoke
|
||||
/// previous ignores).
|
||||
|
||||
void startIteration();
|
||||
///< Set the iterator to the begin of the script list.
|
||||
|
||||
bool isFinished() const;
|
||||
///< Is iteration finished?
|
||||
|
||||
std::pair<std::string, Ptr> getNext();
|
||||
///< Get next local script (must not be called if isFinished())
|
||||
|
||||
void add (const std::string& scriptName, const Ptr& ptr);
|
||||
///< Add script to collection of active local scripts.
|
||||
|
||||
void clear();
|
||||
///< Clear active local scripts collection.
|
||||
|
||||
void clearCell (Ptr::CellStore *cell);
|
||||
///< Remove all scripts belonging to \a cell.
|
||||
|
||||
void remove (const Ptr& ptr);
|
||||
///< Remove script for given reference (ignored if reference does not have a scirpt listed).
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue