Skip to content

Commit

Permalink
Merge remote-tracking branch 'rainChu/torches'
Browse files Browse the repository at this point in the history
  • Loading branch information
zinnschlag committed Oct 16, 2013
2 parents 8995cd8 + 6581815 commit 19bd0f9
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
44 changes: 44 additions & 0 deletions apps/openmw/mwclass/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,34 @@
#include "../mwworld/inventorystore.hpp"
#include "../mwworld/cellstore.hpp"
#include "../mwworld/physicssystem.hpp"
#include "../mwworld/customdata.hpp"

#include "../mwgui/tooltips.hpp"

#include "../mwrender/objects.hpp"
#include "../mwrender/renderinginterface.hpp"

namespace
{
struct CustomData : public MWWorld::CustomData
{
float mTime;
///< Time remaining

CustomData(MWWorld::Ptr ptr)
{
MWWorld::LiveCellRef<ESM::Light> *ref = ptr.get<ESM::Light>();
mTime = ref->mBase->mData.mTime;
}
///< Constructs this CustomData from the base values for Ptr.

virtual MWWorld::CustomData *clone() const
{
return new CustomData (*this);
}
};
}

namespace MWClass
{
void Light::insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const
Expand Down Expand Up @@ -182,6 +204,22 @@ namespace MWClass
return action;
}

void Light::setRemainingUsageTime (const MWWorld::Ptr& ptr, float duration) const
{
ensureCustomData(ptr);

float &timeRemaining = dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mTime;
timeRemaining = duration;
}

float Light::getRemainingUsageTime (const MWWorld::Ptr& ptr) const
{
ensureCustomData(ptr);

ESM::CellRef &ref = ptr.getCellRef();
return dynamic_cast<CustomData&> (*ptr.getRefData().getCustomData()).mTime;
}

MWWorld::Ptr
Light::copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const
{
Expand All @@ -191,6 +229,12 @@ namespace MWClass
return MWWorld::Ptr(&cell.mLights.insert(*ref), &cell);
}

void Light::ensureCustomData (const MWWorld::Ptr& ptr) const
{
if (!ptr.getRefData().getCustomData())
ptr.getRefData().setCustomData(new CustomData(ptr));
}

bool Light::canSell (const MWWorld::Ptr& item, int npcServices) const
{
return npcServices & ESM::NPC::Lights;
Expand Down
8 changes: 8 additions & 0 deletions apps/openmw/mwclass/light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace MWClass
virtual MWWorld::Ptr
copyToCellImpl(const MWWorld::Ptr &ptr, MWWorld::CellStore &cell) const;

void ensureCustomData (const MWWorld::Ptr& ptr) const;

public:

virtual void insertObjectRendering (const MWWorld::Ptr& ptr, MWRender::RenderingInterface& renderingInterface) const;
Expand Down Expand Up @@ -56,6 +58,12 @@ namespace MWClass
const;
///< Generate action for using via inventory menu

virtual void setRemainingUsageTime (const MWWorld::Ptr& ptr, float duration) const;
///< Sets the remaining duration of the object.

virtual float getRemainingUsageTime (const MWWorld::Ptr& ptr) const;
///< Returns the remaining duration of the object.

virtual std::string getModel(const MWWorld::Ptr &ptr) const;

virtual float getWeight (const MWWorld::Ptr& ptr) const;
Expand Down
46 changes: 46 additions & 0 deletions apps/openmw/mwmechanics/actors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ namespace MWMechanics
void Actors::updateNpc (const MWWorld::Ptr& ptr, float duration, bool paused)
{
if(!paused)
{
updateDrowning(ptr, duration);
updateEquippedLight(ptr, duration);
}
}

void Actors::adjustMagicEffects (const MWWorld::Ptr& creature)
Expand Down Expand Up @@ -202,6 +205,49 @@ namespace MWMechanics
stats.setTimeToStartDrowning(20);
}

void Actors::updateEquippedLight (const MWWorld::Ptr& ptr, float duration)
{
//If holding a light...
MWWorld::InventoryStore &inventoryStore = MWWorld::Class::get(ptr).getInventoryStore(ptr);
MWWorld::ContainerStoreIterator heldIter =
inventoryStore.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft);

if(heldIter.getType() == MWWorld::ContainerStore::Type_Light)
{
// Use time from the player's light
bool isPlayer = ptr.getRefData().getHandle()=="player";
if(isPlayer)
{
float timeRemaining = heldIter->getClass().getRemainingUsageTime(*heldIter);

// -1 is infinite light source. Other negative values are treated as 0.
if(timeRemaining != -1.0f)
{
timeRemaining -= duration;

if(timeRemaining > 0.0f)
heldIter->getClass().setRemainingUsageTime(*heldIter, timeRemaining);
else
{
heldIter->getRefData().setCount(0); // remove it
return;
}
}
}

// Both NPC and player lights extinguish in water.
if(MWBase::Environment::get().getWorld()->isSwimming(ptr))
{
heldIter->getRefData().setCount(0); // remove it

// ...But, only the player makes a sound.
if(isPlayer)
MWBase::Environment::get().getSoundManager()->playSound("torch out",
1.0, 1.0, MWBase::SoundManager::Play_TypeSfx, MWBase::SoundManager::Play_NoEnv);
}
}
}

Actors::Actors() : mDuration (0) {}

void Actors::addActor (const MWWorld::Ptr& ptr)
Expand Down
2 changes: 2 additions & 0 deletions apps/openmw/mwmechanics/actors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace MWMechanics

void updateDrowning (const MWWorld::Ptr& ptr, float duration);

void updateEquippedLight (const MWWorld::Ptr& ptr, float duration);

public:

Actors();
Expand Down
10 changes: 10 additions & 0 deletions apps/openmw/mwworld/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ namespace MWWorld
throw std::runtime_error ("class does not support unlocking");
}

void Class::setRemainingUsageTime (const Ptr& ptr, float duration) const
{
throw std::runtime_error ("class does not support time-based uses");
}

float Class::getRemainingUsageTime (const Ptr& ptr) const
{
throw std::runtime_error ("class does not support time-based uses");
}

std::string Class::getScript (const Ptr& ptr) const
{
return "";
Expand Down
8 changes: 8 additions & 0 deletions apps/openmw/mwworld/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ namespace MWWorld
virtual void unlock (const Ptr& ptr) const;
///< Unlock object (default implementation: throw an exception)

virtual void setRemainingUsageTime (const Ptr& ptr, float duration) const;
///< Sets the remaining duration of the object, such as an equippable light
/// source. (default implementation: throw an exception)

virtual float getRemainingUsageTime (const Ptr& ptr) const;
///< Returns the remaining duration of the object, such as an equippable light
/// source. (default implementation: throw an exception)

virtual std::string getScript (const Ptr& ptr) const;
///< Return name of the script attached to ptr (default implementation: return an empty
/// string).
Expand Down

0 comments on commit 19bd0f9

Please sign in to comment.