1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-04-01 13:06:40 +00:00

Restructure function engageCombat

This commit is contained in:
ζeh Matt 2022-03-28 16:32:56 +03:00
parent 8631b96680
commit 0b306bc1ea
No known key found for this signature in database
GPG key ID: 18CE582C71A225B0

View file

@ -432,8 +432,9 @@ namespace MWMechanics
void Actors::turnActorToFacePlayer(const MWWorld::Ptr& actor, Actor& actorState, const osg::Vec3f& dir) void Actors::turnActorToFacePlayer(const MWWorld::Ptr& actor, Actor& actorState, const osg::Vec3f& dir)
{ {
actor.getClass().getMovementSettings(actor).mPosition[1] = 0; auto& movementSettings = actor.getClass().getMovementSettings();
actor.getClass().getMovementSettings(actor).mPosition[0] = 0; movementSettings.mPosition[1] = 0;
movementSettings.mPosition[0] = 0;
if (!actorState.isTurningToPlayer()) if (!actorState.isTurningToPlayer())
{ {
@ -464,7 +465,7 @@ namespace MWMechanics
} }
} }
void Actors::engageCombat (const MWWorld::Ptr& actor1, const MWWorld::Ptr& actor2, std::map<const MWWorld::Ptr, const std::set<MWWorld::Ptr> >& cachedAllies, bool againstPlayer) void Actors::engageCombat(const MWWorld::Ptr& actor1, const MWWorld::Ptr& actor2, std::map<const MWWorld::Ptr, const std::set<MWWorld::Ptr> >& cachedAllies, bool againstPlayer)
{ {
// No combat for totally static creatures // No combat for totally static creatures
if (!actor1.getClass().isMobile(actor1)) if (!actor1.getClass().isMobile(actor1))
@ -480,9 +481,9 @@ namespace MWMechanics
const osg::Vec3f actor1Pos(actor1.getRefData().getPosition().asVec3()); const osg::Vec3f actor1Pos(actor1.getRefData().getPosition().asVec3());
const osg::Vec3f actor2Pos(actor2.getRefData().getPosition().asVec3()); const osg::Vec3f actor2Pos(actor2.getRefData().getPosition().asVec3());
float sqrDist = (actor1Pos - actor2Pos).length2(); const float sqrDist = (actor1Pos - actor2Pos).length2();
if (sqrDist > mActorsProcessingRange*mActorsProcessingRange) if (sqrDist > mActorsProcessingRange * mActorsProcessingRange)
return; return;
// If this is set to true, actor1 will start combat with actor2 if the awareness check at the end of the method returns true // If this is set to true, actor1 will start combat with actor2 if the awareness check at the end of the method returns true
@ -494,15 +495,16 @@ namespace MWMechanics
getActorsSidingWith(actor1, allies1, cachedAllies); getActorsSidingWith(actor1, allies1, cachedAllies);
auto* mechanicsManager = MWBase::Environment::get().getMechanicsManager();
// If an ally of actor1 has been attacked by actor2 or has attacked actor2, start combat between actor1 and actor2 // If an ally of actor1 has been attacked by actor2 or has attacked actor2, start combat between actor1 and actor2
for (const MWWorld::Ptr &ally : allies1) for (const MWWorld::Ptr& ally : allies1)
{ {
if (creatureStats1.getAiSequence().isInCombat(ally)) if (creatureStats1.getAiSequence().isInCombat(ally))
continue; continue;
if (creatureStats2.matchesActorId(ally.getClass().getCreatureStats(ally).getHitAttemptActorId())) if (creatureStats2.matchesActorId(ally.getClass().getCreatureStats(ally).getHitAttemptActorId()))
{ {
MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); mechanicsManager->startCombat(actor1, actor2);
// Also set the same hit attempt actor. Otherwise, if fighting the player, they may stop combat // 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 // if the player gets out of reach, while the ally would continue combat with the player
creatureStats1.setHitAttemptActorId(ally.getClass().getCreatureStats(ally).getHitAttemptActorId()); creatureStats1.setHitAttemptActorId(ally.getClass().getCreatureStats(ally).getHitAttemptActorId());
@ -524,21 +526,21 @@ namespace MWMechanics
if (!aggressive && !isPlayerFollowerOrEscorter) if (!aggressive && !isPlayerFollowerOrEscorter)
{ {
// Check that actor2 is in combat with actor1 // Check that actor2 is in combat with actor1
if (actor2.getClass().getCreatureStats(actor2).getAiSequence().isInCombat(actor1)) if (creatureStats2.getAiSequence().isInCombat(actor1))
{ {
std::set<MWWorld::Ptr> allies2; std::set<MWWorld::Ptr> allies2;
getActorsSidingWith(actor2, allies2, cachedAllies); getActorsSidingWith(actor2, allies2, cachedAllies);
// Check that an ally of actor2 is also in combat with actor1 // Check that an ally of actor2 is also in combat with actor1
for (const MWWorld::Ptr &ally2 : allies2) for (const MWWorld::Ptr& ally2 : allies2)
{ {
if (ally2.getClass().getCreatureStats(ally2).getAiSequence().isInCombat(actor1)) if (ally2.getClass().getCreatureStats(ally2).getAiSequence().isInCombat(actor1))
{ {
MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); mechanicsManager->startCombat(actor1, actor2);
// Also have actor1's allies start combat // Also have actor1's allies start combat
for (const MWWorld::Ptr& ally1 : allies1) for (const MWWorld::Ptr& ally1 : allies1)
MWBase::Environment::get().getMechanicsManager()->startCombat(ally1, actor2); mechanicsManager->startCombat(ally1, actor2);
return; return;
} }
} }
@ -553,13 +555,13 @@ namespace MWMechanics
static const bool followersAttackOnSight = Settings::Manager::getBool("followers attack on sight", "Game"); static const bool followersAttackOnSight = Settings::Manager::getBool("followers attack on sight", "Game");
if (!aggressive && isPlayerFollowerOrEscorter && followersAttackOnSight) if (!aggressive && isPlayerFollowerOrEscorter && followersAttackOnSight)
{ {
if (actor2.getClass().getCreatureStats(actor2).getAiSequence().isInCombat(actor1)) if (creatureStats2.getAiSequence().isInCombat(actor1))
aggressive = true; aggressive = true;
else else
{ {
for (const MWWorld::Ptr &ally : allies1) for (const MWWorld::Ptr& ally : allies1)
{ {
if (actor2.getClass().getCreatureStats(actor2).getAiSequence().isInCombat(ally)) if (creatureStats2.getAiSequence().isInCombat(ally))
{ {
aggressive = true; aggressive = true;
break; break;
@ -576,15 +578,16 @@ namespace MWMechanics
// Player followers and escorters with high fight should not initiate combat with the player or with // Player followers and escorters with high fight should not initiate combat with the player or with
// other player followers or escorters // other player followers or escorters
if (!isPlayerFollowerOrEscorter) if (!isPlayerFollowerOrEscorter)
aggressive = MWBase::Environment::get().getMechanicsManager()->isAggressive(actor1, actor2); aggressive = mechanicsManager->isAggressive(actor1, actor2);
} }
} }
// Make guards go aggressive with creatures that are in combat, unless the creature is a follower or escorter // Make guards go aggressive with creatures that are in combat, unless the creature is a follower or escorter
auto* world = MWBase::Environment::get().getWorld();
if (!aggressive && actor1.getClass().isClass(actor1, "Guard") && !actor2.getClass().isNpc() && creatureStats2.getAiSequence().isInCombat()) if (!aggressive && actor1.getClass().isClass(actor1, "Guard") && !actor2.getClass().isNpc() && creatureStats2.getAiSequence().isInCombat())
{ {
// Check if the creature is too far // Check if the creature is too far
static const float fAlarmRadius = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("fAlarmRadius")->mValue.getFloat(); static const float fAlarmRadius = world->getStore().get<ESM::GameSetting>().find("fAlarmRadius")->mValue.getFloat();
if (sqrDist > fAlarmRadius * fAlarmRadius) if (sqrDist > fAlarmRadius * fAlarmRadius)
return; return;
@ -607,11 +610,11 @@ namespace MWMechanics
// If any of the above conditions turned actor1 aggressive towards actor2, do an awareness check. If it passes, start combat with actor2. // If any of the above conditions turned actor1 aggressive towards actor2, do an awareness check. If it passes, start combat with actor2.
if (aggressive) if (aggressive)
{ {
bool LOS = MWBase::Environment::get().getWorld()->getLOS(actor1, actor2) bool LOS = world->getLOS(actor1, actor2) &&
&& MWBase::Environment::get().getMechanicsManager()->awarenessCheck(actor2, actor1); mechanicsManager->awarenessCheck(actor2, actor1);
if (LOS) if (LOS)
MWBase::Environment::get().getMechanicsManager()->startCombat(actor1, actor2); mechanicsManager->startCombat(actor1, actor2);
} }
} }