Skip to content

Community Lua Functions

King Pendragon edited this page Jul 20, 2024 · 43 revisions

Hey! If you're reading this you're probably looking for a place that a lot of commonly used functions that we all steal borrow of each other. (All in good fun). Note you don't HAVE to use these, and if you have some way to improve them, please feel free to add to it!

MoveTo

This function makes the player fly or walk to a specified location, ensuring the navmesh is ready before proceeding. takes an X, Y, Z, Stop Distance, and Boolean (True/False)

-- needs vnavmesh
  function MoveTo(valuex, valuey, valuez, stopdistance, FlyOrWalk)
      function MeshCheck()
          function Truncate1Dp(num)
              return truncate and ("%.1f"):format(num) or num
          end
          local was_ready = NavIsReady()
          if not NavIsReady() then
              while not NavIsReady() do
                  LogInfo("[Debug]Building navmesh, currently at " .. Truncate1Dp(NavBuildProgress() * 100) .. "%")
                  yield("/wait 1")
                  local was_ready = NavIsReady()
                  if was_ready then
                      LogInfo("[Debug]Navmesh ready!")
                  end
              end
          else
              LogInfo("[Debug]Navmesh ready!")
          end
      end
      MeshCheck()
      if FlyOrWalk then
          while GetCharacterCondition(4, false) do
              yield("/wait 0.1")
              if GetCharacterCondition(27) then
                  yield("/wait 2")
              else
                  yield('/gaction "mount roulette"')
              end
          end
          if HasFlightUnlocked(GetZoneID()) then
              PathfindAndMoveTo(valuex, valuey, valuez, true) -- flying
          else
              LogInfo("[MoveTo] Can't fly trying to walk.")
              PathfindAndMoveTo(valuex, valuey, valuez, false) -- walking
          end
      else
          PathfindAndMoveTo(valuex, valuey, valuez, false) -- walking
      end
      while ((PathIsRunning() or PathfindInProgress()) and GetDistanceToPoint(valuex, valuey, valuez) > stopdistance) do
          yield("/wait 0.3")
      end
      PathStop()
      LogInfo("[MoveTo] Completed")
  end

example

MoveTo(-242.07663, -43.815063, 665.1687, 1, true) --Flies to Coordinates
MoveTo(-162.892, 0.922, -30.488, 3, false) --Walks to coordinates
setSNDProperty

Use these Functions to Set or Unset SND Properties

--[[
All the settings you can edit with SetSNDProperty()

CraftLoopFromRecipeNote -- bool
CraftLoopMaxWait -- int
CraftLoopEcho -- bool
MaxTimeoutRetries -- int
NoisyErrors -- bool
BeepFrequency -- int
BeepDuration -- int
BeepCount -- int
UseSNDTargeting -- bool
UseItemStructsVersion -- bool
StopMacroIfActionTimeout -- bool
StopMacroIfItemNotFound -- bool
StopMacroIfCantUseItem -- bool
StopMacroIfTargetNotFound -- bool
StopMacroIfAddonNotFound -- bool
StopMacroIfAddonNotVisible -- bool

--]]

-- Function to set or unset the property
function setSNDProperty(propertyName, value)
  local currentValue = GetSNDProperty(propertyName)
  if currentValue ~= value then
      SetSNDProperty(propertyName, tostring(value))
      LogInfo("[SetSNDProperty] " .. propertyName .. " set to " .. tostring(value))
  end
end

Example usage

setSNDProperty("UseItemStructsVersion", true)
setSNDProperty("UseSNDTargeting", true)
setSNDProperty("StopMacroIfTargetNotFound", false)
setSNDProperty("StopMacroIfCantUseItem", false)
setSNDProperty("StopMacroIfItemNotFound", false)
setSNDProperty("StopMacroIfAddonNotFound", false)
PlayerTest

Waits Until Player is Ready

  -- This function will wait until player is ready can be used in map changes, menu exit waits ex.
  function PlayerTest()
      repeat
          yield("/wait 0.5")
      until IsPlayerAvailable()
  end
NomNomDelish

Eats Food if you don't have any

function NomNomDelish(FoodKind, StopIfNoFood)
  -- Check if the food effect is not active and if using food is enabled
  if not HasStatusId(48) and UseFood then
      while not HasStatusId(48) and UseFood do
          yield("/item " .. FoodKind)
          yield("/wait 2")
          
          -- Check if there was an error indicating no food remaining
          if GetNodeText("_TextError", 1) == "You do not have that item." and IsAddonVisible("_TextError") then
              UseFood = false
              LogInfo("[FoodCheck] No food remaining, setting UseFood to false")
              
              -- Stop the script if the flag is set
              if StopIfNoFood then
                  LogInfo("[FoodCheck] StopIfNoFood is true, stopping the script")
                  yield("/snd stop")
              end
              
              break
          end
      end
      LogInfo("[FoodCheck] Completed")
  else
      LogInfo("[FoodCheck] Already has food status or UseFood is false")
  end
end

Example usage

NomNomDelish("Vegetable Soup <HQ>", false) --make sure to have the name of the food IN the "" and <HQ> for HQ food,  if set to True will stop script when food runs out.

Useful Code

Get Target name

Gets Targets name and returns it to chat

yield("Target Name: "..tostring(GetTargetName()))
Current Zone ID

Gets the Current Zone ID and returns it to chat.

yield("InZone: " .. tostring(GetZoneID()))
Target Position

Gets the Current Targets X Y Z and returns it to chat.

yield("/e ------------------------------------------------------")
x = ("%.1f"):format(GetTargetRawXPos())
y = ("%.1f"):format(GetTargetRawYPos())
z = ("%.1f"):format(GetTargetRawZPos())
yield("/e Target Position: {"..x..","..y..","..z.."}")
yield("/e ------------------------------------------------------")
Players Position

Gets the Players X Y Z and returns it to chat.

yield("/e ------------------------------------------------------")
x = ("%.1f"):format(GetPlayerRawXPos())
y = ("%.1f"):format(GetPlayerRawYPos())
z = ("%.1f"):format(GetPlayerRawZPos())
yield("/e Current Position: {"..x..","..y..","..z.."}")
yield("/e ------------------------------------------------------")