Skip to content

Commit

Permalink
Merge branch 'goblinsnacks' into 'master'
Browse files Browse the repository at this point in the history
Allow creatures to use potions

Closes #7604

See merge request OpenMW/openmw!3469
  • Loading branch information
psi29a committed Oct 4, 2023
2 parents 15e6aba + 0814ef5 commit d9f8757
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
Bug #7557: Terrain::ChunkManager::createChunk is called twice for the same position, lod on initial loading
Bug #7573: Drain Fatigue can't bring fatigue below zero by default
Bug #7603: Scripts menu size is not updated properly
Bug #7604: Goblins Grunt becomes idle once injured
Feature #3537: Shader-based water ripples
Feature #5492: Let rain and snow collide with statics
Feature #6149: Dehardcode Lua API_REVISION
Expand Down
13 changes: 13 additions & 0 deletions apps/openmw/mwclass/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <components/esm3/loadmgef.hpp>

#include "../mwbase/environment.hpp"
#include "../mwbase/luamanager.hpp"
#include "../mwbase/soundmanager.hpp"
#include "../mwbase/world.hpp"

Expand All @@ -14,6 +15,7 @@
#include "../mwphysics/physicssystem.hpp"

#include "../mwworld/inventorystore.hpp"
#include "../mwworld/worldmodel.hpp"

namespace MWClass
{
Expand Down Expand Up @@ -90,4 +92,15 @@ namespace MWClass
moveSpeed *= 0.75f;
return moveSpeed;
}

bool Actor::consume(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) const
{
MWBase::Environment::get().getWorld()->breakInvisibility(actor);
MWMechanics::CastSpell cast(actor, actor);
const ESM::RefId& recordId = consumable.getCellRef().getRefId();
MWBase::Environment::get().getWorldModel()->registerPtr(consumable);
MWBase::Environment::get().getLuaManager()->itemConsumed(consumable, actor);
actor.getClass().getContainerStore(actor).remove(consumable, 1);
return cast.cast(recordId);
}
}
2 changes: 2 additions & 0 deletions apps/openmw/mwclass/actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace MWClass
/// Return current movement speed.
float getCurrentSpeed(const MWWorld::Ptr& ptr) const override;

bool consume(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) const override;

// not implemented
Actor(const Actor&) = delete;
Actor& operator=(const Actor&) = delete;
Expand Down
12 changes: 0 additions & 12 deletions apps/openmw/mwclass/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "../mwbase/dialoguemanager.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/luamanager.hpp"
#include "../mwbase/mechanicsmanager.hpp"
#include "../mwbase/soundmanager.hpp"
#include "../mwbase/windowmanager.hpp"
Expand Down Expand Up @@ -1123,17 +1122,6 @@ namespace MWClass
return getNpcStats(ptr).isWerewolf() ? 0.0f : Actor::getEncumbrance(ptr);
}

bool Npc::consume(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) const
{
MWBase::Environment::get().getWorld()->breakInvisibility(actor);
MWMechanics::CastSpell cast(actor, actor);
const ESM::RefId& recordId = consumable.getCellRef().getRefId();
MWBase::Environment::get().getWorldModel()->registerPtr(consumable);
MWBase::Environment::get().getLuaManager()->itemConsumed(consumable, actor);
actor.getClass().getContainerStore(actor).remove(consumable, 1);
return cast.cast(recordId);
}

void Npc::skillUsageSucceeded(const MWWorld::Ptr& ptr, ESM::RefId skill, int usageType, float extraFactor) const
{
MWMechanics::NpcStats& stats = getNpcStats(ptr);
Expand Down
2 changes: 0 additions & 2 deletions apps/openmw/mwclass/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ namespace MWClass
float getArmorRating(const MWWorld::Ptr& ptr) const override;
///< @return combined armor rating of this actor

bool consume(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) const override;

void adjustScale(const MWWorld::ConstPtr& ptr, osg::Vec3f& scale, bool rendering) const override;
/// @param rendering Indicates if the scale to adjust is for the rendering mesh, or for the collision mesh

Expand Down
41 changes: 21 additions & 20 deletions apps/openmw/mwmechanics/aicombataction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,34 +176,35 @@ namespace MWMechanics
return bestAction;
}

if (actor.getClass().hasInventoryStore(actor))
const bool hasInventoryStore = actor.getClass().hasInventoryStore(actor);
MWWorld::ContainerStore& store = actor.getClass().getContainerStore(actor);
for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it)
{
MWWorld::InventoryStore& store = actor.getClass().getInventoryStore(actor);

for (MWWorld::ContainerStoreIterator it = store.begin(); it != store.end(); ++it)
if (it->getType() == ESM::Potion::sRecordId)
{
if (it->getType() == ESM::Potion::sRecordId)
float rating = ratePotion(*it, actor);
if (rating > bestActionRating)
{
float rating = ratePotion(*it, actor);
if (rating > bestActionRating)
{
bestActionRating = rating;
bestAction = std::make_unique<ActionPotion>(*it);
antiFleeRating = std::numeric_limits<float>::max();
}
bestActionRating = rating;
bestAction = std::make_unique<ActionPotion>(*it);
antiFleeRating = std::numeric_limits<float>::max();
}
else if (!it->getClass().getEnchantment(*it).empty())
}
// TODO remove inventory store check, creatures should be able to use enchanted items they cannot equip
else if (hasInventoryStore && !it->getClass().getEnchantment(*it).empty())
{
float rating = rateMagicItem(*it, actor, enemy);
if (rating > bestActionRating)
{
float rating = rateMagicItem(*it, actor, enemy);
if (rating > bestActionRating)
{
bestActionRating = rating;
bestAction = std::make_unique<ActionEnchantedItem>(it);
antiFleeRating = std::numeric_limits<float>::max();
}
bestActionRating = rating;
bestAction = std::make_unique<ActionEnchantedItem>(it);
antiFleeRating = std::numeric_limits<float>::max();
}
}
}

if (hasInventoryStore)
{
MWWorld::Ptr bestArrow;
float bestArrowRating = rateAmmo(actor, enemy, bestArrow, ESM::Weapon::Arrow);

Expand Down

0 comments on commit d9f8757

Please sign in to comment.