2013-01-10 16:35:24 +00:00
|
|
|
/*
|
|
|
|
* OpenMW - The completely unofficial reimplementation of Morrowind
|
|
|
|
*
|
|
|
|
* This file (character.cpp) is part of the OpenMW package.
|
|
|
|
*
|
|
|
|
* OpenMW is distributed as free software: you can redistribute it
|
|
|
|
* and/or modify it under the terms of the GNU General Public License
|
|
|
|
* version 3, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* version 3 along with this program. If not, see
|
|
|
|
* http://www.gnu.org/licenses/ .
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "character.hpp"
|
|
|
|
|
2013-01-19 22:56:24 +00:00
|
|
|
#include <OgreStringConverter.h>
|
|
|
|
|
2013-03-31 07:13:56 +00:00
|
|
|
#include "movement.hpp"
|
|
|
|
|
2013-01-16 18:45:18 +00:00
|
|
|
#include "../mwrender/animation.hpp"
|
2013-01-10 16:35:24 +00:00
|
|
|
|
2013-01-16 23:52:03 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2013-02-04 15:10:14 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2013-01-16 23:52:03 +00:00
|
|
|
|
2013-04-28 05:53:04 +00:00
|
|
|
#include "../mwworld/player.hpp"
|
2013-01-16 22:37:32 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
2013-01-16 21:09:21 +00:00
|
|
|
|
2013-04-30 18:42:02 +00:00
|
|
|
#include "../mwmechanics/stat.hpp"
|
|
|
|
#include "../mwmechanics/creaturestats.hpp"
|
2013-02-04 15:10:14 +00:00
|
|
|
|
2013-01-10 16:35:24 +00:00
|
|
|
namespace MWMechanics
|
|
|
|
{
|
|
|
|
|
2013-01-19 22:56:24 +00:00
|
|
|
static const struct {
|
|
|
|
CharacterState state;
|
|
|
|
const char groupname[32];
|
|
|
|
} sStateList[] = {
|
2013-02-03 20:02:51 +00:00
|
|
|
{ CharState_Idle, "idle" },
|
|
|
|
{ CharState_Idle2, "idle2" },
|
|
|
|
{ CharState_Idle3, "idle3" },
|
|
|
|
{ CharState_Idle4, "idle4" },
|
|
|
|
{ CharState_Idle5, "idle5" },
|
|
|
|
{ CharState_Idle6, "idle6" },
|
|
|
|
{ CharState_Idle7, "idle7" },
|
|
|
|
{ CharState_Idle8, "idle8" },
|
|
|
|
{ CharState_Idle9, "idle9" },
|
2013-02-06 03:05:07 +00:00
|
|
|
{ CharState_IdleSwim, "idleswim" },
|
2013-03-06 15:58:56 +00:00
|
|
|
{ CharState_IdleSneak, "idlesneak" },
|
2013-02-03 20:02:51 +00:00
|
|
|
|
|
|
|
{ CharState_WalkForward, "walkforward" },
|
|
|
|
{ CharState_WalkBack, "walkback" },
|
|
|
|
{ CharState_WalkLeft, "walkleft" },
|
|
|
|
{ CharState_WalkRight, "walkright" },
|
|
|
|
|
2013-02-06 03:05:07 +00:00
|
|
|
{ CharState_SwimWalkForward, "swimwalkforward" },
|
|
|
|
{ CharState_SwimWalkBack, "swimwalkback" },
|
|
|
|
{ CharState_SwimWalkLeft, "swimwalkleft" },
|
|
|
|
{ CharState_SwimWalkRight, "swimwalkright" },
|
|
|
|
|
2013-02-07 00:53:52 +00:00
|
|
|
{ CharState_RunForward, "runforward" },
|
|
|
|
{ CharState_RunBack, "runback" },
|
|
|
|
{ CharState_RunLeft, "runleft" },
|
|
|
|
{ CharState_RunRight, "runright" },
|
|
|
|
|
|
|
|
{ CharState_SwimRunForward, "swimrunforward" },
|
|
|
|
{ CharState_SwimRunBack, "swimrunback" },
|
|
|
|
{ CharState_SwimRunLeft, "swimrunleft" },
|
|
|
|
{ CharState_SwimRunRight, "swimrunright" },
|
|
|
|
|
2013-03-06 15:58:56 +00:00
|
|
|
{ CharState_SneakForward, "sneakforward" },
|
|
|
|
{ CharState_SneakBack, "sneakback" },
|
|
|
|
{ CharState_SneakLeft, "sneakleft" },
|
|
|
|
{ CharState_SneakRight, "sneakright" },
|
|
|
|
|
2013-03-31 10:50:20 +00:00
|
|
|
{ CharState_TurnLeft, "turnleft" },
|
|
|
|
{ CharState_TurnRight, "turnright" },
|
|
|
|
|
2013-02-15 12:45:28 +00:00
|
|
|
{ CharState_Jump, "jump" },
|
|
|
|
|
2013-02-03 20:02:51 +00:00
|
|
|
{ CharState_Death1, "death1" },
|
|
|
|
{ CharState_Death2, "death2" },
|
|
|
|
{ CharState_Death3, "death3" },
|
|
|
|
{ CharState_Death4, "death4" },
|
|
|
|
{ CharState_Death5, "death5" },
|
2013-01-19 22:56:24 +00:00
|
|
|
};
|
|
|
|
static const size_t sStateListSize = sizeof(sStateList)/sizeof(sStateList[0]);
|
|
|
|
|
2013-02-03 20:02:51 +00:00
|
|
|
static void getStateInfo(CharacterState state, std::string *group)
|
2013-01-19 22:56:24 +00:00
|
|
|
{
|
|
|
|
for(size_t i = 0;i < sStateListSize;i++)
|
|
|
|
{
|
|
|
|
if(sStateList[i].state == state)
|
|
|
|
{
|
|
|
|
*group = sStateList[i].groupname;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw std::runtime_error("Failed to find character state "+Ogre::StringConverter::toString(state));
|
|
|
|
}
|
|
|
|
|
2013-01-22 06:51:13 +00:00
|
|
|
|
2013-01-19 22:22:15 +00:00
|
|
|
CharacterController::CharacterController(const MWWorld::Ptr &ptr, MWRender::Animation *anim, CharacterState state, bool loop)
|
2013-04-30 16:22:59 +00:00
|
|
|
: mPtr(ptr), mAnimation(anim), mCharState(state), mSkipAnim(false), mMovingAnim(false),
|
2013-04-30 18:42:02 +00:00
|
|
|
mSecondsOfRunning(0), mSecondsOfSwimming(0), mHighestPosition(0)
|
2013-01-16 18:45:18 +00:00
|
|
|
{
|
2013-01-20 05:55:04 +00:00
|
|
|
if(!mAnimation)
|
2013-01-16 18:45:18 +00:00
|
|
|
return;
|
|
|
|
|
2013-04-25 13:12:37 +00:00
|
|
|
std::string group;
|
2013-04-25 13:29:17 +00:00
|
|
|
getStateInfo(mCharState, &group);
|
2013-03-31 23:12:02 +00:00
|
|
|
if(MWWorld::Class::get(mPtr).isActor())
|
2013-02-05 20:55:06 +00:00
|
|
|
{
|
|
|
|
/* Accumulate along X/Y only for now, until we can figure out how we should
|
|
|
|
* handle knockout and death which moves the character down. */
|
|
|
|
mAnimation->setAccumulation(Ogre::Vector3(1.0f, 1.0f, 0.0f));
|
|
|
|
}
|
2013-03-31 23:12:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Don't accumulate with non-actors. */
|
|
|
|
mAnimation->setAccumulation(Ogre::Vector3(0.0f));
|
|
|
|
}
|
2013-04-25 13:12:37 +00:00
|
|
|
if(mAnimation->hasAnimation(group))
|
|
|
|
mMovingAnim = mAnimation->play(group, "start", "stop", 1.0f, loop ? (~(size_t)0) : 0);
|
2013-01-16 18:45:18 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 15:10:14 +00:00
|
|
|
CharacterController::~CharacterController()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:09:21 +00:00
|
|
|
|
2013-02-25 17:57:34 +00:00
|
|
|
void CharacterController::updatePtr(const MWWorld::Ptr &ptr)
|
|
|
|
{
|
|
|
|
mPtr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-31 07:13:56 +00:00
|
|
|
void CharacterController::update(float duration, Movement &movement)
|
2013-01-19 05:40:47 +00:00
|
|
|
{
|
2013-02-18 14:29:16 +00:00
|
|
|
float speed = 0.0f;
|
|
|
|
if(!(getState() >= CharState_Death1))
|
|
|
|
{
|
|
|
|
const MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const MWWorld::Class &cls = MWWorld::Class::get(mPtr);
|
2013-02-03 15:15:34 +00:00
|
|
|
|
2013-02-21 04:08:04 +00:00
|
|
|
bool onground = world->isOnGround(mPtr);
|
2013-02-18 14:29:16 +00:00
|
|
|
bool inwater = world->isSwimming(mPtr);
|
|
|
|
bool isrunning = cls.getStance(mPtr, MWWorld::Class::Run);
|
2013-03-06 15:58:56 +00:00
|
|
|
bool sneak = cls.getStance(mPtr, MWWorld::Class::Sneak);
|
2013-03-31 02:00:46 +00:00
|
|
|
const Ogre::Vector3 &vec = cls.getMovementVector(mPtr);
|
2013-03-31 08:29:24 +00:00
|
|
|
const Ogre::Vector3 &rot = cls.getRotationVector(mPtr);
|
2013-02-18 14:29:16 +00:00
|
|
|
speed = cls.getSpeed(mPtr);
|
2013-02-06 03:05:07 +00:00
|
|
|
|
2013-04-28 05:53:04 +00:00
|
|
|
// advance athletics
|
|
|
|
if (vec.squaredLength() > 0 && mPtr == MWBase::Environment::get().getWorld()->getPlayer().getPlayer())
|
|
|
|
{
|
|
|
|
if (inwater)
|
|
|
|
{
|
|
|
|
mSecondsOfSwimming += duration;
|
|
|
|
while (mSecondsOfSwimming > 1)
|
|
|
|
{
|
|
|
|
MWWorld::Class::get(mPtr).skillUsageSucceeded(mPtr, ESM::Skill::Athletics, 1);
|
|
|
|
mSecondsOfSwimming -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isrunning)
|
|
|
|
{
|
|
|
|
mSecondsOfRunning += duration;
|
|
|
|
while (mSecondsOfRunning > 1)
|
|
|
|
{
|
|
|
|
MWWorld::Class::get(mPtr).skillUsageSucceeded(mPtr, ESM::Skill::Athletics, 0);
|
|
|
|
mSecondsOfRunning -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 16:22:59 +00:00
|
|
|
// FIXME: X/Y movement should be disallowed except for the initial thrust (which would be carried by "physics" until landing).
|
2013-02-24 11:30:33 +00:00
|
|
|
if(onground && vec.z > 0.0f)
|
|
|
|
{
|
2013-04-30 16:22:59 +00:00
|
|
|
//Advance acrobatics on jump
|
|
|
|
if(getState()!=CharState_Jump)
|
|
|
|
{
|
|
|
|
setState(CharState_Jump, true);
|
|
|
|
MWWorld::Class::get(mPtr).skillUsageSucceeded(mPtr, ESM::Skill::Acrobatics, 0);
|
|
|
|
}
|
|
|
|
|
2013-02-24 11:30:33 +00:00
|
|
|
float x = cls.getJump(mPtr);
|
|
|
|
|
|
|
|
if(vec.x == 0 && vec.y == 0)
|
2013-03-31 07:13:56 +00:00
|
|
|
movement.mPosition[2] += x*duration;
|
2013-02-24 11:30:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* FIXME: this would be more correct if we were going into a jumping state,
|
|
|
|
* rather than normal walking/idle states. */
|
|
|
|
//Ogre::Vector3 lat = Ogre::Vector3(vec.x, vec.y, 0.0f).normalisedCopy();
|
|
|
|
//movement += Ogre::Vector3(lat.x, lat.y, 1.0f) * x * 0.707f * duration;
|
2013-03-31 07:13:56 +00:00
|
|
|
movement.mPosition[2] += x * 0.707f * duration;
|
2013-02-24 11:30:33 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 18:42:02 +00:00
|
|
|
//To calculate how much player will fall down
|
|
|
|
if(movement.mPosition[2] > mHighestPosition)
|
|
|
|
mHighestPosition = movement.mPosition[2];
|
|
|
|
|
2013-02-24 11:30:33 +00:00
|
|
|
//decrease fatigue by fFatigueJumpBase + (1 - normalizedEncumbrance) * fFatigueJumpMult;
|
|
|
|
}
|
2013-04-30 18:42:02 +00:00
|
|
|
else if(vec.z==0.0f && getState()==CharState_Jump)
|
|
|
|
{
|
|
|
|
float healthLost = cls.getFallDamage(mPtr, mHighestPosition);
|
|
|
|
|
|
|
|
if(healthLost>0.0f)
|
|
|
|
{
|
|
|
|
DynamicStat<float> health = cls.getCreatureStats(mPtr).getHealth();
|
|
|
|
int fatigue = MWWorld::Class::get (mPtr).getCreatureStats (mPtr).getFatigue().getBase();
|
|
|
|
int iHealth = health.getBase();
|
|
|
|
health.setBase (iHealth-(healthLost * (1 - (0.25 * fatigue))));
|
|
|
|
cls.getCreatureStats(mPtr).setHealth (health);
|
|
|
|
MWWorld::Class::get(mPtr).skillUsageSucceeded(mPtr, ESM::Skill::Acrobatics, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(CharState_Idle, true);
|
|
|
|
mHighestPosition=0;
|
|
|
|
}
|
2013-02-21 04:08:04 +00:00
|
|
|
|
2013-02-19 04:43:55 +00:00
|
|
|
if(std::abs(vec.x/2.0f) > std::abs(vec.y) && speed > 0.0f)
|
2013-02-18 14:29:16 +00:00
|
|
|
{
|
|
|
|
if(vec.x > 0.0f)
|
2013-03-06 15:58:56 +00:00
|
|
|
setState(inwater ? (isrunning ? CharState_SwimRunRight : CharState_SwimWalkRight)
|
|
|
|
: (sneak ? CharState_SneakRight : (isrunning ? CharState_RunRight : CharState_WalkRight)), true);
|
2013-02-18 14:29:16 +00:00
|
|
|
else if(vec.x < 0.0f)
|
2013-03-06 15:58:56 +00:00
|
|
|
setState(inwater ? (isrunning ? CharState_SwimRunLeft : CharState_SwimWalkLeft)
|
|
|
|
: (sneak ? CharState_SneakLeft : (isrunning ? CharState_RunLeft : CharState_WalkLeft)), true);
|
|
|
|
|
2013-04-25 05:45:43 +00:00
|
|
|
// If this animation isn't moving us sideways, do it manually
|
|
|
|
if(!mMovingAnim)
|
|
|
|
movement.mPosition[0] += vec.x * (speed*duration);
|
2013-02-18 14:29:16 +00:00
|
|
|
// Apply any forward/backward movement manually
|
2013-03-31 07:13:56 +00:00
|
|
|
movement.mPosition[1] += vec.y * (speed*duration);
|
2013-02-18 14:29:16 +00:00
|
|
|
}
|
2013-02-19 04:43:55 +00:00
|
|
|
else if(vec.y != 0.0f && speed > 0.0f)
|
2013-02-18 14:29:16 +00:00
|
|
|
{
|
|
|
|
if(vec.y > 0.0f)
|
2013-03-06 15:58:56 +00:00
|
|
|
setState(inwater ? (isrunning ? CharState_SwimRunForward : CharState_SwimWalkForward)
|
|
|
|
: (sneak ? CharState_SneakForward : (isrunning ? CharState_RunForward : CharState_WalkForward)), true);
|
2013-02-18 14:29:16 +00:00
|
|
|
else if(vec.y < 0.0f)
|
2013-03-06 15:58:56 +00:00
|
|
|
setState(inwater ? (isrunning ? CharState_SwimRunBack : CharState_SwimWalkBack)
|
|
|
|
: (sneak ? CharState_SneakBack : (isrunning ? CharState_RunBack : CharState_WalkBack)), true);
|
2013-04-24 13:32:11 +00:00
|
|
|
|
2013-02-18 14:29:16 +00:00
|
|
|
// Apply any sideways movement manually
|
2013-03-31 07:13:56 +00:00
|
|
|
movement.mPosition[0] += vec.x * (speed*duration);
|
2013-04-25 05:45:43 +00:00
|
|
|
// If this animation isn't moving us forward/backward, do it manually
|
|
|
|
if(!mMovingAnim)
|
|
|
|
movement.mPosition[1] += vec.y * (speed*duration);
|
2013-02-18 14:29:16 +00:00
|
|
|
}
|
2013-03-31 10:50:20 +00:00
|
|
|
else if(rot.z != 0.0f && !inwater && !sneak)
|
|
|
|
{
|
|
|
|
if(rot.z > 0.0f)
|
|
|
|
setState(CharState_TurnRight, true);
|
|
|
|
else if(rot.z < 0.0f)
|
|
|
|
setState(CharState_TurnLeft, true);
|
|
|
|
}
|
2013-04-24 13:32:11 +00:00
|
|
|
else if(getState() != CharState_SpecialIdle || !mAnimation->isPlaying(0))
|
2013-04-24 13:48:34 +00:00
|
|
|
{
|
2013-04-25 02:09:36 +00:00
|
|
|
if(mAnimQueue.size() == 0)
|
|
|
|
setState((inwater ? CharState_IdleSwim : (sneak ? CharState_IdleSneak : CharState_Idle)), true);
|
|
|
|
else
|
2013-04-24 13:48:34 +00:00
|
|
|
{
|
2013-04-25 13:12:37 +00:00
|
|
|
mMovingAnim = mAnimation->play(mAnimQueue.front().first,
|
|
|
|
"start", "stop", 0.0f,
|
|
|
|
mAnimQueue.front().second);
|
2013-04-24 13:48:34 +00:00
|
|
|
mAnimQueue.pop_front();
|
|
|
|
}
|
|
|
|
}
|
2013-03-31 08:29:24 +00:00
|
|
|
|
|
|
|
movement.mRotation[0] += rot.x * duration;
|
|
|
|
movement.mRotation[1] += rot.y * duration;
|
|
|
|
movement.mRotation[2] += rot.z * duration;
|
2013-02-03 15:15:34 +00:00
|
|
|
}
|
2013-02-03 07:39:43 +00:00
|
|
|
|
2013-01-17 05:25:50 +00:00
|
|
|
if(mAnimation && !mSkipAnim)
|
2013-02-04 15:10:14 +00:00
|
|
|
{
|
2013-02-15 10:15:39 +00:00
|
|
|
mAnimation->setSpeed(speed);
|
2013-03-31 07:13:56 +00:00
|
|
|
Ogre::Vector3 moved = mAnimation->runAnimation(duration);
|
|
|
|
movement.mPosition[0] += moved.x;
|
|
|
|
movement.mPosition[1] += moved.y;
|
|
|
|
movement.mPosition[2] += moved.z;
|
2013-02-04 15:10:14 +00:00
|
|
|
}
|
2013-01-17 05:25:50 +00:00
|
|
|
mSkipAnim = false;
|
2013-01-17 00:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-17 01:53:18 +00:00
|
|
|
void CharacterController::playGroup(const std::string &groupname, int mode, int count)
|
|
|
|
{
|
2013-01-21 11:24:52 +00:00
|
|
|
if(!mAnimation || !mAnimation->hasAnimation(groupname))
|
|
|
|
std::cerr<< "Animation "<<groupname<<" not found" <<std::endl;
|
|
|
|
else
|
2013-01-17 21:18:40 +00:00
|
|
|
{
|
|
|
|
count = std::max(count, 1);
|
2013-04-24 13:48:34 +00:00
|
|
|
if(mode != 0 || getState() != CharState_SpecialIdle)
|
2013-01-17 21:18:40 +00:00
|
|
|
{
|
|
|
|
mAnimQueue.clear();
|
2013-04-25 13:29:17 +00:00
|
|
|
mCharState = CharState_SpecialIdle;
|
|
|
|
mLooping = false;
|
2013-04-25 13:12:37 +00:00
|
|
|
mMovingAnim = mAnimation->play(groupname, ((mode==2) ? "loop start" : "start"), "stop", 0.0f, count-1);
|
2013-01-17 21:18:40 +00:00
|
|
|
}
|
|
|
|
else if(mode == 0)
|
|
|
|
{
|
2013-04-24 13:48:34 +00:00
|
|
|
mAnimQueue.clear();
|
2013-04-24 13:32:11 +00:00
|
|
|
mAnimQueue.push_back(std::make_pair(groupname, count-1));
|
2013-01-17 21:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-17 01:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CharacterController::skipAnim()
|
|
|
|
{
|
2013-01-17 05:25:50 +00:00
|
|
|
mSkipAnim = true;
|
2013-01-17 01:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-19 22:22:15 +00:00
|
|
|
void CharacterController::setState(CharacterState state, bool loop)
|
2013-01-16 18:45:18 +00:00
|
|
|
{
|
2013-04-25 13:29:17 +00:00
|
|
|
if(mCharState == state)
|
2013-01-21 11:24:52 +00:00
|
|
|
return;
|
2013-04-25 13:29:17 +00:00
|
|
|
mCharState = state;
|
|
|
|
mLooping = loop;
|
2013-01-16 23:00:06 +00:00
|
|
|
|
2013-04-25 14:08:11 +00:00
|
|
|
forceStateUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CharacterController::forceStateUpdate()
|
|
|
|
{
|
2013-01-20 05:55:04 +00:00
|
|
|
if(!mAnimation)
|
2013-01-16 23:00:06 +00:00
|
|
|
return;
|
2013-01-17 21:18:40 +00:00
|
|
|
mAnimQueue.clear();
|
2013-01-19 22:56:24 +00:00
|
|
|
|
2013-01-20 05:55:04 +00:00
|
|
|
std::string anim;
|
2013-04-25 13:29:17 +00:00
|
|
|
getStateInfo(mCharState, &anim);
|
2013-04-25 05:45:43 +00:00
|
|
|
if((mMovingAnim=mAnimation->hasAnimation(anim)) != false)
|
2013-04-25 13:29:17 +00:00
|
|
|
mMovingAnim = mAnimation->play(anim, "start", "stop", 0.0f, mLooping ? (~(size_t)0) : 0);
|
2013-01-16 18:45:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:35:24 +00:00
|
|
|
}
|