Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vbot buffer size string (long scripts) #991

Merged
merged 1 commit into from
Dec 15, 2024
Merged

Conversation

kokekanon
Copy link
Collaborator

@kokekanon kokekanon commented Dec 15, 2024

Description

in vbot you use "hotkeys editor" to put codes.
The problem is by default client has a limit of 4096 characters.
usually these codes of vbot are too long.
a flag is created

#ifndef BOT_PROTECTION
        constexpr size_t BUFFER_SIZE = 65536;
#else
        constexpr size_t BUFFER_SIZE = 4096;
#endif

I hate cpp, it was the solution I came up with.

Behavior

Actual

the scripts has string length: 15k
I can't paste it

limit (4096)

fixura

Expected

I can paste it
with constexpr size_t BUFFER_SIZE = 65536;
samecode

Fixes

Discord

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested

CODE TEST
--Made By VivoDibra
--Tested on vBot 4.8 / OTCV8 3.2 rev 4

g_ui.loadUIFromString([[
BotContainer < Panel
  height: 420
  margin-bottom:10

  UIWidget
    id: title
    anchors.top: parent.top
    anchors.left: parent.left
    anchors.right: parent.right
    text-align: center
  
  ScrollablePanel
    id: items
    anchors.fill: parent
    padding-top:20
    vertical-scrollbar: scroll
    layout:
      type: grid
      cell-size: 40 40
      flow: true

  BotSmallScrollBar
    id: scroll
    anchors.top: prev.top
    anchors.bottom: prev.bottom
    anchors.right: parent.right
    step: 10
    pixels-scroll: true

PotionScrollBar < Panel
  height: 28
  margin-top: 3

  UIWidget
    id: text
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.top: parent.top
    text-align: center
    
  HorizontalScrollBar
    id: scroll
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.top: prev.bottom
    margin-top: 3
    minimum: 0
    maximum: 10
    step: 1

PotionTextEdit < Panel
  height: 40
  margin-top: 7

  UIWidget
    id: text
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.top: parent.top
    text-align: center
    
  TextEdit
    id: textEdit
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.top: prev.bottom
    margin-top: 5
    minimum: 0
    maximum: 10
    step: 1
    text-align: center

PotionLabel < Panel
  height: 20
  margin-top: 7

  UIWidget
    id: text
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.top: parent.top
    text-align: center

PotionItem < Panel
  height: 34
  margin-top: 7
  margin-left: 25
  margin-right: 25

  UIWidget
    id: text
    anchors.left: parent.left
    anchors.verticalCenter: next.verticalCenter

  BotItem
    id: item
    anchors.top: parent.top
    anchors.right: parent.right


PotionCheckBox < BotSwitch
  height: 20
  margin-top: 7

PotionWindow < MainWindow
  !text: tr('Potions')
  size: 500 500
  padding: 25

  Label
    anchors.left: parent.left
    anchors.right: parent.horizontalCenter
    anchors.top: parent.top
    text-align: center
    text:

  Label
    anchors.left: parent.horizontalCenter
    anchors.right: parent.right
    anchors.top: parent.top
    text-align: center
    text:

  VerticalScrollBar
    id: contentScroll
    anchors.top: prev.bottom
    margin-top: 3
    anchors.right: parent.right
    anchors.bottom: separator.top
    step: 28
    pixels-scroll: true
    margin-right: -10
    margin-top: 5
    margin-bottom: 5

  ScrollablePanel
    id: content
    anchors.top: prev.top
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.bottom: separator.top
    vertical-scrollbar: contentScroll
    margin-bottom: 10
      
    Panel
      id: left
      anchors.top: parent.top
      anchors.left: parent.left
      anchors.right: parent.horizontalCenter
      margin-top: 5
      margin-left: 10
      margin-right: 10
      layout:
        type: verticalBox
        fit-children: true

    Panel
      id: right
      anchors.top: parent.top
      anchors.left: parent.horizontalCenter
      anchors.right: parent.right
      margin-top: 5
      margin-left: 10
      margin-right: 10
      layout:
        type: verticalBox
        fit-children: true

    VerticalSeparator
      anchors.top: parent.top
      anchors.bottom: parent.bottom
      anchors.left: parent.horizontalCenter

  HorizontalSeparator
    id: separator
    anchors.right: parent.right
    anchors.left: parent.left
    anchors.bottom: closeButton.top
    margin-bottom: 8

  ResizeBorder
    id: bottomResizeBorder
    anchors.fill: separator
    height: 3
    minimum: 260
    maximum: 600
    margin-left: 3
    margin-right: 3
    background: #ffffff88    

  Button
    id: closeButton
    !text: tr('Close')
    font: cipsoftFont
    anchors.right: parent.right
    anchors.bottom: parent.bottom
    size: 45 21
    margin-right: 5
PvPScriptsButton < Panel
  height: 34
  margin-top: 7
  margin-left: 25
  margin-right: 25

  Button
    id: button
    anchors.top: parent.top
    width:150
    anchors.horizontalCenter: parent.horizontalCenter
]])

setDefaultTab("Main")

local PanelName = "Potions"
local ui = setupUI([[
Panel
  height: 19

  BotSwitch
    id: title
    anchors.top: parent.top
    anchors.left: parent.left
    text-align: center
    width: 130
    !text: tr('Potions')

  Button
    id: push
    anchors.top: prev.top
    anchors.left: prev.right
    anchors.right: parent.right
    margin-left: 3
    height: 17
    text: Setup

]])

ui:setId(PanelName)

if not storage[PanelName] then
  storage[PanelName] = {}
end

local settings = storage[PanelName]
local s = { }
Potion = { }

ui.title:setOn(settings.enabled)
ui.title.onClick = function(widget)
  settings.enabled = not settings.enabled
  widget:setOn(settings.enabled)
  if settings.enabled then
    s.scheduleComboTime()
  end
end

Potion.toggle = function()
  ui.title.onClick(ui.title)
end

Potion.setOff = function()
  settings.enabled = false
  ui.title:setOn(settings.enabled)
end

Potion.setOn = function()
  settings.enabled = true
  ui.title:setOn(settings.enabled)
end

ui.push.onClick = function(widget)
  PotionWindow:show()
  PotionWindow:raise()
  PotionWindow:focus()
end

local function botPrintMessage(message)
  modules.game_textmessage.displayGameMessage(message)
end

botPrintMessage("Potions script made by VivoDibra")

PotionWindow = UI.createWindow("PotionWindow", rootWidget)
PotionWindow:hide()
PotionWindow.closeButton.onClick = function(widget)
  PotionWindow:hide()
end

PotionWindow:setHeight(550)
PotionWindow:setWidth(500)

PotionWindow.onGeometryChange = function(widget, old, new)
  if old.height == 0 then return end
  settings.height = new.height
end

local rightPanel = PotionWindow.content.right
local leftPanel = PotionWindow.content.left

local addItem = function(id, title, defaultItem, dest, tooltip)
  local widget = UI.createWidget("PotionItem", dest)
  widget.text:setText(title)
  widget.text:setTooltip(tooltip)
  widget.item:setTooltip(tooltip)
  widget.item:setItemId(settings[id] or defaultItem)
  widget.item.onItemChange = function(widget)
    settings[id] = widget:getItemId()
  end
  settings[id] = settings[id] or defaultItem
end

local addTextEdit = function(id, title, defaultValue, dest, tooltip)
  local widget = UI.createWidget("PotionTextEdit", dest)
  widget.text:setText(title)
  widget.textEdit:setText(settings[id] or defaultValue or "")
  widget.text:setTooltip(tooltip)
  widget.textEdit.onTextChange = function(widget,text)
    settings[id] = text
  end
  settings[id] = settings[id] or defaultValue or ""
end

local addLabel = function(title, dest, mTop )
  local widget = UI.createWidget("PotionLabel", dest)
  widget.text:setText(title)
  widget:setMarginTop(mTop)
end

local addContainer = function(id, title, unique, parent, defaultValue)
  local widget = UI.createWidget("BotContainer", parent)
  widget.title:setText(title)
  local oldItems = {}
  if not settings[id] then
    settings[id] = defaultValue
  end
  local updateItems = function()
    local items = widget:getItems()
    
    local somethingNew = (#items ~= #oldItems)
    for i, item in ipairs(items) do
      if type(oldItems[i]) ~= "table" then
        somethingNew = true
        break
      end
      if oldItems[i].id ~= item.id or oldItems[i].count ~= item.count then
        somethingNew = true
        break      
      end
    end
    
    if somethingNew then
      oldItems = items
      settings[id] = items
    end
    widget:setItems(items)    
  end
  
  widget.setItems = function(self, items)
    if type(self) == 'table' then
      items = self
    end
    local itemsToShow = math.max(10, #items + 2)
    if itemsToShow % 5 ~= 0 then
      itemsToShow = itemsToShow + 5 - itemsToShow % 5
    end
    widget.items:destroyChildren()
    for i = 1, itemsToShow do 
      local widget = g_ui.createWidget("BotItem", widget.items)
      if type(items[i]) == 'number' then
        items[i] = {id=items[i], count=1}
      end
      if type(items[i]) == 'table' then
        widget:setItem(Item.create(items[i].id, items[i].count))
      end
    end
    oldItems = items
    for i, child in ipairs(widget.items:getChildren()) do
      child.onItemChange = updateItems
    end
  end
  
  widget.getItems = function()
    local items = {}
    local duplicates = {}
    for i, child in ipairs(widget.items:getChildren()) do
      if child:getItemId() >= 100 then
        if not duplicates[child:getItemId()] or not unique then
          table.insert(items, {id=child:getItemId(), count=child:getItemCountOrSubType()})
          duplicates[child:getItemId()] = true
        end
      end
    end
    return items
  end
  
  widget:setItems(settings[id])
  
  return widget
end

local addScrollBar = function(id, title, min, max, defaultValue, dest, tooltip)
  local widget = UI.createWidget("PotionScrollBar", dest)
  widget.text:setTooltip(tooltip)
  widget.scroll.onValueChange = function(scroll, value)
    widget.text:setText(title..value)
    settings[id] = value
  end
  widget.scroll:setRange(min, max)
  widget.scroll:setTooltip(tooltip)
  widget.scroll:setValue(settings[id] or defaultValue)
  widget.scroll.onValueChange(widget.scroll, widget.scroll:getValue())
end

local addCheckBox = function(id, title, defaultValue, dest, tooltip)
  local widget = UI.createWidget("PotionCheckBox", dest)
  widget.onClick = function()
    widget:setOn(not widget:isOn())
    settings[id] = widget:isOn()
  end
  widget:setText(title)
  widget:setTooltip(tooltip)
  if settings[id] == nil then
    widget:setOn(defaultValue)
  else
    widget:setOn(settings[id])
  end
  settings[id] = widget:isOn()
end

local myAddbutton = function(btnText, dest, tooltip, callback)
  local widget = UI.createWidget('PvPScriptsButton', dest)
  widget.button:setText(btnText)
  widget.button:setTooltip(tooltip)
  widget.button.onClick = function()
    callback()
  end
end

addLabel("Server Save Time", leftPanel, 0)
addScrollBar("SSHour", "Hours: ", 0, 24, 6, leftPanel, "")
addScrollBar("SSMinute", "Minutes: ", 0, 59, 0, leftPanel, "")

addLabel("Potion Time", leftPanel, 15)
addScrollBar("PotTime", "Minutes: ", 0, 59, 30, leftPanel, "")

addLabel("Use Delay", leftPanel, 15)
addScrollBar("PotUseDelay", "Milliseconds: ", 0, 1000, 800, leftPanel, "")

addLabel("Toggles", leftPanel, 15)
addCheckBox("shouldStopCaveBot","Stop CaveBot", true, leftPanel, "")
addCheckBox("shouldStopAttackBot", "Stop AttackBot", true, leftPanel, "")
addCheckBox("shouldWaitMonsters", "Wait Monsters", true, leftPanel, "")
addCheckBox("shouldWaitPlayers", "Wait Players", true, leftPanel, "")
addCheckBox("PZOnly", "PZ Only", true, leftPanel, "")
addCheckBox("ShouldConsiderSSTime", "Consider SS Time", true, leftPanel, "")
myAddbutton("Use Combo", leftPanel, "", function()
  s.useCombo() 
end)

addContainer("PotionsIds", "Potions", true, rightPanel, { 2118, 2119, 2120, 2121, 2122, 105, 2123, 2124, 2125, 2126, 2127, 2131, 2132, 2133, 2134, 2135})

s.printMessage = function(message)
  modules.game_textmessage.displayGameMessage("[Potions] "..message)
end

s.getContainerItems = function(data)
  local idsTable = {}
  local data = data or {}
  for _, item in ipairs(data) do
    if type(item) ~= "number" then
      table.insert(idsTable, item.id)
    elseif type(item) == "number" then
      table.insert(idsTable, item)
    end
  end
  return idsTable
end

s.useCombo = function()
  local d = 0
  for _, potId in ipairs(s.getContainerItems(settings.PotionsIds)) do
    schedule(d, function()
      g_game.useInventoryItemWith(potId, g_game.getLocalPlayer())
    end)
    d = d + settings.PotUseDelay
  end
end

s.getMonsters = function(range, multifloor)
  if not range then range = 10 end
  local mobs = 0;
  for _, spec in pairs(getSpectators(multifloor)) do
    mobs = (g_game.getClientVersion() < 960 or spec:getType() < 3) and
    spec:isMonster() and distanceFromPlayer(spec:getPosition()) <= range and mobs + 1 or mobs;
  end
  return mobs;
end

s.getPlayers = function(range, multifloor)
  if not range then range = 10 end
  local specs = 0;
  for _, spec in pairs(getSpectators(multifloor)) do
      if not spec:isLocalPlayer() and spec:isPlayer() and distanceFromPlayer(spec:getPosition()) <= range and not ((spec:getShield() ~= 1 and spec:isPartyMember()) or spec:getEmblem() == 1) then
          specs = specs + 1
      end
  end
  return specs;
end

s.getFutureTime = function(minutes)
  local currentTime = os.time()
  local currentHour = tonumber(os.date("%H", currentTime))
  local currentMinute = tonumber(os.date("%M", currentTime))

  return os.time({
    year = os.date("%Y", currentTime),
    month = os.date("%m", currentTime),
    day = os.date("%d", currentTime),
    hour = math.floor((currentHour * 60 + currentMinute + minutes) / 60) % 24,
    min = (currentMinute + minutes) % 60,
    sec = os.date("%S", currentTime)
  })
end

s.ValidSSTime = function()
  local currentTime = os.time()
  local currentHour = tonumber(os.date("%H", currentTime))
  local currentMinute = tonumber(os.date("%M", currentTime))
  local hourToCheck = settings.SSHour
  local minuteToCheck = settings.SSMinute
  local potMinutes = settings.PotTime
  local futureTime = s.getFutureTime(potMinutes)

  local timeToCheck = os.time({
    year = os.date("%Y", currentTime),
    month = os.date("%m", currentTime),
    day = os.date("%d", currentTime),
    hour = hourToCheck,
    min = minuteToCheck,
    sec = os.date("%S", currentTime)
  })

  if currentTime > timeToCheck then return true end

  return futureTime < timeToCheck
end

s.canUseCombo = function()
  local isMonstersOk = not settings.shouldWaitMonsters or s.getMonsters(8, false) == 0
  local isPlayersOk = not settings.shouldWaitPlayers or s.getPlayers(8, false) == 0
  local isPzOk = not settings.PZOnly or isInPz()
  local isSSTimeOk = not settings.ShouldConsiderSSTime or s.ValidSSTime()
  local isComboUseTimeOk = os.time() > settings.comboTime

  return isMonstersOk and isPlayersOk and isPzOk and isSSTimeOk and isComboUseTimeOk and settings.enabled
end

s.scheduleComboTime = function()
  settings.comboTime = s.getFutureTime(settings.PotTime)
  s.printMessage("Proximo combo agendado para daqui a "..settings.PotTime.." minutos.")
end

if not settings.comboTime then
  s.scheduleComboTime()
end

s.getComboUseDelay = function()
  return #s.getContainerItems(settings.PotionsIds) * settings.PotUseDelay
end

s.pauseMacro = function(m)
  if m.isOn() then
    m.setOff()
    schedule(s.getComboUseDelay(), function()
      m.setOn()
    end)
  end
end

s.pauseMacros = function()
  local a = not settings.shouldStopCaveBot or s.pauseMacro(CaveBot)
  local b = not settings.shouldStopAttackBot or s.pauseMacro(AttackBot)
end

s.m_main = macro(200, function()  
  if s.canUseCombo() then
    s.pauseMacros()
    s.useCombo()
    s.scheduleComboTime()
    delay(s.getComboUseDelay())
  end
end)

addButton("", "+ Free Scripts", function()
    g_platform.openUrl("https://discord.gg/RkQ9nyPMBH")
end)


--Made By VivoDibra
--Tested on vBot 4.8 / OTCV8 3.2 rev 4

Test Configuration:

  • Server Version: canary 1340
  • Client: this pr
  • Operating System: win11

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

@mehah mehah merged commit daf394a into mehah:main Dec 15, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants