2013-09-25 16:01:36 +00:00
|
|
|
#include "aicombat.hpp"
|
|
|
|
|
2014-01-29 19:29:07 +00:00
|
|
|
#include <OgreMath.h>
|
|
|
|
#include <OgreVector3.h>
|
|
|
|
|
2013-09-25 16:01:36 +00:00
|
|
|
|
2013-10-31 08:43:12 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
#include "../mwworld/timestamp.hpp"
|
2014-01-29 19:29:07 +00:00
|
|
|
#include "../mwworld/inventorystore.hpp"
|
2014-02-23 19:11:05 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
|
|
|
#include "../mwworld/cellstore.hpp"
|
2014-01-15 20:56:55 +00:00
|
|
|
|
2013-10-31 08:43:12 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2014-01-15 20:56:55 +00:00
|
|
|
#include "../mwbase/dialoguemanager.hpp"
|
|
|
|
|
2013-09-25 16:01:36 +00:00
|
|
|
|
2014-02-16 17:48:03 +00:00
|
|
|
#include "creaturestats.hpp"
|
2014-01-29 19:29:07 +00:00
|
|
|
#include "steering.hpp"
|
|
|
|
#include "movement.hpp"
|
|
|
|
#include "character.hpp" // fixme: for getActiveWeapon
|
2013-10-27 13:03:58 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2014-01-07 15:37:51 +00:00
|
|
|
static float sgn(Ogre::Radian a)
|
2013-10-27 13:03:58 +00:00
|
|
|
{
|
2014-01-07 15:37:51 +00:00
|
|
|
if(a.valueDegrees() > 0)
|
2013-10-27 13:03:58 +00:00
|
|
|
return 1.0;
|
|
|
|
return -1.0;
|
|
|
|
}
|
2014-01-17 18:33:49 +00:00
|
|
|
|
|
|
|
//chooses an attack depending on probability to avoid uniformity
|
|
|
|
void chooseBestAttack(const ESM::Weapon* weapon, MWMechanics::Movement &movement);
|
2013-10-27 13:03:58 +00:00
|
|
|
}
|
2013-09-25 16:01:36 +00:00
|
|
|
|
|
|
|
namespace MWMechanics
|
|
|
|
{
|
2014-04-19 22:31:02 +00:00
|
|
|
static const float DOOR_CHECK_INTERVAL = 1.5f; // same as AiWander
|
2014-04-18 22:16:56 +00:00
|
|
|
// NOTE: MIN_DIST_TO_DOOR_SQUARED is defined in obstacle.hpp
|
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
AiCombat::AiCombat(const MWWorld::Ptr& actor) :
|
2014-02-21 10:35:46 +00:00
|
|
|
mTarget(actor),
|
|
|
|
mTimerAttack(0),
|
|
|
|
mTimerReact(0),
|
2014-01-15 20:56:55 +00:00
|
|
|
mTimerCombatMove(0),
|
2014-01-19 20:09:51 +00:00
|
|
|
mFollowTarget(false),
|
2014-01-15 20:56:55 +00:00
|
|
|
mReadyToAttack(false),
|
|
|
|
mStrike(false),
|
|
|
|
mCombatMove(false),
|
2014-04-18 22:16:56 +00:00
|
|
|
mBackOffDoor(false),
|
2014-01-29 19:29:07 +00:00
|
|
|
mRotate(false),
|
|
|
|
mMovement(),
|
2014-04-19 22:31:02 +00:00
|
|
|
mCell(NULL),
|
2014-04-18 22:16:56 +00:00
|
|
|
mDoorIter(actor.getCell()->get<ESM::Door>().mList.end()),
|
2014-04-19 22:31:02 +00:00
|
|
|
mDoors(actor.getCell()->get<ESM::Door>()),
|
2014-04-18 22:16:56 +00:00
|
|
|
mDoorCheckDuration(0),
|
2014-01-29 19:29:07 +00:00
|
|
|
mTargetAngle(0)
|
2013-09-25 16:01:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* Current AiCombat movement states (as of 0.29.0), ignoring the details of the
|
|
|
|
* attack states such as CombatMove, Strike and ReadyToAttack:
|
|
|
|
*
|
2014-04-19 22:31:02 +00:00
|
|
|
* +----(within strike range)----->attack--(beyond strike range)-->follow
|
|
|
|
* | | ^ | |
|
|
|
|
* | | | | |
|
|
|
|
* pursue<---(beyond follow range)-----+ +----(within strike range)---+ |
|
|
|
|
* ^ |
|
|
|
|
* | |
|
|
|
|
* +-------------------------(beyond follow range)--------------------+
|
2014-04-18 22:16:56 +00:00
|
|
|
*
|
|
|
|
*
|
2014-04-19 22:31:02 +00:00
|
|
|
* Below diagram is high level only, the code detail is a little different
|
|
|
|
* (but including those detail will just complicate the diagram w/o adding much)
|
2014-04-18 22:16:56 +00:00
|
|
|
*
|
2014-04-19 22:31:02 +00:00
|
|
|
* +----------(same)-------------->attack---------(same)---------->follow
|
|
|
|
* | |^^ |||
|
|
|
|
* | ||| |||
|
|
|
|
* | +--(same)-----------------+|+----------(same)------------+||
|
|
|
|
* | | | ||
|
|
|
|
* | | | (in range) ||
|
|
|
|
* | <---+ (too far) | ||
|
|
|
|
* pursue<-------------------------[door open]<-----+ ||
|
|
|
|
* ^^^ | ||
|
|
|
|
* ||| | ||
|
|
|
|
* ||+----------evade-----+ | ||
|
|
|
|
* || | [closed door] | ||
|
|
|
|
* |+----> maybe stuck, check --------------> back up, check door ||
|
|
|
|
* | ^ | ^ | ^ ||
|
|
|
|
* | | | | | | ||
|
|
|
|
* | | +---+ +---+ ||
|
|
|
|
* | +-------------------------------------------------------+|
|
|
|
|
* | |
|
|
|
|
* +---------------------------(same)---------------------------------+
|
2014-04-18 22:16:56 +00:00
|
|
|
*
|
|
|
|
* FIXME:
|
|
|
|
*
|
|
|
|
* The new scheme is way too complicated, should really be implemented as a
|
|
|
|
* proper state machine.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
*
|
|
|
|
* Use the Observer Pattern to co-ordinate attacks, provide intelligence on
|
|
|
|
* whether the target was hit, etc.
|
|
|
|
*/
|
2013-10-30 19:42:50 +00:00
|
|
|
bool AiCombat::execute (const MWWorld::Ptr& actor,float duration)
|
2013-09-25 16:01:36 +00:00
|
|
|
{
|
2014-01-15 20:56:55 +00:00
|
|
|
//General description
|
2014-01-27 20:38:01 +00:00
|
|
|
if(!actor.getClass().getCreatureStats(actor).isHostile()
|
|
|
|
|| actor.getClass().getCreatureStats(actor).getHealth().getCurrent() <= 0)
|
2014-01-15 20:56:55 +00:00
|
|
|
return true;
|
2013-09-25 16:01:36 +00:00
|
|
|
|
2014-01-27 20:38:01 +00:00
|
|
|
if(mTarget.getClass().getCreatureStats(mTarget).isDead())
|
2014-01-15 20:56:55 +00:00
|
|
|
return true;
|
2013-11-18 11:33:09 +00:00
|
|
|
|
2014-01-16 20:24:05 +00:00
|
|
|
//Update every frame
|
2014-01-15 20:56:55 +00:00
|
|
|
if(mCombatMove)
|
|
|
|
{
|
|
|
|
mTimerCombatMove -= duration;
|
|
|
|
if( mTimerCombatMove <= 0)
|
|
|
|
{
|
|
|
|
mTimerCombatMove = 0;
|
|
|
|
mMovement.mPosition[1] = mMovement.mPosition[0] = 0;
|
|
|
|
mCombatMove = false;
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 19:29:07 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
actor.getClass().getMovementSettings(actor) = mMovement;
|
2014-01-29 19:29:07 +00:00
|
|
|
|
|
|
|
if (mRotate)
|
|
|
|
{
|
|
|
|
if (zTurn(actor, Ogre::Degree(mTargetAngle)))
|
|
|
|
mRotate = false;
|
|
|
|
}
|
2014-02-21 10:35:46 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
mTimerAttack -= duration;
|
|
|
|
actor.getClass().getCreatureStats(actor).setAttackingOrSpell(mStrike);
|
2013-10-28 17:22:36 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
float tReaction = 0.25f;
|
|
|
|
if(mTimerReact < tReaction)
|
|
|
|
{
|
|
|
|
mTimerReact += duration;
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-16 20:24:05 +00:00
|
|
|
//Update with period = tReaction
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-16 20:24:05 +00:00
|
|
|
mTimerReact = 0;
|
2014-01-15 20:56:55 +00:00
|
|
|
|
2014-04-19 22:31:02 +00:00
|
|
|
bool cellChange = mCell && (actor.getCell() != mCell);
|
|
|
|
if(!mCell || cellChange)
|
|
|
|
{
|
|
|
|
mCell = actor.getCell();
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
//actual attacking logic
|
|
|
|
//TODO: Some skills affect period of strikes.For berserk-like style period ~ 0.25f
|
|
|
|
float attackPeriod = 1.0f;
|
|
|
|
if(mReadyToAttack)
|
|
|
|
{
|
|
|
|
if(mTimerAttack <= -attackPeriod)
|
|
|
|
{
|
|
|
|
//TODO: should depend on time between 'start' to 'min attack'
|
|
|
|
//for better controlling of NPCs' attack strength.
|
|
|
|
//Also it seems that this time is different for slash/thrust/chop
|
|
|
|
mTimerAttack = 0.35f * static_cast<float>(rand())/RAND_MAX;
|
|
|
|
mStrike = true;
|
2014-02-21 10:35:46 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
//say a provoking combat phrase
|
|
|
|
if (actor.getClass().isNpc())
|
|
|
|
{
|
|
|
|
const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore();
|
|
|
|
int chance = store.get<ESM::GameSetting>().find("iVoiceAttackOdds")->getInt();
|
|
|
|
int roll = std::rand()/ (static_cast<double> (RAND_MAX) + 1) * 100; // [0, 99]
|
|
|
|
if (roll < chance)
|
|
|
|
{
|
|
|
|
MWBase::Environment::get().getDialogueManager()->say(actor, "attack");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mTimerAttack <= 0)
|
|
|
|
mStrike = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mTimerAttack = -attackPeriod;
|
|
|
|
mStrike = false;
|
|
|
|
}
|
2014-02-21 10:35:46 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
const MWWorld::Class &cls = actor.getClass();
|
|
|
|
const ESM::Weapon *weapon = NULL;
|
|
|
|
MWMechanics::WeaponType weaptype;
|
|
|
|
float weapRange, weapSpeed = 1.0f;
|
2014-01-16 20:24:05 +00:00
|
|
|
|
|
|
|
actor.getClass().getCreatureStats(actor).setMovementFlag(CreatureStats::Flag_Run, true);
|
|
|
|
|
2014-01-25 23:16:56 +00:00
|
|
|
if (actor.getClass().hasInventoryStore(actor))
|
2013-09-25 16:01:36 +00:00
|
|
|
{
|
2014-01-19 12:31:17 +00:00
|
|
|
MWMechanics::DrawState_ state = actor.getClass().getCreatureStats(actor).getDrawState();
|
2013-09-28 10:25:37 +00:00
|
|
|
if (state == MWMechanics::DrawState_Spell || state == MWMechanics::DrawState_Nothing)
|
2014-02-16 17:48:03 +00:00
|
|
|
actor.getClass().getCreatureStats(actor).setDrawState(MWMechanics::DrawState_Weapon);
|
2014-01-15 20:56:55 +00:00
|
|
|
|
|
|
|
//Get weapon speed and range
|
2014-02-21 10:35:46 +00:00
|
|
|
MWWorld::ContainerStoreIterator weaponSlot =
|
2014-02-16 17:48:03 +00:00
|
|
|
MWMechanics::getActiveWeapon(cls.getCreatureStats(actor), cls.getInventoryStore(actor), &weaptype);
|
2014-03-04 08:34:38 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
if (weaptype == WeapType_HandToHand)
|
|
|
|
{
|
2014-02-21 10:35:46 +00:00
|
|
|
const MWWorld::Store<ESM::GameSetting> &gmst =
|
2014-01-15 20:56:55 +00:00
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
|
|
|
|
weapRange = gmst.find("fHandToHandReach")->getFloat();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
weapon = weaponSlot->get<ESM::Weapon>()->mBase;
|
|
|
|
weapRange = weapon->mData.mReach;
|
|
|
|
weapSpeed = weapon->mData.mSpeed;
|
|
|
|
}
|
|
|
|
weapRange *= 100.0f;
|
2013-11-18 11:33:09 +00:00
|
|
|
}
|
2014-01-17 21:30:28 +00:00
|
|
|
else //is creature
|
|
|
|
{
|
|
|
|
weaptype = WeapType_HandToHand; //doesn't matter, should only reflect if it is melee or distant weapon
|
2014-01-23 21:14:20 +00:00
|
|
|
weapRange = 150; //TODO: use true attack range (the same problem in Creature::hit)
|
2014-01-17 21:30:28 +00:00
|
|
|
}
|
2013-11-18 11:33:09 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
ESM::Position pos = actor.getRefData().getPosition();
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-04-18 22:16:56 +00:00
|
|
|
/*
|
|
|
|
* Some notes on meanings of variables:
|
|
|
|
*
|
|
|
|
* rangeMelee:
|
|
|
|
*
|
|
|
|
* - Distance where attack using the actor's weapon is possible
|
|
|
|
* - longer for ranged weapons (obviously?) vs. melee weapons
|
|
|
|
* - Once within this distance mFollowTarget is triggered
|
|
|
|
* (TODO: check whether the follow logic still works for ranged
|
|
|
|
* weapons, since rangeCloseup is set to zero)
|
|
|
|
* - TODO: The variable name is confusing. It was ok when AiCombat only
|
|
|
|
* had melee weapons but now that ranged weapons are supported that is
|
|
|
|
* no longer the case. It should really be renamed to something
|
|
|
|
* like rangeStrike - alternatively, keep this name for melee
|
|
|
|
* weapons and use a different variable for tracking ranged weapon
|
|
|
|
* distance (rangeRanged maybe?)
|
|
|
|
*
|
|
|
|
* rangeCloseup:
|
|
|
|
*
|
|
|
|
* - Applies to melee weapons or hand to hand only (or creatures without
|
|
|
|
* weapons)
|
|
|
|
* - Distance a little further away from the actor's weapon strike
|
|
|
|
* i.e. rangeCloseup > rangeMelee for melee weapons
|
|
|
|
* (the variable names make this simple concept counter-intuitive,
|
|
|
|
* something like rangeMelee > rangeStrike may be better)
|
|
|
|
* - Once the target gets beyond this distance mFollowTarget is cleared
|
|
|
|
* and a path to the target needs to be found
|
|
|
|
* - TODO: Possibly rename this variable to rangeMelee or even rangeFollow
|
|
|
|
*
|
|
|
|
* mFollowTarget:
|
|
|
|
*
|
|
|
|
* - Once triggered, the actor follows the target with LOS shortcut
|
|
|
|
* (the shortcut really only applies to cells where pathgrids are
|
|
|
|
* available, since the default path without pathgrids is direct to
|
|
|
|
* target even if LOS is not achieved)
|
|
|
|
*/
|
2014-01-15 20:56:55 +00:00
|
|
|
float rangeMelee;
|
|
|
|
float rangeCloseUp;
|
|
|
|
bool distantCombat = false;
|
2014-02-04 02:55:40 +00:00
|
|
|
if (weaptype==WeapType_BowAndArrow || weaptype==WeapType_Crossbow || weaptype==WeapType_Thrown)
|
2014-01-15 20:56:55 +00:00
|
|
|
{
|
|
|
|
rangeMelee = 1000; // TODO: should depend on archer skill
|
|
|
|
rangeCloseUp = 0; //doesn't needed when attacking from distance
|
|
|
|
distantCombat = true;
|
|
|
|
}
|
|
|
|
else
|
2013-11-18 11:33:09 +00:00
|
|
|
{
|
2014-01-15 20:56:55 +00:00
|
|
|
rangeMelee = weapRange;
|
|
|
|
rangeCloseUp = 300;
|
2013-11-18 11:33:09 +00:00
|
|
|
}
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
Ogre::Vector3 vStart(pos.pos[0], pos.pos[1], pos.pos[2]);
|
|
|
|
ESM::Position targetPos = mTarget.getRefData().getPosition();
|
|
|
|
Ogre::Vector3 vDest(targetPos.pos[0], targetPos.pos[1], targetPos.pos[2]);
|
|
|
|
Ogre::Vector3 vDir = vDest - vStart;
|
|
|
|
float distBetween = vDir.length();
|
|
|
|
|
2014-04-18 22:16:56 +00:00
|
|
|
// (within strike dist) || (not quite strike dist while following)
|
2014-01-19 20:09:51 +00:00
|
|
|
if(distBetween < rangeMelee || (distBetween <= rangeCloseUp && mFollowTarget) )
|
2014-01-15 20:56:55 +00:00
|
|
|
{
|
|
|
|
//Melee and Close-up combat
|
|
|
|
vDir.z = 0;
|
|
|
|
float dirLen = vDir.length();
|
2014-01-29 19:29:07 +00:00
|
|
|
mTargetAngle = Ogre::Radian( Ogre::Math::ACos(vDir.y / dirLen) * sgn(Ogre::Math::ASin(vDir.x / dirLen)) ).valueDegrees();
|
|
|
|
mRotate = true;
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
//bool LOS = MWBase::Environment::get().getWorld()->getLOS(actor, mTarget);
|
2014-04-18 22:16:56 +00:00
|
|
|
// (not quite strike dist while following)
|
2014-01-19 20:09:51 +00:00
|
|
|
if (mFollowTarget && distBetween > rangeMelee)
|
2014-01-15 20:56:55 +00:00
|
|
|
{
|
|
|
|
//Close-up combat: just run up on target
|
|
|
|
mMovement.mPosition[1] = 1;
|
|
|
|
}
|
2014-04-18 22:16:56 +00:00
|
|
|
else // (within strike dist)
|
2014-01-15 20:56:55 +00:00
|
|
|
{
|
|
|
|
//Melee: stop running and attack
|
|
|
|
mMovement.mPosition[1] = 0;
|
2014-01-30 02:18:34 +00:00
|
|
|
|
|
|
|
// When attacking with a weapon, choose between slash, thrust or chop
|
|
|
|
if (actor.getClass().hasInventoryStore(actor))
|
|
|
|
chooseBestAttack(weapon, mMovement);
|
2014-01-15 20:56:55 +00:00
|
|
|
|
2014-01-28 22:03:00 +00:00
|
|
|
if(mMovement.mPosition[0] || mMovement.mPosition[1])
|
2014-01-15 20:56:55 +00:00
|
|
|
{
|
|
|
|
mTimerCombatMove = 0.1f + 0.1f * static_cast<float>(rand())/RAND_MAX;
|
|
|
|
mCombatMove = true;
|
|
|
|
}
|
2014-01-19 20:09:51 +00:00
|
|
|
else if(actor.getClass().isNpc() && (!distantCombat || (distantCombat && rangeMelee/5)))
|
2014-01-15 20:56:55 +00:00
|
|
|
{
|
|
|
|
//apply sideway movement (kind of dodging) with some probability
|
|
|
|
if(static_cast<float>(rand())/RAND_MAX < 0.25)
|
|
|
|
{
|
|
|
|
mMovement.mPosition[0] = static_cast<float>(rand())/RAND_MAX < 0.5? 1: -1;
|
|
|
|
mTimerCombatMove = 0.05f + 0.15f * static_cast<float>(rand())/RAND_MAX;
|
|
|
|
mCombatMove = true;
|
|
|
|
}
|
|
|
|
}
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
if(distantCombat && distBetween < rangeMelee/4)
|
|
|
|
{
|
|
|
|
mMovement.mPosition[1] = -1;
|
|
|
|
}
|
2013-11-18 11:33:09 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
mReadyToAttack = true;
|
|
|
|
//only once got in melee combat, actor is allowed to use close-up shortcutting
|
2014-01-19 20:09:51 +00:00
|
|
|
mFollowTarget = true;
|
2014-01-15 20:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-18 11:33:09 +00:00
|
|
|
else
|
|
|
|
{
|
2014-04-18 22:16:56 +00:00
|
|
|
//target is at far distance: build path to target
|
2014-01-19 20:09:51 +00:00
|
|
|
mFollowTarget = false;
|
2014-01-15 20:56:55 +00:00
|
|
|
|
2014-02-24 07:47:09 +00:00
|
|
|
buildNewPath(actor); //may fail to build a path, check before use
|
2014-01-15 20:56:55 +00:00
|
|
|
|
|
|
|
//delete visited path node
|
|
|
|
mPathFinder.checkPathCompleted(pos.pos[0],pos.pos[1],pos.pos[2]);
|
|
|
|
|
2014-02-24 07:47:09 +00:00
|
|
|
//if no new path leave mTargetAngle unchanged
|
2014-02-23 07:42:40 +00:00
|
|
|
if(!mPathFinder.getPath().empty())
|
|
|
|
{
|
|
|
|
//try shortcut
|
2014-03-04 08:34:38 +00:00
|
|
|
if(vDir.length() < mPathFinder.getDistToNext(pos.pos[0],pos.pos[1],pos.pos[2]) && MWBase::Environment::get().getWorld()->getLOS(actor, mTarget))
|
2014-02-23 07:42:40 +00:00
|
|
|
mTargetAngle = Ogre::Radian( Ogre::Math::ACos(vDir.y / vDir.length()) * sgn(Ogre::Math::ASin(vDir.x / vDir.length())) ).valueDegrees();
|
|
|
|
else
|
|
|
|
mTargetAngle = mPathFinder.getZAngleToNext(pos.pos[0], pos.pos[1]);
|
2014-02-24 07:47:09 +00:00
|
|
|
mRotate = true;
|
2014-02-23 07:42:40 +00:00
|
|
|
}
|
2014-02-21 10:35:46 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
mMovement.mPosition[1] = 1;
|
|
|
|
mReadyToAttack = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(distBetween > rangeMelee)
|
|
|
|
{
|
|
|
|
//special run attack; it shouldn't affect melee combat tactics
|
|
|
|
if(actor.getClass().getMovementSettings(actor).mPosition[1] == 1)
|
2013-09-28 10:25:37 +00:00
|
|
|
{
|
2014-01-15 20:56:55 +00:00
|
|
|
//check if actor can overcome the distance = distToTarget - attackerWeapRange
|
|
|
|
//less than in time of playing weapon anim from 'start' to 'hit' tags (t_swing)
|
|
|
|
//then start attacking
|
|
|
|
float speed1 = cls.getSpeed(actor);
|
2014-01-17 18:33:49 +00:00
|
|
|
float speed2 = mTarget.getClass().getSpeed(mTarget);
|
2014-01-17 18:55:21 +00:00
|
|
|
if(mTarget.getClass().getMovementSettings(mTarget).mPosition[0] == 0
|
|
|
|
&& mTarget.getClass().getMovementSettings(mTarget).mPosition[1] == 0)
|
2014-01-15 20:56:55 +00:00
|
|
|
speed2 = 0;
|
|
|
|
|
|
|
|
float s1 = distBetween - weapRange;
|
|
|
|
float t = s1/speed1;
|
|
|
|
float s2 = speed2 * t;
|
2014-01-28 22:03:00 +00:00
|
|
|
float t_swing = 0.17f/weapSpeed;//instead of 0.17 should be the time of playing weapon anim from 'start' to 'hit' tags
|
2014-01-15 20:56:55 +00:00
|
|
|
if (t + s2/speed1 <= t_swing)
|
|
|
|
{
|
|
|
|
mReadyToAttack = true;
|
|
|
|
if(mTimerAttack <= -attackPeriod)
|
|
|
|
{
|
2014-01-16 20:24:05 +00:00
|
|
|
mTimerAttack = 0.3f*static_cast<float>(rand())/RAND_MAX;
|
2014-01-15 20:56:55 +00:00
|
|
|
mStrike = true;
|
|
|
|
}
|
|
|
|
}
|
2013-09-28 10:25:37 +00:00
|
|
|
}
|
2013-11-18 11:33:09 +00:00
|
|
|
}
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-04-19 22:31:02 +00:00
|
|
|
// NOTE: This section gets updated every tReaction, which is currently hard
|
|
|
|
// coded at 250ms or 1/4 second
|
|
|
|
//
|
|
|
|
// TODO: Add a parameter to vary DURATION_SAME_SPOT?
|
|
|
|
if((distBetween > rangeMelee || mFollowTarget) &&
|
|
|
|
mObstacleCheck.check(actor, tReaction)) // check if evasive action needed
|
|
|
|
{
|
|
|
|
// first check if we're walking into a door
|
|
|
|
mDoorCheckDuration += 1.0f; // add time taken for obstacle check
|
|
|
|
MWWorld::CellStore *cell = actor.getCell();
|
|
|
|
if(mDoorCheckDuration >= DOOR_CHECK_INTERVAL && !cell->getCell()->isExterior())
|
|
|
|
{
|
|
|
|
mDoorCheckDuration = 0;
|
|
|
|
// Check all the doors in this cell
|
|
|
|
mDoors = cell->get<ESM::Door>(); // update
|
|
|
|
mDoorIter = mDoors.mList.begin();
|
|
|
|
Ogre::Vector3 actorPos(actor.getRefData().getPosition().pos);
|
|
|
|
for (; mDoorIter != mDoors.mList.end(); ++mDoorIter)
|
|
|
|
{
|
|
|
|
MWWorld::LiveCellRef<ESM::Door>& ref = *mDoorIter;
|
|
|
|
float minSqr = 1.3*1.3*MIN_DIST_TO_DOOR_SQUARED; // for legibility
|
|
|
|
if(actorPos.squaredDistance(Ogre::Vector3(ref.mRef.mPos.pos)) < minSqr &&
|
|
|
|
ref.mData.getLocalRotation().rot[2] < 0.4f) // even small opening
|
|
|
|
{
|
|
|
|
//std::cout<<"closed door id \""<<ref.mRef.mRefID<<"\""<<std::endl;
|
|
|
|
mBackOffDoor = true;
|
|
|
|
mObstacleCheck.clear();
|
|
|
|
if(mFollowTarget)
|
|
|
|
mFollowTarget = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // probably walking into another NPC TODO: untested in combat situation
|
|
|
|
{
|
|
|
|
// TODO: diagonal should have same animation as walk forward
|
|
|
|
// but doesn't seem to do that?
|
|
|
|
actor.getClass().getMovementSettings(actor).mPosition[0] = 1;
|
|
|
|
actor.getClass().getMovementSettings(actor).mPosition[1] = 0.1f;
|
|
|
|
// change the angle a bit, too
|
|
|
|
if(mPathFinder.isPathConstructed())
|
|
|
|
zTurn(actor, Ogre::Degree(mPathFinder.getZAngleToNext(pos.pos[0] + 1, pos.pos[1])));
|
|
|
|
|
|
|
|
if(mFollowTarget)
|
|
|
|
mFollowTarget = false;
|
|
|
|
// FIXME: can fool actors to stay behind doors, etc.
|
|
|
|
// Related to Bug#1102 and to some degree #1155 as well
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:16:56 +00:00
|
|
|
MWWorld::LiveCellRef<ESM::Door>& ref = *mDoorIter;
|
|
|
|
Ogre::Vector3 actorPos(actor.getRefData().getPosition().pos);
|
2014-04-19 22:31:02 +00:00
|
|
|
float minSqr = 1.6 * 1.6 * MIN_DIST_TO_DOOR_SQUARED; // for legibility
|
|
|
|
// TODO: add reaction to checking open doors
|
2014-04-18 22:16:56 +00:00
|
|
|
if(mBackOffDoor &&
|
2014-04-19 22:31:02 +00:00
|
|
|
actorPos.squaredDistance(Ogre::Vector3(ref.mRef.mPos.pos)) < minSqr)
|
2014-04-18 22:16:56 +00:00
|
|
|
{
|
|
|
|
mMovement.mPosition[1] = -0.2; // back off, but slowly
|
2014-04-19 22:31:02 +00:00
|
|
|
}
|
|
|
|
else if(mBackOffDoor &&
|
|
|
|
mDoorIter != mDoors.mList.end() &&
|
|
|
|
ref.mData.getLocalRotation().rot[2] >= 1)
|
|
|
|
{
|
|
|
|
mDoorIter = mDoors.mList.end();
|
|
|
|
mBackOffDoor = false;
|
|
|
|
//std::cout<<"open door id \""<<ref.mRef.mRefID<<"\""<<std::endl;
|
|
|
|
mMovement.mPosition[1] = 1;
|
2014-04-18 22:16:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-19 22:31:02 +00:00
|
|
|
mMovement.mPosition[1] = 1; // FIXME: oscillation?
|
2014-04-18 22:16:56 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 19:29:07 +00:00
|
|
|
actor.getClass().getMovementSettings(actor) = mMovement;
|
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
void AiCombat::buildNewPath(const MWWorld::Ptr& actor)
|
|
|
|
{
|
|
|
|
//Construct path to target
|
|
|
|
ESM::Pathgrid::Point dest;
|
|
|
|
dest.mX = mTarget.getRefData().getPosition().pos[0];
|
|
|
|
dest.mY = mTarget.getRefData().getPosition().pos[1];
|
|
|
|
dest.mZ = mTarget.getRefData().getPosition().pos[2];
|
|
|
|
Ogre::Vector3 newPathTarget = Ogre::Vector3(dest.mX, dest.mY, dest.mZ);
|
2014-01-03 16:06:05 +00:00
|
|
|
|
2014-02-23 07:42:40 +00:00
|
|
|
float dist = -1; //hack to indicate first time, to construct a new path
|
|
|
|
if(!mPathFinder.getPath().empty())
|
|
|
|
{
|
|
|
|
ESM::Pathgrid::Point lastPt = mPathFinder.getPath().back();
|
|
|
|
Ogre::Vector3 currPathTarget(lastPt.mX, lastPt.mY, lastPt.mZ);
|
|
|
|
dist = Ogre::Math::Abs((newPathTarget - currPathTarget).length());
|
|
|
|
}
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
float targetPosThreshold;
|
2014-02-21 10:35:46 +00:00
|
|
|
bool isOutside = actor.getCell()->getCell()->isExterior();
|
2014-01-15 20:56:55 +00:00
|
|
|
if (isOutside)
|
|
|
|
targetPosThreshold = 300;
|
|
|
|
else
|
|
|
|
targetPosThreshold = 100;
|
2013-10-30 19:52:23 +00:00
|
|
|
|
2014-02-23 07:42:40 +00:00
|
|
|
if((dist < 0) || (dist > targetPosThreshold))
|
2013-11-18 11:33:09 +00:00
|
|
|
{
|
2014-01-15 20:56:55 +00:00
|
|
|
//construct new path only if target has moved away more than on <targetPosThreshold>
|
|
|
|
ESM::Position pos = actor.getRefData().getPosition();
|
2013-09-28 10:25:37 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
ESM::Pathgrid::Point start;
|
|
|
|
start.mX = pos.pos[0];
|
|
|
|
start.mY = pos.pos[1];
|
|
|
|
start.mZ = pos.pos[2];
|
2013-10-27 13:03:58 +00:00
|
|
|
|
2014-01-15 20:56:55 +00:00
|
|
|
if(!mPathFinder.isPathConstructed())
|
2014-01-28 22:03:00 +00:00
|
|
|
mPathFinder.buildPath(start, dest, actor.getCell(), isOutside);
|
2013-11-18 11:33:09 +00:00
|
|
|
else
|
|
|
|
{
|
2014-01-15 20:56:55 +00:00
|
|
|
PathFinder newPathFinder;
|
2014-01-28 22:03:00 +00:00
|
|
|
newPathFinder.buildPath(start, dest, actor.getCell(), isOutside);
|
2013-11-18 11:33:09 +00:00
|
|
|
|
2014-02-21 10:35:46 +00:00
|
|
|
//TO EXPLORE:
|
2014-01-15 20:56:55 +00:00
|
|
|
//maybe here is a mistake (?): PathFinder::getPathSize() returns number of grid points in the path,
|
|
|
|
//not the actual path length. Here we should know if the new path is actually more effective.
|
|
|
|
//if(pathFinder2.getPathSize() < mPathFinder.getPathSize())
|
2014-02-23 07:42:40 +00:00
|
|
|
if(!mPathFinder.getPath().empty())
|
|
|
|
{
|
|
|
|
newPathFinder.syncStart(mPathFinder.getPath());
|
|
|
|
mPathFinder = newPathFinder;
|
|
|
|
}
|
2014-01-15 20:56:55 +00:00
|
|
|
}
|
2013-09-25 16:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AiCombat::getTypeId() const
|
|
|
|
{
|
2014-01-07 00:12:37 +00:00
|
|
|
return TypeIdCombat;
|
2013-11-18 11:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AiCombat::getPriority() const
|
|
|
|
{
|
|
|
|
return 1;
|
2013-09-25 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
2014-01-07 00:12:37 +00:00
|
|
|
const std::string &AiCombat::getTargetId() const
|
|
|
|
{
|
2014-01-16 20:24:05 +00:00
|
|
|
return mTarget.getRefData().getHandle();
|
2014-01-07 00:12:37 +00:00
|
|
|
}
|
|
|
|
|
2014-01-25 23:16:56 +00:00
|
|
|
|
2013-09-25 16:01:36 +00:00
|
|
|
AiCombat *MWMechanics::AiCombat::clone() const
|
|
|
|
{
|
|
|
|
return new AiCombat(*this);
|
|
|
|
}
|
2014-01-17 18:33:49 +00:00
|
|
|
}
|
2014-01-15 20:56:55 +00:00
|
|
|
|
2014-01-27 20:38:01 +00:00
|
|
|
|
2014-01-17 18:33:49 +00:00
|
|
|
namespace
|
|
|
|
{
|
2014-01-15 20:56:55 +00:00
|
|
|
|
2014-01-17 18:33:49 +00:00
|
|
|
void chooseBestAttack(const ESM::Weapon* weapon, MWMechanics::Movement &movement)
|
|
|
|
{
|
|
|
|
if (weapon == NULL)
|
|
|
|
{
|
2014-01-30 02:18:34 +00:00
|
|
|
//hand-to-hand deal equal damage for each type
|
2014-01-16 20:24:05 +00:00
|
|
|
float roll = static_cast<float>(rand())/RAND_MAX;
|
2014-01-17 18:33:49 +00:00
|
|
|
if(roll <= 0.333f) //side punch
|
2014-01-15 20:56:55 +00:00
|
|
|
{
|
|
|
|
movement.mPosition[0] = (static_cast<float>(rand())/RAND_MAX < 0.5f)? 1: -1;
|
|
|
|
movement.mPosition[1] = 0;
|
|
|
|
}
|
2014-01-17 18:33:49 +00:00
|
|
|
else if(roll <= 0.666f) //forward punch
|
2014-01-15 20:56:55 +00:00
|
|
|
movement.mPosition[1] = 1;
|
2014-01-23 21:14:20 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
movement.mPosition[1] = movement.mPosition[0] = 0;
|
|
|
|
}
|
2014-01-17 18:33:49 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 21:14:20 +00:00
|
|
|
//the more damage attackType deals the more probability it has
|
2014-01-17 18:33:49 +00:00
|
|
|
int slash = (weapon->mData.mSlash[0] + weapon->mData.mSlash[1])/2;
|
|
|
|
int chop = (weapon->mData.mChop[0] + weapon->mData.mChop[1])/2;
|
|
|
|
int thrust = (weapon->mData.mThrust[0] + weapon->mData.mThrust[1])/2;
|
|
|
|
|
|
|
|
float total = slash + chop + thrust;
|
2014-02-21 10:35:46 +00:00
|
|
|
|
2014-01-17 18:33:49 +00:00
|
|
|
float roll = static_cast<float>(rand())/RAND_MAX;
|
|
|
|
if(roll <= static_cast<float>(slash)/total)
|
|
|
|
{
|
|
|
|
movement.mPosition[0] = (static_cast<float>(rand())/RAND_MAX < 0.5f)? 1: -1;
|
|
|
|
movement.mPosition[1] = 0;
|
2014-01-15 20:56:55 +00:00
|
|
|
}
|
2014-01-17 18:33:49 +00:00
|
|
|
else if(roll <= (static_cast<float>(slash) + static_cast<float>(thrust))/total)
|
|
|
|
movement.mPosition[1] = 1;
|
2014-01-23 21:14:20 +00:00
|
|
|
else
|
|
|
|
movement.mPosition[1] = movement.mPosition[0] = 0;
|
2013-09-25 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 22:03:00 +00:00
|
|
|
}
|