setDefaultTab("Main")

if type(storage.autoTargetHP) ~= "table" then
  storage.autoTargetHP = {
    plus = false,
    minus = false
  }
end

local autoTargetUi = setupUI([[
Panel
  height: 40

  BotSwitch
    id: plus
    anchors.top: parent.top
    anchors.left: parent.left
    anchors.right: parent.right
    !text: tr('Auto Targer HP +')

  BotSwitch
    id: minus
    anchors.top: prev.bottom
    anchors.left: parent.left
    anchors.right: parent.right
    margin-top: 2
    !text: tr('Auto Targer HP -')
]])

autoTargetUi.plus:setOn(storage.autoTargetHP.plus)
autoTargetUi.minus:setOn(storage.autoTargetHP.minus)

autoTargetUi.plus.onClick = function()
  storage.autoTargetHP.plus = not storage.autoTargetHP.plus

  if storage.autoTargetHP.plus then
    storage.autoTargetHP.minus = false
  end

  autoTargetUi.plus:setOn(storage.autoTargetHP.plus)
  autoTargetUi.minus:setOn(storage.autoTargetHP.minus)
end

autoTargetUi.minus.onClick = function()
  storage.autoTargetHP.minus = not storage.autoTargetHP.minus

  if storage.autoTargetHP.minus then
    storage.autoTargetHP.plus = false
  end

  autoTargetUi.plus:setOn(storage.autoTargetHP.plus)
  autoTargetUi.minus:setOn(storage.autoTargetHP.minus)
end

macro(100, function()

  if not storage.autoTargetHP.plus
  and not storage.autoTargetHP.minus then
    return
  end

  local pos = player:getPosition()

  local creatures = g_map.getSpectatorsInRange(
    pos,
    false,
    1,
    1
  )

  local target = nil

  if storage.autoTargetHP.plus then

    local highestHp = -1

    for _, creature in ipairs(creatures) do
      if creature:isMonster() then

        local hp = creature:getHealthPercent()

        if hp > highestHp then
          highestHp = hp
          target = creature
        end
      end
    end

  elseif storage.autoTargetHP.minus then

    local lowestHp = 101

    for _, creature in ipairs(creatures) do
      if creature:isMonster() then

        local hp = creature:getHealthPercent()

        if hp < lowestHp then
          lowestHp = hp
          target = creature
        end
      end
    end
  end

  if target then
    g_game.attack(target)
  end
end)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: