root/trunk/src/game/TargetedMovementGenerator.cpp @ 2

Revision 2, 7.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#include "ByteBuffer.h"
20#include "TargetedMovementGenerator.h"
21#include "Errors.h"
22#include "Creature.h"
23#include "MapManager.h"
24#include "DestinationHolderImp.h"
25#include "World.h"
26
27#define SMALL_ALPHA 0.05f
28
29#include <cmath>
30/*
31struct StackCleaner
32{
33    Creature &i_creature;
34    StackCleaner(Creature &creature) : i_creature(creature) {}
35    void Done(void) { i_creature.StopMoving(); }
36    ~StackCleaner()
37    {
38        i_creature->Clear();
39    }
40};
41*/
42
43template<class T>
44void
45TargetedMovementGenerator<T>::_setTargetLocation(T &owner)
46{
47    if( !i_target.isValid() || !&owner )
48        return;
49
50    if( owner.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNDED | UNIT_STAT_DISTRACTED) )
51        return;
52
53    // prevent redundant micro-movement for pets, other followers.
54    if(i_offset && i_target->IsWithinDistInMap(&owner,2*i_offset))
55        return;
56
57    float x, y, z;
58    if(!i_offset)
59    {
60        // to nearest contact position
61        i_target->GetContactPoint( &owner, x, y, z );
62    }
63    else
64    {
65        // to at i_offset distance from target and i_angle from target facing
66        i_target->GetClosePoint(x,y,z,owner.GetObjectSize(),i_offset,i_angle);
67    }
68
69    /*
70        We MUST not check the distance difference and avoid setting the new location for smaller distances.
71        By that we risk having far too many GetContactPoint() calls freezing the whole system.
72        In TargetedMovementGenerator<T>::Update() we check the distance to the target and at
73        some range we calculate a new position. The calculation takes some processor cycles due to vmaps.
74        If the distance to the target it too large to ignore,
75        but the distance to the new contact point is short enough to be ignored,
76        we will calculate a new contact point each update loop, but will never move to it.
77        The system will freeze.
78        ralf
79
80        //We don't update Mob Movement, if the difference between New destination and last destination is < BothObjectSize
81        float  bothObjectSize = i_target->GetObjectSize() + owner.GetObjectSize() + CONTACT_DISTANCE;
82        if( i_destinationHolder.HasDestination() && i_destinationHolder.GetDestinationDiff(x,y,z) < bothObjectSize )
83            return;
84    */
85    Traveller<T> traveller(owner);
86    i_destinationHolder.SetDestination(traveller, x, y, z);
87    owner.addUnitState(UNIT_STAT_CHASE);
88}
89
90template<class T>
91void
92TargetedMovementGenerator<T>::Initialize(T &owner)
93{
94    if(!&owner)
95        return;
96    owner.RemoveUnitMovementFlag(MOVEMENTFLAG_WALK_MODE);
97    _setTargetLocation(owner);
98}
99
100template<class T>
101void
102TargetedMovementGenerator<T>::Finalize(T &owner)
103{
104    owner.clearUnitState(UNIT_STAT_CHASE);
105}
106
107template<class T>
108void
109TargetedMovementGenerator<T>::Reset(T &owner)
110{
111    Initialize(owner);
112}
113
114template<class T>
115bool
116TargetedMovementGenerator<T>::Update(T &owner, const uint32 & time_diff)
117{
118    if(!i_target.isValid())
119        return false;
120
121    if( !&owner || !owner.isAlive())
122        return true;
123
124    if( owner.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNDED | UNIT_STAT_FLEEING | UNIT_STAT_DISTRACTED) )
125        return true;
126
127    // prevent movement while casting spells with cast time or channel time
128    if ( owner.IsNonMeleeSpellCasted(false, false,  true))
129    {
130        if (!owner.IsStopped())
131            owner.StopMoving();
132        return true;
133    }
134
135    // prevent crash after creature killed pet
136    if (!owner.hasUnitState(UNIT_STAT_FOLLOW) && owner.getVictim() != i_target.getTarget())
137        return true;
138
139    Traveller<T> traveller(owner);
140
141    if( !i_destinationHolder.HasDestination() )
142        _setTargetLocation(owner);
143    if( owner.IsStopped() && !i_destinationHolder.HasArrived() )
144    {
145        owner.addUnitState(UNIT_STAT_CHASE);
146        i_destinationHolder.StartTravel(traveller);
147        return true;
148    }
149
150    if (i_destinationHolder.UpdateTraveller(traveller, time_diff, false))
151    {
152        // put targeted movement generators on a higher priority
153        if (owner.GetObjectSize())
154            i_destinationHolder.ResetUpdate(50);
155
156        float dist = i_target->GetObjectSize() + owner.GetObjectSize() + sWorld.getRate(RATE_TARGET_POS_RECALCULATION_RANGE);
157
158        //More distance let have better performance, less distance let have more sensitive reaction at target move.
159
160        // try to counter precision differences
161        if( i_destinationHolder.GetDistance2dFromDestSq(*i_target.getTarget()) >= dist * dist)
162        {
163            owner.SetInFront(i_target.getTarget());         // Set new Angle For Map::
164            _setTargetLocation(owner);                      //Calculate New Dest and Send data To Player
165        }
166        // Update the Angle of the target only for Map::, no need to send packet for player
167        else if ( !i_angle && !owner.HasInArc( 0.01f, i_target.getTarget() ) )
168            owner.SetInFront(i_target.getTarget());
169
170        if(( owner.IsStopped() && !i_destinationHolder.HasArrived() ) || i_recalculateTravel )
171        {
172            i_recalculateTravel = false;
173            //Angle update will take place into owner.StopMoving()
174            owner.SetInFront(i_target.getTarget());
175
176            owner.StopMoving();
177            if(owner.canReachWithAttack(i_target.getTarget()) && !owner.hasUnitState(UNIT_STAT_FOLLOW))
178                owner.Attack(i_target.getTarget(),true);
179        }
180    }
181    return true;
182}
183
184template<class T>
185Unit*
186TargetedMovementGenerator<T>::GetTarget() const
187{
188    return i_target.getTarget();
189}
190
191template void TargetedMovementGenerator<Player>::_setTargetLocation(Player &);
192template void TargetedMovementGenerator<Creature>::_setTargetLocation(Creature &);
193template void TargetedMovementGenerator<Player>::Initialize(Player &);
194template void TargetedMovementGenerator<Creature>::Initialize(Creature &);
195template void TargetedMovementGenerator<Player>::Finalize(Player &);
196template void TargetedMovementGenerator<Creature>::Finalize(Creature &);
197template void TargetedMovementGenerator<Player>::Reset(Player &);
198template void TargetedMovementGenerator<Creature>::Reset(Creature &);
199template bool TargetedMovementGenerator<Player>::Update(Player &, const uint32 &);
200template bool TargetedMovementGenerator<Creature>::Update(Creature &, const uint32 &);
201template Unit* TargetedMovementGenerator<Player>::GetTarget() const;
202template Unit* TargetedMovementGenerator<Creature>::GetTarget() const;
Note: See TracBrowser for help on using the browser.