mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-21 06:53:53 +00:00
AI packages have priority and combat is triggered in actor.cpp using the Hostile setting in CreaturesState
This commit is contained in:
parent
683b8d77dd
commit
4559e932ae
6 changed files with 135 additions and 123 deletions
|
@ -22,6 +22,11 @@
|
|||
#include "creaturestats.hpp"
|
||||
#include "movement.hpp"
|
||||
|
||||
#include "../mwbase/environment.hpp"
|
||||
#include "../mwbase/mechanicsmanager.hpp"
|
||||
|
||||
#include "aicombat.hpp"
|
||||
|
||||
namespace MWMechanics
|
||||
{
|
||||
void Actors::updateActor (const MWWorld::Ptr& ptr, float duration)
|
||||
|
@ -35,6 +40,39 @@ namespace MWMechanics
|
|||
{
|
||||
// AI
|
||||
CreatureStats& creatureStats = MWWorld::Class::get (ptr).getCreatureStats (ptr);
|
||||
|
||||
//engage combat or not?
|
||||
if(ptr != MWBase::Environment::get().getWorld()->getPlayer().getPlayer() && !creatureStats.isHostile())
|
||||
{
|
||||
ESM::Position playerpos = MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getRefData().getPosition();
|
||||
ESM::Position actorpos = ptr.getRefData().getPosition();
|
||||
float d = sqrt((actorpos.pos[0] - playerpos.pos[0])*(actorpos.pos[0] - playerpos.pos[0])
|
||||
+(actorpos.pos[1] - playerpos.pos[1])*(actorpos.pos[1] - playerpos.pos[1])
|
||||
+(actorpos.pos[2] - playerpos.pos[2])*(actorpos.pos[2] - playerpos.pos[2]));
|
||||
float fight = ptr.getClass().getCreatureStats(ptr).getAiSetting(1);
|
||||
float disp = 100; //creatures don't have disposition, so set it to 100 by default
|
||||
if(ptr.getTypeName() == typeid(ESM::NPC).name())
|
||||
{
|
||||
disp = MWBase::Environment::get().getMechanicsManager()->getDerivedDisposition(ptr);
|
||||
}
|
||||
bool LOS = MWBase::Environment::get().getWorld()->getLOS(ptr,MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
|
||||
if( ( (fight == 100 )
|
||||
|| (fight >= 95 && d <= 3000)
|
||||
|| (fight >= 90 && d <= 2000)
|
||||
|| (fight >= 80 && d <= 1000)
|
||||
|| (fight >= 80 && disp <= 40)
|
||||
|| (fight >= 70 && disp <= 35 && d <= 1000)
|
||||
|| (fight >= 60 && disp <= 30 && d <= 1000)
|
||||
|| (fight >= 50 && disp == 0)
|
||||
|| (fight >= 40 && disp <= 10 && d <= 500) )
|
||||
&& LOS
|
||||
)
|
||||
{
|
||||
creatureStats.getAiSequence().stack(AiCombat("player"));
|
||||
creatureStats.setHostile(true);
|
||||
}
|
||||
}
|
||||
|
||||
creatureStats.getAiSequence().execute (ptr,duration);
|
||||
|
||||
// fatigue restoration
|
||||
|
|
|
@ -34,18 +34,21 @@ namespace MWMechanics
|
|||
|
||||
bool AiCombat::execute (const MWWorld::Ptr& actor,float duration)
|
||||
{
|
||||
if(!MWWorld::Class::get(actor).getCreatureStats(actor).isHostile()) return true;
|
||||
|
||||
const MWWorld::Ptr target = MWBase::Environment::get().getWorld()->getPtr(mTargetId, false);
|
||||
|
||||
if(MWWorld::Class::get(actor).getCreatureStats(actor).getHealth().getCurrent() <= 0) return true;
|
||||
|
||||
if(actor.getTypeName() == typeid(ESM::NPC).name())
|
||||
{
|
||||
MWWorld::Class::get(actor).
|
||||
MWWorld::Class::get(actor).setStance(actor, MWWorld::Class::Run,true);
|
||||
MWMechanics::DrawState_ state = MWWorld::Class::get(actor).getNpcStats(actor).getDrawState();
|
||||
if (state == MWMechanics::DrawState_Spell || state == MWMechanics::DrawState_Nothing)
|
||||
MWWorld::Class::get(actor).getNpcStats(actor).setDrawState(MWMechanics::DrawState_Weapon);
|
||||
//MWWorld::Class::get(actor).getCreatureStats(actor).setAttackingOrSpell(true);
|
||||
|
||||
}
|
||||
ESM::Position pos = actor.getRefData().getPosition();
|
||||
const ESM::Pathgrid *pathgrid =
|
||||
MWBase::Environment::get().getWorld()->getStore().get<ESM::Pathgrid>().search(*actor.getCell()->mCell);
|
||||
|
@ -124,13 +127,17 @@ namespace MWMechanics
|
|||
MWWorld::Class::get(actor).getMovementSettings(actor).mPosition[1] = 0;
|
||||
//MWWorld::Class::get(actor).getCreatureStats(actor).setAttackingOrSpell(!MWWorld::Class::get(actor).getCreatureStats(actor).getAttackingOrSpell());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int AiCombat::getTypeId() const
|
||||
{
|
||||
return 2;
|
||||
return 5;
|
||||
}
|
||||
|
||||
unsigned int AiCombat::getPriority() const
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
AiCombat *MWMechanics::AiCombat::clone() const
|
||||
|
|
|
@ -21,6 +21,8 @@ namespace MWMechanics
|
|||
|
||||
virtual int getTypeId() const;
|
||||
|
||||
virtual unsigned int getPriority() const;
|
||||
|
||||
private:
|
||||
std::string mTargetId;
|
||||
|
||||
|
|
|
@ -22,6 +22,9 @@ namespace MWMechanics
|
|||
|
||||
virtual int getTypeId() const = 0;
|
||||
///< 0: Wanter, 1 Travel, 2 Escort, 3 Follow, 4 Activate
|
||||
|
||||
virtual unsigned int getPriority() const {return 0;}
|
||||
///< higher number is higher priority (0 beeing the lowest)
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -17,19 +17,14 @@
|
|||
#include "../mwbase/world.hpp"
|
||||
#include "../mwworld/player.hpp"
|
||||
|
||||
#include "../mwbase/mechanicsmanager.hpp"
|
||||
|
||||
void MWMechanics::AiSequence::copy (const AiSequence& sequence)
|
||||
{
|
||||
for (std::list<AiPackage *>::const_iterator iter (sequence.mPackages.begin());
|
||||
iter!=sequence.mPackages.end(); ++iter)
|
||||
mPackages.push_back ((*iter)->clone());
|
||||
mCombat = sequence.mCombat;
|
||||
mCombatPackage = 0;
|
||||
if(sequence.mCombat) mCombatPackage = sequence.mCombatPackage->clone();
|
||||
}
|
||||
|
||||
MWMechanics::AiSequence::AiSequence() : mDone (false), mCombat (false), mCombatPackage (0) {}
|
||||
MWMechanics::AiSequence::AiSequence() : mDone (false) {}
|
||||
|
||||
MWMechanics::AiSequence::AiSequence (const AiSequence& sequence) : mDone (false)
|
||||
{
|
||||
|
@ -69,38 +64,6 @@ void MWMechanics::AiSequence::execute (const MWWorld::Ptr& actor,float duration)
|
|||
{
|
||||
if(actor != MWBase::Environment::get().getWorld()->getPlayer().getPlayer())
|
||||
{
|
||||
if(mCombat)
|
||||
{
|
||||
mCombatPackage->execute(actor,duration);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(actor.getTypeName() == typeid(ESM::NPC).name())
|
||||
{
|
||||
ESM::Position playerpos = MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getRefData().getPosition();
|
||||
ESM::Position actorpos = actor.getRefData().getPosition();
|
||||
float d = sqrt((actorpos.pos[0] - playerpos.pos[0])*(actorpos.pos[0] - playerpos.pos[0])
|
||||
+(actorpos.pos[1] - playerpos.pos[1])*(actorpos.pos[1] - playerpos.pos[1])
|
||||
+(actorpos.pos[2] - playerpos.pos[2])*(actorpos.pos[2] - playerpos.pos[2]));
|
||||
float fight = actor.getClass().getCreatureStats(actor).getAiSetting(1);
|
||||
float disp = MWBase::Environment::get().getMechanicsManager()->getDerivedDisposition(actor);
|
||||
bool LOS = MWBase::Environment::get().getWorld()->getLOS(actor,MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
|
||||
if( ( (fight == 100 )
|
||||
|| (fight >= 95 && d <= 3000)
|
||||
|| (fight >= 90 && d <= 2000)
|
||||
|| (fight >= 80 && d <= 1000)
|
||||
|| (fight >= 80 && disp <= 40)
|
||||
|| (fight >= 70 && disp <= 35 && d <= 1000)
|
||||
|| (fight >= 60 && disp <= 30 && d <= 1000)
|
||||
|| (fight >= 50 && disp == 0)
|
||||
|| (fight >= 40 && disp <= 10 && d <= 500) )
|
||||
&& LOS
|
||||
)
|
||||
{
|
||||
mCombat = true;
|
||||
mCombatPackage = new AiCombat("player");
|
||||
}
|
||||
}
|
||||
if (!mPackages.empty())
|
||||
{
|
||||
if (mPackages.front()->execute (actor,duration))
|
||||
|
@ -112,7 +75,6 @@ void MWMechanics::AiSequence::execute (const MWWorld::Ptr& actor,float duration)
|
|||
mDone = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MWMechanics::AiSequence::clear()
|
||||
|
@ -120,16 +82,18 @@ void MWMechanics::AiSequence::clear()
|
|||
for (std::list<AiPackage *>::const_iterator iter (mPackages.begin()); iter!=mPackages.end(); ++iter)
|
||||
delete *iter;
|
||||
|
||||
if(mCombatPackage)
|
||||
{
|
||||
delete mCombatPackage;
|
||||
mCombatPackage = 0;
|
||||
}
|
||||
mPackages.clear();
|
||||
}
|
||||
|
||||
void MWMechanics::AiSequence::stack (const AiPackage& package)
|
||||
{
|
||||
for(std::list<AiPackage *>::iterator it = mPackages.begin(); it != mPackages.end(); it++)
|
||||
{
|
||||
if(mPackages.front()->getPriority() <= package.getPriority())
|
||||
mPackages.insert(it,package.clone());
|
||||
}
|
||||
|
||||
if(mPackages.empty())
|
||||
mPackages.push_front (package.clone());
|
||||
}
|
||||
|
||||
|
|
|
@ -18,8 +18,6 @@ namespace MWMechanics
|
|||
class AiSequence
|
||||
{
|
||||
std::list<AiPackage *> mPackages;
|
||||
AiPackage* mCombatPackage;
|
||||
bool mCombat;
|
||||
|
||||
bool mDone;
|
||||
|
||||
|
@ -36,7 +34,7 @@ namespace MWMechanics
|
|||
virtual ~AiSequence();
|
||||
|
||||
int getTypeId() const;
|
||||
///< -1: None, 0: Wanter, 1 Travel, 2 Escort, 3 Follow, 4 Activate
|
||||
///< -1: None, 0: Wanter, 1 Travel, 2 Escort, 3 Follow, 4 Activate, 5 Combat
|
||||
|
||||
bool isPackageDone() const;
|
||||
///< Has a package been completed during the last update?
|
||||
|
|
Loading…
Reference in a new issue