/* * Copyright (C) 2005-2009 MaNGOS * * Copyright (C) 2008-2009 Trinity * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * 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 * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef TRINITY_TRAVELLER_H #define TRINITY_TRAVELLER_H #include "Creature.h" #include "Player.h" #include /** Traveller is a wrapper for units (creatures or players) that * travel from point A to point B using the destination holder. */ #define PLAYER_FLIGHT_SPEED 32.0f template struct TRINITY_DLL_DECL Traveller { T &i_traveller; Traveller(T &t) : i_traveller(t) {} Traveller(const Traveller &obj) : i_traveller(obj) {} Traveller& operator=(const Traveller &obj) { this->~Traveller(); new (this) Traveller(obj); return *this; } operator T&(void) { return i_traveller; } operator const T&(void) { return i_traveller; } float GetPositionX() const { return i_traveller.GetPositionX(); } float GetPositionY() const { return i_traveller.GetPositionY(); } float GetPositionZ() const { return i_traveller.GetPositionZ(); } T& GetTraveller(void) { return i_traveller; } float Speed(void) { assert(false); return 0.0f; } float GetMoveDestinationTo(float x, float y, float z); uint32 GetTotalTrevelTimeTo(float x, float y, float z); void Relocation(float x, float y, float z, float orientation) {} void Relocation(float x, float y, float z) { Relocation(x, y, z, i_traveller.GetOrientation()); } void MoveTo(float x, float y, float z, uint32 t) {} }; template inline uint32 Traveller::GetTotalTrevelTimeTo(float x, float y, float z) { float dist = GetMoveDestinationTo(x,y,z); float speed = Speed();; if (speed <= 0.0f) return 0xfffffffe; // almost infinity-unit should stop else speed *= 0.001f; // speed is in seconds so convert from second to millisecond return static_cast(dist/speed); } // specialization for creatures template<> inline float Traveller::Speed() { if(i_traveller.hasUnitState(UNIT_STAT_CHARGING)) return i_traveller.m_TempSpeed; else if(i_traveller.HasUnitMovementFlag(MOVEMENTFLAG_WALK_MODE)) return i_traveller.GetSpeed(MOVE_WALK); else if(i_traveller.HasUnitMovementFlag(MOVEMENTFLAG_FLYING)) return i_traveller.GetSpeed(MOVE_FLIGHT); else return i_traveller.GetSpeed(MOVE_RUN); } template<> inline void Traveller::Relocation(float x, float y, float z, float orientation) { i_traveller.GetMap()->CreatureRelocation(&i_traveller, x, y, z, orientation); } template<> inline float Traveller::GetMoveDestinationTo(float x, float y, float z) { float dx = x - GetPositionX(); float dy = y - GetPositionY(); float dz = z - GetPositionZ(); //if(i_traveller.HasUnitMovementFlag(MOVEMENTFLAG_FLYING)) return sqrt((dx*dx) + (dy*dy) + (dz*dz)); //else //Walking on the ground // return sqrt((dx*dx) + (dy*dy)); } template<> inline void Traveller::MoveTo(float x, float y, float z, uint32 t) { //i_traveller.AI_SendMoveToPacket(x, y, z, t, i_traveller.GetUnitMovementFlags(), 0); i_traveller.SendMonsterMove(x, y, z, t); } // specialization for players template<> inline float Traveller::Speed() { if(i_traveller.hasUnitState(UNIT_STAT_CHARGING)) return i_traveller.m_TempSpeed; else if(i_traveller.isInFlight()) return PLAYER_FLIGHT_SPEED; else return i_traveller.GetSpeed(i_traveller.m_movementInfo.HasMovementFlag(MOVEMENTFLAG_WALK_MODE) ? MOVE_WALK : MOVE_RUN); } template<> inline float Traveller::GetMoveDestinationTo(float x, float y, float z) { float dx = x - GetPositionX(); float dy = y - GetPositionY(); float dz = z - GetPositionZ(); //if (i_traveller.isInFlight()) return sqrt((dx*dx) + (dy*dy) + (dz*dz)); //else //Walking on the ground // return sqrt((dx*dx) + (dy*dy)); } template<> inline void Traveller::Relocation(float x, float y, float z, float orientation) { i_traveller.GetMap()->PlayerRelocation(&i_traveller, x, y, z, orientation); } template<> inline void Traveller::MoveTo(float x, float y, float z, uint32 t) { //Only send MOVEMENTFLAG_WALK_MODE, client has strange issues with other move flags i_traveller.SendMonsterMove(x, y, z, t); } typedef Traveller CreatureTraveller; typedef Traveller PlayerTraveller; #endif