From 69c487abe51ac655b1f9cd4bc7388fb01e30fb0e Mon Sep 17 00:00:00 2001 From: megamage Date: Sat, 29 Aug 2009 17:14:47 -0500 Subject: *Handle creature rotation using movement generator. Please tell me if this breaks any script. --HG-- branch : trunk --- src/game/IdleMovementGenerator.cpp | 44 +++++++++++++++++++++++++++++++ src/game/IdleMovementGenerator.h | 16 ++++++++++++ src/game/MotionMaster.cpp | 5 ++++ src/game/MotionMaster.h | 10 ++++++- src/game/Unit.cpp | 53 ++------------------------------------ src/game/Unit.h | 32 ++++++----------------- 6 files changed, 84 insertions(+), 76 deletions(-) (limited to 'src/game') diff --git a/src/game/IdleMovementGenerator.cpp b/src/game/IdleMovementGenerator.cpp index 17e777db240..1809d8cd0ca 100644 --- a/src/game/IdleMovementGenerator.cpp +++ b/src/game/IdleMovementGenerator.cpp @@ -39,6 +39,50 @@ IdleMovementGenerator::Reset(Unit& owner) owner.StopMoving(); } +void RotateMovementGenerator::Initialize(Unit& owner) +{ + if(owner.hasUnitState(UNIT_STAT_MOVE)) + owner.StopMoving(); + + if(owner.getVictim()) + owner.SetInFront(owner.getVictim()); + + owner.addUnitState(UNIT_STAT_ROTATING); + + owner.AttackStop(); +} + +bool RotateMovementGenerator::Update(Unit& owner, const uint32& diff) +{ + float angle = owner.GetOrientation(); + if(m_direction == ROTATE_DIRECTION_LEFT) + { + angle += diff / m_duration * M_PI * 2; + if(angle >= M_PI * 2 ) angle = 0; + } + else + { + angle -= diff / m_duration * M_PI * 2; + if(angle < 0) angle = M_PI * 2; + } + owner.SetOrientation(angle); + owner.SendMovementFlagUpdate(); // this is a hack. we do not have anything correct to send in the beginning + + if(m_duration > diff) + m_duration -= diff; + else + return false; + + return true; +} + +void RotateMovementGenerator::Finalize(Unit &unit) +{ + unit.clearUnitState(UNIT_STAT_ROTATING); + if(unit.GetTypeId() == TYPEID_UNIT) + ((Creature*)&unit)->AI()->MovementInform(ROTATE_MOTION_TYPE, 0); +} + void DistractMovementGenerator::Initialize(Unit& owner) { diff --git a/src/game/IdleMovementGenerator.h b/src/game/IdleMovementGenerator.h index 2d5b7d94314..1f3f5e81bd4 100644 --- a/src/game/IdleMovementGenerator.h +++ b/src/game/IdleMovementGenerator.h @@ -36,6 +36,22 @@ class TRINITY_DLL_SPEC IdleMovementGenerator : public MovementGenerator extern IdleMovementGenerator si_idleMovement; +class TRINITY_DLL_SPEC RotateMovementGenerator : public MovementGenerator +{ + public: + explicit RotateMovementGenerator(uint32 time, RotateDirection direction) : m_duration(time), m_direction(direction) {} + + void Initialize(Unit& owner); + void Finalize(Unit& owner); + void Reset(Unit& owner) { Initialize(owner); } + bool Update(Unit& owner, const uint32& time_diff); + MovementGeneratorType GetMovementGeneratorType() { return ROTATE_MOTION_TYPE; } + + private: + uint32 m_duration; + RotateDirection m_direction; +}; + class TRINITY_DLL_SPEC DistractMovementGenerator : public MovementGenerator { public: diff --git a/src/game/MotionMaster.cpp b/src/game/MotionMaster.cpp index 3a7832fe914..b1168cac78d 100644 --- a/src/game/MotionMaster.cpp +++ b/src/game/MotionMaster.cpp @@ -511,6 +511,11 @@ void MotionMaster::MovePath(uint32 path_id, bool repeatable) i_owner->GetGUIDLow(), path_id, repeatable ? "YES" : "NO" ); } +void MotionMaster::MoveRotate(uint32 time, RotateDirection direction) +{ + Mutate(new RotateMovementGenerator(time, direction), MOTION_SLOT_ACTIVE); +} + void MotionMaster::propagateSpeedChange() { /*Impl::container_type::iterator it = Impl::c.begin(); diff --git a/src/game/MotionMaster.h b/src/game/MotionMaster.h index d23b6351955..197f0d07499 100644 --- a/src/game/MotionMaster.h +++ b/src/game/MotionMaster.h @@ -48,7 +48,8 @@ enum MovementGeneratorType ASSISTANCE_MOTION_TYPE= 11, // PointMovementGenerator.h (first part of flee for assistance) ASSISTANCE_DISTRACT_MOTION_TYPE = 12, // IdleMovementGenerator.h (second part of flee for assistance) TIMED_FLEEING_MOTION_TYPE = 13, // FleeingMovementGenerator.h (alt.second part of flee for assistance) - NULL_MOTION_TYPE = 14, + ROTATE_MOTION_TYPE = 14, + NULL_MOTION_TYPE = 15, }; enum MovementSlot @@ -66,6 +67,12 @@ enum MMCleanFlag MMCF_RESET = 2 // Flag if need top()->Reset() }; +enum RotateDirection +{ + ROTATE_DIRECTION_LEFT, + ROTATE_DIRECTION_RIGHT, +}; + // assume it is 25 yard per 0.6 second #define SPEED_CHARGE 42.0f @@ -152,6 +159,7 @@ class TRINITY_DLL_SPEC MotionMaster //: private std::stack void MoveTaxiFlight(uint32 path, uint32 pathnode); void MoveDistract(uint32 time); void MovePath(uint32 path_id, bool repeatable); + void MoveRotate(uint32 time, RotateDirection direction); MovementGeneratorType GetCurrentMovementGeneratorType() const; MovementGeneratorType GetMotionSlotType(int slot) const; diff --git a/src/game/Unit.cpp b/src/game/Unit.cpp index 29bb1c5bcad..7e4bdb6b88a 100644 --- a/src/game/Unit.cpp +++ b/src/game/Unit.cpp @@ -171,8 +171,6 @@ Unit::Unit() // remove aurastates allowing special moves for(uint8 i=0; i < MAX_REACTIVE; ++i) m_reactiveTimer[i] = 0; - - IsRotating = 0; } Unit::~Unit() @@ -256,10 +254,7 @@ void Unit::Update( uint32 p_time ) ModifyAuraState(AURA_STATE_HEALTHLESS_35_PERCENT, GetHealth() < GetMaxHealth()*0.35f); ModifyAuraState(AURA_STATE_HEALTH_ABOVE_75_PERCENT, GetHealth() > GetMaxHealth()*0.75f); - if(!IsUnitRotating()) - i_motionMaster.UpdateMotion(p_time); - else - AutoRotate(p_time); + i_motionMaster.UpdateMotion(p_time); } bool Unit::haveOffhandWeapon() const @@ -503,43 +498,6 @@ void Unit::GetRandomContactPoint( const Unit* obj, float &x, float &y, float &z, , GetAngle(obj) + (attacker_number ? (M_PI/2 - M_PI * rand_norm()) * (float)attacker_number / combat_reach * 0.3 : 0)); } -void Unit::StartAutoRotate(uint8 type, uint32 fulltime) -{ - if(getVictim()) - RotateAngle = GetAngle(getVictim()); - else - RotateAngle = GetOrientation(); - RotateTimer = fulltime; - RotateTimerFull = fulltime; - IsRotating = type; - LastTargetGUID = GetUInt64Value(UNIT_FIELD_TARGET); - SetUInt64Value(UNIT_FIELD_TARGET, 0); -} - -void Unit::AutoRotate(uint32 time) -{ - if(!IsRotating)return; - if(IsRotating == CREATURE_ROTATE_LEFT) - { - RotateAngle += (double)time/RotateTimerFull*(double)M_PI*2; - if (RotateAngle >= M_PI*2)RotateAngle = 0; - } - else - { - RotateAngle -= (double)time/RotateTimerFull*(double)M_PI*2; - if (RotateAngle < 0)RotateAngle = M_PI*2; - } - SetOrientation(RotateAngle); - StopMoving(); - if(RotateTimer <= time) - { - IsRotating = CREATURE_ROTATE_NONE; - RotateAngle = 0; - RotateTimer = RotateTimerFull; - SetUInt64Value(UNIT_FIELD_TARGET, LastTargetGUID); - }else RotateTimer -= time; -} - void Unit::RemoveMovementImpairingAuras() { RemoveAurasWithMechanic((1<