diff --git a/apps/openmw/mwclass/creature.cpp b/apps/openmw/mwclass/creature.cpp index 9e5afb66a..fa38949d8 100644 --- a/apps/openmw/mwclass/creature.cpp +++ b/apps/openmw/mwclass/creature.cpp @@ -325,22 +325,38 @@ namespace MWClass void Creature::onHit(const MWWorld::Ptr &ptr, float damage, bool ishealth, const MWWorld::Ptr &object, const MWWorld::Ptr &attacker, const osg::Vec3f &hitPosition, bool successful) const { - // NOTE: 'object' and/or 'attacker' may be empty. + MWMechanics::CreatureStats& stats = ptr.getClass().getCreatureStats(ptr); - if (!attacker.isEmpty() && !ptr.getClass().getCreatureStats(ptr).getAiSequence().isInCombat(attacker)) - getCreatureStats(ptr).setAttacked(true); + // NOTE: 'object' and/or 'attacker' may be empty. + if (!attacker.isEmpty() && !stats.getAiSequence().isInCombat(attacker)) + stats.setAttacked(true); // Self defense bool setOnPcHitMe = true; // Note OnPcHitMe is not set for friendly hits. // No retaliation for totally static creatures (they have no movement or attacks anyway) if (isMobile(ptr) && !attacker.isEmpty()) - { setOnPcHitMe = MWBase::Environment::get().getMechanicsManager()->actorAttacked(ptr, attacker); + + // Attacker and target store each other as hitattemptactor if they have no one stored yet + if (!attacker.isEmpty() && !ptr.isEmpty()) + { + MWMechanics::CreatureStats& statsAttacker = attacker.getClass().getCreatureStats(attacker); + // First handle the attacked actor + if (stats.getHitAttemptActor().isEmpty() + && (statsAttacker.getAiSequence().isInCombat(ptr) + || attacker == MWMechanics::getPlayer())) + stats.setHitAttemptActor(attacker); + + // Next handle the attacking actor + if (statsAttacker.getHitAttemptActor().isEmpty() + && (statsAttacker.getAiSequence().isInCombat(ptr) + || attacker == MWMechanics::getPlayer())) + statsAttacker.setHitAttemptActor(ptr); } if (!object.isEmpty()) - getCreatureStats(ptr).setLastHitAttemptObject(object.getCellRef().getRefId()); + stats.setLastHitAttemptObject(object.getCellRef().getRefId()); if (setOnPcHitMe && !attacker.isEmpty() && attacker == MWMechanics::getPlayer()) { @@ -358,7 +374,7 @@ namespace MWClass } if (!object.isEmpty()) - getCreatureStats(ptr).setLastHitObject(object.getCellRef().getRefId()); + stats.setLastHitObject(object.getCellRef().getRefId()); if (damage > 0.0f && !object.isEmpty()) MWMechanics::resistNormalWeapon(ptr, attacker, object, damage); @@ -371,16 +387,13 @@ namespace MWClass if (!attacker.isEmpty()) { // Check for knockdown - float agilityTerm = getCreatureStats(ptr).getAttribute(ESM::Attribute::Agility).getModified() * getGmst().fKnockDownMult->getFloat(); - float knockdownTerm = getCreatureStats(ptr).getAttribute(ESM::Attribute::Agility).getModified() + float agilityTerm = stats.getAttribute(ESM::Attribute::Agility).getModified() * getGmst().fKnockDownMult->getFloat(); + float knockdownTerm = stats.getAttribute(ESM::Attribute::Agility).getModified() * getGmst().iKnockDownOddsMult->getInt() * 0.01f + getGmst().iKnockDownOddsBase->getInt(); if (ishealth && agilityTerm <= damage && knockdownTerm <= Misc::Rng::roll0to99()) - { - getCreatureStats(ptr).setKnockedDown(true); - - } + stats.setKnockedDown(true); else - getCreatureStats(ptr).setHitRecovery(true); // Is this supposed to always occur? + stats.setHitRecovery(true); // Is this supposed to always occur? } damage = std::max(1.f, damage); @@ -395,15 +408,15 @@ namespace MWClass MWBase::Environment::get().getSoundManager()->playSound3D(ptr, "Health Damage", 1.0f, 1.0f); - MWMechanics::DynamicStat health(getCreatureStats(ptr).getHealth()); + MWMechanics::DynamicStat health(stats.getHealth()); health.setCurrent(health.getCurrent() - damage); - getCreatureStats(ptr).setHealth(health); + stats.setHealth(health); } else { - MWMechanics::DynamicStat fatigue(getCreatureStats(ptr).getFatigue()); + MWMechanics::DynamicStat fatigue(stats.getFatigue()); fatigue.setCurrent(fatigue.getCurrent() - damage, true); - getCreatureStats(ptr).setFatigue(fatigue); + stats.setFatigue(fatigue); } } } diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index 9e7b568fa..bc5a2fec5 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -654,22 +654,38 @@ namespace MWClass void Npc::onHit(const MWWorld::Ptr &ptr, float damage, bool ishealth, const MWWorld::Ptr &object, const MWWorld::Ptr &attacker, const osg::Vec3f &hitPosition, bool successful) const { MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager(); - - // NOTE: 'object' and/or 'attacker' may be empty. - - bool wasDead = getCreatureStats(ptr).isDead(); + MWMechanics::CreatureStats& stats = ptr.getClass().getCreatureStats(ptr); + bool wasDead = stats.isDead(); // Note OnPcHitMe is not set for friendly hits. bool setOnPcHitMe = true; - if (!attacker.isEmpty() && !ptr.getClass().getCreatureStats(ptr).getAiSequence().isInCombat(attacker)) - { - getCreatureStats(ptr).setAttacked(true); + // NOTE: 'object' and/or 'attacker' may be empty. + if (!attacker.isEmpty() && !stats.getAiSequence().isInCombat(attacker)) + { + stats.setAttacked(true); setOnPcHitMe = MWBase::Environment::get().getMechanicsManager()->actorAttacked(ptr, attacker); } + // Attacker and target store each other as hitattemptactor if they have no one stored yet + if (!attacker.isEmpty() && !ptr.isEmpty()) + { + MWMechanics::CreatureStats& statsAttacker = attacker.getClass().getCreatureStats(attacker); + // First handle the attacked actor + if (stats.getHitAttemptActor().isEmpty() + && (statsAttacker.getAiSequence().isInCombat(ptr) + || attacker == MWMechanics::getPlayer())) + stats.setHitAttemptActor(attacker); + + // Next handle the attacking actor + if (statsAttacker.getHitAttemptActor().isEmpty() + && (statsAttacker.getAiSequence().isInCombat(ptr) + || attacker == MWMechanics::getPlayer())) + statsAttacker.setHitAttemptActor(ptr); + } + if (!object.isEmpty()) - getCreatureStats(ptr).setLastHitAttemptObject(object.getCellRef().getRefId()); + stats.setLastHitAttemptObject(object.getCellRef().getRefId()); if (setOnPcHitMe && !attacker.isEmpty() && attacker == MWMechanics::getPlayer()) { @@ -687,7 +703,7 @@ namespace MWClass } if (!object.isEmpty()) - getCreatureStats(ptr).setLastHitObject(object.getCellRef().getRefId()); + stats.setLastHitObject(object.getCellRef().getRefId()); if (damage > 0.0f && !object.isEmpty()) @@ -706,21 +722,16 @@ namespace MWClass int chance = store.get().find("iVoiceHitOdds")->getInt(); if (Misc::Rng::roll0to99() < chance) - { MWBase::Environment::get().getDialogueManager()->say(ptr, "hit"); - } // Check for knockdown - float agilityTerm = getCreatureStats(ptr).getAttribute(ESM::Attribute::Agility).getModified() * gmst.fKnockDownMult->getFloat(); - float knockdownTerm = getCreatureStats(ptr).getAttribute(ESM::Attribute::Agility).getModified() + float agilityTerm = stats.getAttribute(ESM::Attribute::Agility).getModified() * gmst.fKnockDownMult->getFloat(); + float knockdownTerm = stats.getAttribute(ESM::Attribute::Agility).getModified() * gmst.iKnockDownOddsMult->getInt() * 0.01f + gmst.iKnockDownOddsBase->getInt(); if (ishealth && agilityTerm <= damage && knockdownTerm <= Misc::Rng::roll0to99()) - { - getCreatureStats(ptr).setKnockedDown(true); - - } + stats.setKnockedDown(true); else - getCreatureStats(ptr).setHitRecovery(true); // Is this supposed to always occur? + stats.setHitRecovery(true); // Is this supposed to always occur? if (damage > 0 && ishealth) { @@ -799,13 +810,13 @@ namespace MWClass } MWMechanics::DynamicStat health(getCreatureStats(ptr).getHealth()); health.setCurrent(health.getCurrent() - damage); - getCreatureStats(ptr).setHealth(health); + stats.setHealth(health); } else { MWMechanics::DynamicStat fatigue(getCreatureStats(ptr).getFatigue()); fatigue.setCurrent(fatigue.getCurrent() - damage, true); - getCreatureStats(ptr).setFatigue(fatigue); + stats.setFatigue(fatigue); } if (!wasDead && getCreatureStats(ptr).isDead()) diff --git a/apps/openmw/mwmechanics/actors.cpp b/apps/openmw/mwmechanics/actors.cpp index 5668832f7..a6380608a 100644 --- a/apps/openmw/mwmechanics/actors.cpp +++ b/apps/openmw/mwmechanics/actors.cpp @@ -9,6 +9,8 @@ #include +#include + #include "../mwworld/esmstore.hpp" #include "../mwworld/class.hpp" #include "../mwworld/inventorystore.hpp" @@ -280,7 +282,7 @@ namespace MWMechanics void Actors::engageCombat (const MWWorld::Ptr& actor1, const MWWorld::Ptr& actor2, bool againstPlayer) { - const CreatureStats& creatureStats1 = actor1.getClass().getCreatureStats(actor1); + CreatureStats& creatureStats1 = actor1.getClass().getCreatureStats(actor1); if (creatureStats1.getAiSequence().isInCombat(actor2)) return; @@ -298,97 +300,125 @@ namespace MWMechanics if (!actor1.getClass().isMobile(actor1)) return; - // Start combat if target actor is in combat with one of our followers or escorters - const std::list& followersAndEscorters = getActorsSidingWith(actor1); - for (std::list::const_iterator it = followersAndEscorters.begin(); it != followersAndEscorters.end(); ++it) + // If this is set to true, actor1 will start combat with actor2 if the awareness check at the end of the method returns true + bool aggressive = false; + + // Get actors allied with actor1. Includes those following or escorting actor1, actors following or escorting those actors, (recursive) + // and any actor currently being followed or escorted by actor1 + std::set allies1; + getActorsSidingWith(actor1, allies1); + + // If an ally of actor1 has been attacked by actor2 or has attacked actor2, start combat between actor1 and actor2 + for (std::set::const_iterator it = allies1.begin(); it != allies1.end(); ++it) { - // Need to check both ways since player doesn't use AI packages - if ((creatureStats2.getAiSequence().isInCombat(*it) - || it->getClass().getCreatureStats(*it).getAiSequence().isInCombat(actor2)) - && !creatureStats1.getAiSequence().isInCombat(*it)) + if (creatureStats1.getAiSequence().isInCombat(*it)) + continue; + + if (!it->getClass().getCreatureStats(*it).getHitAttemptActor().isEmpty() + && it->getClass().getCreatureStats(*it).getHitAttemptActor() == actor2) { MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); - return; + // Also set the same hit attempt actor. Otherwise, if fighting the player, they may stop combat + // if the player gets out of reach, while the ally would continue combat with the player + creatureStats1.setHitAttemptActor(actor2); + return; } - } - // Start combat if target actor is in combat with someone we are following through a follow package - for (std::list::const_iterator it = creatureStats1.getAiSequence().begin(); it != creatureStats1.getAiSequence().end(); ++it) - { - if (!(*it)->sideWithTarget()) - continue; - - MWWorld::Ptr followTarget = (*it)->getTarget(); + // If there's been no attack attempt yet but an ally of actor1 is in combat with actor2, become aggressive to actor2 + if (it->getClass().getCreatureStats(*it).getAiSequence().isInCombat(actor2)) + aggressive = true; + } - if (followTarget.isEmpty()) - continue; + std::set playerFollowersAndEscorters; + getActorsSidingWith(MWMechanics::getPlayer(), playerFollowersAndEscorters); - if (creatureStats1.getAiSequence().isInCombat(followTarget)) - continue; + bool isPlayerFollowerOrEscorter = std::find(playerFollowersAndEscorters.begin(), playerFollowersAndEscorters.end(), actor1) != playerFollowersAndEscorters.end(); - // Need to check both ways since player doesn't use AI packages - if (creatureStats2.getAiSequence().isInCombat(followTarget) - || followTarget.getClass().getCreatureStats(followTarget).getAiSequence().isInCombat(actor2)) + // If actor2 and at least one actor2 are in combat with actor1, actor1 and its allies start combat with them + // Doesn't apply for player followers/escorters + if (!aggressive && !isPlayerFollowerOrEscorter) + { + // Check that actor2 is in combat with actor1 + if (actor2.getClass().getCreatureStats(actor2).getAiSequence().isInCombat(actor1)) { - MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); - return; + std::set allies2; + getActorsSidingWith(actor2, allies2); + // Check that an ally of actor2 is also in combat with actor1 + for (std::set::const_iterator it = allies2.begin(); it != allies2.end(); ++it) + { + if ((it)->getClass().getCreatureStats(*it).getAiSequence().isInCombat(actor1)) + { + MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); + // Also have actor1's allies start combat + for (std::set::const_iterator it2 = allies1.begin(); it2 != allies1.end(); ++it2) + MWBase::Environment::get().getMechanicsManager()->startCombat(*it2, actor2); + return; + } + } } } - - // Start combat with the player if we are already in combat with a player follower or escorter - const std::list& playerFollowersAndEscorters = getActorsSidingWith(getPlayer()); - if (againstPlayer) + + // If set in the settings file, player followers and escorters will become aggressive toward enemies in combat with them or the player + if (!aggressive && isPlayerFollowerOrEscorter && Settings::Manager::getBool("followers attack on sight", "Game")) { - for (std::list::const_iterator it = playerFollowersAndEscorters.begin(); it != playerFollowersAndEscorters.end(); ++it) + if (actor2.getClass().getCreatureStats(actor2).getAiSequence().isInCombat(actor1)) + aggressive = true; + else { - if (creatureStats1.getAiSequence().isInCombat(*it)) + for (std::set::const_iterator it = allies1.begin(); it != allies1.end(); ++it) { - MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); - return; + if (actor2.getClass().getCreatureStats(actor2).getAiSequence().isInCombat(*it)) + { + aggressive = true; + break; + } } } } - // Otherwise, don't initiate combat with an unreachable target - if (!MWMechanics::canFight(actor1,actor2)) + // Stop here if target is unreachable + if (!MWMechanics::canFight(actor1, actor2)) return; - bool aggressive = false; - - if (againstPlayer || std::find(playerFollowersAndEscorters.begin(), playerFollowersAndEscorters.end(), actor2) != playerFollowersAndEscorters.end()) + // Do aggression check if actor2 is the player or a player follower or escorter + if (!aggressive) { - // Player followers and escorters with high fight should not initiate combat here with the player or with - // other player followers or escorters - if (std::find(playerFollowersAndEscorters.begin(), playerFollowersAndEscorters.end(), actor1) != playerFollowersAndEscorters.end()) - return; - - aggressive = MWBase::Environment::get().getMechanicsManager()->isAggressive(actor1, actor2); + if (againstPlayer || std::find(playerFollowersAndEscorters.begin(), playerFollowersAndEscorters.end(), actor2) != playerFollowersAndEscorters.end()) + { + // Player followers and escorters with high fight should not initiate combat with the player or with + // other player followers or escorters + if (std::find(playerFollowersAndEscorters.begin(), playerFollowersAndEscorters.end(), actor1) == playerFollowersAndEscorters.end()) + aggressive = MWBase::Environment::get().getMechanicsManager()->isAggressive(actor1, actor2); + } } - else + + // Make guards go aggressive with creatures that are in combat, unless the creature is a follower or escorter + if (actor1.getClass().isClass(actor1, "Guard") && !actor2.getClass().isNpc()) { - // Make guards fight aggressive creatures - if (!actor1.getClass().isNpc() && actor2.getClass().isClass(actor2, "Guard")) + bool followerOrEscorter = false; + for (std::list::const_iterator it = creatureStats2.getAiSequence().begin(); it != creatureStats2.getAiSequence().end(); ++it) { - if (creatureStats1.getAiSequence().isInCombat() && MWBase::Environment::get().getMechanicsManager()->isAggressive(actor1, actor2)) - aggressive = true; + // The follow package must be first or have nothing but combat before it + if ((*it)->sideWithTarget()) + { + followerOrEscorter = true; + break; + } + else if ((*it)->getTypeId() != MWMechanics::AiPackage::TypeIdCombat) + break; } + if (!followerOrEscorter && creatureStats2.getAiSequence().isInCombat()) + aggressive = true; } + // If any of the above conditions turned actor1 aggressive towards actor2, do an awareness check. If it passes, start combat with actor2. if (aggressive) { bool LOS = MWBase::Environment::get().getWorld()->getLOS(actor1, actor2); - - if (againstPlayer || std::find(playerFollowersAndEscorters.begin(), playerFollowersAndEscorters.end(), actor2) != playerFollowersAndEscorters.end()) - LOS &= MWBase::Environment::get().getMechanicsManager()->awarenessCheck(actor2, actor1); + LOS &= MWBase::Environment::get().getMechanicsManager()->awarenessCheck(actor2, actor1); if (LOS) - { MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); - if (!againstPlayer) // start combat between each other - { - MWBase::Environment::get().getMechanicsManager()->startCombat(actor2, actor1); - } - } } } @@ -931,7 +961,10 @@ namespace MWMechanics { static const int iCrimeThresholdMultiplier = esmStore.get().find("iCrimeThresholdMultiplier")->getInt(); if (player.getClass().getNpcStats(player).getBounty() >= cutoff * iCrimeThresholdMultiplier) + { MWBase::Environment::get().getMechanicsManager()->startCombat(ptr, player); + creatureStats.setHitAttemptActor(player); // Stops the guard from quitting combat if player is unreachable + } else creatureStats.getAiSequence().stack(AiPursue(player), ptr); creatureStats.setAlarmed(true); @@ -1054,10 +1087,25 @@ namespace MWMechanics if (iter->first == player) iter->second->getCharacterController()->setAttackingOrSpell(MWBase::Environment::get().getWorld()->getPlayer().getAttackingOrSpell()); + // If dead or no longer in combat, no longer store any actors who attempted to hit us. Also remove for the player. + if (iter->first != player && (iter->first.getClass().getCreatureStats(iter->first).isDead() + || !iter->first.getClass().getCreatureStats(iter->first).getAiSequence().isInCombat() + || !inProcessingRange)) + { + iter->first.getClass().getCreatureStats(iter->first).setHitAttemptActor(NULL); + if (player.getClass().getCreatureStats(player).getHitAttemptActor() == iter->first) + player.getClass().getCreatureStats(player).setHitAttemptActor(NULL); + } + + const MWWorld::Ptr playerHitAttemptActor = MWBase::Environment::get().getWorld()->searchPtrViaActorId(player.getClass().getCreatureStats(player).getHitAttemptActorId()); + + if (!playerHitAttemptActor.isInCell()) + player.getClass().getCreatureStats(player).setHitAttemptActorId(-1); + if (!iter->first.getClass().getCreatureStats(iter->first).isDead()) { - MWWorld::Ptr actor = iter->first; // make a copy of the map key to avoid it being invalidated when the player teleports bool cellChanged = MWBase::Environment::get().getWorld()->hasCellChanged(); + MWWorld::Ptr actor = iter->first; // make a copy of the map key to avoid it being invalidated when the player teleports updateActor(actor, duration); if (!cellChanged && MWBase::Environment::get().getWorld()->hasCellChanged()) { @@ -1470,18 +1518,36 @@ namespace MWMechanics for(PtrActorMap::iterator iter(mActors.begin());iter != mActors.end();++iter) { const MWWorld::Class &cls = iter->first.getClass(); - CreatureStats &stats = cls.getCreatureStats(iter->first); + const CreatureStats &stats = cls.getCreatureStats(iter->first); if (stats.isDead()) continue; - // An actor counts as following if AiFollow or AiEscort is the current AiPackage, or there are only Combat packages before the AiFollow/AiEscort package + // An actor counts as siding with this actor if Follow or Escort is the current AI package, or there are only Combat packages before the Follow/Escort package for (std::list::const_iterator it = stats.getAiSequence().begin(); it != stats.getAiSequence().end(); ++it) { if ((*it)->sideWithTarget() && (*it)->getTarget() == actor) + { list.push_back(iter->first); + break; + } else if ((*it)->getTypeId() != MWMechanics::AiPackage::TypeIdCombat) break; } + // Actors that are targeted by this actor's Follow or Escort packages also side with them + if (actor != getPlayer()) + { + const CreatureStats &stats2 = actor.getClass().getCreatureStats(actor); + for (std::list::const_iterator it2 = stats2.getAiSequence().begin(); it2 != stats2.getAiSequence().end(); ++it2) + { + if ((*it2)->sideWithTarget() && !(*it2)->getTarget().isEmpty()) + { + list.push_back((*it2)->getTarget()); + break; + } + else if ((*it2)->getTypeId() != MWMechanics::AiPackage::TypeIdCombat) + break; + } + } } return list; } @@ -1551,8 +1617,6 @@ namespace MWMechanics return list; } - - std::list Actors::getActorsFighting(const MWWorld::Ptr& actor) { std::list list; std::vector neighbors; diff --git a/apps/openmw/mwmechanics/aicombat.cpp b/apps/openmw/mwmechanics/aicombat.cpp index fbd3819e2..96f3ef73d 100644 --- a/apps/openmw/mwmechanics/aicombat.cpp +++ b/apps/openmw/mwmechanics/aicombat.cpp @@ -20,6 +20,7 @@ #include "combat.hpp" #include "coordinateconverter.hpp" #include "actorutil.hpp" +#include "mechanicsmanagerimp.hpp" namespace { @@ -233,9 +234,14 @@ namespace MWMechanics storage.stopAttack(); characterController.setAttackingOrSpell(false); storage.mActionCooldown = 0.f; - if (target == MWMechanics::getPlayer()) + // Continue combat if target is player or player follower/escorter and an attack has been attempted + const std::list& playerFollowersAndEscorters = MWBase::Environment::get().getMechanicsManager()->getActorsSidingWith(MWMechanics::getPlayer()); + bool targetSidesWithPlayer = (std::find(playerFollowersAndEscorters.begin(), playerFollowersAndEscorters.end(), target) != playerFollowersAndEscorters.end()); + if ((target == MWMechanics::getPlayer() || targetSidesWithPlayer) + && ((actor.getClass().getCreatureStats(actor).getHitAttemptActor() == target) + || (target.getClass().getCreatureStats(target).getHitAttemptActor() == actor))) forceFlee = true; - else + else // Otherwise end combat return true; } diff --git a/apps/openmw/mwmechanics/creaturestats.cpp b/apps/openmw/mwmechanics/creaturestats.cpp index be51cea25..1fce2f8f8 100644 --- a/apps/openmw/mwmechanics/creaturestats.cpp +++ b/apps/openmw/mwmechanics/creaturestats.cpp @@ -371,6 +371,16 @@ namespace MWMechanics return mLastHitAttemptObject; } + void CreatureStats::setHitAttemptActor(const MWWorld::Ptr& actor) + { + mHitAttemptActor = actor; + } + + const MWWorld::Ptr &CreatureStats::getHitAttemptActor() const + { + return mHitAttemptActor; + } + void CreatureStats::addToFallHeight(float height) { mFallHeight += height; diff --git a/apps/openmw/mwmechanics/creaturestats.hpp b/apps/openmw/mwmechanics/creaturestats.hpp index 171857603..6e47a9278 100644 --- a/apps/openmw/mwmechanics/creaturestats.hpp +++ b/apps/openmw/mwmechanics/creaturestats.hpp @@ -53,6 +53,9 @@ namespace MWMechanics std::string mLastHitObject; // The last object to hit this actor std::string mLastHitAttemptObject; // The last object to attempt to hit this actor + MWWorld::Ptr mHitAttemptActor; // Stores an actor that attacked this actor. Only one is stored at a time, + // and it is not changed if a different actor attacks. It is cleared when combat ends. + bool mRecalcMagicka; // For merchants: the last time items were restocked and gold pool refilled. @@ -241,9 +244,11 @@ namespace MWMechanics bool getStance (Stance flag) const; void setLastHitObject(const std::string &objectid); - void setLastHitAttemptObject(const std::string &objectid); const std::string &getLastHitObject() const; + void setLastHitAttemptObject(const std::string &objectid); const std::string &getLastHitAttemptObject() const; + void setHitAttemptActor(const MWWorld::Ptr &actor); + const MWWorld::Ptr &getHitAttemptActor() const; // Note, this is just a cache to avoid checking the whole container store every frame. We don't need to store it in saves. // TODO: Put it somewhere else? diff --git a/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp b/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp index ad1ca9bb9..c04fcefae 100644 --- a/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp +++ b/apps/openmw/mwmechanics/mechanicsmanagerimp.cpp @@ -1214,48 +1214,51 @@ namespace MWMechanics } } - bool MechanicsManager::actorAttacked(const MWWorld::Ptr &ptr, const MWWorld::Ptr &attacker) + bool MechanicsManager::actorAttacked(const MWWorld::Ptr &target, const MWWorld::Ptr &attacker) { - if (ptr == getPlayer()) + std::list followersAttacker = getActorsSidingWith(attacker); + std::list followersTarget = getActorsSidingWith(target); + + MWMechanics::CreatureStats& statsTarget = target.getClass().getCreatureStats(target); + + if (target == getPlayer()) return false; - std::list followers = getActorsSidingWith(attacker); - MWMechanics::CreatureStats& targetStats = ptr.getClass().getCreatureStats(ptr); - if (std::find(followers.begin(), followers.end(), ptr) != followers.end()) + if (std::find(followersAttacker.begin(), followersAttacker.end(), target) != followersAttacker.end()) { - targetStats.friendlyHit(); + statsTarget.friendlyHit(); - if (targetStats.getFriendlyHits() < 4) + if (statsTarget.getFriendlyHits() < 4) { - MWBase::Environment::get().getDialogueManager()->say(ptr, "hit"); + MWBase::Environment::get().getDialogueManager()->say(target, "hit"); return false; } } // Attacking an NPC that is already in combat with any other NPC is not a crime - AiSequence& seq = targetStats.getAiSequence(); + AiSequence& seq = statsTarget.getAiSequence(); bool isFightingNpc = false; for (std::list::const_iterator it = seq.begin(); it != seq.end(); ++it) { if ((*it)->getTypeId() == AiPackage::TypeIdCombat) { - MWWorld::Ptr target = (*it)->getTarget(); - if (!target.isEmpty() && target.getClass().isNpc()) + MWWorld::Ptr target2 = (*it)->getTarget(); + if (!target2.isEmpty() && target2.getClass().isNpc()) isFightingNpc = true; } } - if (ptr.getClass().isNpc() && !attacker.isEmpty() && !seq.isInCombat(attacker) - && !isAggressive(ptr, attacker) && !isFightingNpc) - commitCrime(attacker, ptr, MWBase::MechanicsManager::OT_Assault); + if (target.getClass().isNpc() && !attacker.isEmpty() && !seq.isInCombat(attacker) + && !isAggressive(target, attacker) && !isFightingNpc) + commitCrime(attacker, target, MWBase::MechanicsManager::OT_Assault); - if (!attacker.isEmpty() && (attacker.getClass().getCreatureStats(attacker).getAiSequence().isInCombat(ptr) + if (!attacker.isEmpty() && (attacker.getClass().getCreatureStats(attacker).getAiSequence().isInCombat(target) || attacker == getPlayer()) && !seq.isInCombat(attacker)) { // Attacker is in combat with us, but we are not in combat with the attacker yet. Time to fight back. // Note: accidental or collateral damage attacks are ignored. - startCombat(ptr, attacker); + startCombat(target, attacker); } return true; @@ -1365,6 +1368,7 @@ namespace MWMechanics // if guard starts combat with player, guards pursuing player should do the same if (ptr.getClass().isClass(ptr, "Guard")) { + ptr.getClass().getCreatureStats(ptr).setHitAttemptActor(target); // Stops guard from ending combat if player is unreachable for (Actors::PtrActorMap::const_iterator iter = mActors.begin(); iter != mActors.end(); ++iter) { if (iter->first.getClass().isClass(iter->first, "Guard")) @@ -1374,6 +1378,7 @@ namespace MWMechanics { aiSeq.stopPursuit(); aiSeq.stack(MWMechanics::AiCombat(target), ptr); + iter->first.getClass().getCreatureStats(iter->first).setHitAttemptActor(target); // Stops guard from ending combat if player is unreachable } } } diff --git a/files/settings-default.cfg b/files/settings-default.cfg index 770a9674d..91329b8ca 100644 --- a/files/settings-default.cfg +++ b/files/settings-default.cfg @@ -153,6 +153,10 @@ show effect duration = false # Prevents merchants from equipping items that are sold to them. prevent merchant equipping = false +# Makes player followers and escorters start combat with enemies who have started combat with them +# or the player. Otherwise they wait for the enemies or the player to do an attack first. +followers attack on sight = false + [General] # Anisotropy reduces distortion in textures at low angles (e.g. 0 to 16).