From 8f66527e02b1cb7b81ac5fc154958fde935a2896 Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Tue, 6 Mar 2018 23:03:55 -0500 Subject: [PATCH] Add TryMoveAlong(distance, angle) This will take a distance you would like to move and an EQ angle you would like to move relative to current heading (so 0 is in front, 256 behind) Exported to Lua as well --- zone/lua_mob.cpp | 12 ++++++++++++ zone/lua_mob.h | 2 ++ zone/mob.h | 1 + zone/waypoints.cpp | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/zone/lua_mob.cpp b/zone/lua_mob.cpp index bd5cddbcc..3793e6b9d 100644 --- a/zone/lua_mob.cpp +++ b/zone/lua_mob.cpp @@ -282,6 +282,16 @@ void Lua_Mob::GMMove(double x, double y, double z, double heading, bool send_upd self->GMMove(static_cast(x), static_cast(y), static_cast(z), static_cast(heading), send_update); } +void Lua_Mob::TryMoveAlong(float distance, float angle) { + Lua_Safe_Call_Void(); + self->TryMoveAlong(distance, angle); +} + +void Lua_Mob::TryMoveAlong(float distance, float angle, bool send) { + Lua_Safe_Call_Void(); + self->TryMoveAlong(distance, angle, send); +} + bool Lua_Mob::HasProcs() { Lua_Safe_Call_Bool(); return self->HasProcs(); @@ -2197,6 +2207,8 @@ luabind::scope lua_register_mob() { .def("GMMove", (void(Lua_Mob::*)(double,double,double))&Lua_Mob::GMMove) .def("GMMove", (void(Lua_Mob::*)(double,double,double,double))&Lua_Mob::GMMove) .def("GMMove", (void(Lua_Mob::*)(double,double,double,double,bool))&Lua_Mob::GMMove) + .def("TryMoveAlong", (void(Lua_Mob::*)(float,float))&Lua_Mob::TryMoveAlong) + .def("TryMoveAlong", (void(Lua_Mob::*)(float,float,bool))&Lua_Mob::TryMoveAlong) .def("HasProcs", &Lua_Mob::HasProcs) .def("IsInvisible", (bool(Lua_Mob::*)(void))&Lua_Mob::IsInvisible) .def("IsInvisible", (bool(Lua_Mob::*)(Lua_Mob))&Lua_Mob::IsInvisible) diff --git a/zone/lua_mob.h b/zone/lua_mob.h index 7aeb700e6..17dbc5c43 100644 --- a/zone/lua_mob.h +++ b/zone/lua_mob.h @@ -74,6 +74,8 @@ public: void GMMove(double x, double y, double z); void GMMove(double x, double y, double z, double heading); void GMMove(double x, double y, double z, double heading, bool send_update); + void TryMoveAlong(float distance, float heading); + void TryMoveAlong(float distance, float heading, bool send); bool HasProcs(); bool IsInvisible(); bool IsInvisible(Lua_Mob other); diff --git a/zone/mob.h b/zone/mob.h index 23c0004df..d19f4299c 100644 --- a/zone/mob.h +++ b/zone/mob.h @@ -578,6 +578,7 @@ public: void SetFlyMode(uint8 flymode); inline void Teleport(glm::vec3 NewPosition) { m_Position.x = NewPosition.x; m_Position.y = NewPosition.y; m_Position.z = NewPosition.z; }; + void TryMoveAlong(float distance, float angle, bool send = true); //AI static uint32 GetLevelCon(uint8 mylevel, uint8 iOtherLevel); diff --git a/zone/waypoints.cpp b/zone/waypoints.cpp index 45d199277..6cf74bb65 100644 --- a/zone/waypoints.cpp +++ b/zone/waypoints.cpp @@ -29,10 +29,13 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "npc.h" #include "quest_parser_collection.h" #include "water_map.h" +#include "fastmath.h" #include #include +extern FastMath g_Math; + struct wp_distance { float dist; @@ -990,6 +993,35 @@ float Mob::GetZOffset() const { return 0.2 * GetSize() * offset; } +// This function will try to move the mob along the relative angle a set distance +// if it can't be moved, it will lower the distance and try again +// If we want to move on like say a spawn, we can pass send as false +void Mob::TryMoveAlong(float distance, float angle, bool send) +{ + angle += GetHeading(); + angle = FixHeading(angle); + + glm::vec3 tmp_pos; + glm::vec3 new_pos = GetPosition(); + new_pos.x += distance * g_Math.FastSin(angle); + new_pos.y += distance * g_Math.FastCos(angle); + new_pos.z += GetZOffset(); + + if (zone->HasMap()) { + auto new_z = zone->zonemap->FindClosestZ(new_pos, nullptr); + if (new_z != BEST_Z_INVALID) + new_pos.z = new_z; + + if (zone->zonemap->FindClosestLoS(GetPosition(), new_pos, tmp_pos)) + new_pos = tmp_pos; + } + + new_pos.z = GetFixedZ(new_pos); + Teleport(new_pos); + if (send) + SendPositionUpdate(); +} + int ZoneDatabase::GetHighestGrid(uint32 zoneid) { std::string query = StringFormat("SELECT COALESCE(MAX(id), 0) FROM grid WHERE zoneid = %i", zoneid);