From 35c4867334a9e69d3d7693e26669fbf395d7c642 Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Sun, 4 Mar 2018 02:19:12 -0500 Subject: [PATCH] Add the Fast trig functions that work with EQ headings This should match pretty close to the clients LUTs Also fixed a bug with push --- zone/CMakeLists.txt | 2 ++ zone/attack.cpp | 8 +++++--- zone/fastmath.cpp | 32 ++++++++++++++++++++++++++++++++ zone/fastmath.h | 18 ++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 zone/fastmath.cpp create mode 100644 zone/fastmath.h diff --git a/zone/CMakeLists.txt b/zone/CMakeLists.txt index 49644cfcf..b10ab32a0 100644 --- a/zone/CMakeLists.txt +++ b/zone/CMakeLists.txt @@ -28,6 +28,7 @@ SET(zone_sources encounter.cpp entity.cpp exp.cpp + fastmath.cpp fearpath.cpp forage.cpp groups.cpp @@ -158,6 +159,7 @@ SET(zone_headers entity.h errmsg.h event_codes.h + fastmath.h forage.h global_loot_manager.h groups.h diff --git a/zone/attack.cpp b/zone/attack.cpp index 1ae42b168..90a774550 100644 --- a/zone/attack.cpp +++ b/zone/attack.cpp @@ -32,6 +32,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "worldserver.h" #include "zone.h" #include "lua_parser.h" +#include "fastmath.h" #include #include @@ -43,6 +44,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA extern QueryServ* QServ; extern WorldServer worldserver; +extern FastMath g_Math; #ifdef _WINDOWS #define snprintf _snprintf @@ -3624,9 +3626,9 @@ void Mob::CommonDamage(Mob* attacker, int &damage, const uint16 spell_id, const // update NPC stuff if (a->force != 0.0f) { auto new_pos = glm::vec3( - m_Position.x + (a->force * std::cos(a->hit_heading) + m_Delta.x), - m_Position.y + (a->force * std::sin(a->hit_heading) + m_Delta.y), m_Position.z); - if ((IsNPC() && position_update_melee_push_timer.Check()) && zone->zonemap && + m_Position.x + (a->force * g_Math.FastSin(a->hit_heading) + m_Delta.x), + m_Position.y + (a->force * g_Math.FastCos(a->hit_heading) + m_Delta.y), m_Position.z); + if ((!IsNPC() || position_update_melee_push_timer.Check()) && zone->zonemap && zone->zonemap->CheckLoS( glm::vec3(m_Position), new_pos)) { // If we have LoS on the new loc it should be reachable. diff --git a/zone/fastmath.cpp b/zone/fastmath.cpp new file mode 100644 index 000000000..fb2cef193 --- /dev/null +++ b/zone/fastmath.cpp @@ -0,0 +1,32 @@ +#include +#include "fastmath.h" + +FastMath g_Math; + +// This should match EQ's sin/cos LUTs +// Some values didn't match on linux, but they were the "same" float :P +FastMath::FastMath() +{ + int ci = 0; + int si = 128; + float res; + do { + res = std::cos(static_cast(ci) * M_PI * 2 / 512); + lut_cos[ci] = res; + if (si == 512) + si = 0; + lut_sin[si] = res; + ++ci; + ++si; + } while (ci < 512); + + lut_sin[0] = 0.0f; + lut_sin[128] = 1.0f; + lut_sin[256] = 0.0f; + lut_sin[384] = -1.0f; + + lut_cos[0] = 1.0f; + lut_cos[128] = 0.0f; + lut_cos[384] = 0.0f; +} + diff --git a/zone/fastmath.h b/zone/fastmath.h new file mode 100644 index 000000000..083198e48 --- /dev/null +++ b/zone/fastmath.h @@ -0,0 +1,18 @@ +#ifndef FASTMATH_H +#define FASTMATH_H + +class FastMath +{ +private: + float lut_cos[512]; + float lut_sin[512]; + +public: + FastMath(); + + inline float FastSin(float a) { return lut_sin[static_cast(a) & 0x1ff]; } + inline float FastCos(float a) { return lut_cos[static_cast(a) & 0x1ff]; } + +}; + +#endif /* !FASTMATH_H */