Skip to content

Commit

Permalink
adding dynamic riding support
Browse files Browse the repository at this point in the history
  • Loading branch information
rbgdevx committed Sep 8, 2024
1 parent c0b8c8e commit c962fb1
Show file tree
Hide file tree
Showing 29 changed files with 795 additions and 2,374 deletions.
19 changes: 17 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
"InterfaceOptionsFrame_OpenToCategory",
"NORMAL_FONT_COLOR_CODE",
"SlashCmdList",
"hash_SlashCmdList"
]
"hash_SlashCmdList",
"FrameUtil",
"ButtonFrameTemplate_HidePortrait"
],
"Lua.runtime.version": "Lua 5.1",
"Lua.runtime.builtin": {
"basic": "disable",
"debug": "disable",
"io": "disable",
"math": "disable",
"os": "disable",
"package": "disable",
"string": "disable",
"table": "disable",
"utf8": "disable"
},
"Lua.workspace.library": ["~\\.vscode\\extensions\\ketho.wow-api-0.17.6\\Annotations"]
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Dynamic Movement Speed

## [v1.2.0](https://github.com/rbgdevx/dynamic-movement-speed/releases/tag/v1.2.0) (2024-09-08)

- Adding Dynamic Riding support
- Fixing bugs
- Refactor lib usage to match what im doing on BGWC addon to reduce needed libs and global db issues
- Made it so you can't open the settings when locked and right clicking the text
- Minor random changes based on learning from BGWC

## [v1.1.7](https://github.com/rbgdevx/dynamic-movement-speed/releases/tag/v1.1.7) (2024-05-08)

- Fixing speed not updating at times
Expand Down
187 changes: 169 additions & 18 deletions DynamicMovementSpeed.lua
Original file line number Diff line number Diff line change
@@ -1,43 +1,169 @@
local AddonName, NS = ...
local _, NS = ...

local Interface = NS.Interface

local CreateFrame = CreateFrame
local IsPlayerMoving = IsPlayerMoving
local LibStub = LibStub
local GetTime = GetTime
local IsFlying = IsFlying

local mmin = math.min
local mmax = math.max
local sformat = string.format

local After = C_Timer.After
local GetGlidingInfo = C_PlayerInfo.GetGlidingInfo
local GetPlayerAuraBySpellID = C_UnitAuras.GetPlayerAuraBySpellID

---@type DMS
local DMS = NS.DMS
local DMSFrame = NS.DMS.frame

local isDragonRiding = false
local ascentSpell = 372610
local lastUpdatedSpeed = 0
local ascentStart = 0
local dynamicSpeed = 0
local speedtext = ""

local MAIN_EVENTS = {
"PLAYER_ENTERING_WORLD",
"PLAYER_MOUNT_DISPLAY_CHANGED",
"UNIT_POWER_BAR_SHOW",
"UNIT_POWER_BAR_HIDE",
"UNIT_SPELLCAST_SUCCEEDED",
}

-- Dragon Riding
do
local thrillBuff = 377234
local lastT = 0
local samples = 0
local smoothSpeed, lastSpeed = 0, 0
local smoothAccel = 0
-- local lastAccel = 0
local speedshowunits = true

local maxSamples = 5
local ascentDuration = 3.5
local updatePeriod = 1 / 10
local speedunits = 2

local speedTextFormat, speedTextFactor = "", 1
if speedunits == 1 then
speedTextFormat = speedshowunits and "%.1fyd/s" or "%.1f"
else
speedTextFormat = speedshowunits and "%.0f%%" or "%.0f"
speedTextFactor = 100 / 7
end

local isThrill = false
local isBoosting = false

DMS = LibStub("AceAddon-3.0"):NewAddon("DMS", "AceEvent-3.0", "AceConsole-3.0")
function DMS:GetDynamicSpeed()
local time = GetTime()

-- Delta time
local dt = time - lastT
if dt < updatePeriod then
-- Rate limit speed updates!
return false
end
lastT = time

-- Get flying speed
local _, _, forwardSpeed = GetGlidingInfo()
local speed = forwardSpeed

local thrill = GetPlayerAuraBySpellID(thrillBuff)
local boosting = thrill and time < ascentStart + ascentDuration or false

-- Compute smooth speed
samples = mmin(maxSamples, samples + 1)
local lastWeight = (samples - 1) / samples
local newWeight = 1 / samples
smoothSpeed = speed
local newAccel = smoothSpeed - lastSpeed
lastSpeed = smoothSpeed

-- Compute smooth acceleration
smoothAccel = smoothAccel * lastWeight + newAccel * newWeight
if speed > 63 then
-- Don't track negative acceleration when boosting
smoothAccel = mmax(0, smoothAccel)
end
if not IsFlying() then
smoothAccel = 0 -- Don't track acceleration on ground
end
-- lastAccel = smoothAccel
if NS.db.global.debug then
print("smoothAccel", smoothAccel)
end

-- Update display variables
isBoosting = boosting
isThrill = not not thrill
if NS.db.global.debug then
print("isBoosting", isBoosting)
print("isThrill", isThrill)
end

dynamicSpeed = smoothSpeed * speedTextFactor
speedtext = smoothSpeed < 1 and "" or sformat(speedTextFormat, dynamicSpeed)
if NS.db.global.debug then
print("dynamicSpeed", dynamicSpeed)
print("speedtext", speedtext)
end
end
end

-- Player Moving
do
--- @class PlayerMovingFrame
--- @field moving integer|nil
--- @field moving boolean|nil
--- @field speed integer|nil

--- @type PlayerMovingFrame|Frame|nil
local playerMovingFrame = nil

local function PlayerMoveUpdate()
local moving = IsPlayerMoving()

if playerMovingFrame and (playerMovingFrame.moving ~= moving or playerMovingFrame.moving == nil) then
playerMovingFrame.moving = moving
end

local currentSpeed, runSpeed = NS.GetSpeedInfo()

if playerMovingFrame and playerMovingFrame.speed ~= currentSpeed then
playerMovingFrame.speed = currentSpeed
local correctSpeed = currentSpeed

if moving and IsFlying() and isDragonRiding then
DMS:GetDynamicSpeed()
correctSpeed = dynamicSpeed
end

if playerMovingFrame and playerMovingFrame.speed ~= correctSpeed then
playerMovingFrame.speed = correctSpeed

local speedPercent = playerMovingFrame.speed

if playerMovingFrame.moving or currentSpeed > 0 then
speedPercent = currentSpeed
if NS.db.global.debug then
print("moving", moving, "flying", IsFlying(), "isDragonRiding", isDragonRiding)
end

if playerMovingFrame.moving or correctSpeed > 0 then
local showSpeed = correctSpeed
if not IsFlying() then
showSpeed = currentSpeed == 0 and runSpeed or currentSpeed
end

speedPercent = showSpeed
else
speedPercent = runSpeed
end

Interface.speed = speedPercent
NS.UpdateText(Interface.text, speedPercent)
NS.UpdateText(Interface.text, speedPercent, isDragonRiding and IsFlying())
end
end

Expand All @@ -60,22 +186,47 @@ do
end

function DMS:PLAYER_ENTERING_WORLD()
isDragonRiding = NS.IsDragonriding()
self:WatchForPlayerMoving()
end

local function checkSpeed()
local _, runSpeed = NS.GetSpeedInfo()
if runSpeed == lastUpdatedSpeed then
After(0.1, checkSpeed)
else
isDragonRiding = NS.IsDragonriding()
DMS:WatchForPlayerMoving()
end
end

function DMS:PLAYER_MOUNT_DISPLAY_CHANGED()
local _, runSpeed = NS.GetSpeedInfo()
lastUpdatedSpeed = runSpeed
After(0, checkSpeed)
end

function DMS:UNIT_POWER_BAR_SHOW()
isDragonRiding = NS.IsDragonriding()
self:WatchForPlayerMoving()
end

function DMS:SlashCommands(_)
LibStub("AceConfigDialog-3.0"):Open(AddonName)
function DMS:UNIT_POWER_BAR_HIDE()
isDragonRiding = NS.IsDragonriding()
self:WatchForPlayerMoving()
end

function DMS:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("DMSDB", NS.DefaultDatabase, true)
self:SetupOptions()
self:RegisterChatCommand(AddonName, "SlashCommands")
self:RegisterChatCommand("dms", "SlashCommands")
function DMS:UNIT_SPELLCAST_SUCCEEDED(unitTarget, _, spellID)
if unitTarget == "player" and spellID == ascentSpell then
ascentStart = GetTime()
end
end

function DMS:OnEnable()
function DMS:PLAYER_LOGIN()
DMSFrame:UnregisterEvent("PLAYER_LOGIN")

Interface:CreateInterface()

self:RegisterEvent("PLAYER_ENTERING_WORLD")
FrameUtil.RegisterFrameForEvents(DMSFrame, MAIN_EVENTS)
end
DMSFrame:RegisterEvent("PLAYER_LOGIN")
7 changes: 3 additions & 4 deletions DynamicMovementSpeed.toc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
## Interface: 100207
## Interface: 110002
## Title: DynamicMovementSpeed
## Version: 1.1.7
## Author: Ajax
## Version: 1.2.0
## Author: RBGDEV
## Notes: Provides real time movement speed
## OptionalDeps: Ace3, LibStub, LibSharedMedia-3.0, AceGUI-3.0-SharedMediaWidgets
## IconTexture: Interface\AddOns\DynamicMovementSpeed\logo.tga
## DefaultState: Enabled
## SavedVariables: DMSDB
## X-Category: Miscellaneous
## X-Credits: Ajax
Expand Down
Loading

0 comments on commit c962fb1

Please sign in to comment.