1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 08:23:52 +00:00

code refining + minor fixes

This commit is contained in:
mrcheko 2014-04-26 22:21:20 +04:00
parent f3626adc86
commit dbe1307de0
2 changed files with 18 additions and 43 deletions

View file

@ -175,23 +175,23 @@ namespace MWMechanics
mStrike = false; mStrike = false;
} }
const MWWorld::Class &cls = actor.getClass(); const MWWorld::Class &actorCls = actor.getClass();
const ESM::Weapon *weapon = NULL; const ESM::Weapon *weapon = NULL;
MWMechanics::WeaponType weaptype; MWMechanics::WeaponType weaptype;
float weapRange, weapSpeed = 1.0f; float weapRange, weapSpeed = 1.0f;
actor.getClass().getCreatureStats(actor).setMovementFlag(CreatureStats::Flag_Run, true); actorCls.getCreatureStats(actor).setMovementFlag(CreatureStats::Flag_Run, true);
// Get weapon characteristics // Get weapon characteristics
if (actor.getClass().hasInventoryStore(actor)) if (actorCls.hasInventoryStore(actor))
{ {
MWMechanics::DrawState_ state = actor.getClass().getCreatureStats(actor).getDrawState(); MWMechanics::DrawState_ state = actorCls.getCreatureStats(actor).getDrawState();
if (state == MWMechanics::DrawState_Spell || state == MWMechanics::DrawState_Nothing) if (state == MWMechanics::DrawState_Spell || state == MWMechanics::DrawState_Nothing)
actor.getClass().getCreatureStats(actor).setDrawState(MWMechanics::DrawState_Weapon); actorCls.getCreatureStats(actor).setDrawState(MWMechanics::DrawState_Weapon);
//Get weapon speed and range //Get weapon speed and range
MWWorld::ContainerStoreIterator weaponSlot = MWWorld::ContainerStoreIterator weaponSlot =
MWMechanics::getActiveWeapon(cls.getCreatureStats(actor), cls.getInventoryStore(actor), &weaptype); MWMechanics::getActiveWeapon(actorCls.getCreatureStats(actor), actorCls.getInventoryStore(actor), &weaptype);
if (weaptype == WeapType_HandToHand) if (weaptype == WeapType_HandToHand)
{ {
@ -216,7 +216,7 @@ namespace MWMechanics
float rangeMelee; float rangeMelee;
float rangeCloseUp; float rangeCloseUp;
bool distantCombat = false; bool distantCombat = false;
if (weaptype==WeapType_BowAndArrow || weaptype==WeapType_Crossbow || weaptype==WeapType_Thrown) if (weaptype == WeapType_BowAndArrow || weaptype == WeapType_Crossbow || weaptype == WeapType_Thrown)
{ {
rangeMelee = 1000; // TODO: should depend on archer skill rangeMelee = 1000; // TODO: should depend on archer skill
rangeCloseUp = 0; // not needed in ranged combat rangeCloseUp = 0; // not needed in ranged combat
@ -236,15 +236,15 @@ namespace MWMechanics
bool isStuck = false; bool isStuck = false;
float speed = 0.0f; float speed = 0.0f;
if(mMovement.mPosition[1] && (Ogre::Vector3(mLastPos.pos) - vActorPos).length() < (speed = cls.getSpeed(actor)) / 10.0f) if(mMovement.mPosition[1] && (Ogre::Vector3(mLastPos.pos) - vActorPos).length() < (speed = actorCls.getSpeed(actor)) / 10.0f)
isStuck = true; isStuck = true;
mLastPos = pos; mLastPos = pos;
// check if can move along z-axis // check if can move along z-axis
bool canMoveByZ; bool canMoveByZ;
if(canMoveByZ = ((actor.getClass().isNpc() || actor.getClass().canSwim(actor)) && MWBase::Environment::get().getWorld()->isSwimming(actor)) if(canMoveByZ = ((actorCls.isNpc() || actorCls.canSwim(actor)) && MWBase::Environment::get().getWorld()->isSwimming(actor))
|| (actor.getClass().canFly(actor) && MWBase::Environment::get().getWorld()->isFlying(actor))) || (actorCls.canFly(actor) && MWBase::Environment::get().getWorld()->isFlying(actor)))
{ {
// determine vertical angle to target // determine vertical angle to target
mMovement.mRotation[0] = getXAngleToDir(vDirToTarget, distToTarget); mMovement.mRotation[0] = getXAngleToDir(vDirToTarget, distToTarget);
@ -266,16 +266,15 @@ namespace MWMechanics
//Melee: stop running and attack //Melee: stop running and attack
mMovement.mPosition[1] = 0; mMovement.mPosition[1] = 0;
// When attacking with a weapon, choose between slash, thrust or chop // set slash/thrust/chop attack
if (mStrike && actor.getClass().hasInventoryStore(actor)) if (mStrike) chooseBestAttack(weapon, mMovement);
chooseBestAttack(weapon, mMovement);
if(mMovement.mPosition[0] || mMovement.mPosition[1]) if(mMovement.mPosition[0] || mMovement.mPosition[1])
{ {
mTimerCombatMove = 0.1f + 0.1f * static_cast<float>(rand())/RAND_MAX; mTimerCombatMove = 0.1f + 0.1f * static_cast<float>(rand())/RAND_MAX;
mCombatMove = true; mCombatMove = true;
} }
else if(actor.getClass().isNpc() && (!distantCombat || (distantCombat && rangeMelee/5))) else if(actorCls.isNpc() && (!distantCombat || (distantCombat && distToTarget < rangeMelee/2)))
{ {
//apply sideway movement (kind of dodging) with some probability //apply sideway movement (kind of dodging) with some probability
if(static_cast<float>(rand())/RAND_MAX < 0.25) if(static_cast<float>(rand())/RAND_MAX < 0.25)
@ -309,7 +308,7 @@ namespace MWMechanics
|| (Ogre::Vector3(mShortcutFailPos.pos) - vActorPos).length() >= PATHFIND_SHORTCUT_RETRY_DIST) || (Ogre::Vector3(mShortcutFailPos.pos) - vActorPos).length() >= PATHFIND_SHORTCUT_RETRY_DIST)
&& (inLOS = MWBase::Environment::get().getWorld()->getLOS(actor, mTarget))) && (inLOS = MWBase::Environment::get().getWorld()->getLOS(actor, mTarget)))
{ {
if(speed == 0.0f) speed = cls.getSpeed(actor); if(speed == 0.0f) speed = actorCls.getSpeed(actor);
// maximum dist before pit/obstacle for actor to avoid them depending on his speed // maximum dist before pit/obstacle for actor to avoid them depending on his speed
float maxAvoidDist = tReaction * speed + speed / MAX_VEL_ANGULAR.valueRadians() * 2; float maxAvoidDist = tReaction * speed + speed / MAX_VEL_ANGULAR.valueRadians() * 2;
preferShortcut = checkWayIsClear(vActorPos, vTargetPos, distToTarget > maxAvoidDist*1.5? maxAvoidDist : maxAvoidDist/2); preferShortcut = checkWayIsClear(vActorPos, vTargetPos, distToTarget > maxAvoidDist*1.5? maxAvoidDist : maxAvoidDist/2);
@ -370,15 +369,15 @@ namespace MWMechanics
mReadyToAttack = false; mReadyToAttack = false;
} }
if(distToTarget > rangeMelee) if(distToTarget > rangeMelee && !distantCombat)
{ {
//special run attack; it shouldn't affect melee combat tactics //special run attack; it shouldn't affect melee combat tactics
if(actor.getClass().getMovementSettings(actor).mPosition[1] == 1) if(actorCls.getMovementSettings(actor).mPosition[1] == 1)
{ {
//check if actor can overcome the distance = distToTarget - attackerWeapRange //check if actor can overcome the distance = distToTarget - attackerWeapRange
//less than in time of playing weapon anim from 'start' to 'hit' tags (t_swing) //less than in time of playing weapon anim from 'start' to 'hit' tags (t_swing)
//then start attacking //then start attacking
float speed1 = cls.getSpeed(actor); float speed1 = actorCls.getSpeed(actor);
float speed2 = mTarget.getClass().getSpeed(mTarget); float speed2 = mTarget.getClass().getSpeed(mTarget);
if(mTarget.getClass().getMovementSettings(mTarget).mPosition[0] == 0 if(mTarget.getClass().getMovementSettings(mTarget).mPosition[0] == 0
&& mTarget.getClass().getMovementSettings(mTarget).mPosition[1] == 0) && mTarget.getClass().getMovementSettings(mTarget).mPosition[1] == 0)

View file

@ -41,31 +41,7 @@ bool smoothTurn(const MWWorld::Ptr& actor, Ogre::Radian targetAngle, int axis)
bool zTurn(const MWWorld::Ptr& actor, Ogre::Radian targetAngle) bool zTurn(const MWWorld::Ptr& actor, Ogre::Radian targetAngle)
{ {
Ogre::Radian currentAngle (actor.getRefData().getPosition().rot[2]); return smoothTurn(actor, targetAngle, 2);
Ogre::Radian diff (targetAngle - currentAngle);
if (diff >= Ogre::Degree(180))
{
// Turning the other way would be a better idea
diff = diff-Ogre::Degree(360);
}
else if (diff <= Ogre::Degree(-180))
{
diff = Ogre::Degree(360)-diff;
}
Ogre::Radian absDiff = Ogre::Math::Abs(diff);
// The turning animation actually moves you slightly, so the angle will be wrong again.
// Use epsilon to prevent jerkiness.
const Ogre::Degree epsilon (0.5);
if (absDiff < epsilon)
return true;
Ogre::Radian limit = MAX_VEL_ANGULAR * MWBase::Environment::get().getFrameDuration();
if (absDiff > limit)
diff = Ogre::Math::Sign(diff) * limit;
actor.getClass().getMovementSettings(actor).mRotation[2] = diff.valueRadians();
return false;
} }
} }